Skip to main content

mitsuami_widgets/
toggles.rs

1//! Checkboxes and switches.
2
3use mitsuami_core::{Element, ElementBuilder, EventValue, NodeId, Prop, Tweak, Ui, UiEvent, View, WidgetKind};
4use mitsuami_reactive::{IntoValue, Signal};
5
6pub struct Checkbox(Element);
7widget!(Checkbox);
8toggle!(Checkbox);
9
10impl Checkbox {
11    pub fn new(label: impl IntoValue<String>) -> Checkbox {
12        let mut element = Element::new(WidgetKind::Checkbox);
13        element.prop(label.into_value(), Prop::Label);
14        Checkbox(element)
15    }
16
17    /// Shows the mixed state, whatever `checked` says: some of what the box
18    /// stands for is checked, as in a "Select all" box. A click leaves it,
19    /// checked or not as the platform decides (AppKit checks it, GTK flips
20    /// `checked`), and reports that with `on_change`: work out `mixed`
21    /// again from there.
22    pub fn mixed(mut self, mixed: impl IntoValue<bool>) -> Checkbox {
23        self.0.prop(mixed.into_value(), Prop::Mixed);
24        self
25    }
26
27    /// Raw platform settings, past the semantic ones: see [`Tweak`].
28    pub fn native(mut self, tweak: Tweak<Checkbox>) -> Checkbox {
29        tweak.apply(&mut self.0);
30        self
31    }
32}
33
34/// On/off switch. Most platforms draw no caption; the label is its
35/// accessible name.
36pub struct Switch(Element);
37widget!(Switch);
38toggle!(Switch);
39
40impl Switch {
41    pub fn new(label: impl IntoValue<String>) -> Switch {
42        let mut element = Element::new(WidgetKind::Switch);
43        element.prop(label.into_value(), Prop::Label);
44        Switch(element)
45    }
46
47    /// Raw platform settings: see [`Tweak`]. Switches have no semantic
48    /// options past `checked`: what the platforms offer (sizes, GTK's
49    /// delayed state, WinUI's on and off text) is each one's own.
50    pub fn native(mut self, tweak: Tweak<Switch>) -> Switch {
51        tweak.apply(&mut self.0);
52        self
53    }
54}
55
56text_tag!(Checkbox, Label);
57text_tag!(Switch, Label);