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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Combobox component - Searchable dropdown combining Input and Select functionality.
//!
//! Features:
//! - Searchable input with live filtering
//! - Keyboard navigation (Up/Down arrows, Enter, Escape)
//! - Custom render functions for items
//! - Multiple selection mode
//! - Virtual scrolling for large lists
//! - Clear button to reset selection
//! - Full Styled trait support for customization
use crate::components::icon::Icon;
use crate::theme::use_theme;
use kael::{prelude::*, *};
actions!(
combobox,
[
ComboboxUp,
ComboboxDown,
ComboboxConfirm,
ComboboxCancel,
ComboboxClear
]
);
const DROPDOWN_MARGIN: Pixels = px(4.0);
/// Events emitted by the Combobox
#[derive(Clone, Debug)]
pub enum ComboboxEvent {
Change,
Search,
}
/// Combobox state for managing selections and search
pub struct ComboboxState<T: Clone + 'static> {
pub selected: Vec<T>,
pub search_text: String,
pub is_open: bool,
pub focused_index: Option<usize>,
}
impl<T: Clone + 'static> ComboboxState<T> {
pub fn new() -> Self {
Self {
selected: Vec::new(),
search_text: String::new(),
is_open: false,
focused_index: None,
}
}
pub fn with_selected(mut self, item: T) -> Self {
self.selected.push(item);
self
}
pub fn toggle_open(&mut self) {
self.is_open = !self.is_open;
if !self.is_open {
self.search_text.clear();
self.focused_index = None;
}
}
pub fn close(&mut self) {
self.is_open = false;
self.search_text.clear();
self.focused_index = None;
}
pub fn select_item(&mut self, item: T, multi_select: bool) {
if multi_select {
self.selected.push(item);
} else {
self.selected = vec![item];
self.is_open = false;
}
}
pub fn clear(&mut self) {
self.selected.clear();
self.search_text.clear();
}
pub fn is_selected(&self, item: &T) -> bool
where
T: PartialEq,
{
self.selected.iter().any(|s| s == item)
}
}
impl<T: Clone + 'static> Default for ComboboxState<T> {
fn default() -> Self {
Self::new()
}
}
/// Combobox component with searchable dropdown
pub struct Combobox<T: Clone + 'static> {
focus_handle: FocusHandle,
items: Vec<T>,
state: Entity<ComboboxState<T>>,
placeholder: SharedString,
disabled: bool,
clearable: bool,
multi_select: bool,
max_height: Pixels,
filter_fn: Box<dyn Fn(&T, &str) -> bool>,
render_item: Box<dyn Fn(&T) -> SharedString>,
render_selected: Option<Box<dyn Fn(&[T]) -> SharedString>>,
on_select: Option<Box<dyn Fn(&T, &mut Window, &mut App)>>,
on_search: Option<Box<dyn Fn(&str, &mut App)>>,
bounds: Bounds<Pixels>,
style: StyleRefinement,
}
impl<T: Clone + 'static> Combobox<T> {
/// Create a new combobox with items and state entity
pub fn new(items: Vec<T>, state: &Entity<ComboboxState<T>>, cx: &mut Context<Self>) -> Self {
Self {
focus_handle: cx.focus_handle(),
items,
state: state.clone(),
placeholder: "Search...".into(),
disabled: false,
clearable: true,
multi_select: false,
max_height: px(300.0),
filter_fn: Box::new(|_, _| true),
render_item: Box::new(|_| "Item".into()),
render_selected: None,
on_select: None,
on_search: None,
bounds: Bounds::default(),
style: StyleRefinement::default(),
}
}
/// Set the placeholder text
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = placeholder.into();
self
}
/// Set disabled state
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Enable/disable clear button
pub fn clearable(mut self, clearable: bool) -> Self {
self.clearable = clearable;
self
}
/// Enable multiple selection mode
pub fn multi_select(mut self, multi_select: bool) -> Self {
self.multi_select = multi_select;
self
}
/// Set maximum dropdown height
pub fn max_height(mut self, height: impl Into<Pixels>) -> Self {
self.max_height = height.into();
self
}
/// Set custom filter function
///
/// # Example
/// ```rust,ignore
/// combobox.filter_fn(|item, search| {
/// item.name.to_lowercase().contains(&search.to_lowercase())
/// })
/// ```
pub fn filter_fn<F>(mut self, f: F) -> Self
where
F: Fn(&T, &str) -> bool + 'static,
{
self.filter_fn = Box::new(f);
self
}
/// Set custom item render function
///
/// # Example
/// ```rust,ignore
/// combobox.render_item(|item| item.name.clone().into())
/// ```
pub fn render_item<F>(mut self, f: F) -> Self
where
F: Fn(&T) -> SharedString + 'static,
{
self.render_item = Box::new(f);
self
}
/// Set custom selected items render function
pub fn render_selected<F>(mut self, f: F) -> Self
where
F: Fn(&[T]) -> SharedString + 'static,
{
self.render_selected = Some(Box::new(f));
self
}
/// Set callback when item is selected
pub fn on_select<F>(mut self, callback: F) -> Self
where
F: Fn(&T, &mut Window, &mut App) + 'static,
{
self.on_select = Some(Box::new(callback));
self
}
/// Set callback when search text changes
pub fn on_search<F>(mut self, callback: F) -> Self
where
F: Fn(&str, &mut App) + 'static,
{
self.on_search = Some(Box::new(callback));
self
}
/// Get filtered items based on current search text
fn filtered_items(&self, cx: &App) -> Vec<(usize, &T)> {
let state = self.state.read(cx);
let search = state.search_text.to_lowercase();
self.items
.iter()
.enumerate()
.filter(|(_, item)| (self.filter_fn)(item, &search))
.collect()
}
/// Toggle dropdown open/close
fn toggle_dropdown(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if !self.disabled {
self.state.update(cx, |state, cx| {
state.toggle_open();
if state.is_open {
window.focus(&self.focus_handle);
state.focused_index = Some(0);
}
cx.notify(); // Trigger re-render
});
}
}
/// Close the dropdown
fn close_dropdown(&mut self, cx: &mut Context<Self>) {
self.state.update(cx, |state, cx| {
state.close();
cx.notify(); // Trigger re-render
});
}
/// Clear all selections
fn clear_selection(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
self.state.update(cx, |state, cx| {
state.clear();
cx.notify(); // Trigger re-render
});
cx.emit(ComboboxEvent::Change);
}
/// Select an item
fn select_item(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
let filtered = self.filtered_items(cx);
if let Some(&(original_idx, _)) = filtered.get(index) {
if let Some(item) = self.items.get(original_idx).cloned() {
self.state.update(cx, |state, cx| {
state.select_item(item.clone(), self.multi_select);
cx.notify(); // Trigger re-render
});
cx.emit(ComboboxEvent::Change);
if let Some(ref callback) = self.on_select {
(callback)(&item, window, cx);
}
}
}
}
/// Handle up arrow key
fn combobox_up(&mut self, _: &ComboboxUp, _: &mut Window, cx: &mut Context<Self>) {
let filtered_count = self.filtered_items(cx).len();
if filtered_count == 0 {
return;
}
self.state.update(cx, |state, _| {
if !state.is_open {
state.is_open = true;
state.focused_index = Some(0);
} else {
let current = state.focused_index.unwrap_or(0);
state.focused_index = Some(if current == 0 {
filtered_count - 1
} else {
current - 1
});
}
});
}
/// Handle down arrow key
fn combobox_down(&mut self, _: &ComboboxDown, _: &mut Window, cx: &mut Context<Self>) {
let filtered_count = self.filtered_items(cx).len();
if filtered_count == 0 {
return;
}
self.state.update(cx, |state, _| {
if !state.is_open {
state.is_open = true;
state.focused_index = Some(0);
} else {
let current = state.focused_index.unwrap_or(0);
state.focused_index = Some(if current >= filtered_count - 1 {
0
} else {
current + 1
});
}
});
}
/// Handle enter key (confirm selection)
fn combobox_confirm(
&mut self,
_: &ComboboxConfirm,
window: &mut Window,
cx: &mut Context<Self>,
) {
let state = self.state.read(cx);
if state.is_open {
if let Some(idx) = state.focused_index {
self.select_item(idx, window, cx);
}
} else {
self.toggle_dropdown(window, cx);
}
}
/// Handle escape key (cancel/close)
fn combobox_cancel(&mut self, _: &ComboboxCancel, _: &mut Window, cx: &mut Context<Self>) {
if self.state.read(cx).is_open {
self.close_dropdown(cx);
}
}
/// Handle clear action
fn combobox_clear(&mut self, _: &ComboboxClear, window: &mut Window, cx: &mut Context<Self>) {
self.clear_selection(window, cx);
}
}
impl<T: Clone + 'static> Styled for Combobox<T> {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl<T: Clone + PartialEq + 'static> Render for Combobox<T> {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = use_theme();
let user_style = self.style.clone();
let state = self.state.read(cx);
let is_open = state.is_open;
let search_text = state.search_text.clone();
let focused_idx = state.focused_index;
let has_selection = !state.selected.is_empty();
let display_text: SharedString = if !search_text.is_empty() {
search_text.clone().into()
} else if !state.selected.is_empty() {
if let Some(ref render_fn) = self.render_selected {
render_fn(&state.selected)
} else if self.multi_select {
format!("{} selected", state.selected.len()).into()
} else {
(self.render_item)(&state.selected[0])
}
} else {
self.placeholder.clone()
};
let bounds = self.bounds;
let is_searching = !search_text.is_empty();
let trigger = div()
.id("combobox-trigger")
.relative()
.flex()
.items_center()
.justify_between()
.h(px(40.0))
.px(px(12.0))
.bg(theme.tokens.background)
.border_1()
.border_color(if is_open {
theme.tokens.ring
} else {
theme.tokens.input
})
.rounded(theme.tokens.radius_md)
.text_color(if has_selection && !is_searching {
theme.tokens.foreground
} else {
theme.tokens.muted_foreground
})
.text_size(px(14.0))
.font_family(theme.tokens.font_family.clone())
.cursor(if self.disabled {
CursorStyle::Arrow
} else {
CursorStyle::PointingHand
})
.when(!self.disabled, |div: Stateful<Div>| {
div.hover(|mut style| {
style.border_color = Some(theme.tokens.ring);
style
})
})
.on_mouse_down(
MouseButton::Left,
cx.listener(|this, _, window, cx| {
if !this.disabled {
window.focus(&this.focus_handle);
this.state.update(cx, |state, _| {
if !state.is_open {
state.is_open = true;
state.focused_index = Some(0);
}
});
}
}),
)
.child(div().flex_1().child(display_text))
.child(
div()
.flex()
.items_center()
.gap(px(4.0))
.when(
self.clearable && has_selection && !self.disabled,
|this_div| {
this_div.child(
div()
.w(px(20.0))
.h(px(20.0))
.flex()
.items_center()
.justify_center()
.rounded(px(10.0))
.cursor(CursorStyle::PointingHand)
.hover(|mut style| {
style.background = Some(theme.tokens.muted.into());
style
})
.on_mouse_down(
MouseButton::Left,
cx.listener(|this, _event, window, cx| {
this.clear_selection(window, cx);
}),
)
.child(
Icon::new("x")
.size(px(14.0))
.color(theme.tokens.muted_foreground),
),
)
},
)
.child(
Icon::new(if is_open {
"chevron-up"
} else {
"chevron-down"
})
.size(px(14.0))
.color(theme.tokens.muted_foreground),
),
)
.child({
let entity = cx.entity().clone();
canvas_with_prepaint(
move |bounds, _, cx| {
entity.update(cx, |this, _| {
this.bounds = bounds;
})
},
|_, _, _, _| {},
)
.absolute()
.size_full()
});
div()
.relative()
.w_full()
.key_context("Combobox")
.track_focus(&self.focus_handle)
.when(is_open && !self.disabled, |this: Div| {
this.on_action(cx.listener(Combobox::combobox_up))
.on_action(cx.listener(Combobox::combobox_down))
.on_action(cx.listener(Combobox::combobox_confirm))
.on_action(cx.listener(Combobox::combobox_cancel))
.on_action(cx.listener(Combobox::combobox_clear))
})
.when(is_open, |this: Div| {
this.on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| {
if event.keystroke.key == "backspace" {
this.state.update(cx, |state, cx| {
state.search_text.pop();
cx.notify(); // Trigger re-render
});
cx.emit(ComboboxEvent::Search);
} else if event.keystroke.key.len() == 1
&& !event.keystroke.modifiers.control
&& !event.keystroke.modifiers.platform
&& !event.keystroke.modifiers.alt {
this.state.update(cx, |state, cx| {
state.search_text.push_str(&event.keystroke.key);
state.focused_index = Some(0);
cx.notify(); // Trigger re-render
});
cx.emit(ComboboxEvent::Search);
if let Some(ref callback) = this.on_search {
let search = this.state.read(cx).search_text.clone();
callback(&search, cx);
}
}
}))
})
.on_mouse_down_out(cx.listener(|this, _, _, cx| {
if this.state.read(cx).is_open {
this.close_dropdown(cx);
}
}))
.child(trigger)
.when(is_open, |this| {
this.child(
deferred(
anchored()
.snap_to_window_with_margin(Edges::all(DROPDOWN_MARGIN))
.child(
div()
.occlude()
.w(bounds.size.width)
.child(
div()
.occlude()
.mt(DROPDOWN_MARGIN)
.bg(theme.tokens.popover)
.border_2()
.border_color(theme.tokens.ring)
.rounded(theme.tokens.radius_md)
.shadow_xl()
.overflow_hidden()
.child({
let filtered = self.filtered_items(cx);
div()
.id("combobox-dropdown-list")
.max_h(self.max_height)
.overflow_y_scroll()
.py(px(4.0))
.when(filtered.is_empty(), |this| {
this.child(
div()
.px(px(12.0))
.py(px(16.0))
.flex()
.items_center()
.justify_center()
.text_size(px(13.0))
.font_family(theme.tokens.font_family.clone())
.text_color(theme.tokens.muted_foreground)
.child("No results found")
)
})
.when(!filtered.is_empty(), |this| {
this.children(
filtered.iter().enumerate().map(|(display_idx, &(_original_idx, item))| {
let is_focused = focused_idx == Some(display_idx);
let is_selected = state.is_selected(item);
let item_text = (self.render_item)(item);
div()
.px(px(12.0))
.py(px(8.0))
.flex()
.items_center()
.justify_between()
.text_color(if is_selected {
theme.tokens.primary
} else {
theme.tokens.popover_foreground
})
.bg(if is_focused {
theme.tokens.accent
} else {
kael::transparent_black()
})
.hover(|mut style| {
style.background = Some(theme.tokens.accent.into());
style
})
.cursor(CursorStyle::PointingHand)
.text_size(px(14.0))
.font_family(theme.tokens.font_family.clone())
.on_mouse_down(MouseButton::Left, cx.listener(move |this, _, window, cx| {
this.select_item(display_idx, window, cx);
}))
.child(item_text)
.when(is_selected, |div| {
div.child(
Icon::new("check")
.size(px(14.0))
.color(theme.tokens.primary)
)
})
})
)
})
})
)
)
)
.with_priority(1)
)
})
.map(|this| {
let mut div = this;
div.style().refine(&user_style);
div
})
}
}
/// Initialize combobox key bindings
pub fn init_combobox(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("up", ComboboxUp, Some("Combobox")),
KeyBinding::new("down", ComboboxDown, Some("Combobox")),
KeyBinding::new("enter", ComboboxConfirm, Some("Combobox")),
KeyBinding::new("escape", ComboboxCancel, Some("Combobox")),
#[cfg(target_os = "macos")]
KeyBinding::new("cmd-k", ComboboxClear, Some("Combobox")),
#[cfg(not(target_os = "macos"))]
KeyBinding::new("ctrl-k", ComboboxClear, Some("Combobox")),
]);
}
impl<T: Clone + 'static> Focusable for Combobox<T> {
fn focus_handle(&self, _: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl<T: Clone + 'static> EventEmitter<ComboboxEvent> for Combobox<T> {}