gtk4/application.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{cell::RefCell, rc::Rc};
4
5use gio::ApplicationFlags;
6use glib::{signal::SignalHandlerId, translate::*};
7
8use crate::{prelude::*, rt, Application};
9
10impl Application {
11 #[doc(alias = "gtk_application_new")]
12 pub fn new(application_id: impl IntoOptionalGStr, flags: ApplicationFlags) -> Self {
13 skip_assert_initialized!();
14 let app: Application = unsafe {
15 application_id.run_with_gstr(|application_id| {
16 from_glib_full(crate::ffi::gtk_application_new(
17 application_id.to_glib_none().0,
18 flags.into_glib(),
19 ))
20 })
21 };
22 Self::register_startup_hook(&app);
23 app
24 }
25
26 pub(crate) fn register_startup_hook(app: &Application) {
27 skip_assert_initialized!();
28 let signalid: Rc<RefCell<Option<SignalHandlerId>>> = Rc::new(RefCell::new(None));
29 {
30 let signalid_ = signalid.clone();
31
32 let id = app.connect_startup(move |app| {
33 app.disconnect(
34 signalid_
35 .borrow_mut()
36 .take()
37 .expect("Signal ID went missing"),
38 );
39 unsafe { rt::set_initialized() }
40 });
41 *signalid.borrow_mut() = Some(id);
42 }
43 }
44}
45
46impl Default for Application {
47 fn default() -> Self {
48 let app = glib::object::Object::new::<Self>();
49 Self::register_startup_hook(&app);
50 app
51 }
52}