1use 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 = "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 #[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 pub fn builder() -> ColorDialogBuilder {
77 ColorDialogBuilder::new()
78 }
79
80 #[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 #[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 #[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 #[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 #[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 #[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 #[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#[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 #[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 #[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 #[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 #[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}