Skip to content

Commit e2557b7

Browse files
committed
Remove lifetime from the Event
Lifetimes don't work nicely when dealing with multithreaded environments in the current design of the existing winit's event handling model, so remove it in favor of `Weak` fences passed to client, so they could try to update the size. Fixes rust-windowing#1387.
1 parent ae9b02e commit e2557b7

29 files changed

+169
-406
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ And please only add new entries to the top of this list, right below the `# Unre
88

99
# Unreleased
1010

11+
- **Breaking:** Remove `PartialEq` implementation from the event.
12+
- **Breaking:** Remove `lifetime` from the event.
1113
- **Breaking:** `ActivationTokenDone` event which could be requested with the new `startup_notify` module, see its docs for more.
1214
- On Wayland, make double clicking and moving the CSD frame more reliable.
1315
- On macOS, add tabbing APIs on `WindowExtMacOS` and `EventLoopWindowTargetExtMacOS`.

examples/child_window.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn main() -> Result<(), impl std::error::Error> {
4646

4747
println!("parent window: {parent_window:?})");
4848

49-
event_loop.run(move |event: Event<'_, ()>, event_loop, control_flow| {
49+
event_loop.run(move |event: Event<()>, event_loop, control_flow| {
5050
*control_flow = ControlFlow::Wait;
5151

5252
if let Event::WindowEvent { event, window_id } = event {

examples/multithreaded.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ fn main() -> Result<(), impl std::error::Error> {
195195
}
196196
_ => {
197197
if let Some(tx) = window_senders.get(&window_id) {
198-
if let Some(event) = event.to_static() {
199-
tx.send(event).unwrap();
200-
}
198+
tx.send(event).unwrap();
201199
}
202200
}
203201
},

src/event.rs

+13-256
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
//!
3535
//! [`EventLoop::run(...)`]: crate::event_loop::EventLoop::run
3636
//! [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
37-
use smol_str::SmolStr;
3837
use std::path::PathBuf;
38+
use std::sync::{Mutex, Weak};
3939
#[cfg(not(wasm_platform))]
4040
use std::time::Instant;
41+
42+
use smol_str::SmolStr;
4143
#[cfg(wasm_platform)]
4244
use web_time::Instant;
4345

@@ -54,8 +56,8 @@ use crate::{
5456
/// Describes a generic event.
5557
///
5658
/// See the module-level docs for more information on the event loop manages each event.
57-
#[derive(Debug, PartialEq)]
58-
pub enum Event<'a, T: 'static> {
59+
#[derive(Debug)]
60+
pub enum Event<T: 'static> {
5961
/// Emitted when new events arrive from the OS to be processed.
6062
///
6163
/// This event type is useful as a place to put code that should be done before you start
@@ -67,7 +69,7 @@ pub enum Event<'a, T: 'static> {
6769
/// Emitted when the OS sends an event to a winit window.
6870
WindowEvent {
6971
window_id: WindowId,
70-
event: WindowEvent<'a>,
72+
event: WindowEvent,
7173
},
7274

7375
/// Emitted when the OS sends an event to a device.
@@ -261,33 +263,9 @@ pub enum Event<'a, T: 'static> {
261263
LoopDestroyed,
262264
}
263265

264-
impl<T: Clone> Clone for Event<'static, T> {
265-
fn clone(&self) -> Self {
266-
use self::Event::*;
267-
match self {
268-
WindowEvent { window_id, event } => WindowEvent {
269-
window_id: *window_id,
270-
event: event.clone(),
271-
},
272-
UserEvent(event) => UserEvent(event.clone()),
273-
DeviceEvent { device_id, event } => DeviceEvent {
274-
device_id: *device_id,
275-
event: event.clone(),
276-
},
277-
NewEvents(cause) => NewEvents(*cause),
278-
MainEventsCleared => MainEventsCleared,
279-
RedrawRequested(wid) => RedrawRequested(*wid),
280-
RedrawEventsCleared => RedrawEventsCleared,
281-
LoopDestroyed => LoopDestroyed,
282-
Suspended => Suspended,
283-
Resumed => Resumed,
284-
}
285-
}
286-
}
287-
288-
impl<'a, T> Event<'a, T> {
266+
impl<T> Event<T> {
289267
#[allow(clippy::result_large_err)]
290-
pub fn map_nonuser_event<U>(self) -> Result<Event<'a, U>, Event<'a, T>> {
268+
pub fn map_nonuser_event<U>(self) -> Result<Event<U>, Event<T>> {
291269
use self::Event::*;
292270
match self {
293271
UserEvent(_) => Err(self),
@@ -302,26 +280,6 @@ impl<'a, T> Event<'a, T> {
302280
Resumed => Ok(Resumed),
303281
}
304282
}
305-
306-
/// If the event doesn't contain a reference, turn it into an event with a `'static` lifetime.
307-
/// Otherwise, return `None`.
308-
pub fn to_static(self) -> Option<Event<'static, T>> {
309-
use self::Event::*;
310-
match self {
311-
WindowEvent { window_id, event } => event
312-
.to_static()
313-
.map(|event| WindowEvent { window_id, event }),
314-
UserEvent(event) => Some(UserEvent(event)),
315-
DeviceEvent { device_id, event } => Some(DeviceEvent { device_id, event }),
316-
NewEvents(cause) => Some(NewEvents(cause)),
317-
MainEventsCleared => Some(MainEventsCleared),
318-
RedrawRequested(wid) => Some(RedrawRequested(wid)),
319-
RedrawEventsCleared => Some(RedrawEventsCleared),
320-
LoopDestroyed => Some(LoopDestroyed),
321-
Suspended => Some(Suspended),
322-
Resumed => Some(Resumed),
323-
}
324-
}
325283
}
326284

327285
/// Describes the reason the event loop is resuming.
@@ -355,8 +313,8 @@ pub enum StartCause {
355313
}
356314

357315
/// Describes an event from a [`Window`].
358-
#[derive(Debug, PartialEq)]
359-
pub enum WindowEvent<'a> {
316+
#[derive(Debug, Clone)]
317+
pub enum WindowEvent {
360318
/// The activation token was delivered back and now could be used.
361319
///
362320
#[cfg_attr(
@@ -590,7 +548,9 @@ pub enum WindowEvent<'a> {
590548
/// For more information about DPI in general, see the [`dpi`](crate::dpi) module.
591549
ScaleFactorChanged {
592550
scale_factor: f64,
593-
new_inner_size: &'a mut PhysicalSize<u32>,
551+
/// The pointer could be upgraded successfully only from the callback for this event,
552+
/// otherwise it'll always fail to do so.
553+
new_inner_size: Weak<Mutex<PhysicalSize<u32>>>,
594554
},
595555

596556
/// The system window theme has changed.
@@ -619,209 +579,6 @@ pub enum WindowEvent<'a> {
619579
Occluded(bool),
620580
}
621581

622-
impl Clone for WindowEvent<'static> {
623-
fn clone(&self) -> Self {
624-
use self::WindowEvent::*;
625-
return match self {
626-
ActivationTokenDone { serial, token } => ActivationTokenDone {
627-
serial: *serial,
628-
token: token.clone(),
629-
},
630-
Resized(size) => Resized(*size),
631-
Moved(pos) => Moved(*pos),
632-
CloseRequested => CloseRequested,
633-
Destroyed => Destroyed,
634-
DroppedFile(file) => DroppedFile(file.clone()),
635-
HoveredFile(file) => HoveredFile(file.clone()),
636-
HoveredFileCancelled => HoveredFileCancelled,
637-
Focused(f) => Focused(*f),
638-
KeyboardInput {
639-
device_id,
640-
event,
641-
is_synthetic,
642-
} => KeyboardInput {
643-
device_id: *device_id,
644-
event: event.clone(),
645-
is_synthetic: *is_synthetic,
646-
},
647-
Ime(preedit_state) => Ime(preedit_state.clone()),
648-
ModifiersChanged(modifiers) => ModifiersChanged(*modifiers),
649-
CursorMoved {
650-
device_id,
651-
position,
652-
} => CursorMoved {
653-
device_id: *device_id,
654-
position: *position,
655-
},
656-
CursorEntered { device_id } => CursorEntered {
657-
device_id: *device_id,
658-
},
659-
CursorLeft { device_id } => CursorLeft {
660-
device_id: *device_id,
661-
},
662-
MouseWheel {
663-
device_id,
664-
delta,
665-
phase,
666-
} => MouseWheel {
667-
device_id: *device_id,
668-
delta: *delta,
669-
phase: *phase,
670-
},
671-
MouseInput {
672-
device_id,
673-
state,
674-
button,
675-
} => MouseInput {
676-
device_id: *device_id,
677-
state: *state,
678-
button: *button,
679-
},
680-
TouchpadMagnify {
681-
device_id,
682-
delta,
683-
phase,
684-
} => TouchpadMagnify {
685-
device_id: *device_id,
686-
delta: *delta,
687-
phase: *phase,
688-
},
689-
SmartMagnify { device_id } => SmartMagnify {
690-
device_id: *device_id,
691-
},
692-
TouchpadRotate {
693-
device_id,
694-
delta,
695-
phase,
696-
} => TouchpadRotate {
697-
device_id: *device_id,
698-
delta: *delta,
699-
phase: *phase,
700-
},
701-
TouchpadPressure {
702-
device_id,
703-
pressure,
704-
stage,
705-
} => TouchpadPressure {
706-
device_id: *device_id,
707-
pressure: *pressure,
708-
stage: *stage,
709-
},
710-
AxisMotion {
711-
device_id,
712-
axis,
713-
value,
714-
} => AxisMotion {
715-
device_id: *device_id,
716-
axis: *axis,
717-
value: *value,
718-
},
719-
Touch(touch) => Touch(*touch),
720-
ThemeChanged(theme) => ThemeChanged(*theme),
721-
ScaleFactorChanged { .. } => {
722-
unreachable!("Static event can't be about scale factor changing")
723-
}
724-
Occluded(occluded) => Occluded(*occluded),
725-
};
726-
}
727-
}
728-
729-
impl<'a> WindowEvent<'a> {
730-
pub fn to_static(self) -> Option<WindowEvent<'static>> {
731-
use self::WindowEvent::*;
732-
match self {
733-
ActivationTokenDone { serial, token } => Some(ActivationTokenDone { serial, token }),
734-
Resized(size) => Some(Resized(size)),
735-
Moved(position) => Some(Moved(position)),
736-
CloseRequested => Some(CloseRequested),
737-
Destroyed => Some(Destroyed),
738-
DroppedFile(file) => Some(DroppedFile(file)),
739-
HoveredFile(file) => Some(HoveredFile(file)),
740-
HoveredFileCancelled => Some(HoveredFileCancelled),
741-
Focused(focused) => Some(Focused(focused)),
742-
KeyboardInput {
743-
device_id,
744-
event,
745-
is_synthetic,
746-
} => Some(KeyboardInput {
747-
device_id,
748-
event,
749-
is_synthetic,
750-
}),
751-
ModifiersChanged(modifers) => Some(ModifiersChanged(modifers)),
752-
Ime(event) => Some(Ime(event)),
753-
CursorMoved {
754-
device_id,
755-
position,
756-
} => Some(CursorMoved {
757-
device_id,
758-
position,
759-
}),
760-
CursorEntered { device_id } => Some(CursorEntered { device_id }),
761-
CursorLeft { device_id } => Some(CursorLeft { device_id }),
762-
MouseWheel {
763-
device_id,
764-
delta,
765-
phase,
766-
} => Some(MouseWheel {
767-
device_id,
768-
delta,
769-
phase,
770-
}),
771-
MouseInput {
772-
device_id,
773-
state,
774-
button,
775-
} => Some(MouseInput {
776-
device_id,
777-
state,
778-
button,
779-
}),
780-
TouchpadMagnify {
781-
device_id,
782-
delta,
783-
phase,
784-
} => Some(TouchpadMagnify {
785-
device_id,
786-
delta,
787-
phase,
788-
}),
789-
SmartMagnify { device_id } => Some(SmartMagnify { device_id }),
790-
TouchpadRotate {
791-
device_id,
792-
delta,
793-
phase,
794-
} => Some(TouchpadRotate {
795-
device_id,
796-
delta,
797-
phase,
798-
}),
799-
TouchpadPressure {
800-
device_id,
801-
pressure,
802-
stage,
803-
} => Some(TouchpadPressure {
804-
device_id,
805-
pressure,
806-
stage,
807-
}),
808-
AxisMotion {
809-
device_id,
810-
axis,
811-
value,
812-
} => Some(AxisMotion {
813-
device_id,
814-
axis,
815-
value,
816-
}),
817-
Touch(touch) => Some(Touch(touch)),
818-
ThemeChanged(theme) => Some(ThemeChanged(theme)),
819-
ScaleFactorChanged { .. } => None,
820-
Occluded(occluded) => Some(Occluded(occluded)),
821-
}
822-
}
823-
}
824-
825582
/// Identifier of an input device.
826583
///
827584
/// Whenever you receive an event arising from a particular input device, this event contains a `DeviceId` which

src/event_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<T> EventLoop<T> {
314314
#[inline]
315315
pub fn run<F>(self, event_handler: F) -> Result<(), RunLoopError>
316316
where
317-
F: 'static + FnMut(Event<'_, T>, &EventLoopWindowTarget<T>, &mut ControlFlow),
317+
F: 'static + FnMut(Event<T>, &EventLoopWindowTarget<T>, &mut ControlFlow),
318318
{
319319
self.event_loop.run(event_handler)
320320
}

src/platform/pump_events.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,15 @@ pub trait EventLoopExtPumpEvents {
174174
/// callback.
175175
fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
176176
where
177-
F: FnMut(
178-
Event<'_, Self::UserEvent>,
179-
&EventLoopWindowTarget<Self::UserEvent>,
180-
&mut ControlFlow,
181-
);
177+
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow);
182178
}
183179

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

187183
fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
188184
where
189-
F: FnMut(
190-
Event<'_, Self::UserEvent>,
191-
&EventLoopWindowTarget<Self::UserEvent>,
192-
&mut ControlFlow,
193-
),
185+
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>, &mut ControlFlow),
194186
{
195187
self.event_loop.pump_events(timeout, event_handler)
196188
}

0 commit comments

Comments
 (0)