@@ -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) {
@@ -24,5 +24,6 @@ pub enum Event {
},
MouseMoved {
position: Vector2F,
+ left_mouse_down: bool,
},
}
@@ -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,
}
@@ -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,
+ });
}
_ => {}
}