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