gtk4/auto/switch.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::{
6 Accessible, AccessibleRole, Actionable, Align, Buildable, ConstraintTarget, LayoutManager,
7 Overflow, Widget, ffi,
8};
9use glib::{
10 object::ObjectType as _,
11 prelude::*,
12 signal::{SignalHandlerId, connect_raw},
13 translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18 /// Shows a "light switch" that has two states: on or off.
19 ///
20 /// <picture>
21 /// <source srcset="switch-dark.png" media="(prefers-color-scheme: dark)">
22 /// <img alt="An example GtkSwitch" src="switch.png">
23 /// </picture>
24 ///
25 /// The user can control which state should be active by clicking the
26 /// empty area, or by dragging the slider.
27 ///
28 /// [`Switch`][crate::Switch] can also express situations where the underlying state changes
29 /// with a delay. In this case, the slider position indicates the user's recent
30 /// change (represented by the [`active`][struct@crate::Switch#active] property), while the
31 /// trough color indicates the present underlying state (represented by the
32 /// [`state`][struct@crate::Switch#state] property).
33 ///
34 /// <picture>
35 /// <source srcset="switch-state-dark.png" media="(prefers-color-scheme: dark)">
36 /// <img alt="GtkSwitch with delayed state change" src="switch-state.png">
37 /// </picture>
38 ///
39 /// See [`state-set`][struct@crate::Switch#state-set] for details.
40 ///
41 /// # Shortcuts and Gestures
42 ///
43 /// [`Switch`][crate::Switch] supports pan and drag gestures to move the slider.
44 ///
45 /// # CSS nodes
46 ///
47 /// ```text
48 /// switch
49 /// ├── image
50 /// ├── image
51 /// ╰── slider
52 /// ```
53 ///
54 /// [`Switch`][crate::Switch] has four css nodes, the main node with the name switch and
55 /// subnodes for the slider and the on and off images. Neither of them is
56 /// using any style classes.
57 ///
58 /// # Accessibility
59 ///
60 /// [`Switch`][crate::Switch] uses the [enum@Gtk.AccessibleRole.switch] role.
61 ///
62 /// ## Properties
63 ///
64 ///
65 /// #### `active`
66 /// Whether the [`Switch`][crate::Switch] widget is in its on or off state.
67 ///
68 /// Readable | Writeable
69 ///
70 ///
71 /// #### `state`
72 /// The backend state that is controlled by the switch.
73 ///
74 /// Applications should usually set the [`active`][struct@crate::Switch#active] property,
75 /// except when indicating a change to the backend state which occurs
76 /// separately from the user's interaction.
77 ///
78 /// See [`state-set`][struct@crate::Switch#state-set] for details.
79 ///
80 /// Readable | Writeable
81 /// <details><summary><h4>Widget</h4></summary>
82 ///
83 ///
84 /// #### `can-focus`
85 /// Whether the widget or any of its descendents can accept
86 /// the input focus.
87 ///
88 /// This property is meant to be set by widget implementations,
89 /// typically in their instance init function.
90 ///
91 /// Readable | Writeable
92 ///
93 ///
94 /// #### `can-target`
95 /// Whether the widget can receive pointer events.
96 ///
97 /// Readable | Writeable
98 ///
99 ///
100 /// #### `css-classes`
101 /// A list of css classes applied to this widget.
102 ///
103 /// Readable | Writeable
104 ///
105 ///
106 /// #### `css-name`
107 /// The name of this widget in the CSS tree.
108 ///
109 /// This property is meant to be set by widget implementations,
110 /// typically in their instance init function.
111 ///
112 /// Readable | Writeable | Construct Only
113 ///
114 ///
115 /// #### `cursor`
116 /// The cursor used by @widget.
117 ///
118 /// Readable | Writeable
119 ///
120 ///
121 /// #### `focus-on-click`
122 /// Whether the widget should grab focus when it is clicked with the mouse.
123 ///
124 /// This property is only relevant for widgets that can take focus.
125 ///
126 /// Readable | Writeable
127 ///
128 ///
129 /// #### `focusable`
130 /// Whether this widget itself will accept the input focus.
131 ///
132 /// Readable | Writeable
133 ///
134 ///
135 /// #### `halign`
136 /// How to distribute horizontal space if widget gets extra space.
137 ///
138 /// Readable | Writeable
139 ///
140 ///
141 /// #### `has-default`
142 /// Whether the widget is the default widget.
143 ///
144 /// Readable
145 ///
146 ///
147 /// #### `has-focus`
148 /// Whether the widget has the input focus.
149 ///
150 /// Readable
151 ///
152 ///
153 /// #### `has-tooltip`
154 /// Enables or disables the emission of the [`query-tooltip`][struct@crate::Widget#query-tooltip]
155 /// signal on @widget.
156 ///
157 /// A true value indicates that @widget can have a tooltip, in this case
158 /// the widget will be queried using [`query-tooltip`][struct@crate::Widget#query-tooltip] to
159 /// determine whether it will provide a tooltip or not.
160 ///
161 /// Readable | Writeable
162 ///
163 ///
164 /// #### `height-request`
165 /// Overrides for height request of the widget.
166 ///
167 /// If this is -1, the natural request will be used.
168 ///
169 /// Readable | Writeable
170 ///
171 ///
172 /// #### `hexpand`
173 /// Whether to expand horizontally.
174 ///
175 /// Readable | Writeable
176 ///
177 ///
178 /// #### `hexpand-set`
179 /// Whether to use the `hexpand` property.
180 ///
181 /// Readable | Writeable
182 ///
183 ///
184 /// #### `layout-manager`
185 /// The [`LayoutManager`][crate::LayoutManager] instance to use to compute
186 /// the preferred size of the widget, and allocate its children.
187 ///
188 /// This property is meant to be set by widget implementations,
189 /// typically in their instance init function.
190 ///
191 /// Readable | Writeable
192 ///
193 ///
194 /// #### `limit-events`
195 /// Makes this widget act like a modal dialog, with respect to
196 /// event delivery.
197 ///
198 /// Global event controllers will not handle events with targets
199 /// inside the widget, unless they are set up to ignore propagation
200 /// limits. See [`EventControllerExt::set_propagation_limit()`][crate::prelude::EventControllerExt::set_propagation_limit()].
201 ///
202 /// Readable | Writeable
203 ///
204 ///
205 /// #### `margin-bottom`
206 /// Margin on bottom side of widget.
207 ///
208 /// This property adds margin outside of the widget's normal size
209 /// request, the margin will be added in addition to the size from
210 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
211 ///
212 /// Readable | Writeable
213 ///
214 ///
215 /// #### `margin-end`
216 /// Margin on end of widget, horizontally.
217 ///
218 /// This property supports left-to-right and right-to-left text
219 /// directions.
220 ///
221 /// This property adds margin outside of the widget's normal size
222 /// request, the margin will be added in addition to the size from
223 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
224 ///
225 /// Readable | Writeable
226 ///
227 ///
228 /// #### `margin-start`
229 /// Margin on start of widget, horizontally.
230 ///
231 /// This property supports left-to-right and right-to-left text
232 /// directions.
233 ///
234 /// This property adds margin outside of the widget's normal size
235 /// request, the margin will be added in addition to the size from
236 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
237 ///
238 /// Readable | Writeable
239 ///
240 ///
241 /// #### `margin-top`
242 /// Margin on top side of widget.
243 ///
244 /// This property adds margin outside of the widget's normal size
245 /// request, the margin will be added in addition to the size from
246 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
247 ///
248 /// Readable | Writeable
249 ///
250 ///
251 /// #### `name`
252 /// The name of the widget.
253 ///
254 /// Readable | Writeable
255 ///
256 ///
257 /// #### `opacity`
258 /// The requested opacity of the widget.
259 ///
260 /// Readable | Writeable
261 ///
262 ///
263 /// #### `overflow`
264 /// How content outside the widget's content area is treated.
265 ///
266 /// This property is meant to be set by widget implementations,
267 /// typically in their instance init function.
268 ///
269 /// Readable | Writeable
270 ///
271 ///
272 /// #### `parent`
273 /// The parent widget of this widget.
274 ///
275 /// Readable
276 ///
277 ///
278 /// #### `receives-default`
279 /// Whether the widget will receive the default action when it is focused.
280 ///
281 /// Readable | Writeable
282 ///
283 ///
284 /// #### `root`
285 /// The [`Root`][crate::Root] widget of the widget tree containing this widget.
286 ///
287 /// This will be `NULL` if the widget is not contained in a root widget.
288 ///
289 /// Readable
290 ///
291 ///
292 /// #### `scale-factor`
293 /// The scale factor of the widget.
294 ///
295 /// Readable
296 ///
297 ///
298 /// #### `sensitive`
299 /// Whether the widget responds to input.
300 ///
301 /// Readable | Writeable
302 ///
303 ///
304 /// #### `tooltip-markup`
305 /// Sets the text of tooltip to be the given string, which is marked up
306 /// with Pango markup.
307 ///
308 /// Also see [`Tooltip::set_markup()`][crate::Tooltip::set_markup()].
309 ///
310 /// This is a convenience property which will take care of getting the
311 /// tooltip shown if the given string is not `NULL`:
312 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
313 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
314 /// the default signal handler.
315 ///
316 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
317 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
318 ///
319 /// Readable | Writeable
320 ///
321 ///
322 /// #### `tooltip-text`
323 /// Sets the text of tooltip to be the given string.
324 ///
325 /// Also see [`Tooltip::set_text()`][crate::Tooltip::set_text()].
326 ///
327 /// This is a convenience property which will take care of getting the
328 /// tooltip shown if the given string is not `NULL`:
329 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
330 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
331 /// the default signal handler.
332 ///
333 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
334 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
335 ///
336 /// Readable | Writeable
337 ///
338 ///
339 /// #### `valign`
340 /// How to distribute vertical space if widget gets extra space.
341 ///
342 /// Readable | Writeable
343 ///
344 ///
345 /// #### `vexpand`
346 /// Whether to expand vertically.
347 ///
348 /// Readable | Writeable
349 ///
350 ///
351 /// #### `vexpand-set`
352 /// Whether to use the `vexpand` property.
353 ///
354 /// Readable | Writeable
355 ///
356 ///
357 /// #### `visible`
358 /// Whether the widget is visible.
359 ///
360 /// Readable | Writeable
361 ///
362 ///
363 /// #### `width-request`
364 /// Overrides for width request of the widget.
365 ///
366 /// If this is -1, the natural request will be used.
367 ///
368 /// Readable | Writeable
369 /// </details>
370 /// <details><summary><h4>Accessible</h4></summary>
371 ///
372 ///
373 /// #### `accessible-role`
374 /// The accessible role of the given [`Accessible`][crate::Accessible] implementation.
375 ///
376 /// The accessible role cannot be changed once set.
377 ///
378 /// Readable | Writeable
379 /// </details>
380 /// <details><summary><h4>Actionable</h4></summary>
381 ///
382 ///
383 /// #### `action-name`
384 /// The name of the action with which this widget should be associated.
385 ///
386 /// Readable | Writeable
387 ///
388 ///
389 /// #### `action-target`
390 /// The target value of the actionable widget's action.
391 ///
392 /// Readable | Writeable
393 /// </details>
394 ///
395 /// ## Signals
396 ///
397 ///
398 /// #### `activate`
399 /// Emitted to animate the switch.
400 ///
401 /// Applications should never connect to this signal,
402 /// but use the [`active`][struct@crate::Switch#active] property.
403 ///
404 /// Action
405 ///
406 ///
407 /// #### `state-set`
408 /// Emitted to change the underlying state.
409 ///
410 /// The ::state-set signal is emitted when the user changes the switch
411 /// position. The default handler calls [`Switch::set_state()`][crate::Switch::set_state()] with the
412 /// value of @state.
413 ///
414 /// To implement delayed state change, applications can connect to this
415 /// signal, initiate the change of the underlying state, and call
416 /// [`Switch::set_state()`][crate::Switch::set_state()] when the underlying state change is
417 /// complete. The signal handler should return [`true`] to prevent the
418 /// default handler from running.
419 ///
420 ///
421 /// <details><summary><h4>Widget</h4></summary>
422 ///
423 ///
424 /// #### `destroy`
425 /// Signals that all holders of a reference to the widget should release
426 /// the reference that they hold.
427 ///
428 /// May result in finalization of the widget if all references are released.
429 ///
430 /// This signal is not suitable for saving widget state.
431 ///
432 ///
433 ///
434 ///
435 /// #### `direction-changed`
436 /// Emitted when the text direction of a widget changes.
437 ///
438 ///
439 ///
440 ///
441 /// #### `hide`
442 /// Emitted when @widget is hidden.
443 ///
444 ///
445 ///
446 ///
447 /// #### `keynav-failed`
448 /// Emitted if keyboard navigation fails.
449 ///
450 /// See [`WidgetExt::keynav_failed()`][crate::prelude::WidgetExt::keynav_failed()] for details.
451 ///
452 ///
453 ///
454 ///
455 /// #### `map`
456 /// Emitted when @widget is going to be mapped.
457 ///
458 /// A widget is mapped when the widget is visible (which is controlled with
459 /// [`visible`][struct@crate::Widget#visible]) and all its parents up to the toplevel widget
460 /// are also visible.
461 ///
462 /// The `::map` signal can be used to determine whether a widget will be drawn,
463 /// for instance it can resume an animation that was stopped during the
464 /// emission of [`unmap`][struct@crate::Widget#unmap].
465 ///
466 ///
467 ///
468 ///
469 /// #### `mnemonic-activate`
470 /// Emitted when a widget is activated via a mnemonic.
471 ///
472 /// The default handler for this signal activates @widget if @group_cycling
473 /// is false, or just makes @widget grab focus if @group_cycling is true.
474 ///
475 ///
476 ///
477 ///
478 /// #### `move-focus`
479 /// Emitted when the focus is moved.
480 ///
481 /// The `::move-focus` signal is a [keybinding signal](class.SignalAction.html).
482 ///
483 /// The default bindings for this signal are <kbd>Tab</kbd> to move forward,
484 /// and <kbd>Shift</kbd>+<kbd>Tab</kbd> to move backward.
485 ///
486 /// Action
487 ///
488 ///
489 /// #### `query-tooltip`
490 /// Emitted when the widget’s tooltip is about to be shown.
491 ///
492 /// This happens when the [`has-tooltip`][struct@crate::Widget#has-tooltip] property
493 /// is true and the hover timeout has expired with the cursor hovering
494 /// above @widget; or emitted when @widget got focus in keyboard mode.
495 ///
496 /// Using the given coordinates, the signal handler should determine
497 /// whether a tooltip should be shown for @widget. If this is the case
498 /// true should be returned, false otherwise. Note that if @keyboard_mode
499 /// is true, the values of @x and @y are undefined and should not be used.
500 ///
501 /// The signal handler is free to manipulate @tooltip with the therefore
502 /// destined function calls.
503 ///
504 ///
505 ///
506 ///
507 /// #### `realize`
508 /// Emitted when @widget is associated with a [`gdk::Surface`][crate::gdk::Surface].
509 ///
510 /// This means that [`WidgetExt::realize()`][crate::prelude::WidgetExt::realize()] has been called
511 /// or the widget has been mapped (that is, it is going to be drawn).
512 ///
513 ///
514 ///
515 ///
516 /// #### `show`
517 /// Emitted when @widget is shown.
518 ///
519 ///
520 ///
521 ///
522 /// #### `state-flags-changed`
523 /// Emitted when the widget state changes.
524 ///
525 /// See [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()].
526 ///
527 ///
528 ///
529 ///
530 /// #### `unmap`
531 /// Emitted when @widget is going to be unmapped.
532 ///
533 /// A widget is unmapped when either it or any of its parents up to the
534 /// toplevel widget have been set as hidden.
535 ///
536 /// As `::unmap` indicates that a widget will not be shown any longer,
537 /// it can be used to, for example, stop an animation on the widget.
538 ///
539 ///
540 ///
541 ///
542 /// #### `unrealize`
543 /// Emitted when the [`gdk::Surface`][crate::gdk::Surface] associated with @widget is destroyed.
544 ///
545 /// This means that [`WidgetExt::unrealize()`][crate::prelude::WidgetExt::unrealize()] has been called
546 /// or the widget has been unmapped (that is, it is going to be hidden).
547 ///
548 ///
549 /// </details>
550 ///
551 /// # Implements
552 ///
553 /// [`WidgetExt`][trait@crate::prelude::WidgetExt], [`trait@glib::ObjectExt`], [`AccessibleExt`][trait@crate::prelude::AccessibleExt], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`ConstraintTargetExt`][trait@crate::prelude::ConstraintTargetExt], [`ActionableExt`][trait@crate::prelude::ActionableExt], [`WidgetExtManual`][trait@crate::prelude::WidgetExtManual], [`AccessibleExtManual`][trait@crate::prelude::AccessibleExtManual], [`ActionableExtManual`][trait@crate::prelude::ActionableExtManual]
554 #[doc(alias = "GtkSwitch")]
555 pub struct Switch(Object<ffi::GtkSwitch>) @extends Widget, @implements Accessible, Buildable, ConstraintTarget, Actionable;
556
557 match fn {
558 type_ => || ffi::gtk_switch_get_type(),
559 }
560}
561
562impl Switch {
563 /// Creates a new [`Switch`][crate::Switch] widget.
564 ///
565 /// # Returns
566 ///
567 /// the newly created [`Switch`][crate::Switch] instance
568 #[doc(alias = "gtk_switch_new")]
569 pub fn new() -> Switch {
570 assert_initialized_main_thread!();
571 unsafe { Widget::from_glib_none(ffi::gtk_switch_new()).unsafe_cast() }
572 }
573
574 // rustdoc-stripper-ignore-next
575 /// Creates a new builder-pattern struct instance to construct [`Switch`] objects.
576 ///
577 /// This method returns an instance of [`SwitchBuilder`](crate::builders::SwitchBuilder) which can be used to create [`Switch`] objects.
578 pub fn builder() -> SwitchBuilder {
579 SwitchBuilder::new()
580 }
581
582 /// Gets whether the [`Switch`][crate::Switch] is in its “on” or “off” state.
583 ///
584 /// # Returns
585 ///
586 /// [`true`] if the [`Switch`][crate::Switch] is active, and [`false`] otherwise
587 #[doc(alias = "gtk_switch_get_active")]
588 #[doc(alias = "get_active")]
589 #[doc(alias = "active")]
590 pub fn is_active(&self) -> bool {
591 unsafe { from_glib(ffi::gtk_switch_get_active(self.to_glib_none().0)) }
592 }
593
594 /// Gets the underlying state of the [`Switch`][crate::Switch].
595 ///
596 /// # Returns
597 ///
598 /// the underlying state
599 #[doc(alias = "gtk_switch_get_state")]
600 #[doc(alias = "get_state")]
601 pub fn state(&self) -> bool {
602 unsafe { from_glib(ffi::gtk_switch_get_state(self.to_glib_none().0)) }
603 }
604
605 /// Changes the state of @self to the desired one.
606 /// ## `is_active`
607 /// [`true`] if @self should be active, and [`false`] otherwise
608 #[doc(alias = "gtk_switch_set_active")]
609 #[doc(alias = "active")]
610 pub fn set_active(&self, is_active: bool) {
611 unsafe {
612 ffi::gtk_switch_set_active(self.to_glib_none().0, is_active.into_glib());
613 }
614 }
615
616 /// Sets the underlying state of the [`Switch`][crate::Switch].
617 ///
618 /// This function is typically called from a [`state-set`][struct@crate::Switch#state-set]
619 /// signal handler in order to set up delayed state changes.
620 ///
621 /// See [`state-set`][struct@crate::Switch#state-set] for details.
622 /// ## `state`
623 /// the new state
624 #[doc(alias = "gtk_switch_set_state")]
625 #[doc(alias = "state")]
626 pub fn set_state(&self, state: bool) {
627 unsafe {
628 ffi::gtk_switch_set_state(self.to_glib_none().0, state.into_glib());
629 }
630 }
631
632 /// Emitted to animate the switch.
633 ///
634 /// Applications should never connect to this signal,
635 /// but use the [`active`][struct@crate::Switch#active] property.
636 #[doc(alias = "activate")]
637 pub fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
638 unsafe extern "C" fn activate_trampoline<F: Fn(&Switch) + 'static>(
639 this: *mut ffi::GtkSwitch,
640 f: glib::ffi::gpointer,
641 ) {
642 unsafe {
643 let f: &F = &*(f as *const F);
644 f(&from_glib_borrow(this))
645 }
646 }
647 unsafe {
648 let f: Box_<F> = Box_::new(f);
649 connect_raw(
650 self.as_ptr() as *mut _,
651 c"activate".as_ptr() as *const _,
652 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
653 activate_trampoline::<F> as *const (),
654 )),
655 Box_::into_raw(f),
656 )
657 }
658 }
659
660 pub fn emit_activate(&self) {
661 self.emit_by_name::<()>("activate", &[]);
662 }
663
664 /// Emitted to change the underlying state.
665 ///
666 /// The ::state-set signal is emitted when the user changes the switch
667 /// position. The default handler calls [`set_state()`][Self::set_state()] with the
668 /// value of @state.
669 ///
670 /// To implement delayed state change, applications can connect to this
671 /// signal, initiate the change of the underlying state, and call
672 /// [`set_state()`][Self::set_state()] when the underlying state change is
673 /// complete. The signal handler should return [`true`] to prevent the
674 /// default handler from running.
675 /// ## `state`
676 /// the new state of the switch
677 ///
678 /// # Returns
679 ///
680 /// [`true`] to stop the signal emission
681 #[doc(alias = "state-set")]
682 pub fn connect_state_set<F: Fn(&Self, bool) -> glib::Propagation + 'static>(
683 &self,
684 f: F,
685 ) -> SignalHandlerId {
686 unsafe extern "C" fn state_set_trampoline<
687 F: Fn(&Switch, bool) -> glib::Propagation + 'static,
688 >(
689 this: *mut ffi::GtkSwitch,
690 state: glib::ffi::gboolean,
691 f: glib::ffi::gpointer,
692 ) -> glib::ffi::gboolean {
693 unsafe {
694 let f: &F = &*(f as *const F);
695 f(&from_glib_borrow(this), from_glib(state)).into_glib()
696 }
697 }
698 unsafe {
699 let f: Box_<F> = Box_::new(f);
700 connect_raw(
701 self.as_ptr() as *mut _,
702 c"state-set".as_ptr() as *const _,
703 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
704 state_set_trampoline::<F> as *const (),
705 )),
706 Box_::into_raw(f),
707 )
708 }
709 }
710
711 #[doc(alias = "active")]
712 pub fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
713 unsafe extern "C" fn notify_active_trampoline<F: Fn(&Switch) + 'static>(
714 this: *mut ffi::GtkSwitch,
715 _param_spec: glib::ffi::gpointer,
716 f: glib::ffi::gpointer,
717 ) {
718 unsafe {
719 let f: &F = &*(f as *const F);
720 f(&from_glib_borrow(this))
721 }
722 }
723 unsafe {
724 let f: Box_<F> = Box_::new(f);
725 connect_raw(
726 self.as_ptr() as *mut _,
727 c"notify::active".as_ptr() as *const _,
728 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
729 notify_active_trampoline::<F> as *const (),
730 )),
731 Box_::into_raw(f),
732 )
733 }
734 }
735
736 #[doc(alias = "state")]
737 pub fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
738 unsafe extern "C" fn notify_state_trampoline<F: Fn(&Switch) + 'static>(
739 this: *mut ffi::GtkSwitch,
740 _param_spec: glib::ffi::gpointer,
741 f: glib::ffi::gpointer,
742 ) {
743 unsafe {
744 let f: &F = &*(f as *const F);
745 f(&from_glib_borrow(this))
746 }
747 }
748 unsafe {
749 let f: Box_<F> = Box_::new(f);
750 connect_raw(
751 self.as_ptr() as *mut _,
752 c"notify::state".as_ptr() as *const _,
753 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
754 notify_state_trampoline::<F> as *const (),
755 )),
756 Box_::into_raw(f),
757 )
758 }
759 }
760}
761
762impl Default for Switch {
763 fn default() -> Self {
764 Self::new()
765 }
766}
767
768// rustdoc-stripper-ignore-next
769/// A [builder-pattern] type to construct [`Switch`] objects.
770///
771/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
772#[must_use = "The builder must be built to be used"]
773pub struct SwitchBuilder {
774 builder: glib::object::ObjectBuilder<'static, Switch>,
775}
776
777impl SwitchBuilder {
778 fn new() -> Self {
779 Self {
780 builder: glib::object::Object::builder(),
781 }
782 }
783
784 /// Whether the [`Switch`][crate::Switch] widget is in its on or off state.
785 pub fn active(self, active: bool) -> Self {
786 Self {
787 builder: self.builder.property("active", active),
788 }
789 }
790
791 /// The backend state that is controlled by the switch.
792 ///
793 /// Applications should usually set the [`active`][struct@crate::Switch#active] property,
794 /// except when indicating a change to the backend state which occurs
795 /// separately from the user's interaction.
796 ///
797 /// See [`state-set`][struct@crate::Switch#state-set] for details.
798 pub fn state(self, state: bool) -> Self {
799 Self {
800 builder: self.builder.property("state", state),
801 }
802 }
803
804 /// Whether the widget or any of its descendents can accept
805 /// the input focus.
806 ///
807 /// This property is meant to be set by widget implementations,
808 /// typically in their instance init function.
809 pub fn can_focus(self, can_focus: bool) -> Self {
810 Self {
811 builder: self.builder.property("can-focus", can_focus),
812 }
813 }
814
815 /// Whether the widget can receive pointer events.
816 pub fn can_target(self, can_target: bool) -> Self {
817 Self {
818 builder: self.builder.property("can-target", can_target),
819 }
820 }
821
822 /// A list of css classes applied to this widget.
823 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
824 Self {
825 builder: self.builder.property("css-classes", css_classes.into()),
826 }
827 }
828
829 /// The name of this widget in the CSS tree.
830 ///
831 /// This property is meant to be set by widget implementations,
832 /// typically in their instance init function.
833 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
834 Self {
835 builder: self.builder.property("css-name", css_name.into()),
836 }
837 }
838
839 /// The cursor used by @widget.
840 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
841 Self {
842 builder: self.builder.property("cursor", cursor.clone()),
843 }
844 }
845
846 /// Whether the widget should grab focus when it is clicked with the mouse.
847 ///
848 /// This property is only relevant for widgets that can take focus.
849 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
850 Self {
851 builder: self.builder.property("focus-on-click", focus_on_click),
852 }
853 }
854
855 /// Whether this widget itself will accept the input focus.
856 pub fn focusable(self, focusable: bool) -> Self {
857 Self {
858 builder: self.builder.property("focusable", focusable),
859 }
860 }
861
862 /// How to distribute horizontal space if widget gets extra space.
863 pub fn halign(self, halign: Align) -> Self {
864 Self {
865 builder: self.builder.property("halign", halign),
866 }
867 }
868
869 /// Enables or disables the emission of the [`query-tooltip`][struct@crate::Widget#query-tooltip]
870 /// signal on @widget.
871 ///
872 /// A true value indicates that @widget can have a tooltip, in this case
873 /// the widget will be queried using [`query-tooltip`][struct@crate::Widget#query-tooltip] to
874 /// determine whether it will provide a tooltip or not.
875 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
876 Self {
877 builder: self.builder.property("has-tooltip", has_tooltip),
878 }
879 }
880
881 /// Overrides for height request of the widget.
882 ///
883 /// If this is -1, the natural request will be used.
884 pub fn height_request(self, height_request: i32) -> Self {
885 Self {
886 builder: self.builder.property("height-request", height_request),
887 }
888 }
889
890 /// Whether to expand horizontally.
891 pub fn hexpand(self, hexpand: bool) -> Self {
892 Self {
893 builder: self.builder.property("hexpand", hexpand),
894 }
895 }
896
897 /// Whether to use the `hexpand` property.
898 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
899 Self {
900 builder: self.builder.property("hexpand-set", hexpand_set),
901 }
902 }
903
904 /// The [`LayoutManager`][crate::LayoutManager] instance to use to compute
905 /// the preferred size of the widget, and allocate its children.
906 ///
907 /// This property is meant to be set by widget implementations,
908 /// typically in their instance init function.
909 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
910 Self {
911 builder: self
912 .builder
913 .property("layout-manager", layout_manager.clone().upcast()),
914 }
915 }
916
917 /// Makes this widget act like a modal dialog, with respect to
918 /// event delivery.
919 ///
920 /// Global event controllers will not handle events with targets
921 /// inside the widget, unless they are set up to ignore propagation
922 /// limits. See [`EventControllerExt::set_propagation_limit()`][crate::prelude::EventControllerExt::set_propagation_limit()].
923 #[cfg(feature = "v4_18")]
924 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
925 pub fn limit_events(self, limit_events: bool) -> Self {
926 Self {
927 builder: self.builder.property("limit-events", limit_events),
928 }
929 }
930
931 /// Margin on bottom side of widget.
932 ///
933 /// This property adds margin outside of the widget's normal size
934 /// request, the margin will be added in addition to the size from
935 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
936 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
937 Self {
938 builder: self.builder.property("margin-bottom", margin_bottom),
939 }
940 }
941
942 /// Margin on end of widget, horizontally.
943 ///
944 /// This property supports left-to-right and right-to-left text
945 /// directions.
946 ///
947 /// This property adds margin outside of the widget's normal size
948 /// request, the margin will be added in addition to the size from
949 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
950 pub fn margin_end(self, margin_end: i32) -> Self {
951 Self {
952 builder: self.builder.property("margin-end", margin_end),
953 }
954 }
955
956 /// Margin on start of widget, horizontally.
957 ///
958 /// This property supports left-to-right and right-to-left text
959 /// directions.
960 ///
961 /// This property adds margin outside of the widget's normal size
962 /// request, the margin will be added in addition to the size from
963 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
964 pub fn margin_start(self, margin_start: i32) -> Self {
965 Self {
966 builder: self.builder.property("margin-start", margin_start),
967 }
968 }
969
970 /// Margin on top side of widget.
971 ///
972 /// This property adds margin outside of the widget's normal size
973 /// request, the margin will be added in addition to the size from
974 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
975 pub fn margin_top(self, margin_top: i32) -> Self {
976 Self {
977 builder: self.builder.property("margin-top", margin_top),
978 }
979 }
980
981 /// The name of the widget.
982 pub fn name(self, name: impl Into<glib::GString>) -> Self {
983 Self {
984 builder: self.builder.property("name", name.into()),
985 }
986 }
987
988 /// The requested opacity of the widget.
989 pub fn opacity(self, opacity: f64) -> Self {
990 Self {
991 builder: self.builder.property("opacity", opacity),
992 }
993 }
994
995 /// How content outside the widget's content area is treated.
996 ///
997 /// This property is meant to be set by widget implementations,
998 /// typically in their instance init function.
999 pub fn overflow(self, overflow: Overflow) -> Self {
1000 Self {
1001 builder: self.builder.property("overflow", overflow),
1002 }
1003 }
1004
1005 /// Whether the widget will receive the default action when it is focused.
1006 pub fn receives_default(self, receives_default: bool) -> Self {
1007 Self {
1008 builder: self.builder.property("receives-default", receives_default),
1009 }
1010 }
1011
1012 /// Whether the widget responds to input.
1013 pub fn sensitive(self, sensitive: bool) -> Self {
1014 Self {
1015 builder: self.builder.property("sensitive", sensitive),
1016 }
1017 }
1018
1019 /// Sets the text of tooltip to be the given string, which is marked up
1020 /// with Pango markup.
1021 ///
1022 /// Also see [`Tooltip::set_markup()`][crate::Tooltip::set_markup()].
1023 ///
1024 /// This is a convenience property which will take care of getting the
1025 /// tooltip shown if the given string is not `NULL`:
1026 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
1027 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
1028 /// the default signal handler.
1029 ///
1030 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
1031 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
1032 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
1033 Self {
1034 builder: self
1035 .builder
1036 .property("tooltip-markup", tooltip_markup.into()),
1037 }
1038 }
1039
1040 /// Sets the text of tooltip to be the given string.
1041 ///
1042 /// Also see [`Tooltip::set_text()`][crate::Tooltip::set_text()].
1043 ///
1044 /// This is a convenience property which will take care of getting the
1045 /// tooltip shown if the given string is not `NULL`:
1046 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
1047 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
1048 /// the default signal handler.
1049 ///
1050 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
1051 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
1052 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
1053 Self {
1054 builder: self.builder.property("tooltip-text", tooltip_text.into()),
1055 }
1056 }
1057
1058 /// How to distribute vertical space if widget gets extra space.
1059 pub fn valign(self, valign: Align) -> Self {
1060 Self {
1061 builder: self.builder.property("valign", valign),
1062 }
1063 }
1064
1065 /// Whether to expand vertically.
1066 pub fn vexpand(self, vexpand: bool) -> Self {
1067 Self {
1068 builder: self.builder.property("vexpand", vexpand),
1069 }
1070 }
1071
1072 /// Whether to use the `vexpand` property.
1073 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
1074 Self {
1075 builder: self.builder.property("vexpand-set", vexpand_set),
1076 }
1077 }
1078
1079 /// Whether the widget is visible.
1080 pub fn visible(self, visible: bool) -> Self {
1081 Self {
1082 builder: self.builder.property("visible", visible),
1083 }
1084 }
1085
1086 /// Overrides for width request of the widget.
1087 ///
1088 /// If this is -1, the natural request will be used.
1089 pub fn width_request(self, width_request: i32) -> Self {
1090 Self {
1091 builder: self.builder.property("width-request", width_request),
1092 }
1093 }
1094
1095 /// The accessible role of the given [`Accessible`][crate::Accessible] implementation.
1096 ///
1097 /// The accessible role cannot be changed once set.
1098 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
1099 Self {
1100 builder: self.builder.property("accessible-role", accessible_role),
1101 }
1102 }
1103
1104 /// The name of the action with which this widget should be associated.
1105 pub fn action_name(self, action_name: impl Into<glib::GString>) -> Self {
1106 Self {
1107 builder: self.builder.property("action-name", action_name.into()),
1108 }
1109 }
1110
1111 /// The target value of the actionable widget's action.
1112 pub fn action_target(self, action_target: &glib::Variant) -> Self {
1113 Self {
1114 builder: self
1115 .builder
1116 .property("action-target", action_target.clone()),
1117 }
1118 }
1119
1120 // rustdoc-stripper-ignore-next
1121 /// Build the [`Switch`].
1122 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
1123 pub fn build(self) -> Switch {
1124 assert_initialized_main_thread!();
1125 self.builder.build()
1126 }
1127}