diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index bc73b0008a..ee8b4e76ef 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -65,6 +65,7 @@ changelog entry. - Add basic iOS IME support. The soft keyboard can now be shown using `Window::set_ime_allowed`. - On macOS, add `WindowExtMacOS::set_borderless_game` and `WindowAttributesExtMacOS::with_borderless_game` to fully disable the menu bar and dock in Borderless Fullscreen as commonly done in games. +- Add `WindowId::into_raw()` and `from_raw()`. ### Changed @@ -150,6 +151,8 @@ changelog entry. - Remove `MonitorHandle::size()` and `refresh_rate_millihertz()` in favor of `MonitorHandle::current_video_mode()`. - On Android, remove all `MonitorHandle` support instead of emitting false data. +- Remove `impl From for WindowId` and `impl From for u64`. Replaced with + `WindowId::into_raw()` and `from_raw()`. ### Fixed diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 426dd0fb35..0503bf871e 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -671,16 +671,12 @@ impl WindowId { pub const fn dummy() -> Self { WindowId } -} -impl From for u64 { - fn from(_: WindowId) -> Self { + pub const fn into_raw(self) -> u64 { 0 } -} -impl From for WindowId { - fn from(_: u64) -> Self { + pub const fn from_raw(_id: u64) -> Self { Self } } diff --git a/src/platform_impl/apple/appkit/window.rs b/src/platform_impl/apple/appkit/window.rs index 3a454e0457..aaa5775c6a 100644 --- a/src/platform_impl/apple/appkit/window.rs +++ b/src/platform_impl/apple/appkit/window.rs @@ -342,17 +342,13 @@ impl WindowId { pub const fn dummy() -> Self { Self(0) } -} -impl From for u64 { - fn from(window_id: WindowId) -> Self { - window_id.0 as u64 + pub const fn into_raw(self) -> u64 { + self.0 as u64 } -} -impl From for WindowId { - fn from(raw_id: u64) -> Self { - Self(raw_id as usize) + pub const fn from_raw(id: u64) -> Self { + Self(id as usize) } } diff --git a/src/platform_impl/apple/uikit/window.rs b/src/platform_impl/apple/uikit/window.rs index b4ca0bc721..b7603244ac 100644 --- a/src/platform_impl/apple/uikit/window.rs +++ b/src/platform_impl/apple/uikit/window.rs @@ -3,10 +3,9 @@ use std::collections::VecDeque; use objc2::rc::Retained; -use objc2::runtime::{AnyObject, NSObject}; use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass}; use objc2_foundation::{ - CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker, NSObjectProtocol, + CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker, NSObject, NSObjectProtocol, }; use objc2_ui_kit::{ UIApplication, UICoordinateSpace, UIResponder, UIScreen, UIScreenOverscanCompensation, @@ -106,7 +105,7 @@ impl WinitUIWindow { } pub(crate) fn id(&self) -> WindowId { - (self as *const Self as usize as u64).into() + WindowId::from_window(self) } } @@ -942,34 +941,23 @@ impl Inner { } #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId { - window: *mut WinitUIWindow, -} +pub struct WindowId(usize); impl WindowId { pub const fn dummy() -> Self { - WindowId { window: std::ptr::null_mut() } + WindowId(0) } -} -impl From for u64 { - fn from(window_id: WindowId) -> Self { - window_id.window as u64 + pub const fn into_raw(self) -> u64 { + self.0 as _ } -} -impl From for WindowId { - fn from(raw_id: u64) -> Self { - Self { window: raw_id as _ } + pub const fn from_raw(id: u64) -> Self { + Self(id as _) } -} - -unsafe impl Send for WindowId {} -unsafe impl Sync for WindowId {} -impl From<&AnyObject> for WindowId { - fn from(window: &AnyObject) -> WindowId { - WindowId { window: window as *const _ as _ } + fn from_window(window: &UIWindow) -> Self { + Self(window as *const UIWindow as usize) } } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 9c553c98b7..fe4e6ff885 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -110,21 +110,17 @@ pub(crate) static X11_BACKEND: Lazy, XNotSupported #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct WindowId(u64); -impl From for u64 { - fn from(window_id: WindowId) -> Self { - window_id.0 +impl WindowId { + pub const fn dummy() -> Self { + Self(0) } -} -impl From for WindowId { - fn from(raw_id: u64) -> Self { - Self(raw_id) + pub const fn into_raw(self) -> u64 { + self.0 } -} -impl WindowId { - pub const fn dummy() -> Self { - Self(0) + pub const fn from_raw(id: u64) -> Self { + Self(id) } } diff --git a/src/platform_impl/orbital/mod.rs b/src/platform_impl/orbital/mod.rs index f125b6e51d..22db646d69 100644 --- a/src/platform_impl/orbital/mod.rs +++ b/src/platform_impl/orbital/mod.rs @@ -108,17 +108,13 @@ impl WindowId { pub const fn dummy() -> Self { WindowId { fd: u64::MAX } } -} -impl From for u64 { - fn from(id: WindowId) -> Self { - id.fd + pub const fn into_raw(self) -> u64 { + self.fd } -} -impl From for WindowId { - fn from(fd: u64) -> Self { - Self { fd } + pub const fn from_raw(id: u64) -> Self { + Self { fd: id } } } diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index 64c65a34eb..daaae53d4c 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: RefCell, + id: Cell, window: web_sys::Window, navigator: Navigator, document: Document, @@ -171,7 +171,7 @@ impl Shared { window, navigator, document, - id: RefCell::new(0), + id: Cell::new(0), all_canvases: RefCell::new(Vec::new()), redraw_pending: RefCell::new(HashSet::new()), destroy_pending: RefCell::new(VecDeque::new()), @@ -438,11 +438,11 @@ impl Shared { // Generate a strictly increasing ID // This is used to differentiate windows when handling events - pub fn generate_id(&self) -> u32 { - let mut id = self.0.id.borrow_mut(); - *id += 1; + pub fn generate_id(&self) -> u64 { + let id = self.0.id.get(); + self.0.id.set(id.checked_add(1).expect("exhausted `WindowId`")); - *id + id } pub fn request_redraw(&self, id: WindowId) { diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index d979310a6e..6d4943272b 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -430,23 +430,19 @@ impl Drop for Inner { } } #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct WindowId(pub(crate) u32); +pub struct WindowId(pub(crate) u64); impl WindowId { pub const fn dummy() -> Self { Self(0) } -} -impl From for u64 { - fn from(window_id: WindowId) -> Self { - window_id.0 as u64 + pub const fn into_raw(self) -> u64 { + self.0 } -} -impl From for WindowId { - fn from(raw_id: u64) -> Self { - Self(raw_id as u32) + pub const fn from_raw(id: u64) -> Self { + Self(id) } } diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 92079ebbc1..efdda2c57c 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -118,11 +118,13 @@ impl WindowId { pub const fn dummy() -> Self { WindowId(0) } -} -impl From for u64 { - fn from(window_id: WindowId) -> Self { - window_id.0 as u64 + pub const fn into_raw(self) -> u64 { + self.0 as u64 + } + + pub const fn from_raw(id: u64) -> Self { + Self(id as HWND) } } @@ -132,12 +134,6 @@ impl From for HWND { } } -impl From for WindowId { - fn from(raw_id: u64) -> Self { - Self(raw_id as HWND) - } -} - #[inline(always)] const fn get_xbutton_wparam(x: u32) -> u16 { hiword(x) diff --git a/src/window.rs b/src/window.rs index 1ded68b146..33238eb2ef 100644 --- a/src/window.rs +++ b/src/window.rs @@ -34,23 +34,25 @@ impl WindowId { pub const fn dummy() -> Self { WindowId(platform_impl::WindowId::dummy()) } -} -impl fmt::Debug for WindowId { - fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(fmtr) + /// 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() } -} -impl From for u64 { - fn from(window_id: WindowId) -> Self { - window_id.0.into() + /// 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)) } } -impl From for WindowId { - fn from(raw_id: u64) -> Self { - Self(raw_id.into()) +impl fmt::Debug for WindowId { + fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(fmtr) } }