Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ fn main() {
},
|_elwt, window| softbuffer::Surface::new(&context, window.clone()).unwrap(),
)
.with_event_handler(|window, surface, event, elwt| {
.with_event_handler(|window, surface, window_id, event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if window_id != window.id() {
return;
}

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;
Expand Down Expand Up @@ -121,10 +125,7 @@ fn main() {

buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
WindowEvent::CloseRequested => {
elwt.exit();
}
_ => {}
Expand Down
41 changes: 18 additions & 23 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

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

elwt.set_control_flow(ControlFlow::Poll);

if window_id != window.id() {
return;
}

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;
Expand All @@ -50,10 +51,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;
Expand All @@ -77,26 +75,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);
Expand Down
29 changes: 13 additions & 16 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -35,14 +35,15 @@ fn main() {
surface
},
)
.with_event_handler(move |window, surface, event, elwt| {
.with_event_handler(move |window, surface, window_id, event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if window_id != window.id() {
return;
}

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;
Expand All @@ -61,19 +62,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();
}
_ => {}
Expand Down
50 changes: 20 additions & 30 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -38,16 +38,17 @@ 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, window_id, event, elwt| {
let (window, flag) = state;

elwt.set_control_flow(ControlFlow::Wait);

if window_id != window.id() {
return;
}

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;
Expand All @@ -60,10 +61,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;
Expand All @@ -85,35 +83,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();
Expand Down
65 changes: 42 additions & 23 deletions examples/utils/winit_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -31,7 +31,7 @@ pub(crate) fn make_window(
}

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

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

/// Closure to run on about_to_wait events.
about_to_wait: AboutToWaitHandler,

/// Contained state.
state: Option<T>,

Expand Down Expand Up @@ -75,38 +78,63 @@ where
}

/// Build a new application.
pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, S, Init, InitSurface, F>
#[allow(clippy::type_complexity)]
pub(crate) fn with_event_handler<F>(
self,
handler: F,
) -> WinitApp<T, S, Init, InitSurface, F, impl FnMut(&mut T, Option<&mut S>, &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<T, S, Init, InitSurface, Handler> WinitApp<T, S, Init, InitSurface, Handler>
impl<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
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<F>(
self,
about_to_wait: F,
) -> WinitApp<T, S, Init, InitSurface, Handler, F>
where
F: FnMut(&mut T, Option<&mut S>, &ActiveEventLoop),
{
WinitApp::new(self.init, self.init_surface, self.event, about_to_wait)
}
}

impl<T, S, Init, InitSurface, Handler> ApplicationHandler
for WinitApp<T, S, Init, InitSurface, Handler>
impl<T, S, Init, InitSurface, Handler, AboutToWaitHandler> ApplicationHandler
for WinitApp<T, S, Init, InitSurface, Handler, AboutToWaitHandler>
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());
Expand All @@ -129,22 +157,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);
}
}
}
Loading