From d824d42d259a88a95124cd7787e02a0780ac75df Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 17:28:05 +0700 Subject: [PATCH] ui-patterns: a new palette query's active row is its first match, enabled or not, so Enter on a disabled first match runs nothing and the footer says why, rather than Enter running the next match --- .../src/patterns/panels/palette.rs | 6 ++++++ crates/ui-patterns/tests/it/panels/palette.rs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/ui-patterns/src/patterns/panels/palette.rs b/crates/ui-patterns/src/patterns/panels/palette.rs index 4862add60..37f6153e6 100644 --- a/crates/ui-patterns/src/patterns/panels/palette.rs +++ b/crates/ui-patterns/src/patterns/panels/palette.rs @@ -383,9 +383,15 @@ fn show_rows(ui: &mut Ui, input: &RowsInput<'_>, state: &mut Session, env: &crat Some(Line::Entry(at)) if state.stamp == stamp => Some(at), _ => None, }; + let fresh = state.stamp != stamp; state.stamp = stamp; let suggested = input.suggested.map(|(_, commands)| commands.as_slice()); let listed = layout(input, &state.query, suggested, active_before); + // A new query's active row is its first match, enabled or not: Enter on a disabled one runs nothing, and the footer + // says why, rather than Enter running the next match. + if fresh { + state.selection.lead = listed.keys.iter().copied().find(|key| matches!(key, Line::Entry(_))); + } let heading = input.suggested.map(|(label, _)| env.resolve(label).as_str().to_owned()).unwrap_or_default(); let fallback_text = input.fallback.map(|template| template.fill(&[("text", found.as_str())])).unwrap_or_default(); let rows = row_data(input.ctx, input.entries, &listed.keys, &heading, &fallback_text, env); diff --git a/crates/ui-patterns/tests/it/panels/palette.rs b/crates/ui-patterns/tests/it/panels/palette.rs index b128945cb..70f7ad786 100644 --- a/crates/ui-patterns/tests/it/panels/palette.rs +++ b/crates/ui-patterns/tests/it/panels/palette.rs @@ -502,3 +502,22 @@ fn the_active_row_marks_its_shown_control_and_a_hidden_home_goes_to_the_footer() rig.fixture.settle(); assert_eq!(painted(&rig.fixture, "Toolbox"), 2, "the footer names the home of a command shown nowhere"); } + +/// 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. +#[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); + fixture.command_state(EditCommands::Undo, CommandState::new().enabled(false)); + fixture.settle(); + fixture.type_text("undo").settle(); + let listed = rows(&fixture); + assert!(listed.len() == 2 && listed[0].0.starts_with("Undo") && listed[0].1 && !listed[1].1, "the disabled Undo matches first: {listed:?}"); + 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!(open.get(), "the palette stays open"); + assert!(take_output(fixture.ctx()).fired.is_empty(), "nothing ran"); +}