1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-26 06:42:12 +03:00

factor keyassignment out from winit feature module

This commit is contained in:
Wez Furlong 2019-10-26 10:19:44 -07:00
parent 552368a0cf
commit 887cbb8e5d
7 changed files with 136 additions and 167 deletions

View File

@ -2,9 +2,8 @@
use crate::create_user_owned_dirs; use crate::create_user_owned_dirs;
use crate::font::FontSystemSelection; use crate::font::FontSystemSelection;
use crate::frontend::guicommon::host::KeyAssignment;
use crate::frontend::guicommon::window::SpawnTabDomain;
use crate::frontend::FrontEndSelection; use crate::frontend::FrontEndSelection;
use crate::keyassignment::{KeyAssignment, SpawnTabDomain};
use failure::{bail, err_msg, format_err, Error, Fallible}; use failure::{bail, err_msg, format_err, Error, Fallible};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use portable_pty::{CommandBuilder, PtySystemSelection}; use portable_pty::{CommandBuilder, PtySystemSelection};

View File

@ -1,58 +1,23 @@
#[cfg(feature = "enable-winit")] #![cfg(feature = "enable-winit")]
use super::window::TerminalWindow; use super::window::TerminalWindow;
#[cfg(feature = "enable-winit")]
use crate::font::{FontConfiguration, FontSystemSelection}; use crate::font::{FontConfiguration, FontSystemSelection};
#[cfg(feature = "enable-winit")]
use crate::frontend::guicommon::clipboard::SystemClipboard; use crate::frontend::guicommon::clipboard::SystemClipboard;
use crate::frontend::guicommon::window::SpawnTabDomain;
#[cfg(feature = "enable-winit")]
use crate::frontend::{front_end, gui_executor}; use crate::frontend::{front_end, gui_executor};
#[cfg(feature = "enable-winit")] use crate::keyassignment::{KeyAssignment, KeyMap};
use crate::mux::tab::Tab; use crate::mux::tab::Tab;
use crate::mux::Mux; use crate::mux::Mux;
#[cfg(feature = "enable-winit")]
use failure::Error; use failure::Error;
#[cfg(feature = "enable-winit")]
use failure::Fallible; use failure::Fallible;
#[cfg(feature = "enable-winit")]
use log::error; use log::error;
#[cfg(feature = "enable-winit")]
use portable_pty::PtySize; use portable_pty::PtySize;
#[cfg(feature = "enable-winit")]
use promise::Future; use promise::Future;
use std::collections::HashMap;
#[cfg(feature = "enable-winit")]
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
#[cfg(feature = "enable-winit")]
use std::rc::Rc; use std::rc::Rc;
#[cfg(feature = "enable-winit")]
use std::sync::Arc; use std::sync::Arc;
#[cfg(feature = "enable-winit")]
use term::terminal::Clipboard; use term::terminal::Clipboard;
use term::{KeyCode, KeyModifiers}; use term::{KeyCode, KeyModifiers};
#[cfg(feature = "enable-winit")]
use termwiz::hyperlink::Hyperlink; 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 { pub trait HostHelper {
fn with_window<F: Send + 'static + Fn(&mut dyn TerminalWindow) -> Result<(), Error>>( fn with_window<F: Send + 'static + Fn(&mut dyn TerminalWindow) -> Result<(), Error>>(
&self, &self,
@ -61,105 +26,12 @@ pub trait HostHelper {
fn toggle_full_screen(&mut self); fn toggle_full_screen(&mut self);
} }
#[cfg(feature = "enable-winit")]
pub struct HostImpl<H: HostHelper> { pub struct HostImpl<H: HostHelper> {
helper: H, helper: H,
clipboard: Arc<dyn Clipboard>, clipboard: Arc<dyn Clipboard>,
keys: KeyMap, 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<KeyAssignment> {
self.0.get(&(key, mods)).cloned()
}
}
#[cfg(feature = "enable-winit")]
impl<H: HostHelper> HostImpl<H> { impl<H: HostHelper> HostImpl<H> {
pub fn new(helper: H) -> Self { pub fn new(helper: H) -> Self {
Self { Self {
@ -298,7 +170,6 @@ impl<H: HostHelper> HostImpl<H> {
} }
} }
#[cfg(feature = "enable-winit")]
impl<H: HostHelper> Deref for HostImpl<H> { impl<H: HostHelper> Deref for HostImpl<H> {
type Target = H; type Target = H;
fn deref(&self) -> &H { fn deref(&self) -> &H {
@ -306,7 +177,6 @@ impl<H: HostHelper> Deref for HostImpl<H> {
} }
} }
#[cfg(feature = "enable-winit")]
impl<H: HostHelper> DerefMut for HostImpl<H> { impl<H: HostHelper> DerefMut for HostImpl<H> {
fn deref_mut(&mut self) -> &mut H { fn deref_mut(&mut self) -> &mut H {
&mut self.helper &mut self.helper
@ -316,20 +186,17 @@ impl<H: HostHelper> DerefMut for HostImpl<H> {
/// Implements `TerminalHost` for a Tab. /// Implements `TerminalHost` for a Tab.
/// `TabHost` instances are short lived and borrow references to /// `TabHost` instances are short lived and borrow references to
/// other state. /// other state.
#[cfg(feature = "enable-winit")]
pub struct TabHost<'a, H: HostHelper> { pub struct TabHost<'a, H: HostHelper> {
writer: &'a mut dyn std::io::Write, writer: &'a mut dyn std::io::Write,
host: &'a mut HostImpl<H>, host: &'a mut HostImpl<H>,
} }
#[cfg(feature = "enable-winit")]
impl<'a, H: HostHelper> TabHost<'a, H> { impl<'a, H: HostHelper> TabHost<'a, H> {
pub fn new(writer: &'a mut dyn std::io::Write, host: &'a mut HostImpl<H>) -> Self { pub fn new(writer: &'a mut dyn std::io::Write, host: &'a mut HostImpl<H>) -> Self {
Self { writer, host } Self { writer, host }
} }
} }
#[cfg(feature = "enable-winit")]
impl<'a, H: HostHelper> term::TerminalHost for TabHost<'a, H> { impl<'a, H: HostHelper> term::TerminalHost for TabHost<'a, H> {
fn writer(&mut self) -> &mut dyn std::io::Write { fn writer(&mut self) -> &mut dyn std::io::Write {
&mut self.writer &mut self.writer

View File

@ -1,45 +1,20 @@
#[cfg(feature = "enable-winit")] #![cfg(feature = "enable-winit")]
use crate::config::Config; use crate::config::Config;
#[cfg(feature = "enable-winit")]
use crate::font::FontConfiguration; use crate::font::FontConfiguration;
use crate::mux::domain::DomainId; use crate::keyassignment::SpawnTabDomain;
#[cfg(feature = "enable-winit")]
use crate::mux::tab::{Tab, TabId}; use crate::mux::tab::{Tab, TabId};
#[cfg(feature = "enable-winit")]
use crate::mux::window::WindowId; use crate::mux::window::WindowId;
#[cfg(feature = "enable-winit")]
use crate::mux::Mux; use crate::mux::Mux;
#[cfg(feature = "enable-winit")]
use crate::opengl::render::Renderer; use crate::opengl::render::Renderer;
#[cfg(feature = "enable-winit")]
use crate::opengl::textureatlas::OutOfTextureSpace; use crate::opengl::textureatlas::OutOfTextureSpace;
#[cfg(feature = "enable-winit")]
use glium; use glium;
#[cfg(feature = "enable-winit")]
use portable_pty::PtySize; use portable_pty::PtySize;
#[cfg(feature = "enable-winit")]
use std::rc::Rc; use std::rc::Rc;
#[cfg(feature = "enable-winit")]
use std::sync::Arc; 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 /// Reports the currently configured physical size of the display
/// surface (physical pixels, not adjusted for dpi) and the current /// surface (physical pixels, not adjusted for dpi) and the current
/// cell dimensions, also in physical pixels /// cell dimensions, also in physical pixels
#[cfg(feature = "enable-winit")]
pub struct Dimensions { pub struct Dimensions {
pub width: u16, pub width: u16,
pub height: u16, pub height: u16,
@ -51,7 +26,6 @@ pub struct Dimensions {
/// the different GUI systems. /// the different GUI systems.
/// A number of methods need to be provided by the window in order to /// 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. /// unlock the use of the provided methods towards the bottom of the trait.
#[cfg(feature = "enable-winit")]
pub trait TerminalWindow { pub trait TerminalWindow {
fn set_window_title(&mut self, title: &str) -> failure::Fallible<()>; fn set_window_title(&mut self, title: &str) -> failure::Fallible<()>;
fn get_mux_window_id(&self) -> WindowId; fn get_mux_window_id(&self) -> WindowId;

View File

@ -2,9 +2,8 @@ use crate::config::Config;
use crate::config::TextStyle; use crate::config::TextStyle;
use crate::font::{FontConfiguration, FontSystemSelection, GlyphInfo}; use crate::font::{FontConfiguration, FontSystemSelection, GlyphInfo};
use crate::frontend::guicommon::clipboard::SystemClipboard; 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::frontend::{front_end, gui_executor};
use crate::keyassignment::{KeyAssignment, KeyMap, SpawnTabDomain};
use crate::mux::renderable::Renderable; use crate::mux::renderable::Renderable;
use crate::mux::tab::{Tab, TabId}; use crate::mux::tab::{Tab, TabId};
use crate::mux::window::WindowId as MuxWindowId; use crate::mux::window::WindowId as MuxWindowId;

View File

@ -0,0 +1 @@

128
src/keyassignment.rs Normal file
View File

@ -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<KeyAssignment> {
self.0.get(&(key, mods)).cloned()
}
}

View File

@ -16,6 +16,7 @@ use std::sync::Arc;
mod config; mod config;
mod frontend; mod frontend;
mod keyassignment;
mod mux; mod mux;
#[cfg(feature = "enable-winit")] #[cfg(feature = "enable-winit")]