From 90e0a759e8e55952ada459914f9958d50eab04fe Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Wed, 23 Nov 2016 20:25:37 -0800 Subject: Support normal mouse tracking mode This allows the user for eg clicking columnts in htop to sort. --- src/input.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 3 deletions(-) (limited to 'src/input.rs') diff --git a/src/input.rs b/src/input.rs index 9d70e772..2e90ee20 100644 --- a/src/input.rs +++ b/src/input.rs @@ -29,9 +29,11 @@ use copypasta::{Clipboard, Load}; use glutin::{ElementState, VirtualKeyCode, MouseButton}; use glutin::{Mods, mods}; +use index::{Line, Column}; use config::Config; use event_loop; -use term::mode::{TermMode}; +use term::mode::{self, TermMode}; +use term::Term; /// Processes input from glutin. /// @@ -43,6 +45,24 @@ use term::mode::{TermMode}; pub struct Processor { key_bindings: Vec, mouse_bindings: Vec, + mouse: Mouse, +} + +/// State of the mouse +pub struct Mouse { + x: u32, + y: u32, + left_button_state: ElementState, +} + +impl Default for Mouse { + fn default() -> Mouse { + Mouse { + x: 0, + y: 0, + left_button_state: ElementState::Pressed + } + } } /// Types that are notified of escape sequences from the input::Processor. @@ -219,23 +239,84 @@ impl Processor { Processor { key_bindings: config.key_bindings().to_vec(), mouse_bindings: config.mouse_bindings().to_vec(), + mouse: Mouse::default(), + } + } + + #[inline] + pub fn mouse_moved(&mut self, x: u32, y: u32) { + // 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. + self.mouse.x = x; + self.mouse.y = y; + } + + fn mouse_report( + &mut self, + button: u8, + notifier: &mut N, + terminal: &Term + ) { + if terminal.mode().contains(mode::MOUSE_REPORT_CLICK) { + let (line, column) = terminal.pixels_to_coords( + self.mouse.x as usize, + self.mouse.y as usize + ).unwrap(); + + 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, + ]; + + notifier.notify(msg); + } + } } + pub fn on_mouse_press(&mut self, notifier: &mut N, terminal: &Term) { + self.mouse_report(0, notifier, terminal); + } + + pub fn on_mouse_release(&mut self, notifier: &mut N, terminal: &Term) { + self.mouse_report(3, notifier, terminal); + } + pub fn mouse_input( &mut self, state: ElementState, button: MouseButton, notifier: &mut N, - mode: TermMode + terminal: &Term ) { + if let MouseButton::Left = button { + // TODO handle state changes + if self.mouse.left_button_state != state { + self.mouse.left_button_state = state; + match state { + ElementState::Pressed => { + self.on_mouse_press(notifier, terminal); + }, + ElementState::Released => { + self.on_mouse_release(notifier, terminal); + } + } + } + } + if let ElementState::Released = state { return; } Processor::process_mouse_bindings( &self.mouse_bindings[..], - mode, + *terminal.mode(), notifier, mods::NONE, button -- cgit