Merge branch 'main' into dispatch-tree

This commit is contained in:
Conrad Irwin 2023-11-13 12:50:30 -07:00
commit f464d69ff8
20 changed files with 2801 additions and 2730 deletions

View file

@ -3,9 +3,8 @@ use gpui::{
div, uniform_list, Component, Div, ParentElement, Render, StatelessInteractive, Styled, Task,
UniformListScrollHandle, View, ViewContext, VisualContext, WindowContext,
};
use std::cmp;
use theme::ActiveTheme;
use ui::v_stack;
use std::{cmp, sync::Arc};
use ui::{prelude::*, v_stack, Divider, Label, LabelColor};
pub struct Picker<D: PickerDelegate> {
pub delegate: D,
@ -21,7 +20,7 @@ pub trait PickerDelegate: Sized + 'static {
fn selected_index(&self) -> usize;
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>);
// fn placeholder_text(&self) -> Arc<str>;
fn placeholder_text(&self) -> Arc<str>;
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()>;
fn confirm(&mut self, secondary: bool, cx: &mut ViewContext<Picker<Self>>);
@ -37,7 +36,11 @@ pub trait PickerDelegate: Sized + 'static {
impl<D: PickerDelegate> Picker<D> {
pub fn new(delegate: D, cx: &mut ViewContext<Self>) -> Self {
let editor = cx.build_view(|cx| Editor::single_line(cx));
let editor = cx.build_view(|cx| {
let mut editor = Editor::single_line(cx);
editor.set_placeholder_text(delegate.placeholder_text(), cx);
editor
});
cx.subscribe(&editor, Self::on_input_editor_event).detach();
Self {
delegate,
@ -142,6 +145,7 @@ impl<D: PickerDelegate> Render for Picker<D> {
div()
.context("picker")
.size_full()
.elevation_2(cx)
.on_action(Self::select_next)
.on_action(Self::select_prev)
.on_action(Self::select_first)
@ -149,38 +153,42 @@ impl<D: PickerDelegate> Render for Picker<D> {
.on_action(Self::cancel)
.on_action(Self::confirm)
.on_action(Self::secondary_confirm)
.child(
v_stack().gap_px().child(
v_stack()
.py_0p5()
.px_1()
.child(div().px_2().py_0p5().child(self.editor.clone())),
),
)
.child(
div()
.h_px()
.w_full()
.bg(cx.theme().colors().element_background),
)
.child(
v_stack()
.py_0p5()
.px_1()
.grow()
.child(
uniform_list("candidates", self.delegate.match_count(), {
move |this: &mut Self, visible_range, cx| {
let selected_ix = this.delegate.selected_index();
visible_range
.map(|ix| this.delegate.render_match(ix, ix == selected_ix, cx))
.collect()
}
})
.track_scroll(self.scroll_handle.clone()),
)
.max_h_72()
.overflow_hidden(),
.child(div().px_1().py_0p5().child(self.editor.clone())),
)
.child(Divider::horizontal())
.when(self.delegate.match_count() > 0, |el| {
el.child(
v_stack()
.p_1()
.grow()
.child(
uniform_list("candidates", self.delegate.match_count(), {
move |this: &mut Self, visible_range, cx| {
let selected_ix = this.delegate.selected_index();
visible_range
.map(|ix| {
this.delegate.render_match(ix, ix == selected_ix, cx)
})
.collect()
}
})
.track_scroll(self.scroll_handle.clone()),
)
.max_h_72()
.overflow_hidden(),
)
})
.when(self.delegate.match_count() == 0, |el| {
el.child(
v_stack()
.p_1()
.grow()
.child(Label::new("No matches").color(LabelColor::Muted)),
)
})
}
}