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 } else if options.len() == 1 {
32 return Task::ready(Some(0));
33 }
34 let prompt = prompt.to_string().into();
35
36 window.spawn(cx, async move |cx| {
37 // Modal branch picker has a longer trailoff than a popover one.
38 let (tx, rx) = oneshot::channel();
39 let delegate = PickerPromptDelegate::new(prompt, options, tx, 70);
40
41 workspace
42 .update_in(cx, |workspace, window, cx| {
43 workspace.toggle_modal(window, cx, |window, cx| {
44 PickerPrompt::new(delegate, 34., window, cx)
45 })
46 })
47 .ok();
48
49 (rx.await).ok()
50 })
51}
52
53impl PickerPrompt {
54 fn new(
55 delegate: PickerPromptDelegate,
56 rem_width: f32,
57 window: &mut Window,
58 cx: &mut Context<Self>,
59 ) -> Self {
60 let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
61 let _subscription = cx.subscribe(&picker, |_, _, _, cx| cx.emit(DismissEvent));
62 Self {
63 picker,
64 rem_width,
65 _subscription,
66 }
67 }
68}
69impl ModalView for PickerPrompt {}
70impl EventEmitter<DismissEvent> for PickerPrompt {}
71
72impl Focusable for PickerPrompt {
73 fn focus_handle(&self, cx: &App) -> FocusHandle {
74 self.picker.focus_handle(cx)
75 }
76}
77
78impl Render for PickerPrompt {
79 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
80 v_flex()
81 .w(rems(self.rem_width))
82 .child(self.picker.clone())
83 .on_mouse_down_out(cx.listener(|this, _, window, cx| {
84 this.picker.update(cx, |this, cx| {
85 this.cancel(&Default::default(), window, cx);
86 })
87 }))
88 }
89}
90
91pub struct PickerPromptDelegate {
92 prompt: Arc<str>,
93 matches: Vec<StringMatch>,
94 all_options: Vec<SharedString>,
95 selected_index: usize,
96 max_match_length: usize,
97 tx: Option<oneshot::Sender<usize>>,
98}
99
100impl PickerPromptDelegate {
101 pub fn new(
102 prompt: Arc<str>,
103 options: Vec<SharedString>,
104 tx: oneshot::Sender<usize>,
105 max_chars: usize,
106 ) -> Self {
107 Self {
108 prompt,
109 all_options: options,
110 matches: vec![],
111 selected_index: 0,
112 max_match_length: max_chars,
113 tx: Some(tx),
114 }
115 }
116}
117
118impl PickerDelegate for PickerPromptDelegate {
119 type ListItem = ListItem;
120
121 fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
122 self.prompt.clone()
123 }
124
125 fn match_count(&self) -> usize {
126 self.matches.len()
127 }
128
129 fn selected_index(&self) -> usize {
130 self.selected_index
131 }
132
133 fn set_selected_index(
134 &mut self,
135 ix: usize,
136 _window: &mut Window,
137 _: &mut Context<Picker<Self>>,
138 ) {
139 self.selected_index = ix;
140 }
141
142 fn update_matches(
143 &mut self,
144 query: String,
145 window: &mut Window,
146 cx: &mut Context<Picker<Self>>,
147 ) -> Task<()> {
148 cx.spawn_in(window, async move |picker, cx| {
149 let candidates = picker.read_with(cx, |picker, _| {
150 picker
151 .delegate
152 .all_options
153 .iter()
154 .enumerate()
155 .map(|(ix, option)| StringMatchCandidate::new(ix, option))
156 .collect::<Vec<StringMatchCandidate>>()
157 });
158 let Some(candidates) = candidates.log_err() else {
159 return;
160 };
161 let matches: Vec<StringMatch> = if query.is_empty() {
162 candidates
163 .into_iter()
164 .enumerate()
165 .map(|(index, candidate)| StringMatch {
166 candidate_id: index,
167 string: candidate.string,
168 positions: Vec::new(),
169 score: 0.0,
170 })
171 .collect()
172 } else {
173 fuzzy::match_strings(
174 &candidates,
175 &query,
176 true,
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.get(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}