1use gpui::ColorExt as _;
2use gpui::{
3 AnyElement, App, Bounds, ClickEvent, Context, DismissEvent, Edges, ElementId, Entity,
4 EventEmitter, FocusHandle, Focusable, Hsla, InteractiveElement, IntoElement, Length,
5 MouseDownEvent, ParentElement, Pixels, Render, RenderOnce, SharedString,
6 StatefulInteractiveElement, StyleRefinement, Styled, Window, deferred, div,
7 prelude::FluentBuilder, px, rems,
8};
9
10use rust_i18n::t;
11
12pub use crate::select::Caret;
13
14use crate::ThemeStyled as _;
15use crate::{
16 ActiveTheme, Disableable, ElementExt as _, Icon, IconName, IndexPath, Sizable, Size,
17 StyleSized, StyledExt, h_flex,
18 input::{clear_button, input_style},
19 list::{List, ListState},
20 searchable_list::{
21 SearchableListAdapter, SearchableListChange, SearchableListDelegate, SearchableListItem,
22 SearchableListState,
23 },
24 v_flex,
25};
26use gpui_base::{Combobox as BaseCombobox, GlobalState};
27
28pub struct ComboboxTriggerContext<'a, D: SearchableListDelegate + 'static> {
35 selection: &'a [(IndexPath, D::Item)],
36 placeholder: Option<&'a SharedString>,
37 open: bool,
38 disabled: bool,
39 size: Size,
40}
41
42impl<'a, D: SearchableListDelegate + 'static> ComboboxTriggerContext<'a, D> {
43 pub fn selection(&self) -> &'a [(IndexPath, D::Item)] {
45 self.selection
46 }
47
48 pub fn placeholder(&self) -> Option<&'a SharedString> {
49 self.placeholder
50 }
51
52 pub fn is_open(&self) -> bool {
54 self.open
55 }
56
57 pub fn is_disabled(&self) -> bool {
58 self.disabled
59 }
60
61 pub fn size(&self) -> Size {
62 self.size
63 }
64}
65
66pub type ComboboxChange = SearchableListChange;
70
71struct ComboboxOptions {
74 style: StyleRefinement,
75 size: Size,
76 cleanable: bool,
77 placeholder: Option<SharedString>,
78 search_placeholder: Option<SharedString>,
79 menu_width: Length,
80 menu_max_h: Length,
81 disabled: bool,
82 appearance: bool,
83 focus_ring_enabled: bool,
84 trigger_icon: Option<Icon>,
85 check_icon: Option<Icon>,
86}
87
88impl Default for ComboboxOptions {
89 fn default() -> Self {
90 Self {
91 style: StyleRefinement::default(),
92 size: Size::default(),
93 cleanable: false,
94 placeholder: None,
95 search_placeholder: None,
96 menu_width: Length::Auto,
97 menu_max_h: rems(20.).into(),
98 disabled: false,
99 appearance: true,
100 focus_ring_enabled: true,
101 trigger_icon: None,
102 check_icon: None,
103 }
104 }
105}
106
107pub struct ComboboxState<D: SearchableListDelegate + 'static>
111where
112 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
113{
114 pub(crate) state: SearchableListState<D>,
115
116 multiple: bool,
118 searchable: bool,
119 trigger_icon: Option<Icon>,
120 check_icon: Option<Icon>,
121 render_trigger: Option<
122 Box<dyn Fn(&ComboboxTriggerContext<D>, &mut Window, &mut App) -> AnyElement + 'static>,
123 >,
124 footer: Option<Box<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>>,
125 focus_ring_enabled: bool,
126}
127
128pub enum ComboboxEvent<D: SearchableListDelegate + 'static>
130where
131 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
132{
133 Change(Vec<<D::Item as SearchableListItem>::Value>),
135 Confirm(Vec<<D::Item as SearchableListItem>::Value>),
137}
138
139impl<D> ComboboxState<D>
140where
141 D: SearchableListDelegate + 'static,
142 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
143{
144 pub fn new(
146 delegate: D,
147 selected_indices: Vec<IndexPath>,
148 window: &mut Window,
149 cx: &mut Context<Self>,
150 ) -> Self {
151 let weak = cx.entity().downgrade();
152 let weak_confirm = weak.clone();
153 let weak_cancel = weak.clone();
154 let weak_empty = weak;
155
156 let state = SearchableListState::new(
157 delegate,
158 selected_indices,
159 move |selected_index, _secondary, window, cx| {
160 cx.defer_in(window, {
161 let weak_confirm = weak_confirm.clone();
162 move |list_state, window, cx| {
163 let Some(index) = selected_index else {
164 return;
165 };
166
167 let Some(item) = list_state.delegate().delegate.item(index).cloned() else {
168 return;
169 };
170
171 let ix = index;
172
173 let Some(weak) = weak_confirm.upgrade() else {
174 return;
175 };
176
177 let (multiple, mut selection) = {
178 let s = weak.read(cx);
179 (s.multiple, s.state.selection.clone())
180 };
181
182 let changes = Self::selection_changes(multiple, &selection, ix, &item);
183
184 let before_indices: Vec<IndexPath> =
185 selection.iter().map(|(ix, _)| *ix).collect();
186
187 list_state
190 .delegate_mut()
191 .delegate
192 .on_will_change(&mut selection, &changes);
193
194 let after_indices: Vec<IndexPath> =
195 selection.iter().map(|(ix, _)| *ix).collect();
196 let changed = before_indices != after_indices;
197 let should_close = changed && !multiple;
198
199 let new_selection = weak_confirm.update(cx, |this, cx| {
200 this.state.selection = selection;
201
202 if changed {
203 cx.emit(ComboboxEvent::Change(this.selected_values()));
204 cx.notify();
205 }
206
207 if should_close {
208 cx.emit(ComboboxEvent::Confirm(this.selected_values()));
209 this.set_open(false, cx);
210 this.focus(window, cx);
211 }
212
213 this.state.selection.clone()
214 });
215
216 if let Ok(new_selection) = new_selection {
218 list_state
219 .delegate_mut()
220 .update_selection_snapshot(new_selection.clone());
221
222 if should_close {
223 list_state
224 .delegate_mut()
225 .delegate
226 .on_confirm(&new_selection);
227 }
228 }
229 }
230 });
231 },
232 move |_final_selected_index, window, cx| {
234 cx.defer_in(window, {
235 let weak_cancel = weak_cancel.clone();
236 move |_list_state, window, cx| {
237 _ = weak_cancel.update(cx, |this, cx| {
238 cx.emit(ComboboxEvent::Confirm(this.selected_values()));
239 this.set_open(false, cx);
240 this.focus(window, cx);
241 });
242 }
243 });
244 },
245 move |window, cx| {
247 if let Some(empty) = weak_empty
248 .upgrade()
249 .and_then(|e| e.read(cx).state.empty.as_ref().map(|f| f(window, cx)))
250 {
251 empty
252 } else {
253 h_flex()
254 .justify_center()
255 .py_6()
256 .text_color(cx.theme().muted_foreground.opacity(0.6))
257 .child(Icon::new(IconName::Inbox).size(px(28.)))
258 .into_any_element()
259 }
260 },
261 Self::on_blur,
262 window,
263 cx,
264 );
265
266 Self {
267 state,
268 multiple: false,
269 searchable: false,
270 trigger_icon: None,
271 check_icon: None,
272 render_trigger: None,
273 footer: None,
274 focus_ring_enabled: true,
275 }
276 }
277
278 pub fn multiple(mut self, multiple: bool) -> Self {
283 self.multiple = multiple;
284 self
285 }
286
287 pub fn searchable(mut self, searchable: bool) -> Self {
289 self.searchable = searchable;
290 self
291 }
292
293 pub fn selected_values(&self) -> Vec<<D::Item as SearchableListItem>::Value> {
295 self.state.selected_values()
296 }
297
298 pub fn selected_value(&self) -> Option<<D::Item as SearchableListItem>::Value> {
302 self.state.selected_values().into_iter().next()
303 }
304
305 pub fn selection(&self) -> &[(IndexPath, D::Item)] {
307 self.state.selection()
308 }
309
310 pub fn set_selected_values(
316 &mut self,
317 values: &[<D::Item as SearchableListItem>::Value],
318 window: &mut Window,
319 cx: &mut Context<Self>,
320 ) {
321 self.state.clear_query(window, cx);
322
323 let selected_indices = {
324 let list = self.state.list.read(cx);
325 let delegate = &list.delegate().delegate;
326
327 values
328 .iter()
329 .filter_map(|value| delegate.position(value))
330 .collect::<Vec<_>>()
331 };
332
333 self.set_selected_indices(selected_indices, window, cx);
334 }
335
336 pub fn set_selected_indices(
338 &mut self,
339 indices: impl IntoIterator<Item = IndexPath>,
340 _window: &mut Window,
341 cx: &mut Context<Self>,
342 ) {
343 self.state.set_selected_indices(indices, cx);
344 self.state.sync_snapshot(cx);
345 cx.notify();
346 }
347
348 pub fn add_selected_index(&mut self, index: IndexPath, cx: &mut Context<Self>) -> bool {
350 let added = self.state.add_selected_index(index, cx);
351
352 if added {
353 self.state.sync_snapshot(cx);
354 cx.notify();
355 }
356
357 added
358 }
359
360 pub fn remove_selected_index(&mut self, index: IndexPath, cx: &mut Context<Self>) -> bool {
362 let removed = self.state.remove_selected_index(index);
363
364 if removed {
365 self.state.sync_snapshot(cx);
366 }
367
368 removed
369 }
370
371 pub fn clear_selection(&mut self, cx: &mut Context<Self>) {
373 self.state.selection.clear();
374 self.state.sync_snapshot(cx);
375 cx.emit(ComboboxEvent::Change(self.selected_values()));
376 cx.notify();
377 }
378
379 pub fn set_items(&mut self, items: D, _: &mut Window, cx: &mut Context<Self>) {
381 self.state.list.update(cx, |list, _| {
382 list.delegate_mut().delegate = items;
383 });
384 }
385
386 pub fn focus(&self, window: &mut Window, cx: &mut App) {
388 self.state.focus_handle.focus(window, cx);
389 }
390
391 pub fn query(&self, cx: &App) -> SharedString {
393 self.state.list.read(cx).query_input.read(cx).value()
394 }
395
396 pub fn set_query(&self, query: impl Into<SharedString>, window: &mut Window, cx: &mut App) {
398 let query = query.into();
399 self.state.list.update(cx, |list, cx| {
400 list.set_query(query.as_ref(), window, cx);
401 });
402 }
403
404 fn selection_changes(
405 multiple: bool,
406 selection: &[(IndexPath, D::Item)],
407 ix: IndexPath,
408 item: &D::Item,
409 ) -> Vec<SearchableListChange> {
410 let is_selected = selection
411 .iter()
412 .any(|(_, selected_item)| selected_item.value() == item.value());
413
414 if multiple {
415 if is_selected {
416 vec![SearchableListChange::Deselect { index: ix }]
417 } else {
418 vec![SearchableListChange::Select { index: ix }]
419 }
420 } else {
421 let mut changes: Vec<SearchableListChange> = selection
422 .iter()
423 .map(|(cur_ix, _)| SearchableListChange::Deselect { index: *cur_ix })
424 .collect();
425 changes.push(SearchableListChange::Select { index: ix });
426 changes
427 }
428 }
429
430 #[allow(dead_code)]
434 pub(crate) fn handle_item_select(
435 &mut self,
436 ix: IndexPath,
437 window: &mut Window,
438 cx: &mut Context<Self>,
439 ) {
440 let Some(item) = self
441 .state
442 .list
443 .read(cx)
444 .delegate()
445 .delegate
446 .item(ix)
447 .cloned()
448 else {
449 return;
450 };
451
452 let changes = Self::selection_changes(self.multiple, &self.state.selection, ix, &item);
453
454 let mut selection = self.state.selection.clone();
455 let before_indices: Vec<IndexPath> = selection.iter().map(|(ix, _)| *ix).collect();
456
457 self.state.list.update(cx, |list, _cx| {
458 list.delegate_mut()
459 .delegate
460 .on_will_change(&mut selection, &changes);
461 });
462
463 let after_indices: Vec<IndexPath> = selection.iter().map(|(ix, _)| *ix).collect();
464 let changed = before_indices != after_indices;
465 let should_close = changed && !self.multiple;
466
467 self.state.selection = selection;
468 self.state.sync_snapshot(cx);
469
470 if changed {
471 cx.emit(ComboboxEvent::Change(self.selected_values()));
472 cx.notify();
473 }
474
475 if should_close {
476 let final_selection = self.state.selection.clone();
477 self.state.list.update(cx, |list, _cx| {
478 list.delegate_mut().delegate.on_confirm(&final_selection);
479 });
480
481 cx.emit(ComboboxEvent::Confirm(self.selected_values()));
482 self.set_open(false, cx);
483 self.focus(window, cx);
484 }
485 }
486
487 fn on_blur(&mut self, window: &mut Window, cx: &mut Context<Self>) {
488 if self.state.list.read(cx).is_focused(window, cx)
489 || self.state.focus_handle.is_focused(window)
490 {
491 return;
492 }
493
494 self.set_open(false, cx);
495 cx.notify();
496 }
497
498 fn toggle_menu(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
499 cx.stop_propagation();
500
501 self.set_open(!self.state.open, cx);
502
503 if self.state.open {
504 self.state.list.focus_handle(cx).focus(window, cx);
505 } else {
506 cx.emit(ComboboxEvent::Confirm(self.selected_values()));
507 self.focus(window, cx);
508 }
509
510 cx.notify();
511 }
512
513 fn dismiss(&mut self, event: &MouseDownEvent, window: &mut Window, cx: &mut Context<Self>) {
519 if !self.state.open || self.state.bounds.contains(&event.position) {
520 return;
521 }
522
523 cx.stop_propagation();
524 cx.emit(ComboboxEvent::Confirm(self.selected_values()));
525
526 self.set_open(false, cx);
527 self.focus(window, cx);
528 cx.notify();
529 }
530
531 fn set_open(&mut self, open: bool, cx: &mut Context<Self>) {
532 self.state.open = open;
533 self.state.deferred_context = open.then(|| GlobalState::register_deferred_popover(cx));
534
535 cx.notify();
536 }
537
538 fn clean(&mut self, _: &ClickEvent, _: &mut Window, cx: &mut Context<Self>) {
539 cx.stop_propagation();
540 self.clear_selection(cx);
541 }
542
543 fn default_trigger_body(&self, _window: &mut Window, cx: &mut Context<Self>) -> AnyElement {
544 let placeholder_text = self
545 .state
546 .placeholder
547 .clone()
548 .unwrap_or_else(|| t!("Combobox.placeholder").into());
549
550 if self.state.selection.is_empty() {
551 return div()
552 .text_color(cx.theme().muted_foreground)
553 .child(placeholder_text)
554 .into_any_element();
555 }
556
557 if self.multiple {
558 let items: Vec<SharedString> = self
559 .state
560 .selection
561 .iter()
562 .map(|(_, i)| i.title())
563 .collect();
564
565 div()
566 .w_full()
567 .overflow_hidden()
568 .whitespace_nowrap()
569 .truncate()
570 .child(items.join(", "))
571 .into_any_element()
572 } else {
573 let title = self
574 .state
575 .selection
576 .first()
577 .map(|(_, i)| i.title())
578 .unwrap_or_default();
579
580 div()
581 .w_full()
582 .overflow_hidden()
583 .whitespace_nowrap()
584 .truncate()
585 .child(title)
586 .into_any_element()
587 }
588 }
589}
590
591impl<D> Render for ComboboxState<D>
592where
593 D: SearchableListDelegate + 'static,
594 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
595{
596 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
597 let searchable = self.searchable;
598 let is_focused = self.state.focus_handle.is_focused(window);
599 let show_clean = self.state.cleanable && !self.state.selection.is_empty();
600 let bounds = self.state.bounds;
601 let allow_open = !self.state.disabled;
602 let outline_visible = self.state.open || (is_focused && !self.state.disabled);
603 let disabled = self.state.disabled;
604
605 let (bg, fg) = input_style(disabled, cx);
606
607 self.state.list.update(cx, |list, cx| {
608 list.set_searchable(searchable, cx);
609 list.delegate_mut().size = self.state.size;
610 list.delegate_mut().check_icon = self.check_icon.clone();
611 });
612
613 let selection = &self.state.selection;
614 let placeholder = self.state.placeholder.as_ref();
615 let open = self.state.open;
616 let size = self.state.size;
617 let has_custom_trigger = self.render_trigger.is_some();
618
619 let trigger_body = if let Some(render_trigger) = &self.render_trigger {
620 let trigger = ComboboxTriggerContext {
621 selection,
622 placeholder,
623 open,
624 disabled,
625 size,
626 };
627
628 render_trigger(&trigger, window, cx)
629 } else {
630 self.default_trigger_body(window, cx)
631 };
632
633 let trailing: AnyElement = if has_custom_trigger {
634 div().into_any_element()
635 } else if show_clean {
636 clear_button(cx)
637 .map(|this| {
638 if disabled {
639 this.disabled(true)
640 } else {
641 this.on_click(cx.listener(Self::clean))
642 }
643 })
644 .into_any_element()
645 } else if let Some(icon) = self.trigger_icon.clone() {
646 icon.xsmall()
647 .text_color(cx.theme().muted_foreground)
648 .into_any_element()
649 } else {
650 Caret::new(size)
651 .text_color(cx.theme().muted_foreground)
652 .into_any_element()
653 };
654
655 let toggle_handler: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>> =
656 if allow_open {
657 Some(Box::new(cx.listener(Self::toggle_menu)))
658 } else {
659 None
660 };
661
662 let footer_el = self.footer.as_ref().map(|f| f(window, cx));
663
664 let dismiss_handler: Box<dyn Fn(&MouseDownEvent, &mut Window, &mut App) + 'static> =
665 Box::new(cx.listener(Self::dismiss));
666
667 div().size_full().relative().child(
668 div()
669 .relative()
670 .on_prepaint({
671 let state = cx.entity();
672 move |bounds, _, cx| state.update(cx, |r, _| r.state.bounds = bounds)
673 })
674 .child(render_trigger_container(
675 disabled,
676 self.state.appearance,
677 self.focus_ring_enabled,
678 self.state.size,
679 &self.state.style,
680 bg,
681 fg,
682 outline_visible,
683 allow_open,
684 trigger_body,
685 trailing,
686 toggle_handler,
687 window,
688 cx,
689 ))
690 .when(self.state.open, |this| {
691 this.child(
692 deferred(render_popup_shell(
693 ("combobox-popup", cx.entity_id()),
694 &self.state.list,
695 self.state.menu_width,
696 self.state.search_placeholder.clone(),
697 self.state.size,
698 self.state.menu_max_h,
699 bounds,
700 footer_el,
701 dismiss_handler,
702 cx,
703 ))
704 .with_priority(gpui_base::POPUP_PRIORITY),
705 )
706 }),
707 )
708 }
709}
710
711impl<D> EventEmitter<ComboboxEvent<D>> for ComboboxState<D>
712where
713 D: SearchableListDelegate + 'static,
714 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
715{
716}
717impl<D> EventEmitter<DismissEvent> for ComboboxState<D>
718where
719 D: SearchableListDelegate + 'static,
720 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
721{
722}
723
724impl<D> Focusable for ComboboxState<D>
725where
726 D: SearchableListDelegate + 'static,
727 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
728{
729 fn focus_handle(&self, cx: &App) -> FocusHandle {
730 if self.state.open {
731 self.state.list.focus_handle(cx)
732 } else {
733 self.state.focus_handle.clone()
734 }
735 }
736}
737
738#[derive(IntoElement)]
745pub struct Combobox<D: SearchableListDelegate + 'static>
746where
747 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
748{
749 id: ElementId,
750 state: Entity<ComboboxState<D>>,
751 options: ComboboxOptions,
752 render_trigger: Option<
753 Box<dyn Fn(&ComboboxTriggerContext<D>, &mut Window, &mut App) -> AnyElement + 'static>,
754 >,
755 footer: Option<Box<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>>,
756 empty: Option<Box<dyn Fn(&mut Window, &App) -> AnyElement + 'static>>,
757}
758
759impl<D> Combobox<D>
760where
761 D: SearchableListDelegate + 'static,
762 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
763{
764 pub fn new(state: &Entity<ComboboxState<D>>) -> Self {
765 Self {
766 id: ("multi-combo-box", state.entity_id()).into(),
767 state: state.clone(),
768 options: ComboboxOptions::default(),
769 render_trigger: None,
770 footer: None,
771 empty: None,
772 }
773 }
774
775 pub fn menu_width(mut self, width: impl Into<Length>) -> Self {
777 self.options.menu_width = width.into();
778 self
779 }
780
781 pub fn menu_max_h(mut self, max_h: impl Into<Length>) -> Self {
783 self.options.menu_max_h = max_h.into();
784 self
785 }
786
787 pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
789 self.options.placeholder = Some(placeholder.into());
790 self
791 }
792
793 pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
795 self.options.trigger_icon = Some(icon.into());
796 self
797 }
798
799 pub fn check_icon(mut self, icon: impl Into<Icon>) -> Self {
801 self.options.check_icon = Some(icon.into());
802 self
803 }
804
805 pub fn search_placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
807 self.options.search_placeholder = Some(placeholder.into());
808 self
809 }
810
811 pub fn cleanable(mut self, cleanable: bool) -> Self {
813 self.options.cleanable = cleanable;
814 self
815 }
816
817 pub fn disabled(mut self, disabled: bool) -> Self {
819 self.options.disabled = disabled;
820 self
821 }
822
823 pub fn empty<E: IntoElement + 'static>(
825 mut self,
826 builder: impl Fn(&mut Window, &App) -> E + 'static,
827 ) -> Self {
828 self.empty = Some(Box::new(move |window, cx| {
829 builder(window, cx).into_any_element()
830 }));
831 self
832 }
833
834 pub fn appearance(mut self, appearance: bool) -> Self {
836 self.options.appearance = appearance;
837 self
838 }
839
840 pub fn render_trigger<E: IntoElement + 'static>(
842 mut self,
843 f: impl Fn(&ComboboxTriggerContext<D>, &mut Window, &mut App) -> E + 'static,
844 ) -> Self {
845 self.render_trigger = Some(Box::new(move |trigger, window, cx| {
846 f(trigger, window, cx).into_any_element()
847 }));
848 self
849 }
850
851 pub fn footer<E: IntoElement + 'static>(
853 mut self,
854 f: impl Fn(&mut Window, &mut App) -> E + 'static,
855 ) -> Self {
856 self.footer = Some(Box::new(move |window, cx| f(window, cx).into_any_element()));
857 self
858 }
859}
860
861impl<D> Sizable for Combobox<D>
862where
863 D: SearchableListDelegate + 'static,
864 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
865{
866 fn with_size(mut self, size: impl Into<Size>) -> Self {
867 self.options.size = size.into();
868 self
869 }
870}
871
872impl<D> crate::FocusableExt for Combobox<D>
873where
874 D: SearchableListDelegate + 'static,
875 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
876{
877 fn focus_ring(mut self, enabled: bool) -> Self {
878 self.options.focus_ring_enabled = enabled;
879 self
880 }
881
882 fn is_focus_ring_enabled(&self) -> bool {
883 self.options.focus_ring_enabled
884 }
885}
886
887impl<D> Styled for Combobox<D>
888where
889 D: SearchableListDelegate + 'static,
890 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
891{
892 fn style(&mut self) -> &mut StyleRefinement {
893 &mut self.options.style
894 }
895}
896
897impl<D> RenderOnce for Combobox<D>
898where
899 D: SearchableListDelegate + 'static,
900 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
901{
902 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
903 let disabled = self.options.disabled;
904 let focus_handle = self.state.read(cx).state.focus_handle.clone();
905 let render_trigger = self.render_trigger;
906 let footer = self.footer;
907 let empty = self.empty;
908 let opts = self.options;
909
910 self.state.update(cx, |this, _| {
911 this.state.style = opts.style;
912 this.state.size = opts.size;
913 this.state.cleanable = opts.cleanable;
914 this.state.placeholder = opts.placeholder;
915 this.state.search_placeholder = opts.search_placeholder;
916 this.state.menu_width = opts.menu_width;
917 this.state.menu_max_h = opts.menu_max_h;
918 this.state.disabled = opts.disabled;
919 this.state.appearance = opts.appearance;
920 this.focus_ring_enabled = opts.focus_ring_enabled;
921 this.trigger_icon = opts.trigger_icon;
922 this.check_icon = opts.check_icon;
923 this.render_trigger = render_trigger;
924 this.footer = footer;
925
926 if let Some(empty) = empty {
927 this.state.empty = Some(empty);
928 }
929 });
930
931 let is_open = self.state.read(cx).state.open;
932 let content_focus_handle = self.state.read(cx).state.list.focus_handle(cx);
933 let open_state = self.state.clone();
934 let confirm_state = self.state.clone();
935
936 BaseCombobox::new(self.id)
937 .open(is_open)
938 .disabled(disabled)
939 .focus_handle(&focus_handle)
940 .content_focus_handle(&content_focus_handle)
941 .on_open_change(move |open, _, cx| {
942 open_state.update(cx, |state, cx| state.set_open(open, cx));
943 })
944 .on_dismiss(move |_, cx| {
947 confirm_state.update(cx, |state, cx| {
948 cx.emit(ComboboxEvent::Confirm(state.selected_values()));
949 });
950 })
951 .size_full()
952 .child(self.state)
953 }
954}
955
956#[allow(clippy::too_many_arguments)]
960fn render_trigger_container(
961 disabled: bool,
962 appearance: bool,
963 focus_ring_enabled: bool,
964 size: Size,
965 style: &StyleRefinement,
966 bg: Hsla,
967 fg: Hsla,
968 outline_visible: bool,
969 allow_open: bool,
970 trigger_body: AnyElement,
971 trailing: AnyElement,
972 toggle_handler: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
973 window: &Window,
974 cx: &mut App,
975) -> impl IntoElement {
976 div()
977 .id("input")
978 .relative()
979 .flex()
980 .items_center()
981 .justify_between()
982 .border_1()
983 .border_color(cx.theme().transparent)
984 .when(appearance, |this| {
985 this.bg(bg)
986 .text_color(fg)
987 .when(disabled, |this| this.opacity(0.5))
988 .border_color(cx.theme().input)
989 .rounded(cx.theme().radius)
990 })
991 .input_size(size)
992 .input_text_size(size)
993 .refine_style(style)
994 .when(outline_visible && appearance, |this| {
995 this.border_1().border_color(cx.theme().ring)
996 })
997 .when(
998 outline_visible && appearance && focus_ring_enabled,
999 |this| this.focus_ring_style(window, cx),
1000 )
1001 .when(allow_open, |this| {
1002 this.when_some(toggle_handler, |this, handler| this.on_click(handler))
1003 })
1004 .child(
1005 h_flex()
1006 .id("inner")
1007 .w_full()
1008 .min_w_0()
1009 .overflow_hidden()
1010 .whitespace_nowrap()
1011 .items_center()
1012 .justify_between()
1013 .gap_1()
1014 .child(
1015 div()
1016 .flex_1()
1017 .min_w_0()
1018 .overflow_hidden()
1019 .whitespace_nowrap()
1020 .child(trigger_body),
1021 )
1022 .child(trailing),
1023 )
1024}
1025
1026#[allow(clippy::too_many_arguments)]
1028fn render_popup_shell<D: SearchableListDelegate + 'static>(
1029 id: impl Into<ElementId>,
1030 list: &Entity<ListState<SearchableListAdapter<D>>>,
1031 menu_width: Length,
1032 search_placeholder: Option<SharedString>,
1033 size: Size,
1034 menu_max_h: Length,
1035 bounds: Bounds<Pixels>,
1036 footer_el: Option<AnyElement>,
1037 dismiss_handler: Box<dyn Fn(&MouseDownEvent, &mut Window, &mut App) + 'static>,
1038 cx: &mut App,
1039) -> AnyElement {
1040 let has_footer = footer_el.is_some();
1041
1042 crate::popover::dropdown_popup(
1043 id,
1044 bounds,
1045 v_flex()
1046 .occlude()
1047 .map(|this| match menu_width {
1048 Length::Auto => this.w(bounds.size.width + px(2.)),
1049 Length::Definite(w) => this.w(w),
1050 })
1051 .popover_style(cx)
1052 .child(
1053 List::new(list)
1054 .when_some(search_placeholder, |this, placeholder| {
1055 this.search_placeholder(placeholder)
1056 })
1057 .with_size(size)
1058 .max_h(menu_max_h)
1059 .paddings(Edges::all(px(4.))),
1060 )
1061 .when(has_footer, |this| {
1062 this.child(
1063 div()
1064 .border_t_1()
1065 .border_color(cx.theme().border)
1066 .p_1()
1067 .when_some(footer_el, |this, el| this.child(el)),
1068 )
1069 })
1070 .on_mouse_down_out(dismiss_handler),
1071 cx,
1072 )
1073 .into_any_element()
1074}
1075
1076#[cfg(test)]
1079mod tests {
1080 use std::{cell::Cell, rc::Rc};
1081
1082 use gpui::{
1083 AppContext as _, Bounds, Context, Entity, Modifiers, MouseButton, MouseDownEvent, Pixels,
1084 Point, Subscription, TestAppContext, point, px, size,
1085 };
1086
1087 use crate::{
1088 IndexPath,
1089 combobox::{Combobox, ComboboxEvent, ComboboxState},
1090 searchable_list::{
1091 SearchableListChange, SearchableListDelegate, SearchableListItem, SearchableListState,
1092 SearchableVec,
1093 },
1094 };
1095
1096 struct TestComboboxEventCollector {
1097 event_count: Rc<Cell<usize>>,
1098 _subscription: Subscription,
1099 }
1100
1101 impl TestComboboxEventCollector {
1102 fn new(
1103 state: &Entity<ComboboxState<SearchableVec<&'static str>>>,
1104 cx: &mut Context<Self>,
1105 ) -> Self {
1106 let event_count = Rc::new(Cell::new(0));
1107 let event_count_for_subscription = event_count.clone();
1108 let _subscription = cx.subscribe(
1109 state,
1110 move |_, _, _: &ComboboxEvent<SearchableVec<&'static str>>, _| {
1111 event_count_for_subscription.set(event_count_for_subscription.get() + 1);
1112 },
1113 );
1114
1115 Self {
1116 event_count,
1117 _subscription,
1118 }
1119 }
1120 }
1121
1122 #[gpui::test]
1123 fn test_combo_box_builder(cx: &mut TestAppContext) {
1124 cx.update(crate::init);
1125 let cx = cx.add_empty_window();
1126 cx.update(|window, cx| {
1127 let items = SearchableVec::new(vec!["Rust", "Go", "C++"]);
1128 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).searchable(true));
1129
1130 let _cb = Combobox::new(&state)
1131 .placeholder("Select language")
1132 .search_placeholder("Search...")
1133 .menu_width(gpui::px(300.))
1134 .menu_max_h(gpui::rems(15.))
1135 .cleanable(true)
1136 .disabled(false)
1137 .appearance(true);
1138 });
1139 }
1140
1141 #[gpui::test]
1142 fn test_combo_box_search_filters_items(cx: &mut TestAppContext) {
1143 cx.update(crate::init);
1144 let cx = cx.add_empty_window();
1145 cx.update(|window, cx| {
1146 let items = SearchableVec::new(vec!["Rust", "Go", "C++"]);
1147 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).searchable(true));
1148
1149 let count_before = state
1150 .read(cx)
1151 .state
1152 .list
1153 .read(cx)
1154 .delegate()
1155 .delegate
1156 .items_count(0);
1157 assert_eq!(count_before, 3);
1158
1159 state.update(cx, |s, cx| {
1160 s.state.list.update(cx, |list, cx| {
1161 let _ = list
1162 .delegate_mut()
1163 .delegate
1164 .perform_search("Rust", window, cx);
1165 });
1166 });
1167
1168 let count_after = state
1169 .read(cx)
1170 .state
1171 .list
1172 .read(cx)
1173 .delegate()
1174 .delegate
1175 .items_count(0);
1176 assert_eq!(count_after, 1);
1177 });
1178 }
1179
1180 #[gpui::test]
1181 fn test_combo_box_set_query_updates_text_and_filters_items(cx: &mut TestAppContext) {
1182 cx.update(crate::init);
1183 let cx = cx.add_empty_window();
1184 cx.update(|window, cx| {
1185 let items = SearchableVec::new(vec!["Rust", "Go", "C++"]);
1186 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).searchable(true));
1187
1188 state.update(cx, |state, cx| state.set_query(" Rust ", window, cx));
1189
1190 assert_eq!(state.read(cx).query(cx).as_ref(), " Rust ");
1191 assert_eq!(
1192 state
1193 .read(cx)
1194 .state
1195 .list
1196 .read(cx)
1197 .delegate()
1198 .delegate
1199 .items_count(0),
1200 1,
1201 );
1202 });
1203 }
1204
1205 #[gpui::test]
1206 fn test_multi_combo_box_builder(cx: &mut TestAppContext) {
1207 cx.update(crate::init);
1208 let cx = cx.add_empty_window();
1209 cx.update(|window, cx| {
1210 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1211 let state = cx.new(|cx| {
1212 ComboboxState::new(items, vec![IndexPath::new(0)], window, cx)
1213 .multiple(true)
1214 .searchable(true)
1215 });
1216
1217 let _cb = Combobox::new(&state)
1218 .placeholder("Select frameworks")
1219 .search_placeholder("Search...")
1220 .menu_width(gpui::px(300.))
1221 .cleanable(true)
1222 .disabled(false);
1223
1224 assert_eq!(state.read(cx).selected_values(), vec!["React"]);
1225 });
1226 }
1227
1228 #[gpui::test]
1229 fn test_combo_box_set_selected_values_uses_current_delegate(cx: &mut TestAppContext) {
1230 cx.update(crate::init);
1231 let cx = cx.add_empty_window();
1232 cx.update(|window, cx| {
1233 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1234 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true));
1235
1236 state.update(cx, |state, cx| {
1237 state.set_selected_values(&["Vue", "Missing"], window, cx);
1238
1239 assert_eq!(state.selected_values(), vec!["Vue"]);
1240 assert_eq!(
1241 state
1242 .selection()
1243 .iter()
1244 .map(|(index, _)| *index)
1245 .collect::<Vec<_>>(),
1246 vec![IndexPath::new(1)],
1247 );
1248 assert_eq!(
1249 state
1250 .state
1251 .list
1252 .read(cx)
1253 .delegate()
1254 .selection_snapshot
1255 .as_slice(),
1256 state.selection(),
1257 );
1258
1259 state.set_items(SearchableVec::new(vec!["Vue", "Rust", "Go"]), window, cx);
1260 state.set_selected_values(&["Go", "Vue"], window, cx);
1261
1262 assert_eq!(state.selected_values(), vec!["Go", "Vue"]);
1263 assert_eq!(
1264 state
1265 .selection()
1266 .iter()
1267 .map(|(index, _)| *index)
1268 .collect::<Vec<_>>(),
1269 vec![IndexPath::new(2), IndexPath::new(0)],
1270 );
1271 assert_eq!(
1272 state
1273 .state
1274 .list
1275 .read(cx)
1276 .delegate()
1277 .selection_snapshot
1278 .as_slice(),
1279 state.selection(),
1280 );
1281
1282 state.set_selected_values(&[], window, cx);
1283
1284 assert!(state.selection().is_empty());
1285 assert!(
1286 state
1287 .state
1288 .list
1289 .read(cx)
1290 .delegate()
1291 .selection_snapshot
1292 .is_empty()
1293 );
1294 });
1295 });
1296 }
1297
1298 #[gpui::test]
1301 fn test_combo_box_set_selected_values_keeps_values_hidden_by_the_search(
1302 cx: &mut TestAppContext,
1303 ) {
1304 cx.update(crate::init);
1305 let cx = cx.add_empty_window();
1306 cx.update(|window, cx| {
1307 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1308 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true));
1309
1310 state.update(cx, |state, cx| {
1311 state.set_selected_values(&["React", "Vue"], window, cx);
1312 state.state.list.update(cx, |list, cx| {
1313 list.set_query("Angular", window, cx);
1314 });
1315
1316 state.set_selected_values(&["React", "Vue", "Angular"], window, cx);
1317
1318 assert_eq!(
1319 state.selected_values(),
1320 vec!["React", "Vue", "Angular"],
1321 "a value the search had hidden must survive being reapplied"
1322 );
1323 assert_eq!(
1324 state
1325 .selection()
1326 .iter()
1327 .map(|(index, _)| *index)
1328 .collect::<Vec<_>>(),
1329 vec![IndexPath::new(0), IndexPath::new(1), IndexPath::new(2)],
1330 "clearing the query puts every selected item back in view"
1331 );
1332 });
1333 });
1334 }
1335
1336 #[gpui::test]
1337 fn test_combo_box_set_selected_values_does_not_emit_events(cx: &mut TestAppContext) {
1338 cx.update(crate::init);
1339 let cx = cx.add_empty_window();
1340 let state = cx.update(|window, cx| {
1341 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1342 cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true))
1343 });
1344 let collector = cx.update(|_, cx| cx.new(|cx| TestComboboxEventCollector::new(&state, cx)));
1345
1346 cx.update(|window, cx| {
1347 state.update(cx, |state, cx| {
1348 state.set_selected_values(&["React", "Vue"], window, cx);
1349 });
1350 });
1351
1352 cx.update(|_, cx| {
1353 assert_eq!(collector.read(cx).event_count.get(), 0);
1354 });
1355 }
1356
1357 #[gpui::test]
1358 fn test_combo_box_initial_selection_seeds_cursor(cx: &mut TestAppContext) {
1359 cx.update(crate::init);
1360 let cx = cx.add_empty_window();
1361 cx.update(|window, cx| {
1362 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1363 let state = cx.new(|cx| {
1364 ComboboxState::new(items, vec![IndexPath::new(1)], window, cx).multiple(true)
1365 });
1366
1367 let state_ref = state.read(cx);
1368 assert_eq!(
1369 state_ref.state.list.read(cx).selected_index(),
1370 Some(IndexPath::new(1)),
1371 "initial selected_indices should seed ListState.selected_index, not just the snapshot",
1372 );
1373 assert_eq!(state_ref.selected_values(), vec!["Vue"]);
1374 });
1375 }
1376
1377 #[gpui::test]
1378 fn test_multi_combo_box_toggle(cx: &mut TestAppContext) {
1379 cx.update(crate::init);
1380 let cx = cx.add_empty_window();
1381 cx.update(|window, cx| {
1382 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1383 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true));
1384
1385 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(0), cx));
1386 assert_eq!(state.read(cx).selected_values(), &["React"]);
1387
1388 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(1), cx));
1389 assert_eq!(state.read(cx).selected_values(), &["React", "Vue"]);
1390
1391 state.update(cx, |s, cx| s.remove_selected_index(IndexPath::new(0), cx));
1392 assert_eq!(state.read(cx).selected_values(), &["Vue"]);
1393 });
1394 }
1395
1396 #[gpui::test]
1397 fn test_multi_combo_box_search_selection_uses_value_identity(cx: &mut TestAppContext) {
1398 cx.update(crate::init);
1399 let cx = cx.add_empty_window();
1400 cx.update(|window, cx| {
1401 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1402 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true));
1403
1404 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(0), cx));
1405 assert_eq!(state.read(cx).selected_values(), &["React"]);
1406
1407 state.update(cx, |s, cx| {
1408 s.state.list.update(cx, |list, cx| {
1409 let _ = list
1410 .delegate_mut()
1411 .delegate
1412 .perform_search("Vue", window, cx);
1413 });
1414 });
1415
1416 state.read_with(cx, |s, cx| {
1417 let selection = s.state.selection.clone();
1418 let list = s.state.list.read(cx);
1419 let delegate = &list.delegate().delegate;
1420 let ix = IndexPath::new(0);
1421 let item = delegate.item(ix).expect("filtered item exists");
1422
1423 assert_eq!(item.value(), &"Vue");
1424 assert!(
1425 !delegate.is_item_checked(ix, item, &selection, cx),
1426 "filtered row 0 should not inherit React's checked state",
1427 );
1428 });
1429
1430 state.update(cx, |s, cx| {
1431 s.handle_item_select(IndexPath::new(0), window, cx);
1432 });
1433 assert_eq!(state.read(cx).selected_values(), &["React", "Vue"]);
1434 });
1435 }
1436
1437 #[gpui::test]
1438 fn test_multi_combo_box_search_deselects_by_value(cx: &mut TestAppContext) {
1439 cx.update(crate::init);
1440 let cx = cx.add_empty_window();
1441 cx.update(|window, cx| {
1442 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1443 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true));
1444
1445 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(0), cx));
1446
1447 state.update(cx, |s, cx| {
1448 s.state.list.update(cx, |list, cx| {
1449 let _ = list
1450 .delegate_mut()
1451 .delegate
1452 .perform_search("React", window, cx);
1453 });
1454 });
1455
1456 state.update(cx, |s, cx| {
1457 s.handle_item_select(IndexPath::new(0), window, cx);
1458 });
1459 assert!(state.read(cx).selected_values().is_empty());
1460 });
1461 }
1462
1463 #[gpui::test]
1464 fn test_searchable_list_default_change_uses_value_identity(cx: &mut TestAppContext) {
1465 cx.update(crate::init);
1466 let cx = cx.add_empty_window();
1467 cx.update(|window, cx| {
1468 let mut delegate = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1469 let mut selection = vec![(IndexPath::new(1), "Vue")];
1470
1471 let _ = delegate.perform_search("Vue", window, cx);
1472 delegate.on_will_change(
1473 &mut selection,
1474 &[SearchableListChange::Deselect {
1475 index: IndexPath::new(0),
1476 }],
1477 );
1478 assert!(selection.is_empty());
1479
1480 delegate.on_will_change(
1481 &mut selection,
1482 &[SearchableListChange::Select {
1483 index: IndexPath::new(0),
1484 }],
1485 );
1486 assert_eq!(selection, vec![(IndexPath::new(0), "Vue")]);
1487 });
1488 }
1489
1490 #[gpui::test]
1491 fn test_multi_combo_box_clear(cx: &mut TestAppContext) {
1492 cx.update(crate::init);
1493 let cx = cx.add_empty_window();
1494 cx.update(|window, cx| {
1495 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1496 let state = cx.new(|cx| {
1497 ComboboxState::new(
1498 items,
1499 vec![IndexPath::new(0), IndexPath::new(1)],
1500 window,
1501 cx,
1502 )
1503 .multiple(true)
1504 });
1505
1506 assert_eq!(state.read(cx).selected_values().len(), 2);
1507 state.update(cx, |s, cx| s.clear_selection(cx));
1508 assert!(state.read(cx).selected_values().is_empty());
1509 });
1510 }
1511
1512 #[gpui::test]
1513 fn test_single_combo_box_mode(cx: &mut TestAppContext) {
1514 cx.update(crate::init);
1515 let cx = cx.add_empty_window();
1516 cx.update(|window, cx| {
1517 let items = SearchableVec::new(vec!["Rust", "Go", "C++"]);
1518 let state = cx.new(|cx| ComboboxState::new(items, vec![], window, cx));
1519
1520 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(0), cx));
1522 assert_eq!(state.read(cx).selected_values(), &["Rust"]);
1523
1524 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(1), cx));
1525 assert_eq!(state.read(cx).selected_values(), &["Rust", "Go"]);
1526 });
1527 }
1528
1529 struct VetoDelegate(SearchableVec<&'static str>);
1531
1532 impl SearchableListDelegate for VetoDelegate {
1533 type Item = &'static str;
1534
1535 fn items_count(&self, section: usize) -> usize {
1536 self.0.items_count(section)
1537 }
1538
1539 fn item(&self, ix: IndexPath) -> Option<&&'static str> {
1540 self.0.item(ix)
1541 }
1542
1543 fn position<V>(&self, value: &V) -> Option<IndexPath>
1544 where
1545 &'static str: SearchableListItem<Value = V>,
1546 V: PartialEq,
1547 {
1548 self.0.position(value)
1549 }
1550
1551 fn on_will_change(
1552 &mut self,
1553 _selection: &mut Vec<(IndexPath, &'static str)>,
1554 _changes: &[SearchableListChange],
1555 ) {
1556 }
1558 }
1559
1560 #[gpui::test]
1561 fn test_on_will_change_veto(cx: &mut TestAppContext) {
1562 cx.update(crate::init);
1563 let cx = cx.add_empty_window();
1564 cx.update(|window, cx| {
1565 let delegate = VetoDelegate(SearchableVec::new(vec!["Rust", "Go", "C++"]));
1566 let state = cx.new(|cx| ComboboxState::new(delegate, vec![], window, cx));
1567
1568 state.update(cx, |s, cx| s.add_selected_index(IndexPath::new(0), cx));
1570 assert_eq!(state.read(cx).selected_values(), &["Rust"]);
1571
1572 state.update(cx, |s, cx| {
1574 s.handle_item_select(IndexPath::new(1), window, cx);
1575 });
1576
1577 assert_eq!(state.read(cx).selected_values(), &["Rust"]);
1579 });
1580 }
1581
1582 fn left_press(position: Point<Pixels>) -> MouseDownEvent {
1583 MouseDownEvent {
1584 button: MouseButton::Left,
1585 position,
1586 modifiers: Modifiers::default(),
1587 click_count: 1,
1588 first_mouse: false,
1589 }
1590 }
1591
1592 #[gpui::test]
1593 fn test_combo_box_dismiss_ignores_press_on_trigger(cx: &mut TestAppContext) {
1594 cx.update(crate::init);
1595 let cx = cx.add_empty_window();
1596 let state = cx.update(|window, cx| {
1597 let items = SearchableVec::new(vec!["React", "Vue", "Angular"]);
1598 cx.new(|cx| ComboboxState::new(items, vec![], window, cx).multiple(true))
1599 });
1600 let collector = cx.update(|_, cx| cx.new(|cx| TestComboboxEventCollector::new(&state, cx)));
1601
1602 cx.update(|window, cx| {
1603 state.update(cx, |state, cx| {
1604 state.state.bounds = Bounds {
1605 origin: point(px(10.), px(10.)),
1606 size: size(px(200.), px(32.)),
1607 };
1608 state.set_open(true, cx);
1609 state.dismiss(&left_press(point(px(20.), px(20.))), window, cx);
1610 });
1611 });
1612
1613 cx.update(|_, cx| {
1614 assert!(
1615 state.read(cx).state.open,
1616 "a press on the trigger must reach nested controls, so the menu stays open \
1617 and `toggle_menu` closes it on release instead",
1618 );
1619 assert_eq!(collector.read(cx).event_count.get(), 0);
1620 });
1621
1622 cx.update(|window, cx| {
1623 state.update(cx, |state, cx| {
1624 state.dismiss(&left_press(point(px(20.), px(200.))), window, cx);
1625 });
1626 });
1627
1628 cx.update(|_, cx| {
1629 assert!(!state.read(cx).state.open);
1630 assert_eq!(
1631 collector.read(cx).event_count.get(),
1632 1,
1633 "dismissing from outside the trigger emits Confirm",
1634 );
1635 });
1636 }
1637
1638 #[allow(unused)]
1640 fn _uses_state<D: SearchableListDelegate + 'static>(_: &SearchableListState<D>)
1641 where
1642 <D::Item as SearchableListItem>::Value: PartialEq + Clone,
1643 {
1644 }
1645}