1use std::{boxed::Box as Box_, mem::transmute, ops::ControlFlow};
4
5use glib::{
6 prelude::*,
7 signal::{connect_raw, SignalHandlerId},
8 translate::*,
9 ExitCode, GString,
10};
11
12use crate::{ffi, Application, ApplicationCommandLine, File};
13
14pub trait ApplicationExtManual: IsA<Application> {
15 #[doc(alias = "g_application_run")]
16 fn run(&self) -> ExitCode {
17 self.run_with_args(&std::env::args().collect::<Vec<_>>())
18 }
19
20 #[doc(alias = "g_application_run")]
21 fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> ExitCode {
22 let argv: Vec<&str> = args.iter().map(|a| a.as_ref()).collect();
23 let argc = argv.len() as i32;
24 let exit_code = unsafe {
25 ffi::g_application_run(self.as_ref().to_glib_none().0, argc, argv.to_glib_none().0)
26 };
27 ExitCode::try_from(exit_code).unwrap()
28 }
29
30 #[doc(alias = "open")]
31 fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId {
32 unsafe extern "C" fn open_trampoline<P, F: Fn(&P, &[File], &str) + 'static>(
33 this: *mut ffi::GApplication,
34 files: *const *mut ffi::GFile,
35 n_files: libc::c_int,
36 hint: *mut libc::c_char,
37 f: glib::ffi::gpointer,
38 ) where
39 P: IsA<Application>,
40 {
41 let f: &F = &*(f as *const F);
42 let files: Vec<File> = FromGlibContainer::from_glib_none_num(files, n_files as usize);
43 f(
44 Application::from_glib_borrow(this).unsafe_cast_ref(),
45 &files,
46 &GString::from_glib_borrow(hint),
47 )
48 }
49 unsafe {
50 let f: Box_<F> = Box_::new(f);
51 connect_raw(
52 self.as_ptr() as *mut _,
53 b"open\0".as_ptr() as *const _,
54 Some(transmute::<*const (), unsafe extern "C" fn()>(
55 open_trampoline::<Self, F> as *const (),
56 )),
57 Box_::into_raw(f),
58 )
59 }
60 }
61
62 #[doc(alias = "command-line")]
63 fn connect_command_line<F: Fn(&Self, &ApplicationCommandLine) -> ExitCode + 'static>(
64 &self,
65 f: F,
66 ) -> SignalHandlerId {
67 unsafe extern "C" fn command_line_trampoline<
68 P: IsA<Application>,
69 F: Fn(&P, &ApplicationCommandLine) -> ExitCode + 'static,
70 >(
71 this: *mut ffi::GApplication,
72 command_line: *mut ffi::GApplicationCommandLine,
73 f: glib::ffi::gpointer,
74 ) -> std::ffi::c_int {
75 let f: &F = &*(f as *const F);
76 f(
77 Application::from_glib_borrow(this).unsafe_cast_ref(),
78 &from_glib_borrow(command_line),
79 )
80 .into()
81 }
82 unsafe {
83 let f: Box_<F> = Box_::new(f);
84 connect_raw(
85 self.as_ptr() as *mut _,
86 c"command-line".as_ptr() as *const _,
87 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
88 command_line_trampoline::<Self, F> as *const (),
89 )),
90 Box_::into_raw(f),
91 )
92 }
93 }
94
95 #[doc(alias = "handle-local-options")]
96 fn connect_handle_local_options<
97 F: Fn(&Self, &glib::VariantDict) -> ControlFlow<ExitCode> + 'static,
98 >(
99 &self,
100 f: F,
101 ) -> SignalHandlerId {
102 unsafe extern "C" fn handle_local_options_trampoline<
103 P: IsA<Application>,
104 F: Fn(&P, &glib::VariantDict) -> ControlFlow<ExitCode> + 'static,
105 >(
106 this: *mut ffi::GApplication,
107 options: *mut glib::ffi::GVariantDict,
108 f: glib::ffi::gpointer,
109 ) -> std::ffi::c_int {
110 let f: &F = &*(f as *const F);
111 f(
112 Application::from_glib_borrow(this).unsafe_cast_ref(),
113 &from_glib_borrow(options),
114 )
115 .break_value()
116 .map(i32::from)
117 .unwrap_or(-1)
118 }
119 unsafe {
120 let f: Box_<F> = Box_::new(f);
121 connect_raw(
122 self.as_ptr() as *mut _,
123 c"handle-local-options".as_ptr() as *const _,
124 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
125 handle_local_options_trampoline::<Self, F> as *const (),
126 )),
127 Box_::into_raw(f),
128 )
129 }
130 }
131
132 #[doc(alias = "g_application_hold")]
133 fn hold(&self) -> ApplicationHoldGuard {
134 unsafe {
135 ffi::g_application_hold(self.as_ref().to_glib_none().0);
136 }
137 ApplicationHoldGuard(self.as_ref().downgrade())
138 }
139
140 #[doc(alias = "g_application_mark_busy")]
141 fn mark_busy(&self) -> ApplicationBusyGuard {
142 unsafe {
143 ffi::g_application_mark_busy(self.as_ref().to_glib_none().0);
144 }
145 ApplicationBusyGuard(self.as_ref().downgrade())
146 }
147}
148
149impl<O: IsA<Application>> ApplicationExtManual for O {}
150
151#[derive(Debug)]
152#[must_use = "if unused the Application will immediately be released"]
153pub struct ApplicationHoldGuard(glib::WeakRef<Application>);
154
155impl Drop for ApplicationHoldGuard {
156 #[inline]
157 fn drop(&mut self) {
158 if let Some(application) = self.0.upgrade() {
159 unsafe {
160 ffi::g_application_release(application.to_glib_none().0);
161 }
162 }
163 }
164}
165
166#[derive(Debug)]
167#[must_use = "if unused the Application will immediately be unmarked busy"]
168pub struct ApplicationBusyGuard(glib::WeakRef<Application>);
169
170impl Drop for ApplicationBusyGuard {
171 #[inline]
172 fn drop(&mut self) {
173 if let Some(application) = self.0.upgrade() {
174 unsafe {
175 ffi::g_application_unmark_busy(application.to_glib_none().0);
176 }
177 }
178 }
179}