1use editor::Editor;
2use gpui::{
3 elements::*,
4 geometry::vector::{vec2f, Vector2F},
5 keymap_matcher::KeymapContext,
6 platform::{CursorStyle, MouseButton},
7 AnyElement, AnyViewHandle, AppContext, Axis, Entity, MouseState, Task, View, ViewContext,
8 ViewHandle,
9};
10use menu::{Cancel, Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev};
11use parking_lot::Mutex;
12use std::{cmp, sync::Arc};
13use util::ResultExt;
14use workspace::Modal;
15
16pub enum PickerEvent {
17 Dismiss,
18}
19
20pub struct Picker<D: PickerDelegate> {
21 delegate: D,
22 query_editor: ViewHandle<Editor>,
23 list_state: UniformListState,
24 max_size: Vector2F,
25 theme: Arc<Mutex<Box<dyn Fn(&theme::Theme) -> theme::Picker>>>,
26 confirmed: bool,
27 pending_update_matches: Task<Option<()>>,
28 has_focus: bool,
29}
30
31pub trait PickerDelegate: Sized + 'static {
32 fn placeholder_text(&self) -> Arc<str>;
33 fn match_count(&self) -> usize;
34 fn selected_index(&self) -> usize;
35 fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>);
36 fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()>;
37 fn confirm(&mut self, cx: &mut ViewContext<Picker<Self>>);
38 fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>);
39 fn render_match(
40 &self,
41 ix: usize,
42 state: &mut MouseState,
43 selected: bool,
44 cx: &AppContext,
45 ) -> AnyElement<Picker<Self>>;
46 fn center_selection_after_match_updates(&self) -> bool {
47 false
48 }
49 fn render_header(
50 &self,
51 _cx: &mut ViewContext<Picker<Self>>,
52 ) -> Option<AnyElement<Picker<Self>>> {
53 None
54 }
55 fn render_footer(
56 &self,
57 _cx: &mut ViewContext<Picker<Self>>,
58 ) -> Option<AnyElement<Picker<Self>>> {
59 None
60 }
61}
62
63impl<D: PickerDelegate> Entity for Picker<D> {
64 type Event = PickerEvent;
65}
66
67impl<D: PickerDelegate> View for Picker<D> {
68 fn ui_name() -> &'static str {
69 "Picker"
70 }
71
72 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
73 let theme = (self.theme.lock())(theme::current(cx).as_ref());
74 let query = self.query(cx);
75 let match_count = self.delegate.match_count();
76
77 let container_style;
78 let editor_style;
79 if query.is_empty() && match_count == 0 {
80 container_style = theme.empty_container;
81 editor_style = theme.empty_input_editor.container;
82 } else {
83 container_style = theme.container;
84 editor_style = theme.input_editor.container;
85 };
86
87 Flex::new(Axis::Vertical)
88 .with_child(
89 ChildView::new(&self.query_editor, cx)
90 .contained()
91 .with_style(editor_style),
92 )
93 .with_children(self.delegate.render_header(cx))
94 .with_children(if match_count == 0 {
95 if query.is_empty() {
96 None
97 } else {
98 Some(
99 Label::new("No matches", theme.no_matches.label.clone())
100 .contained()
101 .with_style(theme.no_matches.container)
102 .into_any(),
103 )
104 }
105 } else {
106 Some(
107 UniformList::new(
108 self.list_state.clone(),
109 match_count,
110 cx,
111 move |this, mut range, items, cx| {
112 let selected_ix = this.delegate.selected_index();
113 range.end = cmp::min(range.end, this.delegate.match_count());
114 items.extend(range.map(move |ix| {
115 MouseEventHandler::<D, _>::new(ix, cx, |state, cx| {
116 this.delegate.render_match(ix, state, ix == selected_ix, cx)
117 })
118 // Capture mouse events
119 .on_down(MouseButton::Left, |_, _, _| {})
120 .on_up(MouseButton::Left, |_, _, _| {})
121 .on_click(MouseButton::Left, move |_, picker, cx| {
122 picker.select_index(ix, cx);
123 })
124 .with_cursor_style(CursorStyle::PointingHand)
125 .into_any()
126 }));
127 },
128 )
129 .contained()
130 .with_margin_top(6.0)
131 .flex(1., false)
132 .into_any(),
133 )
134 })
135 .with_children(self.delegate.render_footer(cx))
136 .contained()
137 .with_style(container_style)
138 .constrained()
139 .with_max_width(self.max_size.x())
140 .with_max_height(self.max_size.y())
141 .into_any_named("picker")
142 }
143
144 fn update_keymap_context(&self, keymap: &mut KeymapContext, _: &AppContext) {
145 Self::reset_to_default_keymap_context(keymap);
146 keymap.add_identifier("menu");
147 }
148
149 fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
150 self.has_focus = true;
151 if cx.is_self_focused() {
152 cx.focus(&self.query_editor);
153 }
154 }
155
156 fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {
157 self.has_focus = false;
158 }
159}
160
161impl<D: PickerDelegate> Modal for Picker<D> {
162 fn has_focus(&self) -> bool {
163 self.has_focus
164 }
165
166 fn dismiss_on_event(event: &Self::Event) -> bool {
167 matches!(event, PickerEvent::Dismiss)
168 }
169}
170
171impl<D: PickerDelegate> Picker<D> {
172 pub fn init(cx: &mut AppContext) {
173 cx.add_action(Self::select_first);
174 cx.add_action(Self::select_last);
175 cx.add_action(Self::select_next);
176 cx.add_action(Self::select_prev);
177 cx.add_action(Self::confirm);
178 cx.add_action(Self::cancel);
179 }
180
181 pub fn new(delegate: D, cx: &mut ViewContext<Self>) -> Self {
182 let theme = Arc::new(Mutex::new(
183 Box::new(|theme: &theme::Theme| theme.picker.clone())
184 as Box<dyn Fn(&theme::Theme) -> theme::Picker>,
185 ));
186 let placeholder_text = delegate.placeholder_text();
187 let query_editor = cx.add_view({
188 let picker_theme = theme.clone();
189 |cx| {
190 let mut editor = Editor::single_line(
191 Some(Arc::new(move |theme| {
192 (picker_theme.lock())(theme).input_editor.clone()
193 })),
194 cx,
195 );
196 editor.set_placeholder_text(placeholder_text, cx);
197 editor
198 }
199 });
200 cx.subscribe(&query_editor, Self::on_query_editor_event)
201 .detach();
202 let mut this = Self {
203 query_editor,
204 list_state: Default::default(),
205 delegate,
206 max_size: vec2f(540., 420.),
207 theme,
208 confirmed: false,
209 pending_update_matches: Task::ready(None),
210 has_focus: false,
211 };
212 this.update_matches(String::new(), cx);
213 this
214 }
215
216 pub fn with_max_size(mut self, width: f32, height: f32) -> Self {
217 self.max_size = vec2f(width, height);
218 self
219 }
220
221 pub fn with_theme<F>(self, theme: F) -> Self
222 where
223 F: 'static + Fn(&theme::Theme) -> theme::Picker,
224 {
225 *self.theme.lock() = Box::new(theme);
226 self
227 }
228
229 pub fn delegate(&self) -> &D {
230 &self.delegate
231 }
232
233 pub fn delegate_mut(&mut self) -> &mut D {
234 &mut self.delegate
235 }
236
237 pub fn query(&self, cx: &AppContext) -> String {
238 self.query_editor.read(cx).text(cx)
239 }
240
241 pub fn set_query(&self, query: impl Into<Arc<str>>, cx: &mut ViewContext<Self>) {
242 self.query_editor
243 .update(cx, |editor, cx| editor.set_text(query, cx));
244 }
245
246 fn on_query_editor_event(
247 &mut self,
248 _: ViewHandle<Editor>,
249 event: &editor::Event,
250 cx: &mut ViewContext<Self>,
251 ) {
252 match event {
253 editor::Event::BufferEdited { .. } => self.update_matches(self.query(cx), cx),
254 editor::Event::Blurred if !self.confirmed => {
255 self.dismiss(cx);
256 }
257 _ => {}
258 }
259 }
260
261 pub fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) {
262 let update = self.delegate.update_matches(query, cx);
263 self.matches_updated(cx);
264 self.pending_update_matches = cx.spawn(|this, mut cx| async move {
265 update.await;
266 this.update(&mut cx, |this, cx| this.matches_updated(cx))
267 .log_err()
268 });
269 }
270
271 fn matches_updated(&mut self, cx: &mut ViewContext<Self>) {
272 let index = self.delegate.selected_index();
273 let target = if self.delegate.center_selection_after_match_updates() {
274 ScrollTarget::Center(index)
275 } else {
276 ScrollTarget::Show(index)
277 };
278 self.list_state.scroll_to(target);
279 cx.notify();
280 }
281
282 pub fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
283 if self.delegate.match_count() > 0 {
284 self.delegate.set_selected_index(0, cx);
285 self.list_state.scroll_to(ScrollTarget::Show(0));
286 }
287
288 cx.notify();
289 }
290
291 pub fn select_index(&mut self, index: usize, cx: &mut ViewContext<Self>) {
292 if self.delegate.match_count() > 0 {
293 self.confirmed = true;
294 self.delegate.set_selected_index(index, cx);
295 self.delegate.confirm(cx);
296 }
297 }
298
299 pub fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
300 let match_count = self.delegate.match_count();
301 if match_count > 0 {
302 let index = match_count - 1;
303 self.delegate.set_selected_index(index, cx);
304 self.list_state.scroll_to(ScrollTarget::Show(index));
305 }
306 cx.notify();
307 }
308
309 pub fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
310 let next_index = self.delegate.selected_index() + 1;
311 if next_index < self.delegate.match_count() {
312 self.delegate.set_selected_index(next_index, cx);
313 self.list_state.scroll_to(ScrollTarget::Show(next_index));
314 }
315
316 cx.notify();
317 }
318
319 pub fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
320 let mut selected_index = self.delegate.selected_index();
321 if selected_index > 0 {
322 selected_index -= 1;
323 self.delegate.set_selected_index(selected_index, cx);
324 self.list_state
325 .scroll_to(ScrollTarget::Show(selected_index));
326 }
327
328 cx.notify();
329 }
330
331 pub fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
332 self.confirmed = true;
333 self.delegate.confirm(cx);
334 }
335
336 fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
337 self.dismiss(cx);
338 }
339
340 fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
341 cx.emit(PickerEvent::Dismiss);
342 self.delegate.dismissed(cx);
343 }
344}