chore: remove platform WindowId's

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.
This commit is contained in:
Mads Marquart 2024-10-08 15:29:40 +02:00 committed by GitHub
parent eccd9e415d
commit da2268ae22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 226 additions and 379 deletions

View file

@ -1,4 +1,4 @@
use super::{backend, window, HasMonitorPermissionFuture, MonitorPermissionFuture};
use super::{backend, HasMonitorPermissionFuture, MonitorPermissionFuture};
use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, NotSupportedError};
use crate::event::Event;

View file

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

View file

@ -8,7 +8,6 @@ use web_sys::Element;
use super::super::monitor::MonitorPermissionFuture;
use super::super::{lock, KeyEventExtra};
use super::runner::{EventWrapper, WeakShared};
use super::window::WindowId;
use super::{backend, runner, EventLoopProxy};
use crate::error::{NotSupportedError, RequestError};
use crate::event::{
@ -24,9 +23,7 @@ use crate::platform::web::{CustomCursorFuture, PollStrategy, WaitUntilStrategy};
use crate::platform_impl::platform::cursor::CustomCursor;
use crate::platform_impl::platform::r#async::Waker;
use crate::platform_impl::Window;
use crate::window::{
CustomCursor as RootCustomCursor, CustomCursorSource, Theme, WindowId as RootWindowId,
};
use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource, Theme, WindowId};
#[derive(Default)]
struct ModifiersShared(Rc<Cell<ModifiersState>>);
@ -68,14 +65,14 @@ impl ActiveEventLoop {
}
pub fn generate_id(&self) -> WindowId {
WindowId(self.runner.generate_id())
WindowId::from_raw(self.runner.generate_id())
}
pub fn create_custom_cursor_async(&self, source: CustomCursorSource) -> CustomCursorFuture {
CustomCursorFuture(CustomCursor::new_async(self, source.inner))
}
pub fn register(&self, canvas: &Rc<backend::Canvas>, id: WindowId) {
pub fn register(&self, canvas: &Rc<backend::Canvas>, window_id: WindowId) {
let canvas_clone = canvas.clone();
canvas.on_touch_start();
@ -89,13 +86,13 @@ impl ActiveEventLoop {
let clear_modifiers = (!modifiers.get().is_empty()).then(|| {
modifiers.set(ModifiersState::empty());
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(ModifiersState::empty().into()),
}
});
runner.send_events(clear_modifiers.into_iter().chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::Focused(false),
})));
});
@ -105,7 +102,7 @@ impl ActiveEventLoop {
canvas.on_focus(move || {
if !has_focus.replace(true) {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::Focused(true),
});
}
@ -124,10 +121,8 @@ impl ActiveEventLoop {
if focused {
canvas.has_focus.set(true);
self.runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
})
self.runner
.send_event(Event::WindowEvent { window_id, event: WindowEvent::Focused(true) })
}
let runner = self.runner.clone();
@ -137,14 +132,14 @@ impl ActiveEventLoop {
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
runner.send_events(
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::KeyboardInput {
device_id: None,
event: KeyEvent {
@ -171,14 +166,14 @@ impl ActiveEventLoop {
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
runner.send_events(
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::KeyboardInput {
device_id: None,
event: KeyEvent {
@ -208,13 +203,13 @@ impl ActiveEventLoop {
let focus = (has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
runner.send_events(focus.into_iter().chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::PointerLeft {
device_id: device_id.map(RootDeviceId),
position: Some(position),
@ -233,13 +228,13 @@ impl ActiveEventLoop {
let focus = (has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
runner.send_events(focus.into_iter().chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::PointerEntered {
device_id: device_id.map(RootDeviceId),
position,
@ -263,13 +258,13 @@ impl ActiveEventLoop {
.then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
modifiers.into_iter().chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::PointerMoved { device_id, position, source },
}))
}));
@ -285,7 +280,7 @@ impl ActiveEventLoop {
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
@ -293,7 +288,7 @@ impl ActiveEventLoop {
let device_id = device_id.map(RootDeviceId);
runner.send_events(modifiers.into_iter().chain([Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::PointerButton { device_id, state, position, button },
}]));
}
@ -308,14 +303,14 @@ impl ActiveEventLoop {
let modifiers = (modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
let device_id = pointer_id.map(RootDeviceId);
runner.send_events(modifiers.into_iter().chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::PointerButton {
device_id,
state: ElementState::Pressed,
@ -336,7 +331,7 @@ impl ActiveEventLoop {
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
@ -344,7 +339,7 @@ impl ActiveEventLoop {
let device_id = pointer_id.map(RootDeviceId);
runner.send_events(modifiers.into_iter().chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::PointerButton {
device_id,
state: ElementState::Released,
@ -362,14 +357,14 @@ impl ActiveEventLoop {
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
}
});
runner.send_events(modifiers_changed.into_iter().chain(iter::once(
Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::MouseWheel {
device_id: None,
delta,
@ -383,7 +378,7 @@ impl ActiveEventLoop {
canvas.on_dark_mode(move |is_dark_mode| {
let theme = if is_dark_mode { Theme::Dark } else { Theme::Light };
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::ThemeChanged(theme),
});
});
@ -410,7 +405,7 @@ impl ActiveEventLoop {
if canvas.old_size() != new_size {
canvas.set_old_size(new_size);
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::SurfaceResized(new_size),
});
canvas.request_animation_frame();
@ -426,7 +421,7 @@ impl ActiveEventLoop {
&& !(is_intersecting && canvas_clone.is_intersecting.get().is_none())
{
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
window_id,
event: WindowEvent::Occluded(!is_intersecting),
});
}
@ -435,7 +430,7 @@ impl ActiveEventLoop {
});
let runner = self.runner.clone();
canvas.on_animation_frame(move || runner.request_redraw(RootWindowId(id)));
canvas.on_animation_frame(move || runner.request_redraw(window_id));
canvas.on_context_menu();
}

View file

@ -48,5 +48,5 @@ pub(crate) use self::monitor::{
VideoModeHandle,
};
use self::web_sys as backend;
pub use self::window::{PlatformSpecificWindowAttributes, Window, WindowId};
pub use self::window::{PlatformSpecificWindowAttributes, Window};
pub(crate) use crate::icon::NoIcon as PlatformIcon;

View file

@ -14,7 +14,6 @@ use web_sys::{
use super::super::cursor::CursorHandler;
use super::super::event::DeviceId;
use super::super::main_thread::MainThreadMarker;
use super::super::WindowId;
use super::animation_frame::AnimationFrameHandler;
use super::event_handle::EventListenerHandle;
use super::intersection_handle::IntersectionObserverHandle;
@ -28,7 +27,7 @@ use crate::event::{
};
use crate::keyboard::{Key, KeyLocation, ModifiersState, PhysicalKey};
use crate::platform_impl::Fullscreen;
use crate::window::{WindowAttributes, WindowId as RootWindowId};
use crate::window::{WindowAttributes, WindowId};
#[allow(dead_code)]
pub struct Canvas {
@ -490,7 +489,7 @@ impl Canvas {
let new_size = {
let new_size = Arc::new(Mutex::new(current_size));
event_handler(crate::event::Event::WindowEvent {
window_id: RootWindowId(self.id),
window_id: self.id,
event: crate::event::WindowEvent::ScaleFactorChanged {
scale_factor: scale,
surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&new_size)),
@ -518,7 +517,7 @@ impl Canvas {
// Then we at least send a resized event.
self.set_old_size(new_size);
runner.send_event(crate::event::Event::WindowEvent {
window_id: RootWindowId(self.id),
window_id: self.id,
event: crate::event::WindowEvent::SurfaceResized(new_size),
})
}

View file

@ -14,7 +14,7 @@ use crate::icon::Icon;
use crate::monitor::MonitorHandle as RootMonitorHandle;
use crate::window::{
Cursor, CursorGrabMode, Fullscreen as RootFullscreen, ImePurpose, ResizeDirection, Theme,
UserAttentionType, Window as RootWindow, WindowAttributes, WindowButtons, WindowId as RootWI,
UserAttentionType, Window as RootWindow, WindowAttributes, WindowButtons, WindowId,
WindowLevel,
};
@ -53,7 +53,7 @@ impl Window {
target.register(&canvas, id);
let runner = target.runner.clone();
let destroy_fn = Box::new(move || runner.notify_destroy_window(RootWI(id)));
let destroy_fn = Box::new(move || runner.notify_destroy_window(id));
let inner = Inner {
id,
@ -65,7 +65,7 @@ impl Window {
let canvas = Rc::downgrade(&inner.canvas);
let (dispatcher, runner) = Dispatcher::new(target.runner.main_thread(), inner);
target.runner.add_canvas(RootWI(id), canvas, runner);
target.runner.add_canvas(id, canvas, runner);
Ok(Window { inner: dispatcher })
}
@ -91,8 +91,8 @@ impl Window {
}
impl RootWindow for Window {
fn id(&self) -> RootWI {
RootWI(self.inner.queue(|inner| inner.id))
fn id(&self) -> WindowId {
self.inner.queue(|inner| inner.id)
}
fn scale_factor(&self) -> f64 {
@ -429,19 +429,6 @@ impl Drop for Inner {
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(pub(crate) u64);
impl WindowId {
pub const fn into_raw(self) -> u64 {
self.0
}
pub const fn from_raw(id: u64) -> Self {
Self(id)
}
}
#[derive(Clone, Debug)]
pub struct PlatformSpecificWindowAttributes {
pub(crate) canvas: Option<Arc<MainThreadSafe<backend::RawCanvasType>>>,