No description
Find a file
AngelicosPhosphoros bb4aa22cf9 Improve waiting for messages on Windows
Previous version used [`SetTimer`] with `GetMessageW` for waiting.
The downside of UI timers like ones created by `SetTimer`,
is that they may be late by up to 15-16 ms.

To fix this behaviour, I added use of high resolution timers created by [`CreateWaitableTimerExW`] with the flag `CREATE_WAITABLE_TIMER_HIGH_RESOLUTION`.
In my previous experience, waiting on such timers have precision of roundly 0.5 ms which is the best available on Windows at the moment.
I use [`MsgWaitForMultipleObjectsEx`] to wait simultaneously for both timer and newly arriving events.

Unfortunately, high resolution timers are available only since Windows 10 1803. However:

1. Win 10 is already getting to the end of support, like all previous versions, so it is OK to rely on APIs introduced in it;
2. I use `dwMilliseconds` parameter of `MsgWaitForMultipleObjectsEx` as a fallback. It should perform not worse compared to waiting for events from `SetTimer`.

I also refactored code to remove event dispatching from function responsible for waiting for events. This provides more clear separations of concern and avoids unnecessary duplication of dispatching logic.

After [review] from @rib, I also moved the waiting itself from `wait_for_messages` method to separate function, so it is clearly seen that `wait_for_messages` do 3 things: notify app that we about to wait, wait, notify that we have new events.

I have tested behaviour using a egui app with Vulkan rendering with `VK_PRESENT_MODE_IMMEDIATE_KHR`, and older version consistently have twice less FPS than requested (e.g. 30 FPS when limit is 60 and 60 FPS when limit is 120) while newer version works more correctly (almost always 60 FPS when limit is 60, and only 5-10 frames missing when FPS is set to 120 or more).

Fixes https://github.com/rust-windowing/winit/issues/1610

[`CreateWaitableTimerExW`]: https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createwaitabletimerexw
[`MsgWaitForMultipleObjectsEx`]: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-msgwaitformultipleobjectsex
[`SetTimer`]: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-settimer
[review]: https://github.com/rust-windowing/winit/pull/3950#discussion_r1800184479
2024-10-28 16:38:17 -07:00
.cargo Use Taplo for TOML formatting 2024-07-28 17:38:44 +02:00
.github Add support for MacOS ARM64 (#3862) 2024-08-13 23:01:17 +02:00
docs/res Changes and improvements to the documentation (#3253) 2023-12-03 18:39:08 +01:00
dpi Add support for MacOS ARM64 (#3862) 2024-08-13 23:01:17 +02:00
examples macOS: add a way to hook standard keybinding events 2024-10-23 08:33:42 +00:00
src Improve waiting for messages on Windows 2024-10-28 16:38:17 -07:00
tests api: convert Window to dyn Window 2024-08-23 23:40:27 +03:00
.git-blame-ignore-revs Add to .git-blame-ignore-revs file 2024-07-28 17:39:04 +02:00
.gitattributes Update changelog guidelines to prevent conflicts from blocking PRs (#2145) 2022-01-23 13:55:33 +01:00
.gitignore Move to TypeScript (#3830) 2024-07-27 18:26:52 +02:00
build.rs Move iOS and macOS implementations into new apple module (#3756) 2024-06-24 13:26:49 +02:00
Cargo.toml Improve waiting for messages on Windows 2024-10-28 16:38:17 -07:00
CHANGELOG.md chore(docs): make changelogs visible on docsrs 2024-03-15 17:19:19 +04:00
clippy.toml Use Taplo for TOML formatting 2024-07-28 17:38:44 +02:00
CODE_OF_CONDUCT.md docs: add a code of conduct 2024-03-07 17:32:06 +04:00
CONTRIBUTING.md docs: fix release steps in CONTRIBUTING.md 2024-04-18 20:27:31 +04:00
deny.toml Add support for MacOS ARM64 (#3862) 2024-08-13 23:01:17 +02:00
FEATURES.md Remove EventLoopExtIOS::idiom and ios::Idiom (#2924) 2024-06-24 22:56:20 +02:00
HALL_OF_CHAMPIONS.md Changes and improvements to the documentation (#3253) 2023-12-03 18:39:08 +01:00
LICENSE Initial commit 2014-07-27 11:41:26 +02:00
README.md Bump version on master 2024-08-08 21:40:08 +03:00
rustfmt.toml Use Taplo for TOML formatting 2024-07-28 17:38:44 +02:00
taplo.toml Use Taplo for TOML formatting 2024-07-28 17:38:44 +02:00
typos.toml Use Taplo for TOML formatting 2024-07-28 17:38:44 +02:00

winit - Cross-platform window creation and management in Rust

Crates.io Docs.rs Master Docs CI Status

[dependencies]
winit = "0.30.5"

Documentation

For features within the scope of winit, see FEATURES.md.

For features outside the scope of winit, see Are we GUI Yet? and Are we game yet?, depending on what kind of project you're looking to do.

Contact Us

Join us in our Matrix room.

The maintainers have a meeting every friday at UTC 15. The meeting notes can be found here.

Usage

Winit is a window creation and management library. It can create windows and lets you handle events (for example: the window being resized, a key being pressed, a mouse movement, etc.) produced by the window.

Winit is designed to be a low-level brick in a hierarchy of libraries. Consequently, in order to show something on the window you need to use the platform-specific getters provided by winit, or another library.

MSRV Policy

This crate's Minimum Supported Rust Version (MSRV) is 1.73. Changes to the MSRV will be accompanied by a minor version bump.

As a tentative policy, the upper bound of the MSRV is given by the following formula:

min(sid, stable - 3)

Where sid is the current version of rustc provided by Debian Sid, and stable is the latest stable version of Rust. This bound may be broken in case of a major ecosystem shift or a security vulnerability.

An exception is made for the Android platform, where a higher Rust version must be used for certain Android features. In this case, the MSRV will be capped at the latest stable version of Rust minus three. This inconsistency is not reflected in Cargo metadata, as it is not powerful enough to expose this restriction.

Redox OS is also not covered by this MSRV policy, as it requires a Rust nightly toolchain to compile.

All crates in the rust-windowing organizations have the same MSRV policy.

Platform-specific usage

Check out the winit::platform module for platform-specific usage.