Skip to content

Commit

Permalink
Remove generic parameter T from EventLoopWindowTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 4, 2024
1 parent dd12746 commit 7466225
Show file tree
Hide file tree
Showing 36 changed files with 157 additions and 216 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Unreleased` header.

# Unreleased

- **Breaking:** Removed unnecessary generic parameter `T` from `EventLoopWindowTarget`.
- On Wayland, fix resize not issued when scale changes
- On X11 and Wayland, fix arrow up on keypad reported as `ArrowLeft`.
- On Windows, macOS, X11, Wayland and Web, implement setting images as cursors. See the `custom_cursors.rs` example.
Expand Down
2 changes: 1 addition & 1 deletion examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() -> Result<(), impl std::error::Error> {

fn spawn_child_window(
parent: &Window,
event_loop: &EventLoopWindowTarget<()>,
event_loop: &EventLoopWindowTarget,
windows: &mut HashMap<WindowId, Window>,
) {
let parent = parent.raw_window_handle().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_cursors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use winit::{
window::{CursorIcon, CustomCursor, WindowBuilder},
};

fn decode_cursor<T>(bytes: &[u8], window_target: &EventLoopWindowTarget<T>) -> CustomCursor {
fn decode_cursor(bytes: &[u8], window_target: &EventLoopWindowTarget) -> CustomCursor {
let img = image::load_from_memory(bytes).unwrap().to_rgba8();
let samples = img.into_flat_samples();
let (_, w, h) = samples.extents();
Expand Down
9 changes: 3 additions & 6 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub struct CustomCursorBuilder {
}

impl CustomCursorBuilder {
pub fn build<T>(self, window_target: &EventLoopWindowTarget<T>) -> CustomCursor {
pub fn build(self, window_target: &EventLoopWindowTarget) -> CustomCursor {
CustomCursor {
inner: PlatformCustomCursor::build(self.inner, &window_target.p),
}
Expand Down Expand Up @@ -213,10 +213,7 @@ impl Eq for OnlyCursorImage {}

#[allow(dead_code)]
impl OnlyCursorImage {
fn build<T>(
builder: OnlyCursorImageBuilder,
_: &platform_impl::EventLoopWindowTarget<T>,
) -> Self {
fn build(builder: OnlyCursorImageBuilder, _: &platform_impl::EventLoopWindowTarget) -> Self {
Self(Arc::new(builder.0))
}
}
Expand Down Expand Up @@ -296,7 +293,7 @@ impl NoCustomCursor {
Ok(Self)
}

fn build<T>(self, _: &platform_impl::EventLoopWindowTarget<T>) -> NoCustomCursor {
fn build(self, _: &platform_impl::EventLoopWindowTarget) -> NoCustomCursor {
self
}
}
19 changes: 9 additions & 10 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ pub struct EventLoop<T: 'static> {
/// your callback. [`EventLoop`] will coerce into this type (`impl<T> Deref for
/// EventLoop<T>`), so functions that take this as a parameter can also take
/// `&EventLoop`.
pub struct EventLoopWindowTarget<T: 'static> {
pub(crate) p: platform_impl::EventLoopWindowTarget<T>,
pub(crate) _marker: PhantomData<*mut T>, // Not Send nor Sync + invariant over T
pub struct EventLoopWindowTarget {
pub(crate) p: platform_impl::EventLoopWindowTarget,
}

/// Object that allows building the event loop.
Expand Down Expand Up @@ -142,7 +141,7 @@ impl<T> fmt::Debug for EventLoop<T> {
}
}

impl<T> fmt::Debug for EventLoopWindowTarget<T> {
impl fmt::Debug for EventLoopWindowTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("EventLoopWindowTarget { .. }")
}
Expand Down Expand Up @@ -241,7 +240,7 @@ impl<T> EventLoop<T> {
#[cfg(not(all(wasm_platform, target_feature = "exception-handling")))]
pub fn run<F>(self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<T>, &EventLoopWindowTarget<T>),
F: FnMut(Event<T>, &EventLoopWindowTarget),
{
self.event_loop.run(event_handler)
}
Expand Down Expand Up @@ -298,13 +297,13 @@ impl<T> AsRawFd for EventLoop<T> {
}

impl<T> Deref for EventLoop<T> {
type Target = EventLoopWindowTarget<T>;
fn deref(&self) -> &EventLoopWindowTarget<T> {
type Target = EventLoopWindowTarget;
fn deref(&self) -> &EventLoopWindowTarget {
self.event_loop.window_target()
}
}

impl<T> EventLoopWindowTarget<T> {
impl EventLoopWindowTarget {
/// Returns the list of all the monitors available on the system.
#[inline]
pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
Expand Down Expand Up @@ -370,7 +369,7 @@ impl<T> EventLoopWindowTarget<T> {
}

#[cfg(feature = "rwh_06")]
impl<T> rwh_06::HasDisplayHandle for EventLoopWindowTarget<T> {
impl rwh_06::HasDisplayHandle for EventLoopWindowTarget {
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
let raw = self.p.raw_display_handle_rwh_06()?;
// SAFETY: The display will never be deallocated while the event loop is alive.
Expand All @@ -379,7 +378,7 @@ impl<T> rwh_06::HasDisplayHandle for EventLoopWindowTarget<T> {
}

#[cfg(feature = "rwh_05")]
unsafe impl<T> rwh_05::HasRawDisplayHandle for EventLoopWindowTarget<T> {
unsafe impl rwh_05::HasRawDisplayHandle for EventLoopWindowTarget {
/// Returns a [`rwh_05::RawDisplayHandle`] for the event loop.
fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle {
self.p.raw_display_handle_rwh_05()
Expand Down
2 changes: 1 addition & 1 deletion src/platform/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl WindowExtAndroid for Window {
}
}

impl<T> EventLoopWindowTargetExtAndroid for EventLoopWindowTarget<T> {}
impl EventLoopWindowTargetExtAndroid for EventLoopWindowTarget {}

/// Additional methods on [`WindowBuilder`] that are specific to Android.
pub trait WindowBuilderExtAndroid {}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub trait EventLoopWindowTargetExtMacOS {
fn allows_automatic_window_tabbing(&self) -> bool;
}

impl<T> EventLoopWindowTargetExtMacOS for EventLoopWindowTarget<T> {
impl EventLoopWindowTargetExtMacOS for EventLoopWindowTarget {
fn hide_application(&self) {
self.p.hide_application()
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/pump_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ pub trait EventLoopExtPumpEvents {
/// callback.
fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
where
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>);
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget);
}

impl<T> EventLoopExtPumpEvents for EventLoop<T> {
type UserEvent = T;

fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
where
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>),
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget),
{
self.event_loop.pump_events(timeout, event_handler)
}
Expand Down
6 changes: 3 additions & 3 deletions src/platform/run_on_demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ pub trait EventLoopExtRunOnDemand {
/// [`set_control_flow()`]: EventLoopWindowTarget::set_control_flow()
fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>);
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget);
}

impl<T> EventLoopExtRunOnDemand for EventLoop<T> {
type UserEvent = T;

fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>),
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget),
{
self.event_loop.window_target().clear_exit();
self.event_loop.run_on_demand(event_handler)
}
}

impl<T> EventLoopWindowTarget<T> {
impl EventLoopWindowTarget {
/// Clear exit status.
pub(crate) fn clear_exit(&self) {
self.p.clear_exit()
Expand Down
2 changes: 1 addition & 1 deletion src/platform/startup_notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub trait WindowBuilderExtStartupNotify {
fn with_activation_token(self, token: ActivationToken) -> Self;
}

impl<T> EventLoopExtStartupNotify for EventLoopWindowTarget<T> {
impl EventLoopExtStartupNotify for EventLoopWindowTarget {
fn read_token_from_env(&self) -> Option<ActivationToken> {
match self.p {
#[cfg(wayland_platform)]
Expand Down
2 changes: 1 addition & 1 deletion src/platform/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait EventLoopWindowTargetExtWayland {
fn is_wayland(&self) -> bool;
}

impl<T> EventLoopWindowTargetExtWayland for EventLoopWindowTarget<T> {
impl EventLoopWindowTargetExtWayland for EventLoopWindowTarget {
#[inline]
fn is_wayland(&self) -> bool {
self.p.is_wayland()
Expand Down
6 changes: 3 additions & 3 deletions src/platform/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ pub trait EventLoopExtWebSys {
/// [^1]: `run()` is _not_ available on WASM when the target supports `exception-handling`.
fn spawn<F>(self, event_handler: F)
where
F: 'static + FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>);
F: 'static + FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget);
}

impl<T> EventLoopExtWebSys for EventLoop<T> {
type UserEvent = T;

fn spawn<F>(self, event_handler: F)
where
F: 'static + FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>),
F: 'static + FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget),
{
self.event_loop.spawn(event_handler)
}
Expand All @@ -195,7 +195,7 @@ pub trait EventLoopWindowTargetExtWebSys {
fn poll_strategy(&self) -> PollStrategy;
}

impl<T> EventLoopWindowTargetExtWebSys for EventLoopWindowTarget<T> {
impl EventLoopWindowTargetExtWebSys for EventLoopWindowTarget {
#[inline]
fn set_poll_strategy(&self, strategy: PollStrategy) {
self.p.set_poll_strategy(strategy);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub trait EventLoopWindowTargetExtX11 {
fn is_x11(&self) -> bool;
}

impl<T> EventLoopWindowTargetExtX11 for EventLoopWindowTarget<T> {
impl EventLoopWindowTargetExtX11 for EventLoopWindowTarget {
#[inline]
fn is_x11(&self) -> bool {
!self.p.is_wayland()
Expand Down
27 changes: 12 additions & 15 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub struct KeyEventExtra {}

pub struct EventLoop<T: 'static> {
android_app: AndroidApp,
window_target: event_loop::EventLoopWindowTarget<T>,
window_target: event_loop::EventLoopWindowTarget,
redraw_flag: SharedFlag,
user_events_sender: mpsc::Sender<T>,
user_events_receiver: PeekableReceiver<T>, //must wake looper whenever something gets sent
Expand Down Expand Up @@ -187,9 +187,7 @@ impl<T: 'static> EventLoop<T> {
&redraw_flag,
android_app.create_waker(),
),
_marker: std::marker::PhantomData,
},
_marker: std::marker::PhantomData,
},
redraw_flag,
user_events_sender,
Expand All @@ -205,7 +203,7 @@ impl<T: 'static> EventLoop<T> {

fn single_iteration<F>(&mut self, main_event: Option<MainEvent<'_>>, callback: &mut F)
where
F: FnMut(event::Event<T>, &RootELW<T>),
F: FnMut(event::Event<T>, &RootELW),
{
trace!("Mainloop iteration");

Expand Down Expand Up @@ -377,7 +375,7 @@ impl<T: 'static> EventLoop<T> {
callback: &mut F,
) -> InputStatus
where
F: FnMut(event::Event<T>, &RootELW<T>),
F: FnMut(event::Event<T>, &RootELW),
{
let mut input_status = InputStatus::Handled;
match event {
Expand Down Expand Up @@ -482,14 +480,14 @@ impl<T: 'static> EventLoop<T> {

pub fn run<F>(mut self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget<T>),
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget),
{
self.run_on_demand(event_handler)
}

pub fn run_on_demand<F>(&mut self, mut event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget<T>),
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget),
{
if self.loop_running {
return Err(EventLoopError::AlreadyRunning);
Expand All @@ -512,7 +510,7 @@ impl<T: 'static> EventLoop<T> {

pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
where
F: FnMut(event::Event<T>, &RootELW<T>),
F: FnMut(event::Event<T>, &RootELW),
{
if !self.loop_running {
self.loop_running = true;
Expand Down Expand Up @@ -545,7 +543,7 @@ impl<T: 'static> EventLoop<T> {

fn poll_events_with_timeout<F>(&mut self, mut timeout: Option<Duration>, mut callback: F)
where
F: FnMut(event::Event<T>, &RootELW<T>),
F: FnMut(event::Event<T>, &RootELW),
{
let start = Instant::now();

Expand Down Expand Up @@ -621,7 +619,7 @@ impl<T: 'static> EventLoop<T> {
});
}

pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget<T> {
pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget {
&self.window_target
}

Expand Down Expand Up @@ -665,15 +663,14 @@ impl<T> EventLoopProxy<T> {
}
}

pub struct EventLoopWindowTarget<T: 'static> {
pub struct EventLoopWindowTarget {
app: AndroidApp,
control_flow: Cell<ControlFlow>,
exit: Cell<bool>,
redraw_requester: RedrawRequester,
_marker: std::marker::PhantomData<T>,
}

impl<T: 'static> EventLoopWindowTarget<T> {
impl EventLoopWindowTarget {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
Some(MonitorHandle::new(self.app.clone()))
}
Expand Down Expand Up @@ -763,8 +760,8 @@ pub(crate) struct Window {
}

impl Window {
pub(crate) fn new<T: 'static>(
el: &EventLoopWindowTarget<T>,
pub(crate) fn new(
el: &EventLoopWindowTarget,
_window_attrs: window::WindowAttributes,
_: PlatformSpecificWindowBuilderAttributes,
) -> Result<Self, error::OsError> {
Expand Down
Loading

0 comments on commit 7466225

Please sign in to comment.