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
//! Toggle module.
//!
//! This public module implements the Liora toggle and toggle-group controls for toolbar-like binary state selection. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.
use gpui::{
App, Component, IntoElement, MouseButton, RenderOnce, SharedString, Window, div, prelude::*, px,
};
use liora_core::Config;
use std::sync::Arc;
type ToggleCallback = dyn Fn(bool, &mut Window, &mut App) + 'static;
type ToggleGroupCallback = dyn Fn(SharedString, &mut Window, &mut App) + 'static;
/// Binary toolbar button that exposes selected/unselected state.
pub struct Toggle {
label: SharedString,
selected: bool,
disabled: bool,
on_change: Option<Arc<ToggleCallback>>,
}
impl Toggle {
/// Creates a toggle from a label and selected state.
pub fn new(label: impl Into<SharedString>, selected: bool) -> Self {
Self {
label: label.into(),
selected,
disabled: false,
on_change: None,
}
}
/// Toggles disabled visual state and suppresses interaction.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Registers a callback that receives the next selected state.
pub fn on_change(mut self, callback: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Arc::new(callback));
self
}
/// Returns whether the toggle is selected.
pub fn selected(&self) -> bool {
self.selected
}
}
impl IntoElement for Toggle {
type Element = Component<Self>;
fn into_element(self) -> Self::Element {
Component::new(self)
}
}
impl RenderOnce for Toggle {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.global::<Config>().theme.clone();
let selected = self.selected;
let disabled = self.disabled;
let callback = self.on_change.clone();
div()
.px_3()
.py_2()
.rounded(px(theme.radius.sm))
.border_1()
.border_color(if selected {
theme.primary.base
} else {
theme.neutral.border
})
.bg(if selected {
theme.primary.light_9
} else {
theme.neutral.card
})
.text_color(if selected {
theme.primary.base
} else {
theme.neutral.text_2
})
.text_sm()
.when(disabled, |s| s.opacity(0.55).cursor_not_allowed())
.when(!disabled, |s| {
s.cursor_pointer()
.hover(|s| s.bg(theme.neutral.hover))
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
if let Some(callback) = &callback {
callback(!selected, window, cx);
}
})
})
.child(self.label)
}
}
/// Option model for [`ToggleGroup`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ToggleOption {
/// Stable option value emitted when the option is selected.
pub value: SharedString,
/// Human-readable option label displayed in the control.
pub label: SharedString,
/// Whether the option is visible but not interactive.
pub disabled: bool,
}
impl ToggleOption {
/// Creates a selectable option.
pub fn new(value: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
value: value.into(),
label: label.into(),
disabled: false,
}
}
/// Toggles disabled visual state.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
/// Single-select group of toolbar toggles.
pub struct ToggleGroup {
options: Vec<ToggleOption>,
selected: Option<SharedString>,
on_change: Option<Arc<ToggleGroupCallback>>,
}
impl ToggleGroup {
/// Creates a toggle group from options.
pub fn new(options: impl IntoIterator<Item = ToggleOption>) -> Self {
Self {
options: options.into_iter().collect(),
selected: None,
on_change: None,
}
}
/// Sets the selected option value.
pub fn selected(mut self, value: impl Into<SharedString>) -> Self {
self.selected = Some(value.into());
self
}
/// Registers a callback that receives the selected value.
pub fn on_change(
mut self,
callback: impl Fn(SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_change = Some(Arc::new(callback));
self
}
/// Returns option count.
pub fn len(&self) -> usize {
self.options.len()
}
/// Returns whether there are no options.
pub fn is_empty(&self) -> bool {
self.options.is_empty()
}
}
impl IntoElement for ToggleGroup {
type Element = Component<Self>;
fn into_element(self) -> Self::Element {
Component::new(self)
}
}
impl RenderOnce for ToggleGroup {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.global::<Config>().theme.clone();
div()
.flex()
.flex_row()
.rounded(px(theme.radius.sm))
.border_1()
.border_color(theme.neutral.border)
.overflow_hidden()
.children(self.options.into_iter().enumerate().map(|(index, option)| {
let selected = self.selected.as_ref() == Some(&option.value);
let disabled = option.disabled;
let value = option.value.clone();
let callback = self.on_change.clone();
div()
.px_3()
.py_2()
.text_sm()
.when(index > 0, |s| {
s.border_l_1().border_color(theme.neutral.border)
})
.bg(if selected {
theme.primary.light_9
} else {
theme.neutral.card
})
.text_color(if selected {
theme.primary.base
} else {
theme.neutral.text_2
})
.when(disabled, |s| s.opacity(0.55).cursor_not_allowed())
.when(!disabled, |s| {
s.cursor_pointer()
.hover(|s| s.bg(theme.neutral.hover))
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
if let Some(callback) = &callback {
callback(value.clone(), window, cx);
}
})
})
.child(option.label)
.into_any_element()
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn toggle_group_tracks_options_and_selection() {
let group = ToggleGroup::new([
ToggleOption::new("left", "Left"),
ToggleOption::new("right", "Right"),
])
.selected("right");
assert_eq!(group.len(), 2);
assert_eq!(group.selected.as_ref().map(|v| v.as_ref()), Some("right"));
assert!(Toggle::new("Bold", true).selected());
}
}