1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
use gio::ApplicationExt; use gio::ApplicationFlags; use glib; use glib::signal::SignalHandlerId; use glib::translate::*; use glib::ObjectExt; use gtk_sys; use rt; use Application; use std::cell::RefCell; use std::rc::Rc; impl Application { pub(crate) fn register_startup_hook(app: &Application) { skip_assert_initialized!(); let signalid: Rc<RefCell<Option<SignalHandlerId>>> = Rc::new(RefCell::new(None)); { let signalid_ = signalid.clone(); let id = app.connect_startup(move |app| { app.disconnect( signalid_ .borrow_mut() .take() .expect("Signal ID went missing"), ); unsafe { rt::set_initialized() } }); *signalid.borrow_mut() = Some(id); } } /// Creates a new `Application` instance. /// /// When using `Application`, it is not necessary to call `gtk_init` /// manually. It is called as soon as the application gets registered as /// the primary instance. /// /// Concretely, `gtk_init` is called in the default handler for the /// `gio::Application::startup` signal. Therefore, `Application` subclasses should /// chain up in their `gio::Application::startup` handler before using any GTK+ API. /// /// Note that commandline arguments are not passed to `gtk_init`. /// All GTK+ functionality that is available via commandline arguments /// can also be achieved by setting suitable environment variables /// such as `G_DEBUG`, so this should not be a big /// problem. If you absolutely must support GTK+ commandline arguments, /// you can explicitly call `gtk_init` before creating the application /// instance. /// /// If non-`None`, the application ID must be valid. See /// `gio::Application::id_is_valid`. /// /// If no application ID is given then some features (most notably application /// uniqueness) will be disabled. A null application ID is only allowed with /// GTK+ 3.6 or later. /// ## `application_id` /// The application ID. /// ## `flags` /// the application flags /// /// # Returns /// /// a new `Application` instance pub fn new( application_id: Option<&str>, flags: ApplicationFlags, ) -> Result<Application, glib::BoolError> { skip_assert_initialized!(); let app: Application = unsafe { Option::from_glib_full(gtk_sys::gtk_application_new( application_id.to_glib_none().0, flags.to_glib(), )) .ok_or_else(|| glib_bool_error!("Failed to create application"))? }; Application::register_startup_hook(&app); Ok(app) } }