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