Skip to content

Commit b8b092b

Browse files
committed
Avoid Event enum
1 parent b7fe5e0 commit b8b092b

File tree

8 files changed

+140
-150
lines changed

8 files changed

+140
-150
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ fn main() {
8888
},
8989
|_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(),
9090
)
91-
.with_event_handler(|window, surface, event, elwt| {
91+
.with_event_handler(|window, surface, window_id, event, elwt| {
9292
elwt.set_control_flow(ControlFlow::Wait);
9393
94+
if window_id != window.id() {
95+
return;
96+
}
97+
9498
match event {
95-
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {
99+
WindowEvent::RedrawRequested => {
96100
let Some(surface) = surface else {
97101
eprintln!("RedrawRequested fired before Resumed or after Suspended");
98102
return;
@@ -121,10 +125,7 @@ fn main() {
121125
122126
buffer.present().unwrap();
123127
}
124-
Event::WindowEvent {
125-
event: WindowEvent::CloseRequested,
126-
window_id,
127-
} if window_id == window.id() => {
128+
WindowEvent::CloseRequested => {
128129
elwt.exit();
129130
}
130131
_ => {}

examples/animation.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rayon::prelude::*;
33
use std::f64::consts::PI;
44
use std::num::NonZeroU32;
55
use web_time::Instant;
6-
use winit::event::{Event, KeyEvent, WindowEvent};
6+
use winit::event::{KeyEvent, WindowEvent};
77
use winit::event_loop::{ControlFlow, EventLoop};
88
use winit::keyboard::{Key, NamedKey};
99

@@ -29,16 +29,17 @@ fn main() {
2929
softbuffer::Surface::new(&context, window.clone()).unwrap()
3030
},
3131
)
32-
.with_event_handler(move |state, surface, event, elwt| {
32+
.with_event_handler(move |state, surface, window_id, event, elwt| {
3333
let (window, old_size, frames) = state;
3434

3535
elwt.set_control_flow(ControlFlow::Poll);
3636

37+
if window_id != window.id() {
38+
return;
39+
}
40+
3741
match event {
38-
Event::WindowEvent {
39-
window_id,
40-
event: WindowEvent::Resized(size),
41-
} if window_id == window.id() => {
42+
WindowEvent::Resized(size) => {
4243
let Some(surface) = surface else {
4344
eprintln!("Resized fired before Resumed or after Suspended");
4445
return;
@@ -50,10 +51,7 @@ fn main() {
5051
surface.resize(width, height).unwrap();
5152
}
5253
}
53-
Event::WindowEvent {
54-
window_id,
55-
event: WindowEvent::RedrawRequested,
56-
} if window_id == window.id() => {
54+
WindowEvent::RedrawRequested => {
5755
let Some(surface) = surface else {
5856
eprintln!("RedrawRequested fired before Resumed or after Suspended");
5957
return;
@@ -77,26 +75,23 @@ fn main() {
7775
buffer.present().unwrap();
7876
}
7977
}
80-
Event::AboutToWait => {
81-
window.request_redraw();
82-
}
83-
Event::WindowEvent {
78+
WindowEvent::CloseRequested
79+
| WindowEvent::KeyboardInput {
8480
event:
85-
WindowEvent::CloseRequested
86-
| WindowEvent::KeyboardInput {
87-
event:
88-
KeyEvent {
89-
logical_key: Key::Named(NamedKey::Escape),
90-
..
91-
},
81+
KeyEvent {
82+
logical_key: Key::Named(NamedKey::Escape),
9283
..
9384
},
94-
window_id,
95-
} if window_id == window.id() => {
85+
..
86+
} => {
9687
elwt.exit();
9788
}
9889
_ => {}
9990
}
91+
})
92+
.with_about_to_wait_handler(|state, _, _| {
93+
let (window, _, _) = state;
94+
window.request_redraw();
10095
});
10196

10297
winit_app::run_app(event_loop, app);

examples/fruit.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use image::GenericImageView;
22
use std::num::NonZeroU32;
3-
use winit::event::{Event, KeyEvent, WindowEvent};
3+
use winit::event::{KeyEvent, WindowEvent};
44
use winit::event_loop::{ControlFlow, EventLoop};
55
use winit::keyboard::{Key, NamedKey};
66

@@ -35,14 +35,15 @@ fn main() {
3535
surface
3636
},
3737
)
38-
.with_event_handler(move |window, surface, event, elwt| {
38+
.with_event_handler(move |window, surface, window_id, event, elwt| {
3939
elwt.set_control_flow(ControlFlow::Wait);
4040

41+
if window_id != window.id() {
42+
return;
43+
}
44+
4145
match event {
42-
Event::WindowEvent {
43-
window_id,
44-
event: WindowEvent::RedrawRequested,
45-
} if window_id == window.id() => {
46+
WindowEvent::RedrawRequested => {
4647
let Some(surface) = surface else {
4748
eprintln!("RedrawRequested fired before Resumed or after Suspended");
4849
return;
@@ -61,19 +62,15 @@ fn main() {
6162

6263
buffer.present().unwrap();
6364
}
64-
Event::WindowEvent {
65+
WindowEvent::CloseRequested
66+
| WindowEvent::KeyboardInput {
6567
event:
66-
WindowEvent::CloseRequested
67-
| WindowEvent::KeyboardInput {
68-
event:
69-
KeyEvent {
70-
logical_key: Key::Named(NamedKey::Escape),
71-
..
72-
},
68+
KeyEvent {
69+
logical_key: Key::Named(NamedKey::Escape),
7370
..
7471
},
75-
window_id,
76-
} if window_id == window.id() => {
72+
..
73+
} => {
7774
elwt.exit();
7875
}
7976
_ => {}

examples/rectangle.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::num::NonZeroU32;
2-
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
2+
use winit::event::{ElementState, KeyEvent, WindowEvent};
33
use winit::event_loop::{ControlFlow, EventLoop};
44
use winit::keyboard::{Key, NamedKey};
55

@@ -38,16 +38,17 @@ fn main() {
3838
},
3939
move |_elwt, (window, _flag)| softbuffer::Surface::new(&context, window.clone()).unwrap(),
4040
)
41-
.with_event_handler(|state, surface, event, elwt| {
41+
.with_event_handler(|state, surface, window_id, event, elwt| {
4242
let (window, flag) = state;
4343

4444
elwt.set_control_flow(ControlFlow::Wait);
4545

46+
if window_id != window.id() {
47+
return;
48+
}
49+
4650
match event {
47-
Event::WindowEvent {
48-
window_id,
49-
event: WindowEvent::Resized(size),
50-
} if window_id == window.id() => {
51+
WindowEvent::Resized(size) => {
5152
let Some(surface) = surface else {
5253
eprintln!("Resized fired before Resumed or after Suspended");
5354
return;
@@ -60,10 +61,7 @@ fn main() {
6061
surface.resize(width, height).unwrap();
6162
}
6263
}
63-
Event::WindowEvent {
64-
window_id,
65-
event: WindowEvent::RedrawRequested,
66-
} if window_id == window.id() => {
64+
WindowEvent::RedrawRequested => {
6765
let Some(surface) = surface else {
6866
eprintln!("RedrawRequested fired before Resumed or after Suspended");
6967
return;
@@ -85,35 +83,27 @@ fn main() {
8583
}
8684
}
8785

88-
Event::WindowEvent {
86+
WindowEvent::CloseRequested
87+
| WindowEvent::KeyboardInput {
8988
event:
90-
WindowEvent::CloseRequested
91-
| WindowEvent::KeyboardInput {
92-
event:
93-
KeyEvent {
94-
logical_key: Key::Named(NamedKey::Escape),
95-
..
96-
},
89+
KeyEvent {
90+
logical_key: Key::Named(NamedKey::Escape),
9791
..
9892
},
99-
window_id,
100-
} if window_id == window.id() => {
93+
..
94+
} => {
10195
elwt.exit();
10296
}
10397

104-
Event::WindowEvent {
98+
WindowEvent::KeyboardInput {
10599
event:
106-
WindowEvent::KeyboardInput {
107-
event:
108-
KeyEvent {
109-
state: ElementState::Pressed,
110-
logical_key: Key::Named(NamedKey::Space),
111-
..
112-
},
100+
KeyEvent {
101+
state: ElementState::Pressed,
102+
logical_key: Key::Named(NamedKey::Space),
113103
..
114104
},
115-
window_id,
116-
} if window_id == window.id() => {
105+
..
106+
} => {
117107
// Flip the rectangle flag and request a redraw to show the changed image
118108
*flag = !*flag;
119109
window.request_redraw();

examples/utils/winit_app.rs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::marker::PhantomData;
33
use std::rc::Rc;
44

55
use winit::application::ApplicationHandler;
6-
use winit::event::{Event, WindowEvent};
6+
use winit::event::WindowEvent;
77
use winit::event_loop::{ActiveEventLoop, EventLoop};
88
use winit::window::{Window, WindowAttributes, WindowId};
99

@@ -31,7 +31,7 @@ pub(crate) fn make_window(
3131
}
3232

3333
/// Easily constructable winit application.
34-
pub(crate) struct WinitApp<T, S, Init, InitSurface, Handler> {
34+
pub(crate) struct WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler> {
3535
/// Closure to initialize `state`.
3636
init: Init,
3737

@@ -41,6 +41,9 @@ pub(crate) struct WinitApp<T, S, Init, InitSurface, Handler> {
4141
/// Closure to run on window events.
4242
event: Handler,
4343

44+
/// Closure to run on about_to_wait events.
45+
about_to_wait: AboutToWaitHandler,
46+
4447
/// Contained state.
4548
state: Option<T>,
4649

@@ -75,38 +78,63 @@ where
7578
}
7679

7780
/// Build a new application.
78-
pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, S, Init, InitSurface, F>
81+
#[allow(clippy::type_complexity)]
82+
pub(crate) fn with_event_handler<F>(
83+
self,
84+
handler: F,
85+
) -> WinitApp<T, S, Init, InitSurface, F, impl FnMut(&mut T, Option<&mut S>, &ActiveEventLoop)>
7986
where
80-
F: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop),
87+
F: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop),
8188
{
82-
WinitApp::new(self.init, self.init_surface, handler)
89+
WinitApp::new(self.init, self.init_surface, handler, |_, _, _| {})
8390
}
8491
}
8592

86-
impl<T, S, Init, InitSurface, Handler> WinitApp<T, S, Init, InitSurface, Handler>
93+
impl<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
94+
WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
8795
where
8896
Init: FnMut(&ActiveEventLoop) -> T,
8997
InitSurface: FnMut(&ActiveEventLoop, &mut T) -> S,
90-
Handler: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop),
98+
Handler: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop),
99+
AboutToWaitHandler: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
91100
{
92101
/// Create a new application.
93-
pub(crate) fn new(init: Init, init_surface: InitSurface, event: Handler) -> Self {
102+
pub(crate) fn new(
103+
init: Init,
104+
init_surface: InitSurface,
105+
event: Handler,
106+
about_to_wait: AboutToWaitHandler,
107+
) -> Self {
94108
Self {
95109
init,
96110
init_surface,
97111
event,
112+
about_to_wait,
98113
state: None,
99114
surface_state: None,
100115
}
101116
}
117+
118+
/// Build a new application.
119+
#[allow(dead_code)]
120+
pub(crate) fn with_about_to_wait_handler<F>(
121+
self,
122+
about_to_wait: F,
123+
) -> WinitApp<T, S, Init, InitSurface, Handler, F>
124+
where
125+
F: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
126+
{
127+
WinitApp::new(self.init, self.init_surface, self.event, about_to_wait)
128+
}
102129
}
103130

104-
impl<T, S, Init, InitSurface, Handler> ApplicationHandler
105-
for WinitApp<T, S, Init, InitSurface, Handler>
131+
impl<T, S, Init, InitSurface, Handler, AboutToWaitHandler> ApplicationHandler
132+
for WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
106133
where
107134
Init: FnMut(&ActiveEventLoop) -> T,
108135
InitSurface: FnMut(&ActiveEventLoop, &mut T) -> S,
109-
Handler: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop),
136+
Handler: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop),
137+
AboutToWaitHandler: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
110138
{
111139
fn resumed(&mut self, el: &ActiveEventLoop) {
112140
debug_assert!(self.state.is_none());
@@ -129,22 +157,13 @@ where
129157
) {
130158
let state = self.state.as_mut().unwrap();
131159
let surface_state = self.surface_state.as_mut();
132-
(self.event)(
133-
state,
134-
surface_state,
135-
Event::WindowEvent { window_id, event },
136-
event_loop,
137-
);
160+
(self.event)(state, surface_state, window_id, event, event_loop);
138161
}
139162

140163
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
141164
if let Some(state) = self.state.as_mut() {
142-
(self.event)(
143-
state,
144-
self.surface_state.as_mut(),
145-
Event::AboutToWait,
146-
event_loop,
147-
);
165+
let surface_state = self.surface_state.as_mut();
166+
(self.about_to_wait)(state, surface_state, event_loop);
148167
}
149168
}
150169
}

0 commit comments

Comments
 (0)