1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use super::behaviors::MouseBehavior;
use crate::{api::prelude::*, prelude::*, proc_macros::*};
// --- KEYS --
static ID_SWITCH_TRACK: &str = "switch_track";
static ID_SWITCH_TOGGLE: &str = "switch_toggle";
// --- KEYS --
/// Used to trigger actions on the `Switch` widget.
pub enum SwitchAction {
/// Toggles the selection state.
ToggleSelection,
/// Adjust visual state after selection changed.
SelectionChanged,
}
/// State to handle the position of switch toggle.
#[derive(Default, AsAny)]
pub struct SwitchState {
switch_toggle: Entity,
}
impl SwitchState {
// toggles the selected property
fn toggle_selection(&self, ctx: &mut Context) {
let selected: bool = *Switch::selected_ref(&ctx.widget());
Switch::selected_set(&mut ctx.widget(), !selected);
toggle_flag("selected", &mut ctx.widget());
ctx.widget().update(false);
}
// update the visual state to the selection state.
fn update_visual(&self, ctx: &mut Context) {
let selected: bool = *Switch::selected_ref(&ctx.widget());
let mut switch_toggle = ctx.get_widget(self.switch_toggle);
if selected {
switch_toggle.set("h_align", Alignment::from("end"));
switch_toggle
.get_mut::<Selector>("selector")
.push_state("selected");
} else {
switch_toggle.set("h_align", Alignment::from("start"));
switch_toggle
.get_mut::<Selector>("selector")
.remove_state("selected");
}
switch_toggle.update(true);
}
}
impl State for SwitchState {
fn init(&mut self, _: &mut Registry, ctx: &mut Context) {
self.switch_toggle = ctx.child(ID_SWITCH_TOGGLE).entity();
self.update_visual(ctx);
}
fn messages(
&mut self,
mut messages: MessageReader,
_registry: &mut Registry,
ctx: &mut Context,
) {
for message in messages.read::<SwitchAction>() {
match message {
SwitchAction::ToggleSelection => self.toggle_selection(ctx),
SwitchAction::SelectionChanged => self.update_visual(ctx),
}
}
}
}
widget!(
/// The `Switch` widget can be switch between `selected` and not `selected`.
///
/// **style:** `switch`
///
/// # Example
///
/// ```rust
/// Switch::new().selected(true).build(ctx)
/// ```
Switch<SwitchState>: MouseHandler {
/// Sets or shares the background property.
background: Brush,
/// Sets or shares the border radius property.
border_radius: f64,
/// Sets or shares the border thickness property.
border_width: Thickness,
/// Sets or shares the border brush property.
border_brush: Brush,
/// Sets or shares the padding property.
padding: Thickness,
/// Sets or shares the pressed property.
pressed: bool,
/// Sets or shares the selected property.
selected: bool,
/// Indicates if the widget is hovered by the mouse cursor.
hover: bool,
/// Defines the margin around the inner border.
container_margin: Thickness
}
);
impl Template for Switch {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
self.name("Switch")
.style("switch")
.pressed(false)
.selected(false)
.width(36.0)
.height(30.0)
.border_radius(8.0)
.border_width(1.0)
.padding(4.0)
.container_margin((2, 8))
.child(
MouseBehavior::new()
.pressed(id)
.enabled(id)
.target(id.0)
.on_click(move |ctx, _| {
ctx.send_message(SwitchAction::ToggleSelection, id);
false
})
.child(
Container::new()
.style(ID_SWITCH_TRACK)
.opacity(id)
.background(id)
.border_brush(id)
.border_width(id)
.border_radius(id)
.margin(("container_margin", id))
.build(ctx),
)
.child(
Container::new()
.id(ID_SWITCH_TOGGLE)
.opacity(id)
.style("switch_toggle")
.v_align("center")
.h_align("start")
.width(20.0)
.height(20.0)
.border_radius(10.0)
.build(ctx),
)
.build(ctx),
)
.on_changed("selected", move |ctx, _| {
ctx.send_message(SwitchAction::SelectionChanged, id);
})
}
}