Don't change cursor on mouse move while mouse button is held down

Max Brunsfeld created

Change summary

gpui/src/elements/mouse_event_handler.rs | 33 ++++++++++++++-----------
gpui/src/platform/event.rs               |  1 
gpui/src/platform/mac/event.rs           |  5 ++-
gpui/src/presenter.rs                    | 10 ++++++-
4 files changed, 31 insertions(+), 18 deletions(-)

Detailed changes

gpui/src/elements/mouse_event_handler.rs 🔗

@@ -94,24 +94,29 @@ impl Element for MouseEventHandler {
         let handled_in_child = self.child.dispatch_event(event, cx);
 
         self.state.update(cx, |state, cx| match event {
-            Event::MouseMoved { position } => {
-                let mouse_in = bounds.contains_point(*position);
-                if state.hovered != mouse_in {
-                    state.hovered = mouse_in;
-                    if let Some(cursor_style) = cursor_style {
-                        if !state.clicked {
-                            if state.hovered {
-                                state.cursor_style_handle = Some(cx.set_cursor_style(cursor_style));
-                            } else {
-                                state.cursor_style_handle = None;
+            Event::MouseMoved {
+                position,
+                left_mouse_down,
+            } => {
+                if !left_mouse_down {
+                    let mouse_in = bounds.contains_point(*position);
+                    if state.hovered != mouse_in {
+                        state.hovered = mouse_in;
+                        if let Some(cursor_style) = cursor_style {
+                            if !state.clicked {
+                                if state.hovered {
+                                    state.cursor_style_handle =
+                                        Some(cx.set_cursor_style(cursor_style));
+                                } else {
+                                    state.cursor_style_handle = None;
+                                }
                             }
                         }
+                        cx.notify();
+                        return true;
                     }
-                    cx.notify();
-                    true
-                } else {
-                    handled_in_child
                 }
+                handled_in_child
             }
             Event::LeftMouseDown { position, .. } => {
                 if !handled_in_child && bounds.contains_point(*position) {

gpui/src/platform/mac/event.rs 🔗

@@ -6,8 +6,8 @@ use cocoa::appkit::{
     NSUpArrowFunctionKey as ARROW_UP_KEY,
 };
 use cocoa::{
-    appkit::{NSEvent as _, NSEventModifierFlags, NSEventType},
-    base::{id, YES},
+    appkit::{NSEvent, NSEventModifierFlags, NSEventType},
+    base::{id, nil, YES},
     foundation::NSString as _,
 };
 use std::{ffi::CStr, os::raw::c_char};
@@ -116,6 +116,7 @@ impl Event {
                     native_event.locationInWindow().x as f32,
                     window_height - native_event.locationInWindow().y as f32,
                 ),
+                left_mouse_down: NSEvent::pressedMouseButtons(nil) & 1 != 0,
             }),
             _ => None,
         }

gpui/src/presenter.rs 🔗

@@ -145,8 +145,14 @@ impl Presenter {
     pub fn dispatch_event(&mut self, event: Event, cx: &mut MutableAppContext) {
         if let Some(root_view_id) = cx.root_view_id(self.window_id) {
             match event {
-                Event::MouseMoved { position, .. } | Event::LeftMouseDragged { position } => {
-                    self.last_mouse_moved_event = Some(Event::MouseMoved { position });
+                Event::MouseMoved { .. } => {
+                    self.last_mouse_moved_event = Some(event.clone());
+                }
+                Event::LeftMouseDragged { position } => {
+                    self.last_mouse_moved_event = Some(Event::MouseMoved {
+                        position,
+                        left_mouse_down: true,
+                    });
                 }
                 _ => {}
             }