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`.
- 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<u64> for WindowId` and `impl From<WindowId> for u64`. Replaced with
`WindowId::into_raw()` and `from_raw()`.
### Fixed

View file

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

View file

@ -342,17 +342,13 @@ impl WindowId {
pub const fn dummy() -> Self {
Self(0)
}
}
impl From<WindowId> 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<u64> 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)
}
}

View file

@ -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<WindowId> 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<u64> 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)
}
}

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)]
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 {
pub const fn dummy() -> Self {
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)]

View file

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

View file

@ -49,7 +49,7 @@ struct Execution {
suspended: Cell<bool>,
event_loop_recreation: Cell<bool>,
events: RefCell<VecDeque<EventWrapper>>,
id: RefCell<u32>,
id: Cell<u64>,
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) {

View file

@ -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<WindowId> for u64 {
fn from(window_id: WindowId) -> Self {
window_id.0 as u64
pub const fn into_raw(self) -> u64 {
self.0
}
}
impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self {
Self(raw_id as u32)
pub const fn from_raw(id: u64) -> Self {
Self(id)
}
}

View file

@ -118,11 +118,13 @@ impl WindowId {
pub const fn dummy() -> Self {
WindowId(0)
}
}
impl From<WindowId> 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<WindowId> for HWND {
}
}
impl From<u64> 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)

View file

@ -34,6 +34,20 @@ impl WindowId {
pub const fn dummy() -> Self {
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 {
@ -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.
#[derive(Debug, Clone, PartialEq)]
pub struct WindowAttributes {