picker.rs

  1mod head;
  2pub mod highlighted_match_with_paths;
  3pub mod popover_menu;
  4
  5use anyhow::Result;
  6use editor::{
  7    Editor, SelectionEffects,
  8    actions::{MoveDown, MoveUp},
  9    scroll::Autoscroll,
 10};
 11use gpui::{
 12    Action, AnyElement, App, ClickEvent, Context, DismissEvent, Entity, EventEmitter, FocusHandle,
 13    Focusable, Length, ListSizingBehavior, ListState, MouseButton, MouseUpEvent, Render,
 14    ScrollStrategy, Task, UniformListScrollHandle, Window, actions, div, list, prelude::*,
 15    uniform_list,
 16};
 17use head::Head;
 18use schemars::JsonSchema;
 19use serde::Deserialize;
 20use std::{ops::Range, sync::Arc, time::Duration};
 21use theme::ThemeSettings;
 22use ui::{
 23    Color, Divider, DocumentationAside, DocumentationEdge, DocumentationSide, Label, ListItem,
 24    ListItemSpacing, ScrollAxes, Scrollbars, WithScrollbar, prelude::*, utils::WithRemSize, v_flex,
 25};
 26use workspace::{ModalView, item::Settings};
 27
 28enum ElementContainer {
 29    List(ListState),
 30    UniformList(UniformListScrollHandle),
 31}
 32
 33pub enum Direction {
 34    Up,
 35    Down,
 36}
 37
 38actions!(
 39    picker,
 40    [
 41        /// Confirms the selected completion in the picker.
 42        ConfirmCompletion
 43    ]
 44);
 45
 46/// ConfirmInput is an alternative editor action which - instead of selecting active picker entry - treats pickers editor input literally,
 47/// performing some kind of action on it.
 48#[derive(Clone, PartialEq, Deserialize, JsonSchema, Default, Action)]
 49#[action(namespace = picker)]
 50#[serde(deny_unknown_fields)]
 51pub struct ConfirmInput {
 52    pub secondary: bool,
 53}
 54
 55struct PendingUpdateMatches {
 56    delegate_update_matches: Option<Task<()>>,
 57    _task: Task<Result<()>>,
 58}
 59
 60pub struct Picker<D: PickerDelegate> {
 61    pub delegate: D,
 62    element_container: ElementContainer,
 63    head: Head,
 64    pending_update_matches: Option<PendingUpdateMatches>,
 65    confirm_on_update: Option<bool>,
 66    width: Option<Length>,
 67    widest_item: Option<usize>,
 68    max_height: Option<Length>,
 69    /// An external control to display a scrollbar in the `Picker`.
 70    show_scrollbar: bool,
 71    /// Whether the `Picker` is rendered as a self-contained modal.
 72    ///
 73    /// Set this to `false` when rendering the `Picker` as part of a larger modal.
 74    is_modal: bool,
 75}
 76
 77#[derive(Debug, Default, Clone, Copy, PartialEq)]
 78pub enum PickerEditorPosition {
 79    #[default]
 80    /// Render the editor at the start of the picker. Usually the top
 81    Start,
 82    /// Render the editor at the end of the picker. Usually the bottom
 83    End,
 84}
 85
 86pub trait PickerDelegate: Sized + 'static {
 87    type ListItem: IntoElement;
 88
 89    fn match_count(&self) -> usize;
 90    fn selected_index(&self) -> usize;
 91    fn separators_after_indices(&self) -> Vec<usize> {
 92        Vec::new()
 93    }
 94    fn set_selected_index(
 95        &mut self,
 96        ix: usize,
 97        window: &mut Window,
 98        cx: &mut Context<Picker<Self>>,
 99    );
100
101    /// Called before the picker handles `SelectPrevious` or `SelectNext`. Return `Some(query)` to
102    /// set a new query and prevent the default selection behavior.
103    fn select_history(
104        &mut self,
105        _direction: Direction,
106        _query: &str,
107        _window: &mut Window,
108        _cx: &mut App,
109    ) -> Option<String> {
110        None
111    }
112    fn can_select(
113        &mut self,
114        _ix: usize,
115        _window: &mut Window,
116        _cx: &mut Context<Picker<Self>>,
117    ) -> bool {
118        true
119    }
120
121    // Allows binding some optional effect to when the selection changes.
122    fn selected_index_changed(
123        &self,
124        _ix: usize,
125        _window: &mut Window,
126        _cx: &mut Context<Picker<Self>>,
127    ) -> Option<Box<dyn Fn(&mut Window, &mut App) + 'static>> {
128        None
129    }
130    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str>;
131    fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> Option<SharedString> {
132        Some("No matches".into())
133    }
134    fn update_matches(
135        &mut self,
136        query: String,
137        window: &mut Window,
138        cx: &mut Context<Picker<Self>>,
139    ) -> Task<()>;
140
141    // Delegates that support this method (e.g. the CommandPalette) can chose to block on any background
142    // work for up to `duration` to try and get a result synchronously.
143    // This avoids a flash of an empty command-palette on cmd-shift-p, and lets workspace::SendKeystrokes
144    // mostly work when dismissing a palette.
145    fn finalize_update_matches(
146        &mut self,
147        _query: String,
148        _duration: Duration,
149        _window: &mut Window,
150        _cx: &mut Context<Picker<Self>>,
151    ) -> bool {
152        false
153    }
154
155    /// Override if you want to have <enter> update the query instead of confirming.
156    fn confirm_update_query(
157        &mut self,
158        _window: &mut Window,
159        _cx: &mut Context<Picker<Self>>,
160    ) -> Option<String> {
161        None
162    }
163    fn confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>);
164    /// Instead of interacting with currently selected entry, treats editor input literally,
165    /// performing some kind of action on it.
166    fn confirm_input(
167        &mut self,
168        _secondary: bool,
169        _window: &mut Window,
170        _: &mut Context<Picker<Self>>,
171    ) {
172    }
173    fn dismissed(&mut self, window: &mut Window, cx: &mut Context<Picker<Self>>);
174    fn should_dismiss(&self) -> bool {
175        true
176    }
177    fn confirm_completion(
178        &mut self,
179        _query: String,
180        _window: &mut Window,
181        _: &mut Context<Picker<Self>>,
182    ) -> Option<String> {
183        None
184    }
185
186    fn editor_position(&self) -> PickerEditorPosition {
187        PickerEditorPosition::default()
188    }
189
190    fn render_editor(
191        &self,
192        editor: &Entity<Editor>,
193        _window: &mut Window,
194        _cx: &mut Context<Picker<Self>>,
195    ) -> Div {
196        v_flex()
197            .when(
198                self.editor_position() == PickerEditorPosition::End,
199                |this| this.child(Divider::horizontal()),
200            )
201            .child(
202                h_flex()
203                    .overflow_hidden()
204                    .flex_none()
205                    .h_9()
206                    .px_2p5()
207                    .child(editor.clone()),
208            )
209            .when(
210                self.editor_position() == PickerEditorPosition::Start,
211                |this| this.child(Divider::horizontal()),
212            )
213    }
214
215    fn render_match(
216        &self,
217        ix: usize,
218        selected: bool,
219        window: &mut Window,
220        cx: &mut Context<Picker<Self>>,
221    ) -> Option<Self::ListItem>;
222
223    fn render_header(
224        &self,
225        _window: &mut Window,
226        _: &mut Context<Picker<Self>>,
227    ) -> Option<AnyElement> {
228        None
229    }
230
231    fn render_footer(
232        &self,
233        _window: &mut Window,
234        _: &mut Context<Picker<Self>>,
235    ) -> Option<AnyElement> {
236        None
237    }
238
239    fn documentation_aside(
240        &self,
241        _window: &mut Window,
242        _cx: &mut Context<Picker<Self>>,
243    ) -> Option<DocumentationAside> {
244        None
245    }
246}
247
248impl<D: PickerDelegate> Focusable for Picker<D> {
249    fn focus_handle(&self, cx: &App) -> FocusHandle {
250        match &self.head {
251            Head::Editor(editor) => editor.focus_handle(cx),
252            Head::Empty(head) => head.focus_handle(cx),
253        }
254    }
255}
256
257#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
258enum ContainerKind {
259    List,
260    UniformList,
261}
262
263impl<D: PickerDelegate> Picker<D> {
264    /// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height.
265    /// The picker allows the user to perform search items by text.
266    /// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`.
267    pub fn uniform_list(delegate: D, window: &mut Window, cx: &mut Context<Self>) -> Self {
268        let head = Head::editor(
269            delegate.placeholder_text(window, cx),
270            Self::on_input_editor_event,
271            window,
272            cx,
273        );
274
275        Self::new(delegate, ContainerKind::UniformList, head, window, cx)
276    }
277
278    /// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height.
279    /// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`.
280    pub fn nonsearchable_uniform_list(
281        delegate: D,
282        window: &mut Window,
283        cx: &mut Context<Self>,
284    ) -> Self {
285        let head = Head::empty(Self::on_empty_head_blur, window, cx);
286
287        Self::new(delegate, ContainerKind::UniformList, head, window, cx)
288    }
289
290    /// A picker, which displays its matches using `gpui::list`, matches can have different heights.
291    /// The picker allows the user to perform search items by text.
292    /// If `PickerDelegate::render_match` only returns items with the same height, use `Picker::uniform_list` as its implementation is optimized for that.
293    pub fn nonsearchable_list(delegate: D, window: &mut Window, cx: &mut Context<Self>) -> Self {
294        let head = Head::empty(Self::on_empty_head_blur, window, cx);
295
296        Self::new(delegate, ContainerKind::List, head, window, cx)
297    }
298
299    /// A picker, which displays its matches using `gpui::list`, matches can have different heights.
300    /// The picker allows the user to perform search items by text.
301    /// If `PickerDelegate::render_match` only returns items with the same height, use `Picker::uniform_list` as its implementation is optimized for that.
302    pub fn list(delegate: D, window: &mut Window, cx: &mut Context<Self>) -> Self {
303        let head = Head::editor(
304            delegate.placeholder_text(window, cx),
305            Self::on_input_editor_event,
306            window,
307            cx,
308        );
309
310        Self::new(delegate, ContainerKind::List, head, window, cx)
311    }
312
313    fn new(
314        delegate: D,
315        container: ContainerKind,
316        head: Head,
317        window: &mut Window,
318        cx: &mut Context<Self>,
319    ) -> Self {
320        let element_container = Self::create_element_container(container);
321        let mut this = Self {
322            delegate,
323            head,
324            element_container,
325            pending_update_matches: None,
326            confirm_on_update: None,
327            width: None,
328            widest_item: None,
329            max_height: Some(rems(24.).into()),
330            show_scrollbar: false,
331            is_modal: true,
332        };
333        this.update_matches("".to_string(), window, cx);
334        // give the delegate 4ms to render the first set of suggestions.
335        this.delegate
336            .finalize_update_matches("".to_string(), Duration::from_millis(4), window, cx);
337        this
338    }
339
340    fn create_element_container(container: ContainerKind) -> ElementContainer {
341        match container {
342            ContainerKind::UniformList => {
343                ElementContainer::UniformList(UniformListScrollHandle::new())
344            }
345            ContainerKind::List => {
346                ElementContainer::List(ListState::new(0, gpui::ListAlignment::Top, px(1000.)))
347            }
348        }
349    }
350
351    pub fn width(mut self, width: impl Into<gpui::Length>) -> Self {
352        self.width = Some(width.into());
353        self
354    }
355
356    pub fn widest_item(mut self, ix: Option<usize>) -> Self {
357        self.widest_item = ix;
358        self
359    }
360
361    pub fn max_height(mut self, max_height: Option<gpui::Length>) -> Self {
362        self.max_height = max_height;
363        self
364    }
365
366    pub fn show_scrollbar(mut self, show_scrollbar: bool) -> Self {
367        self.show_scrollbar = show_scrollbar;
368        self
369    }
370
371    pub fn modal(mut self, modal: bool) -> Self {
372        self.is_modal = modal;
373        self
374    }
375
376    pub fn list_measure_all(mut self) -> Self {
377        match self.element_container {
378            ElementContainer::List(state) => {
379                self.element_container = ElementContainer::List(state.measure_all());
380            }
381            _ => {}
382        }
383        self
384    }
385
386    pub fn focus(&self, window: &mut Window, cx: &mut App) {
387        self.focus_handle(cx).focus(window);
388    }
389
390    /// Handles the selecting an index, and passing the change to the delegate.
391    /// If `fallback_direction` is set to `None`, the index will not be selected
392    /// if the element at that index cannot be selected.
393    /// If `fallback_direction` is set to
394    /// `Some(..)`, the next selectable element will be selected in the
395    /// specified direction (Down or Up), cycling through all elements until
396    /// finding one that can be selected or returning if there are no selectable elements.
397    /// If `scroll_to_index` is true, the new selected index will be scrolled into
398    /// view.
399    ///
400    /// If some effect is bound to `selected_index_changed`, it will be executed.
401    pub fn set_selected_index(
402        &mut self,
403        mut ix: usize,
404        fallback_direction: Option<Direction>,
405        scroll_to_index: bool,
406        window: &mut Window,
407        cx: &mut Context<Self>,
408    ) {
409        let match_count = self.delegate.match_count();
410        if match_count == 0 {
411            return;
412        }
413
414        if let Some(bias) = fallback_direction {
415            let mut curr_ix = ix;
416            while !self.delegate.can_select(curr_ix, window, cx) {
417                curr_ix = match bias {
418                    Direction::Down => {
419                        if curr_ix == match_count - 1 {
420                            0
421                        } else {
422                            curr_ix + 1
423                        }
424                    }
425                    Direction::Up => {
426                        if curr_ix == 0 {
427                            match_count - 1
428                        } else {
429                            curr_ix - 1
430                        }
431                    }
432                };
433                // There is no item that can be selected
434                if ix == curr_ix {
435                    return;
436                }
437            }
438            ix = curr_ix;
439        } else if !self.delegate.can_select(ix, window, cx) {
440            return;
441        }
442
443        let previous_index = self.delegate.selected_index();
444        self.delegate.set_selected_index(ix, window, cx);
445        let current_index = self.delegate.selected_index();
446
447        if previous_index != current_index {
448            if let Some(action) = self.delegate.selected_index_changed(ix, window, cx) {
449                action(window, cx);
450            }
451            if scroll_to_index {
452                self.scroll_to_item_index(ix);
453            }
454        }
455    }
456
457    pub fn select_next(
458        &mut self,
459        _: &menu::SelectNext,
460        window: &mut Window,
461        cx: &mut Context<Self>,
462    ) {
463        let query = self.query(cx);
464        if let Some(query) = self
465            .delegate
466            .select_history(Direction::Down, &query, window, cx)
467        {
468            self.set_query(query, window, cx);
469            return;
470        }
471        let count = self.delegate.match_count();
472        if count > 0 {
473            let index = self.delegate.selected_index();
474            let ix = if index == count - 1 { 0 } else { index + 1 };
475            self.set_selected_index(ix, Some(Direction::Down), true, window, cx);
476            cx.notify();
477        }
478    }
479
480    pub fn editor_move_up(&mut self, _: &MoveUp, window: &mut Window, cx: &mut Context<Self>) {
481        self.select_previous(&Default::default(), window, cx);
482    }
483
484    fn select_previous(
485        &mut self,
486        _: &menu::SelectPrevious,
487        window: &mut Window,
488        cx: &mut Context<Self>,
489    ) {
490        let query = self.query(cx);
491        if let Some(query) = self
492            .delegate
493            .select_history(Direction::Up, &query, window, cx)
494        {
495            self.set_query(query, window, cx);
496            return;
497        }
498        let count = self.delegate.match_count();
499        if count > 0 {
500            let index = self.delegate.selected_index();
501            let ix = if index == 0 { count - 1 } else { index - 1 };
502            self.set_selected_index(ix, Some(Direction::Up), true, window, cx);
503            cx.notify();
504        }
505    }
506
507    pub fn editor_move_down(&mut self, _: &MoveDown, window: &mut Window, cx: &mut Context<Self>) {
508        self.select_next(&Default::default(), window, cx);
509    }
510
511    pub fn select_first(
512        &mut self,
513        _: &menu::SelectFirst,
514        window: &mut Window,
515        cx: &mut Context<Self>,
516    ) {
517        let count = self.delegate.match_count();
518        if count > 0 {
519            self.set_selected_index(0, Some(Direction::Down), true, window, cx);
520            cx.notify();
521        }
522    }
523
524    fn select_last(&mut self, _: &menu::SelectLast, window: &mut Window, cx: &mut Context<Self>) {
525        let count = self.delegate.match_count();
526        if count > 0 {
527            self.set_selected_index(count - 1, Some(Direction::Up), true, window, cx);
528            cx.notify();
529        }
530    }
531
532    pub fn cycle_selection(&mut self, window: &mut Window, cx: &mut Context<Self>) {
533        let count = self.delegate.match_count();
534        let index = self.delegate.selected_index();
535        let new_index = if index + 1 == count { 0 } else { index + 1 };
536        self.set_selected_index(new_index, Some(Direction::Down), true, window, cx);
537        cx.notify();
538    }
539
540    pub fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {
541        if self.delegate.should_dismiss() {
542            self.delegate.dismissed(window, cx);
543            cx.emit(DismissEvent);
544        }
545    }
546
547    fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
548        if self.pending_update_matches.is_some()
549            && !self.delegate.finalize_update_matches(
550                self.query(cx),
551                Duration::from_millis(16),
552                window,
553                cx,
554            )
555        {
556            self.confirm_on_update = Some(false)
557        } else {
558            self.pending_update_matches.take();
559            self.do_confirm(false, window, cx);
560        }
561    }
562
563    fn secondary_confirm(
564        &mut self,
565        _: &menu::SecondaryConfirm,
566        window: &mut Window,
567        cx: &mut Context<Self>,
568    ) {
569        if self.pending_update_matches.is_some()
570            && !self.delegate.finalize_update_matches(
571                self.query(cx),
572                Duration::from_millis(16),
573                window,
574                cx,
575            )
576        {
577            self.confirm_on_update = Some(true)
578        } else {
579            self.do_confirm(true, window, cx);
580        }
581    }
582
583    fn confirm_input(&mut self, input: &ConfirmInput, window: &mut Window, cx: &mut Context<Self>) {
584        self.delegate.confirm_input(input.secondary, window, cx);
585    }
586
587    fn confirm_completion(
588        &mut self,
589        _: &ConfirmCompletion,
590        window: &mut Window,
591        cx: &mut Context<Self>,
592    ) {
593        if let Some(new_query) = self.delegate.confirm_completion(self.query(cx), window, cx) {
594            self.set_query(new_query, window, cx);
595        } else {
596            cx.propagate()
597        }
598    }
599
600    fn handle_click(
601        &mut self,
602        ix: usize,
603        secondary: bool,
604        window: &mut Window,
605        cx: &mut Context<Self>,
606    ) {
607        cx.stop_propagation();
608        window.prevent_default();
609        self.set_selected_index(ix, None, false, window, cx);
610        self.do_confirm(secondary, window, cx)
611    }
612
613    fn do_confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<Self>) {
614        if let Some(update_query) = self.delegate.confirm_update_query(window, cx) {
615            self.set_query(update_query, window, cx);
616            self.set_selected_index(0, Some(Direction::Down), false, window, cx);
617        } else {
618            self.delegate.confirm(secondary, window, cx)
619        }
620    }
621
622    fn on_input_editor_event(
623        &mut self,
624        _: &Entity<Editor>,
625        event: &editor::EditorEvent,
626        window: &mut Window,
627        cx: &mut Context<Self>,
628    ) {
629        let Head::Editor(editor) = &self.head else {
630            panic!("unexpected call");
631        };
632        match event {
633            editor::EditorEvent::BufferEdited => {
634                let query = editor.read(cx).text(cx);
635                self.update_matches(query, window, cx);
636            }
637            editor::EditorEvent::Blurred => {
638                if self.is_modal && window.is_window_active() {
639                    self.cancel(&menu::Cancel, window, cx);
640                }
641            }
642            _ => {}
643        }
644    }
645
646    fn on_empty_head_blur(&mut self, window: &mut Window, cx: &mut Context<Self>) {
647        let Head::Empty(_) = &self.head else {
648            panic!("unexpected call");
649        };
650        if window.is_window_active() {
651            self.cancel(&menu::Cancel, window, cx);
652        }
653    }
654
655    pub fn refresh_placeholder(&mut self, window: &mut Window, cx: &mut App) {
656        match &self.head {
657            Head::Editor(editor) => {
658                let placeholder = self.delegate.placeholder_text(window, cx);
659                editor.update(cx, |editor, cx| {
660                    editor.set_placeholder_text(placeholder.as_ref(), window, cx);
661                    cx.notify();
662                });
663            }
664            Head::Empty(_) => {}
665        }
666    }
667
668    pub fn refresh(&mut self, window: &mut Window, cx: &mut Context<Self>) {
669        let query = self.query(cx);
670        self.update_matches(query, window, cx);
671    }
672
673    pub fn update_matches(&mut self, query: String, window: &mut Window, cx: &mut Context<Self>) {
674        let delegate_pending_update_matches = self.delegate.update_matches(query, window, cx);
675
676        self.matches_updated(window, cx);
677        // This struct ensures that we can synchronously drop the task returned by the
678        // delegate's `update_matches` method and the task that the picker is spawning.
679        // If we simply capture the delegate's task into the picker's task, when the picker's
680        // task gets synchronously dropped, the delegate's task would keep running until
681        // the picker's task has a chance of being scheduled, because dropping a task happens
682        // asynchronously.
683        self.pending_update_matches = Some(PendingUpdateMatches {
684            delegate_update_matches: Some(delegate_pending_update_matches),
685            _task: cx.spawn_in(window, async move |this, cx| {
686                let delegate_pending_update_matches = this.update(cx, |this, _| {
687                    this.pending_update_matches
688                        .as_mut()
689                        .unwrap()
690                        .delegate_update_matches
691                        .take()
692                        .unwrap()
693                })?;
694                delegate_pending_update_matches.await;
695                this.update_in(cx, |this, window, cx| {
696                    this.matches_updated(window, cx);
697                })
698            }),
699        });
700    }
701
702    fn matches_updated(&mut self, window: &mut Window, cx: &mut Context<Self>) {
703        if let ElementContainer::List(state) = &mut self.element_container {
704            state.reset(self.delegate.match_count());
705        }
706
707        let index = self.delegate.selected_index();
708        self.scroll_to_item_index(index);
709        self.pending_update_matches = None;
710        if let Some(secondary) = self.confirm_on_update.take() {
711            self.do_confirm(secondary, window, cx);
712        }
713        cx.notify();
714    }
715
716    pub fn query(&self, cx: &App) -> String {
717        match &self.head {
718            Head::Editor(editor) => editor.read(cx).text(cx),
719            Head::Empty(_) => "".to_string(),
720        }
721    }
722
723    pub fn set_query(&self, query: impl Into<Arc<str>>, window: &mut Window, cx: &mut App) {
724        if let Head::Editor(editor) = &self.head {
725            editor.update(cx, |editor, cx| {
726                editor.set_text(query, window, cx);
727                let editor_offset = editor.buffer().read(cx).len(cx);
728                editor.change_selections(
729                    SelectionEffects::scroll(Autoscroll::Next),
730                    window,
731                    cx,
732                    |s| s.select_ranges(Some(editor_offset..editor_offset)),
733                );
734            });
735        }
736    }
737
738    fn scroll_to_item_index(&mut self, ix: usize) {
739        match &mut self.element_container {
740            ElementContainer::List(state) => state.scroll_to_reveal_item(ix),
741            ElementContainer::UniformList(scroll_handle) => {
742                scroll_handle.scroll_to_item(ix, ScrollStrategy::Nearest)
743            }
744        }
745    }
746
747    fn render_element(
748        &self,
749        window: &mut Window,
750        cx: &mut Context<Self>,
751        ix: usize,
752    ) -> impl IntoElement + use<D> {
753        div()
754            .id(("item", ix))
755            .cursor_pointer()
756            .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| {
757                this.handle_click(ix, event.modifiers().secondary(), window, cx)
758            }))
759            // As of this writing, GPUI intercepts `ctrl-[mouse-event]`s on macOS
760            // and produces right mouse button events. This matches platforms norms
761            // but means that UIs which depend on holding ctrl down (such as the tab
762            // switcher) can't be clicked on. Hence, this handler.
763            .on_mouse_up(
764                MouseButton::Right,
765                cx.listener(move |this, event: &MouseUpEvent, window, cx| {
766                    // We specifically want to use the platform key here, as
767                    // ctrl will already be held down for the tab switcher.
768                    this.handle_click(ix, event.modifiers.platform, window, cx)
769                }),
770            )
771            .children(self.delegate.render_match(
772                ix,
773                ix == self.delegate.selected_index(),
774                window,
775                cx,
776            ))
777            .when(
778                self.delegate.separators_after_indices().contains(&ix),
779                |picker| {
780                    picker
781                        .border_color(cx.theme().colors().border_variant)
782                        .border_b_1()
783                        .py(px(-1.0))
784                },
785            )
786    }
787
788    fn render_element_container(&self, cx: &mut Context<Self>) -> impl IntoElement {
789        let sizing_behavior = if self.max_height.is_some() {
790            ListSizingBehavior::Infer
791        } else {
792            ListSizingBehavior::Auto
793        };
794
795        match &self.element_container {
796            ElementContainer::UniformList(scroll_handle) => uniform_list(
797                "candidates",
798                self.delegate.match_count(),
799                cx.processor(move |picker, visible_range: Range<usize>, window, cx| {
800                    visible_range
801                        .map(|ix| picker.render_element(window, cx, ix))
802                        .collect()
803                }),
804            )
805            .with_sizing_behavior(sizing_behavior)
806            .when_some(self.widest_item, |el, widest_item| {
807                el.with_width_from_item(Some(widest_item))
808            })
809            .flex_grow()
810            .py_1()
811            .track_scroll(&scroll_handle)
812            .into_any_element(),
813            ElementContainer::List(state) => list(
814                state.clone(),
815                cx.processor(|this, ix, window, cx| {
816                    this.render_element(window, cx, ix).into_any_element()
817                }),
818            )
819            .with_sizing_behavior(sizing_behavior)
820            .flex_grow()
821            .py_2()
822            .into_any_element(),
823        }
824    }
825
826    #[cfg(any(test, feature = "test-support"))]
827    pub fn logical_scroll_top_index(&self) -> usize {
828        match &self.element_container {
829            ElementContainer::List(state) => state.logical_scroll_top().item_ix,
830            ElementContainer::UniformList(scroll_handle) => {
831                scroll_handle.logical_scroll_top_index()
832            }
833        }
834    }
835}
836
837impl<D: PickerDelegate> EventEmitter<DismissEvent> for Picker<D> {}
838impl<D: PickerDelegate> ModalView for Picker<D> {}
839
840impl<D: PickerDelegate> Render for Picker<D> {
841    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
842        let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
843        let window_size = window.viewport_size();
844        let rem_size = window.rem_size();
845        let is_wide_window = window_size.width / rem_size > rems_from_px(800.).0;
846
847        let aside = self.delegate.documentation_aside(window, cx);
848
849        let editor_position = self.delegate.editor_position();
850        let menu = v_flex()
851            .key_context("Picker")
852            .size_full()
853            .when_some(self.width, |el, width| el.w(width))
854            .overflow_hidden()
855            // This is a bit of a hack to remove the modal styling when we're rendering the `Picker`
856            // as a part of a modal rather than the entire modal.
857            //
858            // We should revisit how the `Picker` is styled to make it more composable.
859            .when(self.is_modal, |this| this.elevation_3(cx))
860            .on_action(cx.listener(Self::select_next))
861            .on_action(cx.listener(Self::select_previous))
862            .on_action(cx.listener(Self::editor_move_down))
863            .on_action(cx.listener(Self::editor_move_up))
864            .on_action(cx.listener(Self::select_first))
865            .on_action(cx.listener(Self::select_last))
866            .on_action(cx.listener(Self::cancel))
867            .on_action(cx.listener(Self::confirm))
868            .on_action(cx.listener(Self::secondary_confirm))
869            .on_action(cx.listener(Self::confirm_completion))
870            .on_action(cx.listener(Self::confirm_input))
871            .children(match &self.head {
872                Head::Editor(editor) => {
873                    if editor_position == PickerEditorPosition::Start {
874                        Some(self.delegate.render_editor(&editor.clone(), window, cx))
875                    } else {
876                        None
877                    }
878                }
879                Head::Empty(empty_head) => Some(div().child(empty_head.clone())),
880            })
881            .when(self.delegate.match_count() > 0, |el| {
882                el.child(
883                    v_flex()
884                        .id("element-container")
885                        .relative()
886                        .flex_grow()
887                        .when_some(self.max_height, |div, max_h| div.max_h(max_h))
888                        .overflow_hidden()
889                        .children(self.delegate.render_header(window, cx))
890                        .child(self.render_element_container(cx))
891                        .when(self.show_scrollbar, |this| {
892                            let base_scrollbar_config =
893                                Scrollbars::new(ScrollAxes::Vertical).width_sm();
894
895                            this.map(|this| match &self.element_container {
896                                ElementContainer::List(state) => this.custom_scrollbars(
897                                    base_scrollbar_config.tracked_scroll_handle(state),
898                                    window,
899                                    cx,
900                                ),
901                                ElementContainer::UniformList(state) => this.custom_scrollbars(
902                                    base_scrollbar_config.tracked_scroll_handle(state),
903                                    window,
904                                    cx,
905                                ),
906                            })
907                        }),
908                )
909            })
910            .when(self.delegate.match_count() == 0, |el| {
911                el.when_some(self.delegate.no_matches_text(window, cx), |el, text| {
912                    el.child(
913                        v_flex().flex_grow().py_2().child(
914                            ListItem::new("empty_state")
915                                .inset(true)
916                                .spacing(ListItemSpacing::Sparse)
917                                .disabled(true)
918                                .child(Label::new(text).color(Color::Muted)),
919                        ),
920                    )
921                })
922            })
923            .children(self.delegate.render_footer(window, cx))
924            .children(match &self.head {
925                Head::Editor(editor) => {
926                    if editor_position == PickerEditorPosition::End {
927                        Some(self.delegate.render_editor(&editor.clone(), window, cx))
928                    } else {
929                        None
930                    }
931                }
932                Head::Empty(empty_head) => Some(div().child(empty_head.clone())),
933            });
934
935        let Some(aside) = aside else {
936            return menu;
937        };
938
939        let render_aside = |aside: DocumentationAside, cx: &mut Context<Self>| {
940            WithRemSize::new(ui_font_size)
941                .occlude()
942                .elevation_2(cx)
943                .w_full()
944                .p_2()
945                .overflow_hidden()
946                .when(is_wide_window, |this| this.max_w_96())
947                .when(!is_wide_window, |this| this.max_w_48())
948                .child((aside.render)(cx))
949        };
950
951        if is_wide_window {
952            div().relative().child(menu).child(
953                h_flex()
954                    .absolute()
955                    .when(aside.side == DocumentationSide::Left, |this| {
956                        this.right_full().mr_1()
957                    })
958                    .when(aside.side == DocumentationSide::Right, |this| {
959                        this.left_full().ml_1()
960                    })
961                    .when(aside.edge == DocumentationEdge::Top, |this| this.top_0())
962                    .when(aside.edge == DocumentationEdge::Bottom, |this| {
963                        this.bottom_0()
964                    })
965                    .child(render_aside(aside, cx)),
966            )
967        } else {
968            v_flex()
969                .w_full()
970                .gap_1()
971                .justify_end()
972                .child(render_aside(aside, cx))
973                .child(menu)
974        }
975    }
976}