Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ControlFlow::Wait the default #3106

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Web, add `EventLoopWindowTargetExtWebSys` and `PollStrategy`, which allows to set different strategies for `ControlFlow::Poll`. By default the Prioritized Task Scheduling API is used, but an option to use `Window.requestIdleCallback` is available as well. Both use `setTimeout()`, with a trick to circumvent throttling to 4ms, as a fallback.
- Implement `PartialOrd` and `Ord` for `MouseButton`.
- On X11, fix event loop not waking up on `ControlFlow::Poll` and `ControlFlow::WaitUntil`.
- **Breaking:** Change default `ControlFlow` from `Poll` to `Wait`.

# 0.29.1-beta

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ another library.
```rust
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -44,8 +44,6 @@ fn main() {
let window = WindowBuilder::new().build(&event_loop).unwrap();

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
4 changes: 1 addition & 3 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> Result<(), impl std::error::Error> {
use winit::{
dpi::{LogicalPosition, LogicalSize, Position},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
event_loop::{EventLoop, EventLoopWindowTarget},
window::raw_window_handle::HasRawWindowHandle,
window::{Window, WindowBuilder, WindowId},
};
Expand Down Expand Up @@ -47,8 +47,6 @@ fn main() -> Result<(), impl std::error::Error> {
println!("parent window: {parent_window:?})");

event_loop.run(move |event: Event<()>, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, window_id } = event {
match event {
WindowEvent::CloseRequested => {
Expand Down
4 changes: 1 addition & 3 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::{CursorIcon, WindowBuilder},
};

Expand All @@ -20,8 +20,6 @@ fn main() -> Result<(), impl std::error::Error> {
let mut cursor_idx = 0;

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::KeyboardInput {
Expand Down
86 changes: 41 additions & 45 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::{Key, ModifiersState},
window::{CursorGrabMode, WindowBuilder},
};
Expand All @@ -22,56 +22,52 @@ fn main() -> Result<(), impl std::error::Error> {

let mut modifiers = ModifiersState::default();

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: key,
state: ElementState::Released,
..
},
..
} => {
let result = match key {
Key::Escape => {
elwt.exit();
event_loop.run(move |event, elwt| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: key,
state: ElementState::Released,
..
},
..
} => {
let result = match key {
Key::Escape => {
elwt.exit();
Ok(())
}
Key::Character(ch) => match ch.to_lowercase().as_str() {
"g" => window.set_cursor_grab(CursorGrabMode::Confined),
"l" => window.set_cursor_grab(CursorGrabMode::Locked),
"a" => window.set_cursor_grab(CursorGrabMode::None),
"h" => {
window.set_cursor_visible(modifiers.shift_key());
Ok(())
}
Key::Character(ch) => match ch.to_lowercase().as_str() {
"g" => window.set_cursor_grab(CursorGrabMode::Confined),
"l" => window.set_cursor_grab(CursorGrabMode::Locked),
"a" => window.set_cursor_grab(CursorGrabMode::None),
"h" => {
window.set_cursor_visible(modifiers.shift_key());
Ok(())
}
_ => Ok(()),
},
_ => Ok(()),
};
},
_ => Ok(()),
};

if let Err(err) = result {
println!("error: {err}");
}
if let Err(err) = result {
println!("error: {err}");
}
WindowEvent::ModifiersChanged(new) => modifiers = new.state(),
WindowEvent::RedrawRequested => fill::fill_window(&window),
_ => (),
},
Event::DeviceEvent { event, .. } => match event {
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {delta:?}"),
DeviceEvent::Button { button, state } => match state {
ElementState::Pressed => println!("mouse button {button} pressed"),
ElementState::Released => println!("mouse button {button} released"),
},
_ => (),
}
WindowEvent::ModifiersChanged(new) => modifiers = new.state(),
WindowEvent::RedrawRequested => fill::fill_window(&window),
_ => (),
},
Event::DeviceEvent { event, .. } => match event {
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {delta:?}"),
DeviceEvent::Button { button, state } => match state {
ElementState::Pressed => println!("mouse button {button} pressed"),
ElementState::Released => println!("mouse button {button} released"),
},
_ => (),
}
},
_ => (),
})
}
30 changes: 13 additions & 17 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> Result<(), impl std::error::Error> {
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoopBuilder},
event_loop::EventLoopBuilder,
window::WindowBuilder,
};

Expand Down Expand Up @@ -40,23 +40,19 @@ fn main() -> Result<(), impl std::error::Error> {
}
});

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

match event {
Event::UserEvent(event) => println!("user event: {event:?}"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => elwt.exit(),
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
fill::fill_window(&window);
}
_ => (),
event_loop.run(move |event, elwt| match event {
Event::UserEvent(event) => println!("user event: {event:?}"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => elwt.exit(),
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
fill::fill_window(&window);
}
_ => (),
})
}

Expand Down
4 changes: 1 addition & 3 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use simple_logger::SimpleLogger;
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::event_loop::EventLoop;
use winit::keyboard::Key;
use winit::window::{Fullscreen, WindowBuilder};

Expand Down Expand Up @@ -53,8 +53,6 @@ fn main() -> Result<(), impl std::error::Error> {
println!("- A\tToggle mAx size limit");

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
Expand Down
4 changes: 1 addition & 3 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::Key,
window::WindowBuilder,
};
Expand All @@ -23,8 +23,6 @@ fn main() -> Result<(), impl std::error::Error> {
let mut close_requested = false;

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => {
Expand Down
3 changes: 1 addition & 2 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use simple_logger::SimpleLogger;
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, Event, Ime, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::{Key, KeyCode},
window::{ImePurpose, WindowBuilder},
};
Expand Down Expand Up @@ -40,7 +40,6 @@ fn main() -> Result<(), impl std::error::Error> {
let mut ime_pos = PhysicalPosition::new(0.0, 0.0);

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
Expand Down
4 changes: 1 addition & 3 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::{Key, ModifiersState},
// WARNING: This is not available on all platforms (for example on the web).
platform::modifier_supplement::KeyEventExtModifierSupplement,
Expand Down Expand Up @@ -32,8 +32,6 @@ fn main() -> Result<(), impl std::error::Error> {
let mut modifiers = ModifiersState::default();

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
Expand Down
4 changes: 1 addition & 3 deletions examples/mouse_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand Down Expand Up @@ -35,8 +35,6 @@ In other words, the deltas indicate the direction in which to move the content (
);

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
Expand Down
9 changes: 4 additions & 5 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> Result<(), impl std::error::Error> {
use winit::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::{Key, ModifiersState},
window::{CursorGrabMode, CursorIcon, Fullscreen, WindowBuilder, WindowLevel},
};
Expand Down Expand Up @@ -174,10 +174,9 @@ fn main() -> Result<(), impl std::error::Error> {
});
}
event_loop.run(move |event, elwt| {
match !window_senders.is_empty() {
true => elwt.set_control_flow(ControlFlow::Wait),
false => elwt.exit(),
};
if window_senders.is_empty() {
elwt.exit()
}
match event {
Event::WindowEvent { event, window_id } => match event {
WindowEvent::CloseRequested
Expand Down
4 changes: 1 addition & 3 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::Key,
window::Window,
};
Expand All @@ -27,8 +27,6 @@ fn main() -> Result<(), impl std::error::Error> {
println!("Press N to open a new window.");

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, window_id } = event {
match event {
WindowEvent::CloseRequested => {
Expand Down
4 changes: 1 addition & 3 deletions examples/request_redraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand All @@ -22,8 +22,6 @@ fn main() -> Result<(), impl std::error::Error> {
event_loop.run(move |event, elwt| {
println!("{event:?}");

elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
Expand Down
4 changes: 1 addition & 3 deletions examples/request_redraw_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() -> Result<(), impl std::error::Error> {
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
window::WindowBuilder,
};

Expand Down Expand Up @@ -36,8 +36,6 @@ fn main() -> Result<(), impl std::error::Error> {
event_loop.run(move |event, elwt| {
println!("{event:?}");

elwt.set_control_flow(ControlFlow::Wait);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
4 changes: 1 addition & 3 deletions examples/resizable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use simple_logger::SimpleLogger;
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
event_loop::EventLoop,
keyboard::KeyCode,
window::WindowBuilder,
};
Expand All @@ -28,8 +28,6 @@ fn main() -> Result<(), impl std::error::Error> {
.unwrap();

event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
Expand Down
Loading