Skip to content

Commit

Permalink
refactor(macOS): use NSEvent::mouseLocation for cursor_position inste…
Browse files Browse the repository at this point in the history
…ad of CGEventSource (#900)

* refactor(macOS): use NSEvent::mouseLocation for cursor_position instead of CGEventSource

* Update mod.rs

* Update mod.rs

* fix build
  • Loading branch information
amrbashir authored Apr 3, 2024
1 parent 9eb189e commit 73883cb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
7 changes: 1 addition & 6 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,7 @@ impl<T: 'static> EventLoopWindowTarget<T> {
}
#[inline]
pub fn cursor_position(&self) -> Result<PhysicalPosition<f64>, 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]
Expand Down
29 changes: 12 additions & 17 deletions src/platform_impl/macos/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -110,17 +111,11 @@ pub fn window_position(position: LogicalPosition<f64>) -> NSPoint {
)
}

// FIXME: This is actually logical position.
pub fn cursor_position() -> Result<LogicalPosition<f64>, 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<PhysicalPosition<f64>, 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 {
Expand Down
7 changes: 1 addition & 6 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,7 @@ impl UnownedWindow {

#[inline]
pub fn cursor_position(&self) -> Result<PhysicalPosition<f64>, 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]
Expand Down

0 comments on commit 73883cb

Please sign in to comment.