use editor::Editor; use gpui::{ elements::*, geometry::vector::{vec2f, Vector2F}, keymap_matcher::KeymapContext, platform::CursorStyle, AnyViewHandle, AppContext, Axis, Entity, MouseButton, MouseState, RenderContext, Task, View, ViewContext, ViewHandle, WeakViewHandle, }; use menu::{Cancel, Confirm, SelectFirst, SelectIndex, SelectLast, SelectNext, SelectPrev}; use parking_lot::Mutex; use std::{cmp, sync::Arc}; pub struct Picker { delegate: WeakViewHandle, query_editor: ViewHandle, list_state: UniformListState, max_size: Vector2F, theme: Arc theme::Picker>>>, confirmed: bool, } pub trait PickerDelegate: View { fn match_count(&self) -> usize; fn selected_index(&self) -> usize; fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext); fn update_matches(&mut self, query: String, cx: &mut ViewContext) -> Task<()>; fn confirm(&mut self, cx: &mut ViewContext); fn dismiss(&mut self, cx: &mut ViewContext); fn render_match( &self, ix: usize, state: &mut MouseState, selected: bool, cx: &AppContext, ) -> ElementBox; fn center_selection_after_match_updates(&self) -> bool { false } } impl Entity for Picker { type Event = (); } impl View for Picker { fn ui_name() -> &'static str { "Picker" } fn render(&mut self, cx: &mut RenderContext) -> gpui::ElementBox { let theme = (self.theme.lock())(&cx.global::().theme); let query = self.query(cx); let delegate = self.delegate.clone(); let match_count = if let Some(delegate) = delegate.upgrade(cx.app) { delegate.read(cx).match_count() } else { 0 }; let container_style; let editor_style; if query.is_empty() && match_count == 0 { container_style = theme.empty_container; editor_style = theme.empty_input_editor.container; } else { container_style = theme.container; editor_style = theme.input_editor.container; }; Flex::new(Axis::Vertical) .with_child( ChildView::new(&self.query_editor, cx) .contained() .with_style(editor_style) .boxed(), ) .with_children(if match_count == 0 { if query.is_empty() { None } else { Some( Label::new("No matches", theme.no_matches.label.clone()) .contained() .with_style(theme.no_matches.container) .boxed(), ) } } else { Some( UniformList::new( self.list_state.clone(), match_count, cx, move |this, mut range, items, cx| { let delegate = this.delegate.upgrade(cx).unwrap(); let selected_ix = delegate.read(cx).selected_index(); range.end = cmp::min(range.end, delegate.read(cx).match_count()); items.extend(range.map(move |ix| { MouseEventHandler::::new(ix, cx, |state, cx| { delegate .read(cx) .render_match(ix, state, ix == selected_ix, cx) }) // Capture mouse events .on_down(MouseButton::Left, |_, _| {}) .on_up(MouseButton::Left, |_, _| {}) .on_click(MouseButton::Left, move |_, cx| { cx.dispatch_action(SelectIndex(ix)) }) .with_cursor_style(CursorStyle::PointingHand) .boxed() })); }, ) .contained() .with_margin_top(6.0) .flex(1., false) .boxed(), ) }) .contained() .with_style(container_style) .constrained() .with_max_width(self.max_size.x()) .with_max_height(self.max_size.y()) .named("picker") } fn keymap_context(&self, _: &AppContext) -> KeymapContext { let mut cx = Self::default_keymap_context(); cx.add_identifier("menu"); cx } fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { if cx.is_self_focused() { cx.focus(&self.query_editor); } } } impl Picker { pub fn init(cx: &mut AppContext) { cx.add_action(Self::select_first); cx.add_action(Self::select_last); cx.add_action(Self::select_next); cx.add_action(Self::select_prev); cx.add_action(Self::select_index); cx.add_action(Self::confirm); cx.add_action(Self::cancel); } pub fn new

(placeholder: P, delegate: WeakViewHandle, cx: &mut ViewContext) -> Self where P: Into>, { let theme = Arc::new(Mutex::new( Box::new(|theme: &theme::Theme| theme.picker.clone()) as Box theme::Picker>, )); let query_editor = cx.add_view({ let picker_theme = theme.clone(); |cx| { let mut editor = Editor::single_line( Some(Arc::new(move |theme| { (picker_theme.lock())(theme).input_editor.clone() })), cx, ); editor.set_placeholder_text(placeholder, cx); editor } }); cx.subscribe(&query_editor, Self::on_query_editor_event) .detach(); let this = Self { query_editor, list_state: Default::default(), delegate, max_size: vec2f(540., 420.), theme, confirmed: false, }; cx.defer(|this, cx| { if let Some(delegate) = this.delegate.upgrade(cx) { cx.observe(&delegate, |_, _, cx| cx.notify()).detach(); this.update_matches(String::new(), cx) } }); this } pub fn with_max_size(mut self, width: f32, height: f32) -> Self { self.max_size = vec2f(width, height); self } pub fn with_theme(self, theme: F) -> Self where F: 'static + Fn(&theme::Theme) -> theme::Picker, { *self.theme.lock() = Box::new(theme); self } pub fn query(&self, cx: &AppContext) -> String { self.query_editor.read(cx).text(cx) } pub fn set_query(&self, query: impl Into>, cx: &mut ViewContext) { self.query_editor .update(cx, |editor, cx| editor.set_text(query, cx)); } fn on_query_editor_event( &mut self, _: ViewHandle, event: &editor::Event, cx: &mut ViewContext, ) { match event { editor::Event::BufferEdited { .. } => self.update_matches(self.query(cx), cx), editor::Event::Blurred if !self.confirmed => { if let Some(delegate) = self.delegate.upgrade(cx) { delegate.update(cx, |delegate, cx| { delegate.dismiss(cx); }) } } _ => {} } } pub fn update_matches(&mut self, query: String, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { let update = delegate.update(cx, |d, cx| d.update_matches(query, cx)); cx.spawn(|this, mut cx| async move { update.await; this.update(&mut cx, |this, cx| { if let Some(delegate) = this.delegate.upgrade(cx) { let delegate = delegate.read(cx); let index = delegate.selected_index(); let target = if delegate.center_selection_after_match_updates() { ScrollTarget::Center(index) } else { ScrollTarget::Show(index) }; this.list_state.scroll_to(target); cx.notify(); } }); }) .detach() } } pub fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { delegate.update(cx, |delegate, cx| { if delegate.match_count() > 0 { delegate.set_selected_index(0, cx); self.list_state.scroll_to(ScrollTarget::Show(0)); } }); cx.notify(); } } pub fn select_index(&mut self, action: &SelectIndex, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { let index = action.0; delegate.update(cx, |delegate, cx| { if delegate.match_count() > 0 { self.confirmed = true; delegate.set_selected_index(index, cx); delegate.confirm(cx); } }); } } pub fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { delegate.update(cx, |delegate, cx| { let match_count = delegate.match_count(); if match_count > 0 { let index = match_count - 1; delegate.set_selected_index(index, cx); self.list_state.scroll_to(ScrollTarget::Show(index)); } }); cx.notify(); } } pub fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { delegate.update(cx, |delegate, cx| { let next_index = delegate.selected_index() + 1; if next_index < delegate.match_count() { delegate.set_selected_index(next_index, cx); self.list_state.scroll_to(ScrollTarget::Show(next_index)); } }); cx.notify(); } } pub fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { delegate.update(cx, |delegate, cx| { let mut selected_index = delegate.selected_index(); if selected_index > 0 { selected_index -= 1; delegate.set_selected_index(selected_index, cx); self.list_state .scroll_to(ScrollTarget::Show(selected_index)); } }); cx.notify(); } } fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { self.confirmed = true; delegate.update(cx, |delegate, cx| delegate.confirm(cx)); } } fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext) { if let Some(delegate) = self.delegate.upgrade(cx) { delegate.update(cx, |delegate, cx| delegate.dismiss(cx)); } } }