gtk4/auto/
color_dialog.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    /// Asynchronous API to present a color chooser dialog.
15    ///
16    /// [`ColorDialog`][crate::ColorDialog] collects the arguments that are needed to present
17    /// the dialog to the user, such as a title for the dialog and whether
18    /// it should be modal.
19    ///
20    /// The dialog is shown with the [`choose_rgba()`][Self::choose_rgba()]
21    /// function.
22    ///
23    /// See [`ColorDialogButton`][crate::ColorDialogButton] for a convenient control
24    /// that uses [`ColorDialog`][crate::ColorDialog] and presents the results.
25    ///
26    /// ## Properties
27    ///
28    ///
29    /// #### `modal`
30    ///  Whether the color chooser dialog is modal.
31    ///
32    /// Readable | Writeable
33    ///
34    ///
35    /// #### `title`
36    ///  A title that may be shown on the color chooser dialog.
37    ///
38    /// Readable | Writeable
39    ///
40    ///
41    /// #### `with-alpha`
42    ///  Whether colors may have alpha (translucency).
43    ///
44    /// When with-alpha is false, the color that is selected
45    /// will be forced to have alpha == 1.
46    ///
47    /// Readable | Writeable
48    ///
49    /// # Implements
50    ///
51    /// [`trait@glib::ObjectExt`]
52    #[doc(alias = "GtkColorDialog")]
53    pub struct ColorDialog(Object<ffi::GtkColorDialog, ffi::GtkColorDialogClass>);
54
55    match fn {
56        type_ => || ffi::gtk_color_dialog_get_type(),
57    }
58}
59
60impl ColorDialog {
61    /// Creates a new [`ColorDialog`][crate::ColorDialog] object.
62    ///
63    /// # Returns
64    ///
65    /// the new [`ColorDialog`][crate::ColorDialog]
66    #[doc(alias = "gtk_color_dialog_new")]
67    pub fn new() -> ColorDialog {
68        assert_initialized_main_thread!();
69        unsafe { from_glib_full(ffi::gtk_color_dialog_new()) }
70    }
71
72    // rustdoc-stripper-ignore-next
73    /// Creates a new builder-pattern struct instance to construct [`ColorDialog`] objects.
74    ///
75    /// This method returns an instance of [`ColorDialogBuilder`](crate::builders::ColorDialogBuilder) which can be used to create [`ColorDialog`] objects.
76    pub fn builder() -> ColorDialogBuilder {
77        ColorDialogBuilder::new()
78    }
79
80    /// Presents a color chooser dialog to the user.
81    /// ## `parent`
82    /// the parent window
83    /// ## `initial_color`
84    /// the color to select initially
85    /// ## `cancellable`
86    /// a cancellable to cancel the operation
87    /// ## `callback`
88    /// a callback to call
89    ///   when the operation is complete
90    #[doc(alias = "gtk_color_dialog_choose_rgba")]
91    pub fn choose_rgba<P: FnOnce(Result<gdk::RGBA, glib::Error>) + 'static>(
92        &self,
93        parent: Option<&impl IsA<Window>>,
94        initial_color: Option<&gdk::RGBA>,
95        cancellable: Option<&impl IsA<gio::Cancellable>>,
96        callback: P,
97    ) {
98        let main_context = glib::MainContext::ref_thread_default();
99        let is_main_context_owner = main_context.is_owner();
100        let has_acquired_main_context = (!is_main_context_owner)
101            .then(|| main_context.acquire().ok())
102            .flatten();
103        assert!(
104            is_main_context_owner || has_acquired_main_context.is_some(),
105            "Async operations only allowed if the thread is owning the MainContext"
106        );
107
108        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
109            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
110        unsafe extern "C" fn choose_rgba_trampoline<
111            P: FnOnce(Result<gdk::RGBA, glib::Error>) + 'static,
112        >(
113            _source_object: *mut glib::gobject_ffi::GObject,
114            res: *mut gio::ffi::GAsyncResult,
115            user_data: glib::ffi::gpointer,
116        ) {
117            let mut error = std::ptr::null_mut();
118            let ret =
119                ffi::gtk_color_dialog_choose_rgba_finish(_source_object as *mut _, res, &mut error);
120            let result = if error.is_null() {
121                Ok(from_glib_full(ret))
122            } else {
123                Err(from_glib_full(error))
124            };
125            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
126                Box_::from_raw(user_data as *mut _);
127            let callback: P = callback.into_inner();
128            callback(result);
129        }
130        let callback = choose_rgba_trampoline::<P>;
131        unsafe {
132            ffi::gtk_color_dialog_choose_rgba(
133                self.to_glib_none().0,
134                parent.map(|p| p.as_ref()).to_glib_none().0,
135                initial_color.to_glib_none().0,
136                cancellable.map(|p| p.as_ref()).to_glib_none().0,
137                Some(callback),
138                Box_::into_raw(user_data) as *mut _,
139            );
140        }
141    }
142
143    pub fn choose_rgba_future(
144        &self,
145        parent: Option<&(impl IsA<Window> + Clone + 'static)>,
146        initial_color: Option<&gdk::RGBA>,
147    ) -> Pin<Box_<dyn std::future::Future<Output = Result<gdk::RGBA, glib::Error>> + 'static>> {
148        let parent = parent.map(ToOwned::to_owned);
149        let initial_color = initial_color.map(ToOwned::to_owned);
150        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
151            obj.choose_rgba(
152                parent.as_ref().map(::std::borrow::Borrow::borrow),
153                initial_color.as_ref().map(::std::borrow::Borrow::borrow),
154                Some(cancellable),
155                move |res| {
156                    send.resolve(res);
157                },
158            );
159        }))
160    }
161
162    /// Returns whether the color chooser dialog
163    /// blocks interaction with the parent window
164    /// while it is presented.
165    ///
166    /// # Returns
167    ///
168    /// true if the color chooser dialog is modal
169    #[doc(alias = "gtk_color_dialog_get_modal")]
170    #[doc(alias = "get_modal")]
171    #[doc(alias = "modal")]
172    pub fn is_modal(&self) -> bool {
173        unsafe { from_glib(ffi::gtk_color_dialog_get_modal(self.to_glib_none().0)) }
174    }
175
176    /// Returns the title that will be shown on the
177    /// color chooser dialog.
178    ///
179    /// # Returns
180    ///
181    /// the title
182    #[doc(alias = "gtk_color_dialog_get_title")]
183    #[doc(alias = "get_title")]
184    pub fn title(&self) -> glib::GString {
185        unsafe { from_glib_none(ffi::gtk_color_dialog_get_title(self.to_glib_none().0)) }
186    }
187
188    /// Returns whether colors may have alpha.
189    ///
190    /// # Returns
191    ///
192    /// true if colors may have alpha
193    #[doc(alias = "gtk_color_dialog_get_with_alpha")]
194    #[doc(alias = "get_with_alpha")]
195    #[doc(alias = "with-alpha")]
196    pub fn is_with_alpha(&self) -> bool {
197        unsafe { from_glib(ffi::gtk_color_dialog_get_with_alpha(self.to_glib_none().0)) }
198    }
199
200    /// Sets whether the color chooser dialog
201    /// blocks interaction with the parent window
202    /// while it is presented.
203    /// ## `modal`
204    /// the new value
205    #[doc(alias = "gtk_color_dialog_set_modal")]
206    #[doc(alias = "modal")]
207    pub fn set_modal(&self, modal: bool) {
208        unsafe {
209            ffi::gtk_color_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
210        }
211    }
212
213    /// Sets the title that will be shown on the
214    /// color chooser dialog.
215    /// ## `title`
216    /// the new title
217    #[doc(alias = "gtk_color_dialog_set_title")]
218    #[doc(alias = "title")]
219    pub fn set_title(&self, title: &str) {
220        unsafe {
221            ffi::gtk_color_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
222        }
223    }
224
225    /// Sets whether colors may have alpha.
226    /// ## `with_alpha`
227    /// the new value
228    #[doc(alias = "gtk_color_dialog_set_with_alpha")]
229    #[doc(alias = "with-alpha")]
230    pub fn set_with_alpha(&self, with_alpha: bool) {
231        unsafe {
232            ffi::gtk_color_dialog_set_with_alpha(self.to_glib_none().0, with_alpha.into_glib());
233        }
234    }
235
236    #[cfg(feature = "v4_10")]
237    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
238    #[doc(alias = "modal")]
239    pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
240        unsafe extern "C" fn notify_modal_trampoline<F: Fn(&ColorDialog) + 'static>(
241            this: *mut ffi::GtkColorDialog,
242            _param_spec: glib::ffi::gpointer,
243            f: glib::ffi::gpointer,
244        ) {
245            let f: &F = &*(f as *const F);
246            f(&from_glib_borrow(this))
247        }
248        unsafe {
249            let f: Box_<F> = Box_::new(f);
250            connect_raw(
251                self.as_ptr() as *mut _,
252                c"notify::modal".as_ptr() as *const _,
253                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
254                    notify_modal_trampoline::<F> as *const (),
255                )),
256                Box_::into_raw(f),
257            )
258        }
259    }
260
261    #[cfg(feature = "v4_10")]
262    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
263    #[doc(alias = "title")]
264    pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
265        unsafe extern "C" fn notify_title_trampoline<F: Fn(&ColorDialog) + 'static>(
266            this: *mut ffi::GtkColorDialog,
267            _param_spec: glib::ffi::gpointer,
268            f: glib::ffi::gpointer,
269        ) {
270            let f: &F = &*(f as *const F);
271            f(&from_glib_borrow(this))
272        }
273        unsafe {
274            let f: Box_<F> = Box_::new(f);
275            connect_raw(
276                self.as_ptr() as *mut _,
277                c"notify::title".as_ptr() as *const _,
278                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
279                    notify_title_trampoline::<F> as *const (),
280                )),
281                Box_::into_raw(f),
282            )
283        }
284    }
285
286    #[cfg(feature = "v4_10")]
287    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
288    #[doc(alias = "with-alpha")]
289    pub fn connect_with_alpha_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
290        unsafe extern "C" fn notify_with_alpha_trampoline<F: Fn(&ColorDialog) + 'static>(
291            this: *mut ffi::GtkColorDialog,
292            _param_spec: glib::ffi::gpointer,
293            f: glib::ffi::gpointer,
294        ) {
295            let f: &F = &*(f as *const F);
296            f(&from_glib_borrow(this))
297        }
298        unsafe {
299            let f: Box_<F> = Box_::new(f);
300            connect_raw(
301                self.as_ptr() as *mut _,
302                c"notify::with-alpha".as_ptr() as *const _,
303                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
304                    notify_with_alpha_trampoline::<F> as *const (),
305                )),
306                Box_::into_raw(f),
307            )
308        }
309    }
310}
311
312#[cfg(feature = "v4_10")]
313#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
314impl Default for ColorDialog {
315    fn default() -> Self {
316        Self::new()
317    }
318}
319
320// rustdoc-stripper-ignore-next
321/// A [builder-pattern] type to construct [`ColorDialog`] objects.
322///
323/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
324#[must_use = "The builder must be built to be used"]
325pub struct ColorDialogBuilder {
326    builder: glib::object::ObjectBuilder<'static, ColorDialog>,
327}
328
329impl ColorDialogBuilder {
330    fn new() -> Self {
331        Self {
332            builder: glib::object::Object::builder(),
333        }
334    }
335
336    /// Whether the color chooser dialog is modal.
337    #[cfg(feature = "v4_10")]
338    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
339    pub fn modal(self, modal: bool) -> Self {
340        Self {
341            builder: self.builder.property("modal", modal),
342        }
343    }
344
345    /// A title that may be shown on the color chooser dialog.
346    #[cfg(feature = "v4_10")]
347    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
348    pub fn title(self, title: impl Into<glib::GString>) -> Self {
349        Self {
350            builder: self.builder.property("title", title.into()),
351        }
352    }
353
354    /// Whether colors may have alpha (translucency).
355    ///
356    /// When with-alpha is false, the color that is selected
357    /// will be forced to have alpha == 1.
358    #[cfg(feature = "v4_10")]
359    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
360    pub fn with_alpha(self, with_alpha: bool) -> Self {
361        Self {
362            builder: self.builder.property("with-alpha", with_alpha),
363        }
364    }
365
366    // rustdoc-stripper-ignore-next
367    /// Build the [`ColorDialog`].
368    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
369    pub fn build(self) -> ColorDialog {
370        assert_initialized_main_thread!();
371        self.builder.build()
372    }
373}