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
//! Switch component for gpuikit
//!
//! A sliding switch control for toggling boolean values, similar to iOS-style switches.
use crate::layout::h_stack;
use crate::theme::{ActiveTheme, ControlSize, Themeable};
use crate::traits::control_sized::ControlSized;
use crate::traits::disableable::Disableable;
use crate::traits::labelable::Labelable;
use crate::traits::selectable::Selectable;
use crate::utils::element_manager::ElementManagerExt;
use gpui::{
App, Context, ElementId, EventEmitter, InteractiveElement, IntoElement, MouseButton,
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window, div,
prelude::*,
};
/// Event emitted when the switch state changes
pub struct SwitchChanged {
pub on: bool,
}
/// A sliding switch component for toggling between on/off states
///
/// Similar to a toggle, but with a more pronounced sliding switch appearance.
///
/// # Example
///
/// ```
/// # use gpui::{Context, IntoElement, Render, Window, prelude::*};
/// use gpuikit::elements::switch::switch;
/// use gpuikit::traits::labelable::Labelable;
/// # struct D;
/// # impl Render for D { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
/// cx.new(|_cx| switch("dark-mode", true).label("Dark Mode"))
/// # }}
/// # let mut tcx = gpui::TestAppContext::single();
/// # tcx.update(gpuikit::init);
/// # let _ = tcx.add_window_view(|_, _| D);
/// ```
pub struct Switch {
id: ElementId,
label: Option<SharedString>,
on: bool,
disabled: bool,
size: ControlSize,
}
impl EventEmitter<SwitchChanged> for Switch {}
impl Switch {
pub fn new(id: impl Into<ElementId>, on: bool) -> Self {
Self {
id: id.into(),
label: None,
on,
disabled: false,
size: ControlSize::default(),
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn is_on(&self) -> bool {
self.on
}
pub fn set_on(&mut self, on: bool, cx: &mut Context<Self>) {
if self.on != on {
self.on = on;
cx.emit(SwitchChanged { on: self.on });
cx.notify();
}
}
pub fn toggle(&mut self, cx: &mut Context<Self>) {
self.set_on(!self.on, cx);
}
fn on_click(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
if !self.disabled {
self.toggle(cx);
}
}
}
impl Render for Switch {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
let metrics = theme.control(self.size);
let on = self.on;
let disabled = self.disabled;
let label = self.label.clone();
// Shared with `Toggle`. The two used to name their own track shapes and
// had drifted to different ones; what still tells them apart is the
// surface-coloured thumb and the heavier shadow below.
let track = metrics.track();
let track_bg = if disabled {
theme.surface_tertiary()
} else if on {
theme.accent()
} else {
theme.surface_secondary()
};
let thumb_bg = if disabled {
theme.fg_disabled()
} else {
theme.surface()
};
let track_border = if disabled {
theme.border_subtle()
} else if on {
theme.accent()
} else {
theme.border()
};
h_stack()
.id(self.id.clone())
.h(metrics.height)
// A label outside the control's own box wants more room than the
// gap between an icon and a label inside one.
.gap(metrics.gap * 2.0)
.items_center()
.when(!disabled, |this| {
this.cursor_pointer()
.on_mouse_down(MouseButton::Left, |_, window, _| window.prevent_default())
.on_click(cx.listener(|this, _, window, cx| {
this.on_click(window, cx);
}))
})
.when(disabled, |this| this.cursor_not_allowed().opacity(0.65))
.child(
div()
.relative()
.w(track.width)
.h(track.height)
.bg(track_bg)
.border_1()
.border_color(track_border)
.rounded(track.height / 2.)
.when(!disabled, |this| {
this.hover(|style| {
style.border_color(if on {
theme.accent()
} else {
theme.border_secondary()
})
})
})
.child(
div()
.absolute()
.top(track.thumb_margin)
.when(on, |this| this.right(track.thumb_margin))
.when(!on, |this| this.left(track.thumb_margin))
.size(track.thumb)
.bg(thumb_bg)
.rounded_full()
.shadow_md(),
),
)
.when_some(label, |this, label| {
this.child(
div()
.text_size(metrics.text_size)
.line_height(metrics.line_height)
.text_color(if disabled {
theme.fg_disabled()
} else {
theme.fg()
})
.child(label),
)
})
}
}
/// Convenience function to create a switch
pub fn switch(id: impl Into<ElementId>, on: bool) -> Switch {
Switch::new(id, on)
}
/// Convenience function to create a switch with auto-generated ID
pub fn switch_auto(cx: &App, on: bool) -> Switch {
Switch::new(cx.next_id_named("switch"), on)
}
impl Disableable for Switch {
fn is_disabled(&self) -> bool {
self.disabled
}
fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl Selectable for Switch {
fn is_selected(&self) -> bool {
self.on
}
fn selected(mut self, selected: bool) -> Self {
self.on = selected;
self
}
}
impl ControlSized for Switch {
fn control_size(mut self, size: ControlSize) -> Self {
self.size = size;
self
}
}
impl Labelable for Switch {
fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
}