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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use crate::get_global_color;
use egui::{self, Color32, FontId, Pos2, Rect, Response, Sense, Stroke, StrokeKind, Ui, Vec2, Widget};
/// Material Design switch component following Material Design 3 specifications
///
/// Switches toggle the state of a single item on or off. They are the preferred way to
/// adjust settings on mobile and should be used instead of checkboxes in most cases.
///
/// ## Usage Examples
/// ```rust
/// # egui::__run_test_ui(|ui| {
/// let mut wifi_enabled = false;
/// let mut bluetooth_enabled = true;
///
/// // Basic switch
/// ui.add(MaterialSwitch::new(&mut wifi_enabled));
///
/// // Switch with label text
/// ui.add(MaterialSwitch::new(&mut bluetooth_enabled)
/// .text("Enable Bluetooth"));
///
/// // Disabled switch
/// let mut disabled_option = false;
/// ui.add(MaterialSwitch::new(&mut disabled_option)
/// .text("Unavailable option")
/// .enabled(false));
/// # });
/// ```
///
/// ## Material Design Spec (Material 3)
/// - Track width: 52dp, height: 32dp
/// - Thumb diameter: 24dp when on, 16dp when off, 28dp when pressed
/// - Corner radius: 16dp (fully rounded)
/// - Touch target: 48x48dp minimum
/// - Colors: Primary when on, surfaceContainerHighest when off
/// - Track outline: 2dp when off, transparent when on
/// - Icons: 16dp, displayed on thumb
/// - Animation: 300ms cubic-bezier transition
pub struct MaterialSwitch<'a> {
/// Mutable reference to the switch state (on/off)
selected: &'a mut bool,
/// Optional text label displayed next to the switch
text: Option<String>,
/// Whether the switch is interactive (enabled/disabled)
enabled: bool,
/// Optional icon displayed on thumb when selected
selected_icon: Option<char>,
/// Optional icon displayed on thumb when unselected
unselected_icon: Option<char>,
/// Whether to show track outline (Material 3: true, Material 2: false)
show_track_outline: bool,
}
impl<'a> MaterialSwitch<'a> {
/// Create a new Material Design switch
///
/// ## Parameters
/// - `selected`: Mutable reference to boolean state representing on/off
///
/// ## Returns
/// A new MaterialSwitch instance with default settings
pub fn new(selected: &'a mut bool) -> Self {
Self {
selected,
text: None,
enabled: true,
selected_icon: None,
unselected_icon: None,
show_track_outline: true, // Material 3 default
}
}
/// Set optional text label for the switch
///
/// The text will be displayed to the right of the switch component.
///
/// ## Parameters
/// - `text`: Label text to display next to the switch
pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
/// Set whether the switch is enabled or disabled
///
/// Disabled switches cannot be interacted with and are visually dimmed.
///
/// ## Parameters
/// - `enabled`: True for interactive, false for disabled
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
/// Set an icon to display on the thumb when the switch is selected (on)
///
/// ## Parameters
/// - `icon`: Character representing a Material icon to display on selected thumb
pub fn selected_icon(mut self, icon: char) -> Self {
self.selected_icon = Some(icon);
self
}
/// Set an icon to display on the thumb when the switch is unselected (off)
///
/// ## Parameters
/// - `icon`: Character representing a Material icon to display on unselected thumb
pub fn unselected_icon(mut self, icon: char) -> Self {
self.unselected_icon = Some(icon);
self
}
/// Set icons for both selected and unselected states
///
/// ## Parameters
/// - `selected`: Character representing a Material icon to display when on
/// - `unselected`: Character representing a Material icon to display when off
pub fn with_icons(mut self, selected: char, unselected: char) -> Self {
self.selected_icon = Some(selected);
self.unselected_icon = Some(unselected);
self
}
/// Set whether to show track outline (Material 3 style)
///
/// ## Parameters
/// - `show`: True to show outline when off, false for no outline
pub fn show_track_outline(mut self, show: bool) -> Self {
self.show_track_outline = show;
self
}
}
impl<'a> Widget for MaterialSwitch<'a> {
fn ui(self, ui: &mut Ui) -> Response {
// Material 3 specifications
let switch_width = 52.0;
let switch_height = 32.0;
let track_height = 32.0;
let desired_size = if let Some(ref text) = self.text {
let text_width = ui.painter().layout_no_wrap(
text.clone(),
egui::FontId::default(),
egui::Color32::WHITE,
).size().x;
Vec2::new(switch_width + 8.0 + text_width, switch_height)
} else {
Vec2::new(switch_width, switch_height)
};
let (rect, mut response) = ui.allocate_exact_size(desired_size, Sense::click());
if response.clicked() && self.enabled {
*self.selected = !*self.selected;
response.mark_changed();
}
// Track interaction states
let is_pressed = response.is_pointer_button_down_on();
let is_hovered = response.hovered();
let is_focused = response.has_focus();
// Material Design 3 colors
let primary_color = get_global_color("primary");
let on_primary = get_global_color("onPrimary");
let primary_container = get_global_color("primaryContainer");
let on_primary_container = get_global_color("onPrimaryContainer");
let surface_container_highest = get_global_color("surfaceContainerHighest");
let on_surface = get_global_color("onSurface");
let on_surface_variant = get_global_color("onSurfaceVariant");
let outline = get_global_color("outline");
// Calculate switch area
let switch_rect = Rect::from_min_size(
Pos2::new(rect.min.x, rect.center().y - switch_height / 2.0),
Vec2::new(switch_width, switch_height),
);
let track_rect =
Rect::from_center_size(switch_rect.center(), Vec2::new(switch_width, track_height));
// Material 3: thumb is 16dp when off, 24dp when on, 28dp when pressed
let has_icon = if *self.selected {
self.selected_icon.is_some()
} else {
self.unselected_icon.is_some()
};
let base_thumb_size_on = 24.0;
let base_thumb_size_off = if has_icon { 24.0 } else { 16.0 };
let pressed_thumb_size = 28.0;
let thumb_size = if is_pressed {
pressed_thumb_size
} else if *self.selected {
base_thumb_size_on
} else {
base_thumb_size_off
};
let thumb_travel = switch_width - base_thumb_size_on - 4.0;
let thumb_x = if *self.selected {
switch_rect.min.x + 2.0 + thumb_travel
} else {
switch_rect.min.x + 2.0
};
let thumb_center = Pos2::new(thumb_x + thumb_size / 2.0, switch_rect.center().y);
// Material 3 color resolution based on state
let (track_color, thumb_color, track_outline_color, icon_color) = if !self.enabled {
// Disabled state
let disabled_track = if *self.selected {
on_surface.linear_multiply(0.12)
} else {
surface_container_highest.linear_multiply(0.12)
};
let disabled_thumb = if *self.selected {
on_surface.linear_multiply(1.0)
} else {
on_surface.linear_multiply(0.38)
};
let disabled_outline = on_surface.linear_multiply(0.12);
let disabled_icon = if *self.selected {
on_surface.linear_multiply(0.38)
} else {
surface_container_highest.linear_multiply(0.38)
};
(disabled_track, disabled_thumb, disabled_outline, disabled_icon)
} else if *self.selected {
// Selected (on) state
let track = primary_color;
let thumb = if is_pressed || is_hovered || is_focused {
primary_container
} else {
on_primary
};
let outline = Color32::TRANSPARENT;
let icon = if is_pressed || is_hovered || is_focused {
on_primary_container
} else {
on_primary_container
};
(track, thumb, outline, icon)
} else {
// Unselected (off) state
let track = if is_pressed || is_hovered || is_focused {
surface_container_highest
} else {
surface_container_highest
};
let thumb = if is_pressed || is_hovered || is_focused {
on_surface_variant
} else {
outline
};
let track_outline = outline;
let icon = surface_container_highest;
(track, thumb, track_outline, icon)
};
// Draw track
ui.painter()
.rect_filled(track_rect, track_height / 2.0, track_color);
// Draw track outline (Material 3)
if self.show_track_outline && track_outline_color != Color32::TRANSPARENT {
ui.painter().rect_stroke(
track_rect,
track_height / 2.0,
Stroke::new(2.0, track_outline_color),
StrokeKind::Outside,
);
}
// Draw state layer (ripple/overlay effect) - Material 3
if self.enabled {
let overlay_radius = 20.0; // 40dp diameter / 2
let overlay_color = if *self.selected {
if is_pressed {
primary_color.linear_multiply(0.1)
} else if is_hovered {
primary_color.linear_multiply(0.08)
} else if is_focused {
primary_color.linear_multiply(0.1)
} else {
Color32::TRANSPARENT
}
} else {
if is_pressed {
on_surface.linear_multiply(0.1)
} else if is_hovered {
on_surface.linear_multiply(0.08)
} else if is_focused {
on_surface.linear_multiply(0.1)
} else {
Color32::TRANSPARENT
}
};
if overlay_color != Color32::TRANSPARENT {
ui.painter()
.circle_filled(thumb_center, overlay_radius, overlay_color);
}
}
// Draw thumb
ui.painter()
.circle_filled(thumb_center, thumb_size / 2.0, thumb_color);
// Draw icon on thumb if present
let current_icon = if *self.selected {
self.selected_icon
} else {
self.unselected_icon
};
if let Some(icon) = current_icon {
let icon_size = 16.0;
let icon_font = FontId::proportional(icon_size);
ui.painter().text(
thumb_center,
egui::Align2::CENTER_CENTER,
icon.to_string(),
icon_font,
icon_color,
);
}
// Draw label text
if let Some(ref text) = self.text {
let text_pos = Pos2::new(switch_rect.max.x + 8.0, rect.center().y);
let text_color = if self.enabled {
on_surface
} else {
on_surface.linear_multiply(0.38)
};
ui.painter().text(
text_pos,
egui::Align2::LEFT_CENTER,
text,
egui::FontId::default(),
text_color,
);
}
response
}
}
pub fn switch(selected: &mut bool) -> MaterialSwitch<'_> {
MaterialSwitch::new(selected)
}