gtk4/auto/
uri_launcher.rs1use crate::{ffi, Window};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::{boxed::Box as Box_, pin::Pin};
12
13glib::wrapper! {
14 #[doc(alias = "GtkUriLauncher")]
38 pub struct UriLauncher(Object<ffi::GtkUriLauncher, ffi::GtkUriLauncherClass>);
39
40 match fn {
41 type_ => || ffi::gtk_uri_launcher_get_type(),
42 }
43}
44
45impl UriLauncher {
46 #[doc(alias = "gtk_uri_launcher_new")]
54 pub fn new(uri: &str) -> UriLauncher {
55 assert_initialized_main_thread!();
56 unsafe { from_glib_full(ffi::gtk_uri_launcher_new(uri.to_glib_none().0)) }
57 }
58
59 pub fn builder() -> UriLauncherBuilder {
64 UriLauncherBuilder::new()
65 }
66
67 #[cfg(feature = "v4_20")]
80 #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
81 #[doc(alias = "gtk_uri_launcher_can_launch")]
82 pub fn can_launch(&self, parent: Option<&impl IsA<Window>>) -> bool {
83 unsafe {
84 from_glib(ffi::gtk_uri_launcher_can_launch(
85 self.to_glib_none().0,
86 parent.map(|p| p.as_ref()).to_glib_none().0,
87 ))
88 }
89 }
90
91 #[doc(alias = "gtk_uri_launcher_get_uri")]
97 #[doc(alias = "get_uri")]
98 pub fn uri(&self) -> Option<glib::GString> {
99 unsafe { from_glib_none(ffi::gtk_uri_launcher_get_uri(self.to_glib_none().0)) }
100 }
101
102 #[doc(alias = "gtk_uri_launcher_launch")]
113 pub fn launch<P: FnOnce(Result<(), glib::Error>) + 'static>(
114 &self,
115 parent: Option<&impl IsA<Window>>,
116 cancellable: Option<&impl IsA<gio::Cancellable>>,
117 callback: P,
118 ) {
119 let main_context = glib::MainContext::ref_thread_default();
120 let is_main_context_owner = main_context.is_owner();
121 let has_acquired_main_context = (!is_main_context_owner)
122 .then(|| main_context.acquire().ok())
123 .flatten();
124 assert!(
125 is_main_context_owner || has_acquired_main_context.is_some(),
126 "Async operations only allowed if the thread is owning the MainContext"
127 );
128
129 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
130 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
131 unsafe extern "C" fn launch_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
132 _source_object: *mut glib::gobject_ffi::GObject,
133 res: *mut gio::ffi::GAsyncResult,
134 user_data: glib::ffi::gpointer,
135 ) {
136 let mut error = std::ptr::null_mut();
137 ffi::gtk_uri_launcher_launch_finish(_source_object as *mut _, res, &mut error);
138 let result = if error.is_null() {
139 Ok(())
140 } else {
141 Err(from_glib_full(error))
142 };
143 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
144 Box_::from_raw(user_data as *mut _);
145 let callback: P = callback.into_inner();
146 callback(result);
147 }
148 let callback = launch_trampoline::<P>;
149 unsafe {
150 ffi::gtk_uri_launcher_launch(
151 self.to_glib_none().0,
152 parent.map(|p| p.as_ref()).to_glib_none().0,
153 cancellable.map(|p| p.as_ref()).to_glib_none().0,
154 Some(callback),
155 Box_::into_raw(user_data) as *mut _,
156 );
157 }
158 }
159
160 pub fn launch_future(
161 &self,
162 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
163 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
164 let parent = parent.map(ToOwned::to_owned);
165 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
166 obj.launch(
167 parent.as_ref().map(::std::borrow::Borrow::borrow),
168 Some(cancellable),
169 move |res| {
170 send.resolve(res);
171 },
172 );
173 }))
174 }
175
176 #[doc(alias = "gtk_uri_launcher_set_uri")]
180 #[doc(alias = "uri")]
181 pub fn set_uri(&self, uri: Option<&str>) {
182 unsafe {
183 ffi::gtk_uri_launcher_set_uri(self.to_glib_none().0, uri.to_glib_none().0);
184 }
185 }
186
187 #[cfg(feature = "v4_10")]
188 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
189 #[doc(alias = "uri")]
190 pub fn connect_uri_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
191 unsafe extern "C" fn notify_uri_trampoline<F: Fn(&UriLauncher) + 'static>(
192 this: *mut ffi::GtkUriLauncher,
193 _param_spec: glib::ffi::gpointer,
194 f: glib::ffi::gpointer,
195 ) {
196 let f: &F = &*(f as *const F);
197 f(&from_glib_borrow(this))
198 }
199 unsafe {
200 let f: Box_<F> = Box_::new(f);
201 connect_raw(
202 self.as_ptr() as *mut _,
203 c"notify::uri".as_ptr() as *const _,
204 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
205 notify_uri_trampoline::<F> as *const (),
206 )),
207 Box_::into_raw(f),
208 )
209 }
210 }
211}
212
213#[cfg(feature = "v4_10")]
214#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
215impl Default for UriLauncher {
216 fn default() -> Self {
217 glib::object::Object::new::<Self>()
218 }
219}
220
221#[must_use = "The builder must be built to be used"]
226pub struct UriLauncherBuilder {
227 builder: glib::object::ObjectBuilder<'static, UriLauncher>,
228}
229
230impl UriLauncherBuilder {
231 fn new() -> Self {
232 Self {
233 builder: glib::object::Object::builder(),
234 }
235 }
236
237 #[cfg(feature = "v4_10")]
239 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
240 pub fn uri(self, uri: impl Into<glib::GString>) -> Self {
241 Self {
242 builder: self.builder.property("uri", uri.into()),
243 }
244 }
245
246 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
249 pub fn build(self) -> UriLauncher {
250 assert_initialized_main_thread!();
251 self.builder.build()
252 }
253}