Skip to main content

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::{Window, ffi};
6use glib::{
7    prelude::*,
8    signal::{SignalHandlerId, connect_raw},
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            unsafe {
118                let mut error = std::ptr::null_mut();
119                let ret = ffi::gtk_color_dialog_choose_rgba_finish(
120                    _source_object as *mut _,
121                    res,
122                    &mut error,
123                );
124                let result = if error.is_null() {
125                    Ok(from_glib_full(ret))
126                } else {
127                    Err(from_glib_full(error))
128                };
129                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
130                    Box_::from_raw(user_data as *mut _);
131                let callback: P = callback.into_inner();
132                callback(result);
133            }
134        }
135        let callback = choose_rgba_trampoline::<P>;
136        unsafe {
137            ffi::gtk_color_dialog_choose_rgba(
138                self.to_glib_none().0,
139                parent.map(|p| p.as_ref()).to_glib_none().0,
140                initial_color.to_glib_none().0,
141                cancellable.map(|p| p.as_ref()).to_glib_none().0,
142                Some(callback),
143                Box_::into_raw(user_data) as *mut _,
144            );
145        }
146    }
147
148    pub fn choose_rgba_future(
149        &self,
150        parent: Option<&(impl IsA<Window> + Clone + 'static)>,
151        initial_color: Option<&gdk::RGBA>,
152    ) -> Pin<Box_<dyn std::future::Future<Output = Result<gdk::RGBA, glib::Error>> + 'static>> {
153        let parent = parent.map(ToOwned::to_owned);
154        let initial_color = initial_color.map(ToOwned::to_owned);
155        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
156            obj.choose_rgba(
157                parent.as_ref().map(::std::borrow::Borrow::borrow),
158                initial_color.as_ref().map(::std::borrow::Borrow::borrow),
159                Some(cancellable),
160                move |res| {
161                    send.resolve(res);
162                },
163            );
164        }))
165    }
166
167    /// Returns whether the color chooser dialog
168    /// blocks interaction with the parent window
169    /// while it is presented.
170    ///
171    /// # Returns
172    ///
173    /// true if the color chooser dialog is modal
174    #[doc(alias = "gtk_color_dialog_get_modal")]
175    #[doc(alias = "get_modal")]
176    #[doc(alias = "modal")]
177    pub fn is_modal(&self) -> bool {
178        unsafe { from_glib(ffi::gtk_color_dialog_get_modal(self.to_glib_none().0)) }
179    }
180
181    /// Returns the title that will be shown on the
182    /// color chooser dialog.
183    ///
184    /// # Returns
185    ///
186    /// the title
187    #[doc(alias = "gtk_color_dialog_get_title")]
188    #[doc(alias = "get_title")]
189    pub fn title(&self) -> glib::GString {
190        unsafe { from_glib_none(ffi::gtk_color_dialog_get_title(self.to_glib_none().0)) }
191    }
192
193    /// Returns whether colors may have alpha.
194    ///
195    /// # Returns
196    ///
197    /// true if colors may have alpha
198    #[doc(alias = "gtk_color_dialog_get_with_alpha")]
199    #[doc(alias = "get_with_alpha")]
200    #[doc(alias = "with-alpha")]
201    pub fn is_with_alpha(&self) -> bool {
202        unsafe { from_glib(ffi::gtk_color_dialog_get_with_alpha(self.to_glib_none().0)) }
203    }
204
205    /// Sets whether the color chooser dialog
206    /// blocks interaction with the parent window
207    /// while it is presented.
208    /// ## `modal`
209    /// the new value
210    #[doc(alias = "gtk_color_dialog_set_modal")]
211    #[doc(alias = "modal")]
212    pub fn set_modal(&self, modal: bool) {
213        unsafe {
214            ffi::gtk_color_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
215        }
216    }
217
218    /// Sets the title that will be shown on the
219    /// color chooser dialog.
220    /// ## `title`
221    /// the new title
222    #[doc(alias = "gtk_color_dialog_set_title")]
223    #[doc(alias = "title")]
224    pub fn set_title(&self, title: &str) {
225        unsafe {
226            ffi::gtk_color_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
227        }
228    }
229
230    /// Sets whether colors may have alpha.
231    /// ## `with_alpha`
232    /// the new value
233    #[doc(alias = "gtk_color_dialog_set_with_alpha")]
234    #[doc(alias = "with-alpha")]
235    pub fn set_with_alpha(&self, with_alpha: bool) {
236        unsafe {
237            ffi::gtk_color_dialog_set_with_alpha(self.to_glib_none().0, with_alpha.into_glib());
238        }
239    }
240
241    #[cfg(feature = "v4_10")]
242    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
243    #[doc(alias = "modal")]
244    pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
245        unsafe extern "C" fn notify_modal_trampoline<F: Fn(&ColorDialog) + 'static>(
246            this: *mut ffi::GtkColorDialog,
247            _param_spec: glib::ffi::gpointer,
248            f: glib::ffi::gpointer,
249        ) {
250            unsafe {
251                let f: &F = &*(f as *const F);
252                f(&from_glib_borrow(this))
253            }
254        }
255        unsafe {
256            let f: Box_<F> = Box_::new(f);
257            connect_raw(
258                self.as_ptr() as *mut _,
259                c"notify::modal".as_ptr() as *const _,
260                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
261                    notify_modal_trampoline::<F> as *const (),
262                )),
263                Box_::into_raw(f),
264            )
265        }
266    }
267
268    #[cfg(feature = "v4_10")]
269    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
270    #[doc(alias = "title")]
271    pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
272        unsafe extern "C" fn notify_title_trampoline<F: Fn(&ColorDialog) + 'static>(
273            this: *mut ffi::GtkColorDialog,
274            _param_spec: glib::ffi::gpointer,
275            f: glib::ffi::gpointer,
276        ) {
277            unsafe {
278                let f: &F = &*(f as *const F);
279                f(&from_glib_borrow(this))
280            }
281        }
282        unsafe {
283            let f: Box_<F> = Box_::new(f);
284            connect_raw(
285                self.as_ptr() as *mut _,
286                c"notify::title".as_ptr() as *const _,
287                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
288                    notify_title_trampoline::<F> as *const (),
289                )),
290                Box_::into_raw(f),
291            )
292        }
293    }
294
295    #[cfg(feature = "v4_10")]
296    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
297    #[doc(alias = "with-alpha")]
298    pub fn connect_with_alpha_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
299        unsafe extern "C" fn notify_with_alpha_trampoline<F: Fn(&ColorDialog) + 'static>(
300            this: *mut ffi::GtkColorDialog,
301            _param_spec: glib::ffi::gpointer,
302            f: glib::ffi::gpointer,
303        ) {
304            unsafe {
305                let f: &F = &*(f as *const F);
306                f(&from_glib_borrow(this))
307            }
308        }
309        unsafe {
310            let f: Box_<F> = Box_::new(f);
311            connect_raw(
312                self.as_ptr() as *mut _,
313                c"notify::with-alpha".as_ptr() as *const _,
314                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
315                    notify_with_alpha_trampoline::<F> as *const (),
316                )),
317                Box_::into_raw(f),
318            )
319        }
320    }
321}
322
323#[cfg(feature = "v4_10")]
324#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
325impl Default for ColorDialog {
326    fn default() -> Self {
327        Self::new()
328    }
329}
330
331// rustdoc-stripper-ignore-next
332/// A [builder-pattern] type to construct [`ColorDialog`] objects.
333///
334/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
335#[must_use = "The builder must be built to be used"]
336pub struct ColorDialogBuilder {
337    builder: glib::object::ObjectBuilder<'static, ColorDialog>,
338}
339
340impl ColorDialogBuilder {
341    fn new() -> Self {
342        Self {
343            builder: glib::object::Object::builder(),
344        }
345    }
346
347    /// Whether the color chooser dialog is modal.
348    #[cfg(feature = "v4_10")]
349    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
350    pub fn modal(self, modal: bool) -> Self {
351        Self {
352            builder: self.builder.property("modal", modal),
353        }
354    }
355
356    /// A title that may be shown on the color chooser dialog.
357    #[cfg(feature = "v4_10")]
358    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
359    pub fn title(self, title: impl Into<glib::GString>) -> Self {
360        Self {
361            builder: self.builder.property("title", title.into()),
362        }
363    }
364
365    /// Whether colors may have alpha (translucency).
366    ///
367    /// When with-alpha is false, the color that is selected
368    /// will be forced to have alpha == 1.
369    #[cfg(feature = "v4_10")]
370    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
371    pub fn with_alpha(self, with_alpha: bool) -> Self {
372        Self {
373            builder: self.builder.property("with-alpha", with_alpha),
374        }
375    }
376
377    // rustdoc-stripper-ignore-next
378    /// Build the [`ColorDialog`].
379    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
380    pub fn build(self) -> ColorDialog {
381        assert_initialized_main_thread!();
382        self.builder.build()
383    }
384}