From d20e0c539be0b05d0283f8aee6b0aa040de38a03 Mon Sep 17 00:00:00 2001 From: panekj Date: Wed, 31 Aug 2022 14:17:47 +0200 Subject: [PATCH] fix: add CloseWindow command, properly handle window/app closing --- defaults/keymaps-macos.toml | 62 +++++++++++++++++++--------------- defaults/keymaps-nonmacos.toml | 56 ++++++++++++++++-------------- lapce-data/src/command.rs | 5 +++ lapce-data/src/data.rs | 7 ++++ lapce-ui/src/app.rs | 9 +++++ lapce-ui/src/window.rs | 6 +++- 6 files changed, 91 insertions(+), 54 deletions(-) diff --git a/defaults/keymaps-macos.toml b/defaults/keymaps-macos.toml index a6c5391c..ad26d0ef 100644 --- a/defaults/keymaps-macos.toml +++ b/defaults/keymaps-macos.toml @@ -1,3 +1,9 @@ +# --------------------------------- Window --------------------------------------------- + +[[keymaps]] +command = "window_close" +key = "Meta+Shift+W" + # --------------------------------- General -------------------------------------------- [[keymaps]] @@ -67,51 +73,51 @@ key = "meta+right" command = "line_end" mode = "i" -[[keymaps]] -key = "alt+backspace" -command = "delete_word_backward" +[[keymaps]] +key = "alt+backspace" +command = "delete_word_backward" mode = "i" -[[keymaps]] -key = "meta+backspace" -command = "delete_to_beginning_of_line" +[[keymaps]] +key = "meta+backspace" +command = "delete_to_beginning_of_line" mode = "i" -[[keymaps]] -key = "alt+delete" -command = "delete_word_forward" +[[keymaps]] +key = "alt+delete" +command = "delete_word_forward" mode = "i" -[[keymaps]] -key = "meta+|" -command = "match_pairs" +[[keymaps]] +key = "meta+|" +command = "match_pairs" mode = "i" -[[keymaps]] -key = "meta+/" -command = "toggle_line_comment" +[[keymaps]] +key = "meta+/" +command = "toggle_line_comment" -[[keymaps]] -key = "meta+]" -command = "indent_line" +[[keymaps]] +key = "meta+]" +command = "indent_line" -[[keymaps]] -key = "meta+[" -command = "outdent_line" +[[keymaps]] +key = "meta+[" +command = "outdent_line" -[[keymaps]] -key = "meta+a" -command = "select_all" +[[keymaps]] +key = "meta+a" +command = "select_all" -[[keymaps]] +[[keymaps]] key = "meta+enter" command = "new_line_below" -mode = "i" +mode = "i" -[[keymaps]] +[[keymaps]] key = "meta+shift+enter" command = "new_line_above" -mode = "i" +mode = "i" # ------------------------------------ Multi cursor ------------------------------------- diff --git a/defaults/keymaps-nonmacos.toml b/defaults/keymaps-nonmacos.toml index 302339ba..109f548a 100644 --- a/defaults/keymaps-nonmacos.toml +++ b/defaults/keymaps-nonmacos.toml @@ -1,3 +1,9 @@ +# --------------------------------- Window --------------------------------------------- + +[[keymaps]] +command = "close_window" +key = "Alt+F4" + # --------------------------------- General -------------------------------------------- [[keymaps]] @@ -62,46 +68,46 @@ key = "ctrl+left" command = "word_backward" mode = "i" -[[keymaps]] -key = "ctrl+backspace" -command = "delete_word_backward" +[[keymaps]] +key = "ctrl+backspace" +command = "delete_word_backward" mode = "i" -[[keymaps]] -key = "ctrl+delete" -command = "delete_word_forward" +[[keymaps]] +key = "ctrl+delete" +command = "delete_word_forward" mode = "i" -[[keymaps]] -key = "ctrl+|" -command = "match_pairs" +[[keymaps]] +key = "ctrl+|" +command = "match_pairs" mode = "i" -[[keymaps]] -key = "ctrl+/" -command = "toggle_line_comment" +[[keymaps]] +key = "ctrl+/" +command = "toggle_line_comment" -[[keymaps]] -key = "ctrl+]" -command = "indent_line" +[[keymaps]] +key = "ctrl+]" +command = "indent_line" -[[keymaps]] -key = "ctrl+[" -command = "outdent_line" +[[keymaps]] +key = "ctrl+[" +command = "outdent_line" -[[keymaps]] -key = "ctrl+a" -command = "select_all" +[[keymaps]] +key = "ctrl+a" +command = "select_all" -[[keymaps]] +[[keymaps]] key = "ctrl+enter" command = "new_line_below" -mode = "i" +mode = "i" -[[keymaps]] +[[keymaps]] key = "ctrl+shift+enter" command = "new_line_above" -mode = "i" +mode = "i" # ------------------------------------ Multi cursor ------------------------------------- diff --git a/lapce-data/src/command.rs b/lapce-data/src/command.rs index da557080..36a03c55 100644 --- a/lapce-data/src/command.rs +++ b/lapce-data/src/command.rs @@ -273,6 +273,10 @@ pub enum LapceWorkbenchCommand { #[strum(serialize = "new_window")] NewWindow, + #[strum(message = "Close Window")] + #[strum(serialize = "close_window")] + CloseWindow, + #[strum(message = "New File")] #[strum(serialize = "new_file")] NewFile, @@ -561,6 +565,7 @@ pub enum LapceUICommand { FilterItems, RestartToUpdate(PathBuf, ReleaseInfo), NewWindow(WindowId), + CloseWindow(WindowId), ReloadWindow, CloseBuffers(Vec), RequestPaintRect(Rect), diff --git a/lapce-data/src/data.rs b/lapce-data/src/data.rs index 492965ea..58d1961d 100644 --- a/lapce-data/src/data.rs +++ b/lapce-data/src/data.rs @@ -1332,6 +1332,13 @@ pub fn run_workbench_command( Target::Global, )); } + LapceWorkbenchCommand::CloseWindow => { + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::CloseWindow(self.window_id), + Target::Auto, + )); + } LapceWorkbenchCommand::ReloadWindow => { ctx.submit_command(Command::new( LAPCE_UI_COMMAND, diff --git a/lapce-ui/src/app.rs b/lapce-ui/src/app.rs index d4ea50bd..ceace286 100644 --- a/lapce-ui/src/app.rs +++ b/lapce-ui/src/app.rs @@ -472,6 +472,15 @@ fn command( ctx.new_window(desc); return druid::Handled::Yes; } + LapceUICommand::CloseWindow(window_id) => { + ctx.submit_command(Command::new( + druid::commands::CLOSE_WINDOW, + (), + Target::Window(*window_id), + )); + let _ = data.db.save_app(data); + return druid::Handled::Yes; + } _ => (), } } diff --git a/lapce-ui/src/window.rs b/lapce-ui/src/window.rs index ece10217..4a90c465 100644 --- a/lapce-ui/src/window.rs +++ b/lapce-ui/src/window.rs @@ -829,7 +829,11 @@ pub fn window_controls( commands.push(( close_rect, - Command::new(druid::commands::QUIT_APP, (), Target::Global), + Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::CloseWindow(window_id), + Target::Auto, + ), )); let hover_color = {