Show placeholder text for pickers

This commit is contained in:
Antonio Scandurra 2022-11-04 10:18:29 +01:00
parent 08b84416d2
commit 0b231e58fd
19 changed files with 76 additions and 40 deletions

View file

@ -16,6 +16,8 @@ util = { path = "../util" }
theme = { path = "../theme" }
workspace = { path = "../workspace" }
parking_lot = "0.11.1"
[dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] }
serde_json = { version = "1.0", features = ["preserve_order"] }

View file

@ -8,15 +8,15 @@ use gpui::{
RenderContext, Task, View, ViewContext, ViewHandle, WeakViewHandle,
};
use menu::{Cancel, Confirm, SelectFirst, SelectIndex, SelectLast, SelectNext, SelectPrev};
use settings::Settings;
use std::cmp;
use parking_lot::Mutex;
use std::{cmp, sync::Arc};
pub struct Picker<D: PickerDelegate> {
delegate: WeakViewHandle<D>,
query_editor: ViewHandle<Editor>,
list_state: UniformListState,
max_size: Vector2F,
theme: Box<dyn FnMut(&AppContext) -> &theme::Picker>,
theme: Arc<Mutex<Box<dyn Fn(&theme::Theme) -> theme::Picker>>>,
confirmed: bool,
}
@ -49,7 +49,7 @@ impl<D: PickerDelegate> View for Picker<D> {
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox {
let theme = (self.theme)(cx);
let theme = (self.theme.lock())(&cx.global::<settings::Settings>().theme);
let query = self.query(cx);
let delegate = self.delegate.clone();
let match_count = if let Some(delegate) = delegate.upgrade(cx.app) {
@ -148,9 +148,26 @@ impl<D: PickerDelegate> Picker<D> {
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.picker.input_editor.clone()), cx)
pub fn new<P>(placeholder: P, delegate: WeakViewHandle<D>, cx: &mut ViewContext<Self>) -> Self
where
P: Into<Arc<str>>,
{
let theme = Arc::new(Mutex::new(
Box::new(|theme: &theme::Theme| theme.picker.clone())
as Box<dyn Fn(&theme::Theme) -> 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();
@ -159,7 +176,7 @@ impl<D: PickerDelegate> Picker<D> {
list_state: Default::default(),
delegate,
max_size: vec2f(540., 420.),
theme: Box::new(|cx| &cx.global::<Settings>().theme.picker),
theme,
confirmed: false,
};
cx.defer(|this, cx| {
@ -176,11 +193,11 @@ impl<D: PickerDelegate> Picker<D> {
self
}
pub fn with_theme<F>(mut self, theme: F) -> Self
pub fn with_theme<F>(self, theme: F) -> Self
where
F: 'static + FnMut(&AppContext) -> &theme::Picker,
F: 'static + Fn(&theme::Theme) -> theme::Picker,
{
self.theme = Box::new(theme);
*self.theme.lock() = Box::new(theme);
self
}