gtk/subclass/
application.rs1use glib::object::Cast;
4
5use glib::translate::*;
6
7use glib::object::IsA;
8use glib::subclass::prelude::*;
9
10use crate::Window;
11use crate::{Application, ffi};
12
13pub trait GtkApplicationImpl:
14 gio::subclass::prelude::ApplicationImpl + ObjectSubclass<Type: IsA<Application>>
15{
16 fn window_added(&self, window: &Window) {
17 self.parent_window_added(window)
18 }
19
20 fn window_removed(&self, window: &Window) {
21 self.parent_window_removed(window)
22 }
23}
24
25pub trait GtkApplicationImplExt: GtkApplicationImpl {
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 fn parent_window_removed(&self, window: &Window) {
39 unsafe {
40 let data = Self::type_data();
41 let parent_class = data.as_ref().parent_class() as *mut ffi::GtkApplicationClass;
42 if let Some(f) = (*parent_class).window_removed {
43 f(
44 self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
45 window.to_glib_none().0,
46 )
47 }
48 }
49 }
50}
51
52impl<T: GtkApplicationImpl> GtkApplicationImplExt for T {}
53
54unsafe impl<T: GtkApplicationImpl> IsSubclassable<T> for Application {
55 fn class_init(class: &mut ::glib::Class<Self>) {
56 {
59 use std::sync;
60
61 struct WrapFn(unsafe extern "C" fn(*mut gio::ffi::GApplication));
64 unsafe impl Send for WrapFn {}
65 unsafe impl Sync for WrapFn {}
66
67 static ONCE: sync::Once = sync::Once::new();
68 static mut OLD_STARTUP: Option<WrapFn> = None;
69
70 ONCE.call_once(|| unsafe {
73 let base_klass =
74 glib::gobject_ffi::g_type_class_ref(ffi::gtk_application_get_type());
75 assert!(!base_klass.is_null());
76
77 let app_klass = &mut *(base_klass as *mut gio::ffi::GApplicationClass);
78 OLD_STARTUP = app_klass.startup.map(WrapFn);
79
80 unsafe extern "C" fn replace_startup(app: *mut gio::ffi::GApplication) {
81 unsafe {
82 if let Some(WrapFn(old_startup)) = OLD_STARTUP {
83 old_startup(app);
84 }
85 crate::rt::set_initialized();
86 }
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 unsafe {
108 let instance = &*(ptr as *mut T::Instance);
109 let imp = instance.imp();
110
111 imp.window_added(&from_glib_borrow(wptr))
112 }
113}
114unsafe extern "C" fn application_window_removed<T: GtkApplicationImpl>(
115 ptr: *mut ffi::GtkApplication,
116 wptr: *mut ffi::GtkWindow,
117) {
118 unsafe {
119 let instance = &*(ptr as *mut T::Instance);
120 let imp = instance.imp();
121
122 imp.window_removed(&from_glib_borrow(wptr))
123 }
124}