Skip to main content

libadwaita/
alert_dialog.rs

1use glib::object::IsA;
2use glib::translate::*;
3
4use crate::AlertDialog;
5use crate::prelude::*;
6
7use std::boxed::Box as Box_;
8use std::pin::Pin;
9
10pub trait AlertDialogExtManual: IsA<AlertDialog> + 'static {
11    /// Gets the label of @response.
12    ///
13    /// See [`AlertDialogExt::set_response_label()`][crate::prelude::AlertDialogExt::set_response_label()].
14    /// ## `response`
15    /// a response ID
16    ///
17    /// # Returns
18    ///
19    /// the label of @response
20    #[doc(alias = "adw_alert_dialog_get_response_label")]
21    #[doc(alias = "get_response_label")]
22    fn response_label(&self, response: &str) -> glib::GString {
23        assert!(self.as_ref().has_response(response));
24
25        unsafe {
26            from_glib_none(ffi::adw_alert_dialog_get_response_label(
27                self.as_ref().to_glib_none().0,
28                response.to_glib_none().0,
29            ))
30        }
31    }
32
33    /// Adds multiple responses to @self.
34    ///
35    /// This is the same as calling [`AlertDialogExt::add_response()`][crate::prelude::AlertDialogExt::add_response()] repeatedly. The
36    /// variable argument list should be `NULL`-terminated list of response IDs and
37    /// labels.
38    ///
39    /// Example:
40    ///
41    /// **⚠️ The following code is in c ⚠️**
42    ///
43    /// ```c
44    /// adw_alert_dialog_add_responses (dialog,
45    ///                                 "cancel",  _("_Cancel"),
46    ///                                 "discard", _("_Discard"),
47    ///                                 "save",    _("_Save"),
48    ///                                 NULL);
49    /// ```
50    /// ## `first_id`
51    /// response id
52    #[doc(alias = "adw_alert_dialog_add_responses")]
53    fn add_responses(&self, ids_and_labels: &[(&str, &str)]) {
54        ids_and_labels.iter().for_each(|(id, label)| {
55            self.add_response(id, label);
56        });
57    }
58
59    /// This function shows @self to the user.
60    ///
61    /// If the window is an [`Window`][crate::Window] or [`ApplicationWindow`][crate::ApplicationWindow], the dialog
62    /// will be shown within it. Otherwise, it will be a separate window.
63    /// ## `parent`
64    /// the parent widget
65    /// ## `cancellable`
66    /// a `GCancellable` to cancel the operation
67    /// ## `callback`
68    /// a callback to call when the operation is complete
69    #[doc(alias = "adw_alert_dialog_choose")]
70    fn choose<P: FnOnce(glib::GString) + 'static>(
71        self,
72        parent: Option<&impl IsA<gtk::Widget>>,
73        cancellable: Option<&impl IsA<gio::Cancellable>>,
74        callback: P,
75    ) {
76        let main_context = glib::MainContext::ref_thread_default();
77        let is_main_context_owner = main_context.is_owner();
78        let has_acquired_main_context = (!is_main_context_owner)
79            .then(|| main_context.acquire().ok())
80            .flatten();
81        assert!(
82            is_main_context_owner || has_acquired_main_context.is_some(),
83            "Async operations only allowed if the thread is owning the MainContext"
84        );
85
86        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
87            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
88        unsafe extern "C" fn choose_trampoline<P: FnOnce(glib::GString) + 'static>(
89            _source_object: *mut glib::gobject_ffi::GObject,
90            res: *mut gio::ffi::GAsyncResult,
91            user_data: glib::ffi::gpointer,
92        ) {
93            unsafe {
94                let result = from_glib_none(ffi::adw_alert_dialog_choose_finish(
95                    _source_object as *mut _,
96                    res,
97                ));
98                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
99                    Box_::from_raw(user_data as *mut _);
100                let callback: P = callback.into_inner();
101                callback(result);
102            }
103        }
104        let callback = choose_trampoline::<P>;
105        unsafe {
106            ffi::adw_alert_dialog_choose(
107                self.upcast().into_glib_ptr(),
108                parent.map(|p| p.as_ref()).to_glib_none().0,
109                cancellable.map(|p| p.as_ref()).to_glib_none().0,
110                Some(callback),
111                Box_::into_raw(user_data) as *mut _,
112            );
113        }
114    }
115
116    fn choose_future(
117        self,
118        parent: Option<&impl IsA<gtk::Widget>>,
119    ) -> Pin<Box_<dyn std::future::Future<Output = glib::GString> + 'static>> {
120        let parent = parent.map(ToOwned::to_owned);
121        Box_::pin(gio::GioFuture::new(
122            &self,
123            move |obj: &Self, cancellable, send| {
124                obj.clone().choose(
125                    parent.as_ref().map(::std::borrow::Borrow::borrow),
126                    Some(cancellable),
127                    move |res| {
128                        send.resolve(res);
129                    },
130                );
131            },
132        ))
133    }
134}
135
136impl<O: IsA<AlertDialog>> AlertDialogExtManual for O {}