Use platform API to request mouse position
Co-Authored-By: Conrad Irwin <conrad@zed.dev>
This commit is contained in:
parent
5996b6b46b
commit
684db11afd
8 changed files with 35 additions and 24 deletions
|
@ -66,7 +66,7 @@ impl<V: 'static, E: Element<V> + Styleable> Element<V> for Hoverable<E> {
|
||||||
|
|
||||||
let hovered = self.hovered.clone();
|
let hovered = self.hovered.clone();
|
||||||
cx.on_event(order, move |view, event: &MouseMovedEvent, cx| {
|
cx.on_event(order, move |view, event: &MouseMovedEvent, cx| {
|
||||||
if bounds.contains_point(event.position) != hovered.get() {
|
if bounds.contains_point(cx.mouse_position()) != hovered.get() {
|
||||||
cx.repaint();
|
cx.repaint();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1363,14 +1363,7 @@ impl AppContext {
|
||||||
window: handle,
|
window: handle,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let mouse_position = self.platform.mouse_position();
|
let mut window = Window::new(handle, platform_window, self, build_root_view);
|
||||||
let mut window = Window::new(
|
|
||||||
handle,
|
|
||||||
platform_window,
|
|
||||||
mouse_position,
|
|
||||||
self,
|
|
||||||
build_root_view,
|
|
||||||
);
|
|
||||||
let mut cx = WindowContext::mutable(self, &mut window, handle);
|
let mut cx = WindowContext::mutable(self, &mut window, handle);
|
||||||
cx.layout(false).expect("initial layout should not error");
|
cx.layout(false).expect("initial layout should not error");
|
||||||
let scene = cx.paint().expect("initial paint should not error");
|
let scene = cx.paint().expect("initial paint should not error");
|
||||||
|
|
|
@ -70,7 +70,6 @@ impl Window {
|
||||||
pub fn new<V, F>(
|
pub fn new<V, F>(
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
platform_window: Box<dyn platform::Window>,
|
platform_window: Box<dyn platform::Window>,
|
||||||
mouse_position: Vector2F,
|
|
||||||
cx: &mut AppContext,
|
cx: &mut AppContext,
|
||||||
build_view: F,
|
build_view: F,
|
||||||
) -> Self
|
) -> Self
|
||||||
|
@ -98,7 +97,7 @@ impl Window {
|
||||||
hovered_region_ids: Default::default(),
|
hovered_region_ids: Default::default(),
|
||||||
clicked_region_ids: Default::default(),
|
clicked_region_ids: Default::default(),
|
||||||
clicked_region: None,
|
clicked_region: None,
|
||||||
mouse_position,
|
mouse_position: Default::default(),
|
||||||
titlebar_height,
|
titlebar_height,
|
||||||
appearance,
|
appearance,
|
||||||
};
|
};
|
||||||
|
@ -508,7 +507,9 @@ impl<'a> WindowContext<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn dispatch_event(&mut self, event: Event, event_reused: bool) -> bool {
|
pub(crate) fn dispatch_event(&mut self, event: Event, event_reused: bool) -> bool {
|
||||||
|
if !event_reused {
|
||||||
self.dispatch_to_new_event_handlers(&event);
|
self.dispatch_to_new_event_handlers(&event);
|
||||||
|
}
|
||||||
|
|
||||||
let mut mouse_events = SmallVec::<[_; 2]>::new();
|
let mut mouse_events = SmallVec::<[_; 2]>::new();
|
||||||
let mut notified_views: HashSet<usize> = Default::default();
|
let mut notified_views: HashSet<usize> = Default::default();
|
||||||
|
|
|
@ -75,7 +75,6 @@ pub trait Platform: Send + Sync {
|
||||||
fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
|
fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
|
||||||
fn delete_credentials(&self, url: &str) -> Result<()>;
|
fn delete_credentials(&self, url: &str) -> Result<()>;
|
||||||
|
|
||||||
fn mouse_position(&self) -> Vector2F;
|
|
||||||
fn set_cursor_style(&self, style: CursorStyle);
|
fn set_cursor_style(&self, style: CursorStyle);
|
||||||
fn should_auto_hide_scrollbars(&self) -> bool;
|
fn should_auto_hide_scrollbars(&self) -> bool;
|
||||||
|
|
||||||
|
@ -147,6 +146,7 @@ pub trait Window {
|
||||||
fn titlebar_height(&self) -> f32;
|
fn titlebar_height(&self) -> f32;
|
||||||
fn appearance(&self) -> Appearance;
|
fn appearance(&self) -> Appearance;
|
||||||
fn screen(&self) -> Rc<dyn Screen>;
|
fn screen(&self) -> Rc<dyn Screen>;
|
||||||
|
fn mouse_position(&self) -> Vector2F;
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||||
fn set_input_handler(&mut self, input_handler: Box<dyn InputHandler>);
|
fn set_input_handler(&mut self, input_handler: Box<dyn InputHandler>);
|
||||||
|
|
|
@ -18,7 +18,7 @@ use cocoa::{
|
||||||
},
|
},
|
||||||
base::{id, nil, selector, BOOL, YES},
|
base::{id, nil, selector, BOOL, YES},
|
||||||
foundation::{
|
foundation::{
|
||||||
NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSPoint, NSProcessInfo, NSString,
|
NSArray, NSAutoreleasePool, NSBundle, NSData, NSInteger, NSProcessInfo, NSString,
|
||||||
NSUInteger, NSURL,
|
NSUInteger, NSURL,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -37,7 +37,7 @@ use objc::{
|
||||||
runtime::{Class, Object, Sel},
|
runtime::{Class, Object, Sel},
|
||||||
sel, sel_impl,
|
sel, sel_impl,
|
||||||
};
|
};
|
||||||
use pathfinder_geometry::vector::{vec2f, Vector2F};
|
|
||||||
use postage::oneshot;
|
use postage::oneshot;
|
||||||
use ptr::null_mut;
|
use ptr::null_mut;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -785,11 +785,6 @@ impl platform::Platform for MacPlatform {
|
||||||
Ok(())
|
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) {
|
fn set_cursor_style(&self, style: CursorStyle) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let new_cursor: id = match style {
|
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 {
|
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
|
||||||
self
|
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 {
|
unsafe fn build_window_class(name: &'static str, superclass: &Class) -> *const Class {
|
||||||
let mut decl = ClassDecl::new(name, superclass).unwrap();
|
let mut decl = ClassDecl::new(name, superclass).unwrap();
|
||||||
decl.add_ivar::<*mut c_void>(WINDOW_STATE_IVAR);
|
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 {
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
@ -195,10 +195,6 @@ impl super::Platform for Platform {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mouse_position(&self) -> Vector2F {
|
|
||||||
Vector2F::zero()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_cursor_style(&self, style: CursorStyle) {
|
fn set_cursor_style(&self, style: CursorStyle) {
|
||||||
*self.cursor.lock() = style;
|
*self.cursor.lock() = style;
|
||||||
}
|
}
|
||||||
|
@ -336,6 +332,10 @@ impl super::Window for Window {
|
||||||
Rc::new(Screen)
|
Rc::new(Screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mouse_position(&self) -> Vector2F {
|
||||||
|
Vector2F::zero()
|
||||||
|
}
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue