Simplify definition of key bindings
This commit is contained in:
parent
2a020221fd
commit
ff06e6105d
2 changed files with 165 additions and 204 deletions
101
src/key_bind.rs
Normal file
101
src/key_bind.rs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
use cosmic::iced::keyboard::{KeyCode, Modifiers};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, fmt};
|
||||
|
||||
use crate::Action;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub enum Modifier {
|
||||
Super,
|
||||
Ctrl,
|
||||
Alt,
|
||||
Shift,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
pub struct KeyBind {
|
||||
pub modifiers: Vec<Modifier>,
|
||||
pub key_code: KeyCode,
|
||||
}
|
||||
|
||||
impl KeyBind {
|
||||
pub fn matches(&self, modifiers: Modifiers, key_code: KeyCode) -> bool {
|
||||
self.key_code == key_code
|
||||
&& modifiers.logo() == self.modifiers.contains(&Modifier::Super)
|
||||
&& modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
|
||||
&& modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
|
||||
&& modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for KeyBind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for modifier in self.modifiers.iter() {
|
||||
write!(f, "{:?} + ", modifier)?;
|
||||
}
|
||||
write!(f, "{:?}", self.key_code)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_binds() -> HashMap<KeyBind, Action> {
|
||||
let mut key_binds = HashMap::new();
|
||||
|
||||
macro_rules! bind {
|
||||
([$($modifier:ident),+ $(,)?], $key_code:ident, $action:ident) => {{
|
||||
key_binds.insert(
|
||||
KeyBind {
|
||||
modifiers: vec![$(Modifier::$modifier),+],
|
||||
key_code: KeyCode::$key_code,
|
||||
},
|
||||
Action::$action,
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
// Standard key bindings
|
||||
bind!([Ctrl, Shift], A, SelectAll);
|
||||
bind!([Ctrl, Shift], C, Copy);
|
||||
bind!([Ctrl, Shift], F, Find);
|
||||
bind!([Ctrl, Shift], T, TabNew);
|
||||
bind!([Ctrl, Shift], V, Paste);
|
||||
bind!([Ctrl, Shift], W, TabClose);
|
||||
|
||||
// Ctrl+Alt+D splits horizontally, Ctrl+Alt+R splits vertically, Ctrl+Shift+X maximizes split
|
||||
//TODO: Adjust bindings as desired by UX
|
||||
bind!([Ctrl, Alt], D, PaneSplitHorizontal);
|
||||
bind!([Ctrl, Alt], R, PaneSplitVertical);
|
||||
bind!([Ctrl, Shift], X, PaneToggleMaximized);
|
||||
|
||||
// Ctrl+Tab and Ctrl+Shift+Tab cycle through tabs
|
||||
// Ctrl+Tab is not a special key for terminals and is free to use
|
||||
bind!([Ctrl], Tab, TabNext);
|
||||
bind!([Ctrl, Shift], Tab, TabPrev);
|
||||
|
||||
// Ctrl+Shift+# activates tabs by index
|
||||
bind!([Ctrl, Shift], Key1, TabActivate0);
|
||||
bind!([Ctrl, Shift], Key2, TabActivate1);
|
||||
bind!([Ctrl, Shift], Key3, TabActivate2);
|
||||
bind!([Ctrl, Shift], Key4, TabActivate3);
|
||||
bind!([Ctrl, Shift], Key5, TabActivate4);
|
||||
bind!([Ctrl, Shift], Key6, TabActivate5);
|
||||
bind!([Ctrl, Shift], Key7, TabActivate6);
|
||||
bind!([Ctrl, Shift], Key8, TabActivate7);
|
||||
bind!([Ctrl, Shift], Key9, TabActivate8);
|
||||
|
||||
// Ctrl+0, Ctrl+-, and Ctrl+= are not special keys for terminals and are free to use
|
||||
bind!([Ctrl], Key0, ZoomReset);
|
||||
bind!([Ctrl], Minus, ZoomOut);
|
||||
bind!([Ctrl], Equals, ZoomIn);
|
||||
|
||||
// Ctrl+Arrows and Ctrl+HJKL move between splits
|
||||
bind!([Ctrl, Shift], Left, PaneFocusLeft);
|
||||
bind!([Ctrl, Shift], H, PaneFocusLeft);
|
||||
bind!([Ctrl, Shift], Down, PaneFocusDown);
|
||||
bind!([Ctrl, Shift], J, PaneFocusDown);
|
||||
bind!([Ctrl, Shift], Up, PaneFocusUp);
|
||||
bind!([Ctrl, Shift], K, PaneFocusUp);
|
||||
bind!([Ctrl, Shift], Right, PaneFocusRight);
|
||||
bind!([Ctrl, Shift], L, PaneFocusRight);
|
||||
|
||||
key_binds
|
||||
}
|
||||
268
src/main.rs
268
src/main.rs
|
|
@ -36,6 +36,9 @@ mod config;
|
|||
use icon_cache::IconCache;
|
||||
mod icon_cache;
|
||||
|
||||
use key_bind::{key_binds, KeyBind};
|
||||
mod key_bind;
|
||||
|
||||
mod localize;
|
||||
|
||||
use menu::menu_bar;
|
||||
|
|
@ -162,28 +165,68 @@ pub struct Flags {
|
|||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Action {
|
||||
Copy,
|
||||
Find,
|
||||
PaneFocusDown,
|
||||
PaneFocusLeft,
|
||||
PaneFocusRight,
|
||||
PaneFocusUp,
|
||||
PaneSplitHorizontal,
|
||||
PaneSplitVertical,
|
||||
PaneToggleMaximized,
|
||||
Paste,
|
||||
SelectAll,
|
||||
Settings,
|
||||
ShowHeaderBar(bool),
|
||||
TabActivate0,
|
||||
TabActivate1,
|
||||
TabActivate2,
|
||||
TabActivate3,
|
||||
TabActivate4,
|
||||
TabActivate5,
|
||||
TabActivate6,
|
||||
TabActivate7,
|
||||
TabActivate8,
|
||||
TabClose,
|
||||
TabNew,
|
||||
PaneSplitHorizontal,
|
||||
PaneSplitVertical,
|
||||
PaneToggleMaximized,
|
||||
TabNext,
|
||||
TabPrev,
|
||||
ZoomIn,
|
||||
ZoomOut,
|
||||
ZoomReset,
|
||||
}
|
||||
|
||||
impl Action {
|
||||
pub fn message(self, entity: segmented_button::Entity) -> Message {
|
||||
match self {
|
||||
Action::Copy => Message::Copy(Some(entity)),
|
||||
Action::Find => Message::Find(true),
|
||||
Action::PaneFocusDown => Message::PaneFocusAdjacent(pane_grid::Direction::Down),
|
||||
Action::PaneFocusLeft => Message::PaneFocusAdjacent(pane_grid::Direction::Left),
|
||||
Action::PaneFocusRight => Message::PaneFocusAdjacent(pane_grid::Direction::Right),
|
||||
Action::PaneFocusUp => Message::PaneFocusAdjacent(pane_grid::Direction::Up),
|
||||
Action::PaneSplitHorizontal => Message::PaneSplit(pane_grid::Axis::Horizontal),
|
||||
Action::PaneSplitVertical => Message::PaneSplit(pane_grid::Axis::Vertical),
|
||||
Action::PaneToggleMaximized => Message::PaneToggleMaximized,
|
||||
Action::Paste => Message::Paste(Some(entity)),
|
||||
Action::SelectAll => Message::SelectAll(Some(entity)),
|
||||
Action::Settings => Message::ToggleContextPage(ContextPage::Settings),
|
||||
Action::ShowHeaderBar(show_headerbar) => Message::ShowHeaderBar(show_headerbar),
|
||||
Action::TabActivate0 => Message::TabActivateJump(0),
|
||||
Action::TabActivate1 => Message::TabActivateJump(1),
|
||||
Action::TabActivate2 => Message::TabActivateJump(2),
|
||||
Action::TabActivate3 => Message::TabActivateJump(3),
|
||||
Action::TabActivate4 => Message::TabActivateJump(4),
|
||||
Action::TabActivate5 => Message::TabActivateJump(5),
|
||||
Action::TabActivate6 => Message::TabActivateJump(6),
|
||||
Action::TabActivate7 => Message::TabActivateJump(7),
|
||||
Action::TabActivate8 => Message::TabActivateJump(8),
|
||||
Action::TabClose => Message::TabClose(None),
|
||||
Action::TabNew => Message::TabNew,
|
||||
Action::PaneSplitVertical => Message::PaneSplit(pane_grid::Axis::Vertical),
|
||||
Action::PaneSplitHorizontal => Message::PaneSplit(pane_grid::Axis::Horizontal),
|
||||
Action::PaneToggleMaximized => Message::PaneToggleMaximized,
|
||||
Action::TabNext => Message::TabNext,
|
||||
Action::TabPrev => Message::TabPrev,
|
||||
Action::ZoomIn => Message::ZoomIn,
|
||||
Action::ZoomOut => Message::ZoomOut,
|
||||
Action::ZoomReset => Message::ZoomReset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -201,6 +244,7 @@ pub enum Message {
|
|||
DefaultDimFontWeight(usize),
|
||||
DefaultBoldFontWeight(usize),
|
||||
DefaultZoomStep(usize),
|
||||
Key(Modifiers, KeyCode),
|
||||
Find(bool),
|
||||
FindNext,
|
||||
FindPrevious,
|
||||
|
|
@ -257,6 +301,7 @@ pub struct App {
|
|||
pane_model: TerminalPaneGrid,
|
||||
config_handler: Option<cosmic_config::Config>,
|
||||
config: Config,
|
||||
key_binds: HashMap<KeyBind, Action>,
|
||||
app_themes: Vec<String>,
|
||||
font_names: Vec<String>,
|
||||
font_size_names: Vec<String>,
|
||||
|
|
@ -775,6 +820,7 @@ impl Application for App {
|
|||
pane_model,
|
||||
config_handler: flags.config_handler,
|
||||
config: flags.config,
|
||||
key_binds: key_binds(),
|
||||
app_themes,
|
||||
font_names,
|
||||
font_size_names,
|
||||
|
|
@ -950,6 +996,16 @@ impl Application for App {
|
|||
log::warn!("failed to find zoom step with index {}", index);
|
||||
}
|
||||
},
|
||||
Message::Key(modifiers, key_code) => {
|
||||
if let Some(tab_model) = self.pane_model.active() {
|
||||
let entity = tab_model.active();
|
||||
for (key_bind, action) in self.key_binds.iter() {
|
||||
if key_bind.matches(modifiers, key_code) {
|
||||
return self.update(action.message(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::Find(find) => {
|
||||
self.find = find;
|
||||
|
||||
|
|
@ -1453,205 +1509,9 @@ impl Application for App {
|
|||
Subscription::batch([
|
||||
event::listen_with(|event, _status| match event {
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::A,
|
||||
key_code,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::SelectAll(None))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::C,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::Copy(None))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::F,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::Find(true))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::T,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::TabNew)
|
||||
} else {
|
||||
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: key @ (KeyCode::PageUp | KeyCode::PageDown),
|
||||
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
|
||||
| KeyCode::Key2
|
||||
| KeyCode::Key3
|
||||
| KeyCode::Key4
|
||||
| KeyCode::Key5
|
||||
| KeyCode::Key6
|
||||
| KeyCode::Key7
|
||||
| KeyCode::Key8
|
||||
| KeyCode::Key9),
|
||||
modifiers,
|
||||
}) => {
|
||||
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))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::R,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::ALT {
|
||||
Some(Message::PaneSplit(pane_grid::Axis::Vertical))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::D,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::ALT {
|
||||
Some(Message::PaneSplit(pane_grid::Axis::Horizontal))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::X,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::PaneToggleMaximized)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Left,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Left))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Right,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Right))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Up,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Up))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Down,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Down))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::V,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
|
||||
Some(Message::Paste(None))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Equals,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL {
|
||||
Some(Message::ZoomIn)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Minus,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL {
|
||||
Some(Message::ZoomOut)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
key_code: KeyCode::Key0,
|
||||
modifiers,
|
||||
}) => {
|
||||
if modifiers == Modifiers::CTRL {
|
||||
Some(Message::ZoomReset)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}) => Some(Message::Key(modifiers, key_code)),
|
||||
Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => {
|
||||
Some(Message::Modifiers(modifiers))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue