2016-06-30 03:56:12 +00:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
2016-06-23 16:48:31 +00:00
|
|
|
//! Handle input from glutin
|
|
|
|
//!
|
|
|
|
//! Certain key combinations should send some escape sequence back to the pty.
|
|
|
|
//! In order to figure that out, state about which modifier keys are pressed
|
|
|
|
//! needs to be tracked. Additionally, we need a bit of a state machine to
|
|
|
|
//! determine what to do when a non-modifier key is pressed.
|
|
|
|
//!
|
|
|
|
//! TODO would be nice to generalize this so it could work with other windowing
|
|
|
|
//! APIs
|
|
|
|
//!
|
|
|
|
//! TODO handling xmodmap would be good
|
2016-12-25 23:54:01 +00:00
|
|
|
use copypasta::{Clipboard, Load, Store};
|
2016-09-27 05:22:28 +00:00
|
|
|
use glutin::{ElementState, VirtualKeyCode, MouseButton};
|
2016-07-30 19:47:22 +00:00
|
|
|
use glutin::{Mods, mods};
|
2016-11-25 03:50:34 +00:00
|
|
|
use glutin::{TouchPhase, MouseScrollDelta};
|
2016-06-23 16:48:31 +00:00
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
use event::Notify;
|
2016-12-22 18:43:06 +00:00
|
|
|
use index::{Line, Column, Side, Location};
|
|
|
|
use selection::Selection;
|
2016-11-24 04:25:37 +00:00
|
|
|
use term::mode::{self, TermMode};
|
2016-12-12 06:02:03 +00:00
|
|
|
use term::{self, Term};
|
2016-12-26 23:33:27 +00:00
|
|
|
use event::Mouse;
|
2016-06-23 16:48:31 +00:00
|
|
|
|
|
|
|
/// Processes input from glutin.
|
|
|
|
///
|
|
|
|
/// An escape sequence may be emitted in case specific keys or key combinations
|
|
|
|
/// are activated.
|
|
|
|
///
|
|
|
|
/// TODO also need terminal state when processing input
|
2016-12-26 23:33:27 +00:00
|
|
|
pub struct Processor<'a, N: 'a> {
|
|
|
|
pub key_bindings: &'a [KeyBinding],
|
|
|
|
pub mouse_bindings: &'a [MouseBinding],
|
|
|
|
pub ctx: ActionContext<'a, N>,
|
2016-11-24 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub struct ActionContext<'a, N: 'a> {
|
|
|
|
pub notifier: &'a mut N,
|
|
|
|
pub terminal: &'a Term,
|
|
|
|
pub selection: &'a mut Selection,
|
|
|
|
pub mouse: &'a mut Mouse,
|
|
|
|
pub size_info: &'a term::SizeInfo,
|
2016-07-02 15:32:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
/// Describes a state and action to take in that state
|
2016-06-23 16:48:31 +00:00
|
|
|
///
|
2016-12-17 06:13:51 +00:00
|
|
|
/// This is the shared component of `MouseBinding` and `KeyBinding`
|
2016-11-15 17:38:50 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2016-12-27 00:00:27 +00:00
|
|
|
pub struct Binding<T> {
|
2016-06-23 16:48:31 +00:00
|
|
|
/// Modifier keys required to activate binding
|
2016-11-15 17:38:50 +00:00
|
|
|
pub mods: Mods,
|
|
|
|
|
2016-06-23 16:48:31 +00:00
|
|
|
/// String to send to pty if mods and mode match
|
2016-11-15 17:38:50 +00:00
|
|
|
pub action: Action,
|
|
|
|
|
2016-06-23 16:48:31 +00:00
|
|
|
/// Terminal mode required to activate binding
|
2016-11-15 17:38:50 +00:00
|
|
|
pub mode: TermMode,
|
|
|
|
|
2016-06-23 16:48:31 +00:00
|
|
|
/// excluded terminal modes where the binding won't be activated
|
2016-11-15 17:38:50 +00:00
|
|
|
pub notmode: TermMode,
|
|
|
|
|
2016-12-27 00:00:27 +00:00
|
|
|
/// This property is used as part of the trigger detection code.
|
|
|
|
///
|
|
|
|
/// For example, this might be a key like "G", or a mouse button.
|
|
|
|
pub trigger: T,
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 00:00:27 +00:00
|
|
|
/// Bindings that are triggered by a keyboard key
|
|
|
|
pub type KeyBinding = Binding<VirtualKeyCode>;
|
|
|
|
|
|
|
|
/// Bindings that are triggered by a mouse button
|
|
|
|
pub type MouseBinding = Binding<MouseButton>;
|
2016-11-15 17:38:50 +00:00
|
|
|
|
2016-12-27 00:00:27 +00:00
|
|
|
impl<T: Eq> Binding<T> {
|
2016-11-15 17:38:50 +00:00
|
|
|
#[inline]
|
|
|
|
fn is_triggered_by(
|
|
|
|
&self,
|
|
|
|
mode: &TermMode,
|
|
|
|
mods: &Mods,
|
2016-12-27 00:00:27 +00:00
|
|
|
input: &T
|
2016-11-15 17:38:50 +00:00
|
|
|
) -> bool {
|
2016-12-27 00:00:27 +00:00
|
|
|
// Check input first since bindings are stored in one big list. This is
|
2016-11-15 17:38:50 +00:00
|
|
|
// the most likely item to fail so prioritizing it here allows more
|
|
|
|
// checks to be short circuited.
|
2016-12-27 00:00:27 +00:00
|
|
|
self.trigger == *input &&
|
|
|
|
self.mode_matches(mode) &&
|
|
|
|
self.not_mode_matches(mode) &&
|
|
|
|
self.mods_match(mods)
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
2016-12-27 00:00:27 +00:00
|
|
|
}
|
2016-11-15 17:38:50 +00:00
|
|
|
|
2016-12-27 00:00:27 +00:00
|
|
|
impl<T> Binding<T> {
|
|
|
|
/// Execute the action associate with this binding
|
2016-11-15 17:38:50 +00:00
|
|
|
#[inline]
|
2016-12-26 23:33:27 +00:00
|
|
|
fn execute<'a, N: Notify>(&self, ctx: &mut ActionContext<'a, N>) {
|
2016-12-27 00:00:27 +00:00
|
|
|
self.action.execute(ctx)
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-12-27 00:00:27 +00:00
|
|
|
fn mode_matches(&self, mode: &TermMode) -> bool {
|
|
|
|
self.mode.is_empty() || mode.intersects(self.mode)
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-12-27 00:00:27 +00:00
|
|
|
fn not_mode_matches(&self, mode: &TermMode) -> bool {
|
|
|
|
self.notmode.is_empty() || !mode.intersects(self.notmode)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn mods_match(&self, mods: &Mods) -> bool {
|
|
|
|
self.mods.is_all() || *mods == self.mods
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2016-09-27 05:22:28 +00:00
|
|
|
pub enum Action {
|
|
|
|
/// Write an escape sequence
|
2016-11-15 17:38:50 +00:00
|
|
|
Esc(String),
|
2016-09-27 05:22:28 +00:00
|
|
|
|
|
|
|
/// Paste contents of system clipboard
|
|
|
|
Paste,
|
2016-09-27 15:27:39 +00:00
|
|
|
|
2016-12-25 23:54:01 +00:00
|
|
|
// Store current selection into clipboard
|
|
|
|
Copy,
|
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
/// Paste contents of selection buffer
|
|
|
|
PasteSelection,
|
2016-09-27 05:22:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
impl Action {
|
|
|
|
#[inline]
|
2016-12-25 23:54:01 +00:00
|
|
|
fn execute<'a, N: Notify>(&self, ctx: &mut ActionContext<'a, N>) {
|
2016-11-15 17:38:50 +00:00
|
|
|
match *self {
|
2016-12-26 23:33:27 +00:00
|
|
|
Action::Esc(ref s) => {
|
|
|
|
ctx.notifier.notify(s.clone().into_bytes())
|
|
|
|
},
|
2016-12-25 23:54:01 +00:00
|
|
|
Action::Copy => {
|
2016-12-27 03:52:37 +00:00
|
|
|
if let Some(selection) = ctx.selection.span() {
|
|
|
|
let buf = ctx.terminal.string_from_selection(&selection);
|
|
|
|
|
|
|
|
Clipboard::new()
|
|
|
|
.expect("get clipboard")
|
|
|
|
.store_primary(buf)
|
|
|
|
.expect("copy into clipboard");
|
|
|
|
}
|
2016-12-25 23:54:01 +00:00
|
|
|
},
|
2016-12-26 23:33:27 +00:00
|
|
|
Action::Paste |
|
|
|
|
Action::PasteSelection => {
|
2016-11-15 17:38:50 +00:00
|
|
|
let clip = Clipboard::new().expect("get clipboard");
|
|
|
|
clip.load_selection()
|
|
|
|
.map(|contents| {
|
2016-12-25 23:54:01 +00:00
|
|
|
if ctx.terminal.mode().contains(mode::BRACKETED_PASTE) {
|
|
|
|
ctx.notifier.notify(&b"\x1b[200~"[..]);
|
|
|
|
ctx.notifier.notify(contents.into_bytes());
|
|
|
|
ctx.notifier.notify(&b"\x1b[201~"[..]);
|
2016-11-28 22:39:37 +00:00
|
|
|
} else {
|
2016-12-25 23:54:01 +00:00
|
|
|
ctx.notifier.notify(contents.into_bytes());
|
2016-11-28 22:39:37 +00:00
|
|
|
}
|
2016-11-15 17:38:50 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|err| {
|
|
|
|
err_println!("Error getting clipboard contents: {}", err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&'static str> for Action {
|
|
|
|
fn from(s: &'static str) -> Action {
|
|
|
|
Action::Esc(s.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
impl<'a, N: Notify + 'a> Processor<'a, N> {
|
2016-11-24 04:25:37 +00:00
|
|
|
#[inline]
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn mouse_moved(&mut self, x: u32, y: u32) {
|
2016-11-24 04:25:37 +00:00
|
|
|
// Record mouse position within window. Pixel coordinates are *not*
|
|
|
|
// translated to grid coordinates here since grid coordinates are rarely
|
|
|
|
// needed and the mouse position updates frequently.
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.mouse.x = x;
|
|
|
|
self.ctx.mouse.y = y;
|
2016-12-12 06:02:03 +00:00
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
if let Some((line, column)) = self.ctx.size_info.pixels_to_coords(x as usize, y as usize) {
|
|
|
|
self.ctx.mouse.line = line;
|
|
|
|
self.ctx.mouse.column = column;
|
2016-12-22 18:43:06 +00:00
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
let cell_x = x as usize % self.ctx.size_info.cell_width as usize;
|
|
|
|
let half_cell_width = (self.ctx.size_info.cell_width / 2.0) as usize;
|
2016-12-22 18:43:06 +00:00
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.mouse.cell_side = if cell_x > half_cell_width {
|
2016-12-22 18:43:06 +00:00
|
|
|
Side::Right
|
|
|
|
} else {
|
|
|
|
Side::Left
|
|
|
|
};
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
if self.ctx.mouse.left_button_state == ElementState::Pressed &&
|
|
|
|
!self.ctx.terminal.mode().contains(mode::MOUSE_REPORT_CLICK)
|
2016-12-22 18:43:06 +00:00
|
|
|
{
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.selection.update(Location {
|
2016-12-22 18:43:06 +00:00
|
|
|
line: line,
|
|
|
|
col: column
|
2016-12-26 23:33:27 +00:00
|
|
|
}, self.ctx.mouse.cell_side);
|
2016-12-22 18:43:06 +00:00
|
|
|
}
|
2016-12-12 06:02:03 +00:00
|
|
|
}
|
2016-11-24 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn mouse_report(&mut self, button: u8) {
|
|
|
|
let (line, column) = (self.ctx.mouse.line, self.ctx.mouse.column);
|
2016-12-22 18:43:06 +00:00
|
|
|
|
|
|
|
if line < Line(223) && column < Column(223) {
|
|
|
|
let msg = vec![
|
|
|
|
'\x1b' as u8,
|
|
|
|
'[' as u8,
|
|
|
|
'M' as u8,
|
|
|
|
32 + button,
|
|
|
|
32 + 1 + column.0 as u8,
|
|
|
|
32 + 1 + line.0 as u8,
|
|
|
|
];
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.notifier.notify(msg);
|
2016-11-15 17:38:50 +00:00
|
|
|
}
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn on_mouse_press(&mut self) {
|
|
|
|
if self.ctx.terminal.mode().contains(mode::MOUSE_REPORT_CLICK) {
|
|
|
|
self.mouse_report(0);
|
2016-12-22 18:43:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.selection.clear();
|
2016-11-24 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn on_mouse_release(&mut self) {
|
|
|
|
if self.ctx.terminal.mode().contains(mode::MOUSE_REPORT_CLICK) {
|
|
|
|
self.mouse_report(3);
|
2016-12-22 18:43:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-24 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn on_mouse_wheel(&mut self, delta: MouseScrollDelta, phase: TouchPhase) {
|
2016-11-25 03:50:34 +00:00
|
|
|
match delta {
|
2016-12-12 04:00:00 +00:00
|
|
|
MouseScrollDelta::LineDelta(_columns, lines) => {
|
|
|
|
let code = if lines > 0.0 {
|
|
|
|
64
|
|
|
|
} else {
|
|
|
|
65
|
|
|
|
};
|
|
|
|
|
|
|
|
for _ in 0..(lines.abs() as usize) {
|
2016-12-26 23:33:27 +00:00
|
|
|
self.mouse_report(code);
|
2016-12-12 04:00:00 +00:00
|
|
|
}
|
2016-11-25 03:50:34 +00:00
|
|
|
},
|
|
|
|
MouseScrollDelta::PixelDelta(_x, y) => {
|
|
|
|
match phase {
|
|
|
|
TouchPhase::Started => {
|
|
|
|
// Reset offset to zero
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.mouse.scroll_px = 0;
|
2016-11-25 03:50:34 +00:00
|
|
|
},
|
|
|
|
TouchPhase::Moved => {
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.mouse.scroll_px += y as i32;
|
|
|
|
let height = self.ctx.size_info.cell_height as i32;
|
2016-11-25 03:50:34 +00:00
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
while self.ctx.mouse.scroll_px.abs() >= height {
|
|
|
|
let button = if self.ctx.mouse.scroll_px > 0 {
|
|
|
|
self.ctx.mouse.scroll_px -= height;
|
2016-11-25 03:50:34 +00:00
|
|
|
64
|
|
|
|
} else {
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.mouse.scroll_px += height;
|
2016-11-25 03:50:34 +00:00
|
|
|
65
|
|
|
|
};
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
self.mouse_report(button);
|
2016-11-25 03:50:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn mouse_input(&mut self, state: ElementState, button: MouseButton) {
|
2016-11-24 04:25:37 +00:00
|
|
|
if let MouseButton::Left = button {
|
|
|
|
// TODO handle state changes
|
2016-12-26 23:33:27 +00:00
|
|
|
if self.ctx.mouse.left_button_state != state {
|
|
|
|
self.ctx.mouse.left_button_state = state;
|
2016-11-24 04:25:37 +00:00
|
|
|
match state {
|
|
|
|
ElementState::Pressed => {
|
2016-12-26 23:33:27 +00:00
|
|
|
self.on_mouse_press();
|
2016-11-24 04:25:37 +00:00
|
|
|
},
|
|
|
|
ElementState::Released => {
|
2016-12-26 23:33:27 +00:00
|
|
|
self.on_mouse_release();
|
2016-11-24 04:25:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 05:22:28 +00:00
|
|
|
if let ElementState::Released = state {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
self.process_mouse_bindings(mods::NONE, button);
|
2016-09-27 05:22:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
pub fn process_key(
|
2016-09-27 05:22:28 +00:00
|
|
|
&mut self,
|
|
|
|
state: ElementState,
|
|
|
|
key: Option<VirtualKeyCode>,
|
|
|
|
mods: Mods,
|
2016-11-12 03:59:18 +00:00
|
|
|
string: Option<String>,
|
2016-09-27 05:22:28 +00:00
|
|
|
) {
|
2016-06-23 16:48:31 +00:00
|
|
|
if let Some(key) = key {
|
|
|
|
// Ignore release events
|
|
|
|
if state == ElementState::Released {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-26 23:33:27 +00:00
|
|
|
if self.process_key_bindings(mods, key) {
|
2016-11-15 17:38:50 +00:00
|
|
|
return;
|
2016-11-12 03:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't process a binding; print the provided character
|
|
|
|
if let Some(string) = string {
|
2016-12-26 23:33:27 +00:00
|
|
|
self.ctx.notifier.notify(string.into_bytes());
|
2016-12-27 03:52:37 +00:00
|
|
|
self.ctx.selection.clear();
|
2016-11-12 03:59:18 +00:00
|
|
|
}
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
/// Attempts to find a binding and execute its action
|
|
|
|
///
|
|
|
|
/// The provided mode, mods, and key must match what is allowed by a binding
|
|
|
|
/// for its action to be executed.
|
|
|
|
///
|
|
|
|
/// Returns true if an action is executed.
|
2016-12-26 23:33:27 +00:00
|
|
|
fn process_key_bindings(&mut self, mods: Mods, key: VirtualKeyCode) -> bool {
|
|
|
|
for binding in self.key_bindings {
|
|
|
|
if binding.is_triggered_by(self.ctx.terminal.mode(), &mods, &key) {
|
2016-11-15 17:38:50 +00:00
|
|
|
// binding was triggered; run the action
|
2016-12-26 23:33:27 +00:00
|
|
|
binding.execute(&mut self.ctx);
|
2016-11-15 17:38:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to find a binding and execute its action
|
|
|
|
///
|
|
|
|
/// The provided mode, mods, and key must match what is allowed by a binding
|
|
|
|
/// for its action to be executed.
|
|
|
|
///
|
|
|
|
/// Returns true if an action is executed.
|
2016-12-26 23:33:27 +00:00
|
|
|
fn process_mouse_bindings(&mut self, mods: Mods, button: MouseButton) -> bool {
|
|
|
|
for binding in self.mouse_bindings {
|
|
|
|
if binding.is_triggered_by(self.ctx.terminal.mode(), &mods, &button) {
|
2016-11-15 17:38:50 +00:00
|
|
|
// binding was triggered; run the action
|
2016-12-26 23:33:27 +00:00
|
|
|
binding.execute(&mut self.ctx);
|
2016-11-15 17:38:50 +00:00
|
|
|
return true;
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 03:59:18 +00:00
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
false
|
|
|
|
}
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-11-15 17:38:50 +00:00
|
|
|
use glutin::{mods, VirtualKeyCode};
|
2016-06-23 16:48:31 +00:00
|
|
|
|
2016-09-27 15:27:39 +00:00
|
|
|
use term::mode;
|
|
|
|
|
2016-12-27 00:00:27 +00:00
|
|
|
use super::{Action, Binding};
|
2016-06-23 16:48:31 +00:00
|
|
|
|
2016-11-15 17:38:50 +00:00
|
|
|
const KEY: VirtualKeyCode = VirtualKeyCode::Key0;
|
|
|
|
|
2016-06-23 16:48:31 +00:00
|
|
|
macro_rules! test_process_binding {
|
|
|
|
{
|
|
|
|
name: $name:ident,
|
|
|
|
binding: $binding:expr,
|
2016-12-27 00:00:27 +00:00
|
|
|
triggers: $triggers:expr,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: $mode:expr,
|
|
|
|
mods: $mods:expr
|
|
|
|
} => {
|
|
|
|
#[test]
|
|
|
|
fn $name() {
|
2016-12-27 00:00:27 +00:00
|
|
|
if $triggers {
|
|
|
|
assert!($binding.is_triggered_by(&$mode, &$mods, &KEY));
|
|
|
|
} else {
|
|
|
|
assert!(!$binding.is_triggered_by(&$mode, &$mods, &KEY));
|
|
|
|
}
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_nomode_shiftmod_require_shift,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::SHIFT, action: Action::from("\x1b[1;2D"), mode: mode::NONE, notmode: mode::NONE },
|
|
|
|
triggers: true,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: mode::NONE,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::SHIFT
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_nomode_nomod_require_shift,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::SHIFT, action: Action::from("\x1b[1;2D"), mode: mode::NONE, notmode: mode::NONE },
|
|
|
|
triggers: false,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: mode::NONE,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::NONE
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_nomode_controlmod,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::CONTROL, action: Action::from("\x1b[1;5D"), mode: mode::NONE, notmode: mode::NONE },
|
|
|
|
triggers: true,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: mode::NONE,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::CONTROL
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_nomode_nomod_require_not_appcursor,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::ANY, action: Action::from("\x1b[D"), mode: mode::NONE, notmode: mode::APP_CURSOR },
|
|
|
|
triggers: true,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: mode::NONE,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::NONE
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_appcursormode_nomod_require_appcursor,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::ANY, action: Action::from("\x1bOD"), mode: mode::APP_CURSOR, notmode: mode::NONE },
|
|
|
|
triggers: true,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: mode::APP_CURSOR,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::NONE
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_nomode_nomod_require_appcursor,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::ANY, action: Action::from("\x1bOD"), mode: mode::APP_CURSOR, notmode: mode::NONE },
|
|
|
|
triggers: false,
|
2016-06-23 16:48:31 +00:00
|
|
|
mode: mode::NONE,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::NONE
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|
2016-07-30 01:27:45 +00:00
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_appcursormode_appkeypadmode_nomod_require_appcursor,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::ANY, action: Action::from("\x1bOD"), mode: mode::APP_CURSOR, notmode: mode::NONE },
|
|
|
|
triggers: true,
|
2016-07-30 01:27:45 +00:00
|
|
|
mode: mode::APP_CURSOR | mode::APP_KEYPAD,
|
2016-07-30 19:47:22 +00:00
|
|
|
mods: mods::NONE
|
2016-07-30 01:27:45 +00:00
|
|
|
}
|
2016-09-27 15:27:39 +00:00
|
|
|
|
|
|
|
test_process_binding! {
|
|
|
|
name: process_binding_fail_with_extra_mods,
|
2016-12-27 00:00:27 +00:00
|
|
|
binding: Binding { trigger: KEY, mods: mods::SUPER, action: Action::from("arst"), mode: mode::NONE, notmode: mode::NONE },
|
|
|
|
triggers: false,
|
2016-09-27 15:27:39 +00:00
|
|
|
mode: mode::NONE,
|
|
|
|
mods: mods::SUPER | mods::ALT
|
|
|
|
}
|
2016-06-23 16:48:31 +00:00
|
|
|
}
|