From 07a2acb840cd588445a964060ca908ed9b28508b Mon Sep 17 00:00:00 2001 From: Autumn Lamonte Date: Sat, 1 Jan 2022 19:15:31 -0600 Subject: [PATCH] #1457 refactor, ClickPosition --- term/src/input.rs | 18 ++++++++---- term/src/terminalstate/mod.rs | 4 +-- term/src/terminalstate/mouse.rs | 18 ++++++------ termwiz/src/input.rs | 18 ++++++++---- wezterm-gui/src/termwindow/mouseevent.rs | 36 +++++++++++++++++------- 5 files changed, 64 insertions(+), 30 deletions(-) diff --git a/term/src/input.rs b/term/src/input.rs index 8c20a2099..931fc14ad 100644 --- a/term/src/input.rs +++ b/term/src/input.rs @@ -37,12 +37,20 @@ pub struct MouseEvent { pub kind: MouseEventKind, pub x: usize, pub y: VisibleRowIndex, - pub x_offset: usize, - pub y_offset: usize, + pub x_pixel_offset: usize, + pub y_pixel_offset: usize, pub button: MouseButton, pub modifiers: KeyModifiers, } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct ClickPosition { + pub column: usize, + pub row: i64, + pub x_pixel_offset: usize, + pub y_pixel_offset: usize, +} + /// This is a little helper that keeps track of the "click streak", /// which is the number of successive clicks of the same mouse button /// within the `CLICK_INTERVAL`. The streak is reset to 1 each time @@ -52,7 +60,7 @@ pub struct MouseEvent { #[derive(Debug, Clone)] pub struct LastMouseClick { pub button: MouseButton, - position: (usize, i64, usize, usize), + pub position: ClickPosition, time: Instant, pub streak: usize, } @@ -61,7 +69,7 @@ pub struct LastMouseClick { const CLICK_INTERVAL: u64 = 500; impl LastMouseClick { - pub fn new(button: MouseButton, position: (usize, i64, usize, usize)) -> Self { + pub fn new(button: MouseButton, position: ClickPosition) -> Self { Self { button, position, @@ -70,7 +78,7 @@ impl LastMouseClick { } } - pub fn add(&self, button: MouseButton, position: (usize, i64, usize, usize)) -> Self { + pub fn add(&self, button: MouseButton, position: ClickPosition) -> Self { let now = Instant::now(); let streak = if button == self.button && position == self.position diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs index bdbc81a64..1e552b79c 100644 --- a/term/src/terminalstate/mod.rs +++ b/term/src/terminalstate/mod.rs @@ -665,8 +665,8 @@ impl TerminalState { modifiers: KeyModifiers::NONE, x: 0, y: 0, - x_offset: 0, - y_offset: 0, + x_pixel_offset: 0, + y_pixel_offset: 0, }) .ok(); } diff --git a/term/src/terminalstate/mouse.rs b/term/src/terminalstate/mouse.rs index 3893cc973..089552fb4 100644 --- a/term/src/terminalstate/mouse.rs +++ b/term/src/terminalstate/mouse.rs @@ -69,8 +69,8 @@ impl TerminalState { self.writer, "\x1b[<{};{};{}M", button, - (event.x * (self.pixel_width / width)) + event.x_offset + 1, - (event.y as usize * (self.pixel_height / height)) + event.y_offset + 1 + (event.x * (self.pixel_width / width)) + event.x_pixel_offset + 1, + (event.y as usize * (self.pixel_height / height)) + event.y_pixel_offset + 1 )?; self.writer.flush()?; } else if self.mouse_tracking || self.button_event_mouse || self.any_event_mouse { @@ -123,8 +123,8 @@ impl TerminalState { self.writer, "\x1b[<{};{};{}M", button, - (event.x * (self.pixel_width / width)) + event.x_offset + 1, - (event.y as usize * (self.pixel_height / height)) + event.y_offset + 1 + (event.x * (self.pixel_width / width)) + event.x_pixel_offset + 1, + (event.y as usize * (self.pixel_height / height)) + event.y_pixel_offset + 1 )?; self.writer.flush()?; } else { @@ -162,8 +162,10 @@ impl TerminalState { self.writer, "\x1b[<{};{};{}m", release_button, - (event.x * (self.pixel_width / width)) + event.x_offset + 1, - (event.y as usize * (self.pixel_height / height)) + event.y_offset + 1 + (event.x * (self.pixel_width / width)) + event.x_pixel_offset + 1, + (event.y as usize * (self.pixel_height / height)) + + event.y_pixel_offset + + 1 )?; self.writer.flush()?; } else { @@ -214,8 +216,8 @@ impl TerminalState { self.writer, "\x1b[<{};{};{}M", button, - (event.x * (self.pixel_width / width)) + event.x_offset + 1, - (event.y as usize * (self.pixel_height / height)) + event.y_offset + 1 + (event.x * (self.pixel_width / width)) + event.x_pixel_offset + 1, + (event.y as usize * (self.pixel_height / height)) + event.y_pixel_offset + 1 )?; self.writer.flush()?; } else { diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index f78347a4c..200ad3c52 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -64,6 +64,7 @@ bitflags! { pub enum InputEvent { Key(KeyEvent), Mouse(MouseEvent), + PixelMouse(PixelMouseEvent), /// Detected that the user has resized the terminal Resized { cols: usize, @@ -85,6 +86,15 @@ pub struct MouseEvent { pub modifiers: Modifiers, } +#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PixelMouseEvent { + pub x_pixels: u16, + pub y_pixels: u16, + pub mouse_buttons: MouseButtons, + pub modifiers: Modifiers, +} + #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, PartialEq, Eq)] pub struct KeyEvent { @@ -1267,17 +1277,15 @@ impl InputParser { modifiers, })); } - // AZL - I don't quite understand this - // bit. Needs to be fixed up. MouseReport::SGR1016 { x_pixels, y_pixels, button, modifiers, } => { - callback(InputEvent::Mouse(MouseEvent { - x: x_pixels, - y: y_pixels, + callback(InputEvent::PixelMouse(PixelMouseEvent { + x_pixels: x_pixels, + y_pixels: y_pixels, mouse_buttons: button.into(), modifiers, })); diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs index 323217f96..773155d0e 100644 --- a/wezterm-gui/src/termwindow/mouseevent.rs +++ b/wezterm-gui/src/termwindow/mouseevent.rs @@ -14,7 +14,7 @@ use std::rc::Rc; use std::sync::Arc; use std::time::Duration; use wezterm_term::input::MouseEventKind as TMEK; -use wezterm_term::{LastMouseClick, StableRowIndex}; +use wezterm_term::{ClickPosition, LastMouseClick, StableRowIndex}; impl super::TermWindow { fn resolve_ui_item(&self, event: &MouseEvent) -> Option { @@ -87,7 +87,7 @@ impl super::TermWindow { } .trunc() as usize; - let y_offset = (event + let y_pixel_offset = (event .coords .y .sub(padding_top as isize) @@ -95,7 +95,7 @@ impl super::TermWindow { .max(0) % self.render_metrics.cell_size.height) as usize; - let x_offset = (event.coords.x.sub(padding_left as isize).max(0) + let x_pixel_offset = (event.coords.x.sub(padding_left as isize).max(0) % self.render_metrics.cell_size.width) as usize; self.last_mouse_coords = (x, y); @@ -118,8 +118,24 @@ impl super::TermWindow { let button = mouse_press_to_tmb(press); let click = match self.last_mouse_click.take() { - None => LastMouseClick::new(button, (x, y, x_offset, y_offset)), - Some(click) => click.add(button, (x, y, x_offset, y_offset)), + None => LastMouseClick::new( + button, + ClickPosition { + column: x, + row: y, + x_pixel_offset, + y_pixel_offset, + }, + ), + Some(click) => click.add( + button, + ClickPosition { + column: x, + row: y, + x_pixel_offset, + y_pixel_offset, + }, + ), }; self.last_mouse_click = Some(click); self.current_mouse_buttons.retain(|p| p != press); @@ -178,7 +194,7 @@ impl super::TermWindow { if let Some(item) = ui_item { self.mouse_event_ui_item(item, pane, y, event, context); } else { - self.mouse_event_terminal(pane, x, y, x_offset, y_offset, event, context); + self.mouse_event_terminal(pane, x, y, x_pixel_offset, y_pixel_offset, event, context); } } @@ -441,8 +457,8 @@ impl super::TermWindow { mut pane: Rc, mut x: usize, mut y: i64, - x_offset: usize, - y_offset: usize, + x_pixel_offset: usize, + y_pixel_offset: usize, event: MouseEvent, context: &dyn WindowOps, ) { @@ -702,8 +718,8 @@ impl super::TermWindow { }, x, y, - x_offset, - y_offset, + x_pixel_offset, + y_pixel_offset, modifiers: window_mods_to_termwiz_mods(event.modifiers), };