gtk4/subclass/
actionable.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 implementing the [`Actionable`] interface.
5
6use glib::{translate::*, GString, Variant};
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Actionable};
9
10pub trait ActionableImpl: WidgetImpl + ObjectSubclass<Type: IsA<Actionable>> {
11    #[doc(alias = "get_action_name")]
12    fn action_name(&self) -> Option<GString>;
13    #[doc(alias = "get_action_target_value")]
14    fn action_target_value(&self) -> Option<Variant>;
15    fn set_action_name(&self, name: Option<&str>);
16    fn set_action_target_value(&self, value: Option<&Variant>);
17}
18
19pub trait ActionableImplExt: ActionableImpl {
20    fn parent_action_name(&self) -> Option<GString> {
21        unsafe {
22            let type_data = Self::type_data();
23            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
24                as *const ffi::GtkActionableInterface;
25
26            let func = (*parent_iface)
27                .get_action_name
28                .expect("no parent \"get_action_name\" implementation");
29
30            from_glib_none(func(
31                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
32            ))
33        }
34    }
35
36    fn parent_action_target_value(&self) -> Option<Variant> {
37        unsafe {
38            let type_data = Self::type_data();
39            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
40                as *const ffi::GtkActionableInterface;
41
42            let func = (*parent_iface)
43                .get_action_target_value
44                .expect("no parent \"get_action_target_value\" implementation");
45
46            from_glib_none(func(
47                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
48            ))
49        }
50    }
51
52    fn parent_set_action_name(&self, name: Option<&str>) {
53        unsafe {
54            let type_data = Self::type_data();
55            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
56                as *const ffi::GtkActionableInterface;
57
58            let func = (*parent_iface)
59                .set_action_name
60                .expect("no parent \"set_action_name\" implementation");
61
62            func(
63                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
64                name.to_glib_none().0,
65            );
66        }
67    }
68
69    fn parent_set_action_target_value(&self, value: Option<&Variant>) {
70        unsafe {
71            let type_data = Self::type_data();
72            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
73                as *const ffi::GtkActionableInterface;
74
75            let func = (*parent_iface)
76                .set_action_target_value
77                .expect("no parent \"set_action_target_value\" implementation");
78
79            func(
80                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
81                value.to_glib_none().0,
82            );
83        }
84    }
85}
86
87impl<T: ActionableImpl> ActionableImplExt for T {}
88
89unsafe impl<T: ActionableImpl> IsImplementable<T> for Actionable {
90    fn interface_init(iface: &mut glib::Interface<Self>) {
91        let iface = iface.as_mut();
92
93        iface.get_action_name = Some(actionable_get_action_name::<T>);
94        iface.get_action_target_value = Some(actionable_get_action_target_value::<T>);
95        iface.set_action_name = Some(actionable_set_action_name::<T>);
96        iface.set_action_target_value = Some(actionable_set_action_target_value::<T>);
97    }
98}
99
100unsafe extern "C" fn actionable_get_action_name<T: ActionableImpl>(
101    actionable: *mut ffi::GtkActionable,
102) -> *const libc::c_char {
103    let instance = &*(actionable as *mut T::Instance);
104    let imp = instance.imp();
105
106    imp.action_name().into_glib_ptr()
107}
108
109unsafe extern "C" fn actionable_get_action_target_value<T: ActionableImpl>(
110    actionable: *mut ffi::GtkActionable,
111) -> *mut glib::ffi::GVariant {
112    let instance = &*(actionable as *mut T::Instance);
113    let imp = instance.imp();
114
115    imp.action_target_value().into_glib_ptr()
116}
117
118unsafe extern "C" fn actionable_set_action_name<T: ActionableImpl>(
119    actionable: *mut ffi::GtkActionable,
120    name: *const libc::c_char,
121) {
122    let instance = &*(actionable as *mut T::Instance);
123    let imp = instance.imp();
124    let name: Borrowed<Option<GString>> = from_glib_borrow(name);
125    imp.set_action_name(name.as_ref().as_ref().map(|s| s.as_str()))
126}
127
128unsafe extern "C" fn actionable_set_action_target_value<T: ActionableImpl>(
129    actionable: *mut ffi::GtkActionable,
130    value: *mut glib::ffi::GVariant,
131) {
132    let instance = &*(actionable as *mut T::Instance);
133    let imp = instance.imp();
134    let val: Borrowed<Option<Variant>> = from_glib_borrow(value);
135
136    imp.set_action_target_value(val.as_ref().as_ref())
137}