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.
This re-works the portable `run()` API that consumes the `EventLoop` and
runs the loop on the calling thread until the app exits.
This can be supported across _all_ platforms and compared to the
previous `run() -> !` API is now able to return a `Result` status on all
platforms except iOS and Web. Fixes: #2709
By moving away from `run() -> !` we stop calling `std::process::exit()`
internally as a means to kill the process without returning which means
it's possible to return an exit status and applications can return from
their `main()` function normally.
This also fixes Android support where an Activity runs in a thread but
we can't assume to have full ownership of the process (other services
could be running in separate threads).
Additionally all examples have generally been updated so that `main()`
returns a `Result` from `run()`
Fixes: #2709
* Fix bug causing RedrawRequested events to only get emitted every other iteration of the event loop.
* Initialize simple_logger in examples.
This PR's primary bug was discovered because a friend of mine reported
that winit was emitting concerning log messages, which I'd never seen
since none of the examples print out the log messages. This addresses
that, to hopefully reduce the chance of bugs going unnoticed in the
future.
* Add changelog entry
* Format
* Modify DPI API publicly and on Windows
* Add generic Position and make dpi creation functions const
* Make examples work
* Fix fullscreen windows not appearing
* Replace Logical coordinates in window events with Physical coordinates
* Update HiDpiFactorChanged
* Document to_static
* Format everything and add rustfmt to travis
* Remove extern crate winit from examples and add force_multiline_blocks
* Format the code properly
* Fix inconsistent period in PULL_REQUEST_TEMPLATE.md
* Only run rustfmt on nightly
* Travis fixings
* Rename EventsLoop and associated types to EventLoop
* Rename WindowEvent::Refresh to WindowEvent::Redraw
* Remove second thread from win32 backend
* Update run_forever to hijack thread
* Replace windows Mutex with parking_lot Mutex
* Implement new ControlFlow and associated events
* Add StartCause::Init support, timer example
* Add ability to send custom user events
* Fully invert windows control flow so win32 calls into winit's callback
* Add request_redraw
* Rename platform to platform_impl
* Rename os to platform, add Ext trait postfixes
* Add platform::desktop module with EventLoopExt::run_return
* Re-organize into module structure
* Improve documentation
* Small changes to examples
* Improve docs for run and run_return
* Change instances of "events_loop" to "event_loop"
* Rename MonitorId to MonitorHandle
* Add CHANGELOG entry
* Improve WaitUntil timer precision
* When SendEvent is called during event closure, buffer events
* Fix resize lag when waiting in some situations
* Update send test and errors that broke some examples/APIs
* Improve clarity/fix typos in docs
* Fix unreachable panic after setting ControlFlow to Poll during some RedrawRequested events.
* Fix crash when running in release mode
* Remove crossbeam dependency and make drop events work again
* Remove serde implementations from ControlFlow
* Fix 1.24.1 build
* Fix freeze when setting decorations
* Replace &EventLoop in callback with &EventLoopWindowTarget
* Document and implement Debug for EventLoopWindowTarget
* Fix some deadlocks that could occur when changing window state
* Fix thread executor not executing closure when called from non-loop thread
* Fix buffered events not getting dispatched
* Fix crash with runner refcell not getting dropped
* Address review feedback
* Fix CHANGELOG typo
* Catch panics in user callback
* Replace Closed event with CloseRequested and Destroyed
Implements #434
The existing Closed event had ambiguous meaning, both in name and in
cross-platform behavior. Closed is now split into two more precise events:
* CloseRequested - the window has been requested to close, most commonly by
having clicked the window's close button. Whether or not you respond by
closing the window is up to you.
* Destroyed - the window has been destroyed, and can no longer be safely
used.
Most notably, now you can reliably implement classic patterns like
prompting the user to save their work before closing, and have the
opportunity to perform any necessary cleanup.
Migrating to the new API is straightforward. In most cases, you can simply
replace all existing usages of Closed with CloseRequested. For more
information, see the example programs, particularly handling_close and
multiwindow.
iOS applications must replace all usages of Closed with Destroyed, and
require no other changes.
* wayland: upgrade wayland-window
This new version of wayland window considerably simplifies the
window handling for winit, meaning much of the previous juggling
is no longer needed, and the windows will appear even if nothing is
drawn.
* wayland: cleanup unused stuff
This removes the need for the EventsLoop::interrupt method by inroducing
a ControlFlow type. This new type is to be returned by the user's
callback and indicates whether the `EventsLoop` should continue waiting
for events or break from the loop.
Only the wayland, x11 and api_transition backends have been updated so
far, and only the wayland backend has actually been tested.
This allows for passing the window_resize_callback fn during the window
building stage. More importantly, this allows setting the callback
without the need for mutable access to the Window, making it possible
to set the callback in the downstream glium crate.
This may solve tomaka/glium#1232 for most folk.