From 7d66bc761da2c88553cb36428a0d5d330b39a296 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 13 Apr 2023 14:18:13 -0700 Subject: [PATCH] termwiz::Modifiers is now wezterm_input_types::Modifiers --- termwiz/Cargo.toml | 4 +- termwiz/src/input.rs | 26 +-------- wezterm-gui/src/termwindow/keyevent.rs | 67 ++---------------------- wezterm-gui/src/termwindow/mod.rs | 4 +- wezterm-gui/src/termwindow/mouseevent.rs | 3 +- wezterm-input-types/Cargo.toml | 5 +- wezterm-input-types/src/lib.rs | 3 +- 7 files changed, 16 insertions(+), 96 deletions(-) diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index 82016e0bc..37d21a878 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -46,12 +46,12 @@ vtparse = { version="0.6.2", path="../vtparse" } wezterm-bidi = { path = "../bidi", version="0.2.1" } wezterm-blob-leases = { path = "../wezterm-blob-leases", version="0.1" } wezterm-color-types = { path = "../color-types", version="0.2" } -wezterm-input-types = { path = "../wezterm-input-types", version="0.1" } +wezterm-input-types = { path = "../wezterm-input-types", version="0.1", default-features=false } wezterm-dynamic = { path = "../wezterm-dynamic", version="0.1" } [features] widgets = ["cassowary", "fnv"] -use_serde = ["serde", "wezterm-color-types/use_serde", "wezterm-blob-leases/serde", "bitflags/serde"] +use_serde = ["serde", "wezterm-color-types/use_serde", "wezterm-blob-leases/serde", "bitflags/serde", "wezterm-input-types/serde"] use_image = ["image"] docs = ["widgets", "use_serde"] diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index ecf369475..a2693adee 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -12,6 +12,8 @@ use bitflags::bitflags; use serde::{Deserialize, Serialize}; use std::fmt::Write; +pub use wezterm_input_types::Modifiers; + pub const CSI: &str = "\x1b["; pub const SS3: &str = "\x1bO"; @@ -42,30 +44,6 @@ use winapi::um::wincon::{ WINDOW_BUFFER_SIZE_EVENT, WINDOW_BUFFER_SIZE_RECORD, }; -bitflags! { - #[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))] - #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] - pub struct Modifiers: u16 { - const NONE = 0; - const SHIFT = 1<<1; - const ALT = 1<<2; - const CTRL = 1<<3; - const SUPER = 1<<4; - const LEFT_ALT = 1<<5; - const RIGHT_ALT = 1<<6; - /// This is a virtual modifier used by wezterm - const LEADER = 1<<7; - const LEFT_CTRL = 1<<8; - const RIGHT_CTRL = 1<<9; - const LEFT_SHIFT = 1<<10; - const RIGHT_SHIFT = 1<<11; - const ENHANCED_KEY = 1<<12; - /// Not really a modifier, but a keyboard driver state - const CAPS_LOCK = 1<<13; - /// Not really a modifier, but a keyboard driver state - const NUM_LOCK = 1<<14; - } -} bitflags! { #[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Default, Clone, PartialEq, Eq)] diff --git a/wezterm-gui/src/termwindow/keyevent.rs b/wezterm-gui/src/termwindow/keyevent.rs index 1d80b9ec4..70a24b904 100644 --- a/wezterm-gui/src/termwindow/keyevent.rs +++ b/wezterm-gui/src/termwindow/keyevent.rs @@ -172,67 +172,6 @@ impl KeyTableState { } } -pub fn window_mods_to_termwiz_mods(modifiers: ::window::Modifiers) -> termwiz::input::Modifiers { - let mut result = termwiz::input::Modifiers::NONE; - - if modifiers.contains(::window::Modifiers::SHIFT) { - result.insert(termwiz::input::Modifiers::SHIFT); - } - if modifiers.contains(::window::Modifiers::LEFT_SHIFT) { - result.insert(termwiz::input::Modifiers::LEFT_SHIFT); - } - if modifiers.contains(::window::Modifiers::RIGHT_SHIFT) { - result.insert(termwiz::input::Modifiers::RIGHT_SHIFT); - } - - if modifiers.contains(::window::Modifiers::LEFT_ALT) { - result.insert(termwiz::input::Modifiers::ALT); - result.insert(termwiz::input::Modifiers::LEFT_ALT); - } - if modifiers.contains(::window::Modifiers::RIGHT_ALT) { - /* We DONT want to do this: we carry through RIGHT_ALT - * only for win32-input mode to track when AltGr was used, - * but we don't want that to be treated as regular ALT - * when encoding regular input for the terminal. - * - result.insert(termwiz::input::Modifiers::ALT); - */ - - // But we do want the positional mod - result.insert(termwiz::input::Modifiers::RIGHT_ALT); - } - if modifiers.contains(::window::Modifiers::ALT) { - result.insert(termwiz::input::Modifiers::ALT); - } - - if modifiers.contains(::window::Modifiers::CTRL) { - result.insert(termwiz::input::Modifiers::CTRL); - } - if modifiers.contains(::window::Modifiers::LEFT_CTRL) { - result.insert(termwiz::input::Modifiers::LEFT_CTRL); - } - if modifiers.contains(::window::Modifiers::RIGHT_CTRL) { - result.insert(termwiz::input::Modifiers::RIGHT_CTRL); - } - - if modifiers.contains(::window::Modifiers::SUPER) { - result.insert(termwiz::input::Modifiers::SUPER); - } - if modifiers.contains(::window::Modifiers::LEADER) { - result.insert(termwiz::input::Modifiers::LEADER); - } - if modifiers.contains(::window::Modifiers::ENHANCED_KEY) { - result.insert(termwiz::input::Modifiers::ENHANCED_KEY); - } - if modifiers.contains(::window::Modifiers::CAPS_LOCK) { - result.insert(termwiz::input::Modifiers::CAPS_LOCK); - } - if modifiers.contains(::window::Modifiers::NUM_LOCK) { - result.insert(termwiz::input::Modifiers::NUM_LOCK); - } - result -} - #[derive(Debug)] pub enum Key { Code(::termwiz::input::KeyCode), @@ -334,7 +273,7 @@ impl super::TermWindow { if only_key_bindings == OnlyKeyBindings::No { if let Some(modal) = self.get_modal() { if let Key::Code(term_key) = self.win_key_code_to_termwiz_key_code(keycode) { - let tw_raw_modifiers = window_mods_to_termwiz_mods(raw_modifiers); + let tw_raw_modifiers = raw_modifiers; match modal.key_down(term_key, tw_raw_modifiers, self) { Ok(true) => return true, Ok(false) => {} @@ -420,7 +359,7 @@ impl super::TermWindow { if bypass_compose { if let Key::Code(term_key) = self.win_key_code_to_termwiz_key_code(keycode) { - let tw_raw_modifiers = window_mods_to_termwiz_mods(raw_modifiers); + let tw_raw_modifiers = raw_modifiers; let mut did_encode = false; if let Some(key_event) = key_event { @@ -676,7 +615,7 @@ impl super::TermWindow { ); } - let modifiers = window_mods_to_termwiz_mods(window_key.modifiers); + let modifiers = window_key.modifiers; if self.process_key( &pane, diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index e6298b582..e650365f6 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -2521,8 +2521,8 @@ impl TermWindow { } SendString(s) => pane.writer().write_all(s.as_bytes())?, SendKey(key) => { - use keyevent::{window_mods_to_termwiz_mods, Key}; - let mods = window_mods_to_termwiz_mods(key.mods); + use keyevent::Key; + let mods = key.mods; if let Key::Code(key) = self.win_key_code_to_termwiz_key_code( &key.key.resolve(self.config.key_map_preference), ) { diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs index 88735f236..4e75572cb 100644 --- a/wezterm-gui/src/termwindow/mouseevent.rs +++ b/wezterm-gui/src/termwindow/mouseevent.rs @@ -1,5 +1,4 @@ use crate::tabbar::TabBarItem; -use crate::termwindow::keyevent::window_mods_to_termwiz_mods; use crate::termwindow::{ GuiWin, MouseCapture, PositionedSplit, ScrollHit, TermWindowNotif, UIItem, UIItemType, TMB, }; @@ -984,7 +983,7 @@ impl super::TermWindow { y: row, x_pixel_offset, y_pixel_offset, - modifiers: window_mods_to_termwiz_mods(event.modifiers), + modifiers: event.modifiers, }; if allow_action diff --git a/wezterm-input-types/Cargo.toml b/wezterm-input-types/Cargo.toml index 3ec2a217d..3abd6dfb6 100644 --- a/wezterm-input-types/Cargo.toml +++ b/wezterm-input-types/Cargo.toml @@ -10,5 +10,8 @@ edition = "2018" bitflags = "1.3" euclid = "0.22" lazy_static = "1.4" -serde = {version="1.0", features = ["rc", "derive"]} +serde = {version="1.0", features = ["rc", "derive"], optional=true} wezterm-dynamic = {path="../wezterm-dynamic"} + +[features] +default = ["serde"] diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 84f804995..e7c9bcdac 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -462,7 +462,8 @@ impl ToString for KeyCode { } bitflags! { - #[derive(Default, Deserialize, Serialize, FromDynamic, ToDynamic)] + #[cfg_attr(feature="serde", derive(Serialize, Deserialize))] + #[derive(Default, FromDynamic, ToDynamic)] #[dynamic(into="String", try_from="String")] pub struct Modifiers: u16 { const NONE = 0;