Use platform API to request mouse position
Co-Authored-By: Conrad Irwin <conrad@zed.dev>
This commit is contained in:
co-authored by
Conrad Irwin
parent
5996b6b46b
commit
684db11afd
@@ -18,7 +18,7 @@ use cocoa::{
|
||||
},
|
||||
base::{id, nil, selector, BOOL, YES},
|
||||
foundation::{
|
||||
NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSPoint, NSProcessInfo, NSString,
|
||||
NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSProcessInfo, NSString,
|
||||
NSUInteger, NSURL,
|
||||
},
|
||||
};
|
||||
@@ -37,7 +37,7 @@ use objc::{
|
||||
runtime::{Class, Object, Sel},
|
||||
sel, sel_impl,
|
||||
};
|
||||
use pathfinder_geometry::vector::{vec2f, Vector2F};
|
||||
|
||||
use postage::oneshot;
|
||||
use ptr::null_mut;
|
||||
use std::{
|
||||
@@ -785,11 +785,6 @@ impl platform::Platform for MacPlatform {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn mouse_position(&self) -> Vector2F {
|
||||
let position: NSPoint = unsafe { msg_send![class!(NSEvent), mouseLocation] };
|
||||
vec2f(position.x as f32, position.y as f32)
|
||||
}
|
||||
|
||||
fn set_cursor_style(&self, style: CursorStyle) {
|
||||
unsafe {
|
||||
let new_cursor: id = match style {
|
||||
|
||||
@@ -202,6 +202,10 @@ impl platform::Window for StatusItem {
|
||||
}
|
||||
}
|
||||
|
||||
fn mouse_position(&self) -> Vector2F {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
|
||||
@@ -221,6 +221,14 @@ unsafe fn build_classes() {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn convert_mouse_position(position: NSPoint, window_height: f32) -> Vector2F {
|
||||
vec2f(
|
||||
position.x as f32,
|
||||
// MacOS screen coordinates are relative to bottom left
|
||||
window_height - position.y as f32,
|
||||
)
|
||||
}
|
||||
|
||||
unsafe fn build_window_class(name: &'static str, superclass: &Class) -> *const Class {
|
||||
let mut decl = ClassDecl::new(name, superclass).unwrap();
|
||||
decl.add_ivar::<*mut c_void>(WINDOW_STATE_IVAR);
|
||||
@@ -661,6 +669,16 @@ impl platform::Window for MacWindow {
|
||||
}
|
||||
}
|
||||
|
||||
fn mouse_position(&self) -> Vector2F {
|
||||
let position = unsafe {
|
||||
self.0
|
||||
.borrow()
|
||||
.native_window
|
||||
.mouseLocationOutsideOfEventStream()
|
||||
};
|
||||
convert_mouse_position(position, self.content_size().y())
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
@@ -195,10 +195,6 @@ impl super::Platform for Platform {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn mouse_position(&self) -> Vector2F {
|
||||
Vector2F::zero()
|
||||
}
|
||||
|
||||
fn set_cursor_style(&self, style: CursorStyle) {
|
||||
*self.cursor.lock() = style;
|
||||
}
|
||||
@@ -336,6 +332,10 @@ impl super::Window for Window {
|
||||
Rc::new(Screen)
|
||||
}
|
||||
|
||||
fn mouse_position(&self) -> Vector2F {
|
||||
Vector2F::zero()
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user