From ae17af2c558e37955e838586eab420dddd3d220b Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 23 Jul 2022 01:19:28 +0100 Subject: [PATCH] fix(#1343): Do not move cursor on first focus for macOS --- src/window/mouse_manager.rs | 71 ++++++++++++++++++++++++++++--------- src/window/settings.rs | 2 ++ 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/window/mouse_manager.rs b/src/window/mouse_manager.rs index 77f2c5c..aa30dd3 100644 --- a/src/window/mouse_manager.rs +++ b/src/window/mouse_manager.rs @@ -13,6 +13,7 @@ use glutin::{ }, PossiblyCurrent, WindowedContext, }; +use log::info; use skia_safe::Rect; use crate::{ @@ -85,6 +86,9 @@ pub struct MouseManager { mouse_hidden: bool, pub enabled: bool, + + // Not exactly a mouse setting but ideally needs to exist for some state + current_window_focused: bool, } impl MouseManager { @@ -100,6 +104,7 @@ impl MouseManager { window_details_under_mouse: None, mouse_hidden: false, enabled: true, + current_window_focused: false, } } @@ -195,6 +200,7 @@ impl MouseManager { mouse_button: &MouseButton, down: bool, keyboard_manager: &KeyboardManager, + click_through: bool, ) { // For some reason pointer down is handled differently from pointer up and drag. // Floating windows: relative coordinates are great. @@ -214,13 +220,15 @@ impl MouseManager { self.relative_position }; - EVENT_AGGREGATOR.send(UiCommand::Serial(SerialCommand::MouseButton { - button: button_text.clone(), - action, - grid_id: details.id, - position: position.into(), - modifier_string: keyboard_manager.format_modifier_string(true), - })); + if click_through || !self.current_window_focused { + EVENT_AGGREGATOR.send(UiCommand::Serial(SerialCommand::MouseButton { + button: button_text.clone(), + action, + grid_id: details.id, + position: position.into(), + modifier_string: keyboard_manager.format_modifier_string(true), + })); + } } if down { @@ -387,13 +395,23 @@ impl MouseManager { renderer, windowed_context, ); - self.handle_pointer_transition(&MouseButton::Left, true, keyboard_manager); + self.handle_pointer_transition( + &MouseButton::Left, + true, + keyboard_manager, + true, + ); } } TouchPhase::Ended | TouchPhase::Cancelled => { if let Some(trace) = self.touch_position.remove(&finger_id) { if self.dragging.is_some() { - self.handle_pointer_transition(&MouseButton::Left, false, keyboard_manager); + self.handle_pointer_transition( + &MouseButton::Left, + false, + keyboard_manager, + true, + ); } if !trace.left_deadzone_once { self.handle_pointer_motion( @@ -403,8 +421,18 @@ impl MouseManager { renderer, windowed_context, ); - self.handle_pointer_transition(&MouseButton::Left, true, keyboard_manager); - self.handle_pointer_transition(&MouseButton::Left, false, keyboard_manager); + self.handle_pointer_transition( + &MouseButton::Left, + true, + keyboard_manager, + true, + ); + self.handle_pointer_transition( + &MouseButton::Left, + false, + keyboard_manager, + true, + ); } } } @@ -419,6 +447,10 @@ impl MouseManager { windowed_context: &WindowedContext, ) { match event { + Event::WindowEvent { + event: WindowEvent::Focused(focused), + .. + } => self.current_window_focused = *focused, Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, .. @@ -476,11 +508,18 @@ impl MouseManager { Event::WindowEvent { event: WindowEvent::MouseInput { button, state, .. }, .. - } => self.handle_pointer_transition( - button, - state == &ElementState::Pressed, - keyboard_manager, - ), + } => { + let window_settings = SETTINGS.get::(); + self.handle_pointer_transition( + button, + state == &ElementState::Pressed, + keyboard_manager, + window_settings.click_through, + ); + + // Always reset focused to false to prevent all mouse inputs not working + self.current_window_focused = false; + } Event::WindowEvent { event: WindowEvent::KeyboardInput { diff --git a/src/window/settings.rs b/src/window/settings.rs index e455dad..5b16adc 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -12,6 +12,7 @@ pub struct WindowSettings { pub hide_mouse_when_typing: bool, pub touch_deadzone: f32, pub touch_drag_timeout: f32, + pub click_through: bool } impl Default for WindowSettings { @@ -27,6 +28,7 @@ impl Default for WindowSettings { hide_mouse_when_typing: false, touch_deadzone: 6.0, touch_drag_timeout: 0.17, + click_through: false } } }