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
//! Checkbox group widget
//!
//! A group of checkboxes for multi-selection from multiple choices.
//! Supports keyboard navigation (Up/Down arrows, Space to toggle).
//!
//! # Example
//!
//! ```ignore
//! use ccf_gpui_widgets::widgets::CheckboxGroup;
//!
//! let group = cx.new(|cx| {
//! CheckboxGroup::new(cx)
//! .choices(vec!["Red".to_string(), "Green".to_string(), "Blue".to_string()])
//! .selected(vec!["Red".to_string(), "Blue".to_string()])
//! });
//!
//! // Subscribe to changes
//! cx.subscribe(&group, |this, _group, event: &CheckboxGroupEvent, cx| {
//! if let CheckboxGroupEvent::Change(selected) = event {
//! println!("Selected: {:?}", selected);
//! }
//! }).detach();
//! ```
use std::collections::HashSet;
use gpui::prelude::*;
use gpui::*;
use crate::theme::{get_theme_or, Theme};
use super::focus_navigation::{handle_tab_navigation, with_focus_actions, EnabledCursorExt};
/// Events emitted by CheckboxGroup
#[derive(Clone, Debug)]
pub enum CheckboxGroupEvent {
/// Selection changed (contains all currently selected values)
Change(Vec<String>),
}
/// Checkbox group widget for multi-selection
pub struct CheckboxGroup {
choices: Vec<String>,
selected: HashSet<String>,
focus_handle: FocusHandle,
highlight_index: usize,
custom_theme: Option<Theme>,
/// Whether the widget is enabled (interactive)
enabled: bool,
}
impl EventEmitter<CheckboxGroupEvent> for CheckboxGroup {}
impl Focusable for CheckboxGroup {
fn focus_handle(&self, _cx: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl CheckboxGroup {
/// Create a new checkbox group
pub fn new(cx: &mut Context<Self>) -> Self {
Self {
choices: Vec::new(),
selected: HashSet::new(),
focus_handle: cx.focus_handle().tab_stop(true),
highlight_index: 0,
custom_theme: None,
enabled: true,
}
}
/// Set choices (builder pattern)
#[must_use]
pub fn choices(mut self, choices: Vec<String>) -> Self {
self.choices = choices;
self
}
/// Set initially selected values (builder pattern)
#[must_use]
pub fn with_selected(mut self, selected: Vec<String>) -> Self {
self.selected = selected.into_iter().collect();
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 the currently selected values (sorted)
pub fn get_selected(&self) -> Vec<String> {
let mut result: Vec<String> = self.selected.iter().cloned().collect();
result.sort();
result
}
/// Check if a specific value is selected
pub fn is_selected(&self, value: &str) -> bool {
self.selected.contains(value)
}
/// Set selected values programmatically
pub fn set_selected(&mut self, selected: Vec<String>, cx: &mut Context<Self>) {
self.selected = selected.into_iter().collect();
cx.emit(CheckboxGroupEvent::Change(self.get_selected()));
cx.notify();
}
/// Get the focus handle
pub fn focus_handle(&self) -> &FocusHandle {
&self.focus_handle
}
/// Check if the checkbox group 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_choice(&mut self, choice: String, cx: &mut Context<Self>) {
if self.selected.contains(&choice) {
self.selected.remove(&choice);
} else {
self.selected.insert(choice);
}
cx.emit(CheckboxGroupEvent::Change(self.get_selected()));
}
}
impl Render for CheckboxGroup {
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 focus_handle = self.focus_handle.clone();
let is_focused = self.focus_handle.is_focused(window);
let highlight_index = self.highlight_index;
let num_choices = self.choices.len();
let enabled = self.enabled;
with_focus_actions(
div()
.id("ccf_checkbox_group")
.track_focus(&focus_handle)
.tab_stop(enabled),
cx,
)
.on_key_down(cx.listener(move |group, event: &KeyDownEvent, window, cx| {
if !group.enabled {
return;
}
if handle_tab_navigation(event, window) {
return;
}
match event.keystroke.key.as_str() {
"up" => {
if group.highlight_index > 0 {
group.highlight_index -= 1;
} else if num_choices > 0 {
group.highlight_index = num_choices - 1;
}
cx.notify();
}
"down" => {
if group.highlight_index < num_choices.saturating_sub(1) {
group.highlight_index += 1;
} else {
group.highlight_index = 0;
}
cx.notify();
}
"space" => {
if let Some(choice) = group.choices.get(group.highlight_index).cloned() {
group.toggle_choice(choice, cx);
}
cx.notify();
}
_ => {}
}
}))
.flex()
.flex_col()
.gap_1()
.p_2()
.when(enabled, |d| d.bg(rgb(theme.bg_input)))
.when(!enabled, |d| d.bg(rgb(theme.disabled_bg)))
.border_1()
.when(enabled, |d| {
d.border_color(if is_focused { rgb(theme.border_focus) } else { rgb(theme.border_input) })
})
.when(!enabled, |d| d.border_color(rgb(theme.disabled_bg)))
.rounded_md()
.children(self.choices.iter().enumerate().map(|(idx, choice)| {
let choice_clone = choice.clone();
let is_selected = self.selected.contains(choice);
let is_highlighted = is_focused && idx == highlight_index && enabled;
div()
.id(("ccf_checkbox_group_choice", idx))
.flex()
.flex_row()
.gap_2()
.items_center()
.py_1()
.px_1()
.cursor_for_enabled(enabled)
.rounded_sm()
.when(is_highlighted, |d| d.bg(rgb(theme.bg_input_hover)))
.when(!is_highlighted && enabled, |d| d.hover(|d| d.bg(rgb(theme.bg_input_hover))))
.when(enabled, |d| {
d.on_click(cx.listener(move |group, _event, window, cx| {
group.focus_handle.focus(window);
group.highlight_index = idx;
group.toggle_choice(choice_clone.clone(), cx);
cx.notify();
}))
})
.child({
// Checkbox
let (bg_color, border_color, check_color) = if enabled {
if is_selected {
(theme.accent, theme.border_checkbox, theme.bg_white)
} else {
(theme.bg_input, theme.border_checkbox, 0)
}
} else if is_selected {
(theme.disabled_text, theme.disabled_text, theme.disabled_bg)
} else {
(theme.disabled_bg, theme.disabled_text, 0)
};
div()
.w(px(16.))
.h(px(16.))
.border_1()
.border_color(rgb(border_color))
.bg(rgb(bg_color))
.rounded(px(3.))
.when(is_selected, |d| {
d.child(
div()
.flex()
.items_center()
.justify_center()
.size_full()
.text_color(rgb(check_color))
.text_xs()
.child("✓")
)
})
})
.child(
div()
.text_sm()
.when(enabled, |d| d.text_color(rgb(theme.text_value)))
.when(!enabled, |d| d.text_color(rgb(theme.disabled_text)))
.child(choice.clone())
)
}))
}
}