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")]
15 pub struct ColorDialog(Object<ffi::GtkColorDialog, ffi::GtkColorDialogClass>);
16
17 match fn {
18 type_ => || ffi::gtk_color_dialog_get_type(),
19 }
20}
21
22impl ColorDialog {
23 #[doc(alias = "gtk_color_dialog_new")]
24 pub fn new() -> ColorDialog {
25 assert_initialized_main_thread!();
26 unsafe { from_glib_full(ffi::gtk_color_dialog_new()) }
27 }
28
29 pub fn builder() -> ColorDialogBuilder {
34 ColorDialogBuilder::new()
35 }
36
37 #[doc(alias = "gtk_color_dialog_choose_rgba")]
38 pub fn choose_rgba<P: FnOnce(Result<gdk::RGBA, glib::Error>) + 'static>(
39 &self,
40 parent: Option<&impl IsA<Window>>,
41 initial_color: Option<&gdk::RGBA>,
42 cancellable: Option<&impl IsA<gio::Cancellable>>,
43 callback: P,
44 ) {
45 let main_context = glib::MainContext::ref_thread_default();
46 let is_main_context_owner = main_context.is_owner();
47 let has_acquired_main_context = (!is_main_context_owner)
48 .then(|| main_context.acquire().ok())
49 .flatten();
50 assert!(
51 is_main_context_owner || has_acquired_main_context.is_some(),
52 "Async operations only allowed if the thread is owning the MainContext"
53 );
54
55 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
56 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
57 unsafe extern "C" fn choose_rgba_trampoline<
58 P: FnOnce(Result<gdk::RGBA, glib::Error>) + 'static,
59 >(
60 _source_object: *mut glib::gobject_ffi::GObject,
61 res: *mut gio::ffi::GAsyncResult,
62 user_data: glib::ffi::gpointer,
63 ) {
64 let mut error = std::ptr::null_mut();
65 let ret =
66 ffi::gtk_color_dialog_choose_rgba_finish(_source_object as *mut _, res, &mut error);
67 let result = if error.is_null() {
68 Ok(from_glib_full(ret))
69 } else {
70 Err(from_glib_full(error))
71 };
72 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
73 Box_::from_raw(user_data as *mut _);
74 let callback: P = callback.into_inner();
75 callback(result);
76 }
77 let callback = choose_rgba_trampoline::<P>;
78 unsafe {
79 ffi::gtk_color_dialog_choose_rgba(
80 self.to_glib_none().0,
81 parent.map(|p| p.as_ref()).to_glib_none().0,
82 initial_color.to_glib_none().0,
83 cancellable.map(|p| p.as_ref()).to_glib_none().0,
84 Some(callback),
85 Box_::into_raw(user_data) as *mut _,
86 );
87 }
88 }
89
90 pub fn choose_rgba_future(
91 &self,
92 parent: Option<&(impl IsA<Window> + Clone + 'static)>,
93 initial_color: Option<&gdk::RGBA>,
94 ) -> Pin<Box_<dyn std::future::Future<Output = Result<gdk::RGBA, glib::Error>> + 'static>> {
95 let parent = parent.map(ToOwned::to_owned);
96 let initial_color = initial_color.map(ToOwned::to_owned);
97 Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
98 obj.choose_rgba(
99 parent.as_ref().map(::std::borrow::Borrow::borrow),
100 initial_color.as_ref().map(::std::borrow::Borrow::borrow),
101 Some(cancellable),
102 move |res| {
103 send.resolve(res);
104 },
105 );
106 }))
107 }
108
109 #[doc(alias = "gtk_color_dialog_get_modal")]
110 #[doc(alias = "get_modal")]
111 #[doc(alias = "modal")]
112 pub fn is_modal(&self) -> bool {
113 unsafe { from_glib(ffi::gtk_color_dialog_get_modal(self.to_glib_none().0)) }
114 }
115
116 #[doc(alias = "gtk_color_dialog_get_title")]
117 #[doc(alias = "get_title")]
118 pub fn title(&self) -> glib::GString {
119 unsafe { from_glib_none(ffi::gtk_color_dialog_get_title(self.to_glib_none().0)) }
120 }
121
122 #[doc(alias = "gtk_color_dialog_get_with_alpha")]
123 #[doc(alias = "get_with_alpha")]
124 #[doc(alias = "with-alpha")]
125 pub fn is_with_alpha(&self) -> bool {
126 unsafe { from_glib(ffi::gtk_color_dialog_get_with_alpha(self.to_glib_none().0)) }
127 }
128
129 #[doc(alias = "gtk_color_dialog_set_modal")]
130 #[doc(alias = "modal")]
131 pub fn set_modal(&self, modal: bool) {
132 unsafe {
133 ffi::gtk_color_dialog_set_modal(self.to_glib_none().0, modal.into_glib());
134 }
135 }
136
137 #[doc(alias = "gtk_color_dialog_set_title")]
138 #[doc(alias = "title")]
139 pub fn set_title(&self, title: &str) {
140 unsafe {
141 ffi::gtk_color_dialog_set_title(self.to_glib_none().0, title.to_glib_none().0);
142 }
143 }
144
145 #[doc(alias = "gtk_color_dialog_set_with_alpha")]
146 #[doc(alias = "with-alpha")]
147 pub fn set_with_alpha(&self, with_alpha: bool) {
148 unsafe {
149 ffi::gtk_color_dialog_set_with_alpha(self.to_glib_none().0, with_alpha.into_glib());
150 }
151 }
152
153 #[cfg(feature = "v4_10")]
154 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
155 #[doc(alias = "modal")]
156 pub fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
157 unsafe extern "C" fn notify_modal_trampoline<F: Fn(&ColorDialog) + 'static>(
158 this: *mut ffi::GtkColorDialog,
159 _param_spec: glib::ffi::gpointer,
160 f: glib::ffi::gpointer,
161 ) {
162 let f: &F = &*(f as *const F);
163 f(&from_glib_borrow(this))
164 }
165 unsafe {
166 let f: Box_<F> = Box_::new(f);
167 connect_raw(
168 self.as_ptr() as *mut _,
169 c"notify::modal".as_ptr() as *const _,
170 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
171 notify_modal_trampoline::<F> as *const (),
172 )),
173 Box_::into_raw(f),
174 )
175 }
176 }
177
178 #[cfg(feature = "v4_10")]
179 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
180 #[doc(alias = "title")]
181 pub fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
182 unsafe extern "C" fn notify_title_trampoline<F: Fn(&ColorDialog) + 'static>(
183 this: *mut ffi::GtkColorDialog,
184 _param_spec: glib::ffi::gpointer,
185 f: glib::ffi::gpointer,
186 ) {
187 let f: &F = &*(f as *const F);
188 f(&from_glib_borrow(this))
189 }
190 unsafe {
191 let f: Box_<F> = Box_::new(f);
192 connect_raw(
193 self.as_ptr() as *mut _,
194 c"notify::title".as_ptr() as *const _,
195 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
196 notify_title_trampoline::<F> as *const (),
197 )),
198 Box_::into_raw(f),
199 )
200 }
201 }
202
203 #[cfg(feature = "v4_10")]
204 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
205 #[doc(alias = "with-alpha")]
206 pub fn connect_with_alpha_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
207 unsafe extern "C" fn notify_with_alpha_trampoline<F: Fn(&ColorDialog) + 'static>(
208 this: *mut ffi::GtkColorDialog,
209 _param_spec: glib::ffi::gpointer,
210 f: glib::ffi::gpointer,
211 ) {
212 let f: &F = &*(f as *const F);
213 f(&from_glib_borrow(this))
214 }
215 unsafe {
216 let f: Box_<F> = Box_::new(f);
217 connect_raw(
218 self.as_ptr() as *mut _,
219 c"notify::with-alpha".as_ptr() as *const _,
220 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
221 notify_with_alpha_trampoline::<F> as *const (),
222 )),
223 Box_::into_raw(f),
224 )
225 }
226 }
227}
228
229#[cfg(feature = "v4_10")]
230#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
231impl Default for ColorDialog {
232 fn default() -> Self {
233 Self::new()
234 }
235}
236
237#[must_use = "The builder must be built to be used"]
242pub struct ColorDialogBuilder {
243 builder: glib::object::ObjectBuilder<'static, ColorDialog>,
244}
245
246impl ColorDialogBuilder {
247 fn new() -> Self {
248 Self {
249 builder: glib::object::Object::builder(),
250 }
251 }
252
253 #[cfg(feature = "v4_10")]
254 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
255 pub fn modal(self, modal: bool) -> Self {
256 Self {
257 builder: self.builder.property("modal", modal),
258 }
259 }
260
261 #[cfg(feature = "v4_10")]
262 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
263 pub fn title(self, title: impl Into<glib::GString>) -> Self {
264 Self {
265 builder: self.builder.property("title", title.into()),
266 }
267 }
268
269 #[cfg(feature = "v4_10")]
270 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
271 pub fn with_alpha(self, with_alpha: bool) -> Self {
272 Self {
273 builder: self.builder.property("with-alpha", with_alpha),
274 }
275 }
276
277 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
280 pub fn build(self) -> ColorDialog {
281 assert_initialized_main_thread!();
282 self.builder.build()
283 }
284}