1use std::{boxed::Box as Box_, mem::transmute};
4
5use glib::{
6 prelude::*,
7 signal::{connect_raw, SignalHandlerId},
8 translate::*,
9 ExitCode, GString,
10};
11
12use crate::{ffi, Application, File};
13
14mod sealed {
15 pub trait Sealed {}
16 impl<T: super::IsA<super::Application>> Sealed for T {}
17}
18
19pub trait ApplicationExtManual: sealed::Sealed + IsA<Application> {
20 #[doc(alias = "g_application_run")]
21 fn run(&self) -> ExitCode {
22 self.run_with_args(&std::env::args().collect::<Vec<_>>())
23 }
24 #[doc(alias = "g_application_run")]
25 fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> ExitCode {
26 let argv: Vec<&str> = args.iter().map(|a| a.as_ref()).collect();
27 let argc = argv.len() as i32;
28 let exit_code = unsafe {
29 ffi::g_application_run(self.as_ref().to_glib_none().0, argc, argv.to_glib_none().0)
30 };
31 ExitCode::from(exit_code)
32 }
33 fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId {
34 unsafe extern "C" fn open_trampoline<P, F: Fn(&P, &[File], &str) + 'static>(
35 this: *mut ffi::GApplication,
36 files: *const *mut ffi::GFile,
37 n_files: libc::c_int,
38 hint: *mut libc::c_char,
39 f: glib::ffi::gpointer,
40 ) where
41 P: IsA<Application>,
42 {
43 let f: &F = &*(f as *const F);
44 let files: Vec<File> = FromGlibContainer::from_glib_none_num(files, n_files as usize);
45 f(
46 Application::from_glib_borrow(this).unsafe_cast_ref(),
47 &files,
48 &GString::from_glib_borrow(hint),
49 )
50 }
51 unsafe {
52 let f: Box_<F> = Box_::new(f);
53 connect_raw(
54 self.as_ptr() as *mut _,
55 b"open\0".as_ptr() as *const _,
56 Some(transmute::<*const (), unsafe extern "C" fn()>(
57 open_trampoline::<Self, F> as *const (),
58 )),
59 Box_::into_raw(f),
60 )
61 }
62 }
63
64 #[doc(alias = "g_application_hold")]
65 fn hold(&self) -> ApplicationHoldGuard {
66 unsafe {
67 ffi::g_application_hold(self.as_ref().to_glib_none().0);
68 }
69 ApplicationHoldGuard(self.as_ref().downgrade())
70 }
71
72 #[doc(alias = "g_application_mark_busy")]
73 fn mark_busy(&self) -> ApplicationBusyGuard {
74 unsafe {
75 ffi::g_application_mark_busy(self.as_ref().to_glib_none().0);
76 }
77 ApplicationBusyGuard(self.as_ref().downgrade())
78 }
79}
80
81impl<O: IsA<Application>> ApplicationExtManual for O {}
82
83#[derive(Debug)]
84#[must_use = "if unused the Application will immediately be released"]
85pub struct ApplicationHoldGuard(glib::WeakRef<Application>);
86
87impl Drop for ApplicationHoldGuard {
88 #[inline]
89 fn drop(&mut self) {
90 if let Some(application) = self.0.upgrade() {
91 unsafe {
92 ffi::g_application_release(application.to_glib_none().0);
93 }
94 }
95 }
96}
97
98#[derive(Debug)]
99#[must_use = "if unused the Application will immediately be unmarked busy"]
100pub struct ApplicationBusyGuard(glib::WeakRef<Application>);
101
102impl Drop for ApplicationBusyGuard {
103 #[inline]
104 fn drop(&mut self) {
105 if let Some(application) = self.0.upgrade() {
106 unsafe {
107 ffi::g_application_unmark_busy(application.to_glib_none().0);
108 }
109 }
110 }
111}