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

This commit is contained in:
Max Brunsfeld 2022-04-14 16:21:22 -07:00
parent bde52d5c93
commit 7964464e3d
5 changed files with 61 additions and 23 deletions

16
Cargo.lock generated
View file

@ -1152,6 +1152,7 @@ dependencies = [
"env_logger 0.8.3", "env_logger 0.8.3",
"fuzzy", "fuzzy",
"gpui", "gpui",
"picker",
"serde_json", "serde_json",
"settings", "settings",
"theme", "theme",
@ -3545,6 +3546,21 @@ dependencies = [
"indexmap", "indexmap",
] ]
[[package]]
name = "picker"
version = "0.1.0"
dependencies = [
"ctor",
"editor",
"env_logger 0.8.3",
"gpui",
"serde_json",
"settings",
"theme",
"util",
"workspace",
]
[[package]] [[package]]
name = "pico-args" name = "pico-args"
version = "0.4.0" version = "0.4.0"

View file

@ -11,6 +11,7 @@ doctest = false
editor = { path = "../editor" } editor = { path = "../editor" }
fuzzy = { path = "../fuzzy" } fuzzy = { path = "../fuzzy" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
picker = { path = "../picker" }
settings = { path = "../settings" } settings = { path = "../settings" }
util = { path = "../util" } util = { path = "../util" }
theme = { path = "../theme" } theme = { path = "../theme" }

View file

@ -5,22 +5,20 @@ use gpui::{
keymap::Keystroke, keymap::Keystroke,
Action, Element, Entity, MutableAppContext, View, ViewContext, ViewHandle, Action, Element, Entity, MutableAppContext, View, ViewContext, ViewHandle,
}; };
use selector::{SelectorModal, SelectorModalDelegate}; use picker::{Picker, PickerDelegate};
use settings::Settings; use settings::Settings;
use std::cmp; use std::cmp;
use workspace::Workspace; use workspace::Workspace;
mod selector;
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(CommandPalette::toggle); cx.add_action(CommandPalette::toggle);
selector::init::<CommandPalette>(cx); Picker::<CommandPalette>::init(cx);
} }
actions!(command_palette, [Toggle]); actions!(command_palette, [Toggle]);
pub struct CommandPalette { pub struct CommandPalette {
selector: ViewHandle<SelectorModal<Self>>, selector: ViewHandle<Picker<Self>>,
actions: Vec<Command>, actions: Vec<Command>,
matches: Vec<StringMatch>, matches: Vec<StringMatch>,
selected_ix: usize, selected_ix: usize,
@ -50,7 +48,7 @@ impl CommandPalette {
.map_or(Vec::new(), |binding| binding.keystrokes().to_vec()), .map_or(Vec::new(), |binding| binding.keystrokes().to_vec()),
}) })
.collect(); .collect();
let selector = cx.add_view(|cx| SelectorModal::new(this, cx)); let selector = cx.add_view(|cx| Picker::new(this, cx));
Self { Self {
selector, selector,
actions, actions,
@ -108,7 +106,7 @@ impl View for CommandPalette {
} }
} }
impl SelectorModalDelegate for CommandPalette { impl PickerDelegate for CommandPalette {
fn match_count(&self) -> usize { fn match_count(&self) -> usize {
self.matches.len() self.matches.len()
} }

23
crates/picker/Cargo.toml Normal file
View file

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

View file

@ -11,22 +11,13 @@ use settings::Settings;
use std::cmp; use std::cmp;
use workspace::menu::{Cancel, Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev}; use workspace::menu::{Cancel, Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev};
pub fn init<D: SelectorModalDelegate>(cx: &mut MutableAppContext) { pub struct Picker<D: PickerDelegate> {
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> {
delegate: WeakViewHandle<D>, delegate: WeakViewHandle<D>,
query_editor: ViewHandle<Editor>, query_editor: ViewHandle<Editor>,
list_state: UniformListState, list_state: UniformListState,
} }
pub trait SelectorModalDelegate: View { pub trait PickerDelegate: View {
fn match_count(&self) -> usize; fn match_count(&self) -> usize;
fn selected_index(&self) -> usize; fn selected_index(&self) -> usize;
fn set_selected_index(&mut self, ix: 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; 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 = (); type Event = ();
} }
impl<D: SelectorModalDelegate> View for SelectorModal<D> { impl<D: PickerDelegate> View for Picker<D> {
fn ui_name() -> &'static str { fn ui_name() -> &'static str {
"SelectorModal" "Picker"
} }
fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox { 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) .with_max_height(420.0)
.aligned() .aligned()
.top() .top()
.named("selector") .named("picker")
} }
fn keymap_context(&self, _: &AppContext) -> keymap::Context { 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 { pub fn new(delegate: WeakViewHandle<D>, cx: &mut ViewContext<Self>) -> Self {
let query_editor = cx.add_view(|cx| { let query_editor = cx.add_view(|cx| {
Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx) Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx)