Reset command palette selection and shortcut handling

This commit is contained in:
MihneaTeodorStoica 2025-11-08 17:34:59 +02:00
parent f18761068d
commit 412ef06ddb
3 changed files with 12 additions and 8 deletions

View file

@ -186,6 +186,11 @@ const STATIC_COMMANDS: &[StaticCommandDefinition] = &[
];
pub fn draw_command_palette(ctx: &mut Context, state: &mut State) {
if state.command_palette_reset_selection {
state.command_palette_selection = 0;
state.command_palette_reset_selection = false;
}
ctx.modal_begin("command-palette", loc(LocId::CommandPaletteTitle));
ctx.attr_focus_well();
ctx.attr_padding(Rect::three(1, 2, 1));
@ -228,6 +233,9 @@ pub fn draw_command_palette(ctx: &mut Context, state: &mut State) {
}
}
let activate_via_keyboard = visible_len != 0
&& (ctx.consume_shortcut(vk::RETURN) || ctx.consume_shortcut(kbmod::CTRL | vk::RETURN));
ctx.block_begin("results");
ctx.attr_padding(Rect::three(0, 0, 1));
@ -259,11 +267,6 @@ pub fn draw_command_palette(ctx: &mut Context, state: &mut State) {
entry.label
};
ctx.inherit_focus();
if idx == selection {
ctx.steal_focus();
}
if ctx.button("command-entry", label_text, ButtonStyle::default()) && entry.enabled {
selection = idx;
activate = Some(entry.action);
@ -272,9 +275,7 @@ pub fn draw_command_palette(ctx: &mut Context, state: &mut State) {
}
ctx.block_end();
if visible_len != 0
&& (ctx.consume_shortcut(vk::RETURN) || ctx.consume_shortcut(kbmod::CTRL | vk::RETURN))
{
if activate_via_keyboard {
if let Some(entry) = filtered.get(selection) {
if entry.enabled {
activate = Some(entry.action);

View file

@ -360,6 +360,7 @@ fn draw(ctx: &mut Context, state: &mut State) {
state.wants_command_palette = true;
state.command_palette_filter.clear();
state.command_palette_selection = 0;
state.command_palette_reset_selection = true;
ctx.needs_rerender();
return;
} else if key == kbmod::CTRL | vk::N {

View file

@ -185,6 +185,7 @@ pub struct State {
pub preferences: Preferences,
pub command_palette_filter: String,
pub command_palette_selection: usize,
pub command_palette_reset_selection: bool,
system_palette: [StraightRgba; INDEXED_COLORS_COUNT],
}
@ -243,6 +244,7 @@ impl State {
preferences,
command_palette_filter: String::new(),
command_palette_selection: 0,
command_palette_reset_selection: false,
system_palette: framebuffer::DEFAULT_THEME,
})
}