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

@ -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;
@ -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,
@ -187,22 +191,19 @@ 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,
@ -210,7 +211,7 @@ impl EventLoop {
scale_factor,
};
app.window_event(&self.window_target, window_id, event);
app.window_event(&self.window_target, GLOBAL_WINDOW, event);
}
},
MainEvent::LowMemory => {
@ -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);
}
}
@ -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();
@ -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,
@ -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 {
@ -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 {
@ -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 {
@ -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!(),
}
@ -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 {
@ -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);
},
}
},
@ -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);
@ -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> {