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

@ -329,7 +329,7 @@ impl EventProcessor {
F: Fn(&Arc<UnownedWindow>) -> Ret,
{
let mut deleted = false;
let window_id = WindowId(window_id as _);
let window_id = WindowId::from_raw(window_id as _);
let result = self
.target
.windows
@ -798,7 +798,7 @@ impl EventProcessor {
// In the event that the window's been destroyed without being dropped first, we
// cleanup again here.
self.target.windows.borrow_mut().remove(&WindowId(window as _));
self.target.windows.borrow_mut().remove(&WindowId::from_raw(window as _));
// Since all XIM stuff needs to happen from the same thread, we destroy the input
// context here instead of when dropping the window.

View file

@ -31,12 +31,12 @@ use crate::event_loop::{
};
use crate::platform::pump_events::PumpStatus;
use crate::platform_impl::common::xkb::Context;
use crate::platform_impl::platform::{min_timeout, WindowId};
use crate::platform_impl::platform::min_timeout;
use crate::platform_impl::x11::window::Window;
use crate::platform_impl::{OwnedDisplayHandle, PlatformCustomCursor};
use crate::window::{
CustomCursor as RootCustomCursor, CustomCursorSource, Theme, Window as CoreWindow,
WindowAttributes,
WindowAttributes, WindowId,
};
mod activation;
@ -521,13 +521,14 @@ impl EventLoop {
// Empty activation tokens.
while let Ok((window_id, serial)) = self.activation_receiver.try_recv() {
let token = self.event_processor.with_window(window_id.0 as xproto::Window, |window| {
window.generate_activation_token()
});
let token = self
.event_processor
.with_window(window_id.into_raw() as xproto::Window, |window| {
window.generate_activation_token()
});
match token {
Some(Ok(token)) => {
let window_id = crate::window::WindowId(window_id);
let event = WindowEvent::ActivationTokenDone {
serial,
token: crate::window::ActivationToken::_new(token),
@ -555,7 +556,6 @@ impl EventLoop {
}
for window_id in windows {
let window_id = crate::window::WindowId(window_id);
app.window_event(
&self.event_processor.target,
window_id,
@ -574,12 +574,9 @@ impl EventLoop {
while unsafe { self.event_processor.poll_one_event(xev.as_mut_ptr()) } {
let mut xev = unsafe { xev.assume_init() };
self.event_processor.process_event(&mut xev, |window_target, event: Event| {
if let Event::WindowEvent {
window_id: crate::window::WindowId(wid),
event: WindowEvent::RedrawRequested,
} = event
if let Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } = event
{
window_target.redraw_sender.send(wid);
window_target.redraw_sender.send(window_id);
} else {
match event {
Event::WindowEvent { window_id, event } => {
@ -994,7 +991,7 @@ impl<'a, E: fmt::Debug> CookieResultExt for Result<VoidCookie<'a>, E> {
}
fn mkwid(w: xproto::Window) -> crate::window::WindowId {
crate::window::WindowId(crate::platform_impl::platform::WindowId(w as _))
crate::window::WindowId::from_raw(w as _)
}
fn mkdid(w: xinput::DeviceId) -> crate::event::DeviceId {
crate::event::DeviceId(crate::platform_impl::DeviceId::X(DeviceId(w)))

View file

@ -18,7 +18,7 @@ use x11rb::protocol::{randr, xinput};
use super::util::{self, SelectedCursor};
use super::{
ffi, ActiveEventLoop, CookieResultExt, ImeRequest, ImeSender, VoidCookie, WindowId, XConnection,
ffi, ActiveEventLoop, CookieResultExt, ImeRequest, ImeSender, VoidCookie, XConnection,
};
use crate::cursor::{Cursor, CustomCursor as RootCustomCursor};
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
@ -36,7 +36,7 @@ use crate::platform_impl::{
};
use crate::window::{
CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, Window as CoreWindow,
WindowAttributes, WindowButtons, WindowLevel,
WindowAttributes, WindowButtons, WindowId, WindowLevel,
};
pub(crate) struct Window(Arc<UnownedWindow>);
@ -62,8 +62,8 @@ impl Window {
}
impl CoreWindow for Window {
fn id(&self) -> crate::window::WindowId {
crate::window::WindowId(self.0.id())
fn id(&self) -> WindowId {
self.0.id()
}
fn scale_factor(&self) -> f64 {
@ -331,7 +331,9 @@ impl Drop for Window {
window.set_fullscreen(None);
}
if let Ok(c) = xconn.xcb_connection().destroy_window(window.id().0 as xproto::Window) {
if let Ok(c) =
xconn.xcb_connection().destroy_window(window.id().into_raw() as xproto::Window)
{
c.ignore_error();
}
}
@ -1220,11 +1222,10 @@ impl UnownedWindow {
&self.shared_state_lock(),
);
let window_id = crate::window::WindowId(self.id());
let old_surface_size = PhysicalSize::new(width, height);
let surface_size = Arc::new(Mutex::new(PhysicalSize::new(new_width, new_height)));
callback(Event::WindowEvent {
window_id,
window_id: self.id(),
event: WindowEvent::ScaleFactorChanged {
scale_factor: new_monitor.scale_factor,
surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade(&surface_size)),
@ -2134,7 +2135,7 @@ impl UnownedWindow {
#[inline]
pub fn id(&self) -> WindowId {
WindowId(self.xwindow as _)
WindowId::from_raw(self.xwindow as _)
}
pub(super) fn sync_counter_id(&self) -> Option<NonZeroU32> {
@ -2143,7 +2144,7 @@ impl UnownedWindow {
#[inline]
pub fn request_redraw(&self) {
self.redraw_sender.send(WindowId(self.xwindow as _));
self.redraw_sender.send(WindowId::from_raw(self.xwindow as _));
}
#[inline]