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

@ -32,6 +32,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Wayland, fix polling during consecutive `EventLoop::run_return` invocations. - On Wayland, fix polling during consecutive `EventLoop::run_return` invocations.
- On Windows, fix race issue creating fullscreen windows with `WindowBuilder::with_fullscreen` - On Windows, fix race issue creating fullscreen windows with `WindowBuilder::with_fullscreen`
- On Android, `virtual_keycode` for `KeyboardInput` events is now filled in where a suitable match is found. - On Android, `virtual_keycode` for `KeyboardInput` events is now filled in where a suitable match is found.
- Added helper methods on `ControlFlow` to set its value.
# 0.26.1 (2022-01-05) # 0.26.1 (2022-01-05)

View file

@ -3,7 +3,7 @@ use std::{thread, time};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, KeyboardInput, WindowEvent}, event::{Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -88,23 +88,21 @@ fn main() {
window.request_redraw(); window.request_redraw();
} }
if close_requested { if close_requested {
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
} }
Event::RedrawRequested(_window_id) => {} Event::RedrawRequested(_window_id) => {}
Event::RedrawEventsCleared => { Event::RedrawEventsCleared => {
*control_flow = match mode { match mode {
Mode::Wait => ControlFlow::Wait, Mode::Wait => control_flow.set_wait(),
Mode::WaitUntil => { Mode::WaitUntil => {
if wait_cancelled { if !wait_cancelled {
*control_flow control_flow.set_wait_until(instant::Instant::now() + WAIT_TIME);
} else {
ControlFlow::WaitUntil(instant::Instant::now() + WAIT_TIME)
} }
} }
Mode::Poll => { Mode::Poll => {
thread::sleep(POLL_SLEEP_TIME); thread::sleep(POLL_SLEEP_TIME);
ControlFlow::Poll control_flow.set_poll();
} }
}; };
} }

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent}, event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::{CursorIcon, WindowBuilder}, window::{CursorIcon, WindowBuilder},
}; };
@ -15,7 +15,7 @@ fn main() {
let mut cursor_idx = 0; let mut cursor_idx = 0;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { Event::WindowEvent {
@ -42,7 +42,7 @@ fn main() {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => { } => {
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
_ => (), _ => (),
} }

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent}, event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -17,11 +17,11 @@ fn main() {
let mut modifiers = ModifiersState::default(); let mut modifiers = ModifiersState::default();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: input:
KeyboardInput { KeyboardInput {
@ -33,7 +33,7 @@ fn main() {
} => { } => {
use winit::event::VirtualKeyCode::*; use winit::event::VirtualKeyCode::*;
match key { match key {
Escape => *control_flow = ControlFlow::Exit, Escape => control_flow.set_exit(),
G => window.set_cursor_grab(!modifiers.shift()).unwrap(), G => window.set_cursor_grab(!modifiers.shift()).unwrap(),
H => window.set_cursor_visible(modifiers.shift()), H => window.set_cursor_visible(modifiers.shift()),
_ => (), _ => (),

View file

@ -3,7 +3,7 @@ fn main() {
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoopBuilder}, event_loop::EventLoopBuilder,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -34,14 +34,14 @@ fn main() {
}); });
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::UserEvent(event) => println!("user event: {:?}", event), Event::UserEvent(event) => println!("user event: {:?}", event),
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => control_flow.set_exit(),
_ => (), _ => (),
} }
}); });

View file

@ -3,7 +3,7 @@ use winit::{
event::{ event::{
ElementState, Event, KeyboardInput, MouseButton, StartCause, VirtualKeyCode, WindowEvent, ElementState, Event, KeyboardInput, MouseButton, StartCause, VirtualKeyCode, WindowEvent,
}, },
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::{Window, WindowBuilder, WindowId}, window::{Window, WindowBuilder, WindowId},
}; };
@ -22,7 +22,7 @@ fn main() {
eprintln!("Switch which window is to be dragged by pressing \"x\".") eprintln!("Switch which window is to be dragged by pressing \"x\".")
} }
Event::WindowEvent { event, window_id } => match event { Event::WindowEvent { event, window_id } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::MouseInput { WindowEvent::MouseInput {
state: ElementState::Pressed, state: ElementState::Pressed,
button: MouseButton::Left, button: MouseButton::Left,

View file

@ -2,7 +2,7 @@ use std::io::{stdin, stdout, Write};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}; use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::EventLoop;
use winit::monitor::{MonitorHandle, VideoMode}; use winit::monitor::{MonitorHandle, VideoMode};
use winit::window::{Fullscreen, WindowBuilder}; use winit::window::{Fullscreen, WindowBuilder};
@ -32,11 +32,11 @@ fn main() {
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: input:
KeyboardInput { KeyboardInput {
@ -46,7 +46,7 @@ fn main() {
}, },
.. ..
} => match (virtual_code, state) { } => match (virtual_code, state) {
(VirtualKeyCode::Escape, _) => *control_flow = ControlFlow::Exit, (VirtualKeyCode::Escape, _) => control_flow.set_exit(),
(VirtualKeyCode::F, ElementState::Pressed) => { (VirtualKeyCode::F, ElementState::Pressed) => {
if window.fullscreen().is_some() { if window.fullscreen().is_some() {
window.set_fullscreen(None); window.set_fullscreen(None);

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, KeyboardInput, WindowEvent}, event::{Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -21,7 +21,7 @@ fn main() {
ElementState::Released, ElementState::Released,
VirtualKeyCode::{N, Y}, VirtualKeyCode::{N, Y},
}; };
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => { Event::WindowEvent { event, .. } => {
@ -63,7 +63,7 @@ fn main() {
// event loop (i.e. if it's a multi-window application), you need to // event loop (i.e. if it's a multi-window application), you need to
// drop the window. That closes it, and results in `Destroyed` being // drop the window. That closes it, and results in `Destroyed` being
// sent. // sent.
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
} }
N => { N => {

View file

@ -2,7 +2,7 @@ use simple_logger::SimpleLogger;
use winit::{ use winit::{
dpi::LogicalSize, dpi::LogicalSize,
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -16,14 +16,14 @@ fn main() {
window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0))); window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0)));
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
println!("{:?}", event); println!("{:?}", event);
match event { match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => control_flow.set_exit(),
_ => (), _ => (),
} }
}); });

View file

@ -2,7 +2,7 @@ extern crate winit;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::event::{Event, VirtualKeyCode, WindowEvent}; use winit::event::{Event, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::EventLoop;
use winit::window::WindowBuilder; use winit::window::WindowBuilder;
fn main() { fn main() {
@ -15,13 +15,13 @@ fn main() {
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => control_flow.set_exit(),
// Keyboard input event to handle minimize via a hotkey // Keyboard input event to handle minimize via a hotkey
Event::WindowEvent { Event::WindowEvent {

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -30,11 +30,11 @@ In other words, the deltas indicate the direction in which to move the content (
); );
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::MouseWheel { delta, .. } => match delta { WindowEvent::MouseWheel { delta, .. } => match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => { winit::event::MouseScrollDelta::LineDelta(x, y) => {
println!("mouse wheel Line Delta: ({},{})", x, y); println!("mouse wheel Line Delta: ({},{})", x, y);

View file

@ -6,7 +6,7 @@ fn main() {
use winit::{ use winit::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size}, dpi::{PhysicalPosition, PhysicalSize, Position, Size},
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::{CursorIcon, Fullscreen, WindowBuilder}, window::{CursorIcon, Fullscreen, WindowBuilder},
}; };
@ -143,9 +143,9 @@ fn main() {
}); });
} }
event_loop.run(move |event, _event_loop, control_flow| { event_loop.run(move |event, _event_loop, control_flow| {
*control_flow = match !window_senders.is_empty() { match !window_senders.is_empty() {
true => ControlFlow::Wait, true => control_flow.set_wait(),
false => ControlFlow::Exit, false => control_flow.set_exit(),
}; };
match event { match event {
Event::WindowEvent { event, window_id } => match event { Event::WindowEvent { event, window_id } => match event {

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent}, event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::Window, window::Window,
}; };
@ -18,7 +18,7 @@ fn main() {
} }
event_loop.run(move |event, event_loop, control_flow| { event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, window_id } => { Event::WindowEvent { event, window_id } => {
@ -30,7 +30,7 @@ fn main() {
windows.remove(&window_id); windows.remove(&window_id);
if windows.is_empty() { if windows.is_empty() {
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
} }
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{ElementState, Event, WindowEvent}, event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -17,11 +17,11 @@ fn main() {
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
println!("{:?}", event); println!("{:?}", event);
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::MouseInput { WindowEvent::MouseInput {
state: ElementState::Released, state: ElementState::Released,
.. ..

View file

@ -5,7 +5,7 @@ fn main() {
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -25,11 +25,11 @@ fn main() {
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
println!("{:?}", event); println!("{:?}", event);
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
_ => (), _ => (),
}, },
Event::RedrawRequested(_) => { Event::RedrawRequested(_) => {

View file

@ -2,7 +2,7 @@ use simple_logger::SimpleLogger;
use winit::{ use winit::{
dpi::LogicalSize, dpi::LogicalSize,
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -20,11 +20,11 @@ fn main() {
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: input:
KeyboardInput { KeyboardInput {

View file

@ -2,7 +2,7 @@ use simple_logger::SimpleLogger;
use winit::{ use winit::{
dpi::PhysicalPosition, dpi::PhysicalPosition,
event::{ElementState, Event, WindowEvent}, event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -18,7 +18,7 @@ fn main() {
let mut cursor_position = PhysicalPosition::new(0.0, 0.0); let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::WindowEvent { Event::WindowEvent {
@ -45,7 +45,7 @@ fn main() {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => { } => {
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
_ => (), _ => (),
} }

View file

@ -4,7 +4,7 @@ use std::time::Duration;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, StartCause, WindowEvent}, event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -24,16 +24,16 @@ fn main() {
match event { match event {
Event::NewEvents(StartCause::Init) => { Event::NewEvents(StartCause::Init) => {
*control_flow = ControlFlow::WaitUntil(Instant::now() + timer_length) control_flow.set_wait_until(Instant::now() + timer_length);
} }
Event::NewEvents(StartCause::ResumeTimeReached { .. }) => { Event::NewEvents(StartCause::ResumeTimeReached { .. }) => {
*control_flow = ControlFlow::WaitUntil(Instant::now() + timer_length); control_flow.set_wait_until(Instant::now() + timer_length);
println!("\nTimer\n"); println!("\nTimer\n");
} }
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => control_flow.set_exit(),
_ => (), _ => (),
} }
}); });

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -18,14 +18,14 @@ fn main() {
window.set_title("A fantastic window!"); window.set_title("A fantastic window!");
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
println!("{:?}", event); println!("{:?}", event);
match event { match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => control_flow.set_exit(),
_ => (), _ => (),
} }
}); });

View file

@ -1,6 +1,6 @@
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -16,7 +16,7 @@ pub fn main() {
let log_list = wasm::create_log_list(&window); let log_list = wasm::create_log_list(&window);
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
wasm::log_event(&log_list, &event); wasm::log_event(&log_list, &event);
@ -25,7 +25,7 @@ pub fn main() {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id, window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit, } if window_id == window.id() => control_flow.set_exit(),
Event::MainEventsCleared => { Event::MainEventsCleared => {
window.request_redraw(); window.request_redraw();
} }

View file

@ -1,7 +1,7 @@
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -16,14 +16,14 @@ fn main() {
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
println!("{:?}", event); println!("{:?}", event);
match event { match event {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id, window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit, } if window_id == window.id() => control_flow.set_exit(),
Event::MainEventsCleared => { Event::MainEventsCleared => {
window.request_redraw(); window.request_redraw();
} }

View file

@ -4,7 +4,7 @@ use simple_logger::SimpleLogger;
use winit::{ use winit::{
dpi::{LogicalSize, PhysicalSize}, dpi::{LogicalSize, PhysicalSize},
event::{DeviceEvent, ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event::{DeviceEvent, ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::{Fullscreen, WindowBuilder}, window::{Fullscreen, WindowBuilder},
}; };
@ -31,7 +31,7 @@ fn main() {
let mut visible = true; let mut visible = true;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
match event { match event {
Event::DeviceEvent { Event::DeviceEvent {
@ -101,7 +101,7 @@ fn main() {
window.set_minimized(minimized); window.set_minimized(minimized);
} }
VirtualKeyCode::Q => { VirtualKeyCode::Q => {
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
VirtualKeyCode::V => { VirtualKeyCode::V => {
visible = !visible; visible = !visible;
@ -118,7 +118,7 @@ fn main() {
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id, window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit, } if window_id == window.id() => control_flow.set_exit(),
_ => (), _ => (),
} }
}); });

View file

@ -4,7 +4,7 @@ use std::path::Path;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::Event, event::Event,
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
window::{Icon, WindowBuilder}, window::{Icon, WindowBuilder},
}; };
@ -30,12 +30,12 @@ fn main() {
.unwrap(); .unwrap();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
if let Event::WindowEvent { event, .. } = event { if let Event::WindowEvent { event, .. } = event {
use winit::event::WindowEvent::*; use winit::event::WindowEvent::*;
match event { match event {
CloseRequested => *control_flow = ControlFlow::Exit, CloseRequested => control_flow.set_exit(),
DroppedFile(path) => { DroppedFile(path) => {
window.set_window_icon(Some(load_icon(&path))); window.set_window_icon(Some(load_icon(&path)));
} }

View file

@ -14,7 +14,7 @@ fn main() {
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
platform::run_return::EventLoopExtRunReturn, platform::run_return::EventLoopExtRunReturn,
window::WindowBuilder, window::WindowBuilder,
}; };
@ -30,7 +30,7 @@ fn main() {
while !quit { while !quit {
event_loop.run_return(|event, _, control_flow| { event_loop.run_return(|event, _, control_flow| {
*control_flow = ControlFlow::Wait; control_flow.set_wait();
if let Event::WindowEvent { event, .. } = &event { if let Event::WindowEvent { event, .. } = &event {
// Print only Window events to reduce noise // Print only Window events to reduce noise
@ -45,7 +45,7 @@ fn main() {
quit = true; quit = true;
} }
Event::MainEventsCleared => { Event::MainEventsCleared => {
*control_flow = ControlFlow::Exit; control_flow.set_exit();
} }
_ => (), _ => (),
} }

View file

@ -169,6 +169,41 @@ impl ControlFlow {
/// [`ExitWithCode`]: ControlFlow::ExitWithCode /// [`ExitWithCode`]: ControlFlow::ExitWithCode
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub const Exit: Self = Self::ExitWithCode(0); 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 { impl Default for ControlFlow {

View file

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

View file

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