[go: up one dir, main page]

gtk4/auto/
uri_launcher.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use 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")]
15    pub struct UriLauncher(Object<ffi::GtkUriLauncher, ffi::GtkUriLauncherClass>);
16
17    match fn {
18        type_ => || ffi::gtk_uri_launcher_get_type(),
19    }
20}
21
22impl UriLauncher {
23    #[doc(alias = "gtk_uri_launcher_new")]
24    pub fn new(uri: &str) -> UriLauncher {
25        assert_initialized_main_thread!();
26        unsafe { from_glib_full(ffi::gtk_uri_launcher_new(uri.to_glib_none().0)) }
27    }
28
29    // rustdoc-stripper-ignore-next
30    /// Creates a new builder-pattern struct instance to construct [`UriLauncher`] objects.
31    ///
32    /// This method returns an instance of [`UriLauncherBuilder`](crate::builders::UriLauncherBuilder) which can be used to create [`UriLauncher`] objects.
33    pub fn builder() -> UriLauncherBuilder {
34        UriLauncherBuilder::new()
35    }
36
37    #[cfg(feature = "v4_20")]
38    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
39    #[doc(alias = "gtk_uri_launcher_can_launch")]
40    pub fn can_launch(&self, parent: Option<&impl IsA<Window>>) -> bool {
41        unsafe {
42            from_glib(ffi::gtk_uri_launcher_can_launch(
43                self.to_glib_none().0,
44                parent.map(|p| p.as_ref()).to_glib_none().0,
45            ))
46        }
47    }
48
49    #[doc(alias = "gtk_uri_launcher_get_uri")]
50    #[doc(alias = "get_uri")]
51    pub fn uri(&self) -> Option<glib::GString> {
52        unsafe { from_glib_none(ffi::gtk_uri_launcher_get_uri(self.to_glib_none().0)) }
53    }
54
55    #[doc(alias = "gtk_uri_launcher_launch")]
56    pub fn launch<P: FnOnce(Result<(), glib::Error>) + 'static>(
57        &self,
58        parent: Option<&impl IsA<Window>>,
59        cancellable: Option<&impl IsA<gio::Cancellable>>,
60        callback: P,
61    ) {
62        let main_context = glib::MainContext::ref_thread_default();
63        let is_main_context_owner = main_context.is_owner();
64        let has_acquired_main_context = (!is_main_context_owner)
65            .then(|| main_context.acquire().ok())
66            .flatten();
67        assert!(
68            is_main_context_owner || has_acquired_main_context.is_some(),
69            "Async operations only allowed if the thread is owning the MainContext"
70        );
71
72        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
73            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
74        unsafe extern "C" fn launch_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
75            _source_object: *mut glib::gobject_ffi::GObject,
76            res: *mut gio::ffi::GAsyncResult,
77            user_data: glib::ffi::gpointer,
78        ) {
79            let mut error = std::ptr::null_mut();
80            ffi::gtk_uri_launcher_launch_finish(_source_object as *mut _, res, &mut error);
81            let result = if error.is_null() {
82                Ok(())
83            } else {
84                Err(from_glib_full(error))
85            };
86            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
87                Box_::from_raw(user_data as *mut _);
88            let callback: P = callback.into_inner();
89            callback(result);
90        }
91        let callback = launch_trampoline::<P>;
92        unsafe {
93            ffi::gtk_uri_launcher_launch(
94                self.to_glib_none().0,
95                parent.map(|p| p.as_ref()).to_glib_none().0,
96                cancellable.map(|p| p.as_ref()).to_glib_none().0,
97                Some(callback),
98                Box_::into_raw(user_data) as *mut _,
99            );
100        }
101    }
102
103    pub fn launch_future(
104        &self,
105        parent: Option<&(impl IsA<Window> + Clone + 'static)>,
106    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
107        let parent = parent.map(ToOwned::to_owned);
108        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
109            obj.launch(
110                parent.as_ref().map(::std::borrow::Borrow::borrow),
111                Some(cancellable),
112                move |res| {
113                    send.resolve(res);
114                },
115            );
116        }))
117    }
118
119    #[doc(alias = "gtk_uri_launcher_set_uri")]
120    #[doc(alias = "uri")]
121    pub fn set_uri(&self, uri: Option<&str>) {
122        unsafe {
123            ffi::gtk_uri_launcher_set_uri(self.to_glib_none().0, uri.to_glib_none().0);
124        }
125    }
126
127    #[cfg(feature = "v4_10")]
128    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
129    #[doc(alias = "uri")]
130    pub fn connect_uri_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
131        unsafe extern "C" fn notify_uri_trampoline<F: Fn(&UriLauncher) + 'static>(
132            this: *mut ffi::GtkUriLauncher,
133            _param_spec: glib::ffi::gpointer,
134            f: glib::ffi::gpointer,
135        ) {
136            let f: &F = &*(f as *const F);
137            f(&from_glib_borrow(this))
138        }
139        unsafe {
140            let f: Box_<F> = Box_::new(f);
141            connect_raw(
142                self.as_ptr() as *mut _,
143                c"notify::uri".as_ptr() as *const _,
144                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
145                    notify_uri_trampoline::<F> as *const (),
146                )),
147                Box_::into_raw(f),
148            )
149        }
150    }
151}
152
153#[cfg(feature = "v4_10")]
154#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
155impl Default for UriLauncher {
156    fn default() -> Self {
157        glib::object::Object::new::<Self>()
158    }
159}
160
161// rustdoc-stripper-ignore-next
162/// A [builder-pattern] type to construct [`UriLauncher`] objects.
163///
164/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
165#[must_use = "The builder must be built to be used"]
166pub struct UriLauncherBuilder {
167    builder: glib::object::ObjectBuilder<'static, UriLauncher>,
168}
169
170impl UriLauncherBuilder {
171    fn new() -> Self {
172        Self {
173            builder: glib::object::Object::builder(),
174        }
175    }
176
177    #[cfg(feature = "v4_10")]
178    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
179    pub fn uri(self, uri: impl Into<glib::GString>) -> Self {
180        Self {
181            builder: self.builder.property("uri", uri.into()),
182        }
183    }
184
185    // rustdoc-stripper-ignore-next
186    /// Build the [`UriLauncher`].
187    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
188    pub fn build(self) -> UriLauncher {
189        assert_initialized_main_thread!();
190        self.builder.build()
191    }
192}