Skip to content

Commit

Permalink
Add methods to set ControlFlow operation
Browse files Browse the repository at this point in the history
This allows downstream users to avoid importing `ControlFlow` from winit.
  • Loading branch information
swooster authored Apr 10, 2022
1 parent c57294b commit d624a3e
Show file tree
Hide file tree
Showing 27 changed files with 120 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.
- Added helper methods on `ControlFlow` to set its value.

# 0.26.1 (2022-01-05)

Expand Down
16 changes: 7 additions & 9 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{thread, time};
use simple_logger::SimpleLogger;
use winit::{
event::{Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand Down Expand Up @@ -88,23 +88,21 @@ fn main() {
window.request_redraw();
}
if close_requested {
*control_flow = ControlFlow::Exit;
control_flow.set_exit();
}
}
Event::RedrawRequested(_window_id) => {}
Event::RedrawEventsCleared => {
*control_flow = match mode {
Mode::Wait => ControlFlow::Wait,
match mode {
Mode::Wait => control_flow.set_wait(),
Mode::WaitUntil => {
if wait_cancelled {
*control_flow
} else {
ControlFlow::WaitUntil(instant::Instant::now() + WAIT_TIME)
if !wait_cancelled {
control_flow.set_wait_until(instant::Instant::now() + WAIT_TIME);
}
}
Mode::Poll => {
thread::sleep(POLL_SLEEP_TIME);
ControlFlow::Poll
control_flow.set_poll();
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::{CursorIcon, WindowBuilder},
};

Expand All @@ -15,7 +15,7 @@ fn main() {
let mut cursor_idx = 0;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent {
Expand All @@ -42,7 +42,7 @@ fn main() {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
control_flow.set_exit();
}
_ => (),
}
Expand Down
8 changes: 4 additions & 4 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -17,11 +17,11 @@ fn main() {
let mut modifiers = ModifiersState::default();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput {
input:
KeyboardInput {
Expand All @@ -33,7 +33,7 @@ fn main() {
} => {
use winit::event::VirtualKeyCode::*;
match key {
Escape => *control_flow = ControlFlow::Exit,
Escape => control_flow.set_exit(),
G => window.set_cursor_grab(!modifiers.shift()).unwrap(),
H => window.set_cursor_visible(modifiers.shift()),
_ => (),
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn main() {
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoopBuilder},
event_loop::EventLoopBuilder,
window::WindowBuilder,
};

Expand Down Expand Up @@ -34,14 +34,14 @@ fn main() {
});

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::UserEvent(event) => println!("user event: {:?}", event),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
} => control_flow.set_exit(),
_ => (),
}
});
Expand Down
4 changes: 2 additions & 2 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use winit::{
event::{
ElementState, Event, KeyboardInput, MouseButton, StartCause, VirtualKeyCode, WindowEvent,
},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::{Window, WindowBuilder, WindowId},
};

Expand All @@ -22,7 +22,7 @@ fn main() {
eprintln!("Switch which window is to be dragged by pressing \"x\".")
}
Event::WindowEvent { event, window_id } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Left,
Expand Down
8 changes: 4 additions & 4 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::{stdin, stdout, Write};

use simple_logger::SimpleLogger;
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::window::{Fullscreen, WindowBuilder};

Expand Down Expand Up @@ -32,11 +32,11 @@ fn main() {
.unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput {
input:
KeyboardInput {
Expand All @@ -46,7 +46,7 @@ fn main() {
},
..
} => match (virtual_code, state) {
(VirtualKeyCode::Escape, _) => *control_flow = ControlFlow::Exit,
(VirtualKeyCode::Escape, _) => control_flow.set_exit(),
(VirtualKeyCode::F, ElementState::Pressed) => {
if window.fullscreen().is_some() {
window.set_fullscreen(None);
Expand Down
6 changes: 3 additions & 3 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -21,7 +21,7 @@ fn main() {
ElementState::Released,
VirtualKeyCode::{N, Y},
};
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, .. } => {
Expand Down Expand Up @@ -63,7 +63,7 @@ fn main() {
// 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
// sent.
*control_flow = ControlFlow::Exit;
control_flow.set_exit();
}
}
N => {
Expand Down
6 changes: 3 additions & 3 deletions examples/min_max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use simple_logger::SimpleLogger;
use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -16,14 +16,14 @@ fn main() {
window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0)));

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();
println!("{:?}", event);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
} => control_flow.set_exit(),
_ => (),
}
});
Expand Down
6 changes: 3 additions & 3 deletions examples/minimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate winit;

use simple_logger::SimpleLogger;
use winit::event::{Event, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::event_loop::EventLoop;
use winit::window::WindowBuilder;

fn main() {
Expand All @@ -15,13 +15,13 @@ fn main() {
.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(),

// Keyboard input event to handle minimize via a hotkey
Event::WindowEvent {
Expand Down
6 changes: 3 additions & 3 deletions examples/mouse_wheel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand Down Expand Up @@ -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| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::MouseWheel { delta, .. } => match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => {
println!("mouse wheel Line Delta: ({},{})", x, y);
Expand Down
8 changes: 4 additions & 4 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
use winit::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::{CursorIcon, Fullscreen, WindowBuilder},
};

Expand Down Expand Up @@ -143,9 +143,9 @@ fn main() {
});
}
event_loop.run(move |event, _event_loop, control_flow| {
*control_flow = match !window_senders.is_empty() {
true => ControlFlow::Wait,
false => ControlFlow::Exit,
match !window_senders.is_empty() {
true => control_flow.set_wait(),
false => control_flow.set_exit(),
};
match event {
Event::WindowEvent { event, window_id } => match event {
Expand Down
6 changes: 3 additions & 3 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::Window,
};

Expand All @@ -18,7 +18,7 @@ fn main() {
}

event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, window_id } => {
Expand All @@ -30,7 +30,7 @@ fn main() {
windows.remove(&window_id);

if windows.is_empty() {
*control_flow = ControlFlow::Exit;
control_flow.set_exit();
}
}
WindowEvent::KeyboardInput {
Expand Down
6 changes: 3 additions & 3 deletions examples/request_redraw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -17,11 +17,11 @@ fn main() {
event_loop.run(move |event, _, control_flow| {
println!("{:?}", event);

*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::MouseInput {
state: ElementState::Released,
..
Expand Down
6 changes: 3 additions & 3 deletions examples/request_redraw_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -25,11 +25,11 @@ fn main() {
event_loop.run(move |event, _, control_flow| {
println!("{:?}", event);

*control_flow = ControlFlow::Wait;
control_flow.set_wait();

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => control_flow.set_exit(),
_ => (),
},
Event::RedrawRequested(_) => {
Expand Down
Loading

0 comments on commit d624a3e

Please sign in to comment.