Skip to content

Commit 58af263

Browse files
committed
Avoid Event enum
1 parent def0d8e commit 58af263

File tree

8 files changed

+112
-151
lines changed

8 files changed

+112
-151
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ 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, _, event, elwt| {
9292
elwt.set_control_flow(ControlFlow::Wait);
9393
9494
match event {
95-
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {
95+
WindowEvent::RedrawRequested => {
9696
let Some(surface) = surface else {
9797
eprintln!("RedrawRequested fired before Resumed or after Suspended");
9898
return;
@@ -121,10 +121,7 @@ fn main() {
121121
122122
buffer.present().unwrap();
123123
}
124-
Event::WindowEvent {
125-
event: WindowEvent::CloseRequested,
126-
window_id,
127-
} if window_id == window.id() => {
124+
WindowEvent::CloseRequested => {
128125
elwt.exit();
129126
}
130127
_ => {}

examples/animation.rs

Lines changed: 14 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,13 @@ 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, _, event, elwt| {
3333
let (window, old_size, frames) = state;
3434

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

3737
match event {
38-
Event::WindowEvent {
39-
window_id,
40-
event: WindowEvent::Resized(size),
41-
} if window_id == window.id() => {
38+
WindowEvent::Resized(size) => {
4239
let Some(surface) = surface else {
4340
eprintln!("Resized fired before Resumed or after Suspended");
4441
return;
@@ -50,10 +47,7 @@ fn main() {
5047
surface.resize(width, height).unwrap();
5148
}
5249
}
53-
Event::WindowEvent {
54-
window_id,
55-
event: WindowEvent::RedrawRequested,
56-
} if window_id == window.id() => {
50+
WindowEvent::RedrawRequested => {
5751
let Some(surface) = surface else {
5852
eprintln!("RedrawRequested fired before Resumed or after Suspended");
5953
return;
@@ -77,26 +71,23 @@ fn main() {
7771
buffer.present().unwrap();
7872
}
7973
}
80-
Event::AboutToWait => {
81-
window.request_redraw();
82-
}
83-
Event::WindowEvent {
74+
WindowEvent::CloseRequested
75+
| WindowEvent::KeyboardInput {
8476
event:
85-
WindowEvent::CloseRequested
86-
| WindowEvent::KeyboardInput {
87-
event:
88-
KeyEvent {
89-
logical_key: Key::Named(NamedKey::Escape),
90-
..
91-
},
77+
KeyEvent {
78+
logical_key: Key::Named(NamedKey::Escape),
9279
..
9380
},
94-
window_id,
95-
} if window_id == window.id() => {
81+
..
82+
} => {
9683
elwt.exit();
9784
}
9885
_ => {}
9986
}
87+
})
88+
.with_about_to_wait_handler(|state, _, _| {
89+
let (window, _, _) = state;
90+
window.request_redraw();
10091
});
10192

10293
winit_app::run_app(event_loop, app);

examples/fruit.rs

Lines changed: 9 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,11 @@ fn main() {
3535
surface
3636
},
3737
)
38-
.with_event_handler(move |window, surface, event, elwt| {
38+
.with_event_handler(move |_window, surface, _, event, elwt| {
3939
elwt.set_control_flow(ControlFlow::Wait);
4040

4141
match event {
42-
Event::WindowEvent {
43-
window_id,
44-
event: WindowEvent::RedrawRequested,
45-
} if window_id == window.id() => {
42+
WindowEvent::RedrawRequested => {
4643
let Some(surface) = surface else {
4744
eprintln!("RedrawRequested fired before Resumed or after Suspended");
4845
return;
@@ -61,19 +58,15 @@ fn main() {
6158

6259
buffer.present().unwrap();
6360
}
64-
Event::WindowEvent {
61+
WindowEvent::CloseRequested
62+
| WindowEvent::KeyboardInput {
6563
event:
66-
WindowEvent::CloseRequested
67-
| WindowEvent::KeyboardInput {
68-
event:
69-
KeyEvent {
70-
logical_key: Key::Named(NamedKey::Escape),
71-
..
72-
},
64+
KeyEvent {
65+
logical_key: Key::Named(NamedKey::Escape),
7366
..
7467
},
75-
window_id,
76-
} if window_id == window.id() => {
68+
..
69+
} => {
7770
elwt.exit();
7871
}
7972
_ => {}

examples/rectangle.rs

Lines changed: 16 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,13 @@ 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, _, event, elwt| {
4242
let (window, flag) = state;
4343

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

4646
match event {
47-
Event::WindowEvent {
48-
window_id,
49-
event: WindowEvent::Resized(size),
50-
} if window_id == window.id() => {
47+
WindowEvent::Resized(size) => {
5148
let Some(surface) = surface else {
5249
eprintln!("Resized fired before Resumed or after Suspended");
5350
return;
@@ -60,10 +57,7 @@ fn main() {
6057
surface.resize(width, height).unwrap();
6158
}
6259
}
63-
Event::WindowEvent {
64-
window_id,
65-
event: WindowEvent::RedrawRequested,
66-
} if window_id == window.id() => {
60+
WindowEvent::RedrawRequested => {
6761
let Some(surface) = surface else {
6862
eprintln!("RedrawRequested fired before Resumed or after Suspended");
6963
return;
@@ -85,35 +79,27 @@ fn main() {
8579
}
8680
}
8781

88-
Event::WindowEvent {
82+
WindowEvent::CloseRequested
83+
| WindowEvent::KeyboardInput {
8984
event:
90-
WindowEvent::CloseRequested
91-
| WindowEvent::KeyboardInput {
92-
event:
93-
KeyEvent {
94-
logical_key: Key::Named(NamedKey::Escape),
95-
..
96-
},
85+
KeyEvent {
86+
logical_key: Key::Named(NamedKey::Escape),
9787
..
9888
},
99-
window_id,
100-
} if window_id == window.id() => {
89+
..
90+
} => {
10191
elwt.exit();
10292
}
10393

104-
Event::WindowEvent {
94+
WindowEvent::KeyboardInput {
10595
event:
106-
WindowEvent::KeyboardInput {
107-
event:
108-
KeyEvent {
109-
state: ElementState::Pressed,
110-
logical_key: Key::Named(NamedKey::Space),
111-
..
112-
},
96+
KeyEvent {
97+
state: ElementState::Pressed,
98+
logical_key: Key::Named(NamedKey::Space),
11399
..
114100
},
115-
window_id,
116-
} if window_id == window.id() => {
101+
..
102+
} => {
117103
// Flip the rectangle flag and request a redraw to show the changed image
118104
*flag = !*flag;
119105
window.request_redraw();

examples/utils/winit_app.rs

Lines changed: 41 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,62 @@ 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+
pub(crate) fn with_event_handler<F>(
82+
self,
83+
handler: F,
84+
) -> WinitApp<T, S, Init, InitSurface, F, impl FnMut(&mut T, Option<&mut S>, &ActiveEventLoop)>
7985
where
80-
F: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop),
86+
F: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop),
8187
{
82-
WinitApp::new(self.init, self.init_surface, handler)
88+
WinitApp::new(self.init, self.init_surface, handler, |_, _, _| {})
8389
}
8490
}
8591

86-
impl<T, S, Init, InitSurface, Handler> WinitApp<T, S, Init, InitSurface, Handler>
92+
impl<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
93+
WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
8794
where
8895
Init: FnMut(&ActiveEventLoop) -> T,
8996
InitSurface: FnMut(&ActiveEventLoop, &mut T) -> S,
90-
Handler: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop),
97+
Handler: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop),
98+
AboutToWaitHandler: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
9199
{
92100
/// Create a new application.
93-
pub(crate) fn new(init: Init, init_surface: InitSurface, event: Handler) -> Self {
101+
pub(crate) fn new(
102+
init: Init,
103+
init_surface: InitSurface,
104+
event: Handler,
105+
about_to_wait: AboutToWaitHandler,
106+
) -> Self {
94107
Self {
95108
init,
96109
init_surface,
97110
event,
111+
about_to_wait,
98112
state: None,
99113
surface_state: None,
100114
}
101115
}
116+
117+
/// Build a new application.
118+
#[allow(dead_code)]
119+
pub(crate) fn with_about_to_wait_handler<F>(
120+
self,
121+
about_to_wait: F,
122+
) -> WinitApp<T, S, Init, InitSurface, Handler, F>
123+
where
124+
F: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
125+
{
126+
WinitApp::new(self.init, self.init_surface, self.event, about_to_wait)
127+
}
102128
}
103129

104-
impl<T, S, Init, InitSurface, Handler> ApplicationHandler
105-
for WinitApp<T, S, Init, InitSurface, Handler>
130+
impl<T, S, Init, InitSurface, Handler, AboutToWaitHandler> ApplicationHandler
131+
for WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
106132
where
107133
Init: FnMut(&ActiveEventLoop) -> T,
108134
InitSurface: FnMut(&ActiveEventLoop, &mut T) -> S,
109-
Handler: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop),
135+
Handler: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop),
136+
AboutToWaitHandler: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
110137
{
111138
fn resumed(&mut self, el: &ActiveEventLoop) {
112139
debug_assert!(self.state.is_none());
@@ -129,22 +156,13 @@ where
129156
) {
130157
let state = self.state.as_mut().unwrap();
131158
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-
);
159+
(self.event)(state, surface_state, window_id, event, event_loop);
138160
}
139161

140162
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
141163
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-
);
164+
let surface_state = self.surface_state.as_mut();
165+
(self.about_to_wait)(state, surface_state, event_loop);
148166
}
149167
}
150168
}

0 commit comments

Comments
 (0)