forked from rust-windowing/winit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add platform::desktop module with EventLoopExt::run_return
- Loading branch information
Showing
4 changed files
with
114 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
extern crate winit; | ||
use winit::platform::desktop::EventLoopExtDesktop; | ||
|
||
fn main() { | ||
let mut events_loop = winit::EventLoop::new(); | ||
|
||
let window = winit::WindowBuilder::new() | ||
.with_title("A fantastic window!") | ||
.build(&events_loop) | ||
.unwrap(); | ||
|
||
println!("Close the window to continue."); | ||
events_loop.run_return(|event, _, control_flow| { | ||
match event { | ||
winit::Event::WindowEvent { | ||
event: winit::WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = winit::ControlFlow::Exit, | ||
_ => *control_flow = winit::ControlFlow::Wait, | ||
} | ||
}); | ||
drop(window); | ||
|
||
let _window_2 = winit::WindowBuilder::new() | ||
.with_title("A second, fantasticer window!") | ||
.build(&events_loop) | ||
.unwrap(); | ||
|
||
println!("Wa ha ha! You thought that closing the window would finish this?!"); | ||
events_loop.run_return(|event, _, control_flow| { | ||
match event { | ||
winit::Event::WindowEvent { | ||
event: winit::WindowEvent::CloseRequested, | ||
.. | ||
} => *control_flow = winit::ControlFlow::Exit, | ||
_ => *control_flow = winit::ControlFlow::Wait, | ||
} | ||
}); | ||
|
||
println!("Okay we're done now for real."); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![cfg(any( | ||
target_os = "windows", | ||
target_os = "macos", | ||
target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd" | ||
))] | ||
|
||
use {EventLoop, Event, ControlFlow}; | ||
|
||
/// Additional methods on `EventLoop` that are specific to desktop platforms. | ||
pub trait EventLoopExtDesktop { | ||
type UserEvent; | ||
/// Initializes the `winit` event loop. | ||
/// | ||
/// Unlikes `run`, this function *does* return control flow to the caller when `control_flow` | ||
/// is set to `ControlFlow::Exit`. | ||
fn run_return<F>(&mut self, event_handler: F) | ||
where F: FnMut(Event<Self::UserEvent>, &EventLoop<Self::UserEvent>, &mut ControlFlow); | ||
} | ||
|
||
impl<T> EventLoopExtDesktop for EventLoop<T> { | ||
type UserEvent = T; | ||
|
||
fn run_return<F>(&mut self, event_handler: F) | ||
where F: FnMut(Event<T>, &EventLoop<T>, &mut ControlFlow) | ||
{ | ||
self.events_loop.run_return(event_handler) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,3 +15,5 @@ pub mod ios; | |
pub mod macos; | ||
pub mod unix; | ||
pub mod windows; | ||
|
||
pub mod desktop; |
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