diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index b7f8e62c9..790d743a1 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -113,12 +113,7 @@ impl EventLoopWindowTarget { } #[inline] pub fn cursor_position(&self) -> Result, ExternalError> { - let point = util::cursor_position()?; - if let Some(m) = self.monitor_from_point(point.x, point.y) { - Ok(point.to_physical(m.scale_factor())) - } else { - Err(ExternalError::Os(os_error!(super::OsError::CGError(0)))) - } + util::cursor_position() } #[inline] diff --git a/src/platform_impl/macos/util/mod.rs b/src/platform_impl/macos/util/mod.rs index 261b2a0b4..63f2892b9 100644 --- a/src/platform_impl/macos/util/mod.rs +++ b/src/platform_impl/macos/util/mod.rs @@ -17,14 +17,15 @@ use cocoa::{ base::{id, nil}, foundation::{NSAutoreleasePool, NSPoint, NSRect, NSString, NSUInteger}, }; -use core_graphics::{ - display::CGDisplay, - event::CGEvent, - event_source::{CGEventSource, CGEventSourceStateID}, -}; +use core_graphics::display::CGDisplay; +use objc::class; use objc::runtime::{Class, Object, Sel, BOOL, YES}; -use crate::{dpi::LogicalPosition, error::ExternalError, platform_impl::platform::ffi}; +use crate::{ + dpi::{LogicalPosition, PhysicalPosition}, + error::ExternalError, + platform_impl::platform::ffi, +}; // Replace with `!` once stable #[derive(Debug)] @@ -110,17 +111,11 @@ pub fn window_position(position: LogicalPosition) -> NSPoint { ) } -// FIXME: This is actually logical position. -pub fn cursor_position() -> Result, ExternalError> { - if let Ok(s) = CGEventSource::new(CGEventSourceStateID::CombinedSessionState) { - if let Ok(e) = CGEvent::new(s) { - let pt = e.location(); - let pos = LogicalPosition::new(pt.x, pt.y); - return Ok(pos); - } - } - - return Err(ExternalError::Os(os_error!(super::OsError::CGError(0)))); +pub fn cursor_position() -> Result, ExternalError> { + let point: NSPoint = unsafe { msg_send![class!(NSEvent), mouseLocation] }; + let y = CGDisplay::main().pixels_high() as f64 - point.y; + let point = LogicalPosition::new(point.x, y); + Ok(point.to_physical(super::monitor::primary_monitor().scale_factor())) } pub unsafe fn ns_string_id_ref(s: &str) -> IdRef { diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 04baf4819..76196c237 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -817,12 +817,7 @@ impl UnownedWindow { #[inline] pub fn cursor_position(&self) -> Result, ExternalError> { - let point = util::cursor_position()?; - if let Some(m) = self.monitor_from_point(point.x, point.y) { - Ok(point.to_physical(m.scale_factor())) - } else { - Err(ExternalError::Os(os_error!(OsError::CGError(0)))) - } + util::cursor_position() } #[inline]