Skip to main content

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::{GString, Variant, translate::*};
7
8use crate::{Actionable, ffi, prelude::*, subclass::prelude::*};
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    unsafe {
104        let instance = &*(actionable as *mut T::Instance);
105        let imp = instance.imp();
106
107        imp.action_name().into_glib_ptr()
108    }
109}
110
111unsafe extern "C" fn actionable_get_action_target_value<T: ActionableImpl>(
112    actionable: *mut ffi::GtkActionable,
113) -> *mut glib::ffi::GVariant {
114    unsafe {
115        let instance = &*(actionable as *mut T::Instance);
116        let imp = instance.imp();
117
118        imp.action_target_value().into_glib_ptr()
119    }
120}
121
122unsafe extern "C" fn actionable_set_action_name<T: ActionableImpl>(
123    actionable: *mut ffi::GtkActionable,
124    name: *const libc::c_char,
125) {
126    unsafe {
127        let instance = &*(actionable as *mut T::Instance);
128        let imp = instance.imp();
129        let name: Borrowed<Option<GString>> = from_glib_borrow(name);
130        imp.set_action_name(name.as_ref().as_ref().map(|s| s.as_str()))
131    }
132}
133
134unsafe extern "C" fn actionable_set_action_target_value<T: ActionableImpl>(
135    actionable: *mut ffi::GtkActionable,
136    value: *mut glib::ffi::GVariant,
137) {
138    unsafe {
139        let instance = &*(actionable as *mut T::Instance);
140        let imp = instance.imp();
141        let val: Borrowed<Option<Variant>> = from_glib_borrow(value);
142
143        imp.set_action_target_value(val.as_ref().as_ref())
144    }
145}