From 46845b86b4f3b8f55d68518a8a14d8e003d2d54c Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 20 Sep 2021 10:25:50 -0700 Subject: [PATCH] add modifiers and button selection to other mouse events --- src/bridge/ui_commands.rs | 12 +++-- src/window/window_wrapper/mouse_manager.rs | 52 +++++++++++++--------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/bridge/ui_commands.rs b/src/bridge/ui_commands.rs index 191432a..bb44216 100644 --- a/src/bridge/ui_commands.rs +++ b/src/bridge/ui_commands.rs @@ -25,6 +25,7 @@ pub enum UiCommand { action: String, grid_id: u64, position: (u32, u32), + modifier_string: String, }, Scroll { direction: String, @@ -33,8 +34,10 @@ pub enum UiCommand { modifier_string: String, }, Drag { + button: String, grid_id: u64, position: (u32, u32), + modifier_string: String, }, FileDrop(String), FocusLost, @@ -64,11 +67,12 @@ impl UiCommand { action, grid_id, position: (grid_x, grid_y), + modifier_string, } => { nvim.input_mouse( &button, &action, - "", + &modifier_string, grid_id as i64, grid_y as i64, grid_x as i64, @@ -94,13 +98,15 @@ impl UiCommand { .expect("Mouse Scroll Failed"); } UiCommand::Drag { + button, grid_id, position: (grid_x, grid_y), + modifier_string, } => { nvim.input_mouse( - "left", + &button, "drag", - "", + &modifier_string, grid_id as i64, grid_y as i64, grid_x as i64, diff --git a/src/window/window_wrapper/mouse_manager.rs b/src/window/window_wrapper/mouse_manager.rs index 99b0846..b5c20e9 100644 --- a/src/window/window_wrapper/mouse_manager.rs +++ b/src/window/window_wrapper/mouse_manager.rs @@ -40,10 +40,19 @@ fn to_grid_coords( ) } +fn mouse_button_to_button_text(mouse_button: &MouseButton) -> Option { + match mouse_button { + MouseButton::Left => Some("left".to_owned()), + MouseButton::Right => Some("right".to_owned()), + MouseButton::Middle => Some("middle".to_owned()), + _ => None, + } +} + pub struct MouseManager { command_sender: LoggingTx, - dragging: bool, + dragging: Option, drag_position: PhysicalPosition, has_moved: bool, @@ -60,7 +69,7 @@ impl MouseManager { pub fn new(command_sender: LoggingTx) -> MouseManager { MouseManager { command_sender, - dragging: false, + dragging: None, has_moved: false, position: PhysicalPosition::new(0, 0), relative_position: PhysicalPosition::new(0, 0), @@ -75,6 +84,7 @@ impl MouseManager { &mut self, x: i32, y: i32, + keyboard_manager: &KeyboardManager, renderer: &Renderer, windowed_context: &WindowedContext, ) { @@ -87,7 +97,7 @@ impl MouseManager { // If dragging, the relevant window (the one which we send all commands to) is the one // which the mouse drag started on. Otherwise its the top rendered window - let relevant_window_details = if self.dragging { + let relevant_window_details = if self.dragging.is_some() { renderer.window_regions.iter().find(|details| { details.id == self @@ -150,11 +160,13 @@ impl MouseManager { let has_moved = self.drag_position != previous_position; // If dragging and we haven't already sent a position, send a drag command - if self.dragging && has_moved { + if self.dragging.is_some() && has_moved { self.command_sender .send(UiCommand::Drag { + button: self.dragging.as_ref().unwrap().to_owned(), grid_id: relevant_window_details.id, position: self.drag_position.into(), + modifier_string: keyboard_manager.format_modifier_string(true), }) .ok(); } else { @@ -162,23 +174,16 @@ impl MouseManager { self.window_details_under_mouse = Some(relevant_window_details.clone()); } - self.has_moved = self.dragging && (self.has_moved || has_moved); + self.has_moved = self.dragging.is_some() && (self.has_moved || has_moved); } } - fn handle_pointer_transition(&mut self, mouse_button: &MouseButton, down: bool) { + fn handle_pointer_transition(&mut self, mouse_button: &MouseButton, down: bool, keyboard_manager: &KeyboardManager) { // For some reason pointer down is handled differently from pointer up and drag. // Floating windows: relative coordinates are great. // Non floating windows: rather than global coordinates, relative are needed if self.enabled { - let button_text = match mouse_button { - MouseButton::Left => Some("left"), - MouseButton::Right => Some("right"), - MouseButton::Middle => Some("middle"), - _ => None, - }; - - if let Some(button_text) = button_text { + if let Some(button_text) = mouse_button_to_button_text(mouse_button) { if let Some(details) = &self.window_details_under_mouse { let action = if down { "press".to_owned() @@ -194,20 +199,21 @@ impl MouseManager { self.command_sender .send(UiCommand::MouseButton { - button: button_text.to_string(), + button: button_text.clone(), action, grid_id: details.id, position: position.into(), + modifier_string: keyboard_manager.format_modifier_string(true), }) .ok(); } - } - } - self.dragging = down; + self.dragging = Some(button_text); - if !self.dragging { - self.has_moved = false; + if !self.dragging.is_some() { + self.has_moved = false; + } + } } } @@ -295,6 +301,7 @@ impl MouseManager { } => self.handle_pointer_motion( position.x as i32, position.y as i32, + keyboard_manager, renderer, windowed_context, ), @@ -321,7 +328,10 @@ impl MouseManager { Event::WindowEvent { event: WindowEvent::MouseInput { button, state, .. }, .. - } => self.handle_pointer_transition(button, state == &ElementState::Pressed), + } => self.handle_pointer_transition( + button, + state == &ElementState::Pressed, + keyboard_manager), _ => {} } }