1use crate::{
6 ffi, Accessible, AccessibleRole, Actionable, Align, Buildable, ConstraintTarget, LayoutManager,
7 Overflow, Widget,
8};
9use glib::{
10 object::ObjectType as _,
11 prelude::*,
12 signal::{connect_raw, SignalHandlerId},
13 translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18 #[doc(alias = "GtkSwitch")]
19 pub struct Switch(Object<ffi::GtkSwitch>) @extends Widget, @implements Accessible, Buildable, ConstraintTarget, Actionable;
20
21 match fn {
22 type_ => || ffi::gtk_switch_get_type(),
23 }
24}
25
26impl Switch {
27 #[doc(alias = "gtk_switch_new")]
28 pub fn new() -> Switch {
29 assert_initialized_main_thread!();
30 unsafe { Widget::from_glib_none(ffi::gtk_switch_new()).unsafe_cast() }
31 }
32
33 pub fn builder() -> SwitchBuilder {
38 SwitchBuilder::new()
39 }
40
41 #[doc(alias = "gtk_switch_get_active")]
42 #[doc(alias = "get_active")]
43 #[doc(alias = "active")]
44 pub fn is_active(&self) -> bool {
45 unsafe { from_glib(ffi::gtk_switch_get_active(self.to_glib_none().0)) }
46 }
47
48 #[doc(alias = "gtk_switch_get_state")]
49 #[doc(alias = "get_state")]
50 pub fn state(&self) -> bool {
51 unsafe { from_glib(ffi::gtk_switch_get_state(self.to_glib_none().0)) }
52 }
53
54 #[doc(alias = "gtk_switch_set_active")]
55 #[doc(alias = "active")]
56 pub fn set_active(&self, is_active: bool) {
57 unsafe {
58 ffi::gtk_switch_set_active(self.to_glib_none().0, is_active.into_glib());
59 }
60 }
61
62 #[doc(alias = "gtk_switch_set_state")]
63 #[doc(alias = "state")]
64 pub fn set_state(&self, state: bool) {
65 unsafe {
66 ffi::gtk_switch_set_state(self.to_glib_none().0, state.into_glib());
67 }
68 }
69
70 #[doc(alias = "activate")]
71 pub fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
72 unsafe extern "C" fn activate_trampoline<F: Fn(&Switch) + 'static>(
73 this: *mut ffi::GtkSwitch,
74 f: glib::ffi::gpointer,
75 ) {
76 let f: &F = &*(f as *const F);
77 f(&from_glib_borrow(this))
78 }
79 unsafe {
80 let f: Box_<F> = Box_::new(f);
81 connect_raw(
82 self.as_ptr() as *mut _,
83 c"activate".as_ptr() as *const _,
84 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
85 activate_trampoline::<F> as *const (),
86 )),
87 Box_::into_raw(f),
88 )
89 }
90 }
91
92 pub fn emit_activate(&self) {
93 self.emit_by_name::<()>("activate", &[]);
94 }
95
96 #[doc(alias = "state-set")]
97 pub fn connect_state_set<F: Fn(&Self, bool) -> glib::Propagation + 'static>(
98 &self,
99 f: F,
100 ) -> SignalHandlerId {
101 unsafe extern "C" fn state_set_trampoline<
102 F: Fn(&Switch, bool) -> glib::Propagation + 'static,
103 >(
104 this: *mut ffi::GtkSwitch,
105 state: glib::ffi::gboolean,
106 f: glib::ffi::gpointer,
107 ) -> glib::ffi::gboolean {
108 let f: &F = &*(f as *const F);
109 f(&from_glib_borrow(this), from_glib(state)).into_glib()
110 }
111 unsafe {
112 let f: Box_<F> = Box_::new(f);
113 connect_raw(
114 self.as_ptr() as *mut _,
115 c"state-set".as_ptr() as *const _,
116 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
117 state_set_trampoline::<F> as *const (),
118 )),
119 Box_::into_raw(f),
120 )
121 }
122 }
123
124 #[doc(alias = "active")]
125 pub fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
126 unsafe extern "C" fn notify_active_trampoline<F: Fn(&Switch) + 'static>(
127 this: *mut ffi::GtkSwitch,
128 _param_spec: glib::ffi::gpointer,
129 f: glib::ffi::gpointer,
130 ) {
131 let f: &F = &*(f as *const F);
132 f(&from_glib_borrow(this))
133 }
134 unsafe {
135 let f: Box_<F> = Box_::new(f);
136 connect_raw(
137 self.as_ptr() as *mut _,
138 c"notify::active".as_ptr() as *const _,
139 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
140 notify_active_trampoline::<F> as *const (),
141 )),
142 Box_::into_raw(f),
143 )
144 }
145 }
146
147 #[doc(alias = "state")]
148 pub fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
149 unsafe extern "C" fn notify_state_trampoline<F: Fn(&Switch) + 'static>(
150 this: *mut ffi::GtkSwitch,
151 _param_spec: glib::ffi::gpointer,
152 f: glib::ffi::gpointer,
153 ) {
154 let f: &F = &*(f as *const F);
155 f(&from_glib_borrow(this))
156 }
157 unsafe {
158 let f: Box_<F> = Box_::new(f);
159 connect_raw(
160 self.as_ptr() as *mut _,
161 c"notify::state".as_ptr() as *const _,
162 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
163 notify_state_trampoline::<F> as *const (),
164 )),
165 Box_::into_raw(f),
166 )
167 }
168 }
169}
170
171impl Default for Switch {
172 fn default() -> Self {
173 Self::new()
174 }
175}
176
177#[must_use = "The builder must be built to be used"]
182pub struct SwitchBuilder {
183 builder: glib::object::ObjectBuilder<'static, Switch>,
184}
185
186impl SwitchBuilder {
187 fn new() -> Self {
188 Self {
189 builder: glib::object::Object::builder(),
190 }
191 }
192
193 pub fn active(self, active: bool) -> Self {
194 Self {
195 builder: self.builder.property("active", active),
196 }
197 }
198
199 pub fn state(self, state: bool) -> Self {
200 Self {
201 builder: self.builder.property("state", state),
202 }
203 }
204
205 pub fn can_focus(self, can_focus: bool) -> Self {
206 Self {
207 builder: self.builder.property("can-focus", can_focus),
208 }
209 }
210
211 pub fn can_target(self, can_target: bool) -> Self {
212 Self {
213 builder: self.builder.property("can-target", can_target),
214 }
215 }
216
217 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
218 Self {
219 builder: self.builder.property("css-classes", css_classes.into()),
220 }
221 }
222
223 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
224 Self {
225 builder: self.builder.property("css-name", css_name.into()),
226 }
227 }
228
229 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
230 Self {
231 builder: self.builder.property("cursor", cursor.clone()),
232 }
233 }
234
235 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
236 Self {
237 builder: self.builder.property("focus-on-click", focus_on_click),
238 }
239 }
240
241 pub fn focusable(self, focusable: bool) -> Self {
242 Self {
243 builder: self.builder.property("focusable", focusable),
244 }
245 }
246
247 pub fn halign(self, halign: Align) -> Self {
248 Self {
249 builder: self.builder.property("halign", halign),
250 }
251 }
252
253 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
254 Self {
255 builder: self.builder.property("has-tooltip", has_tooltip),
256 }
257 }
258
259 pub fn height_request(self, height_request: i32) -> Self {
260 Self {
261 builder: self.builder.property("height-request", height_request),
262 }
263 }
264
265 pub fn hexpand(self, hexpand: bool) -> Self {
266 Self {
267 builder: self.builder.property("hexpand", hexpand),
268 }
269 }
270
271 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
272 Self {
273 builder: self.builder.property("hexpand-set", hexpand_set),
274 }
275 }
276
277 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
278 Self {
279 builder: self
280 .builder
281 .property("layout-manager", layout_manager.clone().upcast()),
282 }
283 }
284
285 #[cfg(feature = "v4_18")]
286 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
287 pub fn limit_events(self, limit_events: bool) -> Self {
288 Self {
289 builder: self.builder.property("limit-events", limit_events),
290 }
291 }
292
293 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
294 Self {
295 builder: self.builder.property("margin-bottom", margin_bottom),
296 }
297 }
298
299 pub fn margin_end(self, margin_end: i32) -> Self {
300 Self {
301 builder: self.builder.property("margin-end", margin_end),
302 }
303 }
304
305 pub fn margin_start(self, margin_start: i32) -> Self {
306 Self {
307 builder: self.builder.property("margin-start", margin_start),
308 }
309 }
310
311 pub fn margin_top(self, margin_top: i32) -> Self {
312 Self {
313 builder: self.builder.property("margin-top", margin_top),
314 }
315 }
316
317 pub fn name(self, name: impl Into<glib::GString>) -> Self {
318 Self {
319 builder: self.builder.property("name", name.into()),
320 }
321 }
322
323 pub fn opacity(self, opacity: f64) -> Self {
324 Self {
325 builder: self.builder.property("opacity", opacity),
326 }
327 }
328
329 pub fn overflow(self, overflow: Overflow) -> Self {
330 Self {
331 builder: self.builder.property("overflow", overflow),
332 }
333 }
334
335 pub fn receives_default(self, receives_default: bool) -> Self {
336 Self {
337 builder: self.builder.property("receives-default", receives_default),
338 }
339 }
340
341 pub fn sensitive(self, sensitive: bool) -> Self {
342 Self {
343 builder: self.builder.property("sensitive", sensitive),
344 }
345 }
346
347 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
348 Self {
349 builder: self
350 .builder
351 .property("tooltip-markup", tooltip_markup.into()),
352 }
353 }
354
355 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
356 Self {
357 builder: self.builder.property("tooltip-text", tooltip_text.into()),
358 }
359 }
360
361 pub fn valign(self, valign: Align) -> Self {
362 Self {
363 builder: self.builder.property("valign", valign),
364 }
365 }
366
367 pub fn vexpand(self, vexpand: bool) -> Self {
368 Self {
369 builder: self.builder.property("vexpand", vexpand),
370 }
371 }
372
373 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
374 Self {
375 builder: self.builder.property("vexpand-set", vexpand_set),
376 }
377 }
378
379 pub fn visible(self, visible: bool) -> Self {
380 Self {
381 builder: self.builder.property("visible", visible),
382 }
383 }
384
385 pub fn width_request(self, width_request: i32) -> Self {
386 Self {
387 builder: self.builder.property("width-request", width_request),
388 }
389 }
390
391 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
392 Self {
393 builder: self.builder.property("accessible-role", accessible_role),
394 }
395 }
396
397 pub fn action_name(self, action_name: impl Into<glib::GString>) -> Self {
398 Self {
399 builder: self.builder.property("action-name", action_name.into()),
400 }
401 }
402
403 pub fn action_target(self, action_target: &glib::Variant) -> Self {
404 Self {
405 builder: self
406 .builder
407 .property("action-target", action_target.clone()),
408 }
409 }
410
411 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
414 pub fn build(self) -> Switch {
415 assert_initialized_main_thread!();
416 self.builder.build()
417 }
418}