From 7f553668b63841ea6b86a0bc479e72fc7b0197d1 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 24 Jan 2023 18:58:52 +0200 Subject: [PATCH 01/18] Add `cursor_position` getter API --- CHANGELOG.md | 1 + examples/window.rs | 6 ++-- src/event_loop.rs | 12 ++++++++ src/platform_impl/android/mod.rs | 12 ++++++++ src/platform_impl/ios/event_loop.rs | 5 ++++ src/platform_impl/ios/window.rs | 4 +++ src/platform_impl/linux/mod.rs | 9 ++++++ .../linux/wayland/event_loop/mod.rs | 7 +++++ src/platform_impl/linux/wayland/window/mod.rs | 5 ++++ src/platform_impl/linux/x11/mod.rs | 7 ++++- src/platform_impl/linux/x11/util/cursor.rs | 30 ++++++++++++++++++- src/platform_impl/linux/x11/window.rs | 4 +++ src/platform_impl/macos/appkit/event.rs | 3 ++ src/platform_impl/macos/event_loop.rs | 12 +++++++- src/platform_impl/macos/util/mod.rs | 7 +++++ src/platform_impl/macos/window.rs | 5 ++++ src/platform_impl/orbital/event_loop.rs | 7 +++++ src/platform_impl/orbital/window.rs | 6 ++++ .../web/event_loop/window_target.rs | 5 ++++ src/platform_impl/web/window.rs | 4 +++ src/platform_impl/windows/event_loop.rs | 6 ++++ src/platform_impl/windows/util.rs | 21 ++++++++----- src/platform_impl/windows/window.rs | 5 ++++ src/window.rs | 12 +++++++- 24 files changed, 182 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac26f34547..c0ad28321a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- Add `Window::cursor_position` and `EventLoopWindowTarget::cursor_position`. - Add `Window::is_minimized`. - On X11, fix errors handled during `register_xlib_error_hook` invocation bleeding into winit. - Add `Window::has_focus`. diff --git a/examples/window.rs b/examples/window.rs index 23a633d2ed..8b5ceaa9c9 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -17,9 +17,11 @@ fn main() { .build(&event_loop) .unwrap(); - event_loop.run(move |event, _, control_flow| { + event_loop.run(move |event, event_loop, control_flow| { + dbg!(event_loop.cursor_position().unwrap()); + control_flow.set_wait(); - println!("{:?}", event); + // println!("{:?}", event); match event { Event::WindowEvent { diff --git a/src/event_loop.rs b/src/event_loop.rs index abdd47ece1..fe3d924e6d 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -15,6 +15,8 @@ use instant::{Duration, Instant}; use once_cell::sync::OnceCell; use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle}; +use crate::dpi::PhysicalPosition; +use crate::error::ExternalError; use crate::{event::Event, monitor::MonitorHandle, platform_impl}; /// Provides a way to retrieve events from the system and from the windows that were registered to @@ -359,6 +361,16 @@ impl EventLoopWindowTarget { #[cfg(any(x11_platform, wayland_platform, windows))] self.p.set_device_event_filter(_filter); } + + /// Returns the current cursor position + /// + /// ## Platform-specific + /// + /// - **iOS / Android**: Unsupported. + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + self.p.cursor_position() + } } unsafe impl HasRawDisplayHandle for EventLoopWindowTarget { diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index d71e9521f7..1219a5c4d9 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -836,6 +836,12 @@ impl EventLoopWindowTarget { pub fn raw_display_handle(&self) -> RawDisplayHandle { RawDisplayHandle::Android(AndroidDisplayHandle::empty()) } + + pub fn cursor_position(&self) -> Result, error::ExternalError> { + Err(error::ExternalError::NotSupported( + error::NotSupportedError::new(), + )) + } } #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] @@ -1026,6 +1032,12 @@ impl Window { pub fn set_cursor_visible(&self, _: bool) {} + pub fn cursor_position(&self) -> Result, error::ExternalError> { + Err(error::ExternalError::NotSupported( + error::NotSupportedError::new(), + )) + } + pub fn drag_window(&self) -> Result<(), error::ExternalError> { Err(error::ExternalError::NotSupported( error::NotSupportedError::new(), diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 8649c8c7a8..7ed15f0c23 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -24,6 +24,7 @@ use super::view::WinitUIWindow; use super::{app_state, monitor, view, MonitorHandle}; use crate::{ dpi::LogicalSize, + error::{ExternalError, NotSupportedError}, event::Event, event_loop::{ ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootEventLoopWindowTarget, @@ -65,6 +66,10 @@ impl EventLoopWindowTarget { pub fn raw_display_handle(&self) -> RawDisplayHandle { RawDisplayHandle::UiKit(UiKitDisplayHandle::empty()) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } pub struct EventLoop { diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index 5a82c53201..776825cc8f 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -201,6 +201,10 @@ impl Inner { debug!("`Window::set_cursor_visible` is ignored on iOS") } + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } + pub fn drag_window(&self) -> Result<(), ExternalError> { Err(ExternalError::NotSupported(NotSupportedError::new())) } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index efefeea603..1049459f77 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -424,6 +424,11 @@ impl Window { x11_or_wayland!(match self; Window(window) => window.set_cursor_visible(visible)) } + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + x11_or_wayland!(match self; Window(window) => window.cursor_position()) + } + #[inline] pub fn drag_window(&self) -> Result<(), ExternalError> { x11_or_wayland!(match self; Window(window) => window.drag_window()) @@ -863,6 +868,10 @@ impl EventLoopWindowTarget { pub fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle { x11_or_wayland!(match self; Self(evlp) => evlp.raw_display_handle()) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + x11_or_wayland!(match self; Self(evlp) => evlp.cursor_position()) + } } fn sticky_exit_callback( diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 5a4c7e2cdc..6ae59799eb 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -19,6 +19,9 @@ use sctk::environment::Environment; use sctk::seat::pointer::{ThemeManager, ThemeSpec}; use sctk::WaylandSource; +use crate::dpi::PhysicalPosition; +use crate::error::ExternalError; +use crate::error::NotSupportedError; use crate::event::{Event, StartCause, WindowEvent}; use crate::event_loop::{ControlFlow, EventLoopWindowTarget as RootEventLoopWindowTarget}; use crate::platform_impl::platform::sticky_exit_callback; @@ -80,6 +83,10 @@ impl EventLoopWindowTarget { display_handle.display = self.display.get_display_ptr() as *mut _; RawDisplayHandle::Wayland(display_handle) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } pub struct EventLoop { diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index b6894556f7..8148374033 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -611,6 +611,11 @@ impl Window { Ok(()) } + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } + #[inline] pub fn set_ime_position(&self, position: Position) { let scale_factor = self.scale_factor(); diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 8f96c2dc5f..bf6a9696de 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -45,7 +45,8 @@ use self::{ util::modifiers::ModifierKeymap, }; use crate::{ - error::OsError as RootOsError, + dpi::PhysicalPosition, + error::{ExternalError, OsError as RootOsError}, event::{Event, StartCause}, event_loop::{ ControlFlow, DeviceEventFilter, EventLoopClosed, EventLoopWindowTarget as RootELW, @@ -574,6 +575,10 @@ impl EventLoopWindowTarget { unsafe { (self.xconn.xlib.XDefaultScreen)(self.xconn.display as *mut _) }; RawDisplayHandle::Xlib(display_handle) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + self.xconn.cursor_position(self.root) + } } impl EventLoopProxy { diff --git a/src/platform_impl/linux/x11/util/cursor.rs b/src/platform_impl/linux/x11/util/cursor.rs index 99b256356f..c405b8b95b 100644 --- a/src/platform_impl/linux/x11/util/cursor.rs +++ b/src/platform_impl/linux/x11/util/cursor.rs @@ -1,4 +1,4 @@ -use crate::window::CursorIcon; +use crate::{dpi::PhysicalPosition, error::ExternalError, window::CursorIcon}; use super::*; @@ -127,4 +127,32 @@ impl XConnection { self.flush_requests().expect("Failed to set the cursor"); } } + + pub fn cursor_position( + &self, + window: ffi::Window, + ) -> Result, ExternalError> { + let mut root_return = 0; + let mut child_return = 0; + let mut root_x_return = 0; + let mut root_y_return = 0; + let mut win_x_return = 0; + let mut win_y_return = 0; + let mut mask_return = 0; + + unsafe { + (self.xlib.XQueryPointer)( + self.display, + window, + &mut root_return, + &mut child_return, + &mut root_x_return, + &mut root_y_return, + &mut win_x_return, + &mut win_y_return, + &mut mask_return, + ); + } + Ok((root_x_return, root_y_return).into()) + } } diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 49036b1950..45d59b2356 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1427,6 +1427,10 @@ impl UnownedWindow { self.xconn.set_cursor_icon(self.xwindow, cursor); } + pub fn cursor_position(&self) -> Result, ExternalError> { + self.xconn.cursor_position(self.root) + } + #[inline] pub fn scale_factor(&self) -> f64 { self.current_monitor().scale_factor diff --git a/src/platform_impl/macos/appkit/event.rs b/src/platform_impl/macos/appkit/event.rs index a952157644..ec1fba6ece 100644 --- a/src/platform_impl/macos/appkit/event.rs +++ b/src/platform_impl/macos/appkit/event.rs @@ -70,6 +70,9 @@ extern_methods!( #[sel(locationInWindow)] pub fn locationInWindow(&self) -> NSPoint; + #[sel(mouseLocation)] + pub fn mouseLocation() -> NSPoint; + // TODO: MainThreadMarker #[sel(pressedMouseButtons)] pub fn pressedMouseButtons() -> NSUInteger; diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 72434ac478..3c8d81f102 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -21,8 +21,13 @@ use objc2::rc::{autoreleasepool, Id, Shared}; use objc2::{msg_send_id, ClassType}; use raw_window_handle::{AppKitDisplayHandle, RawDisplayHandle}; -use super::appkit::{NSApp, NSApplicationActivationPolicy, NSEvent}; +use super::{ + appkit::{NSApp, NSApplicationActivationPolicy, NSEvent}, + util, +}; use crate::{ + dpi::PhysicalPosition, + error::ExternalError, event::Event, event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootWindowTarget}, platform::macos::ActivationPolicy, @@ -91,6 +96,11 @@ impl EventLoopWindowTarget { pub fn raw_display_handle(&self) -> RawDisplayHandle { RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) } + + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + Ok(util::cursor_position()) + } } impl EventLoopWindowTarget { diff --git a/src/platform_impl/macos/util/mod.rs b/src/platform_impl/macos/util/mod.rs index 2cfdc47e84..b0e9e0b6b8 100644 --- a/src/platform_impl/macos/util/mod.rs +++ b/src/platform_impl/macos/util/mod.rs @@ -9,6 +9,8 @@ use objc2::foundation::{CGFloat, NSNotFound, NSPoint, NSRange, NSRect, NSUIntege use crate::dpi::LogicalPosition; +use super::appkit::NSEvent; + // Replace with `!` once stable #[derive(Debug)] pub enum Never {} @@ -63,3 +65,8 @@ pub fn window_position(position: LogicalPosition) -> NSPoint { CGDisplay::main().pixels_high() as CGFloat - position.y as CGFloat, ) } + +pub fn cursor_position() -> crate::dpi::PhysicalPosition { + let pt: NSPoint = NSEvent::mouseLocation(); + (pt.x, pt.y).into() +} diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 7ceaa82306..7ae8499f69 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -758,6 +758,11 @@ impl WinitWindow { } } + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + Ok(util::cursor_position()) + } + #[inline] pub fn scale_factor(&self) -> f64 { self.backingScaleFactor() as f64 diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index 9df7dbd113..c901aa0073 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -12,6 +12,7 @@ use orbclient::{ use raw_window_handle::{OrbitalDisplayHandle, RawDisplayHandle}; use crate::{ + error, event::{self, StartCause, VirtualKeyCode}, event_loop::{self, ControlFlow}, window::WindowId as RootWindowId, @@ -706,4 +707,10 @@ impl EventLoopWindowTarget { pub fn raw_display_handle(&self) -> RawDisplayHandle { RawDisplayHandle::Orbital(OrbitalDisplayHandle::empty()) } + + pub fn cursor_position(&self) -> Result, error::ExternalError> { + Err(error::ExternalError::NotSupported( + error::NotSupportedError::new(), + )) + } } diff --git a/src/platform_impl/orbital/window.rs b/src/platform_impl/orbital/window.rs index eda103d918..53b9a628bd 100644 --- a/src/platform_impl/orbital/window.rs +++ b/src/platform_impl/orbital/window.rs @@ -339,6 +339,12 @@ impl Window { )) } + pub fn cursor_position(&self) -> Result, error::ExternalError> { + Err(error::ExternalError::NotSupported( + error::NotSupportedError::new(), + )) + } + #[inline] pub fn set_cursor_grab(&self, _: window::CursorGrabMode) -> Result<(), error::ExternalError> { Err(error::ExternalError::NotSupported( diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 17aafd946d..77b5aac6c3 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -10,6 +10,7 @@ use super::{ window::WindowId, }; use crate::dpi::{PhysicalSize, Size}; +use crate::error::{ExternalError, NotSupportedError}; use crate::event::{ DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, KeyboardInput, Touch, TouchPhase, WindowEvent, @@ -352,4 +353,8 @@ impl EventLoopWindowTarget { pub fn raw_display_handle(&self) -> RawDisplayHandle { RawDisplayHandle::Web(WebDisplayHandle::empty()) } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 15d82ac088..0dc640512c 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -282,6 +282,10 @@ impl Window { Err(ExternalError::NotSupported(NotSupportedError::new())) } + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } + #[inline] pub fn set_minimized(&self, _minimized: bool) { // Intentionally a no-op, as canvases cannot be 'minimized' diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index d3215f128a..24f30bff9a 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -75,6 +75,7 @@ use windows_sys::Win32::{ use crate::{ dpi::{PhysicalPosition, PhysicalSize}, + error::ExternalError, event::{DeviceEvent, Event, Force, Ime, KeyboardInput, Touch, TouchPhase, WindowEvent}, event_loop::{ ControlFlow, DeviceEventFilter, EventLoopClosed, EventLoopWindowTarget as RootELW, @@ -333,6 +334,11 @@ impl EventLoopWindowTarget { pub fn set_device_event_filter(&self, filter: DeviceEventFilter) { raw_input::register_all_mice_and_keyboards_for_raw_input(self.thread_msg_target, filter); } + + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + util::cursor_position() + } } /// Returns the id of the main thread. diff --git a/src/platform_impl/windows/util.rs b/src/platform_impl/windows/util.rs index 1f94220c78..af4e54c447 100644 --- a/src/platform_impl/windows/util.rs +++ b/src/platform_impl/windows/util.rs @@ -13,7 +13,7 @@ use once_cell::sync::Lazy; use windows_sys::{ core::{HRESULT, PCWSTR}, Win32::{ - Foundation::{BOOL, HINSTANCE, HWND, RECT}, + Foundation::{BOOL, HINSTANCE, HWND, POINT, RECT}, Graphics::Gdi::{ClientToScreen, HMONITOR}, System::{ LibraryLoader::{GetProcAddress, LoadLibraryA}, @@ -23,17 +23,17 @@ use windows_sys::{ HiDpi::{DPI_AWARENESS_CONTEXT, MONITOR_DPI_TYPE, PROCESS_DPI_AWARENESS}, Input::KeyboardAndMouse::GetActiveWindow, WindowsAndMessaging::{ - ClipCursor, GetClientRect, GetClipCursor, GetSystemMetrics, GetWindowRect, - IsIconic, ShowCursor, IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, - IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, - IDC_WAIT, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN, - SM_YVIRTUALSCREEN, + ClipCursor, GetClientRect, GetClipCursor, GetCursorPos, GetSystemMetrics, + GetWindowRect, IsIconic, ShowCursor, IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, + IDC_HAND, IDC_HELP, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS, + IDC_SIZENWSE, IDC_SIZEWE, IDC_WAIT, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, + SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN, }, }, }, }; -use crate::window::CursorIcon; +use crate::{dpi::PhysicalPosition, error::ExternalError, window::CursorIcon}; pub fn encode_wide(string: impl AsRef) -> Vec { string.as_ref().encode_wide().chain(once(0)).collect() @@ -244,3 +244,10 @@ pub static SET_PROCESS_DPI_AWARENESS: Lazy> = Lazy::new(|| get_function!("shcore.dll", SetProcessDpiAwareness)); pub static SET_PROCESS_DPI_AWARE: Lazy> = Lazy::new(|| get_function!("user32.dll", SetProcessDPIAware)); + +pub fn cursor_position() -> Result, ExternalError> { + let mut pt = POINT { x: 0, y: 0 }; + win_to_err(unsafe { GetCursorPos(&mut pt) }) + .map(|_| (pt.x, pt.y).into()) + .map_err(|e| ExternalError::Os(os_error!(e))) +} diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index f6c6c92779..a3b44a3ed4 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -384,6 +384,11 @@ impl Window { rx.recv().unwrap().ok(); } + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + util::cursor_position() + } + #[inline] pub fn scale_factor(&self) -> f64 { self.window_state_lock().scale_factor diff --git a/src/window.rs b/src/window.rs index ab431ccc78..8c7037ac28 100644 --- a/src/window.rs +++ b/src/window.rs @@ -833,7 +833,7 @@ impl Window { /// Sets the window to minimized or back /// - /// ## Platform-specific + /// ## Platform-specific1 /// /// - **iOS / Android / Web / Orbital:** Unsupported. /// - **Wayland:** Un-minimize is unsupported. @@ -1231,6 +1231,16 @@ impl Window { pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> { self.window.set_cursor_hittest(hittest) } + + /// Returns the current cursor position. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / Wayland / Orbital / Web**: Unsupported, returns `None`. + #[inline] + pub fn cursor_position(&self) -> Result, ExternalError> { + self.window.cursor_position() + } } /// Monitor info functions. From 04aec3c11a2be52a357e999618141a79357a3df5 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 24 Jan 2023 19:00:27 +0200 Subject: [PATCH 02/18] Restore examples/window.rs --- examples/window.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index 8b5ceaa9c9..23a633d2ed 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -17,11 +17,9 @@ fn main() { .build(&event_loop) .unwrap(); - event_loop.run(move |event, event_loop, control_flow| { - dbg!(event_loop.cursor_position().unwrap()); - + event_loop.run(move |event, _, control_flow| { control_flow.set_wait(); - // println!("{:?}", event); + println!("{:?}", event); match event { Event::WindowEvent { From 7495b2f8fb8cce8973575ccbaa00c2cf953ab9a7 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 24 Jan 2023 19:01:50 +0200 Subject: [PATCH 03/18] update docs --- src/event_loop.rs | 4 ++-- src/window.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index fe3d924e6d..12eb107a96 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -362,11 +362,11 @@ impl EventLoopWindowTarget { self.p.set_device_event_filter(_filter); } - /// Returns the current cursor position + /// Returns the current cursor position in screen coordinates. /// /// ## Platform-specific /// - /// - **iOS / Android**: Unsupported. + /// - **iOS / Android / Wayland / Orbital / Web**: Unsupported. #[inline] pub fn cursor_position(&self) -> Result, ExternalError> { self.p.cursor_position() diff --git a/src/window.rs b/src/window.rs index 8c7037ac28..6d5eddb819 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1232,11 +1232,11 @@ impl Window { self.window.set_cursor_hittest(hittest) } - /// Returns the current cursor position. + /// Returns the current cursor position in screen coordinates. /// /// ## Platform-specific /// - /// - **iOS / Android / Wayland / Orbital / Web**: Unsupported, returns `None`. + /// - **iOS / Android / Wayland / Orbital / Web**: Unsupported. #[inline] pub fn cursor_position(&self) -> Result, ExternalError> { self.window.cursor_position() From d2bd7afdf11c70e094068ce6e4f948b94444f5a0 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 24 Jan 2023 19:14:34 +0200 Subject: [PATCH 04/18] fix build --- src/platform_impl/ios/event_loop.rs | 2 +- src/platform_impl/orbital/event_loop.rs | 1 + src/platform_impl/web/event_loop/window_target.rs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 7ed15f0c23..c94941a1b4 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -23,7 +23,7 @@ use super::uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen}; use super::view::WinitUIWindow; use super::{app_state, monitor, view, MonitorHandle}; use crate::{ - dpi::LogicalSize, + dpi::{LogicalSize, PhysicalPosition}, error::{ExternalError, NotSupportedError}, event::Event, event_loop::{ diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index c901aa0073..adfaa2cf2d 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -12,6 +12,7 @@ use orbclient::{ use raw_window_handle::{OrbitalDisplayHandle, RawDisplayHandle}; use crate::{ + dpi::PhysicalPosition, error, event::{self, StartCause, VirtualKeyCode}, event_loop::{self, ControlFlow}, diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 77b5aac6c3..b94dcbfd24 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -9,7 +9,7 @@ use super::{ super::monitor::MonitorHandle, backend, device::DeviceId, proxy::EventLoopProxy, runner, window::WindowId, }; -use crate::dpi::{PhysicalSize, Size}; +use crate::dpi::{PhysicalPosition, PhysicalSize, Size}; use crate::error::{ExternalError, NotSupportedError}; use crate::event::{ DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, KeyboardInput, Touch, TouchPhase, From 5b34692efd20a37b42822485ed1d71a8c3f13d96 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Mon, 30 Jan 2023 02:08:50 +0200 Subject: [PATCH 05/18] Update window.rs --- src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/window.rs b/src/window.rs index 6d5eddb819..ddebb42569 100644 --- a/src/window.rs +++ b/src/window.rs @@ -833,7 +833,7 @@ impl Window { /// Sets the window to minimized or back /// - /// ## Platform-specific1 + /// ## Platform-specific /// /// - **iOS / Android / Web / Orbital:** Unsupported. /// - **Wayland:** Un-minimize is unsupported. From 5555119d6c36806498825606234c8b12ca767058 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 7 Feb 2023 20:47:32 +0200 Subject: [PATCH 06/18] remove `Window::cursor_position` --- CHANGELOG.md | 14 +++++++------- src/platform_impl/android/mod.rs | 6 ------ src/platform_impl/ios/window.rs | 4 ---- src/platform_impl/linux/mod.rs | 5 ----- src/platform_impl/linux/wayland/window/mod.rs | 5 ----- src/platform_impl/linux/x11/window.rs | 4 ---- src/platform_impl/macos/window.rs | 5 ----- src/platform_impl/orbital/window.rs | 6 ------ src/platform_impl/web/window.rs | 4 ---- src/platform_impl/windows/window.rs | 5 ----- src/window.rs | 12 +----------- 11 files changed, 8 insertions(+), 62 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0ad28321a..2b4c74c178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased -- Add `Window::cursor_position` and `EventLoopWindowTarget::cursor_position`. +- Add `EventLoopWindowTarget::cursor_position`. - Add `Window::is_minimized`. - On X11, fix errors handled during `register_xlib_error_hook` invocation bleeding into winit. - Add `Window::has_focus`. @@ -301,7 +301,7 @@ And please only add new entries to the top of this list, right below the `# Unre - On macOS, fix inverted horizontal scroll. - **Breaking:** `current_monitor` now returns `Option`. - **Breaking:** `primary_monitor` now returns `Option`. -- On macOS, updated core-* dependencies and cocoa. +- On macOS, updated core-\* dependencies and cocoa. - Bump `parking_lot` to 0.11 - On Android, bump `ndk`, `ndk-sys` and `ndk-glue` to 0.2. Checkout the new ndk-glue main proc attribute. - On iOS, fixed starting the app in landscape where the view still had portrait dimensions. @@ -343,7 +343,7 @@ And please only add new entries to the top of this list, right below the `# Unre - On Windows, fix `WindowBuilder::with_maximized` being ignored. - On Android, minimal platform support. - On iOS, touch positions are now properly converted to physical pixels. -- On macOS, updated core-* dependencies and cocoa +- On macOS, updated core-\* dependencies and cocoa # 0.22.1 (2020-04-16) @@ -530,7 +530,7 @@ And please only add new entries to the top of this list, right below the `# Unre - On X11, non-resizable windows now have maximize explicitly disabled. - On Windows, support paths longer than MAX_PATH (260 characters) in `WindowEvent::DroppedFile` -and `WindowEvent::HoveredFile`. + and `WindowEvent::HoveredFile`. - On Mac, implement `DeviceEvent::Button`. - Change `Event::Suspended(true / false)` to `Event::Suspended` and `Event::Resumed`. - On X11, fix sanity check which checks that a monitor's reported width and height (in millimeters) are non-zero when calculating the DPI factor. @@ -872,20 +872,20 @@ _Yanked_ - Added event `WindowEvent::HiDPIFactorChanged`. - Added method `MonitorId::get_hidpi_factor`. - Deprecated `get_inner_size_pixels` and `get_inner_size_points` methods of `Window` in favor of -`get_inner_size`. + `get_inner_size`. - **Breaking:** `EventsLoop` is `!Send` and `!Sync` because of platform-dependant constraints, but `Window`, `WindowId`, `DeviceId` and `MonitorId` guaranteed to be `Send`. - `MonitorId::get_position` now returns `(i32, i32)` instead of `(u32, u32)`. - Rewrite of the wayland backend to use wayland-client-0.11 - Support for dead keys on wayland for keyboard utf8 input - Monitor enumeration on Windows is now implemented using `EnumDisplayMonitors` instead of -`EnumDisplayDevices`. This changes the value returned by `MonitorId::get_name()`. + `EnumDisplayDevices`. This changes the value returned by `MonitorId::get_name()`. - On Windows added `MonitorIdExt::hmonitor` method - Impl `Clone` for `EventsLoopProxy` - `EventsLoop::get_primary_monitor()` on X11 will fallback to any available monitor if no primary is found - Support for touch event on wayland - `WindowEvent`s `MouseMoved`, `MouseEntered`, and `MouseLeft` have been renamed to -`CursorMoved`, `CursorEntered`, and `CursorLeft`. + `CursorMoved`, `CursorEntered`, and `CursorLeft`. - New `DeviceEvent`s added, `MouseMotion` and `MouseWheel`. - Send `CursorMoved` event after `CursorEntered` and `Focused` events. - Add support for `ModifiersState`, `MouseMove`, `MouseInput`, `MouseMotion` for emscripten backend. diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 1219a5c4d9..26277c604f 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -1032,12 +1032,6 @@ impl Window { pub fn set_cursor_visible(&self, _: bool) {} - pub fn cursor_position(&self) -> Result, error::ExternalError> { - Err(error::ExternalError::NotSupported( - error::NotSupportedError::new(), - )) - } - pub fn drag_window(&self) -> Result<(), error::ExternalError> { Err(error::ExternalError::NotSupported( error::NotSupportedError::new(), diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index 776825cc8f..5a82c53201 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -201,10 +201,6 @@ impl Inner { debug!("`Window::set_cursor_visible` is ignored on iOS") } - pub fn cursor_position(&self) -> Result, ExternalError> { - Err(ExternalError::NotSupported(NotSupportedError::new())) - } - pub fn drag_window(&self) -> Result<(), ExternalError> { Err(ExternalError::NotSupported(NotSupportedError::new())) } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 1049459f77..e39e84e769 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -424,11 +424,6 @@ impl Window { x11_or_wayland!(match self; Window(window) => window.set_cursor_visible(visible)) } - #[inline] - pub fn cursor_position(&self) -> Result, ExternalError> { - x11_or_wayland!(match self; Window(window) => window.cursor_position()) - } - #[inline] pub fn drag_window(&self) -> Result<(), ExternalError> { x11_or_wayland!(match self; Window(window) => window.drag_window()) diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index 8148374033..b6894556f7 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -611,11 +611,6 @@ impl Window { Ok(()) } - #[inline] - pub fn cursor_position(&self) -> Result, ExternalError> { - Err(ExternalError::NotSupported(NotSupportedError::new())) - } - #[inline] pub fn set_ime_position(&self, position: Position) { let scale_factor = self.scale_factor(); diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 45d59b2356..49036b1950 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1427,10 +1427,6 @@ impl UnownedWindow { self.xconn.set_cursor_icon(self.xwindow, cursor); } - pub fn cursor_position(&self) -> Result, ExternalError> { - self.xconn.cursor_position(self.root) - } - #[inline] pub fn scale_factor(&self) -> f64 { self.current_monitor().scale_factor diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 7ae8499f69..7ceaa82306 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -758,11 +758,6 @@ impl WinitWindow { } } - #[inline] - pub fn cursor_position(&self) -> Result, ExternalError> { - Ok(util::cursor_position()) - } - #[inline] pub fn scale_factor(&self) -> f64 { self.backingScaleFactor() as f64 diff --git a/src/platform_impl/orbital/window.rs b/src/platform_impl/orbital/window.rs index 53b9a628bd..eda103d918 100644 --- a/src/platform_impl/orbital/window.rs +++ b/src/platform_impl/orbital/window.rs @@ -339,12 +339,6 @@ impl Window { )) } - pub fn cursor_position(&self) -> Result, error::ExternalError> { - Err(error::ExternalError::NotSupported( - error::NotSupportedError::new(), - )) - } - #[inline] pub fn set_cursor_grab(&self, _: window::CursorGrabMode) -> Result<(), error::ExternalError> { Err(error::ExternalError::NotSupported( diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 0dc640512c..15d82ac088 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -282,10 +282,6 @@ impl Window { Err(ExternalError::NotSupported(NotSupportedError::new())) } - pub fn cursor_position(&self) -> Result, ExternalError> { - Err(ExternalError::NotSupported(NotSupportedError::new())) - } - #[inline] pub fn set_minimized(&self, _minimized: bool) { // Intentionally a no-op, as canvases cannot be 'minimized' diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index a3b44a3ed4..f6c6c92779 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -384,11 +384,6 @@ impl Window { rx.recv().unwrap().ok(); } - #[inline] - pub fn cursor_position(&self) -> Result, ExternalError> { - util::cursor_position() - } - #[inline] pub fn scale_factor(&self) -> f64 { self.window_state_lock().scale_factor diff --git a/src/window.rs b/src/window.rs index 6d5eddb819..ab431ccc78 100644 --- a/src/window.rs +++ b/src/window.rs @@ -833,7 +833,7 @@ impl Window { /// Sets the window to minimized or back /// - /// ## Platform-specific1 + /// ## Platform-specific /// /// - **iOS / Android / Web / Orbital:** Unsupported. /// - **Wayland:** Un-minimize is unsupported. @@ -1231,16 +1231,6 @@ impl Window { pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> { self.window.set_cursor_hittest(hittest) } - - /// Returns the current cursor position in screen coordinates. - /// - /// ## Platform-specific - /// - /// - **iOS / Android / Wayland / Orbital / Web**: Unsupported. - #[inline] - pub fn cursor_position(&self) -> Result, ExternalError> { - self.window.cursor_position() - } } /// Monitor info functions. From 4d6d78d350a516f61f82ab07800017a0ff5b083e Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 26 Mar 2024 05:26:57 +0200 Subject: [PATCH 07/18] fix a few missed imports after merge --- src/event_loop.rs | 3 ++- src/platform_impl/android/mod.rs | 11 ++++------- src/platform_impl/ios/event_loop.rs | 11 ++++++----- src/platform_impl/linux/wayland/event_loop/mod.rs | 2 +- src/platform_impl/windows/event_loop.rs | 11 +++++------ 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 107d745c88..68f16f89fb 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -19,7 +19,8 @@ use std::time::{Duration, Instant}; use web_time::{Duration, Instant}; use crate::application::ApplicationHandler; -use crate::error::{EventLoopError, OsError}; +use crate::dpi::PhysicalPosition; +use crate::error::{EventLoopError, ExternalError, OsError}; use crate::window::{CustomCursor, CustomCursorSource, Window, WindowAttributes}; use crate::{event::Event, monitor::MonitorHandle, platform_impl}; diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 991c4cfead..2d0c937d93 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -21,7 +21,7 @@ use tracing::{debug, trace, warn}; use crate::{ cursor::Cursor, dpi::{PhysicalPosition, PhysicalSize, Position, Size}, - error, + error::{ExternalError, NotSupportedError}, event::{self, Force, InnerSizeWriter, StartCause}, event_loop::{self, ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents}, platform::pump_events::PumpStatus, @@ -732,6 +732,9 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } #[derive(Clone)] @@ -751,12 +754,6 @@ impl OwnedDisplayHandle { ) -> Result { Ok(rwh_06::AndroidDisplayHandle::new().into()) } - - pub fn cursor_position(&self) -> Result, error::ExternalError> { - Err(error::ExternalError::NotSupported( - error::NotSupportedError::new(), - )) - } } #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index d1ab319357..579b4e2791 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -17,7 +17,8 @@ use icrate::Foundation::{MainThreadMarker, NSString}; use objc2::ClassType; use crate::{ - error::EventLoopError, + dpi::PhysicalPosition, + error::{EventLoopError, ExternalError, NotSupportedError}, event::Event, event_loop::{ ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopClosed, @@ -95,6 +96,10 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } #[derive(Clone)] @@ -128,10 +133,6 @@ fn map_user_event( } } } - - pub fn cursor_position(&self) -> Result, ExternalError> { - Err(ExternalError::NotSupported(NotSupportedError::new())) - } } pub struct EventLoop { diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 5d41e831ea..ead00f1733 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -17,7 +17,7 @@ use sctk::reexports::client::{Connection, QueueHandle}; use crate::cursor::OnlyCursorImage; use crate::dpi::{LogicalSize, PhysicalPosition}; -use crate::error::{EventLoopError, ExternalError, OsError as RootOsError}; +use crate::error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError}; use crate::event::{Event, InnerSizeWriter, StartCause, WindowEvent}; use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents}; use crate::platform::pump_events::PumpStatus; diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 2bfda44310..4a688b6422 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -17,7 +17,7 @@ use std::{ time::{Duration, Instant}, }; -use crate::utils::Lazy; +use crate::{error::ExternalError, utils::Lazy}; use windows_sys::Win32::{ Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE, @@ -596,6 +596,10 @@ impl ActiveEventLoop { OwnedDisplayHandle } + pub fn cursor_position(&self) -> Result, ExternalError> { + util::cursor_position() + } + fn exit_code(&self) -> Option { self.runner_shared.exit_code() } @@ -618,11 +622,6 @@ impl OwnedDisplayHandle { ) -> Result { Ok(rwh_06::WindowsDisplayHandle::new().into()) } - - #[inline] - pub fn cursor_position(&self) -> Result, ExternalError> { - util::cursor_position() - } } /// Returns the id of the main thread. From 86506dbd76e8ab0eca3a5339b4686721e1f7c08d Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 26 Mar 2024 05:27:19 +0200 Subject: [PATCH 08/18] Discard changes to dpi/LICENSE From a6a763682dd4857bc54a7c993ccb750364b2462c Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 26 Mar 2024 05:27:23 +0200 Subject: [PATCH 09/18] Discard changes to dpi/LICENSE From f976f2f217f3ed38e783ddae45f4673d5da81da0 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 26 Mar 2024 05:29:58 +0200 Subject: [PATCH 10/18] revert license file change --- dpi/LICENSE | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 120000 dpi/LICENSE diff --git a/dpi/LICENSE b/dpi/LICENSE deleted file mode 100644 index ea5b60640b..0000000000 --- a/dpi/LICENSE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE \ No newline at end of file diff --git a/dpi/LICENSE b/dpi/LICENSE new file mode 120000 index 0000000000..ea5b60640b --- /dev/null +++ b/dpi/LICENSE @@ -0,0 +1 @@ +../LICENSE \ No newline at end of file From 5f811f87adea9ba37b5e2dd2f61ca881959f6f71 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 26 Mar 2024 05:30:54 +0200 Subject: [PATCH 11/18] Update unreleased.md --- src/changelog/unreleased.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 11e7d2fde3..3c26cb4be3 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -41,4 +41,4 @@ - Add `Window::default_attributes` to get default `WindowAttributes`. - `log` has been replaced with `tracing`. The old behavior can be emulated by setting the `log` feature on the `tracing` crate. - On Windows, confine cursor to center of window when grabbed and hidden. -- Add `EventLoopWindowTarget::cursor_position` +- Add `ActiveEventLoop::cursor_position` From e48ec284978c1025663f27af513ee5750d049aa1 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 26 Mar 2024 15:40:39 +0200 Subject: [PATCH 12/18] try fix build --- src/platform_impl/android/mod.rs | 8 ++- src/platform_impl/linux/x11/util/cursor.rs | 56 +++++++++---------- src/platform_impl/macos/cursor.rs | 11 +++- src/platform_impl/macos/event_loop.rs | 2 +- .../web/event_loop/window_target.rs | 8 +-- 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 2d0c937d93..205a1b5d3c 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -21,7 +21,7 @@ use tracing::{debug, trace, warn}; use crate::{ cursor::Cursor, dpi::{PhysicalPosition, PhysicalSize, Position, Size}, - error::{ExternalError, NotSupportedError}, + error, event::{self, Force, InnerSizeWriter, StartCause}, event_loop::{self, ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents}, platform::pump_events::PumpStatus, @@ -732,8 +732,10 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } - pub fn cursor_position(&self) -> Result, ExternalError> { - Err(ExternalError::NotSupported(NotSupportedError::new())) + pub fn cursor_position(&self) -> Result, error::ExternalError> { + Err(error::ExternalError::NotSupported( + error::NotSupportedError::new(), + )) } } diff --git a/src/platform_impl/linux/x11/util/cursor.rs b/src/platform_impl/linux/x11/util/cursor.rs index c03d9969ea..34225a9ea9 100644 --- a/src/platform_impl/linux/x11/util/cursor.rs +++ b/src/platform_impl/linux/x11/util/cursor.rs @@ -98,6 +98,34 @@ impl XConnection { self.xcb_connection().flush()?; Ok(()) } + + pub fn cursor_position( + &self, + window: ffi::Window, + ) -> Result, ExternalError> { + let mut root_return = 0; + let mut child_return = 0; + let mut root_x_return = 0; + let mut root_y_return = 0; + let mut win_x_return = 0; + let mut win_y_return = 0; + let mut mask_return = 0; + + unsafe { + (self.xlib.XQueryPointer)( + self.display, + window, + &mut root_return, + &mut child_return, + &mut root_x_return, + &mut root_y_return, + &mut win_x_return, + &mut win_y_return, + &mut mask_return, + ); + } + Ok((root_x_return, root_y_return).into()) + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -161,34 +189,6 @@ impl CustomCursor { } } } - - pub fn cursor_position( - &self, - window: ffi::Window, - ) -> Result, ExternalError> { - let mut root_return = 0; - let mut child_return = 0; - let mut root_x_return = 0; - let mut root_y_return = 0; - let mut win_x_return = 0; - let mut win_y_return = 0; - let mut mask_return = 0; - - unsafe { - (self.xlib.XQueryPointer)( - self.display, - window, - &mut root_return, - &mut child_return, - &mut root_x_return, - &mut root_y_return, - &mut win_x_return, - &mut win_y_return, - &mut mask_return, - ); - } - Ok((root_x_return, root_y_return).into()) - } } #[derive(Debug)] diff --git a/src/platform_impl/macos/cursor.rs b/src/platform_impl/macos/cursor.rs index afa4fe8839..f9d1a57719 100644 --- a/src/platform_impl/macos/cursor.rs +++ b/src/platform_impl/macos/cursor.rs @@ -2,10 +2,10 @@ use std::ffi::c_uchar; use std::slice; use std::sync::OnceLock; -use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSImage}; +use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSEvent, NSImage}; use icrate::Foundation::{ - ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSSize, - NSString, + ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSPoint, + NSSize, NSString, }; use objc2::rc::Id; use objc2::runtime::Sel; @@ -224,3 +224,8 @@ pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Id { _ => default_cursor(), } } + +pub fn cursor_position() -> crate::dpi::PhysicalPosition { + let pt: NSPoint = NSEvent::mouseLocation(); + (pt.x, pt.y).into() +} diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index d6841a16c8..4dec36a704 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -151,7 +151,7 @@ impl ActiveEventLoop { #[inline] pub fn cursor_position(&self) -> Result, ExternalError> { - Ok(util::cursor_position()) + Ok(super::cursor::cursor_position()) } } diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 42d730bc96..cdc2be33bb 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -724,6 +724,10 @@ impl ActiveEventLoop { pub(crate) fn owned_display_handle(&self) -> OwnedDisplayHandle { OwnedDisplayHandle } + + pub fn cursor_position(&self) -> Result, ExternalError> { + Err(ExternalError::NotSupported(NotSupportedError::new())) + } } #[derive(Clone)] @@ -743,8 +747,4 @@ impl OwnedDisplayHandle { ) -> Result { Ok(rwh_06::WebDisplayHandle::new().into()) } - - pub fn cursor_position(&self) -> Result, ExternalError> { - Err(ExternalError::NotSupported(NotSupportedError::new())) - } } From 37dbcc7f75337f058d1a10f540a8f4b3c0c7ada0 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 26 Mar 2024 15:41:05 +0200 Subject: [PATCH 13/18] Update src/changelog/unreleased.md Co-authored-by: daxpedda --- src/changelog/unreleased.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 3c26cb4be3..5dcbffd87d 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -41,4 +41,4 @@ - Add `Window::default_attributes` to get default `WindowAttributes`. - `log` has been replaced with `tracing`. The old behavior can be emulated by setting the `log` feature on the `tracing` crate. - On Windows, confine cursor to center of window when grabbed and hidden. -- Add `ActiveEventLoop::cursor_position` +- Add `ActiveEventLoop::cursor_position`. From 9ee1fd6ccf381e13e389c3e9d1228386a98deefa Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 26 Mar 2024 15:58:56 +0200 Subject: [PATCH 14/18] duplicate import --- src/platform_impl/macos/cursor.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/macos/cursor.rs b/src/platform_impl/macos/cursor.rs index f9d1a57719..3f9c51765c 100644 --- a/src/platform_impl/macos/cursor.rs +++ b/src/platform_impl/macos/cursor.rs @@ -4,8 +4,8 @@ use std::sync::OnceLock; use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSEvent, NSImage}; use icrate::Foundation::{ - ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSPoint, - NSSize, NSString, + ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSSize, + NSString, }; use objc2::rc::Id; use objc2::runtime::Sel; From 261a709813bd8588435bd1dbb9422e3312bef0fe Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 26 Mar 2024 16:20:07 +0200 Subject: [PATCH 15/18] fix x11 impl --- src/platform_impl/linux/x11/mod.rs | 4 ++- src/platform_impl/linux/x11/util/cursor.rs | 29 +++------------------- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 7acee980b2..f3c30df358 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -760,7 +760,9 @@ impl ActiveEventLoop { } pub fn cursor_position(&self) -> Result, ExternalError> { - self.xconn.cursor_position(self.root) + self.xconn + .cursor_position(self.root) + .map_err(|x_err| ExternalError::Os(os_error!(OsError::XError(Arc::new(x_err))))) } } diff --git a/src/platform_impl/linux/x11/util/cursor.rs b/src/platform_impl/linux/x11/util/cursor.rs index 34225a9ea9..f4a2da6b9c 100644 --- a/src/platform_impl/linux/x11/util/cursor.rs +++ b/src/platform_impl/linux/x11/util/cursor.rs @@ -8,7 +8,6 @@ use std::{ use x11rb::connection::Connection; use crate::dpi::PhysicalPosition; -use crate::error::ExternalError; use crate::{platform_impl::PlatformCustomCursorSource, window::CursorIcon}; use super::super::ActiveEventLoop; @@ -101,30 +100,10 @@ impl XConnection { pub fn cursor_position( &self, - window: ffi::Window, - ) -> Result, ExternalError> { - let mut root_return = 0; - let mut child_return = 0; - let mut root_x_return = 0; - let mut root_y_return = 0; - let mut win_x_return = 0; - let mut win_y_return = 0; - let mut mask_return = 0; - - unsafe { - (self.xlib.XQueryPointer)( - self.display, - window, - &mut root_return, - &mut child_return, - &mut root_x_return, - &mut root_y_return, - &mut win_x_return, - &mut win_y_return, - &mut mask_return, - ); - } - Ok((root_x_return, root_y_return).into()) + window: xproto::Window, + ) -> Result, X11Error> { + let reply = self.xcb_connection().query_pointer(window)?.reply()?; + Ok((reply.root_x, reply.root_y).into()) } } From 76ba0b6c458bdd48812137913159af50843f5c97 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 26 Mar 2024 16:25:01 +0200 Subject: [PATCH 16/18] unsafe --- src/platform_impl/macos/cursor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform_impl/macos/cursor.rs b/src/platform_impl/macos/cursor.rs index 3f9c51765c..3833eb8171 100644 --- a/src/platform_impl/macos/cursor.rs +++ b/src/platform_impl/macos/cursor.rs @@ -226,6 +226,6 @@ pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Id { } pub fn cursor_position() -> crate::dpi::PhysicalPosition { - let pt: NSPoint = NSEvent::mouseLocation(); + let pt: NSPoint = unsafe { NSEvent::mouseLocation() }; (pt.x, pt.y).into() } From 1f9a3df3ff39eca403d0963dbee72267fdb29a2b Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Fri, 29 Mar 2024 03:08:37 +0200 Subject: [PATCH 17/18] reference `Window::outer_position` --- src/event_loop.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 68f16f89fb..4a66c640f3 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -492,7 +492,9 @@ impl ActiveEventLoop { } } - /// Returns the current cursor position in screen coordinates. + /// Returns the current cursor position relative to the top-left hand corner of the desktop. + /// + /// See [`Window::outer_position`] for more information about the coordinates. /// /// ## Platform-specific /// From cd6b857d6986979b3ddb611cc4e1832f84beef79 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 29 Mar 2024 17:50:17 +0100 Subject: [PATCH 18/18] macOS: Fix cursor position calculation We needed to flip and scale the coordinates, as is usual for macOS' screen coordinates. --- src/platform_impl/macos/cursor.rs | 7 +------ src/platform_impl/macos/event_loop.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/platform_impl/macos/cursor.rs b/src/platform_impl/macos/cursor.rs index 3833eb8171..afa4fe8839 100644 --- a/src/platform_impl/macos/cursor.rs +++ b/src/platform_impl/macos/cursor.rs @@ -2,7 +2,7 @@ use std::ffi::c_uchar; use std::slice; use std::sync::OnceLock; -use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSEvent, NSImage}; +use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSImage}; use icrate::Foundation::{ ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSSize, NSString, @@ -224,8 +224,3 @@ pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Id { _ => default_cursor(), } } - -pub fn cursor_position() -> crate::dpi::PhysicalPosition { - let pt: NSPoint = unsafe { NSEvent::mouseLocation() }; - (pt.x, pt.y).into() -} diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 4dec36a704..f9daaebac0 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -18,9 +18,9 @@ use core_foundation::runloop::{ }; use icrate::AppKit::{ NSApplication, NSApplicationActivationPolicyAccessory, NSApplicationActivationPolicyProhibited, - NSApplicationActivationPolicyRegular, NSWindow, + NSApplicationActivationPolicyRegular, NSEvent, NSWindow, }; -use icrate::Foundation::{MainThreadMarker, NSObjectProtocol}; +use icrate::Foundation::{CGRect, CGSize, MainThreadMarker, NSObjectProtocol}; use objc2::{msg_send_id, ClassType}; use objc2::{ rc::{autoreleasepool, Id}, @@ -34,7 +34,7 @@ use super::{ monitor::{self, MonitorHandle}, observer::setup_control_flow_observers, }; -use crate::dpi::PhysicalPosition; +use crate::dpi::{LogicalPosition, PhysicalPosition}; use crate::error::ExternalError; use crate::platform_impl::platform::cursor::CustomCursor; use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource}; @@ -151,7 +151,12 @@ impl ActiveEventLoop { #[inline] pub fn cursor_position(&self) -> Result, ExternalError> { - Ok(super::cursor::cursor_position()) + let point = unsafe { NSEvent::mouseLocation() }; + // The cursor is sizeless + let rect = CGRect::new(point, CGSize::new(0.0, 0.0)); + let point = monitor::flip_window_screen_coordinates(rect); + let point = LogicalPosition::new(point.x, point.y); + Ok(point.to_physical(monitor::primary_monitor().scale_factor())) } }