Allow render_match to return an Option to represent no matches

This commit is contained in:
Marshall Bowers 2023-11-28 10:33:13 -05:00
parent eac4b2d076
commit 63bd4ac999
4 changed files with 55 additions and 49 deletions

View file

@ -294,15 +294,16 @@ impl PickerDelegate for CommandPaletteDelegate {
ix: usize, ix: usize,
selected: bool, selected: bool,
cx: &mut ViewContext<Picker<Self>>, cx: &mut ViewContext<Picker<Self>>,
) -> Self::ListItem { ) -> Option<Self::ListItem> {
let colors = cx.theme().colors(); let colors = cx.theme().colors();
let Some(r#match) = self.matches.get(ix) else { let Some(r#match) = self.matches.get(ix) else {
return div(); return None;
}; };
let Some(command) = self.commands.get(r#match.candidate_id) else { let Some(command) = self.commands.get(r#match.candidate_id) else {
return div(); return None;
}; };
Some(
div() div()
.px_1() .px_1()
.text_color(colors.text) .text_color(colors.text)
@ -319,6 +320,7 @@ impl PickerDelegate for CommandPaletteDelegate {
r#match.positions.clone(), r#match.positions.clone(),
)) ))
.children(KeyBinding::for_action(&*command.action, cx)), .children(KeyBinding::for_action(&*command.action, cx)),
),
) )
} }
} }

View file

@ -711,7 +711,7 @@ impl PickerDelegate for FileFinderDelegate {
ix: usize, ix: usize,
selected: bool, selected: bool,
cx: &mut ViewContext<Picker<Self>>, cx: &mut ViewContext<Picker<Self>>,
) -> Self::ListItem { ) -> Option<Self::ListItem> {
let path_match = self let path_match = self
.matches .matches
.get(ix) .get(ix)
@ -722,6 +722,7 @@ impl PickerDelegate for FileFinderDelegate {
let (file_name, file_name_positions, full_path, full_path_positions) = let (file_name, file_name_positions, full_path, full_path_positions) =
self.labels_for_match(path_match, cx, ix); self.labels_for_match(path_match, cx, ix);
Some(
div() div()
.px_1() .px_1()
.text_color(colors.text) .text_color(colors.text)
@ -734,6 +735,7 @@ impl PickerDelegate for FileFinderDelegate {
v_stack() v_stack()
.child(HighlightedLabel::new(file_name, file_name_positions)) .child(HighlightedLabel::new(file_name, file_name_positions))
.child(HighlightedLabel::new(full_path, full_path_positions)), .child(HighlightedLabel::new(full_path, full_path_positions)),
),
) )
} }
} }

View file

@ -32,7 +32,7 @@ pub trait PickerDelegate: Sized + 'static {
ix: usize, ix: usize,
selected: bool, selected: bool,
cx: &mut ViewContext<Picker<Self>>, cx: &mut ViewContext<Picker<Self>>,
) -> Self::ListItem; ) -> Option<Self::ListItem>;
} }
impl<D: PickerDelegate> FocusableView for Picker<D> { impl<D: PickerDelegate> FocusableView for Picker<D> {
@ -230,7 +230,7 @@ impl<D: PickerDelegate> Render for Picker<D> {
) )
}), }),
) )
.child(picker.delegate.render_match( .children(picker.delegate.render_match(
ix, ix,
ix == selected_index, ix == selected_index,
cx, cx,

View file

@ -51,14 +51,15 @@ impl PickerDelegate for Delegate {
ix: usize, ix: usize,
selected: bool, selected: bool,
cx: &mut gpui::ViewContext<Picker<Self>>, cx: &mut gpui::ViewContext<Picker<Self>>,
) -> Self::ListItem { ) -> Option<Self::ListItem> {
let colors = cx.theme().colors(); let colors = cx.theme().colors();
let Some(candidate_ix) = self.matches.get(ix) else { let Some(candidate_ix) = self.matches.get(ix) else {
return div(); return None;
}; };
// TASK: Make StringMatchCandidate::string a SharedString // TASK: Make StringMatchCandidate::string a SharedString
let candidate = SharedString::from(self.candidates[*candidate_ix].string.clone()); let candidate = SharedString::from(self.candidates[*candidate_ix].string.clone());
Some(
div() div()
.text_color(colors.text) .text_color(colors.text)
.when(selected, |s| { .when(selected, |s| {
@ -69,7 +70,8 @@ impl PickerDelegate for Delegate {
.bg(colors.element_active) .bg(colors.element_active)
.text_color(colors.text_accent) .text_color(colors.text_accent)
}) })
.child(candidate) .child(candidate),
)
} }
fn selected_index(&self) -> usize { fn selected_index(&self) -> usize {