mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 13:51:30 +11:00
ae7497e18f
The idea that redraw events are dispatched with a specific ordering that makes it possible to specifically report when we have finished dispatching redraw events isn't portable and the way in which we dispatched RedrawEventsCleared was inconsistent across backends. More generally speaking, there is no inherent relationship between redrawing and event loop iterations. An event loop may wake up at any frequency depending on what sources of input events are being listened to but redrawing is generally throttled and in some way synchronized with the display frequency. Similarly there's no inherent relationship between a single event loop iteration and the dispatching of any specific kind of "main" event. An event loop wakes up when there are events to read (e.g. input events or responses from a display server / compositor) and goes back to waiting when there's nothing else to read. There isn't really a special kind of "main" event that is dispatched in order with respect to other events. What we can do more portably is emit an event when the event loop is about to block and wait for new events. In practice this is very similar to how MainEventsCleared was implemented except it wasn't the very last event previously since redraw events could be dispatched afterwards. The main backend where we don't strictly know when we're going to wait for events is Web (since the real event loop is internal to the browser). For now we emulate AboutToWait on Web similar to how MainEventsCleared was dispatched. In practice most applications almost certainly shouldn't care about AboutToWait because the frequency of event loop iterations is essentially arbitrary and usually irrelevant.
65 lines
2 KiB
Rust
65 lines
2 KiB
Rust
use log::debug;
|
|
use simple_logger::SimpleLogger;
|
|
use winit::{
|
|
dpi::LogicalSize,
|
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
|
event_loop::EventLoop,
|
|
keyboard::Key,
|
|
window::WindowBuilder,
|
|
};
|
|
|
|
#[path = "util/fill.rs"]
|
|
mod fill;
|
|
|
|
fn main() -> Result<(), impl std::error::Error> {
|
|
SimpleLogger::new().init().unwrap();
|
|
let event_loop = EventLoop::new();
|
|
|
|
let window = WindowBuilder::new()
|
|
.with_title("A fantastic window!")
|
|
.with_inner_size(LogicalSize::new(128.0, 128.0))
|
|
.with_resize_increments(LogicalSize::new(25.0, 25.0))
|
|
.build(&event_loop)
|
|
.unwrap();
|
|
|
|
let mut has_increments = true;
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
control_flow.set_wait();
|
|
|
|
match event {
|
|
Event::WindowEvent {
|
|
event: WindowEvent::CloseRequested,
|
|
window_id,
|
|
} if window_id == window.id() => control_flow.set_exit(),
|
|
Event::WindowEvent {
|
|
event:
|
|
WindowEvent::KeyboardInput {
|
|
event:
|
|
KeyEvent {
|
|
logical_key: Key::Space,
|
|
state: ElementState::Released,
|
|
..
|
|
},
|
|
..
|
|
},
|
|
window_id,
|
|
} if window_id == window.id() => {
|
|
has_increments = !has_increments;
|
|
|
|
let new_increments = match window.resize_increments() {
|
|
Some(_) => None,
|
|
None => Some(LogicalSize::new(25.0, 25.0)),
|
|
};
|
|
debug!("Had increments: {}", new_increments.is_none());
|
|
window.set_resize_increments(new_increments);
|
|
}
|
|
Event::AboutToWait => window.request_redraw(),
|
|
Event::RedrawRequested(_) => {
|
|
fill::fill_window(&window);
|
|
}
|
|
_ => (),
|
|
}
|
|
})
|
|
}
|