add modifiers and button selection to other mouse events

macos-click-through
Keith Simmons 3 years ago committed by Keith Simmons
parent abd3d52ae3
commit 46845b86b4

@ -25,6 +25,7 @@ pub enum UiCommand {
action: String, action: String,
grid_id: u64, grid_id: u64,
position: (u32, u32), position: (u32, u32),
modifier_string: String,
}, },
Scroll { Scroll {
direction: String, direction: String,
@ -33,8 +34,10 @@ pub enum UiCommand {
modifier_string: String, modifier_string: String,
}, },
Drag { Drag {
button: String,
grid_id: u64, grid_id: u64,
position: (u32, u32), position: (u32, u32),
modifier_string: String,
}, },
FileDrop(String), FileDrop(String),
FocusLost, FocusLost,
@ -64,11 +67,12 @@ impl UiCommand {
action, action,
grid_id, grid_id,
position: (grid_x, grid_y), position: (grid_x, grid_y),
modifier_string,
} => { } => {
nvim.input_mouse( nvim.input_mouse(
&button, &button,
&action, &action,
"", &modifier_string,
grid_id as i64, grid_id as i64,
grid_y as i64, grid_y as i64,
grid_x as i64, grid_x as i64,
@ -94,13 +98,15 @@ impl UiCommand {
.expect("Mouse Scroll Failed"); .expect("Mouse Scroll Failed");
} }
UiCommand::Drag { UiCommand::Drag {
button,
grid_id, grid_id,
position: (grid_x, grid_y), position: (grid_x, grid_y),
modifier_string,
} => { } => {
nvim.input_mouse( nvim.input_mouse(
"left", &button,
"drag", "drag",
"", &modifier_string,
grid_id as i64, grid_id as i64,
grid_y as i64, grid_y as i64,
grid_x as i64, grid_x as i64,

@ -40,10 +40,19 @@ fn to_grid_coords(
) )
} }
fn mouse_button_to_button_text(mouse_button: &MouseButton) -> Option<String> {
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 { pub struct MouseManager {
command_sender: LoggingTx<UiCommand>, command_sender: LoggingTx<UiCommand>,
dragging: bool, dragging: Option<String>,
drag_position: PhysicalPosition<u32>, drag_position: PhysicalPosition<u32>,
has_moved: bool, has_moved: bool,
@ -60,7 +69,7 @@ impl MouseManager {
pub fn new(command_sender: LoggingTx<UiCommand>) -> MouseManager { pub fn new(command_sender: LoggingTx<UiCommand>) -> MouseManager {
MouseManager { MouseManager {
command_sender, command_sender,
dragging: false, dragging: None,
has_moved: false, has_moved: false,
position: PhysicalPosition::new(0, 0), position: PhysicalPosition::new(0, 0),
relative_position: PhysicalPosition::new(0, 0), relative_position: PhysicalPosition::new(0, 0),
@ -75,6 +84,7 @@ impl MouseManager {
&mut self, &mut self,
x: i32, x: i32,
y: i32, y: i32,
keyboard_manager: &KeyboardManager,
renderer: &Renderer, renderer: &Renderer,
windowed_context: &WindowedContext<PossiblyCurrent>, windowed_context: &WindowedContext<PossiblyCurrent>,
) { ) {
@ -87,7 +97,7 @@ impl MouseManager {
// If dragging, the relevant window (the one which we send all commands to) is the one // 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 // 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| { renderer.window_regions.iter().find(|details| {
details.id details.id
== self == self
@ -150,11 +160,13 @@ impl MouseManager {
let has_moved = self.drag_position != previous_position; let has_moved = self.drag_position != previous_position;
// If dragging and we haven't already sent a position, send a drag command // 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 self.command_sender
.send(UiCommand::Drag { .send(UiCommand::Drag {
button: self.dragging.as_ref().unwrap().to_owned(),
grid_id: relevant_window_details.id, grid_id: relevant_window_details.id,
position: self.drag_position.into(), position: self.drag_position.into(),
modifier_string: keyboard_manager.format_modifier_string(true),
}) })
.ok(); .ok();
} else { } else {
@ -162,23 +174,16 @@ impl MouseManager {
self.window_details_under_mouse = Some(relevant_window_details.clone()); 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. // For some reason pointer down is handled differently from pointer up and drag.
// Floating windows: relative coordinates are great. // Floating windows: relative coordinates are great.
// Non floating windows: rather than global coordinates, relative are needed // Non floating windows: rather than global coordinates, relative are needed
if self.enabled { if self.enabled {
let button_text = match mouse_button { if let Some(button_text) = mouse_button_to_button_text(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(details) = &self.window_details_under_mouse { if let Some(details) = &self.window_details_under_mouse {
let action = if down { let action = if down {
"press".to_owned() "press".to_owned()
@ -194,22 +199,23 @@ impl MouseManager {
self.command_sender self.command_sender
.send(UiCommand::MouseButton { .send(UiCommand::MouseButton {
button: button_text.to_string(), button: button_text.clone(),
action, action,
grid_id: details.id, grid_id: details.id,
position: position.into(), position: position.into(),
modifier_string: keyboard_manager.format_modifier_string(true),
}) })
.ok(); .ok();
} }
}
}
self.dragging = down; self.dragging = Some(button_text);
if !self.dragging { if !self.dragging.is_some() {
self.has_moved = false; self.has_moved = false;
} }
} }
}
}
fn handle_line_scroll(&mut self, x: f32, y: f32, keyboard_manager: &KeyboardManager) { fn handle_line_scroll(&mut self, x: f32, y: f32, keyboard_manager: &KeyboardManager) {
if !self.enabled { if !self.enabled {
@ -295,6 +301,7 @@ impl MouseManager {
} => self.handle_pointer_motion( } => self.handle_pointer_motion(
position.x as i32, position.x as i32,
position.y as i32, position.y as i32,
keyboard_manager,
renderer, renderer,
windowed_context, windowed_context,
), ),
@ -321,7 +328,10 @@ impl MouseManager {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::MouseInput { button, state, .. }, event: WindowEvent::MouseInput { button, state, .. },
.. ..
} => self.handle_pointer_transition(button, state == &ElementState::Pressed), } => self.handle_pointer_transition(
button,
state == &ElementState::Pressed,
keyboard_manager),
_ => {} _ => {}
} }
} }

Loading…
Cancel
Save