Skip to main content

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::{Application, Window, ffi, prelude::*, subclass::prelude::*};
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                    unsafe {
79                        if let Some(WrapFn(old_startup)) = OLD_STARTUP {
80                            old_startup(app);
81                        }
82                        crate::rt::set_initialized();
83                    }
84                }
85
86                app_klass.startup = Some(replace_startup);
87
88                glib::gobject_ffi::g_type_class_unref(base_klass);
89            });
90        }
91
92        Self::parent_class_init::<T>(class);
93
94        let klass = class.as_mut();
95        klass.window_added = Some(application_window_added::<T>);
96        klass.window_removed = Some(application_window_removed::<T>);
97    }
98}
99
100unsafe extern "C" fn application_window_added<T: GtkApplicationImpl>(
101    ptr: *mut ffi::GtkApplication,
102    wptr: *mut ffi::GtkWindow,
103) {
104    unsafe {
105        let instance = &*(ptr as *mut T::Instance);
106        let imp = instance.imp();
107
108        imp.window_added(&from_glib_borrow(wptr))
109    }
110}
111
112unsafe extern "C" fn application_window_removed<T: GtkApplicationImpl>(
113    ptr: *mut ffi::GtkApplication,
114    wptr: *mut ffi::GtkWindow,
115) {
116    unsafe {
117        let instance = &*(ptr as *mut T::Instance);
118        let imp = instance.imp();
119
120        imp.window_removed(&from_glib_borrow(wptr))
121    }
122}