1use 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 #[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 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 #[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 #[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 #[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 #[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 #[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 #[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#[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 #[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 #[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 #[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 #[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}