From 7964464e3d465977ac4aeafa684c9b0bd1a0d514 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 14 Apr 2022 16:21:22 -0700 Subject: [PATCH] Rename SelectorModal -> Picker, put it in its own crate --- Cargo.lock | 16 ++++++++++ crates/command_palette/Cargo.toml | 1 + crates/command_palette/src/command_palette.rs | 12 +++---- crates/picker/Cargo.toml | 23 +++++++++++++ .../src/selector.rs => picker/src/picker.rs} | 32 +++++++++---------- 5 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 crates/picker/Cargo.toml rename crates/{command_palette/src/selector.rs => picker/src/picker.rs} (91%) diff --git a/Cargo.lock b/Cargo.lock index 484dab47d8..d501ed4b7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1152,6 +1152,7 @@ dependencies = [ "env_logger 0.8.3", "fuzzy", "gpui", + "picker", "serde_json", "settings", "theme", @@ -3545,6 +3546,21 @@ dependencies = [ "indexmap", ] +[[package]] +name = "picker" +version = "0.1.0" +dependencies = [ + "ctor", + "editor", + "env_logger 0.8.3", + "gpui", + "serde_json", + "settings", + "theme", + "util", + "workspace", +] + [[package]] name = "pico-args" version = "0.4.0" diff --git a/crates/command_palette/Cargo.toml b/crates/command_palette/Cargo.toml index b360a04125..ecff82f6f4 100644 --- a/crates/command_palette/Cargo.toml +++ b/crates/command_palette/Cargo.toml @@ -11,6 +11,7 @@ doctest = false editor = { path = "../editor" } fuzzy = { path = "../fuzzy" } gpui = { path = "../gpui" } +picker = { path = "../picker" } settings = { path = "../settings" } util = { path = "../util" } theme = { path = "../theme" } diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index b75c54bdf0..18773aaf9c 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -5,22 +5,20 @@ use gpui::{ keymap::Keystroke, Action, Element, Entity, MutableAppContext, View, ViewContext, ViewHandle, }; -use selector::{SelectorModal, SelectorModalDelegate}; +use picker::{Picker, PickerDelegate}; use settings::Settings; use std::cmp; use workspace::Workspace; -mod selector; - pub fn init(cx: &mut MutableAppContext) { cx.add_action(CommandPalette::toggle); - selector::init::(cx); + Picker::::init(cx); } actions!(command_palette, [Toggle]); pub struct CommandPalette { - selector: ViewHandle>, + selector: ViewHandle>, actions: Vec, matches: Vec, selected_ix: usize, @@ -50,7 +48,7 @@ impl CommandPalette { .map_or(Vec::new(), |binding| binding.keystrokes().to_vec()), }) .collect(); - let selector = cx.add_view(|cx| SelectorModal::new(this, cx)); + let selector = cx.add_view(|cx| Picker::new(this, cx)); Self { selector, actions, @@ -108,7 +106,7 @@ impl View for CommandPalette { } } -impl SelectorModalDelegate for CommandPalette { +impl PickerDelegate for CommandPalette { fn match_count(&self) -> usize { self.matches.len() } diff --git a/crates/picker/Cargo.toml b/crates/picker/Cargo.toml new file mode 100644 index 0000000000..86e657ecad --- /dev/null +++ b/crates/picker/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "picker" +version = "0.1.0" +edition = "2021" + +[lib] +path = "src/picker.rs" +doctest = false + +[dependencies] +editor = { path = "../editor" } +gpui = { path = "../gpui" } +settings = { path = "../settings" } +util = { path = "../util" } +theme = { path = "../theme" } +workspace = { path = "../workspace" } + +[dev-dependencies] +gpui = { path = "../gpui", features = ["test-support"] } +serde_json = { version = "1.0.64", features = ["preserve_order"] } +workspace = { path = "../workspace", features = ["test-support"] } +ctor = "0.1" +env_logger = "0.8" diff --git a/crates/command_palette/src/selector.rs b/crates/picker/src/picker.rs similarity index 91% rename from crates/command_palette/src/selector.rs rename to crates/picker/src/picker.rs index daa0f5ff6a..797f681d42 100644 --- a/crates/command_palette/src/selector.rs +++ b/crates/picker/src/picker.rs @@ -11,22 +11,13 @@ use settings::Settings; use std::cmp; use workspace::menu::{Cancel, Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev}; -pub fn init(cx: &mut MutableAppContext) { - cx.add_action(SelectorModal::::select_first); - cx.add_action(SelectorModal::::select_last); - cx.add_action(SelectorModal::::select_next); - cx.add_action(SelectorModal::::select_prev); - cx.add_action(SelectorModal::::confirm); - cx.add_action(SelectorModal::::cancel); -} - -pub struct SelectorModal { +pub struct Picker { delegate: WeakViewHandle, query_editor: ViewHandle, list_state: UniformListState, } -pub trait SelectorModalDelegate: View { +pub trait PickerDelegate: View { fn match_count(&self) -> usize; fn selected_index(&self) -> usize; fn set_selected_index(&mut self, ix: usize); @@ -36,13 +27,13 @@ pub trait SelectorModalDelegate: View { fn render_match(&self, ix: usize, selected: bool, cx: &AppContext) -> ElementBox; } -impl Entity for SelectorModal { +impl Entity for Picker { type Event = (); } -impl View for SelectorModal { +impl View for Picker { fn ui_name() -> &'static str { - "SelectorModal" + "Picker" } fn render(&mut self, cx: &mut RenderContext) -> gpui::ElementBox { @@ -66,7 +57,7 @@ impl View for SelectorModal { .with_max_height(420.0) .aligned() .top() - .named("selector") + .named("picker") } fn keymap_context(&self, _: &AppContext) -> keymap::Context { @@ -80,7 +71,16 @@ impl View for SelectorModal { } } -impl SelectorModal { +impl Picker { + pub fn init(cx: &mut MutableAppContext) { + 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::confirm); + cx.add_action(Self::cancel); + } + pub fn new(delegate: WeakViewHandle, cx: &mut ViewContext) -> Self { let query_editor = cx.add_view(|cx| { Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx)