From 412ef06ddb5da55e6b0a0000b418e111aaa83fc8 Mon Sep 17 00:00:00 2001 From: MihneaTeodorStoica Date: Sat, 8 Nov 2025 17:34:59 +0200 Subject: [PATCH] Reset command palette selection and shortcut handling --- src/bin/edit/draw_command_palette.rs | 17 +++++++++-------- src/bin/edit/main.rs | 1 + src/bin/edit/state.rs | 2 ++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/bin/edit/draw_command_palette.rs b/src/bin/edit/draw_command_palette.rs index 0805a67..9bfca6e 100644 --- a/src/bin/edit/draw_command_palette.rs +++ b/src/bin/edit/draw_command_palette.rs @@ -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); diff --git a/src/bin/edit/main.rs b/src/bin/edit/main.rs index 7fcbaa7..99791fd 100644 --- a/src/bin/edit/main.rs +++ b/src/bin/edit/main.rs @@ -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 { diff --git a/src/bin/edit/state.rs b/src/bin/edit/state.rs index 703a3c3..767dd33 100644 --- a/src/bin/edit/state.rs +++ b/src/bin/edit/state.rs @@ -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, }) }