Skip to content

Commit

Permalink
fix(macOS): cursor_position returns incorrect position (#711)
Browse files Browse the repository at this point in the history
* On macOS, fix `cursor_position` returns incorrect position

* Add proper error handling

* Restore example
  • Loading branch information
wusyong committed Mar 7, 2023
1 parent 76ae625 commit ea2e60d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/cursor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tao": patch
---

On macOS, Fix `cursor_position` return incorrect position.

18 changes: 14 additions & 4 deletions src/platform_impl/macos/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ use cocoa::{
base::{id, nil},
foundation::{NSAutoreleasePool, NSPoint, NSRect, NSString, NSUInteger},
};
use core_graphics::display::CGDisplay;
use core_graphics::{
display::CGDisplay,
event::CGEvent,
event_source::{CGEventSource, CGEventSourceStateID},
};
use objc::runtime::{Class, Object, Sel, BOOL, YES};

use crate::{
Expand Down Expand Up @@ -124,11 +128,17 @@ pub fn window_position(position: LogicalPosition<f64>) -> NSPoint {
)
}

// FIXME: This is actually logical position.
pub fn cursor_position() -> Result<PhysicalPosition<f64>, ExternalError> {
unsafe {
let pt: NSPoint = msg_send![class!(NSEvent), mouseLocation];
Ok((pt.x, pt.y).into())
if let Ok(s) = CGEventSource::new(CGEventSourceStateID::CombinedSessionState) {
if let Ok(e) = CGEvent::new(s) {
let pt = e.location();
let pos = PhysicalPosition::new(pt.x, pt.y);
return Ok(pos);
}
}

return Err(ExternalError::Os(os_error!(super::OsError::CGError(0))));
}

pub unsafe fn ns_string_id_ref(s: &str) -> IdRef {
Expand Down

0 comments on commit ea2e60d

Please sign in to comment.