gtk4/subclass/
application.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 [`Application`](crate::Application).
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Application, Window};
9
10pub trait GtkApplicationImpl: GtkApplicationImplExt + ApplicationImpl {
11    fn window_added(&self, window: &Window) {
12        self.parent_window_added(window)
13    }
14
15    fn window_removed(&self, window: &Window) {
16        self.parent_window_removed(window)
17    }
18}
19
20mod sealed {
21    pub trait Sealed {}
22    impl<T: super::GtkApplicationImplExt> Sealed for T {}
23}
24
25pub trait GtkApplicationImplExt: sealed::Sealed + ObjectSubclass {
26    fn parent_window_added(&self, window: &Window) {
27        unsafe {
28            let data = Self::type_data();
29            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkApplicationClass;
30            if let Some(f) = (*parent_class).window_added {
31                f(
32                    self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
33                    window.to_glib_none().0,
34                )
35            }
36        }
37    }
38
39    fn parent_window_removed(&self, window: &Window) {
40        unsafe {
41            let data = Self::type_data();
42            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkApplicationClass;
43            if let Some(f) = (*parent_class).window_removed {
44                f(
45                    self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
46                    window.to_glib_none().0,
47                )
48            }
49        }
50    }
51}
52
53impl<T: GtkApplicationImpl> GtkApplicationImplExt for T {}
54
55unsafe impl<T: GtkApplicationImpl> IsSubclassable<T> for Application {
56    fn class_init(class: &mut ::glib::Class<Self>) {
57        // Override the original `GtkApplication` startup implementation so that
58        // we can set GTK as initialized right afterwards.
59        {
60            use std::sync;
61
62            // Needed because the function pointer is not `Send+Sync` otherwise but has to
63            // be to be stored in a `static mut`.
64            struct WrapFn(unsafe extern "C" fn(*mut gio::ffi::GApplication));
65            unsafe impl Send for WrapFn {}
66            unsafe impl Sync for WrapFn {}
67
68            static ONCE: sync::Once = sync::Once::new();
69            static mut OLD_STARTUP: Option<WrapFn> = None;
70
71            // One the very first call replace the original `GtkApplication` startup with a
72            // function that first calls the original one and then marks gtk-rs as
73            // initialized.
74            ONCE.call_once(|| unsafe {
75                let base_klass =
76                    glib::gobject_ffi::g_type_class_ref(ffi::gtk_application_get_type());
77                debug_assert!(!base_klass.is_null());
78
79                let app_klass = &mut *(base_klass as *mut gio::ffi::GApplicationClass);
80                OLD_STARTUP = app_klass.startup.map(WrapFn);
81
82                unsafe extern "C" fn replace_startup(app: *mut gio::ffi::GApplication) {
83                    if let Some(WrapFn(old_startup)) = OLD_STARTUP {
84                        old_startup(app);
85                    }
86                    crate::rt::set_initialized();
87                }
88
89                app_klass.startup = Some(replace_startup);
90
91                glib::gobject_ffi::g_type_class_unref(base_klass);
92            });
93        }
94
95        Self::parent_class_init::<T>(class);
96
97        let klass = class.as_mut();
98        klass.window_added = Some(application_window_added::<T>);
99        klass.window_removed = Some(application_window_removed::<T>);
100    }
101}
102
103unsafe extern "C" fn application_window_added<T: GtkApplicationImpl>(
104    ptr: *mut ffi::GtkApplication,
105    wptr: *mut ffi::GtkWindow,
106) {
107    let instance = &*(ptr as *mut T::Instance);
108    let imp = instance.imp();
109
110    imp.window_added(&from_glib_borrow(wptr))
111}
112
113unsafe extern "C" fn application_window_removed<T: GtkApplicationImpl>(
114    ptr: *mut ffi::GtkApplication,
115    wptr: *mut ffi::GtkWindow,
116) {
117    let instance = &*(ptr as *mut T::Instance);
118    let imp = instance.imp();
119
120    imp.window_removed(&from_glib_borrow(wptr))
121}