api: replace WindowId From/Into u64 with WindowId::{from,into}_raw()

Co-authored-by: Mads Marquart <mads@marquart.dk>
Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
This commit is contained in:
daxpedda 2024-09-27 22:12:50 +02:00 committed by GitHub
parent 380eea0072
commit 6e1b9fa24d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 62 additions and 93 deletions

View file

@ -65,6 +65,7 @@ changelog entry.
- Add basic iOS IME support. The soft keyboard can now be shown using `Window::set_ime_allowed`. - 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` - 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. to fully disable the menu bar and dock in Borderless Fullscreen as commonly done in games.
- Add `WindowId::into_raw()` and `from_raw()`.
### Changed ### Changed
@ -150,6 +151,8 @@ changelog entry.
- Remove `MonitorHandle::size()` and `refresh_rate_millihertz()` in favor of - Remove `MonitorHandle::size()` and `refresh_rate_millihertz()` in favor of
`MonitorHandle::current_video_mode()`. `MonitorHandle::current_video_mode()`.
- On Android, remove all `MonitorHandle` support instead of emitting false data. - On Android, remove all `MonitorHandle` support instead of emitting false data.
- Remove `impl From<u64> for WindowId` and `impl From<WindowId> for u64`. Replaced with
`WindowId::into_raw()` and `from_raw()`.
### Fixed ### Fixed

View file

@ -671,16 +671,12 @@ impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId WindowId
} }
}
impl From<WindowId> for u64 { pub const fn into_raw(self) -> u64 {
fn from(_: WindowId) -> Self {
0 0
} }
}
impl From<u64> for WindowId { pub const fn from_raw(_id: u64) -> Self {
fn from(_: u64) -> Self {
Self Self
} }
} }

View file

@ -342,17 +342,13 @@ impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
}
impl From<WindowId> for u64 { pub const fn into_raw(self) -> u64 {
fn from(window_id: WindowId) -> Self { self.0 as u64
window_id.0 as u64
} }
}
impl From<u64> for WindowId { pub const fn from_raw(id: u64) -> Self {
fn from(raw_id: u64) -> Self { Self(id as usize)
Self(raw_id as usize)
} }
} }

View file

@ -3,10 +3,9 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use objc2::rc::Retained; use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject};
use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass}; use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_foundation::{ use objc2_foundation::{
CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker, NSObjectProtocol, CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker, NSObject, NSObjectProtocol,
}; };
use objc2_ui_kit::{ use objc2_ui_kit::{
UIApplication, UICoordinateSpace, UIResponder, UIScreen, UIScreenOverscanCompensation, UIApplication, UICoordinateSpace, UIResponder, UIScreen, UIScreenOverscanCompensation,
@ -106,7 +105,7 @@ impl WinitUIWindow {
} }
pub(crate) fn id(&self) -> WindowId { 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)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId { pub struct WindowId(usize);
window: *mut WinitUIWindow,
}
impl WindowId { impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId { window: std::ptr::null_mut() } WindowId(0)
} }
}
impl From<WindowId> for u64 { pub const fn into_raw(self) -> u64 {
fn from(window_id: WindowId) -> Self { self.0 as _
window_id.window as u64
} }
}
impl From<u64> for WindowId { pub const fn from_raw(id: u64) -> Self {
fn from(raw_id: u64) -> Self { Self(id as _)
Self { window: raw_id as _ }
} }
}
unsafe impl Send for WindowId {} fn from_window(window: &UIWindow) -> Self {
unsafe impl Sync for WindowId {} Self(window as *const UIWindow as usize)
impl From<&AnyObject> for WindowId {
fn from(window: &AnyObject) -> WindowId {
WindowId { window: window as *const _ as _ }
} }
} }

View file

@ -110,22 +110,18 @@ pub(crate) static X11_BACKEND: Lazy<Mutex<Result<Arc<XConnection>, XNotSupported
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(u64); pub struct WindowId(u64);
impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0
}
}
impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id)
}
}
impl WindowId { impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
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)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

View file

@ -108,17 +108,13 @@ impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId { fd: u64::MAX } WindowId { fd: u64::MAX }
} }
}
impl From<WindowId> for u64 { pub const fn into_raw(self) -> u64 {
fn from(id: WindowId) -> Self { self.fd
id.fd
} }
}
impl From<u64> for WindowId { pub const fn from_raw(id: u64) -> Self {
fn from(fd: u64) -> Self { Self { fd: id }
Self { fd }
} }
} }

View file

@ -49,7 +49,7 @@ struct Execution {
suspended: Cell<bool>, suspended: Cell<bool>,
event_loop_recreation: Cell<bool>, event_loop_recreation: Cell<bool>,
events: RefCell<VecDeque<EventWrapper>>, events: RefCell<VecDeque<EventWrapper>>,
id: RefCell<u32>, id: Cell<u64>,
window: web_sys::Window, window: web_sys::Window,
navigator: Navigator, navigator: Navigator,
document: Document, document: Document,
@ -171,7 +171,7 @@ impl Shared {
window, window,
navigator, navigator,
document, document,
id: RefCell::new(0), id: Cell::new(0),
all_canvases: RefCell::new(Vec::new()), all_canvases: RefCell::new(Vec::new()),
redraw_pending: RefCell::new(HashSet::new()), redraw_pending: RefCell::new(HashSet::new()),
destroy_pending: RefCell::new(VecDeque::new()), destroy_pending: RefCell::new(VecDeque::new()),
@ -438,11 +438,11 @@ impl Shared {
// Generate a strictly increasing ID // Generate a strictly increasing ID
// This is used to differentiate windows when handling events // This is used to differentiate windows when handling events
pub fn generate_id(&self) -> u32 { pub fn generate_id(&self) -> u64 {
let mut id = self.0.id.borrow_mut(); let id = self.0.id.get();
*id += 1; self.0.id.set(id.checked_add(1).expect("exhausted `WindowId`"));
*id id
} }
pub fn request_redraw(&self, id: WindowId) { pub fn request_redraw(&self, id: WindowId) {

View file

@ -430,23 +430,19 @@ impl Drop for Inner {
} }
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(pub(crate) u32); pub struct WindowId(pub(crate) u64);
impl WindowId { impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
}
impl From<WindowId> for u64 { pub const fn into_raw(self) -> u64 {
fn from(window_id: WindowId) -> Self { self.0
window_id.0 as u64
} }
}
impl From<u64> for WindowId { pub const fn from_raw(id: u64) -> Self {
fn from(raw_id: u64) -> Self { Self(id)
Self(raw_id as u32)
} }
} }

View file

@ -118,11 +118,13 @@ impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId(0) WindowId(0)
} }
}
impl From<WindowId> for u64 { pub const fn into_raw(self) -> u64 {
fn from(window_id: WindowId) -> Self { self.0 as u64
window_id.0 as u64 }
pub const fn from_raw(id: u64) -> Self {
Self(id as HWND)
} }
} }
@ -132,12 +134,6 @@ impl From<WindowId> for HWND {
} }
} }
impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id as HWND)
}
}
#[inline(always)] #[inline(always)]
const fn get_xbutton_wparam(x: u32) -> u16 { const fn get_xbutton_wparam(x: u32) -> u16 {
hiword(x) hiword(x)

View file

@ -34,6 +34,20 @@ impl WindowId {
pub const fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId(platform_impl::WindowId::dummy()) WindowId(platform_impl::WindowId::dummy())
} }
/// 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()
}
/// 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 fmt::Debug for WindowId { impl fmt::Debug for WindowId {
@ -42,18 +56,6 @@ impl fmt::Debug for WindowId {
} }
} }
impl From<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0.into()
}
}
impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id.into())
}
}
/// Attributes used when creating a window. /// Attributes used when creating a window.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct WindowAttributes { pub struct WindowAttributes {