From 1f7b17e4d11cf413d06f969fab3446de7f8e8d60 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Wed, 17 Jan 2024 02:07:22 -0500 Subject: [PATCH 1/4] Add hotkey `ctrl-shift-w` to close tabs. --- src/main.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main.rs b/src/main.rs index 2de524d..345acf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1296,6 +1296,16 @@ impl Application for App { None } } + Event::Keyboard(KeyEvent::KeyPressed { + key_code: KeyCode::W, + modifiers, + }) => { + if modifiers == Modifiers::CTRL | Modifiers::SHIFT { + Some(Message::TabClose(None)) + } else { + None + } + } Event::Keyboard(KeyEvent::KeyPressed { key_code: KeyCode::V, modifiers, From f1d442dfeae78976af947255499a611d1df83b82 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Wed, 17 Jan 2024 22:58:04 -0500 Subject: [PATCH 2/4] Add hot keys to cycle tabs forward or backward Port of: https://github.com/pop-os/cosmic-files/pull/20 --- src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/main.rs b/src/main.rs index 345acf1..4a9346d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,6 +212,8 @@ pub enum Message { TabContextAction(segmented_button::Entity, Action), TabContextMenu(segmented_button::Entity, Option), TabNew, + TabPrev, + TabNext, TermEvent(segmented_button::Entity, TermEvent), TermEventTx(mpsc::Sender<(segmented_button::Entity, TermEvent)>), ToggleContextPage(ContextPage), @@ -1030,6 +1032,38 @@ impl Application for App { log::warn!("tried to create new tab before having event channel"); } }, + Message::TabNext => { + let len = self.tab_model.iter().count(); + // Next tab position. Wraps around to 0 (first tab) if the last tab is active. + let pos = self + .tab_model + .position(self.tab_model.active()) + .map(|i| (i as usize + 1) % len) + .expect("at least one tab is always open"); + + let entity = self.tab_model.iter().nth(pos); + if let Some(entity) = entity { + return self.update(Message::TabActivate(entity)); + } + } + Message::TabPrev => { + let pos = self + .tab_model + .position(self.tab_model.active()) + .and_then(|i| (i as usize).checked_sub(1)) + .unwrap_or_else(|| { + self.tab_model + .iter() + .count() + .checked_sub(1) + .unwrap_or_default() + }); + + let entity = self.tab_model.iter().nth(pos); + if let Some(entity) = entity { + return self.update(Message::TabActivate(entity)); + } + } Message::TermEvent(entity, event) => match event { TermEvent::Bell => { //TODO: audible or visible bell options? @@ -1306,6 +1340,14 @@ impl Application for App { None } } + Event::Keyboard(KeyEvent::KeyPressed { + key_code: key @ (KeyCode::PageUp | KeyCode::PageDown), + modifiers: Modifiers::CTRL, + }) => match key { + KeyCode::PageDown => Some(Message::TabPrev), + KeyCode::PageUp => Some(Message::TabNext), + _ => None, + }, Event::Keyboard(KeyEvent::KeyPressed { key_code: KeyCode::V, modifiers, From 9dffe2543e7c65935e62dbebd72a5946421e45db Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Thu, 18 Jan 2024 02:44:25 -0500 Subject: [PATCH 3/4] Add keyboard shortcut ctrl+[1-9] to select tabs `ctrl`+`N` is a common shortcut to switch to a specific tab. 1-8 switches to the exact tab or the last tab. `ctrl`+`9` always switches to the last tab. --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main.rs b/src/main.rs index 4a9346d..3353aef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -208,6 +208,7 @@ pub enum Message { SyntaxTheme(usize, bool), SystemThemeModeChange(cosmic_theme::ThemeMode), TabActivate(segmented_button::Entity), + TabActivateJump(usize), TabClose(Option), TabContextAction(segmented_button::Entity, Action), TabContextMenu(segmented_button::Entity, Option), @@ -950,6 +951,21 @@ impl Application for App { self.tab_model.activate(entity); return self.update_title(); } + Message::TabActivateJump(pos) => { + // Length is always at least one so there shouldn't be a division by zero + let len = self.tab_model.iter().count(); + // The typical pattern is that 1-8 selects tabs 1-8 while 9 selects the last tab + let pos = if pos >= 8 || pos > len - 1 { + len - 1 + } else { + pos % len + }; + + let entity = self.tab_model.iter().nth(pos); + if let Some(entity) = entity { + return self.update(Message::TabActivate(entity)); + } + } Message::TabClose(entity_opt) => { let entity = entity_opt.unwrap_or_else(|| self.tab_model.active()); @@ -1348,6 +1364,29 @@ impl Application for App { KeyCode::PageUp => Some(Message::TabNext), _ => None, }, + // Ctrl + N to switch tabs + Event::Keyboard(KeyEvent::KeyPressed { + key_code: + key @ (KeyCode::Key1 + | KeyCode::Key2 + | KeyCode::Key3 + | KeyCode::Key4 + | KeyCode::Key5 + | KeyCode::Key6 + | KeyCode::Key7 + | KeyCode::Key8 + | KeyCode::Key9 + | KeyCode::Key0), + modifiers: Modifiers::CTRL, + }) => { + // 0 to 9 + // Key1 is 0 and Key0 is 9 + // This does not seem to be platform specific according to iced's source + let code = key as u32 as usize; + debug_assert!(code <= 9); + + Some(Message::TabActivateJump(code)) + } Event::Keyboard(KeyEvent::KeyPressed { key_code: KeyCode::V, modifiers, From c2f06a975a8520e798e37728bb39c8b7de82cd42 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Thu, 18 Jan 2024 22:01:01 -0500 Subject: [PATCH 4/4] Add `shift` as a modifier to the new tab shortcuts --- src/main.rs | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3353aef..e7cd9f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1358,13 +1358,19 @@ impl Application for App { } Event::Keyboard(KeyEvent::KeyPressed { key_code: key @ (KeyCode::PageUp | KeyCode::PageDown), - modifiers: Modifiers::CTRL, - }) => match key { - KeyCode::PageDown => Some(Message::TabPrev), - KeyCode::PageUp => Some(Message::TabNext), - _ => None, - }, - // Ctrl + N to switch tabs + modifiers, + }) => { + if modifiers == Modifiers::CTRL | Modifiers::SHIFT { + match key { + KeyCode::PageDown => Some(Message::TabPrev), + KeyCode::PageUp => Some(Message::TabNext), + _ => None, + } + } else { + None + } + } + // Ctrl + Shift + N to jump to a tab Event::Keyboard(KeyEvent::KeyPressed { key_code: key @ (KeyCode::Key1 @@ -1375,17 +1381,20 @@ impl Application for App { | KeyCode::Key6 | KeyCode::Key7 | KeyCode::Key8 - | KeyCode::Key9 - | KeyCode::Key0), - modifiers: Modifiers::CTRL, + | KeyCode::Key9), + modifiers, }) => { - // 0 to 9 - // Key1 is 0 and Key0 is 9 - // This does not seem to be platform specific according to iced's source - let code = key as u32 as usize; - debug_assert!(code <= 9); + if modifiers == Modifiers::CTRL | Modifiers::SHIFT { + // 0 to 8 + // Key1 is 0 and Key9 is 8 + // This does not seem to be platform specific according to iced's source + let code = key as u32 as usize; + debug_assert!(code <= 8); - Some(Message::TabActivateJump(code)) + Some(Message::TabActivateJump(code)) + } else { + None + } } Event::Keyboard(KeyEvent::KeyPressed { key_code: KeyCode::V,