Rename SelectorModal -> Picker, put it in its own crate

Max Brunsfeld created

Change summary

Cargo.lock                                    | 16 ++++++++++
crates/command_palette/Cargo.toml             |  1 
crates/command_palette/src/command_palette.rs | 12 +++----
crates/picker/Cargo.toml                      | 23 +++++++++++++++
crates/picker/src/picker.rs                   | 32 ++++++++++----------
5 files changed, 61 insertions(+), 23 deletions(-)

Detailed changes

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"

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" }

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::<CommandPalette>(cx);
+    Picker::<CommandPalette>::init(cx);
 }
 
 actions!(command_palette, [Toggle]);
 
 pub struct CommandPalette {
-    selector: ViewHandle<SelectorModal<Self>>,
+    selector: ViewHandle<Picker<Self>>,
     actions: Vec<Command>,
     matches: Vec<StringMatch>,
     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()
     }

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"

crates/command_palette/src/selector.rs → 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<D: SelectorModalDelegate>(cx: &mut MutableAppContext) {
-    cx.add_action(SelectorModal::<D>::select_first);
-    cx.add_action(SelectorModal::<D>::select_last);
-    cx.add_action(SelectorModal::<D>::select_next);
-    cx.add_action(SelectorModal::<D>::select_prev);
-    cx.add_action(SelectorModal::<D>::confirm);
-    cx.add_action(SelectorModal::<D>::cancel);
-}
-
-pub struct SelectorModal<D: SelectorModalDelegate> {
+pub struct Picker<D: PickerDelegate> {
     delegate: WeakViewHandle<D>,
     query_editor: ViewHandle<Editor>,
     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<D: SelectorModalDelegate> Entity for SelectorModal<D> {
+impl<D: PickerDelegate> Entity for Picker<D> {
     type Event = ();
 }
 
-impl<D: SelectorModalDelegate> View for SelectorModal<D> {
+impl<D: PickerDelegate> View for Picker<D> {
     fn ui_name() -> &'static str {
-        "SelectorModal"
+        "Picker"
     }
 
     fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox {
@@ -66,7 +57,7 @@ impl<D: SelectorModalDelegate> View for SelectorModal<D> {
             .with_max_height(420.0)
             .aligned()
             .top()
-            .named("selector")
+            .named("picker")
     }
 
     fn keymap_context(&self, _: &AppContext) -> keymap::Context {
@@ -80,7 +71,16 @@ impl<D: SelectorModalDelegate> View for SelectorModal<D> {
     }
 }
 
-impl<D: SelectorModalDelegate> SelectorModal<D> {
+impl<D: PickerDelegate> Picker<D> {
+    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<D>, cx: &mut ViewContext<Self>) -> Self {
         let query_editor = cx.add_view(|cx| {
             Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx)