Skip to content

Commit

Permalink
api: replace WindowId From/Into u64 with `WindowId::{from,into}…
Browse files Browse the repository at this point in the history
…_raw()`

Co-authored-by: Mads Marquart <[email protected]>
Co-authored-by: Kirill Chibisov <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2024
1 parent 380eea0 commit 6e1b9fa
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 91 deletions.
3 changes: 3 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/platform_impl/apple/appkit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
32 changes: 10 additions & 22 deletions src/platform_impl/apple/uikit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,17 @@ 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 WindowId {
pub const fn dummy() -> Self {
Self(0)
}
}

impl From<u64> 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)
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/platform_impl/orbital/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 5 additions & 9 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WindowId> 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<u64> 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)
}
}

Expand Down

0 comments on commit 6e1b9fa

Please sign in to comment.