more mouse position fixes

macos-click-through
Keith Simmons 3 years ago
parent 3449cfda7a
commit fbd8e959b2

@ -21,10 +21,19 @@ use crate::renderer::{Renderer, WindowDrawDetails};
use crate::settings::SETTINGS; use crate::settings::SETTINGS;
use crate::window::WindowSettings; use crate::window::WindowSettings;
fn clamp_position(position: LogicalPosition<u32>, max_width: u32, max_height: u32) -> LogicalPosition<u32> {
LogicalPosition::new(
position.x.min(max_width),
position.y.min(max_height))
}
pub struct MouseManager { pub struct MouseManager {
command_sender: LoggingTx<UiCommand>, command_sender: LoggingTx<UiCommand>,
dragging: bool, dragging: bool,
has_moved: bool,
position: LogicalPosition<u32>, position: LogicalPosition<u32>,
relative_position: LogicalPosition<u32>,
drag_position: LogicalPosition<u32>,
window_details_under_mouse: Option<WindowDrawDetails>, window_details_under_mouse: Option<WindowDrawDetails>,
pub enabled: bool, pub enabled: bool,
} }
@ -34,7 +43,10 @@ impl MouseManager {
MouseManager { MouseManager {
command_sender, command_sender,
dragging: false, dragging: false,
has_moved: false,
position: LogicalPosition::new(0, 0), position: LogicalPosition::new(0, 0),
relative_position: LogicalPosition::new(0, 0),
drag_position: LogicalPosition::new(0, 0),
window_details_under_mouse: None, window_details_under_mouse: None,
enabled: true, enabled: true,
} }
@ -65,84 +77,84 @@ impl MouseManager {
}).last() }).last()
}; };
self.position =
LogicalPosition::new(
logical_position.x / renderer.font_width as u32,
logical_position.y / renderer.font_height as u32
);
if let Some(relevant_window_details) = relevant_window_details { if let Some(relevant_window_details) = relevant_window_details {
let previous_position = self.position; self.relative_position =
LogicalPosition::new(
(logical_position.x as i64 - relevant_window_details.region.left as i64) as u32 / renderer.font_width as u32,
(logical_position.y as i64 - relevant_window_details.region.top as i64) as u32 / renderer.font_height as u32,
);
let previous_position = self.drag_position;
// Until https://github.com/neovim/neovim/pull/12667 is merged, we have to special // Until https://github.com/neovim/neovim/pull/12667 is merged, we have to special
// case non floating windows. Floating windows correctly transform mouse positions // case non floating windows. Floating windows correctly transform mouse positions
// into grid coordinates, but non floating windows do not. // into grid coordinates, but non floating windows do not.
self.position = if relevant_window_details.floating_order.is_some() { self.drag_position = if relevant_window_details.floating_order.is_some() {
// Floating windows handle relative grid coordinates just fine // Floating windows handle relative grid coordinates just fine
LogicalPosition::new( self.relative_position.clone()
(logical_position.x - relevant_window_details.region.left as u32) / renderer.font_width as u32,
(logical_position.y - relevant_window_details.region.top as u32) / renderer.font_height as u32,
)
} else { } else {
// Non floating windows need global coordinates // Non floating windows need global coordinates
LogicalPosition::new( self.position.clone()
logical_position.x / renderer.font_width as u32,
logical_position.y / renderer.font_height as u32
)
}; };
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 && self.position != previous_position { if self.dragging && has_moved {
let window_id_to_send_to = self.window_details_under_mouse.as_ref().map(|details| details.id).unwrap_or(0);
self.command_sender self.command_sender
.send(UiCommand::Drag { .send(UiCommand::Drag {
grid_id: window_id_to_send_to, grid_id: relevant_window_details.id,
position: self.position.into(), position: dbg!(self.drag_position.into()),
}) })
.ok(); .ok();
} else { } else {
// otherwise, update the window_id_under_mouse to match the one selected // otherwise, update the window_id_under_mouse to match the one selected
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);
} }
} }
fn handle_pointer_down(&mut self, renderer: &Renderer) { fn handle_pointer_transition(&mut self, down: bool) {
// 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 {
if let Some(details) = &self.window_details_under_mouse { if let Some(details) = &self.window_details_under_mouse {
if details.floating_order.is_some() { let action = if down {
self.command_sender "press".to_owned()
.send(UiCommand::MouseButton {
action: String::from("press"),
grid_id: details.id,
position: (self.position.x, self.position.y),
})
.ok();
} else { } else {
let relative_position = ( "release".to_owned()
self.position.x - (details.region.left as u64 / renderer.font_width) as u32, };
self.position.y - (details.region.top as u64 / renderer.font_height) as u32,
); let position = if !down && self.has_moved {
self.drag_position
} else {
self.relative_position
};
self.command_sender self.command_sender
.send(UiCommand::MouseButton { .send(UiCommand::MouseButton {
action: String::from("press"), action,
grid_id: details.id, grid_id: details.id,
position: relative_position, position: position.into(),
}) })
.ok(); .ok();
} }
} }
}
self.dragging = true;
}
fn handle_pointer_up(&mut self) { self.dragging = down;
if self.enabled {
self.command_sender if !self.dragging {
.send(UiCommand::MouseButton { self.has_moved = false;
action: String::from("release"),
grid_id: self.window_details_under_mouse.as_ref().map(|details| details.id).unwrap_or(0),
position: (self.position.x, self.position.y),
})
.ok();
} }
self.dragging = false;
} }
fn handle_mouse_wheel(&mut self, x: f32, y: f32) { fn handle_mouse_wheel(&mut self, x: f32, y: f32) {
@ -163,7 +175,7 @@ impl MouseManager {
.send(UiCommand::Scroll { .send(UiCommand::Scroll {
direction: input_type.to_string(), direction: input_type.to_string(),
grid_id: self.window_details_under_mouse.as_ref().map(|details| details.id).unwrap_or(0), grid_id: self.window_details_under_mouse.as_ref().map(|details| details.id).unwrap_or(0),
position: (self.position.x, self.position.y), position: self.drag_position.into(),
}) })
.ok(); .ok();
} }
@ -179,7 +191,7 @@ impl MouseManager {
.send(UiCommand::Scroll { .send(UiCommand::Scroll {
direction: input_type.to_string(), direction: input_type.to_string(),
grid_id: self.window_details_under_mouse.as_ref().map(|details| details.id).unwrap_or(0), grid_id: self.window_details_under_mouse.as_ref().map(|details| details.id).unwrap_or(0),
position: (self.position.x, self.position.y), position: self.drag_position.into(),
}) })
.ok(); .ok();
} }
@ -217,13 +229,7 @@ impl MouseManager {
.. ..
}, },
.. ..
} => { } => self.handle_pointer_transition(state == &ElementState::Pressed),
if state == &ElementState::Pressed {
self.handle_pointer_down(renderer);
} else {
self.handle_pointer_up();
}
},
_ => {} _ => {}
} }
} }

Loading…
Cancel
Save