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 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 /// 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 let f: &F = &*(f as *const F);
643 f(&from_glib_borrow(this))
644 }
645 unsafe {
646 let f: Box_<F> = Box_::new(f);
647 connect_raw(
648 self.as_ptr() as *mut _,
649 c"activate".as_ptr() as *const _,
650 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
651 activate_trampoline::<F> as *const (),
652 )),
653 Box_::into_raw(f),
654 )
655 }
656 }
657
658 pub fn emit_activate(&self) {
659 self.emit_by_name::<()>("activate", &[]);
660 }
661
662 /// Emitted to change the underlying state.
663 ///
664 /// The ::state-set signal is emitted when the user changes the switch
665 /// position. The default handler calls [`set_state()`][Self::set_state()] with the
666 /// value of @state.
667 ///
668 /// To implement delayed state change, applications can connect to this
669 /// signal, initiate the change of the underlying state, and call
670 /// [`set_state()`][Self::set_state()] when the underlying state change is
671 /// complete. The signal handler should return [`true`] to prevent the
672 /// default handler from running.
673 /// ## `state`
674 /// the new state of the switch
675 ///
676 /// # Returns
677 ///
678 /// [`true`] to stop the signal emission
679 #[doc(alias = "state-set")]
680 pub fn connect_state_set<F: Fn(&Self, bool) -> glib::Propagation + 'static>(
681 &self,
682 f: F,
683 ) -> SignalHandlerId {
684 unsafe extern "C" fn state_set_trampoline<
685 F: Fn(&Switch, bool) -> glib::Propagation + 'static,
686 >(
687 this: *mut ffi::GtkSwitch,
688 state: glib::ffi::gboolean,
689 f: glib::ffi::gpointer,
690 ) -> glib::ffi::gboolean {
691 let f: &F = &*(f as *const F);
692 f(&from_glib_borrow(this), from_glib(state)).into_glib()
693 }
694 unsafe {
695 let f: Box_<F> = Box_::new(f);
696 connect_raw(
697 self.as_ptr() as *mut _,
698 c"state-set".as_ptr() as *const _,
699 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
700 state_set_trampoline::<F> as *const (),
701 )),
702 Box_::into_raw(f),
703 )
704 }
705 }
706
707 #[doc(alias = "active")]
708 pub fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
709 unsafe extern "C" fn notify_active_trampoline<F: Fn(&Switch) + 'static>(
710 this: *mut ffi::GtkSwitch,
711 _param_spec: glib::ffi::gpointer,
712 f: glib::ffi::gpointer,
713 ) {
714 let f: &F = &*(f as *const F);
715 f(&from_glib_borrow(this))
716 }
717 unsafe {
718 let f: Box_<F> = Box_::new(f);
719 connect_raw(
720 self.as_ptr() as *mut _,
721 c"notify::active".as_ptr() as *const _,
722 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
723 notify_active_trampoline::<F> as *const (),
724 )),
725 Box_::into_raw(f),
726 )
727 }
728 }
729
730 #[doc(alias = "state")]
731 pub fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
732 unsafe extern "C" fn notify_state_trampoline<F: Fn(&Switch) + 'static>(
733 this: *mut ffi::GtkSwitch,
734 _param_spec: glib::ffi::gpointer,
735 f: glib::ffi::gpointer,
736 ) {
737 let f: &F = &*(f as *const F);
738 f(&from_glib_borrow(this))
739 }
740 unsafe {
741 let f: Box_<F> = Box_::new(f);
742 connect_raw(
743 self.as_ptr() as *mut _,
744 c"notify::state".as_ptr() as *const _,
745 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
746 notify_state_trampoline::<F> as *const (),
747 )),
748 Box_::into_raw(f),
749 )
750 }
751 }
752}
753
754impl Default for Switch {
755 fn default() -> Self {
756 Self::new()
757 }
758}
759
760// rustdoc-stripper-ignore-next
761/// A [builder-pattern] type to construct [`Switch`] objects.
762///
763/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
764#[must_use = "The builder must be built to be used"]
765pub struct SwitchBuilder {
766 builder: glib::object::ObjectBuilder<'static, Switch>,
767}
768
769impl SwitchBuilder {
770 fn new() -> Self {
771 Self {
772 builder: glib::object::Object::builder(),
773 }
774 }
775
776 /// Whether the [`Switch`][crate::Switch] widget is in its on or off state.
777 pub fn active(self, active: bool) -> Self {
778 Self {
779 builder: self.builder.property("active", active),
780 }
781 }
782
783 /// The backend state that is controlled by the switch.
784 ///
785 /// Applications should usually set the [`active`][struct@crate::Switch#active] property,
786 /// except when indicating a change to the backend state which occurs
787 /// separately from the user's interaction.
788 ///
789 /// See [`state-set`][struct@crate::Switch#state-set] for details.
790 pub fn state(self, state: bool) -> Self {
791 Self {
792 builder: self.builder.property("state", state),
793 }
794 }
795
796 /// Whether the widget or any of its descendents can accept
797 /// the input focus.
798 ///
799 /// This property is meant to be set by widget implementations,
800 /// typically in their instance init function.
801 pub fn can_focus(self, can_focus: bool) -> Self {
802 Self {
803 builder: self.builder.property("can-focus", can_focus),
804 }
805 }
806
807 /// Whether the widget can receive pointer events.
808 pub fn can_target(self, can_target: bool) -> Self {
809 Self {
810 builder: self.builder.property("can-target", can_target),
811 }
812 }
813
814 /// A list of css classes applied to this widget.
815 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
816 Self {
817 builder: self.builder.property("css-classes", css_classes.into()),
818 }
819 }
820
821 /// The name of this widget in the CSS tree.
822 ///
823 /// This property is meant to be set by widget implementations,
824 /// typically in their instance init function.
825 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
826 Self {
827 builder: self.builder.property("css-name", css_name.into()),
828 }
829 }
830
831 /// The cursor used by @widget.
832 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
833 Self {
834 builder: self.builder.property("cursor", cursor.clone()),
835 }
836 }
837
838 /// Whether the widget should grab focus when it is clicked with the mouse.
839 ///
840 /// This property is only relevant for widgets that can take focus.
841 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
842 Self {
843 builder: self.builder.property("focus-on-click", focus_on_click),
844 }
845 }
846
847 /// Whether this widget itself will accept the input focus.
848 pub fn focusable(self, focusable: bool) -> Self {
849 Self {
850 builder: self.builder.property("focusable", focusable),
851 }
852 }
853
854 /// How to distribute horizontal space if widget gets extra space.
855 pub fn halign(self, halign: Align) -> Self {
856 Self {
857 builder: self.builder.property("halign", halign),
858 }
859 }
860
861 /// Enables or disables the emission of the [`query-tooltip`][struct@crate::Widget#query-tooltip]
862 /// signal on @widget.
863 ///
864 /// A true value indicates that @widget can have a tooltip, in this case
865 /// the widget will be queried using [`query-tooltip`][struct@crate::Widget#query-tooltip] to
866 /// determine whether it will provide a tooltip or not.
867 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
868 Self {
869 builder: self.builder.property("has-tooltip", has_tooltip),
870 }
871 }
872
873 /// Overrides for height request of the widget.
874 ///
875 /// If this is -1, the natural request will be used.
876 pub fn height_request(self, height_request: i32) -> Self {
877 Self {
878 builder: self.builder.property("height-request", height_request),
879 }
880 }
881
882 /// Whether to expand horizontally.
883 pub fn hexpand(self, hexpand: bool) -> Self {
884 Self {
885 builder: self.builder.property("hexpand", hexpand),
886 }
887 }
888
889 /// Whether to use the `hexpand` property.
890 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
891 Self {
892 builder: self.builder.property("hexpand-set", hexpand_set),
893 }
894 }
895
896 /// The [`LayoutManager`][crate::LayoutManager] instance to use to compute
897 /// the preferred size of the widget, and allocate its children.
898 ///
899 /// This property is meant to be set by widget implementations,
900 /// typically in their instance init function.
901 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
902 Self {
903 builder: self
904 .builder
905 .property("layout-manager", layout_manager.clone().upcast()),
906 }
907 }
908
909 /// Makes this widget act like a modal dialog, with respect to
910 /// event delivery.
911 ///
912 /// Global event controllers will not handle events with targets
913 /// inside the widget, unless they are set up to ignore propagation
914 /// limits. See [`EventControllerExt::set_propagation_limit()`][crate::prelude::EventControllerExt::set_propagation_limit()].
915 #[cfg(feature = "v4_18")]
916 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
917 pub fn limit_events(self, limit_events: bool) -> Self {
918 Self {
919 builder: self.builder.property("limit-events", limit_events),
920 }
921 }
922
923 /// Margin on bottom side of widget.
924 ///
925 /// This property adds margin outside of the widget's normal size
926 /// request, the margin will be added in addition to the size from
927 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
928 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
929 Self {
930 builder: self.builder.property("margin-bottom", margin_bottom),
931 }
932 }
933
934 /// Margin on end of widget, horizontally.
935 ///
936 /// This property supports left-to-right and right-to-left text
937 /// directions.
938 ///
939 /// This property adds margin outside of the widget's normal size
940 /// request, the margin will be added in addition to the size from
941 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
942 pub fn margin_end(self, margin_end: i32) -> Self {
943 Self {
944 builder: self.builder.property("margin-end", margin_end),
945 }
946 }
947
948 /// Margin on start of widget, horizontally.
949 ///
950 /// This property supports left-to-right and right-to-left text
951 /// directions.
952 ///
953 /// This property adds margin outside of the widget's normal size
954 /// request, the margin will be added in addition to the size from
955 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
956 pub fn margin_start(self, margin_start: i32) -> Self {
957 Self {
958 builder: self.builder.property("margin-start", margin_start),
959 }
960 }
961
962 /// Margin on top side of widget.
963 ///
964 /// This property adds margin outside of the widget's normal size
965 /// request, the margin will be added in addition to the size from
966 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
967 pub fn margin_top(self, margin_top: i32) -> Self {
968 Self {
969 builder: self.builder.property("margin-top", margin_top),
970 }
971 }
972
973 /// The name of the widget.
974 pub fn name(self, name: impl Into<glib::GString>) -> Self {
975 Self {
976 builder: self.builder.property("name", name.into()),
977 }
978 }
979
980 /// The requested opacity of the widget.
981 pub fn opacity(self, opacity: f64) -> Self {
982 Self {
983 builder: self.builder.property("opacity", opacity),
984 }
985 }
986
987 /// How content outside the widget's content area is treated.
988 ///
989 /// This property is meant to be set by widget implementations,
990 /// typically in their instance init function.
991 pub fn overflow(self, overflow: Overflow) -> Self {
992 Self {
993 builder: self.builder.property("overflow", overflow),
994 }
995 }
996
997 /// Whether the widget will receive the default action when it is focused.
998 pub fn receives_default(self, receives_default: bool) -> Self {
999 Self {
1000 builder: self.builder.property("receives-default", receives_default),
1001 }
1002 }
1003
1004 /// Whether the widget responds to input.
1005 pub fn sensitive(self, sensitive: bool) -> Self {
1006 Self {
1007 builder: self.builder.property("sensitive", sensitive),
1008 }
1009 }
1010
1011 /// Sets the text of tooltip to be the given string, which is marked up
1012 /// with Pango markup.
1013 ///
1014 /// Also see [`Tooltip::set_markup()`][crate::Tooltip::set_markup()].
1015 ///
1016 /// This is a convenience property which will take care of getting the
1017 /// tooltip shown if the given string is not `NULL`:
1018 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
1019 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
1020 /// the default signal handler.
1021 ///
1022 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
1023 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
1024 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
1025 Self {
1026 builder: self
1027 .builder
1028 .property("tooltip-markup", tooltip_markup.into()),
1029 }
1030 }
1031
1032 /// Sets the text of tooltip to be the given string.
1033 ///
1034 /// Also see [`Tooltip::set_text()`][crate::Tooltip::set_text()].
1035 ///
1036 /// This is a convenience property which will take care of getting the
1037 /// tooltip shown if the given string is not `NULL`:
1038 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
1039 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
1040 /// the default signal handler.
1041 ///
1042 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
1043 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
1044 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
1045 Self {
1046 builder: self.builder.property("tooltip-text", tooltip_text.into()),
1047 }
1048 }
1049
1050 /// How to distribute vertical space if widget gets extra space.
1051 pub fn valign(self, valign: Align) -> Self {
1052 Self {
1053 builder: self.builder.property("valign", valign),
1054 }
1055 }
1056
1057 /// Whether to expand vertically.
1058 pub fn vexpand(self, vexpand: bool) -> Self {
1059 Self {
1060 builder: self.builder.property("vexpand", vexpand),
1061 }
1062 }
1063
1064 /// Whether to use the `vexpand` property.
1065 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
1066 Self {
1067 builder: self.builder.property("vexpand-set", vexpand_set),
1068 }
1069 }
1070
1071 /// Whether the widget is visible.
1072 pub fn visible(self, visible: bool) -> Self {
1073 Self {
1074 builder: self.builder.property("visible", visible),
1075 }
1076 }
1077
1078 /// Overrides for width request of the widget.
1079 ///
1080 /// If this is -1, the natural request will be used.
1081 pub fn width_request(self, width_request: i32) -> Self {
1082 Self {
1083 builder: self.builder.property("width-request", width_request),
1084 }
1085 }
1086
1087 /// The accessible role of the given [`Accessible`][crate::Accessible] implementation.
1088 ///
1089 /// The accessible role cannot be changed once set.
1090 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
1091 Self {
1092 builder: self.builder.property("accessible-role", accessible_role),
1093 }
1094 }
1095
1096 /// The name of the action with which this widget should be associated.
1097 pub fn action_name(self, action_name: impl Into<glib::GString>) -> Self {
1098 Self {
1099 builder: self.builder.property("action-name", action_name.into()),
1100 }
1101 }
1102
1103 /// The target value of the actionable widget's action.
1104 pub fn action_target(self, action_target: &glib::Variant) -> Self {
1105 Self {
1106 builder: self
1107 .builder
1108 .property("action-target", action_target.clone()),
1109 }
1110 }
1111
1112 // rustdoc-stripper-ignore-next
1113 /// Build the [`Switch`].
1114 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
1115 pub fn build(self) -> Switch {
1116 assert_initialized_main_thread!();
1117 self.builder.build()
1118 }
1119}