|
|
|
@ -1,37 +1,44 @@
|
|
|
|
|
use glutin::{
|
|
|
|
|
self,
|
|
|
|
|
WindowedContext,
|
|
|
|
|
dpi::{
|
|
|
|
|
LogicalPosition,
|
|
|
|
|
PhysicalPosition,
|
|
|
|
|
},
|
|
|
|
|
event::{
|
|
|
|
|
ElementState,
|
|
|
|
|
Event,
|
|
|
|
|
MouseButton,
|
|
|
|
|
MouseScrollDelta,
|
|
|
|
|
WindowEvent,
|
|
|
|
|
},
|
|
|
|
|
PossiblyCurrent
|
|
|
|
|
dpi::{LogicalPosition, PhysicalPosition},
|
|
|
|
|
event::{ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent},
|
|
|
|
|
PossiblyCurrent, WindowedContext,
|
|
|
|
|
};
|
|
|
|
|
use skia_safe::Rect;
|
|
|
|
|
|
|
|
|
|
use crate::channel_utils::LoggingTx;
|
|
|
|
|
use crate::bridge::UiCommand;
|
|
|
|
|
use crate::channel_utils::LoggingTx;
|
|
|
|
|
use crate::renderer::{Renderer, WindowDrawDetails};
|
|
|
|
|
use crate::settings::SETTINGS;
|
|
|
|
|
use crate::window::WindowSettings;
|
|
|
|
|
|
|
|
|
|
fn clamp_position(position: LogicalPosition<f32>, region: Rect, font_width: u64, font_height: u64) -> LogicalPosition<f32> {
|
|
|
|
|
fn clamp_position(
|
|
|
|
|
position: LogicalPosition<f32>,
|
|
|
|
|
region: Rect,
|
|
|
|
|
font_width: u64,
|
|
|
|
|
font_height: u64,
|
|
|
|
|
) -> LogicalPosition<f32> {
|
|
|
|
|
LogicalPosition::new(
|
|
|
|
|
position.x.min(region.right - font_width as f32).max(region.left),
|
|
|
|
|
position.y.min(region.bottom - font_height as f32).max(region.top))
|
|
|
|
|
position
|
|
|
|
|
.x
|
|
|
|
|
.min(region.right - font_width as f32)
|
|
|
|
|
.max(region.left),
|
|
|
|
|
position
|
|
|
|
|
.y
|
|
|
|
|
.min(region.bottom - font_height as f32)
|
|
|
|
|
.max(region.top),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_grid_coords(position: LogicalPosition<f32>, font_width: u64, font_height: u64) -> LogicalPosition<u32> {
|
|
|
|
|
fn to_grid_coords(
|
|
|
|
|
position: LogicalPosition<f32>,
|
|
|
|
|
font_width: u64,
|
|
|
|
|
font_height: u64,
|
|
|
|
|
) -> LogicalPosition<u32> {
|
|
|
|
|
LogicalPosition::new(
|
|
|
|
|
(position.x as u64 / font_width) as u32,
|
|
|
|
|
(position.y as u64 / font_height) as u32)
|
|
|
|
|
(position.y as u64 / font_height) as u32,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct MouseManager {
|
|
|
|
@ -59,7 +66,13 @@ impl MouseManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_pointer_motion(&mut self, x: i32, y: i32, renderer: &Renderer, windowed_context: &WindowedContext<PossiblyCurrent>) {
|
|
|
|
|
fn handle_pointer_motion(
|
|
|
|
|
&mut self,
|
|
|
|
|
x: i32,
|
|
|
|
|
y: i32,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
windowed_context: &WindowedContext<PossiblyCurrent>,
|
|
|
|
|
) {
|
|
|
|
|
let size = windowed_context.window().inner_size();
|
|
|
|
|
if x < 0 || x as u32 >= size.width || y < 0 || y as u32 >= size.height {
|
|
|
|
|
return;
|
|
|
|
@ -71,35 +84,54 @@ 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 {
|
|
|
|
|
renderer.window_regions.iter()
|
|
|
|
|
.find(|details| details.id == self.window_details_under_mouse.as_ref().expect("If dragging, there should be a window details recorded").id)
|
|
|
|
|
renderer.window_regions.iter().find(|details| {
|
|
|
|
|
details.id
|
|
|
|
|
== self
|
|
|
|
|
.window_details_under_mouse
|
|
|
|
|
.as_ref()
|
|
|
|
|
.expect("If dragging, there should be a window details recorded")
|
|
|
|
|
.id
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
// the rendered window regions are sorted by draw order, so the earlier windows in the
|
|
|
|
|
// list are drawn under the later ones
|
|
|
|
|
renderer.window_regions.iter().filter(|details| {
|
|
|
|
|
logical_position.x >= details.region.left &&
|
|
|
|
|
logical_position.x < details.region.right &&
|
|
|
|
|
logical_position.y >= details.region.top &&
|
|
|
|
|
logical_position.y < details.region.bottom
|
|
|
|
|
}).last()
|
|
|
|
|
renderer
|
|
|
|
|
.window_regions
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|details| {
|
|
|
|
|
logical_position.x >= details.region.left
|
|
|
|
|
&& logical_position.x < details.region.right
|
|
|
|
|
&& logical_position.y >= details.region.top
|
|
|
|
|
&& logical_position.y < details.region.bottom
|
|
|
|
|
})
|
|
|
|
|
.last()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let global_bounds = relevant_window_details.map(|details| details.region).unwrap_or(Rect::from_wh(size.width as f32, size.height as f32));
|
|
|
|
|
let clamped_position = clamp_position(logical_position, global_bounds, renderer.font_width, renderer.font_height);
|
|
|
|
|
let global_bounds = relevant_window_details
|
|
|
|
|
.map(|details| details.region)
|
|
|
|
|
.unwrap_or(Rect::from_wh(size.width as f32, size.height as f32));
|
|
|
|
|
let clamped_position = clamp_position(
|
|
|
|
|
logical_position,
|
|
|
|
|
global_bounds,
|
|
|
|
|
renderer.font_width,
|
|
|
|
|
renderer.font_height,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.position = to_grid_coords(clamped_position, renderer.font_width, renderer.font_height);
|
|
|
|
|
|
|
|
|
|
if let Some(relevant_window_details) = relevant_window_details {
|
|
|
|
|
let relative_position = LogicalPosition::new(
|
|
|
|
|
clamped_position.x - relevant_window_details.region.left,
|
|
|
|
|
clamped_position.y - relevant_window_details.region.top);
|
|
|
|
|
self.relative_position = to_grid_coords(relative_position, renderer.font_width, renderer.font_height);
|
|
|
|
|
clamped_position.y - relevant_window_details.region.top,
|
|
|
|
|
);
|
|
|
|
|
self.relative_position =
|
|
|
|
|
to_grid_coords(relative_position, renderer.font_width, renderer.font_height);
|
|
|
|
|
|
|
|
|
|
let previous_position = self.drag_position;
|
|
|
|
|
// Until https://github.com/neovim/neovim/pull/12667 is merged, we have to special
|
|
|
|
|
// case non floating windows. Floating windows correctly transform mouse positions
|
|
|
|
|
// into grid coordinates, but non floating windows do not.
|
|
|
|
|
self.drag_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
|
|
|
|
|
self.relative_position.clone()
|
|
|
|
|
} else {
|
|
|
|
@ -130,7 +162,7 @@ impl MouseManager {
|
|
|
|
|
// 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 {
|
|
|
|
|
if self.enabled {
|
|
|
|
|
if let Some(details) = &self.window_details_under_mouse {
|
|
|
|
|
let action = if down {
|
|
|
|
|
"press".to_owned()
|
|
|
|
@ -178,7 +210,11 @@ impl MouseManager {
|
|
|
|
|
self.command_sender
|
|
|
|
|
.send(UiCommand::Scroll {
|
|
|
|
|
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.drag_position.into(),
|
|
|
|
|
})
|
|
|
|
|
.ok();
|
|
|
|
@ -194,21 +230,33 @@ impl MouseManager {
|
|
|
|
|
self.command_sender
|
|
|
|
|
.send(UiCommand::Scroll {
|
|
|
|
|
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.drag_position.into(),
|
|
|
|
|
})
|
|
|
|
|
.ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn handle_event(&mut self, event: &Event<()>, renderer: &Renderer, windowed_context: &WindowedContext<PossiblyCurrent>) {
|
|
|
|
|
pub fn handle_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: &Event<()>,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
windowed_context: &WindowedContext<PossiblyCurrent>,
|
|
|
|
|
) {
|
|
|
|
|
match event {
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
event: WindowEvent::CursorMoved { position, .. },
|
|
|
|
|
..
|
|
|
|
|
} => self.handle_pointer_motion(
|
|
|
|
|
position.x as i32, position.y as i32,
|
|
|
|
|
renderer, windowed_context),
|
|
|
|
|
position.x as i32,
|
|
|
|
|
position.y as i32,
|
|
|
|
|
renderer,
|
|
|
|
|
windowed_context,
|
|
|
|
|
),
|
|
|
|
|
Event::WindowEvent {
|
|
|
|
|
event:
|
|
|
|
|
WindowEvent::MouseWheel {
|
|
|
|
|