Skip to content

Commit

Permalink
chore: remove platform WindowId's
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
madsmtm authored Oct 8, 2024
1 parent eccd9e4 commit da2268a
Show file tree
Hide file tree
Showing 35 changed files with 226 additions and 379 deletions.
52 changes: 18 additions & 34 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -187,30 +191,27 @@ 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,
)),
scale_factor,
};

app.window_event(&self.window_target, window_id, event);
app.window_event(&self.window_target, GLOBAL_WINDOW, event);
}
},
MainEvent::LowMemory => {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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!(),
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
},
}
},
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<RootMonitorHandle> {
Expand Down
8 changes: 4 additions & 4 deletions src/platform_impl/apple/appkit/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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| {
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/apple/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/apple/appkit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
});
Expand Down
19 changes: 3 additions & 16 deletions src/platform_impl/apple/appkit/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
}
6 changes: 3 additions & 3 deletions src/platform_impl/apple/appkit/window_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
});
Expand Down
9 changes: 4 additions & 5 deletions src/platform_impl/apple/uikit/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*) => {
Expand Down Expand Up @@ -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),
}));
}
Expand All @@ -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,
})
})
Expand Down Expand Up @@ -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,
}));
}
Expand All @@ -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)),
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/apple/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
Loading

0 comments on commit da2268a

Please sign in to comment.