picker.rs

  1use editor::Editor;
  2use gpui::{
  3    elements::{
  4        ChildView, EventHandler, Flex, Label, ParentElement, ScrollTarget, UniformList,
  5        UniformListState,
  6    },
  7    geometry::vector::{vec2f, Vector2F},
  8    keymap, AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task,
  9    View, ViewContext, ViewHandle, WeakViewHandle,
 10};
 11use settings::Settings;
 12use std::cmp;
 13use workspace::menu::{
 14    Cancel, Confirm, SelectFirst, SelectIndex, SelectLast, SelectNext, SelectPrev,
 15};
 16
 17pub struct Picker<D: PickerDelegate> {
 18    delegate: WeakViewHandle<D>,
 19    query_editor: ViewHandle<Editor>,
 20    list_state: UniformListState,
 21    update_task: Option<Task<()>>,
 22    max_size: Vector2F,
 23    confirmed: bool,
 24}
 25
 26pub trait PickerDelegate: View {
 27    fn match_count(&self) -> usize;
 28    fn selected_index(&self) -> usize;
 29    fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Self>);
 30    fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> Task<()>;
 31    fn confirm(&mut self, cx: &mut ViewContext<Self>);
 32    fn dismiss(&mut self, cx: &mut ViewContext<Self>);
 33    fn render_match(&self, ix: usize, selected: bool, cx: &AppContext) -> ElementBox;
 34    fn center_selection_after_match_updates(&self) -> bool {
 35        false
 36    }
 37}
 38
 39impl<D: PickerDelegate> Entity for Picker<D> {
 40    type Event = ();
 41}
 42
 43impl<D: PickerDelegate> View for Picker<D> {
 44    fn ui_name() -> &'static str {
 45        "Picker"
 46    }
 47
 48    fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox {
 49        let settings = cx.global::<Settings>();
 50        let delegate = self.delegate.clone();
 51        let match_count = if let Some(delegate) = delegate.upgrade(cx.app) {
 52            delegate.read(cx).match_count()
 53        } else {
 54            0
 55        };
 56
 57        Flex::new(Axis::Vertical)
 58            .with_child(
 59                ChildView::new(&self.query_editor)
 60                    .contained()
 61                    .with_style(settings.theme.selector.input_editor.container)
 62                    .boxed(),
 63            )
 64            .with_child(
 65                if match_count == 0 {
 66                    Label::new(
 67                        "No matches".into(),
 68                        settings.theme.selector.empty.label.clone(),
 69                    )
 70                    .contained()
 71                    .with_style(settings.theme.selector.empty.container)
 72                } else {
 73                    UniformList::new(
 74                        self.list_state.clone(),
 75                        match_count,
 76                        move |mut range, items, cx| {
 77                            let cx = cx.as_ref();
 78                            let delegate = delegate.upgrade(cx).unwrap();
 79                            let delegate = delegate.read(cx);
 80                            let selected_ix = delegate.selected_index();
 81                            range.end = cmp::min(range.end, delegate.match_count());
 82                            items.extend(range.map(move |ix| {
 83                                EventHandler::new(delegate.render_match(ix, ix == selected_ix, cx))
 84                                    .on_mouse_down(move |cx| {
 85                                        cx.dispatch_action(SelectIndex(ix));
 86                                        true
 87                                    })
 88                                    .boxed()
 89                            }));
 90                        },
 91                    )
 92                    .contained()
 93                    .with_margin_top(6.0)
 94                }
 95                .flex(1., false)
 96                .boxed(),
 97            )
 98            .contained()
 99            .with_style(settings.theme.selector.container)
100            .constrained()
101            .with_max_width(self.max_size.x())
102            .with_max_height(self.max_size.y())
103            .aligned()
104            .top()
105            .named("picker")
106    }
107
108    fn keymap_context(&self, _: &AppContext) -> keymap::Context {
109        let mut cx = Self::default_keymap_context();
110        cx.set.insert("menu".into());
111        cx
112    }
113
114    fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
115        cx.focus(&self.query_editor);
116    }
117}
118
119impl<D: PickerDelegate> Picker<D> {
120    pub fn init(cx: &mut MutableAppContext) {
121        cx.add_action(Self::select_first);
122        cx.add_action(Self::select_last);
123        cx.add_action(Self::select_next);
124        cx.add_action(Self::select_prev);
125        cx.add_action(Self::select_index);
126        cx.add_action(Self::confirm);
127        cx.add_action(Self::cancel);
128    }
129
130    pub fn new(delegate: WeakViewHandle<D>, cx: &mut ViewContext<Self>) -> Self {
131        let query_editor = cx.add_view(|cx| {
132            Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx)
133        });
134        cx.subscribe(&query_editor, Self::on_query_editor_event)
135            .detach();
136        let this = Self {
137            query_editor,
138            list_state: Default::default(),
139            update_task: None,
140            delegate,
141            max_size: vec2f(500., 420.),
142            confirmed: false,
143        };
144        cx.defer(|this, cx| this.update_matches(cx));
145        this
146    }
147
148    pub fn with_max_size(mut self, width: f32, height: f32) -> Self {
149        self.max_size = vec2f(width, height);
150        self
151    }
152
153    fn on_query_editor_event(
154        &mut self,
155        _: ViewHandle<Editor>,
156        event: &editor::Event,
157        cx: &mut ViewContext<Self>,
158    ) {
159        match event {
160            editor::Event::BufferEdited { .. } => self.update_matches(cx),
161            editor::Event::Blurred if !self.confirmed => {
162                if let Some(delegate) = self.delegate.upgrade(cx) {
163                    delegate.update(cx, |delegate, cx| {
164                        delegate.dismiss(cx);
165                    })
166                }
167            }
168            _ => {}
169        }
170    }
171
172    fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
173        if let Some(delegate) = self.delegate.upgrade(cx) {
174            let query = self.query_editor.read(cx).text(cx);
175            let update = delegate.update(cx, |d, cx| d.update_matches(query, cx));
176            cx.notify();
177            self.update_task = Some(cx.spawn(|this, mut cx| async move {
178                update.await;
179                this.update(&mut cx, |this, cx| {
180                    if let Some(delegate) = this.delegate.upgrade(cx) {
181                        let delegate = delegate.read(cx);
182                        let index = delegate.selected_index();
183                        let target = if delegate.center_selection_after_match_updates() {
184                            ScrollTarget::Center(index)
185                        } else {
186                            ScrollTarget::Show(index)
187                        };
188                        this.list_state.scroll_to(target);
189                        cx.notify();
190                        this.update_task.take();
191                    }
192                });
193            }));
194        }
195    }
196
197    pub fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
198        if let Some(delegate) = self.delegate.upgrade(cx) {
199            let index = 0;
200            delegate.update(cx, |delegate, cx| delegate.set_selected_index(0, cx));
201            self.list_state.scroll_to(ScrollTarget::Show(index));
202            cx.notify();
203        }
204    }
205
206    pub fn select_index(&mut self, action: &SelectIndex, cx: &mut ViewContext<Self>) {
207        if let Some(delegate) = self.delegate.upgrade(cx) {
208            let index = action.0;
209            self.confirmed = true;
210            delegate.update(cx, |delegate, cx| {
211                delegate.set_selected_index(index, cx);
212                delegate.confirm(cx);
213            });
214        }
215    }
216
217    pub fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
218        if let Some(delegate) = self.delegate.upgrade(cx) {
219            let index = delegate.update(cx, |delegate, cx| {
220                let match_count = delegate.match_count();
221                let index = if match_count > 0 { match_count - 1 } else { 0 };
222                delegate.set_selected_index(index, cx);
223                index
224            });
225            self.list_state.scroll_to(ScrollTarget::Show(index));
226            cx.notify();
227        }
228    }
229
230    pub fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
231        if let Some(delegate) = self.delegate.upgrade(cx) {
232            let index = delegate.update(cx, |delegate, cx| {
233                let mut selected_index = delegate.selected_index();
234                if selected_index + 1 < delegate.match_count() {
235                    selected_index += 1;
236                    delegate.set_selected_index(selected_index, cx);
237                }
238                selected_index
239            });
240            self.list_state.scroll_to(ScrollTarget::Show(index));
241            cx.notify();
242        }
243    }
244
245    pub fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
246        if let Some(delegate) = self.delegate.upgrade(cx) {
247            let index = delegate.update(cx, |delegate, cx| {
248                let mut selected_index = delegate.selected_index();
249                if selected_index > 0 {
250                    selected_index -= 1;
251                    delegate.set_selected_index(selected_index, cx);
252                }
253                selected_index
254            });
255            self.list_state.scroll_to(ScrollTarget::Show(index));
256            cx.notify();
257        }
258    }
259
260    fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
261        if let Some(delegate) = self.delegate.upgrade(cx) {
262            self.confirmed = true;
263            delegate.update(cx, |delegate, cx| delegate.confirm(cx));
264        }
265    }
266
267    fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
268        if let Some(delegate) = self.delegate.upgrade(cx) {
269            delegate.update(cx, |delegate, cx| delegate.dismiss(cx));
270        }
271    }
272}