diff --git a/README.md b/README.md index 2966266..a82bf8d 100644 --- a/README.md +++ b/README.md @@ -88,11 +88,11 @@ fn main() { }, |_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(), ) - .with_event_handler(|window, surface, event, elwt| { + .with_event_handler(|window, surface, _, event, elwt| { elwt.set_control_flow(ControlFlow::Wait); match event { - Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -121,10 +121,7 @@ fn main() { buffer.present().unwrap(); } - Event::WindowEvent { - event: WindowEvent::CloseRequested, - window_id, - } if window_id == window.id() => { + WindowEvent::CloseRequested => { elwt.exit(); } _ => {} diff --git a/examples/animation.rs b/examples/animation.rs index 714a7b2..21dde7c 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -3,7 +3,7 @@ use rayon::prelude::*; use std::f64::consts::PI; use std::num::NonZeroU32; use web_time::Instant; -use winit::event::{Event, KeyEvent, WindowEvent}; +use winit::event::{KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; @@ -29,16 +29,13 @@ fn main() { softbuffer::Surface::new(&context, window.clone()).unwrap() }, ) - .with_event_handler(move |state, surface, event, elwt| { + .with_event_handler(move |state, surface, _, event, elwt| { let (window, old_size, frames) = state; elwt.set_control_flow(ControlFlow::Poll); match event { - Event::WindowEvent { - window_id, - event: WindowEvent::Resized(size), - } if window_id == window.id() => { + WindowEvent::Resized(size) => { let Some(surface) = surface else { eprintln!("Resized fired before Resumed or after Suspended"); return; @@ -50,10 +47,7 @@ fn main() { surface.resize(width, height).unwrap(); } } - Event::WindowEvent { - window_id, - event: WindowEvent::RedrawRequested, - } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -77,26 +71,23 @@ fn main() { buffer.present().unwrap(); } } - Event::AboutToWait => { - window.request_redraw(); - } - Event::WindowEvent { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - .. - }, + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { elwt.exit(); } _ => {} } + }) + .with_about_to_wait_handler(|state, _, _| { + let (window, _, _) = state; + window.request_redraw(); }); winit_app::run_app(event_loop, app); diff --git a/examples/fruit.rs b/examples/fruit.rs index b325dd5..fa27ee6 100644 --- a/examples/fruit.rs +++ b/examples/fruit.rs @@ -1,6 +1,6 @@ use image::GenericImageView; use std::num::NonZeroU32; -use winit::event::{Event, KeyEvent, WindowEvent}; +use winit::event::{KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; @@ -35,14 +35,11 @@ fn main() { surface }, ) - .with_event_handler(move |window, surface, event, elwt| { + .with_event_handler(move |_window, surface, _, event, elwt| { elwt.set_control_flow(ControlFlow::Wait); match event { - Event::WindowEvent { - window_id, - event: WindowEvent::RedrawRequested, - } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -61,19 +58,15 @@ fn main() { buffer.present().unwrap(); } - Event::WindowEvent { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - .. - }, + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { elwt.exit(); } _ => {} diff --git a/examples/rectangle.rs b/examples/rectangle.rs index 70df087..8647d9e 100644 --- a/examples/rectangle.rs +++ b/examples/rectangle.rs @@ -1,5 +1,5 @@ use std::num::NonZeroU32; -use winit::event::{ElementState, Event, KeyEvent, WindowEvent}; +use winit::event::{ElementState, KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; @@ -38,16 +38,13 @@ fn main() { }, move |_elwt, (window, _flag)| softbuffer::Surface::new(&context, window.clone()).unwrap(), ) - .with_event_handler(|state, surface, event, elwt| { + .with_event_handler(|state, surface, _, event, elwt| { let (window, flag) = state; elwt.set_control_flow(ControlFlow::Wait); match event { - Event::WindowEvent { - window_id, - event: WindowEvent::Resized(size), - } if window_id == window.id() => { + WindowEvent::Resized(size) => { let Some(surface) = surface else { eprintln!("Resized fired before Resumed or after Suspended"); return; @@ -60,10 +57,7 @@ fn main() { surface.resize(width, height).unwrap(); } } - Event::WindowEvent { - window_id, - event: WindowEvent::RedrawRequested, - } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -85,35 +79,27 @@ fn main() { } } - Event::WindowEvent { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - .. - }, + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { elwt.exit(); } - Event::WindowEvent { + WindowEvent::KeyboardInput { event: - WindowEvent::KeyboardInput { - event: - KeyEvent { - state: ElementState::Pressed, - logical_key: Key::Named(NamedKey::Space), - .. - }, + KeyEvent { + state: ElementState::Pressed, + logical_key: Key::Named(NamedKey::Space), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { // Flip the rectangle flag and request a redraw to show the changed image *flag = !*flag; window.request_redraw(); diff --git a/examples/utils/winit_app.rs b/examples/utils/winit_app.rs index 3b4cb4d..bfd09de 100644 --- a/examples/utils/winit_app.rs +++ b/examples/utils/winit_app.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use std::rc::Rc; use winit::application::ApplicationHandler; -use winit::event::{Event, WindowEvent}; +use winit::event::WindowEvent; use winit::event_loop::{ActiveEventLoop, EventLoop}; use winit::window::{Window, WindowAttributes, WindowId}; @@ -31,7 +31,7 @@ pub(crate) fn make_window( } /// Easily constructable winit application. -pub(crate) struct WinitApp { +pub(crate) struct WinitApp { /// Closure to initialize `state`. init: Init, @@ -41,6 +41,9 @@ pub(crate) struct WinitApp { /// Closure to run on window events. event: Handler, + /// Closure to run on about_to_wait events. + about_to_wait: AboutToWaitHandler, + /// Contained state. state: Option, @@ -75,38 +78,62 @@ where } /// Build a new application. - pub(crate) fn with_event_handler(self, handler: F) -> WinitApp + pub(crate) fn with_event_handler( + self, + handler: F, + ) -> WinitApp, &ActiveEventLoop)> where - F: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop), + F: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop), { - WinitApp::new(self.init, self.init_surface, handler) + WinitApp::new(self.init, self.init_surface, handler, |_, _, _| {}) } } -impl WinitApp +impl + WinitApp where Init: FnMut(&ActiveEventLoop) -> T, InitSurface: FnMut(&ActiveEventLoop, &mut T) -> S, - Handler: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop), + Handler: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop), + AboutToWaitHandler: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop), { /// Create a new application. - pub(crate) fn new(init: Init, init_surface: InitSurface, event: Handler) -> Self { + pub(crate) fn new( + init: Init, + init_surface: InitSurface, + event: Handler, + about_to_wait: AboutToWaitHandler, + ) -> Self { Self { init, init_surface, event, + about_to_wait, state: None, surface_state: None, } } + + /// Build a new application. + #[allow(dead_code)] + pub(crate) fn with_about_to_wait_handler( + self, + about_to_wait: F, + ) -> WinitApp + where + F: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop), + { + WinitApp::new(self.init, self.init_surface, self.event, about_to_wait) + } } -impl ApplicationHandler - for WinitApp +impl ApplicationHandler + for WinitApp where Init: FnMut(&ActiveEventLoop) -> T, InitSurface: FnMut(&ActiveEventLoop, &mut T) -> S, - Handler: FnMut(&mut T, Option<&mut S>, Event<()>, &ActiveEventLoop), + Handler: FnMut(&mut T, Option<&mut S>, WindowId, WindowEvent, &ActiveEventLoop), + AboutToWaitHandler: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop), { fn resumed(&mut self, el: &ActiveEventLoop) { debug_assert!(self.state.is_none()); @@ -129,22 +156,13 @@ where ) { let state = self.state.as_mut().unwrap(); let surface_state = self.surface_state.as_mut(); - (self.event)( - state, - surface_state, - Event::WindowEvent { window_id, event }, - event_loop, - ); + (self.event)(state, surface_state, window_id, event, event_loop); } fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { if let Some(state) = self.state.as_mut() { - (self.event)( - state, - self.surface_state.as_mut(), - Event::AboutToWait, - event_loop, - ); + let surface_state = self.surface_state.as_mut(); + (self.about_to_wait)(state, surface_state, event_loop); } } } diff --git a/examples/winit.rs b/examples/winit.rs index 6ecc4bb..d46722b 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -1,5 +1,5 @@ use std::num::NonZeroU32; -use winit::event::{Event, KeyEvent, WindowEvent}; +use winit::event::{KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; @@ -18,14 +18,11 @@ pub(crate) fn entry(event_loop: EventLoop<()>) { |elwt| winit_app::make_window(elwt, |w| w), move |_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(), ) - .with_event_handler(|window, surface, event, elwt| { + .with_event_handler(|window, surface, _, event, elwt| { elwt.set_control_flow(ControlFlow::Wait); match event { - Event::WindowEvent { - window_id, - event: WindowEvent::Resized(size), - } if window_id == window.id() => { + WindowEvent::Resized(size) => { let Some(surface) = surface else { eprintln!("Resized fired before Resumed or after Suspended"); return; @@ -37,10 +34,7 @@ pub(crate) fn entry(event_loop: EventLoop<()>) { surface.resize(width, height).unwrap(); } } - Event::WindowEvent { - window_id, - event: WindowEvent::RedrawRequested, - } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -63,19 +57,15 @@ pub(crate) fn entry(event_loop: EventLoop<()>) { buffer.present().unwrap(); } } - Event::WindowEvent { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - .. - }, + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { elwt.exit(); } _ => {} diff --git a/examples/winit_multithread.rs b/examples/winit_multithread.rs index b58f161..0bed7cc 100644 --- a/examples/winit_multithread.rs +++ b/examples/winit_multithread.rs @@ -8,7 +8,7 @@ mod winit_app; pub mod ex { use std::num::NonZeroU32; use std::sync::{mpsc, Arc, Mutex}; - use winit::event::{Event, KeyEvent, WindowEvent}; + use winit::event::{KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop, OwnedDisplayHandle}; use winit::keyboard::{Key, NamedKey}; use winit::window::Window; @@ -91,15 +91,12 @@ pub mod ex { )) }, ) - .with_event_handler(|state, surface, event, elwt| { - let (window, start_render, finish_render) = state; + .with_event_handler(|state, surface, _, event, elwt| { + let (_window, start_render, finish_render) = state; elwt.set_control_flow(ControlFlow::Wait); match event { - Event::WindowEvent { - window_id, - event: WindowEvent::RedrawRequested, - } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -108,19 +105,15 @@ pub mod ex { start_render.send(surface.clone()).unwrap(); finish_render.recv().unwrap(); } - Event::WindowEvent { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - .. - }, + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { elwt.exit(); } _ => {} diff --git a/examples/winit_wrong_sized_buffer.rs b/examples/winit_wrong_sized_buffer.rs index b135680..555a9b5 100644 --- a/examples/winit_wrong_sized_buffer.rs +++ b/examples/winit_wrong_sized_buffer.rs @@ -1,5 +1,5 @@ use std::num::NonZeroU32; -use winit::event::{Event, KeyEvent, WindowEvent}; +use winit::event::{KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; @@ -27,14 +27,11 @@ fn main() { surface }, ) - .with_event_handler(|window, surface, event, elwt| { + .with_event_handler(|_window, surface, _, event, elwt| { elwt.set_control_flow(ControlFlow::Wait); match event { - Event::WindowEvent { - window_id, - event: WindowEvent::RedrawRequested, - } if window_id == window.id() => { + WindowEvent::RedrawRequested => { let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; @@ -53,19 +50,15 @@ fn main() { } buffer.present().unwrap(); } - Event::WindowEvent { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { event: - WindowEvent::CloseRequested - | WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - .. - }, + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), .. }, - window_id, - } if window_id == window.id() => { + .. + } => { elwt.exit(); } _ => {}