gtk4/subclass/
scale_button.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for subclassing [`ScaleButton`](crate::ScaleButton).
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, ScaleButton};
9
10pub trait ScaleButtonImpl: ScaleButtonImplExt + WidgetImpl {
11    fn value_changed(&self, new_value: f64) {
12        self.parent_value_changed(new_value)
13    }
14}
15
16mod sealed {
17    pub trait Sealed {}
18    impl<T: super::ScaleButtonImplExt> Sealed for T {}
19}
20
21pub trait ScaleButtonImplExt: sealed::Sealed + ObjectSubclass {
22    fn parent_value_changed(&self, new_value: f64) {
23        unsafe {
24            let data = Self::type_data();
25            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScaleButtonClass;
26            if let Some(f) = (*parent_class).value_changed {
27                f(
28                    self.obj().unsafe_cast_ref::<ScaleButton>().to_glib_none().0,
29                    new_value,
30                )
31            }
32        }
33    }
34}
35
36impl<T: ScaleButtonImpl> ScaleButtonImplExt for T {}
37
38unsafe impl<T: ScaleButtonImpl> IsSubclassable<T> for ScaleButton {
39    fn class_init(class: &mut glib::Class<Self>) {
40        Self::parent_class_init::<T>(class);
41
42        let klass = class.as_mut();
43        klass.value_changed = Some(scale_button_value_changed::<T>);
44    }
45}
46
47unsafe extern "C" fn scale_button_value_changed<T: ScaleButtonImpl>(
48    ptr: *mut ffi::GtkScaleButton,
49    new_value: f64,
50) {
51    let instance = &*(ptr as *mut T::Instance);
52    let imp = instance.imp();
53
54    imp.value_changed(new_value)
55}