gtk4/subclass/
application_window.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 [`ApplicationWindow`].
5
6use crate::{prelude::*, subclass::prelude::*, ApplicationWindow};
7
8#[cfg(feature = "v4_22")]
9use crate::ffi;
10
11#[cfg(feature = "v4_22")]
12use glib::translate::*;
13
14pub trait ApplicationWindowImpl:
15    WindowImpl
16    + ObjectSubclass<Type: IsA<ApplicationWindow> + IsA<gio::ActionGroup> + IsA<gio::ActionMap>>
17    + 'static
18{
19    /// Class closure for the [`save-state`][struct@crate::ApplicationWindow#save-state] signal.
20    /// ## `dict`
21    /// a dictionary where to store the window's state
22    ///
23    /// # Returns
24    ///
25    /// true to stop stop further handlers from running
26    #[cfg(feature = "v4_22")]
27    #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
28    fn save_state(&self, state: &glib::VariantDict) -> bool {
29        self.parent_save_state(state)
30    }
31}
32
33pub trait ApplicationWindowImplExt: ApplicationWindowImpl {
34    #[cfg(feature = "v4_22")]
35    #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
36    fn parent_save_state(&self, state: &glib::VariantDict) -> bool {
37        unsafe {
38            let data = Self::type_data();
39            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkApplicationWindowClass;
40            if let Some(f) = (*parent_class).save_state {
41                from_glib(f(
42                    self.obj()
43                        .unsafe_cast_ref::<ApplicationWindow>()
44                        .to_glib_none()
45                        .0,
46                    state.to_glib_none().0,
47                ))
48            } else {
49                false
50            }
51        }
52    }
53}
54
55impl<T: ApplicationWindowImpl> ApplicationWindowImplExt for T {}
56
57unsafe impl<T: ApplicationWindowImpl> IsSubclassable<T> for ApplicationWindow {
58    fn class_init(class: &mut ::glib::Class<Self>) {
59        Self::parent_class_init::<T>(class);
60
61        #[cfg(feature = "v4_22")]
62        {
63            let klass = class.as_mut();
64            klass.save_state = Some(application_window_save_state::<T>);
65        }
66    }
67}
68
69#[cfg(feature = "v4_22")]
70unsafe extern "C" fn application_window_save_state<T: ApplicationWindowImpl>(
71    ptr: *mut ffi::GtkApplicationWindow,
72    state: *mut glib::ffi::GVariantDict,
73) -> glib::ffi::gboolean {
74    let instance = &*(ptr as *mut T::Instance);
75    let imp = instance.imp();
76
77    imp.save_state(&from_glib_borrow(state)).into_glib()
78}