gtk4/subclass/
window.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`Window`](crate::Window).

use glib::translate::*;

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

pub trait WindowImpl: WindowImplExt + WidgetImpl {
    /// Activates the current focused widget within the window.
    fn activate_focus(&self) {
        self.parent_activate_focus()
    }

    /// Activates the default widget for the window.
    fn activate_default(&self) {
        self.parent_activate_default()
    }

    /// Signal gets emitted when the set of accelerators or
    ///   mnemonics that are associated with window changes.
    fn keys_changed(&self) {
        self.parent_keys_changed()
    }

    /// Class handler for the `GtkWindow::enable-debugging`
    ///   keybinding signal.
    fn enable_debugging(&self, toggle: bool) -> bool {
        self.parent_enable_debugging(toggle)
    }

    /// Class handler for the `GtkWindow::close-request` signal.
    fn close_request(&self) -> glib::Propagation {
        self.parent_close_request()
    }
}

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

pub trait WindowImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_activate_focus(&self) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkWindowClass;
            let f = (*parent_class)
                .activate_focus
                .expect("No parent class impl for \"activate_focus\"");
            f(self.obj().unsafe_cast_ref::<Window>().to_glib_none().0)
        }
    }

    fn parent_activate_default(&self) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkWindowClass;
            let f = (*parent_class)
                .activate_default
                .expect("No parent class impl for \"activate_default\"");
            f(self.obj().unsafe_cast_ref::<Window>().to_glib_none().0)
        }
    }

    fn parent_keys_changed(&self) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkWindowClass;
            let f = (*parent_class)
                .keys_changed
                .expect("No parent class impl for \"keys_changed\"");
            f(self.obj().unsafe_cast_ref::<Window>().to_glib_none().0)
        }
    }

    // Returns true if debugging (inspector) should be enabled, false otherwise
    fn parent_enable_debugging(&self, toggle: bool) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkWindowClass;
            let f = (*parent_class)
                .enable_debugging
                .expect("No parent class impl for \"enable_debugging\"");
            from_glib(f(
                self.obj().unsafe_cast_ref::<Window>().to_glib_none().0,
                toggle.into_glib(),
            ))
        }
    }

    fn parent_close_request(&self) -> glib::Propagation {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkWindowClass;
            let f = (*parent_class)
                .close_request
                .expect("No parent class impl for \"close_request\"");
            glib::Propagation::from_glib(f(self.obj().unsafe_cast_ref::<Window>().to_glib_none().0))
        }
    }
}

impl<T: WindowImpl> WindowImplExt for T {}

unsafe impl<T: WindowImpl> IsSubclassable<T> for Window {
    fn class_init(class: &mut ::glib::Class<Self>) {
        Self::parent_class_init::<T>(class);

        let klass = class.as_mut();
        klass.activate_focus = Some(window_activate_focus::<T>);
        klass.activate_default = Some(window_activate_default::<T>);
        klass.keys_changed = Some(window_keys_changed::<T>);
        klass.enable_debugging = Some(window_enable_debugging::<T>);
        klass.close_request = Some(window_close_request::<T>);
    }
}

unsafe extern "C" fn window_activate_focus<T: WindowImpl>(ptr: *mut ffi::GtkWindow) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.activate_focus()
}

unsafe extern "C" fn window_activate_default<T: WindowImpl>(ptr: *mut ffi::GtkWindow) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.activate_default()
}

unsafe extern "C" fn window_keys_changed<T: WindowImpl>(ptr: *mut ffi::GtkWindow) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.keys_changed()
}

unsafe extern "C" fn window_enable_debugging<T: WindowImpl>(
    ptr: *mut ffi::GtkWindow,
    toggleptr: glib::ffi::gboolean,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let toggle: bool = from_glib(toggleptr);

    imp.enable_debugging(toggle).into_glib()
}

unsafe extern "C" fn window_close_request<T: WindowImpl>(
    ptr: *mut ffi::GtkWindow,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.close_request().into_glib()
}