chore: audit components
This commit is contained in:
@@ -39,8 +39,9 @@ pub fn bind_command_palette_keys(cx: &mut App) {
|
||||
]);
|
||||
}
|
||||
|
||||
/// One action the palette can run: a stable id and the label the matcher sees.
|
||||
/// A command the palette can run: a stable id and the label the matcher sees.
|
||||
#[derive(Clone)]
|
||||
#[must_use]
|
||||
pub struct Command {
|
||||
/// The stable domain id handed to `on_execute`.
|
||||
pub id: SharedString,
|
||||
@@ -54,7 +55,11 @@ pub struct Command {
|
||||
|
||||
impl Command {
|
||||
/// Creates a command without a hint or icon.
|
||||
pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
|
||||
pub fn new<I, L>(id: I, label: L) -> Self
|
||||
where
|
||||
I: Into<SharedString>,
|
||||
L: Into<SharedString>,
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
label: label.into(),
|
||||
@@ -73,7 +78,10 @@ impl Command {
|
||||
}
|
||||
|
||||
/// Sets the row's right-edge hint.
|
||||
pub fn hint(mut self, hint: impl Into<SharedString>) -> Self {
|
||||
pub fn hint<H>(mut self, hint: H) -> Self
|
||||
where
|
||||
H: Into<SharedString>,
|
||||
{
|
||||
self.hint = Some(hint.into());
|
||||
self
|
||||
}
|
||||
@@ -85,6 +93,7 @@ pub type CommandMatcher = Rc<dyn Fn(&str, &[Command]) -> Vec<Command>>;
|
||||
pub type CommandHandler = Rc<dyn Fn(&Command, &mut Window, &mut App)>;
|
||||
|
||||
/// Builder for a command palette. Pass it to [`CommandPaletteState::new`].
|
||||
#[must_use]
|
||||
pub struct CommandPalette {
|
||||
id: ElementId,
|
||||
name: SharedString,
|
||||
@@ -261,10 +270,15 @@ impl CommandPaletteState {
|
||||
if self.matches.is_empty() {
|
||||
return;
|
||||
}
|
||||
let count = self.matches.len();
|
||||
let next = match self.highlight {
|
||||
Some(row) => (row as i32 + delta).rem_euclid(self.matches.len() as i32) as usize,
|
||||
Some(row) => match row.checked_add_signed(isize::try_from(delta).unwrap_or(0)) {
|
||||
Some(next) if next < count => next,
|
||||
Some(_) => 0,
|
||||
None => count - 1,
|
||||
},
|
||||
None if delta > 0 => 0,
|
||||
None => self.matches.len() - 1,
|
||||
None => count - 1,
|
||||
};
|
||||
self.highlight = Some(next);
|
||||
cx.notify();
|
||||
@@ -333,7 +347,7 @@ impl Render for CommandPaletteState {
|
||||
.map(|(row, command)| {
|
||||
let highlighted = self.highlight == Some(row);
|
||||
let state = state.clone();
|
||||
let row_ix = row;
|
||||
let row_index = row;
|
||||
|
||||
h_flex()
|
||||
.id((ElementId::from(command.id.clone()), "command"))
|
||||
@@ -341,6 +355,8 @@ impl Render for CommandPaletteState {
|
||||
let id = id.clone();
|
||||
move || format!("{id}-command-{row}")
|
||||
})
|
||||
.role(Role::ListBoxOption)
|
||||
.aria_label(command.label.clone())
|
||||
.gap_2()
|
||||
.px_2()
|
||||
.py_1()
|
||||
@@ -350,9 +366,9 @@ impl Render for CommandPaletteState {
|
||||
.on_mouse_move({
|
||||
let state = state.clone();
|
||||
move |_, _window, cx| {
|
||||
if state.read(cx).highlight != Some(row_ix) {
|
||||
if state.read(cx).highlight != Some(row_index) {
|
||||
state.update(cx, |state, cx| {
|
||||
state.highlight = Some(row_ix);
|
||||
state.highlight = Some(row_index);
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
@@ -361,7 +377,7 @@ impl Render for CommandPaletteState {
|
||||
.on_click({
|
||||
let state = state.clone();
|
||||
move |_, window, cx| {
|
||||
state.update(cx, |state, cx| state.choose_row(row_ix, window, cx));
|
||||
state.update(cx, |state, cx| state.choose_row(row_index, window, cx));
|
||||
}
|
||||
})
|
||||
.when_some(command.icon.clone(), |row, icon| {
|
||||
|
||||
Reference in New Issue
Block a user