-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Make
utils/winit_app.rs
a path
-referenced module again …
…to fix autoformatting Via `include!()` `rustfmt` doesn't know that this file belongs to the current project and doesn't autoformat it. Furthermore, jump-to-symbol likely doesn't play that well with `include!()`. Without creating a `lib.rs` file in `utils/`, use `#[path]` to reference the existing file without following a `mod`ule hierarchy. Note that the file remains in the `utils/` folder as that doesn't get picked up by `autoexamples`.
- Loading branch information
Showing
8 changed files
with
117 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,121 @@ | ||
/// Common boilerplate for setting up a winit application. | ||
mod winit_app { | ||
use std::marker::PhantomData; | ||
use std::rc::Rc; | ||
|
||
use winit::application::ApplicationHandler; | ||
use winit::event::{Event, WindowEvent}; | ||
use winit::event_loop::{EventLoop, ActiveEventLoop}; | ||
use winit::window::{WindowAttributes, WindowId, Window}; | ||
|
||
/// Run a Winit application. | ||
#[allow(unused_mut)] | ||
pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) { | ||
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] | ||
event_loop.run_app(&mut app).unwrap(); | ||
|
||
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] | ||
winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app); | ||
} | ||
|
||
/// Create a window from a set of window attributes. | ||
#[allow(dead_code)] | ||
pub(crate) fn make_window(elwt: &ActiveEventLoop, f: impl FnOnce(WindowAttributes) -> WindowAttributes) -> Rc<Window> { | ||
let attributes = f(WindowAttributes::default()); | ||
#[cfg(target_arch = "wasm32")] | ||
let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append( | ||
attributes, | ||
true | ||
); | ||
let window = elwt.create_window(attributes); | ||
Rc::new(window.unwrap()) | ||
} | ||
use std::marker::PhantomData; | ||
use std::rc::Rc; | ||
|
||
use winit::application::ApplicationHandler; | ||
use winit::event::{Event, WindowEvent}; | ||
use winit::event_loop::{ActiveEventLoop, EventLoop}; | ||
use winit::window::{Window, WindowAttributes, WindowId}; | ||
|
||
/// Run a Winit application. | ||
#[allow(unused_mut)] | ||
pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) { | ||
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] | ||
event_loop.run_app(&mut app).unwrap(); | ||
|
||
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] | ||
winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app); | ||
} | ||
|
||
/// Easily constructable winit application. | ||
pub(crate) struct WinitApp<T, Init, Handler> { | ||
/// Closure to initialize state. | ||
init: Init, | ||
/// Create a window from a set of window attributes. | ||
#[allow(dead_code)] | ||
pub(crate) fn make_window( | ||
elwt: &ActiveEventLoop, | ||
f: impl FnOnce(WindowAttributes) -> WindowAttributes, | ||
) -> Rc<Window> { | ||
let attributes = f(WindowAttributes::default()); | ||
#[cfg(target_arch = "wasm32")] | ||
let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append(attributes, true); | ||
let window = elwt.create_window(attributes); | ||
Rc::new(window.unwrap()) | ||
} | ||
|
||
/// Closure to run on window events. | ||
event: Handler, | ||
/// Easily constructable winit application. | ||
pub(crate) struct WinitApp<T, Init, Handler> { | ||
/// Closure to initialize state. | ||
init: Init, | ||
|
||
/// Contained state. | ||
state: Option<T> | ||
} | ||
/// Closure to run on window events. | ||
event: Handler, | ||
|
||
/// Builder that makes it so we don't have to name `T`. | ||
pub(crate) struct WinitAppBuilder<T, Init> { | ||
/// Closure to initialize state. | ||
init: Init, | ||
/// Contained state. | ||
state: Option<T>, | ||
} | ||
|
||
/// Eat the type parameter. | ||
_marker: PhantomData<Option<T>>, | ||
} | ||
/// Builder that makes it so we don't have to name `T`. | ||
pub(crate) struct WinitAppBuilder<T, Init> { | ||
/// Closure to initialize state. | ||
init: Init, | ||
|
||
impl<T, Init> WinitAppBuilder<T, Init> | ||
where Init: FnMut(&ActiveEventLoop) -> T, | ||
{ | ||
/// Create with an "init" closure. | ||
pub(crate) fn with_init(init: Init) -> Self { | ||
Self { | ||
init, | ||
_marker: PhantomData | ||
} | ||
} | ||
/// Eat the type parameter. | ||
_marker: PhantomData<Option<T>>, | ||
} | ||
|
||
/// Build a new application. | ||
pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, Init, F> | ||
where F: FnMut(&mut T, Event<()>, &ActiveEventLoop) | ||
{ | ||
WinitApp::new(self.init, handler) | ||
impl<T, Init> WinitAppBuilder<T, Init> | ||
where | ||
Init: FnMut(&ActiveEventLoop) -> T, | ||
{ | ||
/// Create with an "init" closure. | ||
pub(crate) fn with_init(init: Init) -> Self { | ||
Self { | ||
init, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
impl<T, Init, Handler> WinitApp<T, Init, Handler> | ||
where Init: FnMut(&ActiveEventLoop) -> T, | ||
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) | ||
/// Build a new application. | ||
pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, Init, F> | ||
where | ||
F: FnMut(&mut T, Event<()>, &ActiveEventLoop), | ||
{ | ||
/// Create a new application. | ||
pub(crate) fn new(init: Init, event: Handler) -> Self { | ||
Self { | ||
init, | ||
event, | ||
state: None | ||
} | ||
} | ||
WinitApp::new(self.init, handler) | ||
} | ||
} | ||
|
||
impl<T, Init, Handler> ApplicationHandler for WinitApp<T, Init, Handler> | ||
where Init: FnMut(&ActiveEventLoop) -> T, | ||
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) { | ||
|
||
fn resumed(&mut self, el: &ActiveEventLoop) { | ||
debug_assert!(self.state.is_none()); | ||
self.state = Some((self.init)(el)); | ||
impl<T, Init, Handler> WinitApp<T, Init, Handler> | ||
where | ||
Init: FnMut(&ActiveEventLoop) -> T, | ||
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop), | ||
{ | ||
/// Create a new application. | ||
pub(crate) fn new(init: Init, event: Handler) -> Self { | ||
Self { | ||
init, | ||
event, | ||
state: None, | ||
} | ||
} | ||
} | ||
|
||
fn suspended(&mut self, _event_loop: &ActiveEventLoop) { | ||
let state = self.state.take(); | ||
debug_assert!(state.is_some()); | ||
drop(state); | ||
} | ||
impl<T, Init, Handler> ApplicationHandler for WinitApp<T, Init, Handler> | ||
where | ||
Init: FnMut(&ActiveEventLoop) -> T, | ||
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop), | ||
{ | ||
fn resumed(&mut self, el: &ActiveEventLoop) { | ||
debug_assert!(self.state.is_none()); | ||
self.state = Some((self.init)(el)); | ||
} | ||
|
||
fn window_event( | ||
&mut self, | ||
event_loop: &ActiveEventLoop, | ||
window_id: WindowId, | ||
event: WindowEvent, | ||
) { | ||
let state = self.state.as_mut().unwrap(); | ||
(self.event)(state, Event::WindowEvent { | ||
window_id, | ||
event | ||
}, event_loop); | ||
} | ||
fn suspended(&mut self, _event_loop: &ActiveEventLoop) { | ||
let state = self.state.take(); | ||
debug_assert!(state.is_some()); | ||
drop(state); | ||
} | ||
|
||
fn window_event( | ||
&mut self, | ||
event_loop: &ActiveEventLoop, | ||
window_id: WindowId, | ||
event: WindowEvent, | ||
) { | ||
let state = self.state.as_mut().unwrap(); | ||
(self.event)(state, Event::WindowEvent { 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, Event::AboutToWait, event_loop); | ||
} | ||
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { | ||
if let Some(state) = self.state.as_mut() { | ||
(self.event)(state, Event::AboutToWait, event_loop); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters