Add methods to set ControlFlow operation

This allows downstream users to avoid importing `ControlFlow` from winit.
This commit is contained in:
Steve Wooster 2022-04-09 18:32:02 -07:00 committed by GitHub
parent c57294b41a
commit d624a3e648
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 120 additions and 86 deletions

View file

@ -169,6 +169,41 @@ impl ControlFlow {
/// [`ExitWithCode`]: ControlFlow::ExitWithCode
#[allow(non_upper_case_globals)]
pub const Exit: Self = Self::ExitWithCode(0);
/// Sets this to [`Poll`].
///
/// [`Poll`]: ControlFlow::Poll
pub fn set_poll(&mut self) {
*self = Self::Poll;
}
/// Sets this to [`Wait`].
///
/// [`Wait`]: ControlFlow::Wait
pub fn set_wait(&mut self) {
*self = Self::Wait;
}
/// Sets this to [`WaitUntil`]`(instant)`.
///
/// [`WaitUntil`]: ControlFlow::WaitUntil
pub fn set_wait_until(&mut self, instant: Instant) {
*self = Self::WaitUntil(instant);
}
/// Sets this to [`ExitWithCode`]`(code)`.
///
/// [`ExitWithCode`]: ControlFlow::ExitWithCode
pub fn set_exit_with_code(&mut self, code: i32) {
*self = Self::ExitWithCode(code);
}
/// Sets this to [`Exit`].
///
/// [`Exit`]: ControlFlow::Exit
pub fn set_exit(&mut self) {
*self = Self::Exit;
}
}
impl Default for ControlFlow {

View file

@ -44,7 +44,7 @@
//! ```no_run
//! use winit::{
//! event::{Event, WindowEvent},
//! event_loop::{ControlFlow, EventLoop},
//! event_loop::EventLoop,
//! window::WindowBuilder,
//! };
//!
@ -54,12 +54,12 @@
//! event_loop.run(move |event, _, control_flow| {
//! // ControlFlow::Poll continuously runs the event loop, even if the OS hasn't
//! // dispatched any events. This is ideal for games and similar applications.
//! *control_flow = ControlFlow::Poll;
//! control_flow.set_poll();
//!
//! // ControlFlow::Wait pauses the event loop if no events are available to process.
//! // This is ideal for non-game applications that only update in response to user
//! // input, and uses significantly less power/CPU time than ControlFlow::Poll.
//! *control_flow = ControlFlow::Wait;
//! control_flow.set_wait();
//!
//! match event {
//! Event::WindowEvent {
@ -67,7 +67,7 @@
//! ..
//! } => {
//! println!("The close button was pressed; stopping");
//! *control_flow = ControlFlow::Exit
//! control_flow.set_exit();
//! },
//! Event::MainEventsCleared => {
//! // Application update code.

View file

@ -18,7 +18,7 @@ pub use crate::icon::{BadIcon, Icon};
/// ```no_run
/// use winit::{
/// event::{Event, WindowEvent},
/// event_loop::{ControlFlow, EventLoop},
/// event_loop::EventLoop,
/// window::Window,
/// };
///
@ -26,13 +26,13 @@ pub use crate::icon::{BadIcon, Icon};
/// let window = Window::new(&event_loop).unwrap();
///
/// event_loop.run(move |event, _, control_flow| {
/// *control_flow = ControlFlow::Wait;
/// control_flow.set_wait();
///
/// match event {
/// Event::WindowEvent {
/// event: WindowEvent::CloseRequested,
/// ..
/// } => *control_flow = ControlFlow::Exit,
/// } => control_flow.set_exit(),
/// _ => (),
/// }
/// });