1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 14:22:37 +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::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};

View File

@ -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<F: Send + 'static + Fn(&mut dyn TerminalWindow) -> Result<(), Error>>(
&self,
@ -61,105 +26,12 @@ pub trait HostHelper {
fn toggle_full_screen(&mut self);
}
#[cfg(feature = "enable-winit")]
pub struct HostImpl<H: HostHelper> {
helper: H,
clipboard: Arc<dyn Clipboard>,
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> {
pub fn new(helper: H) -> Self {
Self {
@ -298,7 +170,6 @@ impl<H: HostHelper> HostImpl<H> {
}
}
#[cfg(feature = "enable-winit")]
impl<H: HostHelper> Deref for HostImpl<H> {
type Target = 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> {
fn deref_mut(&mut self) -> &mut H {
&mut self.helper
@ -316,20 +186,17 @@ impl<H: HostHelper> DerefMut for HostImpl<H> {
/// 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<H>,
}
#[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<H>) -> 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

View File

@ -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;

View File

@ -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;

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 frontend;
mod keyassignment;
mod mux;
#[cfg(feature = "enable-winit")]