ui-patterns: PaletteOutput::refused names the disabled command Enter was pressed on, from the new CollectionEvent::Refused of a disabled lead's Enter, so an app can close the palette and say why: the palette stayed open and told the app nothing

This commit is contained in:
bflatastic committed 2026-11-24 00:57:53 +07:00
1 parent 662ff9264a
commit 85ba69b147
5 files changed
+35 -14

No files matched your search

@@ -232,6 +232,8 @@ pub enum CollectionEvent<K, E> {
},
/// Enter or a double click on an item.
Activate(K),
/// Enter on a disabled lead: nothing ran. A host that says why, or closes what the collection is in, reads it.
Refused(K),
/// The pointer has rested on the hovered lead row for the tooltip delay, or the lead moved by a key: the row's
/// key, once per lead change.
Rested(K),
@@ -150,7 +150,12 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
// A disabled lead does not run, as a press on it does not.
if activated
&& !header_held
&& let Some(row) = lead_at.filter(|&row| self.source.enabled(row))
&& let Some(row) = lead_at.filter(|&row| !self.source.enabled(row))
{
self.events.push(CollectionEvent::Refused(self.source.key(row)));
} else if activated
&& !header_held
&& let Some(row) = lead_at
{
// Where the arrows leave the selection, Enter picks the lead it activates.
if !props.arrows_select && !self.holds(row) {
+3
View File
@@ -649,6 +649,8 @@ pub enum CollectionKind {
Rested,
/// Enter or a double click on an item.
Activate,
/// Enter on a disabled lead.
Refused,
/// A double click on the empty space.
EmptyActivated,
/// A secondary click on the empty space.
@@ -691,6 +693,7 @@ impl CollectionKind {
CollectionEvent::Picked { .. } => CollectionKind::Picked,
CollectionEvent::LeadMoved { .. } => CollectionKind::LeadMoved,
CollectionEvent::Activate(_) => CollectionKind::Activate,
CollectionEvent::Refused(_) => CollectionKind::Refused,
CollectionEvent::Rested(_) => CollectionKind::Rested,
CollectionEvent::EmptyActivated => CollectionKind::EmptyActivated,
CollectionEvent::EmptyContextMenu { .. } => CollectionKind::EmptyContextMenu,
@@ -99,6 +99,9 @@ pub struct PaletteOutput {
pub closed: Option<CloseReason>,
/// The query the fallback row handed back, when the user picked it in this pass.
pub fallback: Option<String>,
/// The disabled command of the active row Enter was pressed on in this pass: nothing ran and the palette stays
/// open, its footer saying why. An app that closes the palette there and reports the reason itself reads it.
pub refused: Option<CommandRef>,
}
impl crate::contract::Outcome for PaletteOutput {
@@ -269,7 +272,7 @@ impl<'a> CommandPalette<'a> {
conclude(cx, &ctx, &chosen, &label, recent, &mut recents);
});
let closed = shown.output.closed;
PaletteOutput { response: shown.output.response, picked: chosen.picked, closed, fallback: chosen.fallback }
PaletteOutput { response: shown.output.response, picked: chosen.picked, closed, fallback: chosen.fallback, refused: chosen.refused }
}
fn show_in_place(self, ui: &mut Ui) -> PaletteOutput {
@@ -336,7 +339,7 @@ impl<'a> CommandPalette<'a> {
closed = popup.output.closed;
});
});
PaletteOutput { response: shown.output, picked: chosen.picked, closed, fallback: chosen.fallback }
PaletteOutput { response: shown.output, picked: chosen.picked, closed, fallback: chosen.fallback, refused: chosen.refused }
}
}
@@ -349,6 +352,7 @@ struct WasOpen(bool);
struct Chosen {
picked: Option<CommandRef>,
fallback: Option<String>,
refused: Option<CommandRef>,
}
/// What the list body reads, the same for both faces.
@@ -407,13 +411,13 @@ fn show_rows(ui: &mut Ui, input: &RowsInput<'_>, state: &mut Session, env: &crat
}
let events = list.show_items(ui, &source, &mut |key, _| rows[position(&listed.keys, *key)].row()).events;
let mut chosen = Chosen::default();
let command = |at: usize| input.entries.get(at).map(|entry| entry.command().clone());
for event in events {
if let CollectionEvent::Activate(key) = event {
match key {
Line::Entry(at) => chosen.picked = input.entries.get(at).map(|entry| entry.command().clone()),
Line::Fallback => chosen.fallback = Some(found.clone()),
Line::Heading | Line::Separator => {}
}
match event {
CollectionEvent::Activate(Line::Entry(at)) => chosen.picked = command(at),
CollectionEvent::Activate(Line::Fallback) => chosen.fallback = Some(found.clone()),
CollectionEvent::Refused(Line::Entry(at)) => chosen.refused = command(at),
_ => {}
}
}
let status = status_of(ui, input, &listed, &rows, state.selection.lead, found.is_empty(), env);
+12 -5
View File
@@ -38,6 +38,8 @@ struct Rig {
open: Rc<Cell<bool>>,
/// The last command picked.
picked: Rc<RefCell<Option<CommandRef>>>,
/// The last disabled command Enter was refused on.
refused: Rc<RefCell<Option<CommandRef>>>,
/// The bound `recents`, newest first; empty and unused when the rig keeps them in the palette.
recents: Rc<RefCell<Vec<CommandRef>>>,
}
@@ -47,9 +49,10 @@ struct Rig {
fn rig(entries: Vec<HelpEntry>, recents: Option<Vec<CommandRef>>, in_dialog: bool, beneath: impl Fn(&mut Ui) + 'static) -> Rig {
let open = Rc::new(Cell::new(true));
let picked = Rc::new(RefCell::new(None::<CommandRef>));
let refused = Rc::new(RefCell::new(None::<CommandRef>));
let bound = recents.is_some();
let held = Rc::new(RefCell::new(recents.unwrap_or_default()));
let (shown_open, shown_picked, shown_held) = (open.clone(), picked.clone(), held.clone());
let (shown_open, shown_picked, shown_refused, shown_held) = (open.clone(), picked.clone(), refused.clone(), held.clone());
let fixture = Fixture::new(move |ui| {
beneath(ui);
let show = |ui: &mut Ui| {
@@ -70,6 +73,9 @@ fn rig(entries: Vec<HelpEntry>, recents: Option<Vec<CommandRef>>, in_dialog: boo
if let Some(command) = output.picked {
*shown_picked.borrow_mut() = Some(command);
}
if let Some(command) = output.refused {
*shown_refused.borrow_mut() = Some(command);
}
};
if in_dialog {
Popover::new("Levels", Bind::given(true)).overlay(|_| Overlay::modal()).show(ui, |ui, _| show(ui));
@@ -77,7 +83,7 @@ fn rig(entries: Vec<HelpEntry>, recents: Option<Vec<CommandRef>>, in_dialog: boo
show(ui);
}
});
Rig { fixture, open, picked, recents: held }
Rig { fixture, open, picked, refused, recents: held }
}
/// A palette open over `entries`, inside a modal dialog when `in_dialog`; `picked` holds the last pick.
@@ -504,12 +510,12 @@ fn the_active_row_marks_its_shown_control_and_a_hidden_home_goes_to_the_footer()
}
/// Enter on a query whose first match is disabled runs nothing, though an enabled match follows it: the first match is
/// the active row, the one the footer explains. Guards against an active row that skipped the disabled first match, so
/// Enter ran the match after it.
/// the active row, the one the footer explains, and the output names it as refused so an app can close and say why.
/// Guards against an active row that skipped the disabled first match, so Enter ran the match after it.
#[trial]
fn enter_on_a_disabled_first_match_runs_nothing() {
let entries = vec![HelpEntry::new(EditCommands::Undo, "Undo").with_path(["Edit"]), HelpEntry::new(TrialCommand::Rotate, "Undo Rotation")];
let (mut fixture, open, picked) = palette(entries, false);
let Rig { mut fixture, open, picked, refused, .. } = rig(entries, None, false, |_| ());
fixture.command_state(EditCommands::Undo, CommandState::new().enabled(false));
fixture.settle();
fixture.type_text("undo").settle();
@@ -518,6 +524,7 @@ fn enter_on_a_disabled_first_match_runs_nothing() {
let _ = take_output(fixture.ctx());
fixture.key(Key::Enter, Modifiers::NONE).settle();
assert_eq!(*picked.borrow(), None, "Enter on the disabled first match picks nothing");
assert_eq!(*refused.borrow(), Some(CommandRef::from(EditCommands::Undo)), "the output names the refused command");
assert!(open.get(), "the palette stays open");
assert!(take_output(fixture.ctx()).fired.is_empty(), "nothing ran");
}