Skip to content

Commit

Permalink
Move ControlFlow to EventLoopWindowTarget
Browse files Browse the repository at this point in the history
Fixes #3042.
  • Loading branch information
daxpedda authored and kchibisov committed Oct 21, 2023
1 parent 42243ce commit 48647b5
Show file tree
Hide file tree
Showing 60 changed files with 799 additions and 859 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Wayland, fix `TouchPhase::Canceled` being sent for moved events.
- Mark `startup_notify` unsafe functions as safe.
- Fix a bug where Wayland would be chosen on Linux even if the user specified `with_x11`. (#3058)
- **Breaking:** Moved `ControlFlow` to `EventLoopWindowTarget::set_control_flow()` and `EventLoopWindowTarget::control_flow()`.
- **Breaking:** Moved `ControlFlow::Exit` to `EventLoopWindowTarget::set_exit()` and `EventLoopWindowTarget::exiting()` and removed `ControlFlow::ExitWithCode(_)` entirely.

# 0.29.1-beta

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();

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

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
} if window_id == window.id() => elwt.exit(),
_ => (),
}
});
Expand Down
8 changes: 4 additions & 4 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ fn main() -> Result<(), impl std::error::Error> {

println!("parent window: {parent_window:?})");

event_loop.run(move |event: Event<()>, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
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 => {
windows.clear();
*control_flow = ControlFlow::Exit;
elwt.exit();
}
WindowEvent::CursorEntered { device_id: _ } => {
// On x11, println when the cursor entered in a window even if the child window is created
Expand All @@ -70,7 +70,7 @@ fn main() -> Result<(), impl std::error::Error> {
},
..
} => {
spawn_child_window(&parent_window, event_loop, &mut windows);
spawn_child_window(&parent_window, elwt, &mut windows);
}
WindowEvent::RedrawRequested => {
if let Some(window) = windows.get(&window_id) {
Expand Down
14 changes: 8 additions & 6 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use web_time as time;
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::WindowBuilder,
};
Expand Down Expand Up @@ -47,7 +47,7 @@ fn main() -> Result<(), impl std::error::Error> {
let mut wait_cancelled = false;
let mut close_requested = false;

event_loop.run(move |event, _, control_flow| {
event_loop.run(move |event, elwt| {
use winit::event::StartCause;
println!("{event:?}");
match event {
Expand Down Expand Up @@ -104,20 +104,22 @@ fn main() -> Result<(), impl std::error::Error> {
}

match mode {
Mode::Wait => control_flow.set_wait(),
Mode::Wait => elwt.set_control_flow(ControlFlow::Wait),
Mode::WaitUntil => {
if !wait_cancelled {
control_flow.set_wait_until(time::Instant::now() + WAIT_TIME);
elwt.set_control_flow(ControlFlow::WaitUntil(
time::Instant::now() + WAIT_TIME,
));
}
}
Mode::Poll => {
thread::sleep(POLL_SLEEP_TIME);
control_flow.set_poll();
elwt.set_control_flow(ControlFlow::Poll);
}
};

if close_requested {
control_flow.set_exit();
elwt.exit();
}
}
_ => (),
Expand Down
8 changes: 4 additions & 4 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::EventLoop,
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, WindowBuilder},
};

Expand All @@ -19,8 +19,8 @@ fn main() -> Result<(), impl std::error::Error> {

let mut cursor_idx = 0;

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

if let Event::WindowEvent { event, .. } = event {
match event {
Expand All @@ -44,7 +44,7 @@ fn main() -> Result<(), impl std::error::Error> {
fill::fill_window(&window);
}
WindowEvent::CloseRequested => {
control_flow.set_exit();
elwt.exit();
}
_ => (),
}
Expand Down
10 changes: 5 additions & 5 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::EventLoop,
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, ModifiersState},
window::{CursorGrabMode, WindowBuilder},
};
Expand All @@ -22,12 +22,12 @@ fn main() -> Result<(), impl std::error::Error> {

let mut modifiers = ModifiersState::default();

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

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
Expand All @@ -39,7 +39,7 @@ fn main() -> Result<(), impl std::error::Error> {
} => {
let result = match key {
Key::Escape => {
control_flow.set_exit();
elwt.exit();
Ok(())
}
Key::Character(ch) => match ch.to_lowercase().as_str() {
Expand Down
8 changes: 4 additions & 4 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::EventLoopBuilder,
event_loop::{ControlFlow, EventLoopBuilder},
window::WindowBuilder,
};

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

event_loop.run(move |event, _, control_flow| {
control_flow.set_wait();
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,
..
} => control_flow.set_exit(),
} => elwt.exit(),
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
Expand Down
4 changes: 2 additions & 2 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ fn main() -> Result<(), impl std::error::Error> {
let mut switched = false;
let mut entered_id = window_2.id();

event_loop.run(move |event, _, control_flow| match event {
event_loop.run(move |event, elwt| match event {
Event::NewEvents(StartCause::Init) => {
eprintln!("Switch which window is to be dragged by pressing \"x\".")
}
Event::WindowEvent { event, window_id } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Left,
Expand Down
10 changes: 5 additions & 5 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::EventLoop;
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::Key;
use winit::window::{Fullscreen, WindowBuilder};

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

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

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
Expand All @@ -67,7 +67,7 @@ fn main() -> Result<(), impl std::error::Error> {
},
..
} => match key {
Key::Escape => control_flow.set_exit(),
Key::Escape => elwt.exit(),
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
Key::Character(ch) => match ch.to_lowercase().as_str() {
Expand Down
8 changes: 4 additions & 4 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::EventLoop,
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::WindowBuilder,
};
Expand All @@ -22,8 +22,8 @@ fn main() -> Result<(), impl std::error::Error> {

let mut close_requested = false;

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

if let Event::WindowEvent { event, .. } = event {
match event {
Expand Down Expand Up @@ -66,7 +66,7 @@ fn main() -> Result<(), impl std::error::Error> {
// 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.set_exit();
elwt.exit();
}
}
Key::Character("n") => {
Expand Down
6 changes: 3 additions & 3 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ fn main() -> Result<(), impl std::error::Error> {
let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
let mut ime_pos = PhysicalPosition::new(0.0, 0.0);

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CursorMoved { position, .. } => {
cursor_position = position;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ fn main() -> Result<(), impl std::error::Error> {

let mut modifiers = ModifiersState::default();

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

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::ModifiersChanged(new) => {
modifiers = new.state();
}
Expand Down
8 changes: 4 additions & 4 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::EventLoop,
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};

Expand Down Expand Up @@ -34,12 +34,12 @@ In both cases the example window should move like the content of a scroll area i
In other words, the deltas indicate the direction in which to move the content (in this case the window)."
);

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

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CloseRequested => elwt.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 @@ -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::EventLoop,
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, ModifiersState},
window::{CursorGrabMode, CursorIcon, Fullscreen, WindowBuilder, WindowLevel},
};
Expand Down Expand Up @@ -173,10 +173,10 @@ fn main() -> Result<(), impl std::error::Error> {
}
});
}
event_loop.run(move |event, _event_loop, control_flow| {
event_loop.run(move |event, elwt| {
match !window_senders.is_empty() {
true => control_flow.set_wait(),
false => control_flow.set_exit(),
true => elwt.set_control_flow(ControlFlow::Wait),
false => elwt.exit(),
};
match event {
Event::WindowEvent { event, window_id } => match event {
Expand Down
10 changes: 5 additions & 5 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::EventLoop,
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::Window,
};
Expand All @@ -26,8 +26,8 @@ fn main() -> Result<(), impl std::error::Error> {

println!("Press N to open a new window.");

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

if let Event::WindowEvent { event, window_id } = event {
match event {
Expand All @@ -38,7 +38,7 @@ fn main() -> Result<(), impl std::error::Error> {
windows.remove(&window_id);

if windows.is_empty() {
control_flow.set_exit();
elwt.exit();
}
}
WindowEvent::KeyboardInput {
Expand All @@ -51,7 +51,7 @@ fn main() -> Result<(), impl std::error::Error> {
is_synthetic: false,
..
} if matches!(c.as_ref(), "n" | "N") => {
let window = Window::new(event_loop).unwrap();
let window = Window::new(elwt).unwrap();
println!("Opened a new window: {:?}", window.id());
windows.insert(window.id(), window);
}
Expand Down
Loading

0 comments on commit 48647b5

Please sign in to comment.