Clean up Picker constructor signature (#9500)

This PR cleans up the (internal) `Picker` constructor signature, mainly
to remove the consecutive boolean parameters.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-03-18 12:27:51 -04:00 committed by GitHub
parent 98111c3b00
commit c116a7ca5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 34 deletions

View File

@ -16,8 +16,8 @@ pub(crate) enum Head {
impl Head { impl Head {
pub fn editor<V: 'static>( pub fn editor<V: 'static>(
placeholder_text: Arc<str>, placeholder_text: Arc<str>,
cx: &mut ViewContext<V>,
edit_handler: impl FnMut(&mut V, View<Editor>, &EditorEvent, &mut ViewContext<'_, V>) + 'static, edit_handler: impl FnMut(&mut V, View<Editor>, &EditorEvent, &mut ViewContext<'_, V>) + 'static,
cx: &mut ViewContext<V>,
) -> Self { ) -> Self {
let editor = cx.new_view(|cx| { let editor = cx.new_view(|cx| {
let mut editor = Editor::single_line(cx); let mut editor = Editor::single_line(cx);

View File

@ -93,42 +93,52 @@ impl<D: PickerDelegate> FocusableView for Picker<D> {
} }
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
enum ContainerKind {
List,
UniformList,
}
impl<D: PickerDelegate> Picker<D> { impl<D: PickerDelegate> Picker<D> {
/// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height. /// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height.
/// The picker allows the user to perform search items by text. /// The picker allows the user to perform search items by text.
/// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`. /// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`.
pub fn uniform_list(delegate: D, cx: &mut ViewContext<Self>) -> Self { pub fn uniform_list(delegate: D, cx: &mut ViewContext<Self>) -> Self {
Self::new(delegate, cx, true, true) let head = Head::editor(
delegate.placeholder_text(cx),
Self::on_input_editor_event,
cx,
);
Self::new(delegate, ContainerKind::UniformList, head, cx)
} }
/// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height. /// A picker, which displays its matches using `gpui::uniform_list`, all matches should have the same height.
/// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`. /// If `PickerDelegate::render_match` can return items with different heights, use `Picker::list`.
pub fn nonsearchable_uniform_list(delegate: D, cx: &mut ViewContext<Self>) -> Self { pub fn nonsearchable_uniform_list(delegate: D, cx: &mut ViewContext<Self>) -> Self {
Self::new(delegate, cx, true, false) let head = Head::empty(cx);
Self::new(delegate, ContainerKind::UniformList, head, cx)
} }
/// A picker, which displays its matches using `gpui::list`, matches can have different heights. /// A picker, which displays its matches using `gpui::list`, matches can have different heights.
/// The picker allows the user to perform search items by text. /// The picker allows the user to perform search items by text.
/// If `PickerDelegate::render_match` only returns items with the same height, use `Picker::uniform_list` as its implementation is optimized for that. /// If `PickerDelegate::render_match` only returns items with the same height, use `Picker::uniform_list` as its implementation is optimized for that.
pub fn list(delegate: D, cx: &mut ViewContext<Self>) -> Self { pub fn list(delegate: D, cx: &mut ViewContext<Self>) -> Self {
Self::new(delegate, cx, false, true) let head = Head::editor(
delegate.placeholder_text(cx),
Self::on_input_editor_event,
cx,
);
Self::new(delegate, ContainerKind::List, head, cx)
} }
fn new(delegate: D, cx: &mut ViewContext<Self>, is_uniform: bool, is_queryable: bool) -> Self { fn new(delegate: D, container: ContainerKind, head: Head, cx: &mut ViewContext<Self>) -> Self {
let head = if is_queryable {
Head::editor(
delegate.placeholder_text(cx),
cx,
Self::on_input_editor_event,
)
} else {
Head::empty(cx)
};
let mut this = Self { let mut this = Self {
delegate, delegate,
head, head,
element_container: Self::create_element_container(is_uniform, cx), element_container: Self::create_element_container(container, cx),
pending_update_matches: None, pending_update_matches: None,
confirm_on_update: None, confirm_on_update: None,
width: None, width: None,
@ -142,25 +152,31 @@ impl<D: PickerDelegate> Picker<D> {
this this
} }
fn create_element_container(is_uniform: bool, cx: &mut ViewContext<Self>) -> ElementContainer { fn create_element_container(
if is_uniform { container: ContainerKind,
ElementContainer::UniformList(UniformListScrollHandle::new()) cx: &mut ViewContext<Self>,
} else { ) -> ElementContainer {
let view = cx.view().downgrade(); match container {
ElementContainer::List(ListState::new( ContainerKind::UniformList => {
0, ElementContainer::UniformList(UniformListScrollHandle::new())
gpui::ListAlignment::Top, }
px(1000.), ContainerKind::List => {
move |ix, cx| { let view = cx.view().downgrade();
view.upgrade() ElementContainer::List(ListState::new(
.map(|view| { 0,
view.update(cx, |this, cx| { gpui::ListAlignment::Top,
this.render_element(cx, ix).into_any_element() px(1000.),
move |ix, cx| {
view.upgrade()
.map(|view| {
view.update(cx, |this, cx| {
this.render_element(cx, ix).into_any_element()
})
}) })
}) .unwrap_or_else(|| div().into_any_element())
.unwrap_or_else(|| div().into_any_element()) },
}, ))
)) }
} }
} }