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