From 887cbb8e5d71b48897d04260ade0d5f08cfe2679 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 26 Oct 2019 10:19:44 -0700 Subject: [PATCH] factor keyassignment out from winit feature module --- src/config.rs | 3 +- src/frontend/guicommon/host.rs | 137 +--------------------------- src/frontend/guicommon/window.rs | 30 +----- src/frontend/software/termwindow.rs | 3 +- src/frontend/spawntabdomain.rs | 1 + src/keyassignment.rs | 128 ++++++++++++++++++++++++++ src/main.rs | 1 + 7 files changed, 136 insertions(+), 167 deletions(-) create mode 100644 src/frontend/spawntabdomain.rs create mode 100644 src/keyassignment.rs diff --git a/src/config.rs b/src/config.rs index 18e6462de..7265135d0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,9 +2,8 @@ use crate::create_user_owned_dirs; use crate::font::FontSystemSelection; -use crate::frontend::guicommon::host::KeyAssignment; -use crate::frontend::guicommon::window::SpawnTabDomain; use crate::frontend::FrontEndSelection; +use crate::keyassignment::{KeyAssignment, SpawnTabDomain}; use failure::{bail, err_msg, format_err, Error, Fallible}; use lazy_static::lazy_static; use portable_pty::{CommandBuilder, PtySystemSelection}; diff --git a/src/frontend/guicommon/host.rs b/src/frontend/guicommon/host.rs index 802c3f802..83cd19324 100644 --- a/src/frontend/guicommon/host.rs +++ b/src/frontend/guicommon/host.rs @@ -1,58 +1,23 @@ -#[cfg(feature = "enable-winit")] +#![cfg(feature = "enable-winit")] use super::window::TerminalWindow; -#[cfg(feature = "enable-winit")] use crate::font::{FontConfiguration, FontSystemSelection}; -#[cfg(feature = "enable-winit")] use crate::frontend::guicommon::clipboard::SystemClipboard; -use crate::frontend::guicommon::window::SpawnTabDomain; -#[cfg(feature = "enable-winit")] use crate::frontend::{front_end, gui_executor}; -#[cfg(feature = "enable-winit")] +use crate::keyassignment::{KeyAssignment, KeyMap}; use crate::mux::tab::Tab; use crate::mux::Mux; -#[cfg(feature = "enable-winit")] use failure::Error; -#[cfg(feature = "enable-winit")] use failure::Fallible; -#[cfg(feature = "enable-winit")] use log::error; -#[cfg(feature = "enable-winit")] use portable_pty::PtySize; -#[cfg(feature = "enable-winit")] use promise::Future; -use std::collections::HashMap; -#[cfg(feature = "enable-winit")] use std::ops::{Deref, DerefMut}; -#[cfg(feature = "enable-winit")] use std::rc::Rc; -#[cfg(feature = "enable-winit")] use std::sync::Arc; -#[cfg(feature = "enable-winit")] use term::terminal::Clipboard; use term::{KeyCode, KeyModifiers}; -#[cfg(feature = "enable-winit")] use termwiz::hyperlink::Hyperlink; -#[derive(Debug, Clone)] -pub enum KeyAssignment { - SpawnTab(SpawnTabDomain), - SpawnWindow, - ToggleFullScreen, - Copy, - Paste, - ActivateTabRelative(isize), - IncreaseFontSize, - DecreaseFontSize, - ResetFontSize, - ActivateTab(usize), - SendString(String), - Nop, - Hide, - Show, - CloseCurrentTab, -} - -#[cfg(feature = "enable-winit")] pub trait HostHelper { fn with_window Result<(), Error>>( &self, @@ -61,105 +26,12 @@ pub trait HostHelper { fn toggle_full_screen(&mut self); } -#[cfg(feature = "enable-winit")] pub struct HostImpl { helper: H, clipboard: Arc, keys: KeyMap, } -pub struct KeyMap(HashMap<(KeyCode, KeyModifiers), KeyAssignment>); - -impl KeyMap { - pub fn new() -> Self { - let mux = Mux::get().unwrap(); - let mut map = mux - .config() - .key_bindings() - .expect("keys section of config to be valid"); - - macro_rules! m { - ($([$mod:expr, $code:expr, $action:expr]),* $(,)?) => { - $( - map.entry(($code, $mod)).or_insert($action); - )* - }; - }; - - use KeyAssignment::*; - - // Apply the default bindings; if the user has already mapped - // a given entry then that will take precedence. - m!( - // Clipboard - [KeyModifiers::SUPER, KeyCode::Char('c'), Copy], - [KeyModifiers::SUPER, KeyCode::Char('v'), Paste], - [KeyModifiers::SHIFT, KeyCode::Insert, Paste], - // Window management - [KeyModifiers::SUPER, KeyCode::Char('m'), Hide], - [KeyModifiers::SUPER, KeyCode::Char('n'), SpawnWindow], - [KeyModifiers::ALT, KeyCode::Char('\n'), ToggleFullScreen], - [KeyModifiers::ALT, KeyCode::Char('\r'), ToggleFullScreen], - [KeyModifiers::ALT, KeyCode::Enter, ToggleFullScreen], - // Font size manipulation - [KeyModifiers::SUPER, KeyCode::Char('-'), DecreaseFontSize], - [KeyModifiers::CTRL, KeyCode::Char('-'), DecreaseFontSize], - [KeyModifiers::SUPER, KeyCode::Char('='), IncreaseFontSize], - [KeyModifiers::CTRL, KeyCode::Char('='), IncreaseFontSize], - [KeyModifiers::SUPER, KeyCode::Char('0'), ResetFontSize], - [KeyModifiers::CTRL, KeyCode::Char('0'), ResetFontSize], - // Tab navigation and management - [ - KeyModifiers::SUPER, - KeyCode::Char('t'), - SpawnTab(SpawnTabDomain::DefaultDomain) - ], - [ - KeyModifiers::SUPER | KeyModifiers::SHIFT, - KeyCode::Char('T'), - SpawnTab(SpawnTabDomain::CurrentTabDomain) - ], - [KeyModifiers::SUPER, KeyCode::Char('w'), CloseCurrentTab], - [KeyModifiers::SUPER, KeyCode::Char('1'), ActivateTab(0)], - [KeyModifiers::SUPER, KeyCode::Char('2'), ActivateTab(1)], - [KeyModifiers::SUPER, KeyCode::Char('3'), ActivateTab(2)], - [KeyModifiers::SUPER, KeyCode::Char('4'), ActivateTab(3)], - [KeyModifiers::SUPER, KeyCode::Char('5'), ActivateTab(4)], - [KeyModifiers::SUPER, KeyCode::Char('6'), ActivateTab(5)], - [KeyModifiers::SUPER, KeyCode::Char('7'), ActivateTab(6)], - [KeyModifiers::SUPER, KeyCode::Char('8'), ActivateTab(7)], - [KeyModifiers::SUPER, KeyCode::Char('9'), ActivateTab(8)], - [ - KeyModifiers::SUPER | KeyModifiers::SHIFT, - KeyCode::Char('['), - ActivateTabRelative(-1) - ], - [ - KeyModifiers::SUPER | KeyModifiers::SHIFT, - KeyCode::Char('{'), - ActivateTabRelative(-1) - ], - [ - KeyModifiers::SUPER | KeyModifiers::SHIFT, - KeyCode::Char(']'), - ActivateTabRelative(1) - ], - [ - KeyModifiers::SUPER | KeyModifiers::SHIFT, - KeyCode::Char('}'), - ActivateTabRelative(1) - ], - ); - - Self(map) - } - - pub fn lookup(&self, key: KeyCode, mods: KeyModifiers) -> Option { - self.0.get(&(key, mods)).cloned() - } -} - -#[cfg(feature = "enable-winit")] impl HostImpl { pub fn new(helper: H) -> Self { Self { @@ -298,7 +170,6 @@ impl HostImpl { } } -#[cfg(feature = "enable-winit")] impl Deref for HostImpl { type Target = H; fn deref(&self) -> &H { @@ -306,7 +177,6 @@ impl Deref for HostImpl { } } -#[cfg(feature = "enable-winit")] impl DerefMut for HostImpl { fn deref_mut(&mut self) -> &mut H { &mut self.helper @@ -316,20 +186,17 @@ impl DerefMut for HostImpl { /// Implements `TerminalHost` for a Tab. /// `TabHost` instances are short lived and borrow references to /// other state. -#[cfg(feature = "enable-winit")] pub struct TabHost<'a, H: HostHelper> { writer: &'a mut dyn std::io::Write, host: &'a mut HostImpl, } -#[cfg(feature = "enable-winit")] impl<'a, H: HostHelper> TabHost<'a, H> { pub fn new(writer: &'a mut dyn std::io::Write, host: &'a mut HostImpl) -> Self { Self { writer, host } } } -#[cfg(feature = "enable-winit")] impl<'a, H: HostHelper> term::TerminalHost for TabHost<'a, H> { fn writer(&mut self) -> &mut dyn std::io::Write { &mut self.writer diff --git a/src/frontend/guicommon/window.rs b/src/frontend/guicommon/window.rs index b68569c59..f8530cf47 100644 --- a/src/frontend/guicommon/window.rs +++ b/src/frontend/guicommon/window.rs @@ -1,45 +1,20 @@ -#[cfg(feature = "enable-winit")] +#![cfg(feature = "enable-winit")] use crate::config::Config; -#[cfg(feature = "enable-winit")] use crate::font::FontConfiguration; -use crate::mux::domain::DomainId; -#[cfg(feature = "enable-winit")] +use crate::keyassignment::SpawnTabDomain; use crate::mux::tab::{Tab, TabId}; -#[cfg(feature = "enable-winit")] use crate::mux::window::WindowId; -#[cfg(feature = "enable-winit")] use crate::mux::Mux; -#[cfg(feature = "enable-winit")] use crate::opengl::render::Renderer; -#[cfg(feature = "enable-winit")] use crate::opengl::textureatlas::OutOfTextureSpace; -#[cfg(feature = "enable-winit")] use glium; -#[cfg(feature = "enable-winit")] use portable_pty::PtySize; -#[cfg(feature = "enable-winit")] use std::rc::Rc; -#[cfg(feature = "enable-winit")] use std::sync::Arc; -/// When spawning a tab, specify which domain should be used to -/// host/spawn that tab. -#[derive(Debug, Clone)] -pub enum SpawnTabDomain { - /// Use the default domain - DefaultDomain, - /// Use the domain from the current tab in the associated window - CurrentTabDomain, - /// Use a specific domain by id - Domain(DomainId), - /// Use a specific domain by name - DomainName(String), -} - /// Reports the currently configured physical size of the display /// surface (physical pixels, not adjusted for dpi) and the current /// cell dimensions, also in physical pixels -#[cfg(feature = "enable-winit")] pub struct Dimensions { pub width: u16, pub height: u16, @@ -51,7 +26,6 @@ pub struct Dimensions { /// the different GUI systems. /// A number of methods need to be provided by the window in order to /// unlock the use of the provided methods towards the bottom of the trait. -#[cfg(feature = "enable-winit")] pub trait TerminalWindow { fn set_window_title(&mut self, title: &str) -> failure::Fallible<()>; fn get_mux_window_id(&self) -> WindowId; diff --git a/src/frontend/software/termwindow.rs b/src/frontend/software/termwindow.rs index acb9a3c91..6349e7e46 100644 --- a/src/frontend/software/termwindow.rs +++ b/src/frontend/software/termwindow.rs @@ -2,9 +2,8 @@ use crate::config::Config; use crate::config::TextStyle; use crate::font::{FontConfiguration, FontSystemSelection, GlyphInfo}; use crate::frontend::guicommon::clipboard::SystemClipboard; -use crate::frontend::guicommon::host::{KeyAssignment, KeyMap}; -use crate::frontend::guicommon::window::SpawnTabDomain; use crate::frontend::{front_end, gui_executor}; +use crate::keyassignment::{KeyAssignment, KeyMap, SpawnTabDomain}; use crate::mux::renderable::Renderable; use crate::mux::tab::{Tab, TabId}; use crate::mux::window::WindowId as MuxWindowId; diff --git a/src/frontend/spawntabdomain.rs b/src/frontend/spawntabdomain.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/src/frontend/spawntabdomain.rs @@ -0,0 +1 @@ + diff --git a/src/keyassignment.rs b/src/keyassignment.rs new file mode 100644 index 000000000..e8401ef7d --- /dev/null +++ b/src/keyassignment.rs @@ -0,0 +1,128 @@ +use crate::mux::domain::DomainId; +use crate::mux::Mux; +use std::collections::HashMap; +use term::{KeyCode, KeyModifiers}; + +/// When spawning a tab, specify which domain should be used to +/// host/spawn that tab. +#[derive(Debug, Clone)] +pub enum SpawnTabDomain { + /// Use the default domain + DefaultDomain, + /// Use the domain from the current tab in the associated window + CurrentTabDomain, + /// Use a specific domain by id + Domain(DomainId), + /// Use a specific domain by name + DomainName(String), +} + +#[derive(Debug, Clone)] +pub enum KeyAssignment { + SpawnTab(SpawnTabDomain), + SpawnWindow, + ToggleFullScreen, + Copy, + Paste, + ActivateTabRelative(isize), + IncreaseFontSize, + DecreaseFontSize, + ResetFontSize, + ActivateTab(usize), + SendString(String), + Nop, + Hide, + Show, + CloseCurrentTab, +} + +pub struct KeyMap(HashMap<(KeyCode, KeyModifiers), KeyAssignment>); + +impl KeyMap { + pub fn new() -> Self { + let mux = Mux::get().unwrap(); + let mut map = mux + .config() + .key_bindings() + .expect("keys section of config to be valid"); + + macro_rules! m { + ($([$mod:expr, $code:expr, $action:expr]),* $(,)?) => { + $( + map.entry(($code, $mod)).or_insert($action); + )* + }; + }; + + use KeyAssignment::*; + + // Apply the default bindings; if the user has already mapped + // a given entry then that will take precedence. + m!( + // Clipboard + [KeyModifiers::SUPER, KeyCode::Char('c'), Copy], + [KeyModifiers::SUPER, KeyCode::Char('v'), Paste], + [KeyModifiers::SHIFT, KeyCode::Insert, Paste], + // Window management + [KeyModifiers::SUPER, KeyCode::Char('m'), Hide], + [KeyModifiers::SUPER, KeyCode::Char('n'), SpawnWindow], + [KeyModifiers::ALT, KeyCode::Char('\n'), ToggleFullScreen], + [KeyModifiers::ALT, KeyCode::Char('\r'), ToggleFullScreen], + [KeyModifiers::ALT, KeyCode::Enter, ToggleFullScreen], + // Font size manipulation + [KeyModifiers::SUPER, KeyCode::Char('-'), DecreaseFontSize], + [KeyModifiers::CTRL, KeyCode::Char('-'), DecreaseFontSize], + [KeyModifiers::SUPER, KeyCode::Char('='), IncreaseFontSize], + [KeyModifiers::CTRL, KeyCode::Char('='), IncreaseFontSize], + [KeyModifiers::SUPER, KeyCode::Char('0'), ResetFontSize], + [KeyModifiers::CTRL, KeyCode::Char('0'), ResetFontSize], + // Tab navigation and management + [ + KeyModifiers::SUPER, + KeyCode::Char('t'), + SpawnTab(SpawnTabDomain::DefaultDomain) + ], + [ + KeyModifiers::SUPER | KeyModifiers::SHIFT, + KeyCode::Char('T'), + SpawnTab(SpawnTabDomain::CurrentTabDomain) + ], + [KeyModifiers::SUPER, KeyCode::Char('w'), CloseCurrentTab], + [KeyModifiers::SUPER, KeyCode::Char('1'), ActivateTab(0)], + [KeyModifiers::SUPER, KeyCode::Char('2'), ActivateTab(1)], + [KeyModifiers::SUPER, KeyCode::Char('3'), ActivateTab(2)], + [KeyModifiers::SUPER, KeyCode::Char('4'), ActivateTab(3)], + [KeyModifiers::SUPER, KeyCode::Char('5'), ActivateTab(4)], + [KeyModifiers::SUPER, KeyCode::Char('6'), ActivateTab(5)], + [KeyModifiers::SUPER, KeyCode::Char('7'), ActivateTab(6)], + [KeyModifiers::SUPER, KeyCode::Char('8'), ActivateTab(7)], + [KeyModifiers::SUPER, KeyCode::Char('9'), ActivateTab(8)], + [ + KeyModifiers::SUPER | KeyModifiers::SHIFT, + KeyCode::Char('['), + ActivateTabRelative(-1) + ], + [ + KeyModifiers::SUPER | KeyModifiers::SHIFT, + KeyCode::Char('{'), + ActivateTabRelative(-1) + ], + [ + KeyModifiers::SUPER | KeyModifiers::SHIFT, + KeyCode::Char(']'), + ActivateTabRelative(1) + ], + [ + KeyModifiers::SUPER | KeyModifiers::SHIFT, + KeyCode::Char('}'), + ActivateTabRelative(1) + ], + ); + + Self(map) + } + + pub fn lookup(&self, key: KeyCode, mods: KeyModifiers) -> Option { + self.0.get(&(key, mods)).cloned() + } +} diff --git a/src/main.rs b/src/main.rs index 9635dfd79..32f3ae3e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use std::sync::Arc; mod config; mod frontend; +mod keyassignment; mod mux; #[cfg(feature = "enable-winit")]