picker_prompt.rs

  1use futures::channel::oneshot;
  2use fuzzy::{StringMatch, StringMatchCandidate};
  3
  4use core::cmp;
  5use gpui::{
  6    App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, InteractiveElement,
  7    IntoElement, ParentElement, Render, SharedString, Styled, Subscription, Task, WeakEntity,
  8    Window, rems,
  9};
 10use picker::{Picker, PickerDelegate};
 11use std::sync::Arc;
 12use ui::{HighlightedLabel, ListItem, ListItemSpacing, prelude::*};
 13use util::ResultExt;
 14use workspace::{ModalView, Workspace};
 15
 16pub struct PickerPrompt {
 17    pub picker: Entity<Picker<PickerPromptDelegate>>,
 18    rem_width: f32,
 19    _subscription: Subscription,
 20}
 21
 22pub fn prompt(
 23    prompt: &str,
 24    options: Vec<SharedString>,
 25    workspace: WeakEntity<Workspace>,
 26    window: &mut Window,
 27    cx: &mut App,
 28) -> Task<Option<usize>> {
 29    if options.is_empty() {
 30        return Task::ready(None);
 31    }
 32    let prompt = prompt.to_string().into();
 33
 34    window.spawn(cx, async move |cx| {
 35        // Modal branch picker has a longer trailoff than a popover one.
 36        let (tx, rx) = oneshot::channel();
 37        let delegate = PickerPromptDelegate::new(prompt, options, tx, 70);
 38
 39        workspace
 40            .update_in(cx, |workspace, window, cx| {
 41                workspace.toggle_modal(window, cx, |window, cx| {
 42                    PickerPrompt::new(delegate, 34., window, cx)
 43                })
 44            })
 45            .ok();
 46
 47        match rx.await {
 48            Ok(selection) => Some(selection),
 49            Err(_) => None, // User cancelled
 50        }
 51    })
 52}
 53
 54impl PickerPrompt {
 55    fn new(
 56        delegate: PickerPromptDelegate,
 57        rem_width: f32,
 58        window: &mut Window,
 59        cx: &mut Context<Self>,
 60    ) -> Self {
 61        let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
 62        let _subscription = cx.subscribe(&picker, |_, _, _, cx| cx.emit(DismissEvent));
 63        Self {
 64            picker,
 65            rem_width,
 66            _subscription,
 67        }
 68    }
 69}
 70impl ModalView for PickerPrompt {}
 71impl EventEmitter<DismissEvent> for PickerPrompt {}
 72
 73impl Focusable for PickerPrompt {
 74    fn focus_handle(&self, cx: &App) -> FocusHandle {
 75        self.picker.focus_handle(cx)
 76    }
 77}
 78
 79impl Render for PickerPrompt {
 80    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 81        v_flex()
 82            .w(rems(self.rem_width))
 83            .child(self.picker.clone())
 84            .on_mouse_down_out(cx.listener(|this, _, window, cx| {
 85                this.picker.update(cx, |this, cx| {
 86                    this.cancel(&Default::default(), window, cx);
 87                })
 88            }))
 89    }
 90}
 91
 92pub struct PickerPromptDelegate {
 93    prompt: Arc<str>,
 94    matches: Vec<StringMatch>,
 95    all_options: Vec<SharedString>,
 96    selected_index: usize,
 97    max_match_length: usize,
 98    tx: Option<oneshot::Sender<usize>>,
 99}
100
101impl PickerPromptDelegate {
102    pub fn new(
103        prompt: Arc<str>,
104        options: Vec<SharedString>,
105        tx: oneshot::Sender<usize>,
106        max_chars: usize,
107    ) -> Self {
108        Self {
109            prompt,
110            all_options: options,
111            matches: vec![],
112            selected_index: 0,
113            max_match_length: max_chars,
114            tx: Some(tx),
115        }
116    }
117}
118
119impl PickerDelegate for PickerPromptDelegate {
120    type ListItem = ListItem;
121
122    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
123        self.prompt.clone()
124    }
125
126    fn match_count(&self) -> usize {
127        self.matches.len()
128    }
129
130    fn selected_index(&self) -> usize {
131        self.selected_index
132    }
133
134    fn set_selected_index(
135        &mut self,
136        ix: usize,
137        _window: &mut Window,
138        _: &mut Context<Picker<Self>>,
139    ) {
140        self.selected_index = ix;
141    }
142
143    fn update_matches(
144        &mut self,
145        query: String,
146        window: &mut Window,
147        cx: &mut Context<Picker<Self>>,
148    ) -> Task<()> {
149        cx.spawn_in(window, async move |picker, cx| {
150            let candidates = picker.update(cx, |picker, _| {
151                picker
152                    .delegate
153                    .all_options
154                    .iter()
155                    .enumerate()
156                    .map(|(ix, option)| StringMatchCandidate::new(ix, &option))
157                    .collect::<Vec<StringMatchCandidate>>()
158            });
159            let Some(candidates) = candidates.log_err() else {
160                return;
161            };
162            let matches: Vec<StringMatch> = if query.is_empty() {
163                candidates
164                    .into_iter()
165                    .enumerate()
166                    .map(|(index, candidate)| StringMatch {
167                        candidate_id: index,
168                        string: candidate.string,
169                        positions: Vec::new(),
170                        score: 0.0,
171                    })
172                    .collect()
173            } else {
174                fuzzy::match_strings(
175                    &candidates,
176                    &query,
177                    true,
178                    10000,
179                    &Default::default(),
180                    cx.background_executor().clone(),
181                )
182                .await
183            };
184            picker
185                .update(cx, |picker, _| {
186                    let delegate = &mut picker.delegate;
187                    delegate.matches = matches;
188                    if delegate.matches.is_empty() {
189                        delegate.selected_index = 0;
190                    } else {
191                        delegate.selected_index =
192                            cmp::min(delegate.selected_index, delegate.matches.len() - 1);
193                    }
194                })
195                .log_err();
196        })
197    }
198
199    fn confirm(&mut self, _: bool, _window: &mut Window, cx: &mut Context<Picker<Self>>) {
200        let Some(option) = self.matches.get(self.selected_index()) else {
201            return;
202        };
203
204        self.tx.take().map(|tx| tx.send(option.candidate_id));
205        cx.emit(DismissEvent);
206    }
207
208    fn dismissed(&mut self, _: &mut Window, cx: &mut Context<Picker<Self>>) {
209        cx.emit(DismissEvent);
210    }
211
212    fn render_match(
213        &self,
214        ix: usize,
215        selected: bool,
216        _window: &mut Window,
217        _cx: &mut Context<Picker<Self>>,
218    ) -> Option<Self::ListItem> {
219        let hit = &self.matches[ix];
220        let shortened_option = util::truncate_and_trailoff(&hit.string, self.max_match_length);
221
222        Some(
223            ListItem::new(SharedString::from(format!("picker-prompt-menu-{ix}")))
224                .inset(true)
225                .spacing(ListItemSpacing::Sparse)
226                .toggle_state(selected)
227                .map(|el| {
228                    let highlights: Vec<_> = hit
229                        .positions
230                        .iter()
231                        .filter(|index| index < &&self.max_match_length)
232                        .copied()
233                        .collect();
234
235                    el.child(HighlightedLabel::new(shortened_option, highlights))
236                }),
237        )
238    }
239}