From 85ba69b147b17fba8cd2eb554e0e723cb5932cbc Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 00:57:53 +0700 Subject: [PATCH] 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 --- .../src/contract/collection/events.rs | 2 ++ crates/ui-contract/src/contract/items/keys.rs | 7 ++++++- crates/ui-contract/src/testing/story.rs | 3 +++ .../src/patterns/panels/palette.rs | 20 +++++++++++-------- crates/ui-patterns/tests/it/panels/palette.rs | 17 +++++++++++----- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/crates/ui-contract/src/contract/collection/events.rs b/crates/ui-contract/src/contract/collection/events.rs index b9d884319b..ecf778f440 100644 --- a/crates/ui-contract/src/contract/collection/events.rs +++ b/crates/ui-contract/src/contract/collection/events.rs @@ -232,6 +232,8 @@ pub enum CollectionEvent { }, /// 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), diff --git a/crates/ui-contract/src/contract/items/keys.rs b/crates/ui-contract/src/contract/items/keys.rs index a12afc00b8..bc2c1ad8fc 100644 --- a/crates/ui-contract/src/contract/items/keys.rs +++ b/crates/ui-contract/src/contract/items/keys.rs @@ -150,7 +150,12 @@ impl 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) { diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index d8619dd577..a4f8af6a50 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -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, diff --git a/crates/ui-patterns/src/patterns/panels/palette.rs b/crates/ui-patterns/src/patterns/panels/palette.rs index 37f6153e61..2ac09e7201 100644 --- a/crates/ui-patterns/src/patterns/panels/palette.rs +++ b/crates/ui-patterns/src/patterns/panels/palette.rs @@ -99,6 +99,9 @@ pub struct PaletteOutput { pub closed: Option, /// The query the fallback row handed back, when the user picked it in this pass. pub fallback: Option, + /// 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, } 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, fallback: Option, + refused: Option, } /// 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); diff --git a/crates/ui-patterns/tests/it/panels/palette.rs b/crates/ui-patterns/tests/it/panels/palette.rs index 70f7ad786b..ecfd5ea24a 100644 --- a/crates/ui-patterns/tests/it/panels/palette.rs +++ b/crates/ui-patterns/tests/it/panels/palette.rs @@ -38,6 +38,8 @@ struct Rig { open: Rc>, /// The last command picked. picked: Rc>>, + /// The last disabled command Enter was refused on. + refused: Rc>>, /// The bound `recents`, newest first; empty and unused when the rig keeps them in the palette. recents: Rc>>, } @@ -47,9 +49,10 @@ struct Rig { fn rig(entries: Vec, recents: Option>, in_dialog: bool, beneath: impl Fn(&mut Ui) + 'static) -> Rig { let open = Rc::new(Cell::new(true)); let picked = Rc::new(RefCell::new(None::)); + let refused = Rc::new(RefCell::new(None::)); 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, recents: Option>, 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, recents: Option>, 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"); }