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

This commit is contained in:
bflatastic
2026-11-23 17:28:05 +07:00
parent 508faa1eb7
commit d824d42d25
2 changed files with 25 additions and 0 deletions
@@ -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);
@@ -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");
}