From cc82e498a1ef4dbe7d51299df75da0ce25206e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sun, 29 Sep 2024 16:44:27 +0300 Subject: [PATCH] feat: add draft implementation for leader key --- frontends/rioterm/src/bindings/mod.rs | 28 +++++++++++++++++++-------- frontends/rioterm/src/screen/mod.rs | 2 +- rio-backend/src/config/bindings.rs | 1 + rio-backend/src/crosswords/mod.rs | 2 ++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/frontends/rioterm/src/bindings/mod.rs b/frontends/rioterm/src/bindings/mod.rs index cfc573ab83..3a9eb110cc 100644 --- a/frontends/rioterm/src/bindings/mod.rs +++ b/frontends/rioterm/src/bindings/mod.rs @@ -7,7 +7,7 @@ pub mod kitty_keyboard; use crate::crosswords::vi_mode::ViMotion; use crate::crosswords::Mode; use bitflags::bitflags; -use rio_backend::config::bindings::KeyBinding as ConfigKeyBinding; +use rio_backend::config::bindings::{Bindings, KeyBinding as ConfigKeyBinding}; use rio_backend::config::keyboard::Keyboard as ConfigKeyboard; use rio_window::event::MouseButton; use rio_window::keyboard::Key::*; @@ -162,6 +162,7 @@ bitflags! { const SEARCH = 0b0001_0000; const DISAMBIGUATE_KEYS = 0b0010_0000; const ALL_KEYS_AS_ESC = 0b0100_0000; + const LEADER = 0b1000_0000; } } @@ -181,6 +182,7 @@ impl BindingMode { mode.contains(Mode::REPORT_ALL_KEYS_AS_ESC), ); binding_mode.set(BindingMode::VI, mode.contains(Mode::VI)); + binding_mode.set(BindingMode::LEADER, mode.contains(Mode::LEADER)); binding_mode } } @@ -553,7 +555,7 @@ pub fn default_mouse_bindings() -> Vec { } pub fn default_key_bindings( - unprocessed_config_key_bindings: Vec, + unprocessed_config_key_bindings: Bindings, use_navigation_key_bindings: bool, config_keyboard: ConfigKeyboard, ) -> Vec { @@ -942,31 +944,41 @@ fn convert(config_key_binding: ConfigKeyBinding) -> Result { "~alt" => res_mode.not_mode |= BindingMode::ALT_SCREEN, "vi" => res_mode.mode |= BindingMode::VI, "~vi" => res_mode.not_mode |= BindingMode::VI, + "leader" => res_mode.mode |= BindingMode::LEADER, _ => { res_mode.not_mode |= BindingMode::empty(); res_mode.mode |= BindingMode::empty(); } } } - - Ok(KeyBinding { + let key_binding = KeyBinding { trigger, mods: res, action, mode: res_mode.mode, notmode: res_mode.not_mode, - }) + }; + + // panic!("Parsed key binding: {:?}", key_binding); + + Ok(key_binding) } pub fn config_key_bindings( - config_key_bindings: Vec, + config_key_bindings: Bindings, mut bindings: Vec, ) -> Vec { - if config_key_bindings.is_empty() { + if config_key_bindings.keys.is_empty() { return bindings; } - for ckb in config_key_bindings { + if let Some(leader) = config_key_bindings.leader { + let mut binding = convert(leader).unwrap(); + binding.mode |= BindingMode::LEADER; + bindings.push(binding); + } + + for ckb in config_key_bindings.keys { match convert(ckb) { Ok(key_binding) => match key_binding.action { Action::None | Action::ReceiveChar => { diff --git a/frontends/rioterm/src/screen/mod.rs b/frontends/rioterm/src/screen/mod.rs index 97dc3fc48a..4c4fe451d7 100644 --- a/frontends/rioterm/src/screen/mod.rs +++ b/frontends/rioterm/src/screen/mod.rs @@ -178,7 +178,7 @@ impl Screen<'_> { let renderer = Renderer::new(config, font_library); let bindings = crate::bindings::default_key_bindings( - config.bindings.keys.to_owned(), + config.bindings.to_owned(), config.navigation.has_navigation_key_bindings(), config.keyboard, ); diff --git a/rio-backend/src/config/bindings.rs b/rio-backend/src/config/bindings.rs index 714e116162..acfb0815d5 100644 --- a/rio-backend/src/config/bindings.rs +++ b/rio-backend/src/config/bindings.rs @@ -25,6 +25,7 @@ pub type KeyBindings = Vec; #[derive(Default, Debug, PartialEq, Clone, Serialize, Deserialize)] pub struct Bindings { pub keys: KeyBindings, + pub leader: Option, } #[cfg(test)] diff --git a/rio-backend/src/crosswords/mod.rs b/rio-backend/src/crosswords/mod.rs index 3770945418..aa805ebeb2 100644 --- a/rio-backend/src/crosswords/mod.rs +++ b/rio-backend/src/crosswords/mod.rs @@ -110,6 +110,8 @@ bitflags! { const SIXEL_DISPLAY = 1 << 28; const SIXEL_PRIV_PALETTE = 1 << 29; const SIXEL_CURSOR_TO_THE_RIGHT = 1 << 31; + const LEADER = 1 << 30; + const ANY = u32::MAX; } }