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
//! Checkbox module.
//!
//! This public module implements the Liora single checkbox control with keyboard and pointer interaction. 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 crate::gpui_compat::element_id;
use crate::motion::pop_in;
use gpui::{
App, Context, EventEmitter, FocusHandle, Focusable, Hsla, KeyBinding, MouseButton, Render,
Rgba, SharedString, Window, prelude::*, px,
};
use liora_icons::Icon;
use liora_icons_lucide::IconName;
fn rgba(r: u8, g: u8, b: u8, a: f32) -> Hsla {
Rgba {
r: r as f32 / 255.0,
g: g as f32 / 255.0,
b: b as f32 / 255.0,
a,
}
.into()
}
gpui::actions!(
checkbox,
[
#[doc = "Keyboard action that toggles the focused checkbox."]
CheckboxToggle
]
);
/// Fluent native GPUI component for rendering Liora checkbox.
pub struct Checkbox {
checked: bool,
disabled: bool,
label: Option<SharedString>,
focus_handle: FocusHandle,
on_change: Option<Box<dyn Fn(bool, &mut Window, &mut App) + 'static>>,
}
#[derive(Clone, Copy)]
/// Fluent native GPUI component for rendering Liora checkbox changed.
pub struct CheckboxChanged(pub bool);
impl EventEmitter<CheckboxChanged> for Checkbox {}
impl Checkbox {
/// Creates `Checkbox` initialized from the supplied checked.
pub fn new(checked: bool, cx: &mut Context<Self>) -> Self {
Self {
checked,
disabled: false,
label: None,
focus_handle: cx.focus_handle(),
on_change: None,
}
}
/// Toggles the disabled state and suppresses user interaction when enabled.
pub fn disabled(mut self, d: bool) -> Self {
self.disabled = d;
self
}
/// Returns the stable user-facing label for this value.
pub fn label(mut self, text: impl Into<SharedString>) -> Self {
self.label = Some(text.into());
self
}
/// Registers a callback that runs when change occurs.
pub fn on_change(mut self, cb: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Box::new(cb));
self
}
/// Updates the stored disabled value and keeps the existing component identity.
pub fn set_disabled(&mut self, d: bool, cx: &mut Context<Self>) {
self.disabled = d;
cx.notify();
}
/// Registers GPUI key bindings required for keyboard interaction.
pub fn register_key_bindings(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("space", CheckboxToggle, None),
KeyBinding::new("enter", CheckboxToggle, None),
]);
}
fn toggle(&mut self, _: &CheckboxToggle, window: &mut Window, cx: &mut Context<Self>) {
if !self.disabled {
self.checked = !self.checked;
cx.emit(CheckboxChanged(self.checked));
cx.notify();
if let Some(ref cb) = self.on_change {
cb(self.checked, window, cx);
}
}
}
}
impl Focusable for Checkbox {
fn focus_handle(&self, _cx: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl Render for Checkbox {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = &cx.global::<liora_core::Config>().theme;
let focused = self.focus_handle.is_focused(_window);
let sz = 16.0;
let (bg, border, check_color) = if self.disabled {
(
theme.neutral.hover,
theme.neutral.border,
theme.neutral.text_disabled,
)
} else if self.checked {
(
theme.primary.base,
theme.primary.base,
rgba(255, 255, 255, 1.0),
)
} else {
(
rgba(0, 0, 0, 0.0),
if focused {
theme.primary.base
} else {
theme.neutral.border
},
rgba(0, 0, 0, 0.0),
)
};
let mut row = gpui::div()
.flex()
.flex_row()
.items_center()
.gap_2()
.on_action(cx.listener(Self::toggle));
if !self.disabled {
row = row.cursor_pointer().track_focus(&self.focus_handle);
row = row.on_mouse_down(
MouseButton::Left,
cx.listener(|this, _, window, _cx| {
window.focus(&this.focus_handle, _cx);
}),
);
row = row.on_mouse_up(
MouseButton::Left,
cx.listener(|this, _, window, cx| {
this.toggle(&CheckboxToggle, window, cx);
}),
);
} else {
row = row.cursor_not_allowed();
}
let mut box_el = gpui::div()
.flex_none()
.w(px(sz))
.h(px(sz))
.rounded(px(2.0))
.bg(bg)
.border_1()
.border_color(border)
.flex()
.items_center()
.justify_center();
if self.checked {
box_el = box_el.child(pop_in(
element_id(format!(
"liora-checkbox-check-motion-{}",
cx.entity().entity_id()
)),
gpui::div()
.flex()
.items_center()
.justify_center()
.child(Icon::new(IconName::Check).size(px(12.0)).color(check_color)),
));
}
row = row.child(box_el);
if let Some(ref label) = self.label {
row = row.child(
gpui::div()
.text_size(px(theme.font_size.md))
.text_color(theme.neutral.text_1)
.child(label.clone()),
);
}
row
}
}