gtk4/subclass/
actionable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for implementing the [`Actionable`](crate::Actionable)
//! interface.

use glib::{translate::*, GString, Variant};

use crate::{ffi, prelude::*, subclass::prelude::*, Actionable};

pub trait ActionableImpl: WidgetImpl {
    #[doc(alias = "get_action_name")]
    fn action_name(&self) -> Option<GString>;
    #[doc(alias = "get_action_target_value")]
    fn action_target_value(&self) -> Option<Variant>;
    fn set_action_name(&self, name: Option<&str>);
    fn set_action_target_value(&self, value: Option<&Variant>);
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::ActionableImplExt> Sealed for T {}
}

pub trait ActionableImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_action_name(&self) -> Option<GString> {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
                as *const ffi::GtkActionableInterface;

            let func = (*parent_iface)
                .get_action_name
                .expect("no parent \"get_action_name\" implementation");

            from_glib_none(func(
                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
            ))
        }
    }

    fn parent_action_target_value(&self) -> Option<Variant> {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
                as *const ffi::GtkActionableInterface;

            let func = (*parent_iface)
                .get_action_target_value
                .expect("no parent \"get_action_target_value\" implementation");

            from_glib_none(func(
                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
            ))
        }
    }

    fn parent_set_action_name(&self, name: Option<&str>) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
                as *const ffi::GtkActionableInterface;

            let func = (*parent_iface)
                .set_action_name
                .expect("no parent \"set_action_name\" implementation");

            func(
                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
                name.to_glib_none().0,
            );
        }
    }

    fn parent_set_action_target_value(&self, value: Option<&Variant>) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Actionable>()
                as *const ffi::GtkActionableInterface;

            let func = (*parent_iface)
                .set_action_target_value
                .expect("no parent \"set_action_target_value\" implementation");

            func(
                self.obj().unsafe_cast_ref::<Actionable>().to_glib_none().0,
                value.to_glib_none().0,
            );
        }
    }
}

impl<T: ActionableImpl> ActionableImplExt for T {}

unsafe impl<T: ActionableImpl> IsImplementable<T> for Actionable {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        iface.get_action_name = Some(actionable_get_action_name::<T>);
        iface.get_action_target_value = Some(actionable_get_action_target_value::<T>);
        iface.set_action_name = Some(actionable_set_action_name::<T>);
        iface.set_action_target_value = Some(actionable_set_action_target_value::<T>);
    }
}

unsafe extern "C" fn actionable_get_action_name<T: ActionableImpl>(
    actionable: *mut ffi::GtkActionable,
) -> *const libc::c_char {
    let instance = &*(actionable as *mut T::Instance);
    let imp = instance.imp();

    imp.action_name().into_glib_ptr()
}

unsafe extern "C" fn actionable_get_action_target_value<T: ActionableImpl>(
    actionable: *mut ffi::GtkActionable,
) -> *mut glib::ffi::GVariant {
    let instance = &*(actionable as *mut T::Instance);
    let imp = instance.imp();

    imp.action_target_value().into_glib_ptr()
}

unsafe extern "C" fn actionable_set_action_name<T: ActionableImpl>(
    actionable: *mut ffi::GtkActionable,
    name: *const libc::c_char,
) {
    let instance = &*(actionable as *mut T::Instance);
    let imp = instance.imp();
    let name: Borrowed<Option<GString>> = from_glib_borrow(name);
    imp.set_action_name(name.as_ref().as_ref().map(|s| s.as_str()))
}

unsafe extern "C" fn actionable_set_action_target_value<T: ActionableImpl>(
    actionable: *mut ffi::GtkActionable,
    value: *mut glib::ffi::GVariant,
) {
    let instance = &*(actionable as *mut T::Instance);
    let imp = instance.imp();
    let val: Borrowed<Option<Variant>> = from_glib_borrow(value);

    imp.set_action_target_value(val.as_ref().as_ref())
}