From da2268ae226951405839cb4fbd29dc72077d7808 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 8 Oct 2024 15:29:40 +0200 Subject: [PATCH] chore: remove platform WindowId's WindowId is a window _identifier_, and as such doesn't store anything (unlike a _handle_). So we can safely make only be defined once, in the core crate. There are a few backends where we still use `into_raw` internally; I consider these patterns discouraged, we should not be passing around important state in the window id. --- src/platform_impl/android/mod.rs | 52 ++++------- src/platform_impl/apple/appkit/app_state.rs | 8 +- src/platform_impl/apple/appkit/mod.rs | 2 +- src/platform_impl/apple/appkit/view.rs | 3 +- src/platform_impl/apple/appkit/window.rs | 19 +--- .../apple/appkit/window_delegate.rs | 6 +- src/platform_impl/apple/uikit/app_state.rs | 9 +- src/platform_impl/apple/uikit/mod.rs | 2 +- src/platform_impl/apple/uikit/view.rs | 22 ++--- src/platform_impl/apple/uikit/window.rs | 30 ++----- src/platform_impl/linux/mod.rs | 13 --- .../linux/wayland/event_loop/mod.rs | 8 +- .../linux/wayland/event_loop/sink.rs | 5 +- src/platform_impl/linux/wayland/mod.rs | 4 +- .../linux/wayland/types/xdg_activation.rs | 3 +- src/platform_impl/linux/wayland/window/mod.rs | 10 +-- .../linux/wayland/window/state.rs | 4 +- .../linux/x11/event_processor.rs | 4 +- src/platform_impl/linux/x11/mod.rs | 23 +++-- src/platform_impl/linux/x11/window.rs | 19 ++-- src/platform_impl/orbital/event_loop.rs | 86 +++++++------------ src/platform_impl/orbital/mod.rs | 15 ---- src/platform_impl/orbital/window.rs | 12 +-- src/platform_impl/web/event_loop/mod.rs | 2 +- src/platform_impl/web/event_loop/runner.rs | 4 +- .../web/event_loop/window_target.rs | 65 +++++++------- src/platform_impl/web/mod.rs | 2 +- src/platform_impl/web/web_sys/canvas.rs | 7 +- src/platform_impl/web/window.rs | 23 ++--- src/platform_impl/windows/drop_handler.rs | 9 +- src/platform_impl/windows/event_loop.rs | 78 ++++++++--------- .../windows/event_loop/runner.rs | 13 ++- src/platform_impl/windows/mod.rs | 21 ----- src/platform_impl/windows/window.rs | 10 +-- src/window.rs | 12 +-- 35 files changed, 226 insertions(+), 379 deletions(-) diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 771c9735db..62fd036f50 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -24,7 +24,8 @@ use crate::monitor::MonitorHandle as RootMonitorHandle; use crate::platform::pump_events::PumpStatus; use crate::window::{ self, CursorGrabMode, CustomCursor, CustomCursorSource, Fullscreen, ImePurpose, - ResizeDirection, Theme, Window as CoreWindow, WindowAttributes, WindowButtons, WindowLevel, + ResizeDirection, Theme, Window as CoreWindow, WindowAttributes, WindowButtons, WindowId, + WindowLevel, }; mod keycodes; @@ -122,6 +123,9 @@ impl Default for PlatformSpecificEventLoopAttributes { } } +// Android currently only supports one window +const GLOBAL_WINDOW: WindowId = WindowId::from_raw(0); + impl EventLoop { pub(crate) fn new( attributes: &PlatformSpecificEventLoopAttributes, @@ -187,22 +191,19 @@ impl EventLoop { }, MainEvent::GainedFocus => { HAS_FOCUS.store(true, Ordering::Relaxed); - let window_id = window::WindowId(WindowId); let event = event::WindowEvent::Focused(true); - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); }, MainEvent::LostFocus => { HAS_FOCUS.store(false, Ordering::Relaxed); - let window_id = window::WindowId(WindowId); let event = event::WindowEvent::Focused(false); - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); }, MainEvent::ConfigChanged { .. } => { let old_scale_factor = scale_factor(&self.android_app); let scale_factor = scale_factor(&self.android_app); if (scale_factor - old_scale_factor).abs() < f64::EPSILON { let new_surface_size = Arc::new(Mutex::new(screen_size(&self.android_app))); - let window_id = window::WindowId(WindowId); let event = event::WindowEvent::ScaleFactorChanged { surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade( &new_surface_size, @@ -210,7 +211,7 @@ impl EventLoop { scale_factor, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); } }, MainEvent::LowMemory => { @@ -286,17 +287,15 @@ impl EventLoop { } else { PhysicalSize::new(0, 0) }; - let window_id = window::WindowId(WindowId); let event = event::WindowEvent::SurfaceResized(size); - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); } pending_redraw |= self.redraw_flag.get_and_reset(); if pending_redraw { pending_redraw = false; - let window_id = window::WindowId(WindowId); let event = event::WindowEvent::RedrawRequested; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); } } @@ -315,7 +314,6 @@ impl EventLoop { let mut input_status = InputStatus::Handled; match event { InputEvent::MotionEvent(motion_event) => { - let window_id = window::WindowId(WindowId); let device_id = Some(event::DeviceId(DeviceId(motion_event.device_id()))); let action = motion_event.action(); @@ -361,7 +359,7 @@ impl EventLoop { _ => event::PointerKind::Unknown, }, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); let event = event::WindowEvent::PointerButton { device_id, state: event::ElementState::Pressed, @@ -375,7 +373,7 @@ impl EventLoop { _ => event::ButtonSource::Unknown(0), }, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); }, MotionAction::Move => { let event = event::WindowEvent::PointerMoved { @@ -390,7 +388,7 @@ impl EventLoop { _ => event::PointerSource::Unknown, }, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); }, MotionAction::Up | MotionAction::PointerUp | MotionAction::Cancel => { if let MotionAction::Up | MotionAction::PointerUp = action { @@ -407,7 +405,7 @@ impl EventLoop { _ => event::ButtonSource::Unknown(0), }, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); } let event = event::WindowEvent::PointerLeft { @@ -422,7 +420,7 @@ impl EventLoop { _ => event::PointerKind::Unknown, }, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); }, _ => unreachable!(), } @@ -453,7 +451,6 @@ impl EventLoop { &mut self.combining_accent, ); - let window_id = window::WindowId(WindowId); let event = event::WindowEvent::KeyboardInput { device_id: Some(event::DeviceId(DeviceId(key.device_id()))), event: event::KeyEvent { @@ -468,7 +465,7 @@ impl EventLoop { is_synthetic: false, }; - app.window_event(&self.window_target, window_id, event); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); }, } }, @@ -731,19 +728,6 @@ impl OwnedDisplayHandle { } } -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub(crate) struct WindowId; - -impl WindowId { - pub const fn into_raw(self) -> u64 { - 0 - } - - pub const fn from_raw(_id: u64) -> Self { - Self - } -} - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct DeviceId(i32); @@ -824,8 +808,8 @@ impl rwh_06::HasWindowHandle for Window { } impl CoreWindow for Window { - fn id(&self) -> window::WindowId { - window::WindowId(WindowId) + fn id(&self) -> WindowId { + GLOBAL_WINDOW } fn primary_monitor(&self) -> Option { diff --git a/src/platform_impl/apple/appkit/app_state.rs b/src/platform_impl/apple/appkit/app_state.rs index a91fe0253a..6c1769ff3b 100644 --- a/src/platform_impl/apple/appkit/app_state.rs +++ b/src/platform_impl/apple/appkit/app_state.rs @@ -10,12 +10,12 @@ use objc2_foundation::{MainThreadMarker, NSNotification}; use super::super::event_handler::EventHandler; use super::event_loop::{stop_app_immediately, ActiveEventLoop, PanicInfo}; +use super::menu; use super::observer::{EventLoopWaker, RunLoop}; -use super::{menu, WindowId}; use crate::application::ApplicationHandler; use crate::event::{StartCause, WindowEvent}; use crate::event_loop::ControlFlow; -use crate::window::WindowId as RootWindowId; +use crate::window::WindowId; #[derive(Debug)] pub(super) struct AppState { @@ -245,7 +245,7 @@ impl AppState { // -> Don't go back into the event handler when our callstack originates from there if !self.event_handler.in_use() { self.with_handler(|app, event_loop| { - app.window_event(event_loop, RootWindowId(window_id), WindowEvent::RedrawRequested); + app.window_event(event_loop, window_id, WindowEvent::RedrawRequested); }); // `pump_events` will request to stop immediately _after_ dispatching RedrawRequested @@ -357,7 +357,7 @@ impl AppState { let redraw = mem::take(&mut *self.pending_redraw.borrow_mut()); for window_id in redraw { self.with_handler(|app, event_loop| { - app.window_event(event_loop, RootWindowId(window_id), WindowEvent::RedrawRequested); + app.window_event(event_loop, window_id, WindowEvent::RedrawRequested); }); } self.with_handler(|app, event_loop| { diff --git a/src/platform_impl/apple/appkit/mod.rs b/src/platform_impl/apple/appkit/mod.rs index c34e3a9c24..735b941821 100644 --- a/src/platform_impl/apple/appkit/mod.rs +++ b/src/platform_impl/apple/appkit/mod.rs @@ -21,7 +21,7 @@ pub(crate) use self::event_loop::{ PlatformSpecificEventLoopAttributes, }; pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle}; -pub(crate) use self::window::{Window, WindowId}; +pub(crate) use self::window::Window; pub(crate) use self::window_delegate::PlatformSpecificWindowAttributes; pub(crate) use crate::cursor::OnlyCursorImageSource as PlatformCustomCursorSource; pub(crate) use crate::icon::NoIcon as PlatformIcon; diff --git a/src/platform_impl/apple/appkit/view.rs b/src/platform_impl/apple/appkit/view.rs index b8d878da0e..357e1cd648 100644 --- a/src/platform_impl/apple/appkit/view.rs +++ b/src/platform_impl/apple/appkit/view.rs @@ -31,7 +31,6 @@ use crate::event::{ }; use crate::keyboard::{Key, KeyCode, KeyLocation, ModifiersState, NamedKey}; use crate::platform::macos::OptionAsAlt; -use crate::window::WindowId as RootWindowId; #[derive(Debug)] struct CursorState { @@ -842,7 +841,7 @@ impl WinitView { } fn queue_event(&self, event: WindowEvent) { - let window_id = RootWindowId(self.window().id()); + let window_id = self.window().id(); self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| { app.window_event(event_loop, window_id, event); }); diff --git a/src/platform_impl/apple/appkit/window.rs b/src/platform_impl/apple/appkit/window.rs index 1fcdb4c8f5..9373042ae8 100644 --- a/src/platform_impl/apple/appkit/window.rs +++ b/src/platform_impl/apple/appkit/window.rs @@ -12,7 +12,7 @@ use crate::error::RequestError; use crate::monitor::MonitorHandle as CoreMonitorHandle; use crate::window::{ Cursor, Fullscreen, Icon, ImePurpose, Theme, UserAttentionType, Window as CoreWindow, - WindowAttributes, WindowButtons, WindowLevel, + WindowAttributes, WindowButtons, WindowId, WindowLevel, }; pub(crate) struct Window { @@ -92,7 +92,7 @@ impl rwh_06::HasWindowHandle for Window { impl CoreWindow for Window { fn id(&self) -> crate::window::WindowId { - self.maybe_wait_on_main(|delegate| crate::window::WindowId(delegate.id())) + self.maybe_wait_on_main(|delegate| delegate.id()) } fn scale_factor(&self) -> f64 { @@ -335,19 +335,6 @@ impl CoreWindow for Window { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(pub usize); - -impl WindowId { - pub const fn into_raw(self) -> u64 { - self.0 as u64 - } - - pub const fn from_raw(id: u64) -> Self { - Self(id as usize) - } -} - declare_class!( #[derive(Debug)] pub struct WinitWindow; @@ -378,6 +365,6 @@ declare_class!( impl WinitWindow { pub(super) fn id(&self) -> WindowId { - WindowId(self as *const Self as usize) + WindowId::from_raw(self as *const Self as usize) } } diff --git a/src/platform_impl/apple/appkit/window_delegate.rs b/src/platform_impl/apple/appkit/window_delegate.rs index a4bf1f70b7..c78ce2921d 100644 --- a/src/platform_impl/apple/appkit/window_delegate.rs +++ b/src/platform_impl/apple/appkit/window_delegate.rs @@ -33,14 +33,14 @@ use super::monitor::{self, flip_window_screen_coordinates, get_display_id}; use super::observer::RunLoop; use super::view::WinitView; use super::window::WinitWindow; -use super::{ffi, Fullscreen, MonitorHandle, WindowId}; +use super::{ffi, Fullscreen, MonitorHandle}; use crate::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}; use crate::error::{NotSupportedError, RequestError}; use crate::event::{SurfaceSizeWriter, WindowEvent}; use crate::platform::macos::{OptionAsAlt, WindowExtMacOS}; use crate::window::{ Cursor, CursorGrabMode, Icon, ImePurpose, ResizeDirection, Theme, UserAttentionType, - WindowAttributes, WindowButtons, WindowId as RootWindowId, WindowLevel, + WindowAttributes, WindowButtons, WindowId, WindowLevel, }; #[derive(Clone, Debug, PartialEq)] @@ -819,7 +819,7 @@ impl WindowDelegate { } pub(crate) fn queue_event(&self, event: WindowEvent) { - let window_id = RootWindowId(self.window().id()); + let window_id = self.window().id(); self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| { app.window_event(event_loop, window_id, event); }); diff --git a/src/platform_impl/apple/uikit/app_state.rs b/src/platform_impl/apple/uikit/app_state.rs index 1c33dcb63c..4b5880d170 100644 --- a/src/platform_impl/apple/uikit/app_state.rs +++ b/src/platform_impl/apple/uikit/app_state.rs @@ -29,7 +29,6 @@ use crate::application::ApplicationHandler; use crate::dpi::PhysicalSize; use crate::event::{Event, StartCause, SurfaceSizeWriter, WindowEvent}; use crate::event_loop::ControlFlow; -use crate::window::WindowId as RootWindowId; macro_rules! bug { ($($msg:tt)*) => { @@ -599,7 +598,7 @@ pub(crate) fn send_occluded_event_for_all_windows(application: &UIApplication, o &*ptr }; events.push(EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::Occluded(occluded), })); } @@ -626,7 +625,7 @@ pub fn handle_main_events_cleared(mtm: MainThreadMarker) { .into_iter() .map(|window| { EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::RedrawRequested, }) }) @@ -655,7 +654,7 @@ pub(crate) fn terminated(application: &UIApplication) { &*ptr }; events.push(EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::Destroyed, })); } @@ -673,7 +672,7 @@ fn handle_hidpi_proxy(mtm: MainThreadMarker, event: ScaleFactorChanged) { let ScaleFactorChanged { suggested_size, scale_factor, window } = event; let new_surface_size = Arc::new(Mutex::new(suggested_size)); let event = Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::ScaleFactorChanged { scale_factor, surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&new_surface_size)), diff --git a/src/platform_impl/apple/uikit/mod.rs b/src/platform_impl/apple/uikit/mod.rs index ee581aa9ef..43109f83d3 100644 --- a/src/platform_impl/apple/uikit/mod.rs +++ b/src/platform_impl/apple/uikit/mod.rs @@ -14,7 +14,7 @@ pub(crate) use self::event_loop::{ PlatformSpecificEventLoopAttributes, }; pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle}; -pub(crate) use self::window::{PlatformSpecificWindowAttributes, Window, WindowId}; +pub(crate) use self::window::{PlatformSpecificWindowAttributes, Window}; pub(crate) use crate::cursor::{ NoCustomCursor as PlatformCustomCursor, NoCustomCursor as PlatformCustomCursorSource, }; diff --git a/src/platform_impl/apple/uikit/view.rs b/src/platform_impl/apple/uikit/view.rs index b11d01354c..e4c1844006 100644 --- a/src/platform_impl/apple/uikit/view.rs +++ b/src/platform_impl/apple/uikit/view.rs @@ -22,7 +22,7 @@ use crate::event::{ }; use crate::keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKeyCode, PhysicalKey}; use crate::platform_impl::KeyEventExtra; -use crate::window::{WindowAttributes, WindowId as RootWindowId}; +use crate::window::WindowAttributes; pub struct WinitViewState { pinch_gesture_recognizer: RefCell>>, @@ -58,7 +58,7 @@ declare_class!( app_state::handle_nonuser_event( mtm, EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::RedrawRequested, }), ); @@ -93,7 +93,7 @@ declare_class!( app_state::handle_nonuser_event( mtm, EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::SurfaceResized(size), }), ); @@ -132,7 +132,7 @@ declare_class!( width: screen_frame.size.width as f64, height: screen_frame.size.height as f64, }; - let window_id = RootWindowId(window.id()); + let window_id = window.id(); app_state::handle_nonuser_events( mtm, std::iter::once(EventWrapper::ScaleFactorChanged( @@ -197,7 +197,7 @@ declare_class!( }; let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::PinchGesture { device_id: None, delta: delta as f64, @@ -215,7 +215,7 @@ declare_class!( if recognizer.state() == UIGestureRecognizerState::Ended { let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::DoubleTapGesture { device_id: None, }, @@ -257,7 +257,7 @@ declare_class!( // Make delta negative to match macos, convert to degrees let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::RotationGesture { device_id: None, delta: -delta.to_degrees() as _, @@ -308,7 +308,7 @@ declare_class!( let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { - window_id: RootWindowId(window.id()), + window_id: window.id(), event: WindowEvent::PanGesture { device_id: None, delta: PhysicalPosition::new(dx as _, dy as _), @@ -512,7 +512,7 @@ impl WinitView { scale_factor as f64, ) }; - let window_id = RootWindowId(window.id()); + let window_id = window.id(); let finger_id = RootFingerId(FingerId(touch_id)); match phase { @@ -597,7 +597,7 @@ impl WinitView { fn handle_insert_text(&self, text: &NSString) { let window = self.window().unwrap(); - let window_id = RootWindowId(window.id()); + let window_id = window.id(); let mtm = MainThreadMarker::new().unwrap(); // send individual events for each character app_state::handle_nonuser_events( @@ -635,7 +635,7 @@ impl WinitView { fn handle_delete_backward(&self) { let window = self.window().unwrap(); - let window_id = RootWindowId(window.id()); + let window_id = window.id(); let mtm = MainThreadMarker::new().unwrap(); app_state::handle_nonuser_events( mtm, diff --git a/src/platform_impl/apple/uikit/window.rs b/src/platform_impl/apple/uikit/window.rs index 93f8b00aa4..1be3a555c1 100644 --- a/src/platform_impl/apple/uikit/window.rs +++ b/src/platform_impl/apple/uikit/window.rs @@ -26,7 +26,7 @@ use crate::monitor::MonitorHandle as CoreMonitorHandle; use crate::platform::ios::{ScreenEdge, StatusBarStyle, ValidOrientations}; use crate::window::{ CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, Window as CoreWindow, - WindowAttributes, WindowButtons, WindowId as CoreWindowId, WindowLevel, + WindowAttributes, WindowButtons, WindowId, WindowLevel, }; declare_class!( @@ -49,7 +49,7 @@ declare_class!( app_state::handle_nonuser_event( mtm, EventWrapper::StaticEvent(Event::WindowEvent { - window_id: CoreWindowId(self.id()), + window_id: self.id(), event: WindowEvent::Focused(true), }), ); @@ -62,7 +62,7 @@ declare_class!( app_state::handle_nonuser_event( mtm, EventWrapper::StaticEvent(Event::WindowEvent { - window_id: CoreWindowId(self.id()), + window_id: self.id(), event: WindowEvent::Focused(false), }), ); @@ -105,7 +105,7 @@ impl WinitUIWindow { } pub(crate) fn id(&self) -> WindowId { - WindowId::from_window(self) + WindowId::from_raw(self as *const Self as usize) } } @@ -522,7 +522,6 @@ impl Window { width: screen_frame.size.width as f64, height: screen_frame.size.height as f64, }; - let window_id = CoreWindowId(window.id()); app_state::handle_nonuser_events( mtm, std::iter::once(EventWrapper::ScaleFactorChanged(app_state::ScaleFactorChanged { @@ -532,7 +531,7 @@ impl Window { })) .chain(std::iter::once(EventWrapper::StaticEvent( Event::WindowEvent { - window_id, + window_id: window.id(), event: WindowEvent::SurfaceResized(size.to_physical(scale_factor)), }, ))), @@ -586,7 +585,7 @@ impl rwh_06::HasWindowHandle for Window { impl CoreWindow for Window { fn id(&self) -> crate::window::WindowId { - self.maybe_wait_on_main(|delegate| crate::window::WindowId(delegate.id())) + self.maybe_wait_on_main(|delegate| delegate.id()) } fn scale_factor(&self) -> f64 { @@ -940,23 +939,6 @@ impl Inner { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(usize); - -impl WindowId { - pub const fn into_raw(self) -> u64 { - self.0 as _ - } - - pub const fn from_raw(id: u64) -> Self { - Self(id as _) - } - - fn from_window(window: &UIWindow) -> Self { - Self(window as *const UIWindow as usize) - } -} - #[derive(Clone, Debug, Default, PartialEq)] pub struct PlatformSpecificWindowAttributes { pub scale_factor: Option, diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 5d4482481a..507bac87c9 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -107,19 +107,6 @@ impl Default for PlatformSpecificWindowAttributes { pub(crate) static X11_BACKEND: Lazy, XNotSupported>>> = Lazy::new(|| Mutex::new(XConnection::new(Some(x_error_callback)).map(Arc::new))); -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(u64); - -impl WindowId { - pub const fn into_raw(self) -> u64 { - self.0 - } - - pub const fn from_raw(id: u64) -> Self { - Self(id) - } -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum DeviceId { #[cfg(x11_platform)] diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 6b12e20a5c..b7bf9f5a13 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -312,13 +312,12 @@ impl EventLoop { let old_physical_size = physical_size; let new_surface_size = Arc::new(Mutex::new(physical_size)); - let root_window_id = crate::window::WindowId(window_id); let event = WindowEvent::ScaleFactorChanged { scale_factor, surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&new_surface_size)), }; - app.window_event(&self.active_event_loop, root_window_id, event); + app.window_event(&self.active_event_loop, window_id, event); let physical_size = *new_surface_size.lock().unwrap(); drop(new_surface_size); @@ -361,13 +360,11 @@ impl EventLoop { size }); - let window_id = crate::window::WindowId(window_id); let event = WindowEvent::SurfaceResized(physical_size); app.window_event(&self.active_event_loop, window_id, event); } if compositor_update.close_window { - let window_id = crate::window::WindowId(window_id); app.window_event(&self.active_event_loop, window_id, WindowEvent::CloseRequested); } } @@ -437,8 +434,7 @@ impl EventLoop { }); if let Some(event) = event { - let window_id = crate::window::WindowId(*window_id); - app.window_event(&self.active_event_loop, window_id, event); + app.window_event(&self.active_event_loop, *window_id, event); } } diff --git a/src/platform_impl/linux/wayland/event_loop/sink.rs b/src/platform_impl/linux/wayland/event_loop/sink.rs index ed33f8fd06..a7ad7a22f6 100644 --- a/src/platform_impl/linux/wayland/event_loop/sink.rs +++ b/src/platform_impl/linux/wayland/event_loop/sink.rs @@ -2,9 +2,8 @@ use std::vec::Drain; -use super::WindowId; use crate::event::{DeviceEvent, Event, WindowEvent}; -use crate::window::WindowId as RootWindowId; +use crate::window::WindowId; /// An event loop's sink to deliver events from the Wayland event callbacks /// to the winit's user. @@ -33,7 +32,7 @@ impl EventSink { /// Add new window event to a queue. #[inline] pub fn push_window_event(&mut self, event: WindowEvent, window_id: WindowId) { - self.window_events.push(Event::WindowEvent { event, window_id: RootWindowId(window_id) }); + self.window_events.push(Event::WindowEvent { event, window_id }); } #[inline] diff --git a/src/platform_impl/linux/wayland/mod.rs b/src/platform_impl/linux/wayland/mod.rs index bb700e2a2c..113211628b 100644 --- a/src/platform_impl/linux/wayland/mod.rs +++ b/src/platform_impl/linux/wayland/mod.rs @@ -8,7 +8,7 @@ pub use window::Window; pub(super) use crate::cursor::OnlyCursorImage as CustomCursor; use crate::dpi::{LogicalSize, PhysicalSize}; -pub use crate::platform_impl::platform::WindowId; +use crate::window::WindowId; mod event_loop; mod output; @@ -34,7 +34,7 @@ impl FingerId { /// Get the WindowId out of the surface. #[inline] fn make_wid(surface: &WlSurface) -> WindowId { - WindowId(surface.id().as_ptr() as u64) + WindowId::from_raw(surface.id().as_ptr() as usize) } /// The default routine does floor, but we need round on Wayland. diff --git a/src/platform_impl/linux/wayland/types/xdg_activation.rs b/src/platform_impl/linux/wayland/types/xdg_activation.rs index 2a638d3c97..13f9663eaa 100644 --- a/src/platform_impl/linux/wayland/types/xdg_activation.rs +++ b/src/platform_impl/linux/wayland/types/xdg_activation.rs @@ -14,8 +14,7 @@ use sctk::reexports::protocols::xdg::activation::v1::client::xdg_activation_v1:: use crate::event_loop::AsyncRequestSerial; use crate::platform_impl::wayland::state::WinitState; -use crate::platform_impl::WindowId; -use crate::window::ActivationToken; +use crate::window::{ActivationToken, WindowId}; pub struct XdgActivationState { xdg_activation: XdgActivationV1, diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index 42f37a0963..d555987918 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -16,7 +16,7 @@ use super::event_loop::sink::EventSink; use super::output::MonitorHandle; use super::state::WinitState; use super::types::xdg_activation::XdgActivationTokenData; -use super::{ActiveEventLoop, WindowId}; +use super::ActiveEventLoop; use crate::dpi::{LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}; use crate::error::{NotSupportedError, RequestError}; use crate::event::{Ime, WindowEvent}; @@ -25,8 +25,8 @@ use crate::monitor::MonitorHandle as CoreMonitorHandle; use crate::platform_impl::{Fullscreen, MonitorHandle as PlatformMonitorHandle}; use crate::window::{ Cursor, CursorGrabMode, Fullscreen as CoreFullscreen, ImePurpose, ResizeDirection, Theme, - UserAttentionType, Window as CoreWindow, WindowAttributes, WindowButtons, - WindowId as CoreWindowId, WindowLevel, + UserAttentionType, Window as CoreWindow, WindowAttributes, WindowButtons, WindowId, + WindowLevel, }; pub(crate) mod state; @@ -273,8 +273,8 @@ impl rwh_06::HasDisplayHandle for Window { } impl CoreWindow for Window { - fn id(&self) -> CoreWindowId { - CoreWindowId(self.window_id) + fn id(&self) -> WindowId { + self.window_id } fn request_redraw(&self) { diff --git a/src/platform_impl/linux/wayland/window/state.rs b/src/platform_impl/linux/wayland/window/state.rs index e21e6e03d5..8c859edfc1 100644 --- a/src/platform_impl/linux/wayland/window/state.rs +++ b/src/platform_impl/linux/wayland/window/state.rs @@ -38,8 +38,8 @@ use crate::platform_impl::wayland::seat::{ use crate::platform_impl::wayland::state::{WindowCompositorUpdate, WinitState}; use crate::platform_impl::wayland::types::cursor::{CustomCursor, SelectedCursor}; use crate::platform_impl::wayland::types::kwin_blur::KWinBlurManager; -use crate::platform_impl::{PlatformCustomCursor, WindowId}; -use crate::window::{CursorGrabMode, CursorIcon, ImePurpose, ResizeDirection, Theme}; +use crate::platform_impl::PlatformCustomCursor; +use crate::window::{CursorGrabMode, CursorIcon, ImePurpose, ResizeDirection, Theme, WindowId}; #[cfg(feature = "sctk-adwaita")] pub type WinitFrame = sctk_adwaita::AdwaitaFrame; diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index 353b93f07a..0f9e8d78ca 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -329,7 +329,7 @@ impl EventProcessor { F: Fn(&Arc) -> Ret, { let mut deleted = false; - let window_id = WindowId(window_id as _); + let window_id = WindowId::from_raw(window_id as _); let result = self .target .windows @@ -798,7 +798,7 @@ impl EventProcessor { // In the event that the window's been destroyed without being dropped first, we // cleanup again here. - self.target.windows.borrow_mut().remove(&WindowId(window as _)); + self.target.windows.borrow_mut().remove(&WindowId::from_raw(window as _)); // Since all XIM stuff needs to happen from the same thread, we destroy the input // context here instead of when dropping the window. diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index d82d8b04a0..8204b3cb1b 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -31,12 +31,12 @@ use crate::event_loop::{ }; use crate::platform::pump_events::PumpStatus; use crate::platform_impl::common::xkb::Context; -use crate::platform_impl::platform::{min_timeout, WindowId}; +use crate::platform_impl::platform::min_timeout; use crate::platform_impl::x11::window::Window; use crate::platform_impl::{OwnedDisplayHandle, PlatformCustomCursor}; use crate::window::{ CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow, - WindowAttributes, + WindowAttributes, WindowId, }; mod activation; @@ -521,13 +521,14 @@ impl EventLoop { // Empty activation tokens. while let Ok((window_id, serial)) = self.activation_receiver.try_recv() { - let token = self.event_processor.with_window(window_id.0 as xproto::Window, |window| { - window.generate_activation_token() - }); + let token = self + .event_processor + .with_window(window_id.into_raw() as xproto::Window, |window| { + window.generate_activation_token() + }); match token { Some(Ok(token)) => { - let window_id = crate::window::WindowId(window_id); let event = WindowEvent::ActivationTokenDone { serial, token: crate::window::ActivationToken::_new(token), @@ -555,7 +556,6 @@ impl EventLoop { } for window_id in windows { - let window_id = crate::window::WindowId(window_id); app.window_event( &self.event_processor.target, window_id, @@ -574,12 +574,9 @@ impl EventLoop { while unsafe { self.event_processor.poll_one_event(xev.as_mut_ptr()) } { let mut xev = unsafe { xev.assume_init() }; self.event_processor.process_event(&mut xev, |window_target, event: Event| { - if let Event::WindowEvent { - window_id: crate::window::WindowId(wid), - event: WindowEvent::RedrawRequested, - } = event + if let Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } = event { - window_target.redraw_sender.send(wid); + window_target.redraw_sender.send(window_id); } else { match event { Event::WindowEvent { window_id, event } => { @@ -994,7 +991,7 @@ impl<'a, E: fmt::Debug> CookieResultExt for Result, E> { } fn mkwid(w: xproto::Window) -> crate::window::WindowId { - crate::window::WindowId(crate::platform_impl::platform::WindowId(w as _)) + crate::window::WindowId::from_raw(w as _) } fn mkdid(w: xinput::DeviceId) -> crate::event::DeviceId { crate::event::DeviceId(crate::platform_impl::DeviceId::X(DeviceId(w))) diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index ac5a0bc0d9..016d059622 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -18,7 +18,7 @@ use x11rb::protocol::{randr, xinput}; use super::util::{self, SelectedCursor}; use super::{ - ffi, ActiveEventLoop, CookieResultExt, ImeRequest, ImeSender, VoidCookie, WindowId, XConnection, + ffi, ActiveEventLoop, CookieResultExt, ImeRequest, ImeSender, VoidCookie, XConnection, }; use crate::cursor::{Cursor, CustomCursor as RootCustomCursor}; use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size}; @@ -36,7 +36,7 @@ use crate::platform_impl::{ }; use crate::window::{ CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, Window as CoreWindow, - WindowAttributes, WindowButtons, WindowLevel, + WindowAttributes, WindowButtons, WindowId, WindowLevel, }; pub(crate) struct Window(Arc); @@ -62,8 +62,8 @@ impl Window { } impl CoreWindow for Window { - fn id(&self) -> crate::window::WindowId { - crate::window::WindowId(self.0.id()) + fn id(&self) -> WindowId { + self.0.id() } fn scale_factor(&self) -> f64 { @@ -331,7 +331,9 @@ impl Drop for Window { window.set_fullscreen(None); } - if let Ok(c) = xconn.xcb_connection().destroy_window(window.id().0 as xproto::Window) { + if let Ok(c) = + xconn.xcb_connection().destroy_window(window.id().into_raw() as xproto::Window) + { c.ignore_error(); } } @@ -1220,11 +1222,10 @@ impl UnownedWindow { &self.shared_state_lock(), ); - let window_id = crate::window::WindowId(self.id()); let old_surface_size = PhysicalSize::new(width, height); let surface_size = Arc::new(Mutex::new(PhysicalSize::new(new_width, new_height))); callback(Event::WindowEvent { - window_id, + window_id: self.id(), event: WindowEvent::ScaleFactorChanged { scale_factor: new_monitor.scale_factor, surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&surface_size)), @@ -2134,7 +2135,7 @@ impl UnownedWindow { #[inline] pub fn id(&self) -> WindowId { - WindowId(self.xwindow as _) + WindowId::from_raw(self.xwindow as _) } pub(super) fn sync_counter_id(&self) -> Option { @@ -2143,7 +2144,7 @@ impl UnownedWindow { #[inline] pub fn request_redraw(&self) { - self.redraw_sender.send(WindowId(self.xwindow as _)); + self.redraw_sender.send(WindowId::from_raw(self.xwindow as _)); } #[inline] diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index f0f397ff68..55b52aef50 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -13,7 +13,7 @@ use smol_str::SmolStr; use super::{ KeyEventExtra, MonitorHandle, PlatformSpecificEventLoopAttributes, RedoxSocket, TimeSocket, - WindowId, WindowProperties, + WindowProperties, }; use crate::application::ApplicationHandler; use crate::error::{EventLoopError, NotSupportedError, RequestError}; @@ -25,8 +25,7 @@ use crate::keyboard::{ }; use crate::platform_impl::Window; use crate::window::{ - CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow, - WindowId as RootWindowId, + CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow, WindowId, }; fn convert_scancode(scancode: u8) -> (PhysicalKey, Option) { @@ -362,7 +361,6 @@ impl EventLoop { key_without_modifiers = logical_key.clone(); } - let window_id = RootWindowId(window_id); let event = event::WindowEvent::KeyboardInput { device_id: None, event: event::KeyEvent { @@ -394,25 +392,21 @@ impl EventLoop { EventOption::TextInput(TextInputEvent { character }) => { app.window_event( window_target, - RootWindowId(window_id), + window_id, event::WindowEvent::Ime(Ime::Preedit("".into(), None)), ); app.window_event( window_target, - RootWindowId(window_id), + window_id, event::WindowEvent::Ime(Ime::Commit(character.into())), ); }, EventOption::Mouse(MouseEvent { x, y }) => { - app.window_event( - window_target, - RootWindowId(window_id), - event::WindowEvent::PointerMoved { - device_id: None, - position: (x, y).into(), - source: event::PointerSource::Mouse, - }, - ); + app.window_event(window_target, window_id, event::WindowEvent::PointerMoved { + device_id: None, + position: (x, y).into(), + source: event::PointerSource::Mouse, + }); }, EventOption::MouseRelative(MouseRelativeEvent { dx, dy }) => { app.device_event(window_target, None, event::DeviceEvent::PointerMotion { @@ -421,54 +415,38 @@ impl EventLoop { }, EventOption::Button(ButtonEvent { left, middle, right }) => { while let Some((button, state)) = event_state.mouse(left, middle, right) { - app.window_event( - window_target, - RootWindowId(window_id), - event::WindowEvent::PointerButton { - device_id: None, - state, - position: dpi::PhysicalPosition::default(), - button: button.into(), - }, - ); + app.window_event(window_target, window_id, event::WindowEvent::PointerButton { + device_id: None, + state, + position: dpi::PhysicalPosition::default(), + button: button.into(), + }); } }, EventOption::Scroll(ScrollEvent { x, y }) => { - app.window_event( - window_target, - RootWindowId(window_id), - event::WindowEvent::MouseWheel { - device_id: None, - delta: event::MouseScrollDelta::LineDelta(x as f32, y as f32), - phase: event::TouchPhase::Moved, - }, - ); + app.window_event(window_target, window_id, event::WindowEvent::MouseWheel { + device_id: None, + delta: event::MouseScrollDelta::LineDelta(x as f32, y as f32), + phase: event::TouchPhase::Moved, + }); }, EventOption::Quit(QuitEvent {}) => { - app.window_event( - window_target, - RootWindowId(window_id), - event::WindowEvent::CloseRequested, - ); + app.window_event(window_target, window_id, event::WindowEvent::CloseRequested); }, EventOption::Focus(FocusEvent { focused }) => { - app.window_event( - window_target, - RootWindowId(window_id), - event::WindowEvent::Focused(focused), - ); + app.window_event(window_target, window_id, event::WindowEvent::Focused(focused)); }, EventOption::Move(MoveEvent { x, y }) => { app.window_event( window_target, - RootWindowId(window_id), + window_id, event::WindowEvent::Moved((x, y).into()), ); }, EventOption::Resize(ResizeEvent { width, height }) => { app.window_event( window_target, - RootWindowId(window_id), + window_id, event::WindowEvent::SurfaceResized((width, height).into()), ); @@ -491,7 +469,7 @@ impl EventLoop { } }; - app.window_event(window_target, RootWindowId(window_id), event); + app.window_event(window_target, window_id, event); }, other => { tracing::warn!("unhandled event: {:?}", other); @@ -513,7 +491,7 @@ impl EventLoop { let mut creates = self.window_target.creates.lock().unwrap(); creates.pop_front() } { - let window_id = WindowId { fd: window.fd as u64 }; + let window_id = WindowId::from_raw(window.fd); let mut buf: [u8; 4096] = [0; 4096]; let path = window.fpath(&mut buf).expect("failed to read properties"); @@ -521,8 +499,6 @@ impl EventLoop { self.windows.push((window, EventState::default())); - let window_id = RootWindowId(window_id); - // Send resize event on create to indicate first size. let event = event::WindowEvent::SurfaceResized((properties.w, properties.h).into()); app.window_event(&self.window_target, window_id, event); @@ -537,16 +513,16 @@ impl EventLoop { let mut destroys = self.window_target.destroys.lock().unwrap(); destroys.pop_front() } { - let window_id = RootWindowId(destroy_id); - app.window_event(&self.window_target, window_id, event::WindowEvent::Destroyed); - self.windows.retain(|(window, _event_state)| window.fd as u64 != destroy_id.fd); + app.window_event(&self.window_target, destroy_id, event::WindowEvent::Destroyed); + self.windows + .retain(|(window, _event_state)| WindowId::from_raw(window.fd) != destroy_id); } // Handle window events. let mut i = 0; // While loop is used here because the same window may be processed more than once. while let Some((window, event_state)) = self.windows.get_mut(i) { - let window_id = WindowId { fd: window.fd as u64 }; + let window_id = WindowId::from_raw(window.fd); let mut event_buf = [0u8; 16 * mem::size_of::()]; let count = @@ -604,7 +580,7 @@ impl EventLoop { } { app.window_event( &self.window_target, - RootWindowId(window_id), + window_id, event::WindowEvent::RedrawRequested, ); } diff --git a/src/platform_impl/orbital/mod.rs b/src/platform_impl/orbital/mod.rs index e29f9aea0e..51175f6b35 100644 --- a/src/platform_impl/orbital/mod.rs +++ b/src/platform_impl/orbital/mod.rs @@ -99,21 +99,6 @@ impl TimeSocket { #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub(crate) struct PlatformSpecificEventLoopAttributes {} -#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct WindowId { - fd: u64, -} - -impl WindowId { - pub const fn into_raw(self) -> u64 { - self.fd - } - - pub const fn from_raw(id: u64) -> Self { - Self { fd: id } - } -} - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct DeviceId; diff --git a/src/platform_impl/orbital/window.rs b/src/platform_impl/orbital/window.rs index 25f011fa11..0446a4ecca 100644 --- a/src/platform_impl/orbital/window.rs +++ b/src/platform_impl/orbital/window.rs @@ -1,12 +1,12 @@ use std::collections::VecDeque; use std::sync::{Arc, Mutex}; -use super::{ActiveEventLoop, MonitorHandle, RedoxSocket, TimeSocket, WindowId, WindowProperties}; +use super::{ActiveEventLoop, MonitorHandle, RedoxSocket, TimeSocket, WindowProperties}; use crate::cursor::Cursor; use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size}; use crate::error::{NotSupportedError, RequestError}; use crate::monitor::MonitorHandle as CoreMonitorHandle; -use crate::window::{self, Fullscreen, ImePurpose, Window as CoreWindow, WindowId as CoreWindowId}; +use crate::window::{self, Fullscreen, ImePurpose, Window as CoreWindow, WindowId}; // These values match the values uses in the `window_new` function in orbital: // https://gitlab.redox-os.org/redox-os/orbital/-/blob/master/src/scheme.rs @@ -155,8 +155,8 @@ impl Window { } impl CoreWindow for Window { - fn id(&self) -> CoreWindowId { - CoreWindowId(WindowId { fd: self.window_socket.fd as u64 }) + fn id(&self) -> WindowId { + WindowId::from_raw(self.window_socket.fd) } #[inline] @@ -181,7 +181,7 @@ impl CoreWindow for Window { #[inline] fn request_redraw(&self) { - let window_id = self.id().0; + let window_id = self.id(); let mut redraws = self.redraws.lock().unwrap(); if !redraws.contains(&window_id) { redraws.push_back(window_id); @@ -478,7 +478,7 @@ impl Drop for Window { fn drop(&mut self) { { let mut destroys = self.destroys.lock().unwrap(); - destroys.push_back(self.id().0); + destroys.push_back(self.id()); } self.wake_socket.wake().unwrap(); diff --git a/src/platform_impl/web/event_loop/mod.rs b/src/platform_impl/web/event_loop/mod.rs index eced29cdd9..9c13266814 100644 --- a/src/platform_impl/web/event_loop/mod.rs +++ b/src/platform_impl/web/event_loop/mod.rs @@ -1,4 +1,4 @@ -use super::{backend, window, HasMonitorPermissionFuture, MonitorPermissionFuture}; +use super::{backend, HasMonitorPermissionFuture, MonitorPermissionFuture}; use crate::application::ApplicationHandler; use crate::error::{EventLoopError, NotSupportedError}; use crate::event::Event; diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index 6f33691231..c0a5ee72a6 100644 --- a/src/platform_impl/web/event_loop/runner.rs +++ b/src/platform_impl/web/event_loop/runner.rs @@ -49,7 +49,7 @@ struct Execution { suspended: Cell, event_loop_recreation: Cell, events: RefCell>, - id: Cell, + id: Cell, window: web_sys::Window, navigator: Navigator, document: Document, @@ -438,7 +438,7 @@ impl Shared { // Generate a strictly increasing ID // This is used to differentiate windows when handling events - pub fn generate_id(&self) -> u64 { + pub fn generate_id(&self) -> usize { let id = self.0.id.get(); self.0.id.set(id.checked_add(1).expect("exhausted `WindowId`")); diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 77196fef1f..a409ec6969 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -8,7 +8,6 @@ use web_sys::Element; use super::super::monitor::MonitorPermissionFuture; use super::super::{lock, KeyEventExtra}; use super::runner::{EventWrapper, WeakShared}; -use super::window::WindowId; use super::{backend, runner, EventLoopProxy}; use crate::error::{NotSupportedError, RequestError}; use crate::event::{ @@ -24,9 +23,7 @@ use crate::platform::web::{CustomCursorFuture, PollStrategy, WaitUntilStrategy}; use crate::platform_impl::platform::cursor::CustomCursor; use crate::platform_impl::platform::r#async::Waker; use crate::platform_impl::Window; -use crate::window::{ - CustomCursor as RootCustomCursor, CustomCursorSource, Theme, WindowId as RootWindowId, -}; +use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource, Theme, WindowId}; #[derive(Default)] struct ModifiersShared(Rc>); @@ -68,14 +65,14 @@ impl ActiveEventLoop { } pub fn generate_id(&self) -> WindowId { - WindowId(self.runner.generate_id()) + WindowId::from_raw(self.runner.generate_id()) } pub fn create_custom_cursor_async(&self, source: CustomCursorSource) -> CustomCursorFuture { CustomCursorFuture(CustomCursor::new_async(self, source.inner)) } - pub fn register(&self, canvas: &Rc, id: WindowId) { + pub fn register(&self, canvas: &Rc, window_id: WindowId) { let canvas_clone = canvas.clone(); canvas.on_touch_start(); @@ -89,13 +86,13 @@ impl ActiveEventLoop { let clear_modifiers = (!modifiers.get().is_empty()).then(|| { modifiers.set(ModifiersState::empty()); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(ModifiersState::empty().into()), } }); runner.send_events(clear_modifiers.into_iter().chain(iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::Focused(false), }))); }); @@ -105,7 +102,7 @@ impl ActiveEventLoop { canvas.on_focus(move || { if !has_focus.replace(true) { runner.send_event(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::Focused(true), }); } @@ -124,10 +121,8 @@ impl ActiveEventLoop { if focused { canvas.has_focus.set(true); - self.runner.send_event(Event::WindowEvent { - window_id: RootWindowId(id), - event: WindowEvent::Focused(true), - }) + self.runner + .send_event(Event::WindowEvent { window_id, event: WindowEvent::Focused(true) }) } let runner = self.runner.clone(); @@ -137,14 +132,14 @@ impl ActiveEventLoop { let modifiers_changed = (modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); runner.send_events( iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::KeyboardInput { device_id: None, event: KeyEvent { @@ -171,14 +166,14 @@ impl ActiveEventLoop { let modifiers_changed = (modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); runner.send_events( iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::KeyboardInput { device_id: None, event: KeyEvent { @@ -208,13 +203,13 @@ impl ActiveEventLoop { let focus = (has_focus.get() && modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); runner.send_events(focus.into_iter().chain(iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::PointerLeft { device_id: device_id.map(RootDeviceId), position: Some(position), @@ -233,13 +228,13 @@ impl ActiveEventLoop { let focus = (has_focus.get() && modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); runner.send_events(focus.into_iter().chain(iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::PointerEntered { device_id: device_id.map(RootDeviceId), position, @@ -263,13 +258,13 @@ impl ActiveEventLoop { .then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); modifiers.into_iter().chain(iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::PointerMoved { device_id, position, source }, })) })); @@ -285,7 +280,7 @@ impl ActiveEventLoop { (has_focus.get() && modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); @@ -293,7 +288,7 @@ impl ActiveEventLoop { let device_id = device_id.map(RootDeviceId); runner.send_events(modifiers.into_iter().chain([Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::PointerButton { device_id, state, position, button }, }])); } @@ -308,14 +303,14 @@ impl ActiveEventLoop { let modifiers = (modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); let device_id = pointer_id.map(RootDeviceId); runner.send_events(modifiers.into_iter().chain(iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::PointerButton { device_id, state: ElementState::Pressed, @@ -336,7 +331,7 @@ impl ActiveEventLoop { (has_focus.get() && modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); @@ -344,7 +339,7 @@ impl ActiveEventLoop { let device_id = pointer_id.map(RootDeviceId); runner.send_events(modifiers.into_iter().chain(iter::once(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::PointerButton { device_id, state: ElementState::Released, @@ -362,14 +357,14 @@ impl ActiveEventLoop { (has_focus.get() && modifiers.get() != active_modifiers).then(|| { modifiers.set(active_modifiers); Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ModifiersChanged(active_modifiers.into()), } }); runner.send_events(modifiers_changed.into_iter().chain(iter::once( Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::MouseWheel { device_id: None, delta, @@ -383,7 +378,7 @@ impl ActiveEventLoop { canvas.on_dark_mode(move |is_dark_mode| { let theme = if is_dark_mode { Theme::Dark } else { Theme::Light }; runner.send_event(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::ThemeChanged(theme), }); }); @@ -410,7 +405,7 @@ impl ActiveEventLoop { if canvas.old_size() != new_size { canvas.set_old_size(new_size); runner.send_event(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::SurfaceResized(new_size), }); canvas.request_animation_frame(); @@ -426,7 +421,7 @@ impl ActiveEventLoop { && !(is_intersecting && canvas_clone.is_intersecting.get().is_none()) { runner.send_event(Event::WindowEvent { - window_id: RootWindowId(id), + window_id, event: WindowEvent::Occluded(!is_intersecting), }); } @@ -435,7 +430,7 @@ impl ActiveEventLoop { }); let runner = self.runner.clone(); - canvas.on_animation_frame(move || runner.request_redraw(RootWindowId(id))); + canvas.on_animation_frame(move || runner.request_redraw(window_id)); canvas.on_context_menu(); } diff --git a/src/platform_impl/web/mod.rs b/src/platform_impl/web/mod.rs index b99dd3cb90..6256569d1e 100644 --- a/src/platform_impl/web/mod.rs +++ b/src/platform_impl/web/mod.rs @@ -48,5 +48,5 @@ pub(crate) use self::monitor::{ VideoModeHandle, }; use self::web_sys as backend; -pub use self::window::{PlatformSpecificWindowAttributes, Window, WindowId}; +pub use self::window::{PlatformSpecificWindowAttributes, Window}; pub(crate) use crate::icon::NoIcon as PlatformIcon; diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index a627473396..ff44676785 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -14,7 +14,6 @@ use web_sys::{ use super::super::cursor::CursorHandler; use super::super::event::DeviceId; use super::super::main_thread::MainThreadMarker; -use super::super::WindowId; use super::animation_frame::AnimationFrameHandler; use super::event_handle::EventListenerHandle; use super::intersection_handle::IntersectionObserverHandle; @@ -28,7 +27,7 @@ use crate::event::{ }; use crate::keyboard::{Key, KeyLocation, ModifiersState, PhysicalKey}; use crate::platform_impl::Fullscreen; -use crate::window::{WindowAttributes, WindowId as RootWindowId}; +use crate::window::{WindowAttributes, WindowId}; #[allow(dead_code)] pub struct Canvas { @@ -490,7 +489,7 @@ impl Canvas { let new_size = { let new_size = Arc::new(Mutex::new(current_size)); event_handler(crate::event::Event::WindowEvent { - window_id: RootWindowId(self.id), + window_id: self.id, event: crate::event::WindowEvent::ScaleFactorChanged { scale_factor: scale, surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&new_size)), @@ -518,7 +517,7 @@ impl Canvas { // Then we at least send a resized event. self.set_old_size(new_size); runner.send_event(crate::event::Event::WindowEvent { - window_id: RootWindowId(self.id), + window_id: self.id, event: crate::event::WindowEvent::SurfaceResized(new_size), }) } diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index c93b1e3bc4..5eb02c498d 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -14,7 +14,7 @@ use crate::icon::Icon; use crate::monitor::MonitorHandle as RootMonitorHandle; use crate::window::{ Cursor, CursorGrabMode, Fullscreen as RootFullscreen, ImePurpose, ResizeDirection, Theme, - UserAttentionType, Window as RootWindow, WindowAttributes, WindowButtons, WindowId as RootWI, + UserAttentionType, Window as RootWindow, WindowAttributes, WindowButtons, WindowId, WindowLevel, }; @@ -53,7 +53,7 @@ impl Window { target.register(&canvas, id); let runner = target.runner.clone(); - let destroy_fn = Box::new(move || runner.notify_destroy_window(RootWI(id))); + let destroy_fn = Box::new(move || runner.notify_destroy_window(id)); let inner = Inner { id, @@ -65,7 +65,7 @@ impl Window { let canvas = Rc::downgrade(&inner.canvas); let (dispatcher, runner) = Dispatcher::new(target.runner.main_thread(), inner); - target.runner.add_canvas(RootWI(id), canvas, runner); + target.runner.add_canvas(id, canvas, runner); Ok(Window { inner: dispatcher }) } @@ -91,8 +91,8 @@ impl Window { } impl RootWindow for Window { - fn id(&self) -> RootWI { - RootWI(self.inner.queue(|inner| inner.id)) + fn id(&self) -> WindowId { + self.inner.queue(|inner| inner.id) } fn scale_factor(&self) -> f64 { @@ -429,19 +429,6 @@ impl Drop for Inner { } } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(pub(crate) u64); - -impl WindowId { - pub const fn into_raw(self) -> u64 { - self.0 - } - - pub const fn from_raw(id: u64) -> Self { - Self(id) - } -} - #[derive(Clone, Debug)] pub struct PlatformSpecificWindowAttributes { pub(crate) canvas: Option>>, diff --git a/src/platform_impl/windows/drop_handler.rs b/src/platform_impl/windows/drop_handler.rs index c30dd655e3..99c413bfaa 100644 --- a/src/platform_impl/windows/drop_handler.rs +++ b/src/platform_impl/windows/drop_handler.rs @@ -15,8 +15,7 @@ use crate::event::Event; use crate::platform_impl::platform::definitions::{ IDataObjectVtbl, IDropTarget, IDropTargetVtbl, IUnknownVtbl, }; -use crate::platform_impl::platform::WindowId; -use crate::window::WindowId as RootWindowId; +use crate::window::WindowId; #[repr(C)] pub struct FileDropHandlerData { @@ -86,7 +85,7 @@ impl FileDropHandler { let hdrop = unsafe { Self::iterate_filenames(pDataObj, |filename| { drop_handler.send_event(Event::WindowEvent { - window_id: RootWindowId(WindowId(drop_handler.window)), + window_id: WindowId::from_raw(drop_handler.window as usize), event: HoveredFile(filename), }); }) @@ -120,7 +119,7 @@ impl FileDropHandler { let drop_handler = unsafe { Self::from_interface(this) }; if drop_handler.hovered_is_valid { drop_handler.send_event(Event::WindowEvent { - window_id: RootWindowId(WindowId(drop_handler.window)), + window_id: WindowId::from_raw(drop_handler.window as usize), event: HoveredFileCancelled, }); } @@ -140,7 +139,7 @@ impl FileDropHandler { let hdrop = unsafe { Self::iterate_filenames(pDataObj, |filename| { drop_handler.send_event(Event::WindowEvent { - window_id: RootWindowId(WindowId(drop_handler.window)), + window_id: WindowId::from_raw(drop_handler.window as usize), event: DroppedFile(filename), }); }) diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 8c6e5391de..3751834a49 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -80,14 +80,12 @@ use crate::platform_impl::platform::window::InitData; use crate::platform_impl::platform::window_state::{ CursorFlags, ImeState, WindowFlags, WindowState, }; -use crate::platform_impl::platform::{ - raw_input, util, wrap_device_id, FingerId, Fullscreen, WindowId, -}; +use crate::platform_impl::platform::{raw_input, util, wrap_device_id, FingerId, Fullscreen}; use crate::platform_impl::Window; use crate::utils::Lazy; use crate::window::{ CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow, - WindowAttributes, WindowId as CoreWindowId, + WindowAttributes, WindowId, }; pub(crate) struct WindowData { @@ -920,7 +918,7 @@ fn update_modifiers(window: HWND, userdata: &WindowData) { drop(window_state); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: ModifiersChanged(modifiers.into()), }); } @@ -932,7 +930,7 @@ unsafe fn gain_active_focus(window: HWND, userdata: &WindowData) { update_modifiers(window, userdata); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: Focused(true), }); } @@ -942,12 +940,12 @@ unsafe fn lose_active_focus(window: HWND, userdata: &WindowData) { userdata.window_state_lock().modifiers_state = ModifiersState::empty(); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: ModifiersChanged(ModifiersState::empty().into()), }); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: Focused(false), }); } @@ -1045,7 +1043,7 @@ unsafe fn public_window_callback_inner( userdata.key_event_builder.process_message(window, msg, wparam, lparam, &mut result); for event in events { userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: KeyboardInput { device_id: None, event: event.event, @@ -1130,7 +1128,7 @@ unsafe fn public_window_callback_inner( WM_CLOSE => { use crate::event::WindowEvent::CloseRequested; userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: CloseRequested, }); result = ProcResult::Value(0); @@ -1140,7 +1138,7 @@ unsafe fn public_window_callback_inner( use crate::event::WindowEvent::Destroyed; unsafe { RevokeDragDrop(window) }; userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: Destroyed, }); result = ProcResult::Value(0); @@ -1161,7 +1159,7 @@ unsafe fn public_window_callback_inner( // and request a normal redraw with `RedrawWindow`. if !userdata.event_loop_runner.should_buffer() { userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::RedrawRequested, }); } @@ -1264,7 +1262,7 @@ unsafe fn public_window_callback_inner( let physical_position = unsafe { PhysicalPosition::new((*windowpos).x, (*windowpos).y) }; userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: Moved(physical_position), }); } @@ -1280,7 +1278,7 @@ unsafe fn public_window_callback_inner( let physical_size = PhysicalSize::new(w, h); let event = Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: SurfaceResized(physical_size), }; @@ -1395,7 +1393,7 @@ unsafe fn public_window_callback_inner( userdata.window_state_lock().ime_state = ImeState::Enabled; userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Enabled), }); } @@ -1415,7 +1413,7 @@ unsafe fn public_window_callback_inner( if lparam == 0 { userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Preedit(String::new(), None)), }); } @@ -1427,11 +1425,11 @@ unsafe fn public_window_callback_inner( userdata.window_state_lock().ime_state = ImeState::Enabled; userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Preedit(String::new(), None)), }); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Commit(text)), }); } @@ -1446,7 +1444,7 @@ unsafe fn public_window_callback_inner( let cursor_range = first.map(|f| (f, last.unwrap_or(f))); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Preedit(text, cursor_range)), }); } @@ -1469,11 +1467,11 @@ unsafe fn public_window_callback_inner( let ime_context = unsafe { ImeContext::current(window) }; if let Some(text) = unsafe { ime_context.get_composed_text() } { userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Preedit(String::new(), None)), }); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Commit(text)), }); } @@ -1482,7 +1480,7 @@ unsafe fn public_window_callback_inner( userdata.window_state_lock().ime_state = ImeState::Disabled; userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::Ime(Ime::Disabled), }); } @@ -1541,7 +1539,7 @@ unsafe fn public_window_callback_inner( drop(w); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerEntered { device_id: None, position, @@ -1566,7 +1564,7 @@ unsafe fn public_window_callback_inner( drop(w); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerLeft { device_id: None, position: Some(position), @@ -1589,7 +1587,7 @@ unsafe fn public_window_callback_inner( update_modifiers(window, userdata); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerMoved { device_id: None, position, source: PointerSource::Mouse }, }); } @@ -1607,7 +1605,7 @@ unsafe fn public_window_callback_inner( } userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerLeft { device_id: None, position: None, kind: Mouse }, }); @@ -1623,7 +1621,7 @@ unsafe fn public_window_callback_inner( update_modifiers(window, userdata); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::MouseWheel { device_id: None, delta: LineDelta(0.0, value), @@ -1643,7 +1641,7 @@ unsafe fn public_window_callback_inner( update_modifiers(window, userdata); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: WindowEvent::MouseWheel { device_id: None, delta: LineDelta(value, 0.0), @@ -1682,7 +1680,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Pressed, @@ -1707,7 +1705,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Released, @@ -1732,7 +1730,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Pressed, @@ -1757,7 +1755,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Released, @@ -1782,7 +1780,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Pressed, @@ -1807,7 +1805,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Released, @@ -1833,7 +1831,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Pressed, @@ -1864,7 +1862,7 @@ unsafe fn public_window_callback_inner( let position = PhysicalPosition::new(x as f64, y as f64); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: PointerButton { device_id: None, state: Released, @@ -1919,7 +1917,7 @@ unsafe fn public_window_callback_inner( let y = position.y as f64 + (input.y % 100) as f64 / 100f64; let position = PhysicalPosition::new(x, y); - let window_id = CoreWindowId(WindowId(window)); + let window_id = WindowId::from_raw(window as usize); let finger_id = RootFingerId(FingerId { id: input.dwID, primary: util::has_flag(input.dwFlags, TOUCHEVENTF_PRIMARY), @@ -2087,7 +2085,7 @@ unsafe fn public_window_callback_inner( let y = location.y as f64 + y.fract(); let position = PhysicalPosition::new(x, y); - let window_id = CoreWindowId(WindowId(window)); + let window_id = WindowId::from_raw(window as usize); let finger_id = RootFingerId(FingerId { id: pointer_info.pointerId, primary: util::has_flag(pointer_info.pointerFlags, POINTER_FLAG_PRIMARY), @@ -2320,7 +2318,7 @@ unsafe fn public_window_callback_inner( let new_surface_size = Arc::new(Mutex::new(new_physical_surface_size)); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: ScaleFactorChanged { scale_factor: new_scale_factor, surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&new_surface_size)), @@ -2472,7 +2470,7 @@ unsafe fn public_window_callback_inner( window_state.current_theme = new_theme; drop(window_state); userdata.send_event(Event::WindowEvent { - window_id: CoreWindowId(WindowId(window)), + window_id: WindowId::from_raw(window as usize), event: ThemeChanged(new_theme), }); } diff --git a/src/platform_impl/windows/event_loop/runner.rs b/src/platform_impl/windows/event_loop/runner.rs index d288d6d590..3b712167ec 100644 --- a/src/platform_impl/windows/event_loop/runner.rs +++ b/src/platform_impl/windows/event_loop/runner.rs @@ -53,7 +53,7 @@ pub(crate) enum RunnerState { enum BufferedEvent { Event(Event), - ScaleFactorChanged(WindowId, f64, PhysicalSize), + ScaleFactorChanged(HWND, f64, PhysicalSize), } impl EventLoopRunner { @@ -360,7 +360,7 @@ impl BufferedEvent { event: WindowEvent::ScaleFactorChanged { scale_factor, surface_size_writer }, window_id, } => BufferedEvent::ScaleFactorChanged( - window_id, + window_id.into_raw() as HWND, scale_factor, *surface_size_writer.new_surface_size.upgrade().unwrap().lock().unwrap(), ), @@ -371,10 +371,10 @@ impl BufferedEvent { pub fn dispatch_event(self, dispatch: impl FnOnce(Event)) { match self { Self::Event(event) => dispatch(event), - Self::ScaleFactorChanged(window_id, scale_factor, new_surface_size) => { + Self::ScaleFactorChanged(window, scale_factor, new_surface_size) => { let user_new_surface_size = Arc::new(Mutex::new(new_surface_size)); dispatch(Event::WindowEvent { - window_id, + window_id: WindowId::from_raw(window as usize), event: WindowEvent::ScaleFactorChanged { scale_factor, surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade( @@ -388,12 +388,11 @@ impl BufferedEvent { if surface_size != new_surface_size { let window_flags = unsafe { - let userdata = - get_window_long(window_id.0.into(), GWL_USERDATA) as *mut WindowData; + let userdata = get_window_long(window, GWL_USERDATA) as *mut WindowData; (*userdata).window_state_lock().window_flags }; - window_flags.set_size((window_id.0).0, surface_size); + window_flags.set_size(window, surface_size); } }, } diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 93fdb5c21d..110cd60fa0 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -101,27 +101,6 @@ pub struct KeyEventExtra { pub key_without_modifiers: Key, } -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(HWND); -unsafe impl Send for WindowId {} -unsafe impl Sync for WindowId {} - -impl WindowId { - pub const fn into_raw(self) -> u64 { - self.0 as u64 - } - - pub const fn from_raw(id: u64) -> Self { - Self(id as HWND) - } -} - -impl From for HWND { - fn from(window_id: WindowId) -> Self { - window_id.0 - } -} - #[inline(always)] const fn get_xbutton_wparam(x: u32) -> u16 { hiword(x) diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index db0dd9ba7c..7d76da0695 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -66,11 +66,11 @@ use crate::platform_impl::platform::keyboard::KeyEventBuilder; use crate::platform_impl::platform::window_state::{ CursorFlags, SavedWindow, WindowFlags, WindowState, }; -use crate::platform_impl::platform::{monitor, util, Fullscreen, SelectedCursor, WindowId}; +use crate::platform_impl::platform::{monitor, util, Fullscreen, SelectedCursor}; use crate::window::{ CursorGrabMode, Fullscreen as CoreFullscreen, ImePurpose, ResizeDirection, Theme, - UserAttentionType, Window as CoreWindow, WindowAttributes, WindowButtons, - WindowId as CoreWindowId, WindowLevel, + UserAttentionType, Window as CoreWindow, WindowAttributes, WindowButtons, WindowId, + WindowLevel, }; /// The Win32 implementation of the main `Window` object. @@ -696,8 +696,8 @@ impl CoreWindow for Window { Ok(()) } - fn id(&self) -> CoreWindowId { - CoreWindowId(WindowId(self.hwnd())) + fn id(&self) -> WindowId { + WindowId::from_raw(self.hwnd() as usize) } fn set_minimized(&self, minimized: bool) { diff --git a/src/window.rs b/src/window.rs index debb58a6a6..071cdf2bab 100644 --- a/src/window.rs +++ b/src/window.rs @@ -11,7 +11,7 @@ use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size}; use crate::error::RequestError; pub use crate::icon::{BadIcon, Icon}; use crate::monitor::{MonitorHandle, VideoModeHandle}; -use crate::platform_impl::{self, PlatformSpecificWindowAttributes}; +use crate::platform_impl::PlatformSpecificWindowAttributes; use crate::utils::AsAny; /// Identifier of a window. Unique for each window. @@ -21,21 +21,21 @@ use crate::utils::AsAny; /// Whenever you receive an event specific to a window, this event contains a `WindowId` which you /// can then compare to the ids of your windows. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(pub(crate) platform_impl::WindowId); +pub struct WindowId(usize); impl WindowId { /// Convert the `WindowId` into the underlying integer. /// /// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic. - pub const fn into_raw(self) -> u64 { - self.0.into_raw() + pub const fn into_raw(self) -> usize { + self.0 } /// Construct a `WindowId` from the underlying integer. /// /// This should only be called with integers returned from [`WindowId::into_raw`]. - pub const fn from_raw(id: u64) -> Self { - Self(platform_impl::WindowId::from_raw(id)) + pub const fn from_raw(id: usize) -> Self { + Self(id) } }