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
//! Toggle switch widget
//!
//! A toggle switch (on/off) control similar to iOS-style switches.
//! Supports keyboard interaction (Space/Enter to toggle) and mouse clicks.
//!
//! # Example
//!
//! ```ignore
//! use ccf_gpui_widgets::widgets::ToggleSwitch;
//!
//! let toggle = cx.new(|cx| {
//! ToggleSwitch::new(cx)
//! .with_on(true)
//! .label("Enable notifications")
//! });
//!
//! // Subscribe to changes
//! cx.subscribe(&toggle, |this, _toggle, event: &ToggleSwitchEvent, cx| {
//! if let ToggleSwitchEvent::Toggle(is_on) = event {
//! println!("Toggle is now: {}", is_on);
//! }
//! }).detach();
//! ```
use gpui::prelude::*;
use gpui::*;
use crate::theme::{get_theme_or, Theme};
use super::focus_navigation::{handle_tab_navigation, with_focus_actions, EnabledCursorExt};
/// Position of the label relative to the toggle switch
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LabelPosition {
/// Label appears to the left of the toggle
Left,
/// Label appears to the right of the toggle (default)
#[default]
Right,
}
/// Events emitted by ToggleSwitch
#[derive(Clone, Debug)]
pub enum ToggleSwitchEvent {
/// Toggle state changed.
/// The boolean indicates the new on/off state: `true` = on, `false` = off.
Toggle(bool),
}
/// Toggle switch widget
pub struct ToggleSwitch {
/// Whether the toggle is in the "on" state
on: bool,
label: Option<SharedString>,
label_position: LabelPosition,
focus_handle: FocusHandle,
custom_theme: Option<Theme>,
/// Whether the widget is enabled (interactive)
enabled: bool,
}
impl EventEmitter<ToggleSwitchEvent> for ToggleSwitch {}
impl Focusable for ToggleSwitch {
fn focus_handle(&self, _cx: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl ToggleSwitch {
/// Create a new toggle switch
pub fn new(cx: &mut Context<Self>) -> Self {
Self {
on: false,
label: None,
label_position: LabelPosition::default(),
focus_handle: cx.focus_handle().tab_stop(true),
custom_theme: None,
enabled: true,
}
}
/// Set initial on/off state (builder pattern)
#[must_use]
pub fn with_on(mut self, value: bool) -> Self {
self.on = value;
self
}
/// Set label text (builder pattern)
#[must_use]
pub fn label(mut self, text: impl Into<SharedString>) -> Self {
self.label = Some(text.into());
self
}
/// Set label position (builder pattern)
#[must_use]
pub fn label_position(mut self, position: LabelPosition) -> Self {
self.label_position = position;
self
}
/// Set custom theme (builder pattern)
#[must_use]
pub fn theme(mut self, theme: Theme) -> Self {
self.custom_theme = Some(theme);
self
}
/// Set enabled state (builder pattern)
#[must_use]
pub fn with_enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
/// Get current on/off state
pub fn is_on(&self) -> bool {
self.on
}
/// Set on/off state programmatically
pub fn set_on(&mut self, on: bool, cx: &mut Context<Self>) {
if self.on != on {
self.on = on;
cx.emit(ToggleSwitchEvent::Toggle(on));
cx.notify();
}
}
/// Get the focus handle
pub fn focus_handle(&self) -> &FocusHandle {
&self.focus_handle
}
/// Check if the toggle switch is enabled
pub fn is_enabled(&self) -> bool {
self.enabled
}
/// Set enabled state programmatically
pub fn set_enabled(&mut self, enabled: bool, cx: &mut Context<Self>) {
if self.enabled != enabled {
self.enabled = enabled;
cx.notify();
}
}
fn toggle(&mut self, cx: &mut Context<Self>) {
self.on = !self.on;
cx.emit(ToggleSwitchEvent::Toggle(self.on));
cx.notify();
}
}
impl Render for ToggleSwitch {
fn render(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement {
let theme = get_theme_or(cx, self.custom_theme.as_ref());
let is_on = self.on;
let label = self.label.clone();
let label_position = self.label_position;
let focus_handle = self.focus_handle.clone();
let is_focused = self.focus_handle.is_focused(window);
let enabled = self.enabled;
// Toggle dimensions
let track_width = 44.0;
let track_height = 24.0;
let thumb_size = 18.0;
let thumb_padding = 3.0;
// Calculate thumb position (left edge when off, right edge when on)
let thumb_left = if is_on {
track_width - thumb_size - thumb_padding
} else {
thumb_padding
};
// Helper to create label element
let make_label = |text: SharedString| {
div()
.text_sm()
.font_weight(FontWeight::SEMIBOLD)
.when(enabled, |d| d.text_color(rgb(theme.text_label)))
.when(!enabled, |d| d.text_color(rgb(theme.disabled_text)))
.child(text)
};
// Helper to create toggle element
let make_toggle = || {
let (track_bg, thumb_bg) = if enabled {
let track = if is_on { theme.primary } else { theme.bg_input };
(track, theme.bg_white)
} else {
let track = if is_on { theme.disabled_text } else { theme.disabled_bg };
(track, theme.disabled_bg)
};
div()
.w(px(track_width))
.h(px(track_height))
.rounded(px(track_height / 2.0)) // Pill shape
.relative()
.bg(rgb(track_bg))
.cursor_for_enabled(enabled)
.child(
// Thumb
div()
.absolute()
.top(px(thumb_padding))
.left(px(thumb_left))
.w(px(thumb_size))
.h(px(thumb_size))
.rounded_full()
.bg(rgb(thumb_bg))
.when(enabled, |d| d.shadow_sm())
)
};
let mut container = with_focus_actions(
div()
.id("ccf_toggle_switch")
.track_focus(&focus_handle)
.tab_stop(enabled),
cx,
)
.on_key_down(cx.listener(move |toggle, event: &KeyDownEvent, window, cx| {
if !toggle.enabled {
return;
}
if handle_tab_navigation(event, window) {
return;
}
if matches!(event.keystroke.key.as_str(), "space" | "enter") {
toggle.toggle(cx);
}
}))
.flex()
.flex_row()
.gap_2()
.items_center()
.py_1()
.px_1()
.rounded_sm()
.cursor_for_enabled(enabled)
.border_2()
.border_color(if is_focused && enabled { rgb(theme.border_focus) } else { rgba(0x00000000) });
if enabled {
container = container.on_mouse_down(MouseButton::Left, cx.listener(|toggle, _event, window, cx| {
toggle.focus_handle.focus(window);
toggle.toggle(cx);
}));
}
// Arrange label and toggle based on position
match (label_position, label) {
(LabelPosition::Left, Some(text)) => {
container = container.child(make_label(text)).child(make_toggle());
}
(LabelPosition::Right, Some(text)) => {
container = container.child(make_toggle()).child(make_label(text));
}
(_, None) => {
container = container.child(make_toggle());
}
}
container
}
}