From 7a886a3093e9e423841fb1e2b02fc32b367afcd9 Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Thu, 24 Mar 2022 10:11:58 +0000 Subject: [PATCH] esc on settings --- lapce-data/src/command.rs | 2 ++ lapce-data/src/settings.rs | 12 ++++++++---- lapce-ui/src/keymap.rs | 1 + lapce-ui/src/settings.rs | 29 +++++++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lapce-data/src/command.rs b/lapce-data/src/command.rs index 2224a33a..1ea42419 100644 --- a/lapce-data/src/command.rs +++ b/lapce-data/src/command.rs @@ -574,6 +574,8 @@ pub enum LapceUICommand { CancelPalette, ShowCodeActions, CancelCodeActions, + Hide, + ResignFocus, Focus, EnsureEditorTabActiveVisble, FocusSourceControl, diff --git a/lapce-data/src/settings.rs b/lapce-data/src/settings.rs index 0494762e..0fa1a946 100644 --- a/lapce-data/src/settings.rs +++ b/lapce-data/src/settings.rs @@ -1,7 +1,7 @@ -use druid::{Env, EventCtx, Modifiers, WidgetId}; +use druid::{Command, Env, EventCtx, Modifiers, Target, WidgetId}; use crate::{ - command::{CommandExecuted, LapceCommand}, + command::{CommandExecuted, LapceCommand, LapceUICommand, LAPCE_UI_COMMAND}, keypress::KeyPressFocus, state::Mode, }; @@ -36,14 +36,18 @@ fn check_condition(&self, condition: &str) -> bool { fn run_command( &mut self, - _ctx: &mut EventCtx, + ctx: &mut EventCtx, command: &LapceCommand, _count: Option, _mods: Modifiers, _env: &Env, ) -> CommandExecuted { if let LapceCommand::ModalClose = command { - self.shown = false; + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::Hide, + Target::Widget(self.panel_widget_id), + )); CommandExecuted::Yes } else { CommandExecuted::No diff --git a/lapce-ui/src/keymap.rs b/lapce-ui/src/keymap.rs index 46046c2a..81edbf6f 100644 --- a/lapce-ui/src/keymap.rs +++ b/lapce-ui/src/keymap.rs @@ -143,6 +143,7 @@ fn event( } keys.push(keypress); ctx.request_paint(); + ctx.set_handled(); } } else { let mut keypress = data.keypress.clone(); diff --git a/lapce-ui/src/settings.rs b/lapce-ui/src/settings.rs index 53bfbbea..d063b547 100644 --- a/lapce-ui/src/settings.rs +++ b/lapce-ui/src/settings.rs @@ -13,7 +13,10 @@ use inflector::Inflector; use lapce_data::{ buffer::{Buffer, BufferContent}, - command::{CommandExecuted, LapceCommand, LapceUICommand, LAPCE_UI_COMMAND}, + command::{ + CommandExecuted, LapceCommand, LapceUICommand, LAPCE_NEW_COMMAND, + LAPCE_UI_COMMAND, + }, config::{EditorConfig, LapceConfig, LapceTheme}, data::{LapceEditorData, LapceTabData}, keypress::KeyPressFocus, @@ -114,6 +117,7 @@ fn mouse_down( } ctx.set_handled(); + ctx.request_focus(); if self.switcher_rect.contains(mouse_event.pos) { let index = ((mouse_event.pos.y - self.switcher_rect.y0) / self.switcher_line_height) @@ -145,7 +149,16 @@ fn event( if !data.settings.shown { return; } - self.children[self.active].event(ctx, event, data, env); + match event { + Event::Command(cmd) if cmd.is(LAPCE_UI_COMMAND) => {} + Event::Command(cmd) if cmd.is(LAPCE_NEW_COMMAND) => {} + _ => { + self.children[self.active].event(ctx, event, data, env); + } + } + if ctx.is_handled() { + return; + } match event { Event::KeyDown(key_event) => { let mut keypress = data.keypress.clone(); @@ -181,11 +194,23 @@ fn event( let command = cmd.get_unchecked(LAPCE_UI_COMMAND); match command { LapceUICommand::ShowSettings => { + ctx.request_focus(); self.active = 0; } LapceUICommand::ShowKeybindings => { + ctx.request_focus(); self.active = 2; } + LapceUICommand::Hide => { + Arc::make_mut(&mut data.settings).shown = false; + if let Some(active) = *data.main_split.active { + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::Focus, + Target::Widget(active), + )); + } + } _ => (), } }