Compare commits

..

5 Commits
v0.30.2 ... it

Author SHA1 Message Date
John Nunley
68acedcdda feat: Add Docker support to integration tests
This allows the tests to be run inside of a Docker container with linux
with X11 inside.

Signed-off-by: John Nunley <dev@notgull.net>
2024-03-17 19:26:52 -07:00
John Nunley
facef799d3 it: Don't capture stdout
Signed-off-by: John Nunley <dev@notgull.net>
2024-03-17 19:26:52 -07:00
John Nunley
25fab64f6e it: Add runner and common tests
Still needs some work, but the idea should be clear.

Signed-off-by: John Nunley <dev@notgull.net>
2024-03-17 19:26:52 -07:00
John Nunley
b3333b47e1 it: Add basic handlers
Signed-off-by: John Nunley <dev@notgull.net>
2024-03-17 19:26:52 -07:00
John Nunley
1bdba3cacc it: Create the 'gui-test' crate
The 'gui-test' crate is intended to provide a test framework for process
isolated and remote test cases. Like how I intend to test winit.

Signed-off-by: John Nunley <dev@notgull.net>
2024-03-17 19:26:52 -07:00
199 changed files with 8963 additions and 5887 deletions

View File

@@ -1,3 +1,6 @@
[alias]
run-wasm = ["run", "--release", "--package", "run-wasm", "--"]
# Allow rust-analyzer and local `cargo doc` invocations to pick up unreleased changelog entries # Allow rust-analyzer and local `cargo doc` invocations to pick up unreleased changelog entries
# #
# Note that these flags are (intentionally) not included when building from the downloaded crate. # Note that these flags are (intentionally) not included when building from the downloaded crate.

View File

@@ -1,4 +0,0 @@
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs
# chore(rustfmt): use nightly
7b0c7b6cb2c62767ca0c73c857b299883f55a883

3
.github/CODEOWNERS vendored
View File

@@ -32,3 +32,6 @@
# Orbital (Redox OS) # Orbital (Redox OS)
/src/platform/orbital.rs @jackpot51 /src/platform/orbital.rs @jackpot51
/src/platform_impl/orbital @jackpot51 /src/platform_impl/orbital @jackpot51
# Integration tests
/it @notgull

View File

@@ -10,8 +10,8 @@ jobs:
name: Check formatting name: Check formatting
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly - uses: dtolnay/rust-toolchain@stable
with: with:
components: rustfmt components: rustfmt
- name: Check Formatting - name: Check Formatting
@@ -88,7 +88,7 @@ jobs:
CMD: ${{ matrix.platform.cmd }} CMD: ${{ matrix.platform.cmd }}
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: Restore cache of cargo folder - name: Restore cache of cargo folder
# We use `restore` and later `save`, so that we can create the key after # We use `restore` and later `save`, so that we can create the key after
@@ -202,6 +202,41 @@ jobs:
~/.cargo/git/db/ ~/.cargo/git/db/
key: cargo-${{ matrix.toolchain }}-${{ matrix.platform.name }}-${{ hashFiles('Cargo.lock') }} key: cargo-${{ matrix.toolchain }}-${{ matrix.platform.name }}-${{ hashFiles('Cargo.lock') }}
it:
name: Run integration tests on ${{ matrix.platform.name }}
runs-on: ${{ matrix.platform.os }}
strategy:
fail-fast: false
matrix:
toolchain: [stable, nightly]
platform:
# Note: Make sure that we test all the `docs.rs` targets defined in Cargo.toml!
- { name: 'X11', target: x86_64-unknown-linux-gnu, os: ubuntu-latest, options: '--no-default-features --features=x11' }
env:
# Set more verbose terminal output
CARGO_TERM_VERBOSE: true
RUST_BACKTRACE: 1
# Faster compilation and error on warnings
RUSTFLAGS: '--codegen=debuginfo=0 --deny=warnings ${{ matrix.platform.rustflags }}'
OPTIONS: --target=${{ matrix.platform.target }} ${{ matrix.platform.options }}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
- name: Log into GHCR
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
- name: Common tests
run: cargo run -p gui-test-runner -- common-tests ${{ matrix.platform.target }}
cargo-deny: cargo-deny:
name: Run cargo-deny on ${{ matrix.platform.name }} name: Run cargo-deny on ${{ matrix.platform.name }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -220,7 +255,7 @@ jobs:
- { name: 'Windows', target: x86_64-pc-windows-gnu } - { name: 'Windows', target: x86_64-pc-windows-gnu }
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- uses: EmbarkStudios/cargo-deny-action@v1 - uses: EmbarkStudios/cargo-deny-action@v1
with: with:
command: check command: check

View File

@@ -19,7 +19,7 @@ jobs:
id-token: write id-token: write
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master - uses: dtolnay/rust-toolchain@master
with: with:

View File

@@ -1,122 +1,52 @@
# Contribution Guidelines # Winit Contributing Guidelines
This document contains guidelines for contributing code to winit. It has to be ## Scope
followed in order for your patch to be approved and applied. [See `FEATURES.md`](./FEATURES.md). When requesting or implementing a new Winit feature, you should
consider whether or not it's directly related to window creation or input handling. If it isn't, it
may be worth creating a separate crate that extends Winit's API to add that functionality.
## Contributing
Anyone can contribute to winit, however given that it's a cross platform ## Reporting an issue
windowing toolkit getting certain changes incorporated could be challenging.
To save your time it's wise to check already opened [pull requests][prs] and When reporting an issue, in order to help the maintainers understand what the problem is, please make
[issues][issues]. In general, bug fixes and missing implementations are always your description of the issue as detailed as possible:
accepted, however larger new API proposals should go into the issue first. When
in doubt contact us on [Matrix][matrix] or via opening an issue.
### Submitting your work and handling review - if it is a bug, please provide a clear explanation of what happens, what should happen, and how to
reproduce the issue, ideally by providing a minimal program exhibiting the problem
- if it is a feature request, please provide a clear argumentation about why you believe this feature
should be supported by winit
All patches have to be sent on Github as [pull requests][prs]. To simplify your ## Making a pull request
life during review it's recommended to check the "give contributors write access
to the branch" checkbox.
#### Handling review When making a code contribution to winit, before opening your pull request, please make sure that:
During the review process certain events could require an action from your side, - your patch builds with Winit's minimal supported rust version - Rust 1.70.
common patterns and reactions are described below. - you tested your modifications on all the platforms impacted, or if not possible, detail which platforms
were not tested, and what should be tested, so that a maintainer or another contributor can test them
- you updated any relevant documentation in winit
- you left comments in your code explaining any part that is not straightforward, so that the
maintainers and future contributors don't have to try to guess what your code is supposed to do
- your PR adds an entry to the current changelog if the introduced change is relevant to winit users.
_Event:_ The CI fails to build, but it looks like it is not your fault. Not You needn't worry about the added entry causing conflicts, the maintainer that merges the PR will
communicating so could result in maintainers not looking into your patch, handle those for you when merging (see below).
since they may assume that you're still working on it.\ - if your PR affects the platform compatibility of one or more features or adds another feature, the
_Desired behaviour:_ Write a message saying roughly the following "The CI relevant sections in [`FEATURES.md`](https://github.com/rust-windowing/winit/blob/master/FEATURES.md#features)
failure is unrelated", so that the maintainers will fix it for you. should be updated.
_Event:_ Maintainer requested changes to your PR.\ Once your PR is open, you can ask for a review by a maintainer of your platform. Winit's merging policy
_Desired behavior:_ Once you address the request, you should re-request a review is that a PR must be approved by at least two maintainers of winit before being merged, including
with GitHub's UI. If you don't agree with what maintainer suggested, you at least a maintainer of the platform (a maintainer making a PR themselves counts as approving it).
should state your objections and re-request the review. That will indicate that
the ball is on maintainer's side.
_Event:_ You've opened a PR, but maintainer shortly after commented that they Once your PR is deemed ready, the merging maintainer will take care of resolving conflicts in the
want to work on that themselves.\ `changelog` module (but you must resolve other conflicts yourself). Doing this requires that you check the
_Desired behavior:_ Discuss with the maintainer regarding their plans if they "give contributors write access to the branch" checkbox when creating the PR.
were not outlined in the initial response, because such response means that they
are not interested in reviewing your code. Such thing could happen when
underestimating complexity of the task you're solving or when your patch
mandate certain downstream designs. In general, the maintainer will likely
close your PR in order to prevent work being done on it.
[prs]: https://github.com/rust-windowing/winit/pulls
[issues]: https://github.com/rust-windowing/winit/issues
[matrix]: https://matrix.to/#/#rust-windowing:matrix.org
## Maintainers ## Maintainers
Winit has plenty of maintainers with different backgrounds, different time The current maintainers for each platform are listed in the [CODEOWNERS](.github/CODEOWNERS) file.
available to work on Winit, and reasons to be winit maintainer in the first
place. To ensure that Winit's code quality does not decrease over time and to
make it easier to teach new maintainers the "winit way of doing things" the
common policies and routines are defined in this section.
The current maintainers for each platform are listed in [this file][CODEOWNERS]. ## Release process
### Contributions handling
The maintainers must ensure that the external contributions meet Winit's
quality standards. If it's not, it **is the maintainer's responsibility** to
bring it on par, which includes:
- Ensure that formatting is consistent and `CHANGELOG` messages are clear
for the end users.
- Improve the commit message, so it'll be easier for other maintainers to
understand the motivation without going through all the discussions on the
particular patch/PR.
- Ensure that the proposed patch doesn't break platform parity. If the
breakage is desired by contributor, an issue should be opened to discuss
with other maintainers before merging.
- Always fix CI issues before merging if they don't originate from the
submitted work.
However, maintainers should give some leeway for external contributors, so they
don't feel discouraged contributing, for example:
- Suggest a patch to resolve style issues, if it's the only issue with the
submitted work. Keep in mind that pushing the resolution yourself is not
desired, because the contributor might not agree with what you did.
- Be more explicit on how things should be done if you don't like the
approach.
- Suggest to finish the PR for them if they're absent for a while and you need
the proposed changes to move forward with something. In such cases the
maintainer must preserve attribution with `Co-authored-by`, `Suggested-by`,
or keep the original committer.
- Rebase their work for them when massive changes to the Winit codebase were
introduced.
When reviewing code of other maintainers all of the above is on the maintainer
who submitted the patch. Interested maintainers could help push the work over
the finish line, but teaching other maintainers should be preferred.
For a _regular_ contributor to winit, the maintainer should slowly start
requiring contributor to match *maintainer* quality standards when writing
patches and commit messages.
### Contributing
When submitting a patch, the maintainer should follow the general contributing
guidelines, however greater attention to detail is expected in this case.
To make life simpler for other maintainers it's suggested to create your branch
under the project repository instead of your own fork. The naming scheme is
`github_user_name/branch_name`. Doing so will make your work easier to rebase
for other maintainers when you're absent.
### Administrative Actions
Some things (such as changing required CI steps, adding contributors, ...)
require administrative permissions. If you don't have those, ask about the
change in an issue. If you have the permissions, discuss it with at least one
other admin before making the change.
### Release process
Given that winit is a widely used library, we should be able to make a patch Given that winit is a widely used library, we should be able to make a patch
releases at any time we want without blocking the development of new features. releases at any time we want without blocking the development of new features.
@@ -128,8 +58,7 @@ The exact steps for an exemplary `0.2.0` release might look like this:
1. Initially, the version on the latest master is `0.1.0` 1. Initially, the version on the latest master is `0.1.0`
2. A new `v0.2.x` branch is created for the release 2. A new `v0.2.x` branch is created for the release
3. Update released `cfg_attr` in `src/changelog/mod.rs` to `v0.2.md` 3. Update released `cfg_attr` in `src/changelog/mod.rs` to `v0.2.md`
4. Move entries from `src/changelog/unreleased.md` into 4. Move entries from `src/changelog/unreleased.md` into `src/changelog/v0.2.md`
`src/changelog/v0.2.md`
5. In the branch, the version is bumped to `v0.2.0` 5. In the branch, the version is bumped to `v0.2.0`
6. The new commit in the branch is tagged `v0.2.0` 6. The new commit in the branch is tagged `v0.2.0`
7. The version is pushed to crates.io 7. The version is pushed to crates.io
@@ -141,5 +70,3 @@ When doing a patch release, the process is similar:
2. Checkout the `v0.2.x` branch 2. Checkout the `v0.2.x` branch
3. Cherry-pick the required non-breaking changes into the `v0.2.x` 3. Cherry-pick the required non-breaking changes into the `v0.2.x`
4. Follow steps 4-9 of the regular release example 4. Follow steps 4-9 of the regular release example
[CODEOWNERS]: .github/CODEOWNERS

View File

@@ -1,10 +1,7 @@
[package] [package]
name = "winit" name = "winit"
version = "0.30.2" version = "0.29.15"
authors = [ authors = ["The winit contributors", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
"The winit contributors",
"Pierre Krieger <pierre.krieger1708@gmail.com>",
]
description = "Cross-platform window creation library." description = "Cross-platform window creation library."
keywords = ["windowing"] keywords = ["windowing"]
readme = "README.md" readme = "README.md"
@@ -14,7 +11,6 @@ rust-version.workspace = true
repository.workspace = true repository.workspace = true
license.workspace = true license.workspace = true
edition.workspace = true edition.workspace = true
exclude = ["/.cargo"]
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = [ features = [
@@ -49,15 +45,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features] [features]
default = ["rwh_06", "x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] default = ["rwh_06", "x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"] x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"]
wayland = [ wayland = ["wayland-client", "wayland-backend", "wayland-protocols", "wayland-protocols-plasma", "sctk", "ahash", "memmap2"]
"wayland-client",
"wayland-backend",
"wayland-protocols",
"wayland-protocols-plasma",
"sctk",
"ahash",
"memmap2",
]
wayland-dlopen = ["wayland-backend/dlopen"] wayland-dlopen = ["wayland-backend/dlopen"]
wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"] wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"]
wayland-csd-adwaita-crossfont = ["sctk-adwaita", "sctk-adwaita/crossfont"] wayland-csd-adwaita-crossfont = ["sctk-adwaita", "sctk-adwaita/crossfont"]
@@ -71,143 +59,94 @@ rwh_05 = ["dep:rwh_05", "ndk/rwh_05"]
rwh_06 = ["dep:rwh_06", "ndk/rwh_06"] rwh_06 = ["dep:rwh_06", "ndk/rwh_06"]
[build-dependencies] [build-dependencies]
cfg_aliases = "0.2.1" cfg_aliases = "0.2.0"
[dependencies] [dependencies]
bitflags = "2" bitflags = "2"
cursor-icon = "1.1.0" cursor-icon = "1.1.0"
dpi = { version = "0.1.1", path = "dpi" } dpi = { version = "0.1.1", path = "dpi" }
rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true } rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true }
rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = [ rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = ["std"], optional = true }
"std", rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"], optional = true }
], optional = true }
rwh_06 = { package = "raw-window-handle", version = "0.6", features = [
"std",
], optional = true }
serde = { workspace = true, optional = true } serde = { workspace = true, optional = true }
smol_str = "0.2.0" smol_str = "0.2.0"
tracing = { version = "0.1.40", default_features = false } tracing = { version = "0.1.40", default_features = false }
[dev-dependencies] [dev-dependencies]
image = { version = "0.25.0", default-features = false, features = ["png"] } image = { version = "0.24.0", default-features = false, features = ["png"] }
tracing = { version = "0.1.40", default_features = false, features = ["log"] } tracing = { version = "0.1.40", default_features = false, features = ["log"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
winit = { path = ".", features = ["rwh_05"] } winit = { path = ".", features = ["rwh_05"] }
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies] [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies]
softbuffer = { version = "0.4.0", default-features = false, features = [ softbuffer = { version = "0.3.0", default-features = false, features = ["x11", "x11-dlopen", "wayland", "wayland-dlopen"] }
"x11",
"x11-dlopen",
"wayland",
"wayland-dlopen",
] }
[target.'cfg(target_os = "android")'.dependencies] [target.'cfg(target_os = "android")'.dependencies]
android-activity = "0.6.0" android-activity = "0.5.0"
ndk = { version = "0.9.0", default-features = false } ndk = { version = "0.8.0", default-features = false }
ndk-sys = "0.5.0"
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
core-foundation = "0.9.3" core-foundation = "0.9.3"
objc2 = "0.5.2" objc2 = "0.5.0"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
core-graphics = "0.23.1" core-graphics = "0.23.1"
block2 = "0.5.1"
[target.'cfg(target_os = "macos")'.dependencies.objc2-foundation] [target.'cfg(target_os = "macos")'.dependencies.icrate]
version = "0.2.2" version = "0.1.0"
features = [
"block2",
"dispatch",
"NSArray",
"NSAttributedString",
"NSData",
"NSDictionary",
"NSDistributedNotificationCenter",
"NSEnumerator",
"NSNotification",
"NSObjCRuntime",
"NSPathUtilities",
"NSProcessInfo",
"NSRunLoop",
"NSString",
"NSThread",
"NSValue",
]
[target.'cfg(target_os = "macos")'.dependencies.objc2-app-kit]
version = "0.2.2"
features = [
"NSAppearance",
"NSApplication",
"NSBitmapImageRep",
"NSButton",
"NSColor",
"NSControl",
"NSCursor",
"NSDragging",
"NSEvent",
"NSGraphics",
"NSGraphicsContext",
"NSImage",
"NSImageRep",
"NSMenu",
"NSMenuItem",
"NSOpenGLView",
"NSPasteboard",
"NSResponder",
"NSRunningApplication",
"NSScreen",
"NSTextInputClient",
"NSTextInputContext",
"NSView",
"NSWindow",
"NSWindowScripting",
"NSWindowTabGroup",
]
[target.'cfg(target_os = "ios")'.dependencies.objc2-foundation]
version = "0.2.2"
features = [ features = [
"dispatch", "dispatch",
"NSArray", "Foundation",
"NSEnumerator", "Foundation_NSArray",
"NSGeometry", "Foundation_NSAttributedString",
"NSObjCRuntime", "Foundation_NSMutableAttributedString",
"NSString", "Foundation_NSData",
"NSProcessInfo", "Foundation_NSDictionary",
"NSThread", "Foundation_NSString",
"NSSet", "Foundation_NSProcessInfo",
"Foundation_NSThread",
"Foundation_NSNumber",
"AppKit",
"AppKit_NSAppearance",
"AppKit_NSApplication",
"AppKit_NSBitmapImageRep",
"AppKit_NSButton",
"AppKit_NSColor",
"AppKit_NSControl",
"AppKit_NSCursor",
"AppKit_NSEvent",
"AppKit_NSGraphicsContext",
"AppKit_NSImage",
"AppKit_NSImageRep",
"AppKit_NSMenu",
"AppKit_NSMenuItem",
"AppKit_NSPasteboard",
"AppKit_NSResponder",
"AppKit_NSScreen",
"AppKit_NSTextInputContext",
"AppKit_NSView",
"AppKit_NSWindow",
"AppKit_NSWindowTabGroup",
] ]
[target.'cfg(target_os = "ios")'.dependencies.objc2-ui-kit] [target.'cfg(target_os = "ios")'.dependencies.icrate]
version = "0.2.2" version = "0.1.0"
features = [ features = [
"UIApplication", "dispatch",
"UIDevice", "Foundation",
"UIEvent", "Foundation_NSArray",
"UIGeometry", "Foundation_NSString",
"UIGestureRecognizer", "Foundation_NSProcessInfo",
"UIOrientation", "Foundation_NSThread",
"UIPanGestureRecognizer", "Foundation_NSSet",
"UIPinchGestureRecognizer",
"UIResponder",
"UIRotationGestureRecognizer",
"UIScreen",
"UIScreenMode",
"UITapGestureRecognizer",
"UITouch",
"UITraitCollection",
"UIView",
"UIViewController",
"UIWindow",
] ]
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
unicode-segmentation = "1.7.1" unicode-segmentation = "1.7.1"
[target.'cfg(target_os = "windows")'.dependencies.windows-sys] [target.'cfg(target_os = "windows")'.dependencies.windows-sys]
version = "0.52.0" version = "0.48"
features = [ features = [
"Win32_Devices_HumanInterfaceDevice", "Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation", "Win32_Foundation",
@@ -242,35 +181,15 @@ calloop = "0.12.3"
libc = "0.2.64" libc = "0.2.64"
memmap2 = { version = "0.9.0", optional = true } memmap2 = { version = "0.9.0", optional = true }
percent-encoding = { version = "2.0", optional = true } percent-encoding = { version = "2.0", optional = true }
rustix = { version = "0.38.4", default-features = false, features = [ rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "thread", "process"] }
"std", sctk = { package = "smithay-client-toolkit", version = "0.18.0", default-features = false, features = ["calloop"], optional = true }
"system", sctk-adwaita = { version = "0.8.0", default_features = false, optional = true }
"thread", wayland-backend = { version = "0.3.0", default_features = false, features = ["client_system"], optional = true }
"process",
] }
sctk = { package = "smithay-client-toolkit", version = "0.18.0", default-features = false, features = [
"calloop",
], optional = true }
sctk-adwaita = { version = "0.9.0", default_features = false, optional = true }
wayland-backend = { version = "0.3.0", default_features = false, features = [
"client_system",
], optional = true }
wayland-client = { version = "0.31.1", optional = true } wayland-client = { version = "0.31.1", optional = true }
wayland-protocols = { version = "0.31.0", features = [ wayland-protocols = { version = "0.31.0", features = [ "staging"], optional = true }
"staging", wayland-protocols-plasma = { version = "0.2.0", features = [ "client" ], optional = true }
], optional = true }
wayland-protocols-plasma = { version = "0.2.0", features = [
"client",
], optional = true }
x11-dl = { version = "2.19.1", optional = true } x11-dl = { version = "2.19.1", optional = true }
x11rb = { version = "0.13.0", default-features = false, features = [ x11rb = { version = "0.13.0", default-features = false, features = ["allow-unsafe-code", "dl-libxcb", "randr", "resource_manager", "xinput", "xkb"], optional = true }
"allow-unsafe-code",
"dl-libxcb",
"randr",
"resource_manager",
"xinput",
"xkb",
], optional = true }
xkbcommon-dl = "0.4.2" xkbcommon-dl = "0.4.2"
[target.'cfg(target_os = "redox")'.dependencies] [target.'cfg(target_os = "redox")'.dependencies]
@@ -334,8 +253,8 @@ atomic-waker = "1"
concurrent-queue = { version = "2", default-features = false } concurrent-queue = { version = "2", default-features = false }
[target.'cfg(target_family = "wasm")'.dev-dependencies] [target.'cfg(target_family = "wasm")'.dev-dependencies]
console_error_panic_hook = "0.1" console_log = "1"
tracing-web = "0.1" web-sys = { version = "0.3.22", features = ['CanvasRenderingContext2d'] }
[[example]] [[example]]
doc-scrape-examples = true doc-scrape-examples = true
@@ -343,7 +262,13 @@ name = "window"
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["dpi"] members = [
"dpi",
"it/common-tests",
"it/gui-test",
"it/gui-test-runner",
"run-wasm",
]
[workspace.package] [workspace.package]
rust-version = "1.70.0" rust-version = "1.70.0"
@@ -352,5 +277,9 @@ license = "Apache-2.0"
edition = "2021" edition = "2021"
[workspace.dependencies] [workspace.dependencies]
serde = { version = "1", features = ["serde_derive"] } async-io = "2.3.1"
gui-test = { path = "it/gui-test" }
mint = "0.5.6" mint = "0.5.6"
serde = { version = "1", features = ["serde_derive"] }
serde_json = "1.0.114"
winit = { path = "." }

View File

@@ -179,7 +179,7 @@ Legend:
|Window decorations |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|✔️ | |Window decorations |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|✔️ |
|Window decorations toggle |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** | |Window decorations toggle |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** |
|Window resizing |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|✔️ |✔️ | |Window resizing |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|✔️ |✔️ |
|Window resize increments |✔️ |✔️ |✔️ |❌ |**N/A**|**N/A**|**N/A**|**N/A** | |Window resize increments | |✔️ |✔️ |❌ |**N/A**|**N/A**|**N/A**|**N/A** |
|Window transparency |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|N/A |✔️ | |Window transparency |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|N/A |✔️ |
|Window blur |❌ |❌ |❌ |✔️ |**N/A**|**N/A**|N/A |❌ | |Window blur |❌ |❌ |❌ |✔️ |**N/A**|**N/A**|N/A |❌ |
|Window maximization |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** | |Window maximization |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** |

View File

@@ -8,7 +8,7 @@
```toml ```toml
[dependencies] [dependencies]
winit = "0.30.2" winit = "0.29.15"
``` ```
## [Documentation](https://docs.rs/winit) ## [Documentation](https://docs.rs/winit)

View File

@@ -1,10 +1,10 @@
use cfg_aliases::cfg_aliases; use cfg_aliases::cfg_aliases;
fn main() { fn main() {
// The script doesn't depend on our code. // The script doesn't depend on our code
println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=build.rs");
// Setup cfg aliases. // Setup cfg aliases
cfg_aliases! { cfg_aliases! {
// Systems. // Systems.
android_platform: { target_os = "android" }, android_platform: { target_os = "android" },
@@ -21,7 +21,4 @@ fn main() {
wayland_platform: { all(feature = "wayland", free_unix, not(redox)) }, wayland_platform: { all(feature = "wayland", free_unix, not(redox)) },
orbital_platform: { redox }, orbital_platform: { redox },
} }
// Winit defined cfgs.
println!("cargo:rustc-check-cfg=cfg(unreleased_changelogs)");
} }

View File

@@ -10,6 +10,6 @@ disallowed-methods = [
{ path = "web_sys::Element::request_fullscreen", reason = "Doesn't account for compatibility with Safari" }, { path = "web_sys::Element::request_fullscreen", reason = "Doesn't account for compatibility with Safari" },
{ path = "web_sys::Document::exit_fullscreen", reason = "Doesn't account for compatibility with Safari" }, { path = "web_sys::Document::exit_fullscreen", reason = "Doesn't account for compatibility with Safari" },
{ path = "web_sys::Document::fullscreen_element", reason = "Doesn't account for compatibility with Safari" }, { path = "web_sys::Document::fullscreen_element", reason = "Doesn't account for compatibility with Safari" },
{ path = "objc2_app_kit::NSView::visibleRect", reason = "We expose a render target to the user, and visibility is not really relevant to that (and can break if you don't use the rectangle position as well). Use `frame` instead." }, { path = "icrate::AppKit::NSView::visibleRect", reason = "We expose a render target to the user, and visibility is not really relevant to that (and can break if you don't use the rectangle position as well). Use `frame` instead." },
{ path = "objc2_app_kit::NSWindow::setFrameTopLeftPoint", reason = "Not sufficient when working with Winit's coordinate system, use `flip_window_screen_coordinates` instead" }, { path = "icrate::AppKit::NSWindow::setFrameTopLeftPoint", reason = "Not sufficient when working with Winit's coordinate system, use `flip_window_screen_coordinates` instead" },
] ]

View File

@@ -0,0 +1,40 @@
# syntax=docker/dockerfile:1
# Copyright 2024 The Winit Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
ARG DISTRO=ubuntu
ARG DISTRO_VERSION=22.04
FROM "${DISTRO}":"${DISTRO_VERSION}"
SHELL ["/bin/bash", "-eEuxo", "pipefail", "-c"]
ARG DEBIAN_FRONTEND=noninteractive
RUN \
apt-get -o Acquire::Retries=10 -qq update && \
apt-get -o Acquire::Retries=10 -o Dpkg::Use-Pty=0 install -y --no-install-recommends \
cargo \
ca-certificates \
libx11-dev \
libxcursor-dev \
libxcb1-dev \
libxi-dev \
libxkbcommon-dev \
libxkbcommon-x11-dev \
xvfb && \
rm -rf \
/var/lib/apt/lists/* \
/var/cache/* \
/var/log/* \
/usr/share/{doc,man}

View File

@@ -35,9 +35,9 @@
//! //!
//! ### Position and Size types //! ### Position and Size types
//! //!
//! The [`PhysicalPosition`] / [`PhysicalSize`] / [`PhysicalUnit`] types correspond with the actual //! The [`PhysicalPosition`] / [`PhysicalSize`] / [`PhysicalUnit`] types correspond with the actual pixels on the
//! pixels on the device, and the [`LogicalPosition`] / [`LogicalSize`] / [`LogicalUnit`] types //! device, and the [`LogicalPosition`] / [`LogicalSize`] / [`LogicalUnit`] types correspond to the physical pixels
//! correspond to the physical pixels divided by the scale factor. //! divided by the scale factor.
//! //!
//! The position and size types are generic over their exact pixel type, `P`, to allow the //! The position and size types are generic over their exact pixel type, `P`, to allow the
//! API to have integer precision where appropriate (e.g. most window manipulation functions) and //! API to have integer precision where appropriate (e.g. most window manipulation functions) and
@@ -52,14 +52,19 @@
//! //!
//! This crate provides the following Cargo features: //! This crate provides the following Cargo features:
//! //!
//! * `serde`: Enables serialization/deserialization of certain types with [Serde](https://crates.io/crates/serde). //! * `serde`: Enables serialization/deserialization of certain types with
//! [Serde](https://crates.io/crates/serde).
//! * `mint`: Enables mint (math interoperability standard types) conversions. //! * `mint`: Enables mint (math interoperability standard types) conversions.
//! //!
//! //!
//! [points]: https://en.wikipedia.org/wiki/Point_(typography) //! [points]: https://en.wikipedia.org/wiki/Point_(typography)
//! [picas]: https://en.wikipedia.org/wiki/Pica_(typography) //! [picas]: https://en.wikipedia.org/wiki/Pica_(typography)
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide), doc(cfg_hide(doc, docsrs)))] #![cfg_attr(
docsrs,
feature(doc_auto_cfg, doc_cfg_hide),
doc(cfg_hide(doc, docsrs))
)]
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
@@ -115,9 +120,9 @@ impl Pixel for f64 {
/// Checks that the scale factor is a normal positive `f64`. /// Checks that the scale factor is a normal positive `f64`.
/// ///
/// All functions that take a scale factor assert that this will return `true`. If you're sourcing /// All functions that take a scale factor assert that this will return `true`. If you're sourcing scale factors from
/// scale factors from anywhere other than winit, it's recommended to validate them using this /// anywhere other than winit, it's recommended to validate them using this function before passing them to winit;
/// function before passing them to winit; otherwise, you risk panics. /// otherwise, you risk panics.
#[inline] #[inline]
pub fn validate_scale_factor(scale_factor: f64) -> bool { pub fn validate_scale_factor(scale_factor: f64) -> bool {
scale_factor.is_sign_positive() && scale_factor.is_normal() scale_factor.is_sign_positive() && scale_factor.is_normal()
@@ -129,12 +134,12 @@ pub fn validate_scale_factor(scale_factor: f64) -> bool {
pub struct LogicalUnit<P>(pub P); pub struct LogicalUnit<P>(pub P);
impl<P> LogicalUnit<P> { impl<P> LogicalUnit<P> {
/// Represents a maximum logical unit that is equal to [`f64::MAX`].
pub const MAX: LogicalUnit<f64> = LogicalUnit::new(f64::MAX);
/// Represents a minimum logical unit of [`f64::MAX`]. /// Represents a minimum logical unit of [`f64::MAX`].
pub const MIN: LogicalUnit<f64> = LogicalUnit::new(f64::MIN); pub const MIN: LogicalUnit<f64> = LogicalUnit::new(f64::MIN);
/// Represents a logical unit of `0_f64`. /// Represents a logical unit of `0_f64`.
pub const ZERO: LogicalUnit<f64> = LogicalUnit::new(0.0); pub const ZERO: LogicalUnit<f64> = LogicalUnit::new(0.0);
/// Represents a maximum logical unit that is equal to [`f64::MAX`].
pub const MAX: LogicalUnit<f64> = LogicalUnit::new(f64::MAX);
#[inline] #[inline]
pub const fn new(v: P) -> Self { pub const fn new(v: P) -> Self {
@@ -223,12 +228,12 @@ impl<P: Pixel> From<LogicalUnit<P>> for f64 {
pub struct PhysicalUnit<P>(pub P); pub struct PhysicalUnit<P>(pub P);
impl<P> PhysicalUnit<P> { impl<P> PhysicalUnit<P> {
/// Represents a maximum physical unit that is equal to [`f64::MAX`].
pub const MAX: LogicalUnit<f64> = LogicalUnit::new(f64::MAX);
/// Represents a minimum physical unit of [`f64::MAX`]. /// Represents a minimum physical unit of [`f64::MAX`].
pub const MIN: LogicalUnit<f64> = LogicalUnit::new(f64::MIN); pub const MIN: LogicalUnit<f64> = LogicalUnit::new(f64::MIN);
/// Represents a physical unit of `0_f64`. /// Represents a physical unit of `0_f64`.
pub const ZERO: LogicalUnit<f64> = LogicalUnit::new(0.0); pub const ZERO: LogicalUnit<f64> = LogicalUnit::new(0.0);
/// Represents a maximum physical unit that is equal to [`f64::MAX`].
pub const MAX: LogicalUnit<f64> = LogicalUnit::new(f64::MAX);
#[inline] #[inline]
pub const fn new(v: P) -> Self { pub const fn new(v: P) -> Self {
@@ -317,12 +322,12 @@ pub enum PixelUnit {
} }
impl PixelUnit { impl PixelUnit {
/// Represents a maximum logical unit that is equal to [`f64::MAX`].
pub const MAX: PixelUnit = PixelUnit::Logical(LogicalUnit::new(f64::MAX));
/// Represents a minimum logical unit of [`f64::MAX`]. /// Represents a minimum logical unit of [`f64::MAX`].
pub const MIN: PixelUnit = PixelUnit::Logical(LogicalUnit::new(f64::MIN)); pub const MIN: PixelUnit = PixelUnit::Logical(LogicalUnit::new(f64::MIN));
/// Represents a logical unit of `0_f64`. /// Represents a logical unit of `0_f64`.
pub const ZERO: PixelUnit = PixelUnit::Logical(LogicalUnit::new(0.0)); pub const ZERO: PixelUnit = PixelUnit::Logical(LogicalUnit::new(0.0));
/// Represents a maximum logical unit that is equal to [`f64::MAX`].
pub const MAX: PixelUnit = PixelUnit::Logical(LogicalUnit::new(f64::MAX));
pub fn new<S: Into<PixelUnit>>(unit: S) -> PixelUnit { pub fn new<S: Into<PixelUnit>>(unit: S) -> PixelUnit {
unit.into() unit.into()
@@ -395,7 +400,10 @@ impl<P: Pixel> LogicalPosition<P> {
#[inline] #[inline]
pub fn cast<X: Pixel>(&self) -> LogicalPosition<X> { pub fn cast<X: Pixel>(&self) -> LogicalPosition<X> {
LogicalPosition { x: self.x.cast(), y: self.y.cast() } LogicalPosition {
x: self.x.cast(),
y: self.y.cast(),
}
} }
} }
@@ -471,7 +479,10 @@ impl<P: Pixel> PhysicalPosition<P> {
#[inline] #[inline]
pub fn cast<X: Pixel>(&self) -> PhysicalPosition<X> { pub fn cast<X: Pixel>(&self) -> PhysicalPosition<X> {
PhysicalPosition { x: self.x.cast(), y: self.y.cast() } PhysicalPosition {
x: self.x.cast(),
y: self.y.cast(),
}
} }
} }
@@ -547,7 +558,10 @@ impl<P: Pixel> LogicalSize<P> {
#[inline] #[inline]
pub fn cast<X: Pixel>(&self) -> LogicalSize<X> { pub fn cast<X: Pixel>(&self) -> LogicalSize<X> {
LogicalSize { width: self.width.cast(), height: self.height.cast() } LogicalSize {
width: self.width.cast(),
height: self.height.cast(),
}
} }
} }
@@ -585,7 +599,10 @@ impl<P: Pixel> From<mint::Vector2<P>> for LogicalSize<P> {
#[cfg(feature = "mint")] #[cfg(feature = "mint")]
impl<P: Pixel> From<LogicalSize<P>> for mint::Vector2<P> { impl<P: Pixel> From<LogicalSize<P>> for mint::Vector2<P> {
fn from(s: LogicalSize<P>) -> Self { fn from(s: LogicalSize<P>) -> Self {
mint::Vector2 { x: s.width, y: s.height } mint::Vector2 {
x: s.width,
y: s.height,
}
} }
} }
@@ -620,7 +637,10 @@ impl<P: Pixel> PhysicalSize<P> {
#[inline] #[inline]
pub fn cast<X: Pixel>(&self) -> PhysicalSize<X> { pub fn cast<X: Pixel>(&self) -> PhysicalSize<X> {
PhysicalSize { width: self.width.cast(), height: self.height.cast() } PhysicalSize {
width: self.width.cast(),
height: self.height.cast(),
}
} }
} }
@@ -658,7 +678,10 @@ impl<P: Pixel> From<mint::Vector2<P>> for PhysicalSize<P> {
#[cfg(feature = "mint")] #[cfg(feature = "mint")]
impl<P: Pixel> From<PhysicalSize<P>> for mint::Vector2<P> { impl<P: Pixel> From<PhysicalSize<P>> for mint::Vector2<P> {
fn from(s: PhysicalSize<P>) -> Self { fn from(s: PhysicalSize<P>) -> Self {
mint::Vector2 { x: s.width, y: s.height } mint::Vector2 {
x: s.width,
y: s.height,
}
} }
} }
@@ -823,7 +846,12 @@ mod tests {
macro_rules! assert_approx_eq { macro_rules! assert_approx_eq {
($a:expr, $b:expr $(,)?) => { ($a:expr, $b:expr $(,)?) => {
assert!(($a - $b).abs() < 0.001, "{} is not approximately equal to {}", $a, $b); assert!(
($a - $b).abs() < 0.001,
"{} is not approximately equal to {}",
$a,
$b
);
}; };
} }
@@ -942,8 +970,14 @@ mod tests {
assert_eq!(log_unit.to_physical::<u32>(1.0), PhysicalUnit::new(1)); assert_eq!(log_unit.to_physical::<u32>(1.0), PhysicalUnit::new(1));
assert_eq!(log_unit.to_physical::<u32>(2.0), PhysicalUnit::new(2)); assert_eq!(log_unit.to_physical::<u32>(2.0), PhysicalUnit::new(2));
assert_eq!(log_unit.cast::<u32>(), LogicalUnit::new(1)); assert_eq!(log_unit.cast::<u32>(), LogicalUnit::new(1));
assert_eq!(log_unit, LogicalUnit::from_physical(PhysicalUnit::new(1.0), 1.0)); assert_eq!(
assert_eq!(log_unit, LogicalUnit::from_physical(PhysicalUnit::new(2.0), 2.0)); log_unit,
LogicalUnit::from_physical(PhysicalUnit::new(1.0), 1.0)
);
assert_eq!(
log_unit,
LogicalUnit::from_physical(PhysicalUnit::new(2.0), 2.0)
);
assert_eq!(LogicalUnit::from(2.0), LogicalUnit::new(2.0)); assert_eq!(LogicalUnit::from(2.0), LogicalUnit::new(2.0));
let x: f64 = log_unit.into(); let x: f64 = log_unit.into();
@@ -952,8 +986,14 @@ mod tests {
#[test] #[test]
fn test_physical_unit() { fn test_physical_unit() {
assert_eq!(PhysicalUnit::from_logical(LogicalUnit::new(1.0), 1.0), PhysicalUnit::new(1)); assert_eq!(
assert_eq!(PhysicalUnit::from_logical(LogicalUnit::new(2.0), 0.5), PhysicalUnit::new(1)); PhysicalUnit::from_logical(LogicalUnit::new(1.0), 1.0),
PhysicalUnit::new(1)
);
assert_eq!(
PhysicalUnit::from_logical(LogicalUnit::new(2.0), 0.5),
PhysicalUnit::new(1)
);
assert_eq!(PhysicalUnit::from(2.0), PhysicalUnit::new(2.0,)); assert_eq!(PhysicalUnit::from(2.0), PhysicalUnit::new(2.0,));
assert_eq!(PhysicalUnit::from(2.0), PhysicalUnit::new(2.0)); assert_eq!(PhysicalUnit::from(2.0), PhysicalUnit::new(2.0));
@@ -967,10 +1007,22 @@ mod tests {
assert_eq!(log_pos.to_physical::<u32>(1.0), PhysicalPosition::new(1, 2)); assert_eq!(log_pos.to_physical::<u32>(1.0), PhysicalPosition::new(1, 2));
assert_eq!(log_pos.to_physical::<u32>(2.0), PhysicalPosition::new(2, 4)); assert_eq!(log_pos.to_physical::<u32>(2.0), PhysicalPosition::new(2, 4));
assert_eq!(log_pos.cast::<u32>(), LogicalPosition::new(1, 2)); assert_eq!(log_pos.cast::<u32>(), LogicalPosition::new(1, 2));
assert_eq!(log_pos, LogicalPosition::from_physical(PhysicalPosition::new(1.0, 2.0), 1.0)); assert_eq!(
assert_eq!(log_pos, LogicalPosition::from_physical(PhysicalPosition::new(2.0, 4.0), 2.0)); log_pos,
assert_eq!(LogicalPosition::from((2.0, 2.0)), LogicalPosition::new(2.0, 2.0)); LogicalPosition::from_physical(PhysicalPosition::new(1.0, 2.0), 1.0)
assert_eq!(LogicalPosition::from([2.0, 3.0]), LogicalPosition::new(2.0, 3.0)); );
assert_eq!(
log_pos,
LogicalPosition::from_physical(PhysicalPosition::new(2.0, 4.0), 2.0)
);
assert_eq!(
LogicalPosition::from((2.0, 2.0)),
LogicalPosition::new(2.0, 2.0)
);
assert_eq!(
LogicalPosition::from([2.0, 3.0]),
LogicalPosition::new(2.0, 3.0)
);
let x: (f64, f64) = log_pos.into(); let x: (f64, f64) = log_pos.into();
assert_eq!(x, (1.0, 2.0)); assert_eq!(x, (1.0, 2.0));
@@ -988,8 +1040,14 @@ mod tests {
PhysicalPosition::from_logical(LogicalPosition::new(2.0, 4.0), 0.5), PhysicalPosition::from_logical(LogicalPosition::new(2.0, 4.0), 0.5),
PhysicalPosition::new(1, 2) PhysicalPosition::new(1, 2)
); );
assert_eq!(PhysicalPosition::from((2.0, 2.0)), PhysicalPosition::new(2.0, 2.0)); assert_eq!(
assert_eq!(PhysicalPosition::from([2.0, 3.0]), PhysicalPosition::new(2.0, 3.0)); PhysicalPosition::from((2.0, 2.0)),
PhysicalPosition::new(2.0, 2.0)
);
assert_eq!(
PhysicalPosition::from([2.0, 3.0]),
PhysicalPosition::new(2.0, 3.0)
);
let x: (f64, f64) = PhysicalPosition::new(1, 2).into(); let x: (f64, f64) = PhysicalPosition::new(1, 2).into();
assert_eq!(x, (1.0, 2.0)); assert_eq!(x, (1.0, 2.0));
@@ -1003,8 +1061,14 @@ mod tests {
assert_eq!(log_size.to_physical::<u32>(1.0), PhysicalSize::new(1, 2)); assert_eq!(log_size.to_physical::<u32>(1.0), PhysicalSize::new(1, 2));
assert_eq!(log_size.to_physical::<u32>(2.0), PhysicalSize::new(2, 4)); assert_eq!(log_size.to_physical::<u32>(2.0), PhysicalSize::new(2, 4));
assert_eq!(log_size.cast::<u32>(), LogicalSize::new(1, 2)); assert_eq!(log_size.cast::<u32>(), LogicalSize::new(1, 2));
assert_eq!(log_size, LogicalSize::from_physical(PhysicalSize::new(1.0, 2.0), 1.0)); assert_eq!(
assert_eq!(log_size, LogicalSize::from_physical(PhysicalSize::new(2.0, 4.0), 2.0)); log_size,
LogicalSize::from_physical(PhysicalSize::new(1.0, 2.0), 1.0)
);
assert_eq!(
log_size,
LogicalSize::from_physical(PhysicalSize::new(2.0, 4.0), 2.0)
);
assert_eq!(LogicalSize::from((2.0, 2.0)), LogicalSize::new(2.0, 2.0)); assert_eq!(LogicalSize::from((2.0, 2.0)), LogicalSize::new(2.0, 2.0));
assert_eq!(LogicalSize::from([2.0, 3.0]), LogicalSize::new(2.0, 3.0)); assert_eq!(LogicalSize::from([2.0, 3.0]), LogicalSize::new(2.0, 3.0));
@@ -1035,7 +1099,10 @@ mod tests {
#[test] #[test]
fn test_size() { fn test_size() {
assert_eq!(Size::new(PhysicalSize::new(1, 2)), Size::Physical(PhysicalSize::new(1, 2))); assert_eq!(
Size::new(PhysicalSize::new(1, 2)),
Size::Physical(PhysicalSize::new(1, 2))
);
assert_eq!( assert_eq!(
Size::new(LogicalSize::new(1.0, 2.0)), Size::new(LogicalSize::new(1.0, 2.0)),
Size::Logical(LogicalSize::new(1.0, 2.0)) Size::Logical(LogicalSize::new(1.0, 2.0))

View File

@@ -1,4 +1,7 @@
#[cfg(all(feature = "rwh_06", any(x11_platform, macos_platform, windows_platform)))] #[cfg(all(
feature = "rwh_06",
any(x11_platform, macos_platform, windows_platform)
))]
#[allow(deprecated)] #[allow(deprecated)]
fn main() -> Result<(), impl std::error::Error> { fn main() -> Result<(), impl std::error::Error> {
use std::collections::HashMap; use std::collections::HashMap;
@@ -43,22 +46,25 @@ fn main() -> Result<(), impl std::error::Error> {
println!("Parent window id: {parent_window_id:?})"); println!("Parent window id: {parent_window_id:?})");
windows.insert(window.id(), window); windows.insert(window.id(), window);
}, }
Event::WindowEvent { window_id, event } => match event { Event::WindowEvent { window_id, event } => match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
windows.clear(); windows.clear();
event_loop.exit(); event_loop.exit();
}, }
WindowEvent::CursorEntered { device_id: _ } => { WindowEvent::CursorEntered { device_id: _ } => {
// On x11, println when the cursor entered in a window even if the child window // On x11, println when the cursor entered in a window even if the child window is created
// is created by some key inputs. // by some key inputs.
// the child windows are always placed at (0, 0) with size (200, 200) in the // the child windows are always placed at (0, 0) with size (200, 200) in the parent window,
// parent window, so we also can see this log when we move // so we also can see this log when we move the cursor around (200, 200) in parent window.
// the cursor around (200, 200) in parent window.
println!("cursor entered in the window {window_id:?}"); println!("cursor entered in the window {window_id:?}");
}, }
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
event: KeyEvent { state: ElementState::Pressed, .. }, event:
KeyEvent {
state: ElementState::Pressed,
..
},
.. ..
} => { } => {
let parent_window = windows.get(&parent_window_id.unwrap()).unwrap(); let parent_window = windows.get(&parent_window_id.unwrap()).unwrap();
@@ -66,12 +72,12 @@ fn main() -> Result<(), impl std::error::Error> {
let child_id = child_window.id(); let child_id = child_window.id();
println!("Child window created with id: {child_id:?}"); println!("Child window created with id: {child_id:?}");
windows.insert(child_id, child_window); windows.insert(child_id, child_window);
}, }
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
if let Some(window) = windows.get(&window_id) { if let Some(window) = windows.get(&window_id) {
fill::fill_window(window); fill::fill_window(window);
} }
}, }
_ => (), _ => (),
}, },
_ => (), _ => (),
@@ -79,10 +85,10 @@ fn main() -> Result<(), impl std::error::Error> {
}) })
} }
#[cfg(all(feature = "rwh_06", not(any(x11_platform, macos_platform, windows_platform))))] #[cfg(all(
feature = "rwh_06",
not(any(x11_platform, macos_platform, windows_platform))
))]
fn main() { fn main() {
panic!( panic!("This example is supported only on x11, macOS, and Windows, with the `rwh_06` feature enabled.");
"This example is supported only on x11, macOS, and Windows, with the `rwh_06` feature \
enabled."
);
} }

View File

@@ -4,7 +4,6 @@ use std::thread;
#[cfg(not(web_platform))] #[cfg(not(web_platform))]
use std::time; use std::time;
use ::tracing::{info, warn};
#[cfg(web_platform)] #[cfg(web_platform)]
use web_time as time; use web_time as time;
@@ -16,8 +15,6 @@ use winit::window::{Window, WindowId};
#[path = "util/fill.rs"] #[path = "util/fill.rs"]
mod fill; mod fill;
#[path = "util/tracing.rs"]
mod tracing;
const WAIT_TIME: time::Duration = time::Duration::from_millis(100); const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100); const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
@@ -31,16 +28,13 @@ enum Mode {
} }
fn main() -> Result<(), impl std::error::Error> { fn main() -> Result<(), impl std::error::Error> {
#[cfg(web_platform)] tracing_subscriber::fmt::init();
console_error_panic_hook::set_once();
tracing::init(); println!("Press '1' to switch to Wait mode.");
println!("Press '2' to switch to WaitUntil mode.");
info!("Press '1' to switch to Wait mode."); println!("Press '3' to switch to Poll mode.");
info!("Press '2' to switch to WaitUntil mode."); println!("Press 'R' to toggle request_redraw() calls.");
info!("Press '3' to switch to Poll mode."); println!("Press 'Esc' to close the window.");
info!("Press 'R' to toggle request_redraw() calls.");
info!("Press 'Esc' to close the window.");
let event_loop = EventLoop::new().unwrap(); let event_loop = EventLoop::new().unwrap();
@@ -59,7 +53,7 @@ struct ControlFlowDemo {
impl ApplicationHandler for ControlFlowDemo { impl ApplicationHandler for ControlFlowDemo {
fn new_events(&mut self, _event_loop: &ActiveEventLoop, cause: StartCause) { fn new_events(&mut self, _event_loop: &ActiveEventLoop, cause: StartCause) {
info!("new_events: {cause:?}"); println!("new_events: {cause:?}");
self.wait_cancelled = match cause { self.wait_cancelled = match cause {
StartCause::WaitCancelled { .. } => self.mode == Mode::WaitUntil, StartCause::WaitCancelled { .. } => self.mode == Mode::WaitUntil,
@@ -80,44 +74,49 @@ impl ApplicationHandler for ControlFlowDemo {
_window_id: WindowId, _window_id: WindowId,
event: WindowEvent, event: WindowEvent,
) { ) {
info!("{event:?}"); println!("{event:?}");
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
self.close_requested = true; self.close_requested = true;
}, }
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
event: KeyEvent { logical_key: key, state: ElementState::Pressed, .. }, event:
KeyEvent {
logical_key: key,
state: ElementState::Pressed,
..
},
.. ..
} => match key.as_ref() { } => match key.as_ref() {
// WARNING: Consider using `key_without_modifiers()` if available on your platform. // WARNING: Consider using `key_without_modifiers()` if available on your platform.
// See the `key_binding` example // See the `key_binding` example
Key::Character("1") => { Key::Character("1") => {
self.mode = Mode::Wait; self.mode = Mode::Wait;
warn!("mode: {:?}", self.mode); println!("\nmode: {:?}\n", self.mode);
}, }
Key::Character("2") => { Key::Character("2") => {
self.mode = Mode::WaitUntil; self.mode = Mode::WaitUntil;
warn!("mode: {:?}", self.mode); println!("\nmode: {:?}\n", self.mode);
}, }
Key::Character("3") => { Key::Character("3") => {
self.mode = Mode::Poll; self.mode = Mode::Poll;
warn!("mode: {:?}", self.mode); println!("\nmode: {:?}\n", self.mode);
}, }
Key::Character("r") => { Key::Character("r") => {
self.request_redraw = !self.request_redraw; self.request_redraw = !self.request_redraw;
warn!("request_redraw: {}", self.request_redraw); println!("\nrequest_redraw: {}\n", self.request_redraw);
}, }
Key::Named(NamedKey::Escape) => { Key::Named(NamedKey::Escape) => {
self.close_requested = true; self.close_requested = true;
}, }
_ => (), _ => (),
}, },
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
let window = self.window.as_ref().unwrap(); let window = self.window.as_ref().unwrap();
window.pre_present_notify(); window.pre_present_notify();
fill::fill_window(window); fill::fill_window(window);
}, }
_ => (), _ => (),
} }
} }
@@ -134,11 +133,11 @@ impl ApplicationHandler for ControlFlowDemo {
event_loop event_loop
.set_control_flow(ControlFlow::WaitUntil(time::Instant::now() + WAIT_TIME)); .set_control_flow(ControlFlow::WaitUntil(time::Instant::now() + WAIT_TIME));
} }
}, }
Mode::Poll => { Mode::Poll => {
thread::sleep(POLL_SLEEP_TIME); thread::sleep(POLL_SLEEP_TIME);
event_loop.set_control_flow(ControlFlow::Poll); event_loop.set_control_flow(ControlFlow::Poll);
}, }
}; };
if self.close_requested { if self.close_requested {

View File

@@ -1,11 +1,15 @@
#![allow(clippy::single_match)] #![allow(clippy::single_match)]
// Limit this example to only compatible platforms. // Limit this example to only compatible platforms.
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, android_platform,))] #[cfg(any(
windows_platform,
macos_platform,
x11_platform,
wayland_platform,
android_platform,
))]
fn main() -> std::process::ExitCode { fn main() -> std::process::ExitCode {
use std::process::ExitCode; use std::{process::ExitCode, thread::sleep, time::Duration};
use std::thread::sleep;
use std::time::Duration;
use winit::application::ApplicationHandler; use winit::application::ApplicationHandler;
use winit::event::WindowEvent; use winit::event::WindowEvent;
@@ -45,7 +49,7 @@ fn main() -> std::process::ExitCode {
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
fill::fill_window(window); fill::fill_window(window);
window.request_redraw(); window.request_redraw();
}, }
_ => (), _ => (),
} }
} }

View File

@@ -60,17 +60,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
println!( println!("--------------------------------------------------------- Window {} CloseRequested", self.idx);
"--------------------------------------------------------- Window {} \
CloseRequested",
self.idx
);
fill::cleanup_window(window); fill::cleanup_window(window);
self.window = None; self.window = None;
}, }
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
fill::fill_window(window); fill::fill_window(window);
}, }
_ => (), _ => (),
} }
} }
@@ -80,7 +76,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut event_loop = EventLoop::new().unwrap(); let mut event_loop = EventLoop::new().unwrap();
let mut app = App { idx: 1, ..Default::default() }; let mut app = App {
idx: 1,
..Default::default()
};
event_loop.run_app_on_demand(&mut app)?; event_loop.run_app_on_demand(&mut app)?;
println!("--------------------------------------------------------- Finished first loop"); println!("--------------------------------------------------------- Finished first loop");

View File

@@ -15,12 +15,12 @@ pub use platform::fill_window;
mod platform { mod platform {
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::mem;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use softbuffer::{Context, Surface}; use softbuffer::{Context, Surface};
use winit::window::{Window, WindowId}; use winit::window::Window;
use winit::window::WindowId;
thread_local! { thread_local! {
// NOTE: You should never do things like that, create context and drop it before // NOTE: You should never do things like that, create context and drop it before
@@ -34,32 +34,24 @@ mod platform {
/// The graphics context used to draw to a window. /// The graphics context used to draw to a window.
struct GraphicsContext { struct GraphicsContext {
/// The global softbuffer context. /// The global softbuffer context.
context: RefCell<Context<&'static Window>>, context: Context,
/// The hash map of window IDs to surfaces. /// The hash map of window IDs to surfaces.
surfaces: HashMap<WindowId, Surface<&'static Window, &'static Window>>, surfaces: HashMap<WindowId, Surface>,
} }
impl GraphicsContext { impl GraphicsContext {
fn new(w: &Window) -> Self { fn new(w: &Window) -> Self {
Self { Self {
context: RefCell::new( context: unsafe { Context::new(w) }.expect("Failed to create a softbuffer context"),
Context::new(unsafe { mem::transmute::<&'_ Window, &'static Window>(w) })
.expect("Failed to create a softbuffer context"),
),
surfaces: HashMap::new(), surfaces: HashMap::new(),
} }
} }
fn create_surface( fn create_surface(&mut self, window: &Window) -> &mut Surface {
&mut self,
window: &Window,
) -> &mut Surface<&'static Window, &'static Window> {
self.surfaces.entry(window.id()).or_insert_with(|| { self.surfaces.entry(window.id()).or_insert_with(|| {
Surface::new(&self.context.borrow(), unsafe { unsafe { Surface::new(&self.context, window) }
mem::transmute::<&'_ Window, &'static Window>(window) .expect("Failed to create a softbuffer surface")
})
.expect("Failed to create a softbuffer surface")
}) })
} }
@@ -79,17 +71,24 @@ mod platform {
// Either get the last context used or create a new one. // Either get the last context used or create a new one.
let mut gc = gc.borrow_mut(); let mut gc = gc.borrow_mut();
let surface = let surface = gc
gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window); .get_or_insert_with(|| GraphicsContext::new(window))
.create_surface(window);
// Fill a buffer with a solid color. // Fill a buffer with a solid color.
const DARK_GRAY: u32 = 0xff181818; const DARK_GRAY: u32 = 0xFF181818;
surface.resize(width, height).expect("Failed to resize the softbuffer surface"); surface
.resize(width, height)
.expect("Failed to resize the softbuffer surface");
let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer"); let mut buffer = surface
.buffer_mut()
.expect("Failed to get the softbuffer buffer");
buffer.fill(DARK_GRAY); buffer.fill(DARK_GRAY);
buffer.present().expect("Failed to present the softbuffer buffer"); buffer
.present()
.expect("Failed to present the softbuffer buffer");
}) })
} }

View File

@@ -1,25 +0,0 @@
#[cfg(not(web_platform))]
pub fn init() {
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder().with_default_directive(LevelFilter::INFO.into()).from_env_lossy(),
)
.init();
}
#[cfg(web_platform)]
pub fn init() {
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.without_time()
.with_writer(tracing_web::MakeWebConsoleWriter::new()),
)
.init();
}

View File

@@ -2,28 +2,29 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::sync::Arc; use std::path::Path;
use std::{fmt, mem};
use ::tracing::{error, info};
use cursor_icon::CursorIcon; use cursor_icon::CursorIcon;
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
use rwh_06::{DisplayHandle, HasDisplayHandle}; use rwh_05::HasRawDisplayHandle;
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
use softbuffer::{Context, Surface}; use softbuffer::{Context, Surface};
use winit::application::ApplicationHandler; use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize}; use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
use winit::event::{DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent}; use winit::event::{DeviceEvent, DeviceId, Ime, WindowEvent};
use winit::event::{MouseButton, MouseScrollDelta};
use winit::event_loop::{ActiveEventLoop, EventLoop}; use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::keyboard::{Key, ModifiersState}; use winit::keyboard::{Key, ModifiersState};
use winit::window::{ use winit::window::{
Cursor, CursorGrabMode, CustomCursor, CustomCursorSource, Fullscreen, Icon, ResizeDirection, Cursor, CursorGrabMode, CustomCursor, CustomCursorSource, Fullscreen, Icon, ResizeDirection,
Theme, Window, WindowId, Theme,
}; };
use winit::window::{Window, WindowId};
#[cfg(macos_platform)] #[cfg(macos_platform)]
use winit::platform::macos::{OptionAsAlt, WindowAttributesExtMacOS, WindowExtMacOS}; use winit::platform::macos::{OptionAsAlt, WindowAttributesExtMacOS, WindowExtMacOS};
@@ -32,18 +33,10 @@ use winit::platform::startup_notify::{
self, EventLoopExtStartupNotify, WindowAttributesExtStartupNotify, WindowExtStartupNotify, self, EventLoopExtStartupNotify, WindowAttributesExtStartupNotify, WindowExtStartupNotify,
}; };
#[path = "util/tracing.rs"]
mod tracing;
/// The amount of points to around the window for drag resize direction calculations. /// The amount of points to around the window for drag resize direction calculations.
const BORDER_SIZE: f64 = 20.; const BORDER_SIZE: f64 = 20.;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
#[cfg(web_platform)]
console_error_panic_hook::set_once();
tracing::init();
let event_loop = EventLoop::<UserEvent>::with_user_event().build()?; let event_loop = EventLoop::<UserEvent>::with_user_event().build()?;
let _event_loop_proxy = event_loop.create_proxy(); let _event_loop_proxy = event_loop.create_proxy();
@@ -52,7 +45,7 @@ fn main() -> Result<(), Box<dyn Error>> {
std::thread::spawn(move || { std::thread::spawn(move || {
// Wake up the `event_loop` once every second and dispatch a custom event // Wake up the `event_loop` once every second and dispatch a custom event
// from a different thread. // from a different thread.
info!("Starting to send user event every second"); println!("Starting to send user event every second");
loop { loop {
let _ = _event_loop_proxy.send_event(UserEvent::WakeUp); let _ = _event_loop_proxy.send_event(UserEvent::WakeUp);
std::thread::sleep(std::time::Duration::from_secs(1)); std::thread::sleep(std::time::Duration::from_secs(1));
@@ -81,30 +74,24 @@ struct Application {
/// ///
/// With OpenGL it could be EGLDisplay. /// With OpenGL it could be EGLDisplay.
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
context: Option<Context<DisplayHandle<'static>>>, context: Option<Context>,
} }
impl Application { impl Application {
fn new<T>(event_loop: &EventLoop<T>) -> Self { fn new<T>(event_loop: &EventLoop<T>) -> Self {
// SAFETY: we drop the context right before the event loop is stopped, thus making it safe. // SAFETY: we drop the context right before the event loop is stopped, thus making it safe.
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
let context = Some( let context = Some(unsafe { Context::from_raw(event_loop.raw_display_handle()).unwrap() });
Context::new(unsafe {
std::mem::transmute::<DisplayHandle<'_>, DisplayHandle<'static>>(
event_loop.display_handle().unwrap(),
)
})
.unwrap(),
);
// You'll have to choose an icon size at your own discretion. On X11, the desired size // You'll have to choose an icon size at your own discretion. On X11, the desired size varies
// varies by WM, and on Windows, you still have to account for screen scaling. Here // by WM, and on Windows, you still have to account for screen scaling. Here we use 32px,
// we use 32px, since it seems to work well enough in most cases. Be careful about // since it seems to work well enough in most cases. Be careful about going too high, or
// going too high, or you'll be bitten by the low-quality downscaling built into the // you'll be bitten by the low-quality downscaling built into the WM.
// WM. let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/data/icon.png");
let icon = load_icon(include_bytes!("data/icon.png"));
info!("Loading cursor assets"); let icon = load_icon(Path::new(path));
println!("Loading cursor assets");
let custom_cursors = vec![ let custom_cursors = vec![
event_loop.create_custom_cursor(decode_cursor(include_bytes!("data/cross.png"))), event_loop.create_custom_cursor(decode_cursor(include_bytes!("data/cross.png"))),
event_loop.create_custom_cursor(decode_cursor(include_bytes!("data/cross2.png"))), event_loop.create_custom_cursor(decode_cursor(include_bytes!("data/cross2.png"))),
@@ -136,7 +123,7 @@ impl Application {
#[cfg(any(x11_platform, wayland_platform))] #[cfg(any(x11_platform, wayland_platform))]
if let Some(token) = event_loop.read_token_from_env() { if let Some(token) = event_loop.read_token_from_env() {
startup_notify::reset_activation_token_env(); startup_notify::reset_activation_token_env();
info!("Using token {:?} to activate a window", token); println!("Using token {:?} to activate a window", token);
window_attributes = window_attributes.with_activation_token(token); window_attributes = window_attributes.with_activation_token(token);
} }
@@ -145,12 +132,6 @@ impl Application {
window_attributes = window_attributes.with_tabbing_identifier(&tab_id); window_attributes = window_attributes.with_tabbing_identifier(&tab_id);
} }
#[cfg(web_platform)]
{
use winit::platform::web::WindowAttributesExtWebSys;
window_attributes = window_attributes.with_append(true);
}
let window = event_loop.create_window(window_attributes)?; let window = event_loop.create_window(window_attributes)?;
#[cfg(ios_platform)] #[cfg(ios_platform)]
@@ -159,12 +140,11 @@ impl Application {
window.recognize_doubletap_gesture(true); window.recognize_doubletap_gesture(true);
window.recognize_pinch_gesture(true); window.recognize_pinch_gesture(true);
window.recognize_rotation_gesture(true); window.recognize_rotation_gesture(true);
window.recognize_pan_gesture(true, 2, 2);
} }
let window_state = WindowState::new(self, window)?; let window_state = WindowState::new(self, window)?;
let window_id = window_state.window.id(); let window_id = window_state.window.id();
info!("Created new window with id={window_id:?}"); println!("Created new window with id={window_id:?}");
self.windows.insert(window_id, window_state); self.windows.insert(window_id, window_state);
Ok(window_id) Ok(window_id)
} }
@@ -172,23 +152,23 @@ impl Application {
fn handle_action(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, action: Action) { fn handle_action(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, action: Action) {
// let cursor_position = self.cursor_position; // let cursor_position = self.cursor_position;
let window = self.windows.get_mut(&window_id).unwrap(); let window = self.windows.get_mut(&window_id).unwrap();
info!("Executing action: {action:?}"); println!("Executing action: {action:?}");
match action { match action {
Action::CloseWindow => { Action::CloseWindow => {
let _ = self.windows.remove(&window_id); let _ = self.windows.remove(&window_id);
}, }
Action::CreateNewWindow => { Action::CreateNewWindow => {
#[cfg(any(x11_platform, wayland_platform))] #[cfg(any(x11_platform, wayland_platform))]
if let Err(err) = window.window.request_activation_token() { if let Err(err) = window.window.request_activation_token() {
info!("Failed to get activation token: {err}"); println!("Failed to get activation token: {err}");
} else { } else {
return; return;
} }
if let Err(err) = self.create_window(event_loop, None) { if let Err(err) = self.create_window(event_loop, None) {
error!("Error creating new window: {err}"); eprintln!("Error creating new window: {err}");
} }
}, }
Action::ToggleResizeIncrements => window.toggle_resize_increments(), Action::ToggleResizeIncrements => window.toggle_resize_increments(),
Action::ToggleCursorVisibility => window.toggle_cursor_visibility(), Action::ToggleCursorVisibility => window.toggle_cursor_visibility(),
Action::ToggleResizable => window.toggle_resizable(), Action::ToggleResizable => window.toggle_resizable(),
@@ -199,12 +179,6 @@ impl Application {
Action::Minimize => window.minimize(), Action::Minimize => window.minimize(),
Action::NextCursor => window.next_cursor(), Action::NextCursor => window.next_cursor(),
Action::NextCustomCursor => window.next_custom_cursor(&self.custom_cursors), Action::NextCustomCursor => window.next_custom_cursor(&self.custom_cursors),
#[cfg(web_platform)]
Action::UrlCustomCursor => window.url_custom_cursor(event_loop),
#[cfg(web_platform)]
Action::AnimationCustomCursor => {
window.animation_custom_cursor(event_loop, &self.custom_cursors)
},
Action::CycleCursorGrab => window.cycle_cursor_grab(), Action::CycleCursorGrab => window.cycle_cursor_grab(),
Action::DragWindow => window.drag_window(), Action::DragWindow => window.drag_window(),
Action::DragResizeWindow => window.drag_resize_window(), Action::DragResizeWindow => window.drag_resize_window(),
@@ -216,15 +190,14 @@ impl Application {
Action::CreateNewTab => { Action::CreateNewTab => {
let tab_id = window.window.tabbing_identifier(); let tab_id = window.window.tabbing_identifier();
if let Err(err) = self.create_window(event_loop, Some(tab_id)) { if let Err(err) = self.create_window(event_loop, Some(tab_id)) {
error!("Error creating new window: {err}"); eprintln!("Error creating new window: {err}");
} }
}, }
Action::RequestResize => window.swap_dimensions(),
} }
} }
fn dump_monitors(&self, event_loop: &ActiveEventLoop) { fn dump_monitors(&self, event_loop: &ActiveEventLoop) {
info!("Monitors information"); println!("Monitors information");
let primary_monitor = event_loop.primary_monitor(); let primary_monitor = event_loop.primary_monitor();
for monitor in event_loop.available_monitors() { for monitor in event_loop.available_monitors() {
let intro = if primary_monitor.as_ref() == Some(&monitor) { let intro = if primary_monitor.as_ref() == Some(&monitor) {
@@ -234,54 +207,60 @@ impl Application {
}; };
if let Some(name) = monitor.name() { if let Some(name) = monitor.name() {
info!("{intro}: {name}"); println!("{intro}: {name}");
} else { } else {
info!("{intro}: [no name]"); println!("{intro}: [no name]");
} }
let PhysicalSize { width, height } = monitor.size(); let PhysicalSize { width, height } = monitor.size();
info!( print!(" Current mode: {width}x{height}");
" Current mode: {width}x{height}{}", if let Some(m_hz) = monitor.refresh_rate_millihertz() {
if let Some(m_hz) = monitor.refresh_rate_millihertz() { println!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000);
format!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000) } else {
} else { println!();
String::new() }
}
);
let PhysicalPosition { x, y } = monitor.position(); let PhysicalPosition { x, y } = monitor.position();
info!(" Position: {x},{y}"); println!(" Position: {x},{y}");
info!(" Scale factor: {}", monitor.scale_factor()); println!(" Scale factor: {}", monitor.scale_factor());
info!(" Available modes (width x height x bit-depth):"); println!(" Available modes (width x height x bit-depth):");
for mode in monitor.video_modes() { for mode in monitor.video_modes() {
let PhysicalSize { width, height } = mode.size(); let PhysicalSize { width, height } = mode.size();
let bits = mode.bit_depth(); let bits = mode.bit_depth();
let m_hz = mode.refresh_rate_millihertz(); let m_hz = mode.refresh_rate_millihertz();
info!(" {width}x{height}x{bits} @ {}.{} Hz", m_hz / 1000, m_hz % 1000); println!(
" {width}x{height}x{bits} @ {}.{} Hz",
m_hz / 1000,
m_hz % 1000
);
} }
} }
} }
/// Process the key binding. /// Process the key binding.
fn process_key_binding(key: &str, mods: &ModifiersState) -> Option<Action> { fn process_key_binding(key: &str, mods: &ModifiersState) -> Option<Action> {
KEY_BINDINGS KEY_BINDINGS.iter().find_map(|binding| {
.iter() binding
.find_map(|binding| binding.is_triggered_by(&key, mods).then_some(binding.action)) .is_triggered_by(&key, mods)
.then_some(binding.action)
})
} }
/// Process mouse binding. /// Process mouse binding.
fn process_mouse_binding(button: MouseButton, mods: &ModifiersState) -> Option<Action> { fn process_mouse_binding(button: MouseButton, mods: &ModifiersState) -> Option<Action> {
MOUSE_BINDINGS MOUSE_BINDINGS.iter().find_map(|binding| {
.iter() binding
.find_map(|binding| binding.is_triggered_by(&button, mods).then_some(binding.action)) .is_triggered_by(&button, mods)
.then_some(binding.action)
})
} }
fn print_help(&self) { fn print_help(&self) {
info!("Keyboard bindings:"); println!("Keyboard bindings:");
for binding in KEY_BINDINGS { for binding in KEY_BINDINGS {
info!( println!(
"{}{:<10} - {} ({})", "{}{:<10} - {} ({})",
modifiers_to_string(binding.mods), modifiers_to_string(binding.mods),
binding.trigger, binding.trigger,
@@ -289,9 +268,9 @@ impl Application {
binding.action.help(), binding.action.help(),
); );
} }
info!("Mouse bindings:"); println!("Mouse bindings:");
for binding in MOUSE_BINDINGS { for binding in MOUSE_BINDINGS {
info!( println!(
"{}{:<10} - {} ({})", "{}{:<10} - {} ({})",
modifiers_to_string(binding.mods), modifiers_to_string(binding.mods),
mouse_button_to_string(binding.trigger), mouse_button_to_string(binding.trigger),
@@ -304,7 +283,7 @@ impl Application {
impl ApplicationHandler<UserEvent> for Application { impl ApplicationHandler<UserEvent> for Application {
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) { fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) {
info!("User event: {event:?}"); println!("User event: {event:?}");
} }
fn window_event( fn window_event(
@@ -321,46 +300,50 @@ impl ApplicationHandler<UserEvent> for Application {
match event { match event {
WindowEvent::Resized(size) => { WindowEvent::Resized(size) => {
window.resize(size); window.resize(size);
}, }
WindowEvent::Focused(focused) => { WindowEvent::Focused(focused) => {
if focused { if focused {
info!("Window={window_id:?} focused"); println!("Window={window_id:?} fosused");
} else { } else {
info!("Window={window_id:?} unfocused"); println!("Window={window_id:?} unfosused");
} }
}, }
WindowEvent::ScaleFactorChanged { scale_factor, .. } => { WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
info!("Window={window_id:?} changed scale to {scale_factor}"); println!("Window={window_id:?} changed scale to {scale_factor}");
}, }
WindowEvent::ThemeChanged(theme) => { WindowEvent::ThemeChanged(theme) => {
info!("Theme changed to {theme:?}"); println!("Theme changed to {theme:?}");
window.set_theme(theme); window.set_theme(theme);
}, }
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
if let Err(err) = window.draw() { if let Err(err) = window.draw() {
error!("Error drawing window: {err}"); eprintln!("Error drawing window: {err}");
} }
}, }
WindowEvent::Occluded(occluded) => { WindowEvent::Occluded(occluded) => {
window.set_occluded(occluded); window.set_occluded(occluded);
}, }
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
info!("Closing Window={window_id:?}"); println!("Closing Window={window_id:?}");
self.windows.remove(&window_id); self.windows.remove(&window_id);
}, }
WindowEvent::ModifiersChanged(modifiers) => { WindowEvent::ModifiersChanged(modifiers) => {
window.modifiers = modifiers.state(); window.modifiers = modifiers.state();
info!("Modifiers changed to {:?}", window.modifiers); println!("Modifiers changed to {:?}", window.modifiers);
}, }
WindowEvent::MouseWheel { delta, .. } => match delta { WindowEvent::MouseWheel { delta, .. } => match delta {
MouseScrollDelta::LineDelta(x, y) => { MouseScrollDelta::LineDelta(x, y) => {
info!("Mouse wheel Line Delta: ({x},{y})"); println!("Mouse wheel Line Delta: ({x},{y})");
}, }
MouseScrollDelta::PixelDelta(px) => { MouseScrollDelta::PixelDelta(px) => {
info!("Mouse wheel Pixel Delta: ({},{})", px.x, px.y); println!("Mouse wheel Pixel Delta: ({},{})", px.x, px.y);
}, }
}, },
WindowEvent::KeyboardInput { event, is_synthetic: false, .. } => { WindowEvent::KeyboardInput {
event,
is_synthetic: false,
..
} => {
let mods = window.modifiers; let mods = window.modifiers;
// Dispatch actions only on press. // Dispatch actions only on press.
@@ -375,68 +358,65 @@ impl ApplicationHandler<UserEvent> for Application {
self.handle_action(event_loop, window_id, action); self.handle_action(event_loop, window_id, action);
} }
} }
}, }
WindowEvent::MouseInput { button, state, .. } => { WindowEvent::MouseInput { button, state, .. } => {
let mods = window.modifiers; let mods = window.modifiers;
if let Some(action) = if let Some(action) = state
state.is_pressed().then(|| Self::process_mouse_binding(button, &mods)).flatten() .is_pressed()
.then(|| Self::process_mouse_binding(button, &mods))
.flatten()
{ {
self.handle_action(event_loop, window_id, action); self.handle_action(event_loop, window_id, action);
} }
}, }
WindowEvent::CursorLeft { .. } => { WindowEvent::CursorLeft { .. } => {
info!("Cursor left Window={window_id:?}"); println!("Cursor left Window={window_id:?}");
window.cursor_left(); window.cursor_left();
}, }
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
info!("Moved cursor to {position:?}"); println!("Moved cursor to {position:?}");
window.cursor_moved(position); window.cursor_moved(position);
}, }
WindowEvent::ActivationTokenDone { token: _token, .. } => { WindowEvent::ActivationTokenDone { token: _token, .. } => {
#[cfg(any(x11_platform, wayland_platform))] #[cfg(any(x11_platform, wayland_platform))]
{ {
startup_notify::set_activation_token_env(_token); startup_notify::set_activation_token_env(_token);
if let Err(err) = self.create_window(event_loop, None) { if let Err(err) = self.create_window(event_loop, None) {
error!("Error creating new window: {err}"); eprintln!("Error creating new window: {err}");
} }
} }
}, }
WindowEvent::Ime(event) => match event { WindowEvent::Ime(event) => match event {
Ime::Enabled => info!("IME enabled for Window={window_id:?}"), Ime::Enabled => println!("IME enabled for Window={window_id:?}"),
Ime::Preedit(text, caret_pos) => { Ime::Preedit(text, caret_pos) => {
info!("Preedit: {}, with caret at {:?}", text, caret_pos); println!("Preedit: {}, with caret at {:?}", text, caret_pos);
}, }
Ime::Commit(text) => { Ime::Commit(text) => {
info!("Committed: {}", text); println!("Committed: {}", text);
}, }
Ime::Disabled => info!("IME disabled for Window={window_id:?}"), Ime::Disabled => println!("IME disabled for Window={window_id:?}"),
}, },
WindowEvent::PinchGesture { delta, .. } => { WindowEvent::PinchGesture { delta, .. } => {
window.zoom += delta; window.zoom += delta;
let zoom = window.zoom; let zoom = window.zoom;
if delta > 0.0 { if delta > 0.0 {
info!("Zoomed in {delta:.5} (now: {zoom:.5})"); println!("Zoomed in {delta:.5} (now: {zoom:.5})");
} else { } else {
info!("Zoomed out {delta:.5} (now: {zoom:.5})"); println!("Zoomed out {delta:.5} (now: {zoom:.5})");
} }
}, }
WindowEvent::RotationGesture { delta, .. } => { WindowEvent::RotationGesture { delta, .. } => {
window.rotated += delta; window.rotated += delta;
let rotated = window.rotated; let rotated = window.rotated;
if delta > 0.0 { if delta > 0.0 {
info!("Rotated counterclockwise {delta:.5} (now: {rotated:.5})"); println!("Rotated counterclockwise {delta:.5} (now: {rotated:.5})");
} else { } else {
info!("Rotated clockwise {delta:.5} (now: {rotated:.5})"); println!("Rotated clockwise {delta:.5} (now: {rotated:.5})");
} }
}, }
WindowEvent::PanGesture { delta, phase, .. } => {
window.panned.x += delta.x;
window.panned.y += delta.y;
info!("Panned ({delta:?})) (now: {:?}), {phase:?}", window.panned);
},
WindowEvent::DoubleTapGesture { .. } => { WindowEvent::DoubleTapGesture { .. } => {
info!("Smart zoom"); println!("Smart zoom");
}, }
WindowEvent::TouchpadPressure { .. } WindowEvent::TouchpadPressure { .. }
| WindowEvent::HoveredFileCancelled | WindowEvent::HoveredFileCancelled
| WindowEvent::KeyboardInput { .. } | WindowEvent::KeyboardInput { .. }
@@ -456,22 +436,23 @@ impl ApplicationHandler<UserEvent> for Application {
device_id: DeviceId, device_id: DeviceId,
event: DeviceEvent, event: DeviceEvent,
) { ) {
info!("Device {device_id:?} event: {event:?}"); println!("Device {device_id:?} event: {event:?}");
} }
fn resumed(&mut self, event_loop: &ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
info!("Resumed the event loop"); println!("Resumed the event loop");
self.dump_monitors(event_loop); self.dump_monitors(event_loop);
// Create initial window. // Create initial window.
self.create_window(event_loop, None).expect("failed to create initial window"); self.create_window(event_loop, None)
.expect("failed to create initial window");
self.print_help(); self.print_help();
} }
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
if self.windows.is_empty() { if self.windows.is_empty() {
info!("No windows left, exiting..."); println!("No windows left, exiting...");
event_loop.exit(); event_loop.exit();
} }
} }
@@ -491,9 +472,9 @@ struct WindowState {
/// ///
/// NOTE: This surface must be dropped before the `Window`. /// NOTE: This surface must be dropped before the `Window`.
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
surface: Surface<DisplayHandle<'static>, Arc<Window>>, surface: Surface,
/// The actual winit Window. /// The actual winit Window.
window: Arc<Window>, window: Window,
/// The window theme we're drawing with. /// The window theme we're drawing with.
theme: Theme, theme: Theme,
/// Cursor position over the window. /// Cursor position over the window.
@@ -508,8 +489,6 @@ struct WindowState {
zoom: f64, zoom: f64,
/// The amount of rotation of the window. /// The amount of rotation of the window.
rotated: f32, rotated: f32,
/// The amount of pan of the window.
panned: PhysicalPosition<f32>,
#[cfg(macos_platform)] #[cfg(macos_platform)]
option_as_alt: OptionAsAlt, option_as_alt: OptionAsAlt,
@@ -522,15 +501,13 @@ struct WindowState {
impl WindowState { impl WindowState {
fn new(app: &Application, window: Window) -> Result<Self, Box<dyn Error>> { fn new(app: &Application, window: Window) -> Result<Self, Box<dyn Error>> {
let window = Arc::new(window);
// SAFETY: the surface is dropped before the `window` which provided it with handle, thus // SAFETY: the surface is dropped before the `window` which provided it with handle, thus
// it doesn't outlive it. // it doesn't outlive it.
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
let surface = Surface::new(app.context.as_ref().unwrap(), Arc::clone(&window))?; let surface = unsafe { Surface::new(app.context.as_ref().unwrap(), &window)? };
let theme = window.theme().unwrap_or(Theme::Dark); let theme = window.theme().unwrap_or(Theme::Dark);
info!("Theme: {theme:?}"); println!("Theme: {theme:?}");
let named_idx = 0; let named_idx = 0;
window.set_cursor(CURSORS[named_idx]); window.set_cursor(CURSORS[named_idx]);
@@ -555,7 +532,6 @@ impl WindowState {
modifiers: Default::default(), modifiers: Default::default(),
occluded: Default::default(), occluded: Default::default(),
rotated: Default::default(), rotated: Default::default(),
panned: Default::default(),
zoom: Default::default(), zoom: Default::default(),
}; };
@@ -567,7 +543,8 @@ impl WindowState {
self.ime = !self.ime; self.ime = !self.ime;
self.window.set_ime_allowed(self.ime); self.window.set_ime_allowed(self.ime);
if let Some(position) = self.ime.then_some(self.cursor_position).flatten() { if let Some(position) = self.ime.then_some(self.cursor_position).flatten() {
self.window.set_ime_cursor_area(position, PhysicalSize::new(20, 20)); self.window
.set_ime_cursor_area(position, PhysicalSize::new(20, 20));
} }
} }
@@ -578,7 +555,8 @@ impl WindowState {
pub fn cursor_moved(&mut self, position: PhysicalPosition<f64>) { pub fn cursor_moved(&mut self, position: PhysicalPosition<f64>) {
self.cursor_position = Some(position); self.cursor_position = Some(position);
if self.ime { if self.ime {
self.window.set_ime_cursor_area(position, PhysicalSize::new(20, 20)); self.window
.set_ime_cursor_area(position, PhysicalSize::new(20, 20));
} }
} }
@@ -616,7 +594,7 @@ impl WindowState {
Some(_) => None, Some(_) => None,
None => Some(LogicalSize::new(25.0, 25.0)), None => Some(LogicalSize::new(25.0, 25.0)),
}; };
info!("Had increments: {}", new_increments.is_none()); println!("Had increments: {}", new_increments.is_none());
self.window.set_resize_increments(new_increments); self.window.set_resize_increments(new_increments);
} }
@@ -638,9 +616,9 @@ impl WindowState {
CursorGrabMode::Confined => CursorGrabMode::Locked, CursorGrabMode::Confined => CursorGrabMode::Locked,
CursorGrabMode::Locked => CursorGrabMode::None, CursorGrabMode::Locked => CursorGrabMode::None,
}; };
info!("Changing cursor grab mode to {:?}", self.cursor_grab); println!("Changing cursor grab mode to {:?}", self.cursor_grab);
if let Err(err) = self.window.set_cursor_grab(self.cursor_grab) { if let Err(err) = self.window.set_cursor_grab(self.cursor_grab) {
error!("Error setting cursor grab: {err}"); eprintln!("Error setting cursor grab: {err}");
} }
} }
@@ -652,34 +630,16 @@ impl WindowState {
OptionAsAlt::OnlyRight => OptionAsAlt::Both, OptionAsAlt::OnlyRight => OptionAsAlt::Both,
OptionAsAlt::Both => OptionAsAlt::None, OptionAsAlt::Both => OptionAsAlt::None,
}; };
info!("Setting option as alt {:?}", self.option_as_alt); println!("Setting option as alt {:?}", self.option_as_alt);
self.window.set_option_as_alt(self.option_as_alt); self.window.set_option_as_alt(self.option_as_alt);
} }
/// Swap the window dimensions with `request_inner_size`.
fn swap_dimensions(&mut self) {
let old_inner_size = self.window.inner_size();
let mut inner_size = old_inner_size;
mem::swap(&mut inner_size.width, &mut inner_size.height);
info!("Requesting resize from {old_inner_size:?} to {inner_size:?}");
if let Some(new_inner_size) = self.window.request_inner_size(inner_size) {
if old_inner_size == new_inner_size {
info!("Inner size change got ignored");
} else {
self.resize(new_inner_size);
}
} else {
info!("Request inner size is asynchronous");
}
}
/// Pick the next cursor. /// Pick the next cursor.
fn next_cursor(&mut self) { fn next_cursor(&mut self) {
self.named_idx = (self.named_idx + 1) % CURSORS.len(); self.named_idx = (self.named_idx + 1) % CURSORS.len();
info!("Setting cursor to \"{:?}\"", CURSORS[self.named_idx]); println!("Setting cursor to \"{:?}\"", CURSORS[self.named_idx]);
self.window.set_cursor(Cursor::Icon(CURSORS[self.named_idx])); self.window
.set_cursor(Cursor::Icon(CURSORS[self.named_idx]));
} }
/// Pick the next custom cursor. /// Pick the next custom cursor.
@@ -689,46 +649,18 @@ impl WindowState {
self.window.set_cursor(cursor); self.window.set_cursor(cursor);
} }
/// Custom cursor from an URL.
#[cfg(web_platform)]
fn url_custom_cursor(&mut self, event_loop: &ActiveEventLoop) {
let cursor = event_loop.create_custom_cursor(url_custom_cursor());
self.window.set_cursor(cursor);
}
/// Custom cursor from a URL.
#[cfg(web_platform)]
fn animation_custom_cursor(
&mut self,
event_loop: &ActiveEventLoop,
custom_cursors: &[CustomCursor],
) {
use std::time::Duration;
use winit::platform::web::CustomCursorExtWebSys;
let cursors = vec![
custom_cursors[0].clone(),
custom_cursors[1].clone(),
event_loop.create_custom_cursor(url_custom_cursor()),
];
let cursor = CustomCursor::from_animation(Duration::from_secs(3), cursors).unwrap();
let cursor = event_loop.create_custom_cursor(cursor);
self.window.set_cursor(cursor);
}
/// Resize the window to the new size. /// Resize the window to the new size.
fn resize(&mut self, size: PhysicalSize<u32>) { fn resize(&mut self, _size: PhysicalSize<u32>) {
info!("Resized to {size:?}");
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
{ {
let (width, height) = match (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) let (width, height) =
{ match (NonZeroU32::new(_size.width), NonZeroU32::new(_size.height)) {
(Some(width), Some(height)) => (width, height), (Some(width), Some(height)) => (width, height),
_ => return, _ => return,
}; };
self.surface.resize(width, height).expect("failed to resize inner buffer"); self.surface
.resize(width, height)
.expect("failed to resize inner buffer");
} }
self.window.request_redraw(); self.window.request_redraw();
} }
@@ -749,9 +681,9 @@ impl WindowState {
/// Drag the window. /// Drag the window.
fn drag_window(&self) { fn drag_window(&self) {
if let Err(err) = self.window.drag_window() { if let Err(err) = self.window.drag_window() {
info!("Error starting window drag: {err}"); println!("Error starting window drag: {err}");
} else { } else {
info!("Dragging window Window={:?}", self.window.id()); println!("Dragging window Window={:?}", self.window.id());
} }
} }
@@ -760,9 +692,9 @@ impl WindowState {
let position = match self.cursor_position { let position = match self.cursor_position {
Some(position) => position, Some(position) => position,
None => { None => {
info!("Drag-resize requires cursor to be inside the window"); println!("Drag-resize requires cursor to be inside the window");
return; return;
}, }
}; };
let win_size = self.window.inner_size(); let win_size = self.window.inner_size();
@@ -799,9 +731,9 @@ impl WindowState {
}; };
if let Err(err) = self.window.drag_resize_window(direction) { if let Err(err) = self.window.drag_resize_window(direction) {
info!("Error starting window drag-resize: {err}"); println!("Error starting window drag-resize: {err}");
} else { } else {
info!("Drag-resizing window Window={:?}", self.window.id()); println!("Drag-resizing window Window={:?}", self.window.id());
} }
} }
@@ -817,12 +749,12 @@ impl WindowState {
#[cfg(not(any(android_platform, ios_platform)))] #[cfg(not(any(android_platform, ios_platform)))]
fn draw(&mut self) -> Result<(), Box<dyn Error>> { fn draw(&mut self) -> Result<(), Box<dyn Error>> {
if self.occluded { if self.occluded {
info!("Skipping drawing occluded window={:?}", self.window.id()); println!("Skipping drawing occluded window={:?}", self.window.id());
return Ok(()); return Ok(());
} }
const WHITE: u32 = 0xffffffff; const WHITE: u32 = 0xFFFFFFFF;
const DARK_GRAY: u32 = 0xff181818; const DARK_GRAY: u32 = 0xFF181818;
let color = match self.theme { let color = match self.theme {
Theme::Light => WHITE, Theme::Light => WHITE,
@@ -838,7 +770,7 @@ impl WindowState {
#[cfg(any(android_platform, ios_platform))] #[cfg(any(android_platform, ios_platform))]
fn draw(&mut self) -> Result<(), Box<dyn Error>> { fn draw(&mut self) -> Result<(), Box<dyn Error>> {
info!("Drawing but without rendering..."); println!("Drawing but without rendering...");
Ok(()) Ok(())
} }
} }
@@ -851,7 +783,11 @@ struct Binding<T: Eq> {
impl<T: Eq> Binding<T> { impl<T: Eq> Binding<T> {
const fn new(trigger: T, mods: ModifiersState, action: Action) -> Self { const fn new(trigger: T, mods: ModifiersState, action: Action) -> Self {
Self { trigger, mods, action } Self {
trigger,
mods,
action,
}
} }
fn is_triggered_by(&self, trigger: &T, mods: &ModifiersState) -> bool { fn is_triggered_by(&self, trigger: &T, mods: &ModifiersState) -> bool {
@@ -873,10 +809,6 @@ enum Action {
Minimize, Minimize,
NextCursor, NextCursor,
NextCustomCursor, NextCustomCursor,
#[cfg(web_platform)]
UrlCustomCursor,
#[cfg(web_platform)]
AnimationCustomCursor,
CycleCursorGrab, CycleCursorGrab,
PrintHelp, PrintHelp,
DragWindow, DragWindow,
@@ -886,7 +818,6 @@ enum Action {
CycleOptionAsAlt, CycleOptionAsAlt,
#[cfg(macos_platform)] #[cfg(macos_platform)]
CreateNewTab, CreateNewTab,
RequestResize,
} }
impl Action { impl Action {
@@ -904,10 +835,6 @@ impl Action {
Action::ToggleResizeIncrements => "Use resize increments when resizing window", Action::ToggleResizeIncrements => "Use resize increments when resizing window",
Action::NextCursor => "Advance the cursor to the next value", Action::NextCursor => "Advance the cursor to the next value",
Action::NextCustomCursor => "Advance custom cursor to the next value", Action::NextCustomCursor => "Advance custom cursor to the next value",
#[cfg(web_platform)]
Action::UrlCustomCursor => "Custom cursor from an URL",
#[cfg(web_platform)]
Action::AnimationCustomCursor => "Custom cursor from an animation",
Action::CycleCursorGrab => "Cycle through cursor grab mode", Action::CycleCursorGrab => "Cycle through cursor grab mode",
Action::PrintHelp => "Print help", Action::PrintHelp => "Print help",
Action::DragWindow => "Start window drag", Action::DragWindow => "Start window drag",
@@ -917,7 +844,6 @@ impl Action {
Action::CycleOptionAsAlt => "Cycle option as alt mode", Action::CycleOptionAsAlt => "Cycle option as alt mode",
#[cfg(macos_platform)] #[cfg(macos_platform)]
Action::CreateNewTab => "Create new tab", Action::CreateNewTab => "Create new tab",
Action::RequestResize => "Request a resize",
} }
} }
} }
@@ -936,24 +862,11 @@ fn decode_cursor(bytes: &[u8]) -> CustomCursorSource {
CustomCursor::from_rgba(samples.samples, w, h, w / 2, h / 2).unwrap() CustomCursor::from_rgba(samples.samples, w, h, w / 2, h / 2).unwrap()
} }
#[cfg(web_platform)] fn load_icon(path: &Path) -> Icon {
fn url_custom_cursor() -> CustomCursorSource {
use std::sync::atomic::{AtomicU64, Ordering};
use winit::platform::web::CustomCursorExtWebSys;
static URL_COUNTER: AtomicU64 = AtomicU64::new(0);
CustomCursor::from_url(
format!("https://picsum.photos/128?random={}", URL_COUNTER.fetch_add(1, Ordering::Relaxed)),
64,
64,
)
}
fn load_icon(bytes: &[u8]) -> Icon {
let (icon_rgba, icon_width, icon_height) = { let (icon_rgba, icon_width, icon_height) = {
let image = image::load_from_memory(bytes).unwrap().into_rgba8(); let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions(); let (width, height) = image.dimensions();
let rgba = image.into_raw(); let rgba = image.into_raw();
(rgba, width, height) (rgba, width, height)
@@ -1037,7 +950,6 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
Binding::new("L", ModifiersState::CONTROL, Action::CycleCursorGrab), Binding::new("L", ModifiersState::CONTROL, Action::CycleCursorGrab),
Binding::new("P", ModifiersState::CONTROL, Action::ToggleResizeIncrements), Binding::new("P", ModifiersState::CONTROL, Action::ToggleResizeIncrements),
Binding::new("R", ModifiersState::CONTROL, Action::ToggleResizable), Binding::new("R", ModifiersState::CONTROL, Action::ToggleResizable),
Binding::new("R", ModifiersState::ALT, Action::RequestResize),
// M. // M.
Binding::new("M", ModifiersState::CONTROL, Action::ToggleMaximize), Binding::new("M", ModifiersState::CONTROL, Action::ToggleMaximize),
Binding::new("M", ModifiersState::ALT, Action::Minimize), Binding::new("M", ModifiersState::ALT, Action::Minimize),
@@ -1046,18 +958,6 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
// C. // C.
Binding::new("C", ModifiersState::CONTROL, Action::NextCursor), Binding::new("C", ModifiersState::CONTROL, Action::NextCursor),
Binding::new("C", ModifiersState::ALT, Action::NextCustomCursor), Binding::new("C", ModifiersState::ALT, Action::NextCustomCursor),
#[cfg(web_platform)]
Binding::new(
"C",
ModifiersState::CONTROL.union(ModifiersState::SHIFT),
Action::UrlCustomCursor,
),
#[cfg(web_platform)]
Binding::new(
"C",
ModifiersState::ALT.union(ModifiersState::SHIFT),
Action::AnimationCustomCursor,
),
Binding::new("Z", ModifiersState::CONTROL, Action::ToggleCursorVisibility), Binding::new("Z", ModifiersState::CONTROL, Action::ToggleCursorVisibility),
#[cfg(macos_platform)] #[cfg(macos_platform)]
Binding::new("T", ModifiersState::SUPER, Action::CreateNewTab), Binding::new("T", ModifiersState::SUPER, Action::CreateNewTab),
@@ -1066,7 +966,19 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
]; ];
const MOUSE_BINDINGS: &[Binding<MouseButton>] = &[ const MOUSE_BINDINGS: &[Binding<MouseButton>] = &[
Binding::new(MouseButton::Left, ModifiersState::ALT, Action::DragResizeWindow), Binding::new(
Binding::new(MouseButton::Left, ModifiersState::CONTROL, Action::DragWindow), MouseButton::Left,
Binding::new(MouseButton::Right, ModifiersState::CONTROL, Action::ShowWindowMenu), ModifiersState::ALT,
Action::DragResizeWindow,
),
Binding::new(
MouseButton::Left,
ModifiersState::CONTROL,
Action::DragWindow,
),
Binding::new(
MouseButton::Right,
ModifiersState::CONTROL,
Action::ShowWindowMenu,
),
]; ];

View File

@@ -39,7 +39,7 @@ fn main() -> Result<(), Box<dyn Error>> {
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
window.pre_present_notify(); window.pre_present_notify();
fill::fill_window(window); fill::fill_window(window);
}, }
_ => (), _ => (),
} }
} }
@@ -58,7 +58,10 @@ fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let event_loop = EventLoop::new()?; let event_loop = EventLoop::new()?;
let mut app = XEmbedDemo { parent_window_id, window: None }; let mut app = XEmbedDemo {
parent_window_id,
window: None,
};
event_loop.run_app(&mut app).map_err(Into::into) event_loop.run_app(&mut app).map_err(Into::into)
} }

View File

@@ -0,0 +1,12 @@
[package]
name = "common-tests"
version = "0.1.0"
rust-version.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
[dependencies]
gui-test.workspace = true
macro_rules_attribute = "0.2.0"
winit.workspace = true

View File

@@ -0,0 +1,23 @@
//! Run the test.
use gui_test::{test, Harness};
use macro_rules_attribute::apply;
use winit::event_loop::EventLoop;
#[allow(deprecated)]
#[apply(test)]
fn initialize(harness: &mut Harness) {
let mut group = harness.group("sanity");
group.harness().with_test("startup/shutdown", || {
let evl = EventLoop::new().expect("initialization");
evl.run(|_event, elwt| {
elwt.exit();
})
.expect("running");
});
}
gui_test::main! {
gui_test::remote::handler()
}

View File

@@ -0,0 +1,13 @@
[package]
name = "gui-test-runner"
version = "0.1.0"
rust-version.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
[dependencies]
camino = "1.1.6"
fastrand = "2.0.1"
gui-test.workspace = true
serde_json.workspace = true

View File

@@ -0,0 +1,81 @@
// Copyright 2024 The Winit Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! A wrapper around the `Command` type that dumps the command to stderr.
//!
//! Essentially it's like `set -x` in Bash.
use std::ffi::{OsStr, OsString};
use std::io::{self, prelude::*};
use std::process::Child;
/// Simple `Command` wrapper.
pub(super) struct Command {
/// Actual inner command.
inner: std::process::Command,
/// Command to run.
text: Vec<OsString>,
}
impl Command {
/// Create a new `Command`.
pub(super) fn new(cmd: impl AsRef<OsStr>) -> Self {
let cmd = cmd.as_ref();
Self {
inner: std::process::Command::new(cmd),
text: vec![cmd.to_os_string()],
}
}
/// Add an argument to the `Command`.
pub(super) fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
let arg = arg.as_ref();
self.inner.arg(arg);
self.text.push(arg.to_os_string());
self
}
/// Add multiple arguments to the `Command`.
pub(super) fn args<T: AsRef<OsStr>>(&mut self, args: impl IntoIterator<Item = T>) -> &mut Self {
for arg in args {
let arg = arg.as_ref();
self.inner.arg(arg);
self.text.push(arg.to_os_string());
}
self
}
/// Spawn the process.
pub(super) fn spawn(&mut self) -> io::Result<Child> {
dump_text(&self.text);
self.inner.spawn()
}
}
/// Dump `OsString` list to stderr.
fn dump_text(text: &[OsString]) {
let mut cerr = io::stderr().lock();
write!(&mut cerr, "+").unwrap();
for arg in text {
match arg.to_str() {
Some(arg) => write!(&mut cerr, " {}", arg).unwrap(),
None => write!(&mut cerr, " {:?}", arg).unwrap(),
}
}
writeln!(&mut cerr).unwrap();
}

View File

@@ -0,0 +1,91 @@
// Copyright 2024 The Winit Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Run the actual Docker command.
use crate::command::Command;
use camino::Utf8Path;
use std::ffi::OsStr;
use std::io;
use std::process::Child;
/// The Docker command line.
pub(super) struct DockerRun {
command: Command,
}
impl DockerRun {
/// Start the command.
pub(super) fn new() -> Self {
let mut command = Command::new("docker");
command.arg("run");
Self { command }
}
/// Run with an environment variable.
pub(super) fn env(&mut self, name: impl AsRef<str>, value: impl AsRef<str>) -> &mut Self {
let env_arg = format!("{}={}", name.as_ref(), value.as_ref());
self.command.args(["--env", &env_arg]);
self
}
/// Run with a simple `init` process.
pub(super) fn init(&mut self) -> &mut Self {
self.command.arg("--init");
self
}
/// Set the working directory.
pub(super) fn workdir(&mut self, dir: impl AsRef<OsStr>) -> &mut Self {
self.command.arg("--workdir");
self.command.arg(dir);
self
}
/// Remove the container once it is complete.
pub(super) fn rm(&mut self) -> &mut Self {
self.command.arg("--rm");
self
}
/// Pass a volume into the container.
pub(super) fn volume(
&mut self,
host: impl AsRef<Utf8Path>,
container: impl AsRef<Utf8Path>,
) -> &mut Self {
let list = format!("{}:{}", host.as_ref(), container.as_ref());
self.command.args(["--volume", &list]);
self
}
/// Run the container with a command.
pub(super) fn run_with_command<T: AsRef<OsStr>>(
&mut self,
container_name: impl AsRef<str>,
container_version: impl AsRef<str>,
command: impl IntoIterator<Item = T>,
) -> io::Result<Child> {
self.command.arg(format!(
"{}:{}",
container_name.as_ref(),
container_version.as_ref()
));
self.command.args(command);
self.command.spawn()
}
}

View File

@@ -0,0 +1,114 @@
// Copyright 2024 The Winit Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Run tests inside of a Linux docker container.
use super::command::DockerRun;
use crate::stream::StreamReader;
use gui_test::remote::handler;
use gui_test::TestHandler;
use std::io;
use std::os::unix::net::UnixListener;
use std::path::Path;
use std::thread;
const UBUNTU_DOCKERFILE: &str = "ghcr.io/rust-windowing/testubuntu";
const LATEST: &str = "latest";
/// Run the provided test in a Linux docker container.
pub(crate) fn linux_test(test_name: &str) -> io::Result<()> {
// Create a Unix socket to listen for events on.
let unix_path = format!("/tmp/gui_test_{}.sock", fastrand::u16(..));
let listener = UnixListener::bind(&unix_path)?;
// Spawn the Docker container.
let mut container = {
let mut docker = DockerRun::new();
// Usual options.
docker.rm().init();
// Pass through the socket as a volume.
docker.volume(&unix_path, &unix_path);
// Pass through the winit directory.
let winit_directory = Path::new(env!("CARGO_MANIFEST_DIR"))
.ancestors()
.find_map(|path| {
let cargo_toml = path.join("Cargo.toml");
let contents = std::fs::read(cargo_toml).ok()?;
if std::str::from_utf8(&contents)
.ok()?
.contains("name = \"winit\"")
{
Some(path)
} else {
None
}
})
.unwrap();
docker.volume(
camino::Utf8Path::from_path(winit_directory).unwrap(),
"/app/winit/",
);
// Set the working dir to this directory.
docker.workdir("/app/winit/");
// Set GUI_TEST_UNIX_STREAM to the socket.
docker.env("GUI_TEST_UNIX_STREAM", &unix_path);
// Set CARGO_TARGET_DIR to a random other directory.
docker.env("CARGO_TARGET_DIR", "/tmp/");
// The command to run the test.
let command = ["xvfb-run", "cargo", "run", "-p", test_name];
// Spawn the test container.
docker.run_with_command(UBUNTU_DOCKERFILE, LATEST, command)?
};
// Run the console listener in another thread.
let handle = thread::spawn(move || {
// Attach to the listener.
let (event_reader, _) = listener.accept().unwrap();
// Read events and output them as we get them.
let input = StreamReader::new(event_reader);
let mut output = handler();
for event in input {
let event = event?;
output.handle_test(event);
}
io::Result::Ok(())
});
// Wait for the container to finish.
if !container.wait()?.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
"docker exited with a failure exit code",
));
}
// Stop the thread.
handle.join().unwrap().unwrap();
Ok(())
}

View File

@@ -0,0 +1,20 @@
// Copyright 2024 The Winit Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Dealing with Docker.
mod command;
#[cfg(unix)]
pub(super) mod linux;

View File

@@ -0,0 +1,69 @@
//! Runner for the `gui-test` system.
mod command;
mod docker;
mod stream;
use std::env;
use std::process::{Command, Stdio};
fn main() {
let mut args = env::args();
// Get the test crate name.
let test_crate = args.nth(1).unwrap();
// Get the target.
let target_tag = args.next().unwrap();
// Split the target into the target and the tag.
let (target, tag) = {
let mut split = target_tag.splitn(1, ':');
let target = split.next().unwrap();
let tag = split.next();
(target, tag)
};
// Get the current target.
let current_target = current_target();
// If we are building for Linux, run the Linux Docker container.
// TODO: Architecture differences.
if target.contains("linux") {
docker::linux::linux_test(&test_crate).unwrap();
return;
}
// For now, we only support building for the current target.
assert_eq!(target, current_target);
assert!(tag.is_none());
// Just run the crate.
if !Command::new("cargo")
.args(["run", "-p", &test_crate])
.status()
.unwrap()
.success()
{
panic!("test failed");
}
}
/// Get the current target.
fn current_target() -> String {
let output = Command::new("rustc")
.arg("-vV")
.stdout(Stdio::piped())
.output()
.unwrap();
// Look for the line that starts with "host".
let stdout = String::from_utf8(output.stdout).unwrap();
for line in stdout.lines() {
if let Some(host) = line.strip_prefix("host: ") {
return host.to_string();
}
}
panic!("failed to find host: line in rustc output")
}

View File

@@ -0,0 +1,81 @@
// Copyright 2024 The Winit Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Read test events from a stream.
use gui_test::{TestEvent, TestEventType};
use std::io::{self, Read};
/// Read events from a stream.
pub(super) struct StreamReader<R> {
/// The inner reader.
reader: Option<R>,
/// Reused buffer.
buffer: Vec<u8>,
}
impl<R: Read> StreamReader<R> {
/// Create a new stream reader.
pub(super) fn new(reader: R) -> Self {
Self {
reader: Some(reader),
buffer: vec![0u8; 1024],
}
}
}
macro_rules! leap {
($self:expr, $e:expr) => {{
match ($e) {
Ok(x) => x,
Err(err) => {
($self).reader = None;
return Some(Err(err));
}
}
}};
}
impl<R: Read> Iterator for StreamReader<R> {
type Item = io::Result<TestEvent>;
fn next(&mut self) -> Option<Self::Item> {
let reader = self.reader.as_mut()?;
// Read eight bytes from the reader to get payload length.
let mut len_buffer = [0u8; 8];
leap!(self, reader.read_exact(&mut len_buffer));
// Parse that, then read the length's worth of bytes.
let length = u64::from_be_bytes(len_buffer);
self.buffer.resize(length as usize, 0);
leap!(self, reader.read_exact(&mut self.buffer));
// Parse as a test event.
let event: TestEvent = leap!(
self,
serde_json::from_slice(&self.buffer)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
);
// If this is complete, stop running.
if matches!(event.ty, TestEventType::Complete { .. }) {
self.reader = None;
}
// We are okay.
Some(Ok(event))
}
}

17
it/gui-test/Cargo.toml Normal file
View File

@@ -0,0 +1,17 @@
[package]
name = "gui-test"
version = "0.1.0"
rust-version.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
[dependencies]
async-executor = "1.8.0"
async-io.workspace = true
async-lock = "3.3.0"
async-process = "2.1.0"
inventory = "0.3.15"
owo-colors = "4.0.0"
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true

443
it/gui-test/src/lib.rs Normal file
View File

@@ -0,0 +1,443 @@
//! A testing framework that can be run remotely.
pub mod remote;
pub mod stream;
pub mod user;
use serde::{Deserialize, Serialize};
use std::env;
use std::ffi::{OsStr, OsString};
use std::mem;
use std::num::NonZeroUsize;
use std::panic;
const GUI_TEST_CURRENT_TEST_NAME: &str = "GUI_TEST_CURRENT_TEST_NAME";
const GUI_TEST_SUBPROCESS_LIMIT: &str = "GUI_TEST_SUBPROCESS_LIMIT";
const DEFAULT_LIMIT: usize = 1;
#[doc(hidden)]
pub use inventory as __inventory;
/// Replacement for the `main` function.
#[macro_export]
macro_rules! main {
($handler:expr) => {
fn main() {
$crate::__entry(|| $handler)
}
};
}
/// Set up a test for the test framework.
#[macro_export]
macro_rules! test {
(
$(#[$attr:meta])*
fn $name:ident ($hname:ident : $htype:ty) $bl:block
) => {
const _: () = {
$(#[$attr])*
fn $name ($hname: $htype) $bl
$crate::__inventory::submit! {
$crate::__TestStart::__new(
stringify!($name),
$name
)
}
};
};
}
/// Test start.
#[doc(hidden)]
pub struct __TestStart {
/// The name of the test.
name: &'static str,
/// The function to call.
func: fn(&mut Harness),
}
impl __TestStart {
/// Create a new test start.
#[doc(hidden)]
pub const fn __new(name: &'static str, func: fn(&mut Harness)) -> Self {
Self { name, func }
}
}
inventory::collect! {
__TestStart
}
/// A harness for running the tests.
pub struct Harness {
/// Name of the test start.
name: String,
/// The inner test handler.
handler: Box<dyn TestHandler + Send + 'static>,
/// Number of tests that have been run so far.
test_count: usize,
/// Number of tests that have failed so far.
test_fails: usize,
/// Number of tests that have succeeded.
test_passed: usize,
/// Current state of the test harness.
state: State,
}
impl Harness {
/// Create a new test harness.
fn new<H: TestHandler + Send + 'static>(name: &str, handler: H) -> Self {
Self {
name: name.to_string(),
handler: Box::new(handler),
test_count: 0,
test_fails: 0,
test_passed: 0,
state: State::Default,
}
}
/// Begin a test.
pub fn test(&mut self, name: impl Into<String>) -> Testing<'_> {
// Make sure we aren't mid test.
match mem::replace(&mut self.state, State::Default) {
State::InTest { past_groups } => {
self.state = State::InTest { past_groups };
panic!("tried to start a test while another was underway");
}
State::InGroups(groups) => {
self.state = State::InTest {
past_groups: Some(groups),
};
}
State::Default => {
self.state = State::InTest { past_groups: None };
}
}
// Send the "test started" event to the handler.
self.send_event(TestEventType::TestStarted { name: name.into() });
// Return the handle.
Testing {
harness: Some(self),
}
}
/// Run a closure as a test.
pub fn with_test<T>(&mut self, name: impl Into<String>, f: impl FnOnce() -> T) -> T {
let test = self.test(name.into());
match panic::catch_unwind(panic::AssertUnwindSafe(f)) {
Ok(x) => x,
Err(err) => {
if let Some(panic) = err.downcast_ref::<&'static str>() {
test.fail(panic.to_string());
} else if let Some(panic) = err.downcast_ref::<String>() {
test.fail(panic.clone());
} else {
test.fail("unintelligible error".to_string());
}
panic::resume_unwind(err)
}
}
}
/// Begin a test group.
pub fn group(&mut self, name: impl Into<String>) -> Grouping<'_> {
// Make sure we can begin a group.
match mem::replace(&mut self.state, State::Default) {
State::Default => {
self.state = State::InGroups(NonZeroUsize::new(1).unwrap());
}
State::InGroups(groups) => {
self.state = State::InGroups(groups.checked_add(1).unwrap());
}
State::InTest { past_groups } => {
self.state = State::InTest { past_groups };
panic!("cannot start group mid-test")
}
}
// Send the "group started" event to the handler.
self.send_event(TestEventType::GroupStarted { name: name.into() });
// Return the handle.
Grouping { harness: self }
}
/// Run a closure inside of a group.
pub fn with_group<T>(&mut self, name: impl Into<String>, f: impl FnOnce(&mut Self) -> T) -> T {
let mut group = self.group(name.into());
f(group.harness())
}
/// End an ongoing test.
fn end_test(&mut self, reason: TestResult) {
self.test_count += 1;
match &reason {
TestResult::Passed => self.test_passed += 1,
TestResult::Failed(..) => self.test_fails += 1,
_ => {}
}
self.send_event(TestEventType::TestEnded { result: reason });
let count = match mem::replace(&mut self.state, State::Default) {
State::InTest { past_groups } => past_groups,
_ => unreachable!(),
};
self.state = match count {
None => State::Default,
Some(count) => State::InGroups(count),
};
}
/// End the current group.
fn end_group(&mut self) {
self.send_event(TestEventType::GroupEnded);
let count = match mem::replace(&mut self.state, State::Default) {
State::InGroups(groups) => groups,
_ => unreachable!(),
};
self.state = match NonZeroUsize::new(count.get() - 1) {
None => State::Default,
Some(groups) => State::InGroups(groups),
};
}
/// Send a test event of the provided type.
fn send_event(&mut self, ty: TestEventType) {
let event = TestEvent {
runner: self.name.clone(),
ty,
};
self.handler.handle_test(event);
}
}
impl Drop for Harness {
fn drop(&mut self) {
self.send_event(TestEventType::Complete {
total: self.test_count,
fail: self.test_fails,
pass: self.test_passed,
});
}
}
/// An in-progress test.
pub struct Testing<'a> {
harness: Option<&'a mut Harness>,
}
impl Testing<'_> {
/// Skip this test.
pub fn skip(mut self) {
// Send the "skipped" event.
self.harness.take().unwrap().end_test(TestResult::Skipped);
}
/// Fail this test.
fn fail(mut self, panic: String) {
self.harness
.take()
.unwrap()
.end_test(TestResult::Failed(panic));
}
}
impl Drop for Testing<'_> {
fn drop(&mut self) {
if let Some(harness) = self.harness.take() {
let result = if std::thread::panicking() {
TestResult::Failed("thread panicked".into())
} else {
TestResult::Passed
};
harness.end_test(result);
}
}
}
/// We are running a test group.
pub struct Grouping<'a> {
harness: &'a mut Harness,
}
impl Grouping<'_> {
/// Get the underlying test harness.
pub fn harness(&mut self) -> &mut Harness {
&mut self.harness
}
}
impl Drop for Grouping<'_> {
fn drop(&mut self) {
self.harness.end_group();
}
}
/// Current testing state.
enum State {
/// We are in the middle of this many groups.
InGroups(NonZeroUsize),
/// We are in the middle of a test.
InTest { past_groups: Option<NonZeroUsize> },
/// We are in the default state.
Default,
}
/// A handler for incoming test events.
pub trait TestHandler {
/// Handle a test.
fn handle_test(&mut self, event: TestEvent);
}
impl<T: TestHandler + ?Sized> TestHandler for Box<T> {
fn handle_test(&mut self, event: TestEvent) {
(**self).handle_test(event)
}
}
/// An event produced by the test harness.
#[derive(Debug, Serialize, Deserialize)]
pub struct TestEvent {
/// The name of the runner associated with the event.
pub runner: String,
/// The type of the event.
pub ty: TestEventType,
}
/// The type of the event.
#[non_exhaustive]
#[derive(Debug, Serialize, Deserialize)]
pub enum TestEventType {
/// The tests are complete and the harness can be disconnected.
Complete {
/// Total number of tests.
total: usize,
/// Total number of passing tests.
pass: usize,
/// Total number of failed tests.
fail: usize,
},
/// A test has started.
TestStarted { name: String },
/// A test has completed.
TestEnded { result: TestResult },
/// A test group has started.
GroupStarted { name: String },
/// A test group has ended.
GroupEnded,
}
/// The result of a test.
#[non_exhaustive]
#[derive(Debug, Serialize, Deserialize)]
pub enum TestResult {
/// The test passed.
Passed,
/// The test failed with the provided error.
Failed(String),
/// The test was skipped.
Skipped,
}
/// Entry point of the test.
#[doc(hidden)]
pub fn __entry<H: TestHandler + Send + 'static>(handler: impl FnOnce() -> H) {
// Look for the test name environment variable.
if let Some(test_name) = env::var(GUI_TEST_CURRENT_TEST_NAME)
.ok()
.filter(|test_name| !test_name.is_empty())
{
// Find the provided test.
let test_to_run = inventory::iter::<__TestStart>
.into_iter()
.find(|test| test.name == test_name)
.unwrap_or_else(|| panic!("unable to find test '{test_name}'"));
// Create a harness.
let mut harness = Harness::new(test_to_run.name, handler());
// Run the test.
panic::catch_unwind(panic::AssertUnwindSafe(move || {
(test_to_run.func)(&mut harness)
}))
.ok();
} else {
// Run a subprocess for every test.
let limit = env::var(GUI_TEST_SUBPROCESS_LIMIT)
.ok()
.and_then(|limit| limit.parse::<usize>().ok())
.unwrap_or(DEFAULT_LIMIT);
let process_name = env::args_os().next().unwrap();
let sema = async_lock::Semaphore::new(limit);
let ex = async_executor::Executor::new();
async_io::block_on(ex.run(async {
let mut tasks = vec![];
// Set up an environment variable for this.
for test in inventory::iter::<__TestStart> {
// Acquire a guard.
let guard = sema.acquire().await;
// Spawn a subprocess.
let mut process = async_process::Command::new(&process_name)
.envs(env::vars_os().chain(Some({
(path(&GUI_TEST_CURRENT_TEST_NAME), path(&test.name))
})))
.spawn()
.expect("failed to spawn child process");
// Spawn a task to poll that subprocess.
let task = ex.spawn(async move {
let _guard = guard;
process.status().await.unwrap()
});
tasks.push(task);
}
// Finish all of the tasks.
for task in tasks {
task.await;
}
}));
}
}
fn path<A: AsRef<OsStr>>(s: &A) -> OsString {
s.as_ref().into()
}

31
it/gui-test/src/remote.rs Normal file
View File

@@ -0,0 +1,31 @@
//! Create a test handler that can be run remotely.
use crate::stream::WriteHandler;
use crate::user::UserHandler;
use crate::TestHandler;
use std::env;
use std::net::TcpStream;
/// Create a test handler adjusted for the current environment.
pub fn handler() -> Box<dyn TestHandler + Send + 'static> {
// If GUI_TEST_UNIX_STREAM is enabled, use that as a Unix stream.
#[cfg(unix)]
if let Some(stream_path) = env::var_os("GUI_TEST_UNIX_STREAM").filter(|s| !s.is_empty()) {
let stream = std::os::unix::net::UnixStream::connect(stream_path)
.expect("unable to connect to gui-test handler");
return Box::new(WriteHandler::new(stream));
}
// If GUI_TEST_TCP_STREAM is enabled, use that as a TCP stream.
if let Some(tcp_ip) = env::var("GUI_TEST_TCP_STREAM")
.ok()
.filter(|s| !s.is_empty())
{
let stream = TcpStream::connect(tcp_ip).unwrap();
return Box::new(WriteHandler::new(stream));
}
// By default, use the user handler.
Box::new(UserHandler::new())
}

39
it/gui-test/src/stream.rs Normal file
View File

@@ -0,0 +1,39 @@
//! Write events to an output stream.
//!
//! The format is as follows:
//! - First 8 bytes: big-endian length of payload.
//! - Next {len} bytes: JSON payload to deserialize from.
use crate::{TestEvent, TestHandler};
use std::io::Write;
/// A wrapper around a writer that sends data down a stream.
#[derive(Debug)]
pub struct WriteHandler<W: Write> {
/// The inner writer.
writer: W,
}
impl<W: Write> WriteHandler<W> {
/// Create a new write handler.
pub fn new(writer: W) -> Self {
Self { writer }
}
}
impl<W: Write> TestHandler for WriteHandler<W> {
fn handle_test(&mut self, event: TestEvent) {
let payload = serde_json::to_vec(&event).unwrap();
let length = u64::to_be_bytes(payload.len() as u64);
// Write the payload to the stream.
self.writer.write_all(&length).unwrap();
self.writer.write_all(&payload).unwrap();
}
}
impl<W: Write> Drop for WriteHandler<W> {
fn drop(&mut self) {
self.writer.flush().ok();
}
}

180
it/gui-test/src/user.rs Normal file
View File

@@ -0,0 +1,180 @@
//! User-facing reporter.
use crate::{TestEvent, TestEventType, TestHandler, TestResult};
use owo_colors::OwoColorize;
use std::collections::BTreeMap;
use std::io::{self, prelude::*};
const TABSIZE: usize = 2;
/// User-facing reporter.
///
/// This reporter dumps events to the console in a user-readable format.
pub struct UserHandler {
/// Current indent.
indent: usize,
/// The test set we're currently displaying.
current_start: Option<String>,
/// Test name we are running, if any.
test_name: Option<String>,
/// Cached events.
cache: BTreeMap<String, Vec<TestEventType>>,
/// Failures we had.
failures: Vec<(String, String)>,
}
impl UserHandler {
/// Create a new handler.
pub fn new() -> Self {
Self {
indent: 0,
current_start: None,
test_name: None,
cache: BTreeMap::new(),
failures: vec![],
}
}
/// Process the provided events.
fn process_events(&mut self, events: impl IntoIterator<Item = TestEvent>) {
for event in events {
// Tell if this is an end event.
let mut ender = matches!(event.ty, TestEventType::Complete { .. });
// If there is no test name set, run the current one.
match self.current_start.as_ref() {
None => {
let TestEvent { runner, ty } = event;
self.current_start = Some(runner);
self.dump_events(Some(ty));
}
Some(test_name) => {
// If there is a test name set and it's ours, post it immediately.
if test_name == &event.runner {
self.dump_events(Some(event.ty));
} else {
// Add it to the back of another one of the events.
self.cache
.entry(test_name.clone())
.or_default()
.push(event.ty);
}
}
}
// If this is the end, dump other events.
while ender {
ender = false;
assert!(self.current_start.take().is_some());
// Pick one set.
if let Some(entry) = self.cache.first_entry() {
let (test_name, entries) = entry.remove_entry();
self.current_start = Some(test_name);
// Dump events and look for a conclusion.
self.dump_events(entries.into_iter().inspect(|ty| {
ender |= matches!(ty, TestEventType::Complete { .. });
}));
}
println!();
}
}
}
/// Dump the provided events to the console.
fn dump_events(&mut self, events: impl IntoIterator<Item = TestEventType>) {
let mut stdout = io::stdout().lock();
for event in events {
// Write the indent.
for _ in 0..(self.indent * TABSIZE) {
stdout.write_all(b" ").unwrap();
}
match event {
TestEventType::GroupStarted { name } => {
assert!(self.test_name.is_none());
// Write the group name and bump the indent.
writeln!(stdout, "{}", name.yellow().italic()).unwrap();
// Add to the indent.
self.indent += 1;
}
TestEventType::GroupEnded => {
assert!(self.test_name.is_none());
// Drop the indent.
self.indent = self.indent.checked_sub(1).unwrap();
}
TestEventType::TestStarted { name } => {
assert!(self.test_name.is_none());
// Write the line.
write!(stdout, "{} ", name.white().italic()).unwrap();
self.test_name = Some(name);
}
TestEventType::TestEnded { result } => {
let test_name = self.test_name.take().unwrap();
// Write the result.
match result {
TestResult::Passed => {
writeln!(stdout, "{}", "ok".green().bold()).unwrap();
}
TestResult::Failed(failure) => {
self.failures.push((test_name, failure));
writeln!(stdout, "{}", "FAIL".red().bold()).unwrap();
}
TestResult::Skipped => {
writeln!(stdout, "{}", "skipped".yellow().bold()).unwrap();
}
}
}
_ => {
// Completion.
}
}
}
}
}
impl TestHandler for UserHandler {
fn handle_test(&mut self, event: TestEvent) {
self.process_events(Some(event));
}
}
impl Drop for UserHandler {
fn drop(&mut self) {
assert!(self.cache.is_empty());
// Write the final bit to the stdout.
let mut stdout = io::stdout().lock();
if !self.failures.is_empty() {
writeln!(stdout, "Test Failures:").ok();
for (test_name, panic) in &self.failures {
writeln!(stdout, " {}", test_name).ok();
writeln!(stdout, "-------------").ok();
writeln!(stdout, "{}", panic).ok();
writeln!(stdout, "-------------").ok();
}
}
}
}

11
run-wasm/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "run-wasm"
version = "0.1.0"
rust-version.workspace = true
repository.workspace = true
license.workspace = true
edition.workspace = true
publish = false
[dependencies]
cargo-run-wasm = "0.2.0"

3
run-wasm/src/main.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
cargo_run_wasm::run_wasm_with_css("body { margin: 0px; }");
}

View File

@@ -1,19 +1,3 @@
format_code_in_doc_comments = true force_explicit_abi=true
match_block_trailing_comma = true use_field_init_shorthand=true
condense_wildcard_suffixes = true # merge_imports=true
use_field_init_shorthand = true
normalize_doc_attributes = true
overflow_delimited_expr = true
imports_granularity = "Module"
use_small_heuristics = "Max"
format_macro_matchers = true
error_on_unformatted = true
format_macro_bodies = true
hex_literal_case = "Lower"
normalize_comments = true
# Some macros break with this.
reorder_impl_items = false
newline_style = "Unix"
format_strings = true
wrap_comments = true
comment_width = 100

View File

@@ -20,8 +20,8 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// ///
/// For consistency, all platforms emit a `Resumed` event even if they don't themselves have a /// For consistency, all platforms emit a `Resumed` event even if they don't themselves have a
/// formal suspend/resume lifecycle. For systems without a formal suspend/resume lifecycle /// formal suspend/resume lifecycle. For systems without a formal suspend/resume lifecycle
/// the `Resumed` event is always emitted after the /// the `Resumed` event is always emitted after the [`NewEvents(StartCause::Init)`][StartCause::Init]
/// [`NewEvents(StartCause::Init)`][StartCause::Init] event. /// event.
/// ///
/// # Portability /// # Portability
/// ///
@@ -33,16 +33,15 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// Considering that the implementation of [`Suspended`] and `Resumed` events may be internally /// Considering that the implementation of [`Suspended`] and `Resumed` events may be internally
/// driven by multiple platform-specific events, and that there may be subtle differences across /// driven by multiple platform-specific events, and that there may be subtle differences across
/// platforms with how these internal events are delivered, it's recommended that applications /// platforms with how these internal events are delivered, it's recommended that applications
/// be able to gracefully handle redundant (i.e. back-to-back) [`Suspended`] or `Resumed` /// be able to gracefully handle redundant (i.e. back-to-back) [`Suspended`] or `Resumed` events.
/// events.
/// ///
/// Also see [`Suspended`] notes. /// Also see [`Suspended`] notes.
/// ///
/// ## Android /// ## Android
/// ///
/// On Android, the `Resumed` event is sent when a new [`SurfaceView`] has been created. This is /// On Android, the `Resumed` event is sent when a new [`SurfaceView`] has been created. This is
/// expected to closely correlate with the [`onResume`] lifecycle event but there may /// expected to closely correlate with the [`onResume`] lifecycle event but there may technically
/// technically be a discrepancy. /// be a discrepancy.
/// ///
/// [`onResume`]: https://developer.android.com/reference/android/app/Activity#onResume() /// [`onResume`]: https://developer.android.com/reference/android/app/Activity#onResume()
/// ///
@@ -110,8 +109,7 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// Emitted when the event loop is about to block and wait for new events. /// Emitted when the event loop is about to block and wait for new events.
/// ///
/// Most applications shouldn't need to hook into this event since there is no real relationship /// Most applications shouldn't need to hook into this event since there is no real relationship
/// between how often the event loop needs to wake up and the dispatching of any specific /// between how often the event loop needs to wake up and the dispatching of any specific events.
/// events.
/// ///
/// High frequency event sources, such as input devices could potentially lead to lots of wake /// High frequency event sources, such as input devices could potentially lead to lots of wake
/// ups and also lots of corresponding `AboutToWait` events. /// ups and also lots of corresponding `AboutToWait` events.
@@ -135,8 +133,7 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// Considering that the implementation of `Suspended` and [`Resumed`] events may be internally /// Considering that the implementation of `Suspended` and [`Resumed`] events may be internally
/// driven by multiple platform-specific events, and that there may be subtle differences across /// driven by multiple platform-specific events, and that there may be subtle differences across
/// platforms with how these internal events are delivered, it's recommended that applications /// platforms with how these internal events are delivered, it's recommended that applications
/// be able to gracefully handle redundant (i.e. back-to-back) `Suspended` or [`Resumed`] /// be able to gracefully handle redundant (i.e. back-to-back) `Suspended` or [`Resumed`] events.
/// events.
/// ///
/// Also see [`Resumed`] notes. /// Also see [`Resumed`] notes.
/// ///
@@ -153,8 +150,7 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// created outside of Winit (such as an `EGLSurface`, [`VkSurfaceKHR`] or [`wgpu::Surface`]). /// created outside of Winit (such as an `EGLSurface`, [`VkSurfaceKHR`] or [`wgpu::Surface`]).
/// ///
/// After being `Suspended` on Android applications must drop all render surfaces before /// After being `Suspended` on Android applications must drop all render surfaces before
/// the event callback completes, which may be re-created when the application is next /// the event callback completes, which may be re-created when the application is next [`Resumed`].
/// [`Resumed`].
/// ///
/// [`SurfaceView`]: https://developer.android.com/reference/android/view/SurfaceView /// [`SurfaceView`]: https://developer.android.com/reference/android/view/SurfaceView
/// [Activity lifecycle]: https://developer.android.com/guide/components/activities/activity-lifecycle /// [Activity lifecycle]: https://developer.android.com/guide/components/activities/activity-lifecycle
@@ -190,7 +186,7 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// Emitted when the event loop is being shut down. /// Emitted when the event loop is being shut down.
/// ///
/// This is irreversible - if this method is called, it is guaranteed that the event loop /// This is irreversible - if this method is called, it is guaranteed that the event loop
/// will exit right after. /// will exist right after.
fn exiting(&mut self, event_loop: &ActiveEventLoop) { fn exiting(&mut self, event_loop: &ActiveEventLoop) {
let _ = event_loop; let _ = event_loop;
} }
@@ -201,17 +197,17 @@ pub trait ApplicationHandler<T: 'static = ()> {
/// ///
/// ### Android /// ### Android
/// ///
/// On Android, the `MemoryWarning` event is sent when [`onLowMemory`] was called. The /// On Android, the `MemoryWarning` event is sent when [`onLowMemory`] was called. The application
/// application must [release memory] or risk being killed. /// must [release memory] or risk being killed.
/// ///
/// [`onLowMemory`]: https://developer.android.com/reference/android/app/Application.html#onLowMemory() /// [`onLowMemory`]: https://developer.android.com/reference/android/app/Application.html#onLowMemory()
/// [release memory]: https://developer.android.com/topic/performance/memory#release /// [release memory]: https://developer.android.com/topic/performance/memory#release
/// ///
/// ### iOS /// ### iOS
/// ///
/// On iOS, the `MemoryWarning` event is emitted in response to an /// On iOS, the `MemoryWarning` event is emitted in response to an [`applicationDidReceiveMemoryWarning`]
/// [`applicationDidReceiveMemoryWarning`] callback. The application must free as much /// callback. The application must free as much memory as possible or risk being terminated, see
/// memory as possible or risk being terminated, see [how to respond to memory warnings]. /// [how to respond to memory warnings].
/// ///
/// [`applicationDidReceiveMemoryWarning`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623063-applicationdidreceivememorywarni /// [`applicationDidReceiveMemoryWarning`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623063-applicationdidreceivememorywarni
/// [how to respond to memory warnings]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle/responding_to_memory_warnings /// [how to respond to memory warnings]: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle/responding_to_memory_warnings
@@ -223,117 +219,3 @@ pub trait ApplicationHandler<T: 'static = ()> {
let _ = event_loop; let _ = event_loop;
} }
} }
impl<A: ?Sized + ApplicationHandler<T>, T: 'static> ApplicationHandler<T> for &mut A {
#[inline]
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause) {
(**self).new_events(event_loop, cause);
}
#[inline]
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
(**self).resumed(event_loop);
}
#[inline]
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: T) {
(**self).user_event(event_loop, event);
}
#[inline]
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: WindowId,
event: WindowEvent,
) {
(**self).window_event(event_loop, window_id, event);
}
#[inline]
fn device_event(
&mut self,
event_loop: &ActiveEventLoop,
device_id: DeviceId,
event: DeviceEvent,
) {
(**self).device_event(event_loop, device_id, event);
}
#[inline]
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
(**self).about_to_wait(event_loop);
}
#[inline]
fn suspended(&mut self, event_loop: &ActiveEventLoop) {
(**self).suspended(event_loop);
}
#[inline]
fn exiting(&mut self, event_loop: &ActiveEventLoop) {
(**self).exiting(event_loop);
}
#[inline]
fn memory_warning(&mut self, event_loop: &ActiveEventLoop) {
(**self).memory_warning(event_loop);
}
}
impl<A: ?Sized + ApplicationHandler<T>, T: 'static> ApplicationHandler<T> for Box<A> {
#[inline]
fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause) {
(**self).new_events(event_loop, cause);
}
#[inline]
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
(**self).resumed(event_loop);
}
#[inline]
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: T) {
(**self).user_event(event_loop, event);
}
#[inline]
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: WindowId,
event: WindowEvent,
) {
(**self).window_event(event_loop, window_id, event);
}
#[inline]
fn device_event(
&mut self,
event_loop: &ActiveEventLoop,
device_id: DeviceId,
event: DeviceEvent,
) {
(**self).device_event(event_loop, device_id, event);
}
#[inline]
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
(**self).about_to_wait(event_loop);
}
#[inline]
fn suspended(&mut self, event_loop: &ActiveEventLoop) {
(**self).suspended(event_loop);
}
#[inline]
fn exiting(&mut self, event_loop: &ActiveEventLoop) {
(**self).exiting(event_loop);
}
#[inline]
fn memory_warning(&mut self, event_loop: &ActiveEventLoop) {
(**self).memory_warning(event_loop);
}
}

View File

@@ -2,13 +2,11 @@
//! //!
//! All notable changes to this project will be documented in this module, //! All notable changes to this project will be documented in this module,
//! along with migration instructions for larger changes. //! along with migration instructions for larger changes.
//!
// Put the current entry at the top of this page, for discoverability. // Put the current entry at the top of this page, for discoverability.
// See `.cargo/config.toml` for details about `unreleased_changelogs`. // See `.cargo/config.toml` for details about `unreleased_changelogs`.
#![cfg_attr(unreleased_changelogs, doc = include_str!("unreleased.md"))] #![cfg_attr(unreleased_changelogs, doc = include_str!("unreleased.md"))]
#![cfg_attr(not(unreleased_changelogs), doc = include_str!("v0.30.md"))] #![cfg_attr(not(unreleased_changelogs), doc = include_str!("v0.29.md"))]
#[doc = include_str!("v0.30.md")]
pub mod v0_30 {}
#[doc = include_str!("v0.29.md")] #[doc = include_str!("v0.29.md")]
pub mod v0_29 {} pub mod v0_29 {}

View File

@@ -1,41 +1,42 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
The sections should follow the order `Added`, `Changed`, `Deprecated`,
`Removed`, and `Fixed`.
Platform specific changed should be added to the end of the section and grouped
by platform name. Common API additions should have `, implemented` at the end
for platforms where the API was initially implemented. See the following example
on how to add them:
```md
### Added
- Add `Window::turbo()`, implemented on X11, Wayland, and Web.
- On X11, add `Window::some_rare_api`.
- On X11, add `Window::even_more_rare_api`.
- On Wayland, add `Window::common_api`.
- On Windows, add `Window::some_rare_api`.
```
When the change requires non-trivial amount of work for users to comply
with it, the migration guide should be added below the entry, like:
```md
- Deprecate `Window` creation outside of `EventLoop::run`
This was done to simply migration in the future. Consider the
following code:
// Code snippet.
To migrate it we should do X, Y, and then Z, for example:
// Code snippet.
```
The migration guide could reference other migration examples in the current
changelog entry.
## Unreleased ## Unreleased
- Deprecate `EventLoop::run` in favor of `EventLoop::run_app`.
- Deprecate `EventLoopExtRunOnDemand::run_on_demand` in favor of `EventLoop::run_app_on_demand`.
- Deprecate `EventLoopExtPumpEvents::pump_events` in favor of `EventLoopExtPumpEvents::pump_app_events`.
- Add `ApplicationHandler<T>` trait which mimics `Event<T>`.
- Move `dpi` types to its own crate, and re-export it from the root crate.
- Implement `Sync` for `EventLoopProxy<T: Send>`.
- **Breaking:** Move `Window::new` to `ActiveEventLoop::create_window` and `EventLoop::create_window` (with the latter being deprecated).
- **Breaking:** Rename `EventLoopWindowTarget` to `ActiveEventLoop`.
- **Breaking:** Remove `Deref` implementation for `EventLoop` that gave `EventLoopWindowTarget`.
- **Breaking**: Remove `WindowBuilder` in favor of `WindowAttributes`.
- **Breaking:** Removed unnecessary generic parameter `T` from `EventLoopWindowTarget`.
- On Windows, macOS, X11, Wayland and Web, implement setting images as cursors. See the `custom_cursors.rs` example.
- **Breaking:** Remove `Window::set_cursor_icon`
- Add `WindowBuilder::with_cursor` and `Window::set_cursor` which takes a `CursorIcon` or `CustomCursor`
- Add `CustomCursor::from_rgba` to allow creating cursor images from RGBA data.
- Add `CustomCursorExtWebSys::from_url` to allow loading cursor images from URLs.
- Add `CustomCursorExtWebSys::from_animation` to allow creating animated cursors from other `CustomCursor`s.
- Add `{Active,}EventLoop::create_custom_cursor` to load custom cursor image sources.
- On macOS, add services menu.
- **Breaking:** On Web, remove queuing fullscreen request in absence of transient activation.
- On Web, fix setting cursor icon overriding cursor visibility.
- **Breaking:** On Web, return `RawWindowHandle::WebCanvas` instead of `RawWindowHandle::Web`.
- **Breaking:** On Web, macOS and iOS, return `HandleError::Unavailable` when a window handle is not available.
- **Breaking:** Bump MSRV from `1.65` to `1.70`.
- On Web, add the ability to toggle calling `Event.preventDefault()` on `Window`.
- **Breaking:** Remove `WindowAttributes::fullscreen()` and expose as field directly.
- **Breaking:** Rename `VideoMode` to `VideoModeHandle` to represent that it doesn't hold static data.
- **Breaking:** No longer export `platform::x11::XNotSupported`.
- **Breaking:** Renamed `platform::x11::XWindowType` to `platform::x11::WindowType`.
- Add the `OwnedDisplayHandle` type for allowing safe display handle usage outside of trivial cases.
- **Breaking:** Rename `TouchpadMagnify` to `PinchGesture`, `SmartMagnify` to `DoubleTapGesture` and `TouchpadRotate` to `RotationGesture` to represent the action rather than the intent.
- on iOS, add detection support for `PinchGesture`, `DoubleTapGesture` and `RotationGesture`.
- on Windows: add `with_system_backdrop`, `with_border_color`, `with_title_background_color`, `with_title_text_color` and `with_corner_preference`
- On Windows, Remove `WS_CAPTION`, `WS_BORDER` and `WS_EX_WINDOWEDGE` styles for child windows without decorations.
- **Breaking:** Removed `EventLoopError::AlreadyRunning`, which can't happen as it is already prevented by the type system.
- Added `EventLoop::builder`, which is intended to replace the (now deprecated) `EventLoopBuilder::new`.
- **Breaking:** Changed the signature of `EventLoop::with_user_event` to return a builder.
- **Breaking:** Removed `EventLoopBuilder::with_user_event`, the functionality is now available in `EventLoop::with_user_event`.
- Add `Window::default_attributes` to get default `WindowAttributes`.
- `log` has been replaced with `tracing`. The old behavior can be emulated by setting the `log` feature on the `tracing` crate.

View File

@@ -1,249 +0,0 @@
## 0.30.2
### Fixed
- On Web, fix `EventLoopProxy::send_event()` triggering event loop immediately
when not called from inside the event loop. Now queues a microtask instead.
- On Web, stop overwriting default cursor with `CursorIcon::Default`.
- On Web, prevent crash when using `InnerSizeWriter::request_inner_size()`.
- On macOS, fix not working opacity for entire window.
## 0.30.1
### Added
- Reexport `raw-window-handle` versions 0.4 and 0.5 as `raw_window_handle_04` and `raw_window_handle_05`.
- Implement `ApplicationHandler` for `&mut` references and heap allocations to something that implements `ApplicationHandler`.
### Fixed
- On macOS, fix panic on exit when dropping windows outside the event loop.
- On macOS, fix window dragging glitches when dragging across a monitor boundary with different scale factor.
- On macOS, fix the range in `Ime::Preedit`.
- On macOS, use the system's internal mechanisms for queuing events.
- On macOS, handle events directly instead of queuing when possible.
## 0.30.0
### Added
- Add `OwnedDisplayHandle` type for allowing safe display handle usage outside of
trivial cases.
- Add `ApplicationHandler<T>` trait which mimics `Event<T>`.
- Add `WindowBuilder::with_cursor` and `Window::set_cursor` which takes a
`CursorIcon` or `CustomCursor`.
- Add `Sync` implementation for `EventLoopProxy<T: Send>`.
- Add `Window::default_attributes` to get default `WindowAttributes`.
- Add `EventLoop::builder` to get `EventLoopBuilder` without export.
- Add `CustomCursor::from_rgba` to allow creating cursor images from RGBA data.
- Add `CustomCursorExtWebSys::from_url` to allow loading cursor images from URLs.
- Add `CustomCursorExtWebSys::from_animation` to allow creating animated
cursors from other `CustomCursor`s.
- Add `{Active,}EventLoop::create_custom_cursor` to load custom cursor image sources.
- Add `ActiveEventLoop::create_window` and `EventLoop::create_window`.
- Add `CustomCursor` which could be set via `Window::set_cursor`, implemented on
Windows, macOS, X11, Wayland, and Web.
- On Web, add to toggle calling `Event.preventDefault()` on `Window`.
- On iOS, add `PinchGesture`, `DoubleTapGesture`, `PanGesture` and `RotationGesture`.
- on iOS, use `UIGestureRecognizerDelegate` for fine grained control of gesture recognizers.
- On macOS, add services menu.
- On Windows, add `with_title_text_color`, and `with_corner_preference` on
`WindowAttributesExtWindows`.
- On Windows, implement resize increments.
- On Windows, add `AnyThread` API to access window handle off the main thread.
### Changed
- Bump MSRV from `1.65` to `1.70`.
- On Wayland, bump `sctk-adwaita` to `0.9.0`, which changed system library
crates. This change is a **cascading breaking change**, you must do breaking
change as well, even if you don't expose winit.
- Rename `TouchpadMagnify` to `PinchGesture`.
- Rename `SmartMagnify` to `DoubleTapGesture`.
- Rename `TouchpadRotate` to `RotationGesture`.
- Rename `EventLoopWindowTarget` to `ActiveEventLoop`.
- Rename `platform::x11::XWindowType` to `platform::x11::WindowType`.
- Rename `VideoMode` to `VideoModeHandle` to represent that it doesn't hold
static data.
- Make `Debug` formatting of `WindowId` more concise.
- Move `dpi` types to its own crate, and re-export it from the root crate.
- Replace `log` with `tracing`, use `log` feature on `tracing` to restore old
behavior.
- `EventLoop::with_user_event` now returns `EventLoopBuilder`.
- On Web, return `HandleError::Unavailable` when a window handle is not available.
- On Web, return `RawWindowHandle::WebCanvas` instead of `RawWindowHandle::Web`.
- On Web, remove queuing fullscreen request in absence of transient activation.
- On iOS, return `HandleError::Unavailable` when a window handle is not available.
- On macOS, return `HandleError::Unavailable` when a window handle is not available.
- On Windows, remove `WS_CAPTION`, `WS_BORDER`, and `WS_EX_WINDOWEDGE` styles
for child windows without decorations.
- On Android, bump `ndk` to `0.9.0` and `android-activity` to `0.6.0`,
and remove unused direct dependency on `ndk-sys`.
### Deprecated
- Deprecate `EventLoop::run`, use `EventLoop::run_app`.
- Deprecate `EventLoopExtRunOnDemand::run_on_demand`, use `EventLoop::run_app_on_demand`.
- Deprecate `EventLoopExtPumpEvents::pump_events`, use `EventLoopExtPumpEvents::pump_app_events`.
The new `app` APIs accept a newly added `ApplicationHandler<T>` instead of
`Fn`. The semantics are mostly the same, given that the capture list of the
closure is your new `State`. Consider the following code:
```rust,no_run
use winit::event::Event;
use winit::event_loop::EventLoop;
use winit::window::Window;
struct MyUserEvent;
let event_loop = EventLoop::<MyUserEvent>::with_user_event().build().unwrap();
let window = event_loop.create_window(Window::default_attributes()).unwrap();
let mut counter = 0;
let _ = event_loop.run(move |event, event_loop| {
match event {
Event::AboutToWait => {
window.request_redraw();
counter += 1;
}
Event::WindowEvent { window_id, event } => {
// Handle window event.
}
Event::UserEvent(event) => {
// Handle user event.
}
Event::DeviceEvent { device_id, event } => {
// Handle device event.
}
_ => (),
}
});
```
To migrate this code, you should move all the captured values into some
newtype `State` and implement `ApplicationHandler` for this type. Finally,
we move particular `match event` arms into methods on `ApplicationHandler`,
for example:
```rust,no_run
use winit::application::ApplicationHandler;
use winit::event::{Event, WindowEvent, DeviceEvent, DeviceId};
use winit::event_loop::{EventLoop, ActiveEventLoop};
use winit::window::{Window, WindowId};
struct MyUserEvent;
struct State {
window: Window,
counter: i32,
}
impl ApplicationHandler<MyUserEvent> for State {
fn user_event(&mut self, event_loop: &ActiveEventLoop, user_event: MyUserEvent) {
// Handle user event.
}
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
// Your application got resumed.
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
// Handle window event.
}
fn device_event(&mut self, event_loop: &ActiveEventLoop, device_id: DeviceId, event: DeviceEvent) {
// Handle device event.
}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
self.window.request_redraw();
self.counter += 1;
}
}
let event_loop = EventLoop::<MyUserEvent>::with_user_event().build().unwrap();
#[allow(deprecated)]
let window = event_loop.create_window(Window::default_attributes()).unwrap();
let mut state = State { window, counter: 0 };
let _ = event_loop.run_app(&mut state);
```
Please submit your feedback after migrating in [this issue](https://github.com/rust-windowing/winit/issues/3626).
- Deprecate `Window::set_cursor_icon`, use `Window::set_cursor`.
### Removed
- Remove `Window::new`, use `ActiveEventLoop::create_window` instead.
You now have to create your windows inside the actively running event loop
(usually the `new_events(cause: StartCause::Init)` or `resumed()` events),
and can no longer do it before the application has properly launched.
This change is done to fix many long-standing issues on iOS and macOS, and
will improve things on Wayland once fully implemented.
To ease migration, we provide the deprecated `EventLoop::create_window` that
will allow you to bypass this restriction in this release.
Using the migration example from above, you can change your code as follows:
```rust,no_run
use winit::application::ApplicationHandler;
use winit::event::{Event, WindowEvent, DeviceEvent, DeviceId};
use winit::event_loop::{EventLoop, ActiveEventLoop};
use winit::window::{Window, WindowId};
#[derive(Default)]
struct State {
// Use an `Option` to allow the window to not be available until the
// application is properly running.
window: Option<Window>,
counter: i32,
}
impl ApplicationHandler for State {
// This is a common indicator that you can create a window.
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap());
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
// `unwrap` is fine, the window will always be available when
// receiving a window event.
let window = self.window.as_ref().unwrap();
// Handle window event.
}
fn device_event(&mut self, event_loop: &ActiveEventLoop, device_id: DeviceId, event: DeviceEvent) {
// Handle window event.
}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
if let Some(window) = self.window.as_ref() {
window.request_redraw();
self.counter += 1;
}
}
}
let event_loop = EventLoop::new().unwrap();
let mut state = State::default();
let _ = event_loop.run_app(&mut state);
```
- Remove `Deref` implementation for `EventLoop` that gave `EventLoopWindowTarget`.
- Remove `WindowBuilder` in favor of `WindowAttributes`.
- Remove Generic parameter `T` from `ActiveEventLoop`.
- Remove `EventLoopBuilder::with_user_event`, use `EventLoop::with_user_event`.
- Remove Redundant `EventLoopError::AlreadyRunning`.
- Remove `WindowAttributes::fullscreen` and expose as field directly.
- On X11, remove `platform::x11::XNotSupported` export.
### Fixed
- On Web, fix setting cursor icon overriding cursor visibility.
- On Windows, fix cursor not confined to center of window when grabbed and hidden.
- On macOS, fix sequence of mouse events being out of order when dragging on the trackpad.
- On Wayland, fix decoration glitch on close with some compositors.
- On Android, fix a regression introduced in #2748 to allow volume key events to be received again.
- On Windows, don't return a valid window handle outside of the GUI thread.
- On macOS, don't set the background color when initializing a window with transparency.

View File

@@ -1,7 +1,7 @@
use core::fmt; use core::fmt;
use std::error::Error; use std::hash::Hasher;
use std::hash::{Hash, Hasher};
use std::sync::Arc; use std::sync::Arc;
use std::{error::Error, hash::Hash};
use cursor_icon::CursorIcon; use cursor_icon::CursorIcon;
@@ -88,9 +88,14 @@ impl CustomCursor {
hotspot_x: u16, hotspot_x: u16,
hotspot_y: u16, hotspot_y: u16,
) -> Result<CustomCursorSource, BadImage> { ) -> Result<CustomCursorSource, BadImage> {
let _span = let _span = tracing::debug_span!(
tracing::debug_span!("winit::Cursor::from_rgba", width, height, hotspot_x, hotspot_y) "winit::Cursor::from_rgba",
.entered(); width,
height,
hotspot_x,
hotspot_y
)
.entered();
Ok(CustomCursorSource { Ok(CustomCursorSource {
inner: PlatformCustomCursorSource::from_rgba( inner: PlatformCustomCursorSource::from_rgba(
@@ -124,36 +129,45 @@ pub enum BadImage {
ByteCountNotDivisibleBy4 { byte_count: usize }, ByteCountNotDivisibleBy4 { byte_count: usize },
/// Produced when the number of pixels (`rgba.len() / 4`) isn't equal to `width * height`. /// Produced when the number of pixels (`rgba.len() / 4`) isn't equal to `width * height`.
/// At least one of your arguments is incorrect. /// At least one of your arguments is incorrect.
DimensionsVsPixelCount { width: u16, height: u16, width_x_height: u64, pixel_count: u64 }, DimensionsVsPixelCount {
width: u16,
height: u16,
width_x_height: u64,
pixel_count: u64,
},
/// Produced when the hotspot is outside the image bounds /// Produced when the hotspot is outside the image bounds
HotspotOutOfBounds { width: u16, height: u16, hotspot_x: u16, hotspot_y: u16 }, HotspotOutOfBounds {
width: u16,
height: u16,
hotspot_x: u16,
hotspot_y: u16,
},
} }
impl fmt::Display for BadImage { impl fmt::Display for BadImage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
BadImage::TooLarge { width, height } => write!( BadImage::TooLarge { width, height } => write!(f,
f, "The specified dimensions ({width:?}x{height:?}) are too large. The maximum is {MAX_CURSOR_SIZE:?}x{MAX_CURSOR_SIZE:?}.",
"The specified dimensions ({width:?}x{height:?}) are too large. The maximum is \
{MAX_CURSOR_SIZE:?}x{MAX_CURSOR_SIZE:?}.",
), ),
BadImage::ByteCountNotDivisibleBy4 { byte_count } => write!( BadImage::ByteCountNotDivisibleBy4 { byte_count } => write!(f,
f, "The length of the `rgba` argument ({byte_count:?}) isn't divisible by 4, making it impossible to interpret as 32bpp RGBA pixels.",
"The length of the `rgba` argument ({byte_count:?}) isn't divisible by 4, making \
it impossible to interpret as 32bpp RGBA pixels.",
), ),
BadImage::DimensionsVsPixelCount { width, height, width_x_height, pixel_count } => { BadImage::DimensionsVsPixelCount {
write!( width,
f, height,
"The specified dimensions ({width:?}x{height:?}) don't match the number of \ width_x_height,
pixels supplied by the `rgba` argument ({pixel_count:?}). For those \ pixel_count,
dimensions, the expected pixel count is {width_x_height:?}.", } => write!(f,
) "The specified dimensions ({width:?}x{height:?}) don't match the number of pixels supplied by the `rgba` argument ({pixel_count:?}). For those dimensions, the expected pixel count is {width_x_height:?}.",
}, ),
BadImage::HotspotOutOfBounds { width, height, hotspot_x, hotspot_y } => write!( BadImage::HotspotOutOfBounds {
f, width,
"The specified hotspot ({hotspot_x:?}, {hotspot_y:?}) is outside the image bounds \ height,
({width:?}x{height:?}).", hotspot_x,
hotspot_y,
} => write!(f,
"The specified hotspot ({hotspot_x:?}, {hotspot_y:?}) is outside the image bounds ({width:?}x{height:?}).",
), ),
} }
} }
@@ -222,7 +236,9 @@ impl CursorImage {
} }
if rgba.len() % PIXEL_SIZE != 0 { if rgba.len() % PIXEL_SIZE != 0 {
return Err(BadImage::ByteCountNotDivisibleBy4 { byte_count: rgba.len() }); return Err(BadImage::ByteCountNotDivisibleBy4 {
byte_count: rgba.len(),
});
} }
let pixel_count = (rgba.len() / PIXEL_SIZE) as u64; let pixel_count = (rgba.len() / PIXEL_SIZE) as u64;
@@ -237,10 +253,21 @@ impl CursorImage {
} }
if hotspot_x >= width || hotspot_y >= height { if hotspot_x >= width || hotspot_y >= height {
return Err(BadImage::HotspotOutOfBounds { width, height, hotspot_x, hotspot_y }); return Err(BadImage::HotspotOutOfBounds {
width,
height,
hotspot_x,
hotspot_y,
});
} }
Ok(CursorImage { rgba, width, height, hotspot_x, hotspot_y }) Ok(CursorImage {
rgba,
width,
height,
hotspot_x,
hotspot_y,
})
} }
} }

View File

@@ -71,7 +71,10 @@ macro_rules! os_error {
impl fmt::Display for OsError { impl fmt::Display for OsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.pad(&format!("os error at {}:{}: {}", self.file, self.line, self.error)) f.pad(&format!(
"os error at {}:{}: {}",
self.file, self.line, self.error
))
} }
} }
@@ -114,14 +117,19 @@ impl error::Error for NotSupportedError {}
impl error::Error for EventLoopError {} impl error::Error for EventLoopError {}
#[cfg(test)] #[cfg(test)]
#[allow(clippy::redundant_clone)]
mod tests { mod tests {
#![allow(clippy::redundant_clone)]
use super::*; use super::*;
// Eat attributes for testing // Eat attributes for testing
#[test] #[test]
fn ensure_fmt_does_not_panic() { fn ensure_fmt_does_not_panic() {
let _ = format!("{:?}, {}", NotSupportedError::new(), NotSupportedError::new().clone()); let _ = format!(
"{:?}, {}",
NotSupportedError::new(),
NotSupportedError::new().clone()
);
let _ = format!( let _ = format!(
"{:?}, {}", "{:?}, {}",
ExternalError::NotSupported(NotSupportedError::new()), ExternalError::NotSupported(NotSupportedError::new()),

View File

@@ -1,8 +1,7 @@
//! The [`Event`] enum and assorted supporting types. //! The [`Event`] enum and assorted supporting types.
//! //!
//! These are sent to the closure given to [`EventLoop::run_app(...)`], where they get //! These are sent to the closure given to [`EventLoop::run_app(...)`], where they get
//! processed and used to modify the program state. For more details, see the root-level //! processed and used to modify the program state. For more details, see the root-level documentation.
//! documentation.
//! //!
//! Some of these events represent different "parts" of a traditional event-handling loop. You could //! Some of these events represent different "parts" of a traditional event-handling loop. You could
//! approximate the basic ordering loop of [`EventLoop::run_app(...)`] like this: //! approximate the basic ordering loop of [`EventLoop::run_app(...)`] like this:
@@ -45,14 +44,16 @@ use smol_str::SmolStr;
#[cfg(web_platform)] #[cfg(web_platform)]
use web_time::Instant; use web_time::Instant;
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::error::ExternalError; use crate::error::ExternalError;
use crate::event_loop::AsyncRequestSerial;
use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState};
use crate::platform_impl;
#[cfg(doc)] #[cfg(doc)]
use crate::window::Window; use crate::window::Window;
use crate::window::{ActivationToken, Theme, WindowId}; use crate::{
dpi::{PhysicalPosition, PhysicalSize},
event_loop::AsyncRequestSerial,
keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState},
platform_impl,
window::{ActivationToken, Theme, WindowId},
};
/// Describes a generic event. /// Describes a generic event.
/// ///
@@ -67,12 +68,18 @@ pub enum Event<T: 'static> {
/// See [`ApplicationHandler::window_event`] for details. /// See [`ApplicationHandler::window_event`] for details.
/// ///
/// [`ApplicationHandler::window_event`]: crate::application::ApplicationHandler::window_event /// [`ApplicationHandler::window_event`]: crate::application::ApplicationHandler::window_event
WindowEvent { window_id: WindowId, event: WindowEvent }, WindowEvent {
window_id: WindowId,
event: WindowEvent,
},
/// See [`ApplicationHandler::device_event`] for details. /// See [`ApplicationHandler::device_event`] for details.
/// ///
/// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event /// [`ApplicationHandler::device_event`]: crate::application::ApplicationHandler::device_event
DeviceEvent { device_id: DeviceId, event: DeviceEvent }, DeviceEvent {
device_id: DeviceId,
event: DeviceEvent,
},
/// See [`ApplicationHandler::user_event`] for details. /// See [`ApplicationHandler::user_event`] for details.
/// ///
@@ -131,11 +138,17 @@ pub enum StartCause {
/// guaranteed to be equal to or after the requested resume time. /// guaranteed to be equal to or after the requested resume time.
/// ///
/// [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil /// [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
ResumeTimeReached { start: Instant, requested_resume: Instant }, ResumeTimeReached {
start: Instant,
requested_resume: Instant,
},
/// Sent if the OS has new events to send to the window, after a wait was requested. Contains /// Sent if the OS has new events to send to the window, after a wait was requested. Contains
/// the moment the wait was requested and the resume time, if requested. /// the moment the wait was requested and the resume time, if requested.
WaitCancelled { start: Instant, requested_resume: Option<Instant> }, WaitCancelled {
start: Instant,
requested_resume: Option<Instant>,
},
/// Sent if the event loop is being resumed after the loop's control flow was set to /// Sent if the event loop is being resumed after the loop's control flow was set to
/// [`ControlFlow::Poll`]. /// [`ControlFlow::Poll`].
@@ -151,11 +164,18 @@ pub enum StartCause {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum WindowEvent { pub enum WindowEvent {
/// The activation token was delivered back and now could be used. /// The activation token was delivered back and now could be used.
#[cfg_attr(not(any(x11_platform, wayland_platform)), allow(rustdoc::broken_intra_doc_links))] ///
#[cfg_attr(
not(any(x11_platform, wayland_platform)),
allow(rustdoc::broken_intra_doc_links)
)]
/// Delivered in response to [`request_activation_token`]. /// Delivered in response to [`request_activation_token`].
/// ///
/// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token /// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token
ActivationTokenDone { serial: AsyncRequestSerial, token: ActivationToken }, ActivationTokenDone {
serial: AsyncRequestSerial,
token: ActivationToken,
},
/// The size of the window has changed. Contains the client area's new dimensions. /// The size of the window has changed. Contains the client area's new dimensions.
Resized(PhysicalSize<u32>), Resized(PhysicalSize<u32>),
@@ -209,10 +229,10 @@ pub enum WindowEvent {
/// If `true`, the event was generated synthetically by winit /// If `true`, the event was generated synthetically by winit
/// in one of the following circumstances: /// in one of the following circumstances:
/// ///
/// * Synthetic key press events are generated for all keys pressed when a window gains /// * Synthetic key press events are generated for all keys pressed
/// focus. Likewise, synthetic key release events are generated for all keys pressed when /// when a window gains focus. Likewise, synthetic key release events
/// a window goes out of focus. ***Currently, this is only functional on X11 and /// are generated for all keys pressed when a window goes out of focus.
/// Windows*** /// ***Currently, this is only functional on X11 and Windows***
/// ///
/// Otherwise, this value is always `false`. /// Otherwise, this value is always `false`.
is_synthetic: bool, is_synthetic: bool,
@@ -242,10 +262,9 @@ pub enum WindowEvent {
CursorMoved { CursorMoved {
device_id: DeviceId, device_id: DeviceId,
/// (x,y) coords in pixels relative to the top-left corner of the window. Because the range /// (x,y) coords in pixels relative to the top-left corner of the window. Because the range of this data is
/// of this data is limited by the display area and it may have been transformed by /// limited by the display area and it may have been transformed by the OS to implement effects such as cursor
/// the OS to implement effects such as cursor acceleration, it should not be used /// acceleration, it should not be used to implement non-cursor-like interactions such as 3D camera control.
/// to implement non-cursor-like interactions such as 3D camera control.
position: PhysicalPosition<f64>, position: PhysicalPosition<f64>,
}, },
@@ -272,10 +291,18 @@ pub enum WindowEvent {
CursorLeft { device_id: DeviceId }, CursorLeft { device_id: DeviceId },
/// A mouse wheel movement or touchpad scroll occurred. /// A mouse wheel movement or touchpad scroll occurred.
MouseWheel { device_id: DeviceId, delta: MouseScrollDelta, phase: TouchPhase }, MouseWheel {
device_id: DeviceId,
delta: MouseScrollDelta,
phase: TouchPhase,
},
/// An mouse button press has been received. /// An mouse button press has been received.
MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton }, MouseInput {
device_id: DeviceId,
state: ElementState,
button: MouseButton,
},
/// Two-finger pinch gesture, often used for magnification. /// Two-finger pinch gesture, often used for magnification.
/// ///
@@ -293,19 +320,6 @@ pub enum WindowEvent {
phase: TouchPhase, phase: TouchPhase,
}, },
/// N-finger pan gesture
///
/// ## Platform-specific
///
/// - Only available on **iOS**.
/// - On iOS, not recognized by default. It must be enabled when needed.
PanGesture {
device_id: DeviceId,
/// Change in pixels of pan gesture from last update.
delta: PhysicalPosition<f32>,
phase: TouchPhase,
},
/// Double tap gesture. /// Double tap gesture.
/// ///
/// On a Mac, smart magnification is triggered by a double tap with two fingers /// On a Mac, smart magnification is triggered by a double tap with two fingers
@@ -337,7 +351,6 @@ pub enum WindowEvent {
/// - On iOS, not recognized by default. It must be enabled when needed. /// - On iOS, not recognized by default. It must be enabled when needed.
RotationGesture { RotationGesture {
device_id: DeviceId, device_id: DeviceId,
/// change in rotation in degrees
delta: f32, delta: f32,
phase: TouchPhase, phase: TouchPhase,
}, },
@@ -345,12 +358,20 @@ pub enum WindowEvent {
/// Touchpad pressure event. /// Touchpad pressure event.
/// ///
/// At the moment, only supported on Apple forcetouch-capable macbooks. /// At the moment, only supported on Apple forcetouch-capable macbooks.
/// The parameters are: pressure level (value between 0 and 1 representing how hard the /// The parameters are: pressure level (value between 0 and 1 representing how hard the touchpad
/// touchpad is being pressed) and stage (integer representing the click level). /// is being pressed) and stage (integer representing the click level).
TouchpadPressure { device_id: DeviceId, pressure: f32, stage: i64 }, TouchpadPressure {
device_id: DeviceId,
pressure: f32,
stage: i64,
},
/// Motion on some analog axis. May report data redundant to other, more specific events. /// Motion on some analog axis. May report data redundant to other, more specific events.
AxisMotion { device_id: DeviceId, axis: AxisId, value: f64 }, AxisMotion {
device_id: DeviceId,
axis: AxisId,
value: f64,
},
/// Touch event has been received /// Touch event has been received
/// ///
@@ -372,8 +393,8 @@ pub enum WindowEvent {
/// * Changing the display's scale factor (e.g. in Control Panel on Windows). /// * Changing the display's scale factor (e.g. in Control Panel on Windows).
/// * Moving the window to a display with a different scale factor. /// * Moving the window to a display with a different scale factor.
/// ///
/// To update the window size, use the provided [`InnerSizeWriter`] handle. By default, the /// To update the window size, use the provided [`InnerSizeWriter`] handle. By default, the window is
/// window is resized to the value suggested by the OS, but it can be changed to any value. /// resized to the value suggested by the OS, but it can be changed to any value.
/// ///
/// For more information about DPI in general, see the [`dpi`] crate. /// For more information about DPI in general, see the [`dpi`] crate.
ScaleFactorChanged { ScaleFactorChanged {
@@ -403,11 +424,10 @@ pub enum WindowEvent {
/// ///
/// ### iOS /// ### iOS
/// ///
/// On iOS, the `Occluded(false)` event is emitted in response to an /// On iOS, the `Occluded(false)` event is emitted in response to an [`applicationWillEnterForeground`]
/// [`applicationWillEnterForeground`] callback which means the application should start /// callback which means the application should start preparing its data. The `Occluded(true)` event is
/// preparing its data. The `Occluded(true)` event is emitted in response to an /// emitted in response to an [`applicationDidEnterBackground`] callback which means the application
/// [`applicationDidEnterBackground`] callback which means the application should free /// should free resources (according to the [iOS application lifecycle]).
/// resources (according to the [iOS application lifecycle]).
/// ///
/// [`applicationWillEnterForeground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623076-applicationwillenterforeground /// [`applicationWillEnterForeground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623076-applicationwillenterforeground
/// [`applicationDidEnterBackground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground /// [`applicationDidEnterBackground`]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground
@@ -437,10 +457,9 @@ pub enum WindowEvent {
/// Identifier of an input device. /// Identifier of an input device.
/// ///
/// Whenever you receive an event arising from a particular input device, this event contains a /// Whenever you receive an event arising from a particular input device, this event contains a `DeviceId` which
/// `DeviceId` which identifies its origin. Note that devices may be virtual (representing an /// identifies its origin. Note that devices may be virtual (representing an on-screen cursor and keyboard focus) or
/// on-screen cursor and keyboard focus) or physical. Virtual devices typically aggregate inputs /// physical. Virtual devices typically aggregate inputs from multiple physical devices.
/// from multiple physical devices.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(pub(crate) platform_impl::DeviceId); pub struct DeviceId(pub(crate) platform_impl::DeviceId);
@@ -462,10 +481,10 @@ impl DeviceId {
/// Represents raw hardware events that are not associated with any particular window. /// Represents raw hardware events that are not associated with any particular window.
/// ///
/// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera /// Useful for interactions that diverge significantly from a conventional 2D GUI, such as 3D camera or first-person
/// or first-person game controls. Many physical actions, such as mouse movement, can produce both /// game controls. Many physical actions, such as mouse movement, can produce both device and window events. Because
/// device and window events. Because window events typically arise from virtual devices /// window events typically arise from virtual devices (corresponding to GUI cursors and keyboard focus) the device IDs
/// (corresponding to GUI cursors and keyboard focus) the device IDs may not match. /// may not match.
/// ///
/// Note that these events are delivered regardless of input focus. /// Note that these events are delivered regardless of input focus.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@@ -475,8 +494,7 @@ pub enum DeviceEvent {
/// Change in physical position of a pointing device. /// Change in physical position of a pointing device.
/// ///
/// This represents raw, unfiltered physical motion. Not to be confused with /// This represents raw, unfiltered physical motion. Not to be confused with [`WindowEvent::CursorMoved`].
/// [`WindowEvent::CursorMoved`].
MouseMotion { MouseMotion {
/// (x, y) change in position in unspecified units. /// (x, y) change in position in unspecified units.
/// ///
@@ -597,13 +615,13 @@ pub struct KeyEvent {
/// Contains the location of this key on the keyboard. /// Contains the location of this key on the keyboard.
/// ///
/// Certain keys on the keyboard may appear in more than once place. For example, the "Shift" /// Certain keys on the keyboard may appear in more than once place. For example, the "Shift" key
/// key appears on the left side of the QWERTY keyboard as well as the right side. However, /// appears on the left side of the QWERTY keyboard as well as the right side. However, both keys
/// both keys have the same symbolic value. Another example of this phenomenon is the "1" /// have the same symbolic value. Another example of this phenomenon is the "1" key, which appears
/// key, which appears both above the "Q" key and as the "Keypad 1" key. /// both above the "Q" key and as the "Keypad 1" key.
/// ///
/// This field allows the user to differentiate between keys like this that have the same /// This field allows the user to differentiate between keys like this that have the same symbolic
/// symbolic value but different locations on the keyboard. /// value but different locations on the keyboard.
/// ///
/// See the [`KeyLocation`] type for more details. /// See the [`KeyLocation`] type for more details.
/// ///
@@ -618,8 +636,8 @@ pub struct KeyEvent {
/// Whether or not this key is a key repeat event. /// Whether or not this key is a key repeat event.
/// ///
/// On some systems, holding down a key for some period of time causes that key to be repeated /// On some systems, holding down a key for some period of time causes that key to be repeated
/// as though it were being pressed and released repeatedly. This field is `true` if and only /// as though it were being pressed and released repeatedly. This field is `true` if and only if
/// if this event is the result of one of those repeats. /// this event is the result of one of those repeats.
/// ///
/// # Example /// # Example
/// ///
@@ -627,31 +645,30 @@ pub struct KeyEvent {
/// done by ignoring events where this property is set. /// done by ignoring events where this property is set.
/// ///
/// ``` /// ```
/// use winit::event::{ElementState, KeyEvent, WindowEvent}; /// use winit::event::{WindowEvent, KeyEvent, ElementState};
/// use winit::keyboard::{KeyCode, PhysicalKey}; /// use winit::keyboard::{KeyCode, PhysicalKey};
/// # let window_event = WindowEvent::RedrawRequested; // To make the example compile /// # let window_event = WindowEvent::RedrawRequested; // To make the example compile
/// match window_event { /// match window_event {
/// WindowEvent::KeyboardInput { /// WindowEvent::KeyboardInput {
/// event: /// event: KeyEvent {
/// KeyEvent { /// physical_key: PhysicalKey::Code(KeyCode::KeyW),
/// physical_key: PhysicalKey::Code(KeyCode::KeyW), /// state: ElementState::Pressed,
/// state: ElementState::Pressed, /// repeat: false,
/// repeat: false, /// ..
/// .. /// },
/// },
/// .. /// ..
/// } => { /// } => {
/// // The physical key `W` was pressed, and it was not a repeat /// // The physical key `W` was pressed, and it was not a repeat
/// }, /// }
/// _ => {}, // Handle other events /// _ => {} // Handle other events
/// } /// }
/// ``` /// ```
pub repeat: bool, pub repeat: bool,
/// Platform-specific key event information. /// Platform-specific key event information.
/// ///
/// On Windows, Linux and macOS, this type contains the key without modifiers and the text with /// On Windows, Linux and macOS, this type contains the key without modifiers and the text with all
/// all modifiers applied. /// modifiers applied.
/// ///
/// On Android, iOS, Redox and Web, this type is a no-op. /// On Android, iOS, Redox and Web, this type is a no-op.
pub(crate) platform_specific: platform_impl::KeyEventExtra, pub(crate) platform_specific: platform_impl::KeyEventExtra,
@@ -725,7 +742,10 @@ impl Modifiers {
impl From<ModifiersState> for Modifiers { impl From<ModifiersState> for Modifiers {
fn from(value: ModifiersState) -> Self { fn from(value: ModifiersState) -> Self {
Self { state: value, pressed_mods: Default::default() } Self {
state: value,
pressed_mods: Default::default(),
}
} }
} }
@@ -733,16 +753,12 @@ impl From<ModifiersState> for Modifiers {
/// ///
/// This is also called a "composition event". /// This is also called a "composition event".
/// ///
/// Most keypresses using a latin-like keyboard layout simply generate a /// Most keypresses using a latin-like keyboard layout simply generate a [`WindowEvent::KeyboardInput`].
/// [`WindowEvent::KeyboardInput`]. However, one couldn't possibly have a key for every single /// However, one couldn't possibly have a key for every single unicode character that the user might want to type
/// unicode character that the user might want to type /// - so the solution operating systems employ is to allow the user to type these using _a sequence of keypresses_ instead.
/// - so the solution operating systems employ is to allow the user to type these using _a sequence
/// of keypresses_ instead.
///
/// A prominent example of this is accents - many keyboard layouts allow you to first click the
/// "accent key", and then the character you want to apply the accent to. In this case, some
/// platforms will generate the following event sequence:
/// ///
/// A prominent example of this is accents - many keyboard layouts allow you to first click the "accent key", and then
/// the character you want to apply the accent to. In this case, some platforms will generate the following event sequence:
/// ```ignore /// ```ignore
/// // Press "`" key /// // Press "`" key
/// Ime::Preedit("`", Some((0, 0))) /// Ime::Preedit("`", Some((0, 0)))
@@ -751,13 +767,11 @@ impl From<ModifiersState> for Modifiers {
/// Ime::Commit("é") /// Ime::Commit("é")
/// ``` /// ```
/// ///
/// Additionally, certain input devices are configured to display a candidate box that allow the /// Additionally, certain input devices are configured to display a candidate box that allow the user to select the
/// user to select the desired character interactively. (To properly position this box, you must use /// desired character interactively. (To properly position this box, you must use [`Window::set_ime_cursor_area`].)
/// [`Window::set_ime_cursor_area`].)
///
/// An example of a keyboard layout which uses candidate boxes is pinyin. On a latin keyboard the
/// following event sequence could be obtained:
/// ///
/// An example of a keyboard layout which uses candidate boxes is pinyin. On a latin keyboard the following event
/// sequence could be obtained:
/// ```ignore /// ```ignore
/// // Press "A" key /// // Press "A" key
/// Ime::Preedit("a", Some((1, 1))) /// Ime::Preedit("a", Some((1, 1)))
@@ -799,8 +813,8 @@ pub enum Ime {
/// ///
/// After receiving this event you won't get any more [`Preedit`][Self::Preedit] or /// After receiving this event you won't get any more [`Preedit`][Self::Preedit] or
/// [`Commit`][Self::Commit] events until the next [`Enabled`][Self::Enabled] event. You should /// [`Commit`][Self::Commit] events until the next [`Enabled`][Self::Enabled] event. You should
/// also stop issuing IME related requests like [`Window::set_ime_cursor_area`] and clear /// also stop issuing IME related requests like [`Window::set_ime_cursor_area`] and clear pending
/// pending preedit text. /// preedit text.
Disabled, Disabled,
} }
@@ -899,13 +913,17 @@ impl Force {
/// consistent across devices. /// consistent across devices.
pub fn normalized(&self) -> f64 { pub fn normalized(&self) -> f64 {
match self { match self {
Force::Calibrated { force, max_possible_force, altitude_angle } => { Force::Calibrated {
force,
max_possible_force,
altitude_angle,
} => {
let force = match altitude_angle { let force = match altitude_angle {
Some(altitude_angle) => force / altitude_angle.sin(), Some(altitude_angle) => force / altitude_angle.sin(),
None => *force, None => *force,
}; };
force / max_possible_force force / max_possible_force
}, }
Force::Normalized(force) => *force, Force::Normalized(force) => *force,
} }
} }
@@ -1011,7 +1029,6 @@ impl PartialEq for InnerSizeWriter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::dpi::PhysicalPosition;
use crate::event; use crate::event;
use std::collections::{BTreeSet, HashSet}; use std::collections::{BTreeSet, HashSet};
@@ -1023,9 +1040,7 @@ mod tests {
#[allow(deprecated)] #[allow(deprecated)]
{ {
use crate::event::Event::*; use crate::event::{Event::*, Ime::Enabled, WindowEvent::*};
use crate::event::Ime::Enabled;
use crate::event::WindowEvent::*;
use crate::window::WindowId; use crate::window::WindowId;
// Mainline events. // Mainline events.
@@ -1038,7 +1053,12 @@ mod tests {
x(Resumed); x(Resumed);
// Window events. // Window events.
let with_window_event = |wev| x(WindowEvent { window_id: wid, event: wev }); let with_window_event = |wev| {
x(WindowEvent {
window_id: wid,
event: wev,
})
};
with_window_event(CloseRequested); with_window_event(CloseRequested);
with_window_event(Destroyed); with_window_event(Destroyed);
@@ -1049,7 +1069,10 @@ mod tests {
with_window_event(HoveredFile("x.txt".into())); with_window_event(HoveredFile("x.txt".into()));
with_window_event(HoveredFileCancelled); with_window_event(HoveredFileCancelled);
with_window_event(Ime(Enabled)); with_window_event(Ime(Enabled));
with_window_event(CursorMoved { device_id: did, position: (0, 0).into() }); with_window_event(CursorMoved {
device_id: did,
position: (0, 0).into(),
});
with_window_event(ModifiersChanged(event::Modifiers::default())); with_window_event(ModifiersChanged(event::Modifiers::default()));
with_window_event(CursorEntered { device_id: did }); with_window_event(CursorEntered { device_id: did });
with_window_event(CursorLeft { device_id: did }); with_window_event(CursorLeft { device_id: did });
@@ -1074,13 +1097,16 @@ mod tests {
delta: 0.0, delta: 0.0,
phase: event::TouchPhase::Started, phase: event::TouchPhase::Started,
}); });
with_window_event(PanGesture { with_window_event(TouchpadPressure {
device_id: did, device_id: did,
delta: PhysicalPosition::<f32>::new(0.0, 0.0), pressure: 0.0,
phase: event::TouchPhase::Started, stage: 0,
});
with_window_event(AxisMotion {
device_id: did,
axis: 0,
value: 0.0,
}); });
with_window_event(TouchpadPressure { device_id: did, pressure: 0.0, stage: 0 });
with_window_event(AxisMotion { device_id: did, axis: 0, value: 0.0 });
with_window_event(Touch(event::Touch { with_window_event(Touch(event::Touch {
device_id: did, device_id: did,
phase: event::TouchPhase::Started, phase: event::TouchPhase::Started,
@@ -1096,17 +1122,29 @@ mod tests {
{ {
use event::DeviceEvent::*; use event::DeviceEvent::*;
let with_device_event = let with_device_event = |dev_ev| {
|dev_ev| x(event::Event::DeviceEvent { device_id: did, event: dev_ev }); x(event::Event::DeviceEvent {
device_id: did,
event: dev_ev,
})
};
with_device_event(Added); with_device_event(Added);
with_device_event(Removed); with_device_event(Removed);
with_device_event(MouseMotion { delta: (0.0, 0.0).into() }); with_device_event(MouseMotion {
delta: (0.0, 0.0).into(),
});
with_device_event(MouseWheel { with_device_event(MouseWheel {
delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), delta: event::MouseScrollDelta::LineDelta(0.0, 0.0),
}); });
with_device_event(Motion { axis: 0, value: 0.0 }); with_device_event(Motion {
with_device_event(Button { button: 0, state: event::ElementState::Pressed }); axis: 0,
value: 0.0,
});
with_device_event(Button {
button: 0,
state: event::ElementState::Pressed,
});
} }
}}; }};
} }
@@ -1138,8 +1176,11 @@ mod tests {
let force = event::Force::Normalized(0.0); let force = event::Force::Normalized(0.0);
assert_eq!(force.normalized(), 0.0); assert_eq!(force.normalized(), 0.0);
let force2 = let force2 = event::Force::Calibrated {
event::Force::Calibrated { force: 5.0, max_possible_force: 2.5, altitude_angle: None }; force: 5.0,
max_possible_force: 2.5,
altitude_angle: None,
};
assert_eq!(force2.normalized(), 2.0); assert_eq!(force2.normalized(), 2.0);
let force3 = event::Force::Calibrated { let force3 = event::Force::Calibrated {
@@ -1178,8 +1219,11 @@ mod tests {
force: Some(event::Force::Normalized(0.0)), force: Some(event::Force::Normalized(0.0)),
} }
.clone(); .clone();
let _ = let _ = event::Force::Calibrated {
event::Force::Calibrated { force: 0.0, max_possible_force: 0.0, altitude_angle: None } force: 0.0,
.clone(); max_possible_force: 0.0,
altitude_angle: None,
}
.clone();
} }
} }

View File

@@ -20,10 +20,8 @@ use web_time::{Duration, Instant};
use crate::application::ApplicationHandler; use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, OsError}; use crate::error::{EventLoopError, OsError};
use crate::event::Event;
use crate::monitor::MonitorHandle;
use crate::platform_impl;
use crate::window::{CustomCursor, CustomCursorSource, Window, WindowAttributes}; use crate::window::{CustomCursor, CustomCursorSource, Window, WindowAttributes};
use crate::{event::Event, monitor::MonitorHandle, platform_impl};
/// Provides a way to retrieve events from the system and from the windows that were registered to /// Provides a way to retrieve events from the system and from the windows that were registered to
/// the events loop. /// the events loop.
@@ -35,8 +33,8 @@ use crate::window::{CustomCursor, CustomCursorSource, Window, WindowAttributes};
/// To wake up an `EventLoop` from a another thread, see the [`EventLoopProxy`] docs. /// To wake up an `EventLoop` from a another thread, see the [`EventLoopProxy`] docs.
/// ///
/// Note that this cannot be shared across threads (due to platform-dependant logic /// Note that this cannot be shared across threads (due to platform-dependant logic
/// forbidding it), as such it is neither [`Send`] nor [`Sync`]. If you need cross-thread access, /// forbidding it), as such it is neither [`Send`] nor [`Sync`]. If you need cross-thread access, the
/// the [`Window`] created from this _can_ be sent to an other thread, and the /// [`Window`] created from this _can_ be sent to an other thread, and the
/// [`EventLoopProxy`] allows you to wake up an `EventLoop` from another thread. /// [`EventLoopProxy`] allows you to wake up an `EventLoop` from another thread.
/// ///
/// [`Window`]: crate::window::Window /// [`Window`]: crate::window::Window
@@ -96,19 +94,18 @@ impl<T> EventLoopBuilder<T> {
/// ///
/// ## Platform-specific /// ## Platform-specific
/// ///
/// - **Wayland/X11:** to prevent running under `Wayland` or `X11` unset `WAYLAND_DISPLAY` or /// - **Wayland/X11:** to prevent running under `Wayland` or `X11` unset `WAYLAND_DISPLAY`
/// `DISPLAY` respectively when building the event loop. /// or `DISPLAY` respectively when building the event loop.
/// - **Android:** must be configured with an `AndroidApp` from `android_main()` by calling /// - **Android:** must be configured with an `AndroidApp` from `android_main()` by calling
/// [`.with_android_app(app)`] before calling `.build()`, otherwise it'll panic. /// [`.with_android_app(app)`] before calling `.build()`, otherwise it'll panic.
/// ///
/// [`platform`]: crate::platform /// [`platform`]: crate::platform
#[cfg_attr( #[cfg_attr(
android_platform, android,
doc = "[`.with_android_app(app)`]: \ doc = "[`.with_android_app(app)`]: crate::platform::android::EventLoopBuilderExtAndroid::with_android_app"
crate::platform::android::EventLoopBuilderExtAndroid::with_android_app"
)] )]
#[cfg_attr( #[cfg_attr(
not(android_platform), not(android),
doc = "[`.with_android_app(app)`]: #only-available-on-android" doc = "[`.with_android_app(app)`]: #only-available-on-android"
)] )]
#[inline] #[inline]
@@ -165,9 +162,9 @@ pub enum ControlFlow {
/// When the current loop iteration finishes, suspend the thread until either another event /// When the current loop iteration finishes, suspend the thread until either another event
/// arrives or the given time is reached. /// arrives or the given time is reached.
/// ///
/// Useful for implementing efficient timers. Applications which want to render at the /// Useful for implementing efficient timers. Applications which want to render at the display's
/// display's native refresh rate should instead use [`Poll`] and the VSync functionality /// native refresh rate should instead use [`Poll`] and the VSync functionality of a graphics API
/// of a graphics API to reduce odds of missed frames. /// to reduce odds of missed frames.
/// ///
/// [`Poll`]: Self::Poll /// [`Poll`]: Self::Poll
WaitUntil(Instant), WaitUntil(Instant),
@@ -213,7 +210,10 @@ impl<T> EventLoop<T> {
/// Start building a new event loop, with the given type as the user event /// Start building a new event loop, with the given type as the user event
/// type. /// type.
pub fn with_user_event() -> EventLoopBuilder<T> { pub fn with_user_event() -> EventLoopBuilder<T> {
EventLoopBuilder { platform_specific: Default::default(), _p: PhantomData } EventLoopBuilder {
platform_specific: Default::default(),
_p: PhantomData,
}
} }
/// See [`run_app`]. /// See [`run_app`].
@@ -239,9 +239,9 @@ impl<T> EventLoop<T> {
/// ///
/// - **iOS:** Will never return to the caller and so values not passed to this function will /// - **iOS:** Will never return to the caller and so values not passed to this function will
/// *not* be dropped before the process exits. /// *not* be dropped before the process exits.
/// - **Web:** Will _act_ as if it never returns to the caller by throwing a Javascript /// - **Web:** Will _act_ as if it never returns to the caller by throwing a Javascript exception
/// exception (that Rust doesn't see) that will also mean that the rest of the function is /// (that Rust doesn't see) that will also mean that the rest of the function is never executed
/// never executed and any values not passed to this function will *not* be dropped. /// and any values not passed to this function will *not* be dropped.
/// ///
/// Web applications are recommended to use /// Web applications are recommended to use
#[cfg_attr( #[cfg_attr(
@@ -262,20 +262,25 @@ impl<T> EventLoop<T> {
#[inline] #[inline]
#[cfg(not(all(web_platform, target_feature = "exception-handling")))] #[cfg(not(all(web_platform, target_feature = "exception-handling")))]
pub fn run_app<A: ApplicationHandler<T>>(self, app: &mut A) -> Result<(), EventLoopError> { pub fn run_app<A: ApplicationHandler<T>>(self, app: &mut A) -> Result<(), EventLoopError> {
self.event_loop.run(|event, event_loop| dispatch_event_for_app(app, event_loop, event)) self.event_loop
.run(|event, event_loop| dispatch_event_for_app(app, event_loop, event))
} }
/// Creates an [`EventLoopProxy`] that can be used to dispatch user events /// Creates an [`EventLoopProxy`] that can be used to dispatch user events
/// to the main event loop, possibly from another thread. /// to the main event loop, possibly from another thread.
pub fn create_proxy(&self) -> EventLoopProxy<T> { pub fn create_proxy(&self) -> EventLoopProxy<T> {
EventLoopProxy { event_loop_proxy: self.event_loop.create_proxy() } EventLoopProxy {
event_loop_proxy: self.event_loop.create_proxy(),
}
} }
/// Gets a persistent reference to the underlying platform display. /// Gets a persistent reference to the underlying platform display.
/// ///
/// See the [`OwnedDisplayHandle`] type for more information. /// See the [`OwnedDisplayHandle`] type for more information.
pub fn owned_display_handle(&self) -> OwnedDisplayHandle { pub fn owned_display_handle(&self) -> OwnedDisplayHandle {
OwnedDisplayHandle { platform: self.event_loop.window_target().p.owned_display_handle() } OwnedDisplayHandle {
platform: self.event_loop.window_target().p.owned_display_handle(),
}
} }
/// Change if or when [`DeviceEvent`]s are captured. /// Change if or when [`DeviceEvent`]s are captured.
@@ -290,12 +295,18 @@ impl<T> EventLoop<T> {
) )
.entered(); .entered();
self.event_loop.window_target().p.listen_device_events(allowed); self.event_loop
.window_target()
.p
.listen_device_events(allowed);
} }
/// Sets the [`ControlFlow`]. /// Sets the [`ControlFlow`].
pub fn set_control_flow(&self, control_flow: ControlFlow) { pub fn set_control_flow(&self, control_flow: ControlFlow) {
self.event_loop.window_target().p.set_control_flow(control_flow) self.event_loop
.window_target()
.p
.set_control_flow(control_flow)
} }
/// Create a window. /// Create a window.
@@ -318,7 +329,10 @@ impl<T> EventLoop<T> {
/// Create custom cursor. /// Create custom cursor.
pub fn create_custom_cursor(&self, custom_cursor: CustomCursorSource) -> CustomCursor { pub fn create_custom_cursor(&self, custom_cursor: CustomCursorSource) -> CustomCursor {
self.event_loop.window_target().p.create_custom_cursor(custom_cursor) self.event_loop
.window_target()
.p
.create_custom_cursor(custom_cursor)
} }
} }
@@ -399,7 +413,10 @@ impl ActiveEventLoop {
let _span = tracing::debug_span!("winit::ActiveEventLoop::available_monitors",).entered(); let _span = tracing::debug_span!("winit::ActiveEventLoop::available_monitors",).entered();
#[allow(clippy::useless_conversion)] // false positive on some platforms #[allow(clippy::useless_conversion)] // false positive on some platforms
self.p.available_monitors().into_iter().map(|inner| MonitorHandle { inner }) self.p
.available_monitors()
.into_iter()
.map(|inner| MonitorHandle { inner })
} }
/// Returns the primary monitor of the system. /// Returns the primary monitor of the system.
@@ -413,7 +430,9 @@ impl ActiveEventLoop {
pub fn primary_monitor(&self) -> Option<MonitorHandle> { pub fn primary_monitor(&self) -> Option<MonitorHandle> {
let _span = tracing::debug_span!("winit::ActiveEventLoop::primary_monitor",).entered(); let _span = tracing::debug_span!("winit::ActiveEventLoop::primary_monitor",).entered();
self.p.primary_monitor().map(|inner| MonitorHandle { inner }) self.p
.primary_monitor()
.map(|inner| MonitorHandle { inner })
} }
/// Change if or when [`DeviceEvent`]s are captured. /// Change if or when [`DeviceEvent`]s are captured.
@@ -467,7 +486,9 @@ impl ActiveEventLoop {
/// ///
/// See the [`OwnedDisplayHandle`] type for more information. /// See the [`OwnedDisplayHandle`] type for more information.
pub fn owned_display_handle(&self) -> OwnedDisplayHandle { pub fn owned_display_handle(&self) -> OwnedDisplayHandle {
OwnedDisplayHandle { platform: self.p.owned_display_handle() } OwnedDisplayHandle {
platform: self.p.owned_display_handle(),
}
} }
} }
@@ -541,7 +562,9 @@ pub struct EventLoopProxy<T: 'static> {
impl<T: 'static> Clone for EventLoopProxy<T> { impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { event_loop_proxy: self.event_loop_proxy.clone() } Self {
event_loop_proxy: self.event_loop_proxy.clone(),
}
} }
} }

View File

@@ -1,6 +1,5 @@
use crate::platform_impl::PlatformIcon; use crate::platform_impl::PlatformIcon;
use std::error::Error; use std::{error::Error, fmt, io, mem};
use std::{fmt, io, mem};
#[repr(C)] #[repr(C)]
#[derive(Debug)] #[derive(Debug)]
@@ -21,7 +20,12 @@ pub enum BadIcon {
ByteCountNotDivisibleBy4 { byte_count: usize }, ByteCountNotDivisibleBy4 { byte_count: usize },
/// Produced when the number of pixels (`rgba.len() / 4`) isn't equal to `width * height`. /// Produced when the number of pixels (`rgba.len() / 4`) isn't equal to `width * height`.
/// At least one of your arguments is incorrect. /// At least one of your arguments is incorrect.
DimensionsVsPixelCount { width: u32, height: u32, width_x_height: usize, pixel_count: usize }, DimensionsVsPixelCount {
width: u32,
height: u32,
width_x_height: usize,
pixel_count: usize,
},
/// Produced when underlying OS functionality failed to create the icon /// Produced when underlying OS functionality failed to create the icon
OsError(io::Error), OsError(io::Error),
} }
@@ -29,19 +33,17 @@ pub enum BadIcon {
impl fmt::Display for BadIcon { impl fmt::Display for BadIcon {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
BadIcon::ByteCountNotDivisibleBy4 { byte_count } => write!( BadIcon::ByteCountNotDivisibleBy4 { byte_count } => write!(f,
f, "The length of the `rgba` argument ({byte_count:?}) isn't divisible by 4, making it impossible to interpret as 32bpp RGBA pixels.",
"The length of the `rgba` argument ({byte_count:?}) isn't divisible by 4, making \ ),
it impossible to interpret as 32bpp RGBA pixels.", BadIcon::DimensionsVsPixelCount {
width,
height,
width_x_height,
pixel_count,
} => write!(f,
"The specified dimensions ({width:?}x{height:?}) don't match the number of pixels supplied by the `rgba` argument ({pixel_count:?}). For those dimensions, the expected pixel count is {width_x_height:?}.",
), ),
BadIcon::DimensionsVsPixelCount { width, height, width_x_height, pixel_count } => {
write!(
f,
"The specified dimensions ({width:?}x{height:?}) don't match the number of \
pixels supplied by the `rgba` argument ({pixel_count:?}). For those \
dimensions, the expected pixel count is {width_x_height:?}.",
)
},
BadIcon::OsError(e) => write!(f, "OS error when instantiating the icon: {e:?}"), BadIcon::OsError(e) => write!(f, "OS error when instantiating the icon: {e:?}"),
} }
} }
@@ -67,7 +69,9 @@ mod constructors {
impl RgbaIcon { impl RgbaIcon {
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> { pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
if rgba.len() % PIXEL_SIZE != 0 { if rgba.len() % PIXEL_SIZE != 0 {
return Err(BadIcon::ByteCountNotDivisibleBy4 { byte_count: rgba.len() }); return Err(BadIcon::ByteCountNotDivisibleBy4 {
byte_count: rgba.len(),
});
} }
let pixel_count = rgba.len() / PIXEL_SIZE; let pixel_count = rgba.len() / PIXEL_SIZE;
if pixel_count != (width * height) as usize { if pixel_count != (width * height) as usize {
@@ -78,7 +82,11 @@ mod constructors {
pixel_count, pixel_count,
}) })
} else { } else {
Ok(RgbaIcon { rgba, width, height }) Ok(RgbaIcon {
rgba,
width,
height,
})
} }
} }
} }
@@ -112,6 +120,8 @@ impl Icon {
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> { pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
let _span = tracing::debug_span!("winit::Icon::from_rgba", width, height).entered(); let _span = tracing::debug_span!("winit::Icon::from_rgba", width, height).entered();
Ok(Icon { inner: PlatformIcon::from_rgba(rgba, width, height)? }) Ok(Icon {
inner: PlatformIcon::from_rgba(rgba, width, height)?,
})
} }
} }

View File

@@ -106,23 +106,23 @@ impl std::fmt::Debug for NativeKeyCode {
match self { match self {
Unidentified => { Unidentified => {
debug_tuple = f.debug_tuple("Unidentified"); debug_tuple = f.debug_tuple("Unidentified");
}, }
Android(code) => { Android(code) => {
debug_tuple = f.debug_tuple("Android"); debug_tuple = f.debug_tuple("Android");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
MacOS(code) => { MacOS(code) => {
debug_tuple = f.debug_tuple("MacOS"); debug_tuple = f.debug_tuple("MacOS");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
Windows(code) => { Windows(code) => {
debug_tuple = f.debug_tuple("Windows"); debug_tuple = f.debug_tuple("Windows");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
Xkb(code) => { Xkb(code) => {
debug_tuple = f.debug_tuple("Xkb"); debug_tuple = f.debug_tuple("Xkb");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
} }
debug_tuple.finish() debug_tuple.finish()
} }
@@ -162,27 +162,27 @@ impl std::fmt::Debug for NativeKey {
match self { match self {
Unidentified => { Unidentified => {
debug_tuple = f.debug_tuple("Unidentified"); debug_tuple = f.debug_tuple("Unidentified");
}, }
Android(code) => { Android(code) => {
debug_tuple = f.debug_tuple("Android"); debug_tuple = f.debug_tuple("Android");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
MacOS(code) => { MacOS(code) => {
debug_tuple = f.debug_tuple("MacOS"); debug_tuple = f.debug_tuple("MacOS");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
Windows(code) => { Windows(code) => {
debug_tuple = f.debug_tuple("Windows"); debug_tuple = f.debug_tuple("Windows");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
Xkb(code) => { Xkb(code) => {
debug_tuple = f.debug_tuple("Xkb"); debug_tuple = f.debug_tuple("Xkb");
debug_tuple.field(&format_args!("0x{code:04X}")); debug_tuple.field(&format_args!("0x{code:04X}"));
}, }
Web(code) => { Web(code) => {
debug_tuple = f.debug_tuple("Web"); debug_tuple = f.debug_tuple("Web");
debug_tuple.field(code); debug_tuple.field(code);
}, }
} }
debug_tuple.finish() debug_tuple.finish()
} }
@@ -442,8 +442,7 @@ pub enum KeyCode {
Tab, Tab,
/// Japanese: <kbd>変</kbd> (henkan) /// Japanese: <kbd>変</kbd> (henkan)
Convert, Convert,
/// Japanese: <kbd>カタカナ</kbd>/<kbd>ひらがな</kbd>/<kbd>ローマ字</kbd> /// Japanese: <kbd>カタカナ</kbd>/<kbd>ひらがな</kbd>/<kbd>ローマ字</kbd> (katakana/hiragana/romaji)
/// (katakana/hiragana/romaji)
KanaMode, KanaMode,
/// Korean: HangulMode <kbd>한/영</kbd> (han/yeong) /// Korean: HangulMode <kbd>한/영</kbd> (han/yeong)
/// ///
@@ -491,8 +490,7 @@ pub enum KeyCode {
NumLock, NumLock,
/// <kbd>0 Ins</kbd> on a keyboard. <kbd>0</kbd> on a phone or remote control /// <kbd>0 Ins</kbd> on a keyboard. <kbd>0</kbd> on a phone or remote control
Numpad0, Numpad0,
/// <kbd>1 End</kbd> on a keyboard. <kbd>1</kbd> or <kbd>1 QZ</kbd> on a phone or remote /// <kbd>1 End</kbd> on a keyboard. <kbd>1</kbd> or <kbd>1 QZ</kbd> on a phone or remote control
/// control
Numpad1, Numpad1,
/// <kbd>2 ↓</kbd> on a keyboard. <kbd>2 ABC</kbd> on a phone or remote control /// <kbd>2 ↓</kbd> on a keyboard. <kbd>2 ABC</kbd> on a phone or remote control
Numpad2, Numpad2,
@@ -796,14 +794,13 @@ pub enum NamedKey {
// Legacy modifier key. // Legacy modifier key.
Hyper, Hyper,
/// Used to enable "super" modifier function for interpreting concurrent or subsequent keyboard /// Used to enable "super" modifier function for interpreting concurrent or subsequent keyboard
/// input. This key value is used for the "Windows Logo" key and the Apple `Command` or `⌘` /// input. This key value is used for the "Windows Logo" key and the Apple `Command` or `⌘` key.
/// key.
/// ///
/// Note: In some contexts (e.g. the Web) this is referred to as the "Meta" key. /// Note: In some contexts (e.g. the Web) this is referred to as the "Meta" key.
Super, Super,
/// The `Enter` or `↵` key. Used to activate current selection or accept current input. This /// The `Enter` or `↵` key. Used to activate current selection or accept current input. This key
/// key value is also used for the `Return` (Macintosh numpad) key. This key value is also /// value is also used for the `Return` (Macintosh numpad) key. This key value is also used for
/// used for the Android `KEYCODE_DPAD_CENTER`. /// the Android `KEYCODE_DPAD_CENTER`.
Enter, Enter,
/// The Horizontal Tabulation `Tab` key. /// The Horizontal Tabulation `Tab` key.
Tab, Tab,
@@ -839,8 +836,8 @@ pub enum NamedKey {
CrSel, CrSel,
/// Cut the current selection. (`APPCOMMAND_CUT`) /// Cut the current selection. (`APPCOMMAND_CUT`)
Cut, Cut,
/// Used to delete the character to the right of the cursor. This key value is also used for /// Used to delete the character to the right of the cursor. This key value is also used for the
/// the key labeled `Delete` on MacOS keyboards when `Fn` is active. /// key labeled `Delete` on MacOS keyboards when `Fn` is active.
Delete, Delete,
/// The Erase to End of Field key. This key deletes all characters from the current cursor /// The Erase to End of Field key. This key deletes all characters from the current cursor
/// position to the end of the current field. /// position to the end of the current field.
@@ -924,8 +921,8 @@ pub enum NamedKey {
/// their code points. /// their code points.
CodeInput, CodeInput,
/// The Compose key, also known as "Multi_key" on the X Window System. This key acts in a /// The Compose key, also known as "Multi_key" on the X Window System. This key acts in a
/// manner similar to a dead key, triggering a mode where subsequent key presses are combined /// manner similar to a dead key, triggering a mode where subsequent key presses are combined to
/// to produce a different character. /// produce a different character.
Compose, Compose,
/// Convert the current input method sequence. /// Convert the current input method sequence.
Convert, Convert,
@@ -964,9 +961,9 @@ pub enum NamedKey {
/// The Kana Mode (Kana Lock) key. This key is used to enter hiragana mode (typically from /// The Kana Mode (Kana Lock) key. This key is used to enter hiragana mode (typically from
/// romaji mode). /// romaji mode).
KanaMode, KanaMode,
/// The Kanji (Japanese name for ideographic characters of Chinese origin) Mode key. This key /// The Kanji (Japanese name for ideographic characters of Chinese origin) Mode key. This key is
/// is typically used to switch to a hiragana keyboard for the purpose of converting input /// typically used to switch to a hiragana keyboard for the purpose of converting input into
/// into kanji. (`KEYCODE_KANA`) /// kanji. (`KEYCODE_KANA`)
KanjiMode, KanjiMode,
/// The Katakana (Japanese Kana characters) key. /// The Katakana (Japanese Kana characters) key.
Katakana, Katakana,
@@ -1591,7 +1588,7 @@ impl Key {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use winit::keyboard::{Key, NamedKey}; /// use winit::keyboard::{NamedKey, Key};
/// ///
/// assert_eq!(Key::Character("a".into()).to_text(), Some("a")); /// assert_eq!(Key::Character("a".into()).to_text(), Some("a"));
/// assert_eq!(Key::Named(NamedKey::Enter).to_text(), Some("\r")); /// assert_eq!(Key::Named(NamedKey::Enter).to_text(), Some("\r"));
@@ -1613,8 +1610,7 @@ impl Key {
/// keys can be above the letters or on the numpad. This enum allows the user to differentiate /// keys can be above the letters or on the numpad. This enum allows the user to differentiate
/// them. /// them.
/// ///
/// See the documentation for the [`location`] field on the [`KeyEvent`] struct for more /// See the documentation for the [`location`] field on the [`KeyEvent`] struct for more information.
/// information.
/// ///
/// [`location`]: ../event/struct.KeyEvent.html#structfield.location /// [`location`]: ../event/struct.KeyEvent.html#structfield.location
/// [`KeyEvent`]: crate::event::KeyEvent /// [`KeyEvent`]: crate::event::KeyEvent
@@ -1623,8 +1619,8 @@ impl Key {
pub enum KeyLocation { pub enum KeyLocation {
/// The key is in its "normal" location on the keyboard. /// The key is in its "normal" location on the keyboard.
/// ///
/// For instance, the "1" key above the "Q" key on a QWERTY keyboard will use this location. /// For instance, the "1" key above the "Q" key on a QWERTY keyboard will use this location. This
/// This invariant is also returned when the location of the key cannot be identified. /// invariant is also returned when the location of the key cannot be identified.
/// ///
/// ![Standard 1 key](https://raw.githubusercontent.com/rust-windowing/winit/master/docs/res/keyboard_standard_1_key.svg) /// ![Standard 1 key](https://raw.githubusercontent.com/rust-windowing/winit/master/docs/res/keyboard_standard_1_key.svg)
/// ///
@@ -1707,17 +1703,14 @@ impl ModifiersState {
pub fn shift_key(&self) -> bool { pub fn shift_key(&self) -> bool {
self.intersects(Self::SHIFT) self.intersects(Self::SHIFT)
} }
/// Returns `true` if the control key is pressed. /// Returns `true` if the control key is pressed.
pub fn control_key(&self) -> bool { pub fn control_key(&self) -> bool {
self.intersects(Self::CONTROL) self.intersects(Self::CONTROL)
} }
/// Returns `true` if the alt key is pressed. /// Returns `true` if the alt key is pressed.
pub fn alt_key(&self) -> bool { pub fn alt_key(&self) -> bool {
self.intersects(Self::ALT) self.intersects(Self::ALT)
} }
/// Returns `true` if the super key is pressed. /// Returns `true` if the super key is pressed.
pub fn super_key(&self) -> bool { pub fn super_key(&self) -> bool {
self.intersects(Self::SUPER) self.intersects(Self::SUPER)
@@ -1791,8 +1784,12 @@ mod modifiers_serde {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let ModifiersStateSerialize { shift_key, control_key, alt_key, super_key } = let ModifiersStateSerialize {
ModifiersStateSerialize::deserialize(deserializer)?; shift_key,
control_key,
alt_key,
super_key,
} = ModifiersStateSerialize::deserialize(deserializer)?;
let mut m = ModifiersState::empty(); let mut m = ModifiersState::empty();
m.set(ModifiersState::SHIFT, shift_key); m.set(ModifiersState::SHIFT, shift_key);
m.set(ModifiersState::CONTROL, control_key); m.set(ModifiersState::CONTROL, control_key);

View File

@@ -19,22 +19,33 @@
//! window or a key getting pressed while the window is focused. Devices can generate //! window or a key getting pressed while the window is focused. Devices can generate
//! [`DeviceEvent`]s, which contain unfiltered event data that isn't specific to a certain window. //! [`DeviceEvent`]s, which contain unfiltered event data that isn't specific to a certain window.
//! Some user activity, like mouse movement, can generate both a [`WindowEvent`] *and* a //! Some user activity, like mouse movement, can generate both a [`WindowEvent`] *and* a
//! [`DeviceEvent`]. You can also create and handle your own custom [`Event::UserEvent`]s, if //! [`DeviceEvent`]. You can also create and handle your own custom [`Event::UserEvent`]s, if desired.
//! desired.
//! //!
//! You can retrieve events by calling [`EventLoop::run_app()`]. This function will //! You can retrieve events by calling [`EventLoop::run_app()`]. This function will
//! dispatch events for every [`Window`] that was created with that particular [`EventLoop`], and //! dispatch events for every [`Window`] that was created with that particular [`EventLoop`], and
//! will run until [`exit()`] is used, at which point [`Event::LoopExiting`]. //! will run until [`exit()`] is used, at which point [`Event::LoopExiting`].
//! //!
//! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop //! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop
//! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works //! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works poorly on
//! poorly on most other platforms. However, this model can be re-implemented to an extent with //! most other platforms. However, this model can be re-implemented to an extent with
#![cfg_attr( #![cfg_attr(
any(windows_platform, macos_platform, android_platform, x11_platform, wayland_platform), any(
windows_platform,
macos_platform,
android_platform,
x11_platform,
wayland_platform
),
doc = "[`EventLoopExtPumpEvents::pump_app_events()`][platform::pump_events::EventLoopExtPumpEvents::pump_app_events()]" doc = "[`EventLoopExtPumpEvents::pump_app_events()`][platform::pump_events::EventLoopExtPumpEvents::pump_app_events()]"
)] )]
#![cfg_attr( #![cfg_attr(
not(any(windows_platform, macos_platform, android_platform, x11_platform, wayland_platform)), not(any(
windows_platform,
macos_platform,
android_platform,
x11_platform,
wayland_platform
)),
doc = "`EventLoopExtPumpEvents::pump_app_events()`" doc = "`EventLoopExtPumpEvents::pump_app_events()`"
)] )]
//! [^1]. See that method's documentation for more reasons about why //! [^1]. See that method's documentation for more reasons about why
@@ -105,16 +116,16 @@
//! //!
//! # Drawing on the window //! # Drawing on the window
//! //!
//! Winit doesn't directly provide any methods for drawing on a [`Window`]. However, it allows you //! Winit doesn't directly provide any methods for drawing on a [`Window`]. However, it allows you to
//! to retrieve the raw handle of the window and display (see the [`platform`] module and/or the //! retrieve the raw handle of the window and display (see the [`platform`] module and/or the
//! [`raw_window_handle`] and [`raw_display_handle`] methods), which in turn allows //! [`raw_window_handle`] and [`raw_display_handle`] methods), which in turn allows
//! you to create an OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics. //! you to create an OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics.
//! //!
//! Note that many platforms will display garbage data in the window's client area if the //! Note that many platforms will display garbage data in the window's client area if the
//! application doesn't render anything to the window by the time the desktop compositor is ready to //! application doesn't render anything to the window by the time the desktop compositor is ready to
//! display the window to the user. If you notice this happening, you should create the window with //! display the window to the user. If you notice this happening, you should create the window with
//! [`visible` set to `false`][crate::window::WindowAttributes::with_visible] and explicitly make //! [`visible` set to `false`][crate::window::WindowAttributes::with_visible] and explicitly make the
//! the window visible only once you're ready to render into it. //! window visible only once you're ready to render into it.
//! //!
//! # UI scaling //! # UI scaling
//! //!
@@ -140,11 +151,13 @@
//! Winit provides the following Cargo features: //! Winit provides the following Cargo features:
//! //!
//! * `x11` (enabled by default): On Unix platforms, enables the X11 backend. //! * `x11` (enabled by default): On Unix platforms, enables the X11 backend.
//! * `wayland` (enabled by default): On Unix platforms, enables the Wayland backend. //! * `wayland` (enabled by default): On Unix platforms, enables the Wayland
//! backend.
//! * `rwh_04`: Implement `raw-window-handle v0.4` traits. //! * `rwh_04`: Implement `raw-window-handle v0.4` traits.
//! * `rwh_05`: Implement `raw-window-handle v0.5` traits. //! * `rwh_05`: Implement `raw-window-handle v0.5` traits.
//! * `rwh_06`: Implement `raw-window-handle v0.6` traits. //! * `rwh_06`: Implement `raw-window-handle v0.6` traits.
//! * `serde`: Enables serialization/deserialization of certain types with [Serde](https://crates.io/crates/serde). //! * `serde`: Enables serialization/deserialization of certain types with
//! [Serde](https://crates.io/crates/serde).
//! * `mint`: Enables mint (math interoperability standard types) conversions. //! * `mint`: Enables mint (math interoperability standard types) conversions.
//! //!
//! See the [`platform`] module for documentation on platform-specific cargo //! See the [`platform`] module for documentation on platform-specific cargo
@@ -173,15 +186,14 @@
#![deny(clippy::all)] #![deny(clippy::all)]
#![deny(unsafe_op_in_unsafe_fn)] #![deny(unsafe_op_in_unsafe_fn)]
#![cfg_attr(clippy, deny(warnings))] #![cfg_attr(clippy, deny(warnings))]
// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly // Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc
// doc #![cfg_attr(
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide), doc(cfg_hide(doc, docsrs)))] docsrs,
feature(doc_auto_cfg, doc_cfg_hide),
doc(cfg_hide(doc, docsrs))
)]
#![allow(clippy::missing_safety_doc)] #![allow(clippy::missing_safety_doc)]
#[cfg(feature = "rwh_04")]
pub use rwh_04 as raw_window_handle_04;
#[cfg(feature = "rwh_05")]
pub use rwh_05 as raw_window_handle_05;
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
pub use rwh_06 as raw_window_handle; pub use rwh_06 as raw_window_handle;

View File

@@ -5,8 +5,10 @@
//! methods, which return an iterator of [`MonitorHandle`]: //! methods, which return an iterator of [`MonitorHandle`]:
//! - [`ActiveEventLoop::available_monitors`][crate::event_loop::ActiveEventLoop::available_monitors]. //! - [`ActiveEventLoop::available_monitors`][crate::event_loop::ActiveEventLoop::available_monitors].
//! - [`Window::available_monitors`][crate::window::Window::available_monitors]. //! - [`Window::available_monitors`][crate::window::Window::available_monitors].
use crate::dpi::{PhysicalPosition, PhysicalSize}; use crate::{
use crate::platform_impl; dpi::{PhysicalPosition, PhysicalSize},
platform_impl,
};
/// Deprecated! Use `VideoModeHandle` instead. /// Deprecated! Use `VideoModeHandle` instead.
#[deprecated = "Renamed to `VideoModeHandle`"] #[deprecated = "Renamed to `VideoModeHandle`"]
@@ -77,7 +79,9 @@ impl VideoModeHandle {
/// a separate set of valid video modes. /// a separate set of valid video modes.
#[inline] #[inline]
pub fn monitor(&self) -> MonitorHandle { pub fn monitor(&self) -> MonitorHandle {
MonitorHandle { inner: self.video_mode.monitor() } MonitorHandle {
inner: self.video_mode.monitor(),
}
} }
} }
@@ -162,6 +166,8 @@ impl MonitorHandle {
/// - **Web:** Always returns an empty iterator /// - **Web:** Always returns an empty iterator
#[inline] #[inline]
pub fn video_modes(&self) -> impl Iterator<Item = VideoModeHandle> { pub fn video_modes(&self) -> impl Iterator<Item = VideoModeHandle> {
self.inner.video_modes().map(|video_mode| VideoModeHandle { video_mode }) self.inner
.video_modes()
.map(|video_mode| VideoModeHandle { video_mode })
} }
} }

View File

@@ -19,7 +19,6 @@
//! //!
//! | winit | ndk-glue | //! | winit | ndk-glue |
//! | :---: | :--------------------------: | //! | :---: | :--------------------------: |
//! | 0.30 | `android-activity = "0.6"` |
//! | 0.29 | `android-activity = "0.5"` | //! | 0.29 | `android-activity = "0.5"` |
//! | 0.28 | `android-activity = "0.4"` | //! | 0.28 | `android-activity = "0.4"` |
//! | 0.27 | `ndk-glue = "0.7"` | //! | 0.27 | `ndk-glue = "0.7"` |
@@ -59,19 +58,16 @@
//! //!
//! ## Converting from `ndk-glue` to `android-activity` //! ## Converting from `ndk-glue` to `android-activity`
//! //!
//! If your application is currently based on `NativeActivity` via the `ndk-glue` crate and building //! If your application is currently based on `NativeActivity` via the `ndk-glue` crate and building with `cargo apk`, then the minimal changes would be:
//! with `cargo apk`, then the minimal changes would be:
//! 1. Remove `ndk-glue` from your `Cargo.toml` //! 1. Remove `ndk-glue` from your `Cargo.toml`
//! 2. Enable the `"android-native-activity"` feature for Winit: `winit = { version = "0.30.2", //! 2. Enable the `"android-native-activity"` feature for Winit: `winit = { version = "0.29.15", features = [ "android-native-activity" ] }`
//! features = [ "android-native-activity" ] }` //! 3. Add an `android_main` entrypoint (as above), instead of using the '`[ndk_glue::main]` proc macro from `ndk-macros` (optionally add a dependency on `android_logger` and initialize logging as above).
//! 3. Add an `android_main` entrypoint (as above), instead of using the '`[ndk_glue::main]` proc //! 4. Pass a clone of the `AndroidApp` that your application receives to Winit when building your event loop (as shown above).
//! macro from `ndk-macros` (optionally add a dependency on `android_logger` and initialize
//! logging as above).
//! 4. Pass a clone of the `AndroidApp` that your application receives to Winit when building your
//! event loop (as shown above).
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}; use crate::{
use crate::window::{Window, WindowAttributes}; event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder},
window::{Window, WindowAttributes},
};
use self::activity::{AndroidApp, ConfigurationRef, Rect}; use self::activity::{AndroidApp, ConfigurationRef, Rect};
@@ -150,7 +146,7 @@ impl<T> EventLoopBuilderExtAndroid for EventLoopBuilder<T> {
/// For compatibility applications should then import the `AndroidApp` type for /// For compatibility applications should then import the `AndroidApp` type for
/// their `android_main(app: AndroidApp)` function like: /// their `android_main(app: AndroidApp)` function like:
/// ```rust /// ```rust
/// #[cfg(target_os = "android")] /// #[cfg(target_os="android")]
/// use winit::platform::android::activity::AndroidApp; /// use winit::platform::android::activity::AndroidApp;
/// ``` /// ```
pub mod activity { pub mod activity {

View File

@@ -66,9 +66,11 @@
use std::os::raw::c_void; use std::os::raw::c_void;
use crate::event_loop::EventLoop; use crate::{
use crate::monitor::{MonitorHandle, VideoModeHandle}; event_loop::EventLoop,
use crate::window::{Window, WindowAttributes}; monitor::{MonitorHandle, VideoModeHandle},
window::{Window, WindowAttributes},
};
/// Additional methods on [`EventLoop`] that are specific to iOS. /// Additional methods on [`EventLoop`] that are specific to iOS.
pub trait EventLoopExtIOS { pub trait EventLoopExtIOS {
@@ -155,21 +157,6 @@ pub trait WindowExtIOS {
/// The default is to not recognize gestures. /// The default is to not recognize gestures.
fn recognize_pinch_gesture(&self, should_recognize: bool); fn recognize_pinch_gesture(&self, should_recognize: bool);
/// Sets whether the [`Window`] should recognize pan gestures.
///
/// The default is to not recognize gestures.
/// Installs [`UIPanGestureRecognizer`](https://developer.apple.com/documentation/uikit/uipangesturerecognizer) onto view
///
/// Set the minimum number of touches required: [`minimumNumberOfTouches`](https://developer.apple.com/documentation/uikit/uipangesturerecognizer/1621208-minimumnumberoftouches)
///
/// Set the maximum number of touches recognized: [`maximumNumberOfTouches`](https://developer.apple.com/documentation/uikit/uipangesturerecognizer/1621208-maximumnumberoftouches)
fn recognize_pan_gesture(
&self,
should_recognize: bool,
minimum_number_of_touches: u8,
maximum_number_of_touches: u8,
);
/// Sets whether the [`Window`] should recognize double tap gestures. /// Sets whether the [`Window`] should recognize double tap gestures.
/// ///
/// The default is to not recognize gestures. /// The default is to not recognize gestures.
@@ -184,17 +171,20 @@ pub trait WindowExtIOS {
impl WindowExtIOS for Window { impl WindowExtIOS for Window {
#[inline] #[inline]
fn set_scale_factor(&self, scale_factor: f64) { fn set_scale_factor(&self, scale_factor: f64) {
self.window.maybe_queue_on_main(move |w| w.set_scale_factor(scale_factor)) self.window
.maybe_queue_on_main(move |w| w.set_scale_factor(scale_factor))
} }
#[inline] #[inline]
fn set_valid_orientations(&self, valid_orientations: ValidOrientations) { fn set_valid_orientations(&self, valid_orientations: ValidOrientations) {
self.window.maybe_queue_on_main(move |w| w.set_valid_orientations(valid_orientations)) self.window
.maybe_queue_on_main(move |w| w.set_valid_orientations(valid_orientations))
} }
#[inline] #[inline]
fn set_prefers_home_indicator_hidden(&self, hidden: bool) { fn set_prefers_home_indicator_hidden(&self, hidden: bool) {
self.window.maybe_queue_on_main(move |w| w.set_prefers_home_indicator_hidden(hidden)) self.window
.maybe_queue_on_main(move |w| w.set_prefers_home_indicator_hidden(hidden))
} }
#[inline] #[inline]
@@ -206,43 +196,32 @@ impl WindowExtIOS for Window {
#[inline] #[inline]
fn set_prefers_status_bar_hidden(&self, hidden: bool) { fn set_prefers_status_bar_hidden(&self, hidden: bool) {
self.window.maybe_queue_on_main(move |w| w.set_prefers_status_bar_hidden(hidden)) self.window
.maybe_queue_on_main(move |w| w.set_prefers_status_bar_hidden(hidden))
} }
#[inline] #[inline]
fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) { fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) {
self.window.maybe_queue_on_main(move |w| w.set_preferred_status_bar_style(status_bar_style)) self.window
.maybe_queue_on_main(move |w| w.set_preferred_status_bar_style(status_bar_style))
} }
#[inline] #[inline]
fn recognize_pinch_gesture(&self, should_recognize: bool) { fn recognize_pinch_gesture(&self, should_recognize: bool) {
self.window.maybe_queue_on_main(move |w| w.recognize_pinch_gesture(should_recognize)); self.window
} .maybe_queue_on_main(move |w| w.recognize_pinch_gesture(should_recognize));
#[inline]
fn recognize_pan_gesture(
&self,
should_recognize: bool,
minimum_number_of_touches: u8,
maximum_number_of_touches: u8,
) {
self.window.maybe_queue_on_main(move |w| {
w.recognize_pan_gesture(
should_recognize,
minimum_number_of_touches,
maximum_number_of_touches,
)
});
} }
#[inline] #[inline]
fn recognize_doubletap_gesture(&self, should_recognize: bool) { fn recognize_doubletap_gesture(&self, should_recognize: bool) {
self.window.maybe_queue_on_main(move |w| w.recognize_doubletap_gesture(should_recognize)); self.window
.maybe_queue_on_main(move |w| w.recognize_doubletap_gesture(should_recognize));
} }
#[inline] #[inline]
fn recognize_rotation_gesture(&self, should_recognize: bool) { fn recognize_rotation_gesture(&self, should_recognize: bool) {
self.window.maybe_queue_on_main(move |w| w.recognize_rotation_gesture(should_recognize)); self.window
.maybe_queue_on_main(move |w| w.recognize_rotation_gesture(should_recognize));
} }
} }
@@ -322,7 +301,8 @@ impl WindowAttributesExtIOS for WindowAttributes {
#[inline] #[inline]
fn with_preferred_screen_edges_deferring_system_gestures(mut self, edges: ScreenEdge) -> Self { fn with_preferred_screen_edges_deferring_system_gestures(mut self, edges: ScreenEdge) -> Self {
self.platform_specific.preferred_screen_edges_deferring_system_gestures = edges; self.platform_specific
.preferred_screen_edges_deferring_system_gestures = edges;
self self
} }
@@ -356,13 +336,15 @@ impl MonitorHandleExtIOS for MonitorHandle {
#[inline] #[inline]
fn ui_screen(&self) -> *mut c_void { fn ui_screen(&self) -> *mut c_void {
// SAFETY: The marker is only used to get the pointer of the screen // SAFETY: The marker is only used to get the pointer of the screen
let mtm = unsafe { objc2_foundation::MainThreadMarker::new_unchecked() }; let mtm = unsafe { icrate::Foundation::MainThreadMarker::new_unchecked() };
objc2::rc::Retained::as_ptr(self.inner.ui_screen(mtm)) as *mut c_void objc2::rc::Id::as_ptr(self.inner.ui_screen(mtm)) as *mut c_void
} }
#[inline] #[inline]
fn preferred_video_mode(&self) -> VideoModeHandle { fn preferred_video_mode(&self) -> VideoModeHandle {
VideoModeHandle { video_mode: self.inner.preferred_video_mode() } VideoModeHandle {
video_mode: self.inner.preferred_video_mode(),
}
} }
} }

View File

@@ -19,9 +19,11 @@ use std::os::raw::c_void;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::event_loop::{ActiveEventLoop, EventLoopBuilder}; use crate::{
use crate::monitor::MonitorHandle; event_loop::{ActiveEventLoop, EventLoopBuilder},
use crate::window::{Window, WindowAttributes}; monitor::MonitorHandle,
window::{Window, WindowAttributes},
};
/// Additional methods on [`Window`] that are specific to MacOS. /// Additional methods on [`Window`] that are specific to MacOS.
pub trait WindowExtMacOS { pub trait WindowExtMacOS {
@@ -104,7 +106,8 @@ impl WindowExtMacOS for Window {
#[inline] #[inline]
fn set_simple_fullscreen(&self, fullscreen: bool) -> bool { fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
self.window.maybe_wait_on_main(move |w| w.set_simple_fullscreen(fullscreen)) self.window
.maybe_wait_on_main(move |w| w.set_simple_fullscreen(fullscreen))
} }
#[inline] #[inline]
@@ -114,12 +117,14 @@ impl WindowExtMacOS for Window {
#[inline] #[inline]
fn set_has_shadow(&self, has_shadow: bool) { fn set_has_shadow(&self, has_shadow: bool) {
self.window.maybe_queue_on_main(move |w| w.set_has_shadow(has_shadow)) self.window
.maybe_queue_on_main(move |w| w.set_has_shadow(has_shadow))
} }
#[inline] #[inline]
fn set_tabbing_identifier(&self, identifier: &str) { fn set_tabbing_identifier(&self, identifier: &str) {
self.window.maybe_wait_on_main(|w| w.set_tabbing_identifier(identifier)) self.window
.maybe_wait_on_main(|w| w.set_tabbing_identifier(identifier))
} }
#[inline] #[inline]
@@ -139,7 +144,8 @@ impl WindowExtMacOS for Window {
#[inline] #[inline]
fn select_tab_at_index(&self, index: usize) { fn select_tab_at_index(&self, index: usize) {
self.window.maybe_queue_on_main(move |w| w.select_tab_at_index(index)) self.window
.maybe_queue_on_main(move |w| w.select_tab_at_index(index))
} }
#[inline] #[inline]
@@ -154,12 +160,14 @@ impl WindowExtMacOS for Window {
#[inline] #[inline]
fn set_document_edited(&self, edited: bool) { fn set_document_edited(&self, edited: bool) {
self.window.maybe_queue_on_main(move |w| w.set_document_edited(edited)) self.window
.maybe_queue_on_main(move |w| w.set_document_edited(edited))
} }
#[inline] #[inline]
fn set_option_as_alt(&self, option_as_alt: OptionAsAlt) { fn set_option_as_alt(&self, option_as_alt: OptionAsAlt) {
self.window.maybe_queue_on_main(move |w| w.set_option_as_alt(option_as_alt)) self.window
.maybe_queue_on_main(move |w| w.set_option_as_alt(option_as_alt))
} }
#[inline] #[inline]
@@ -184,8 +192,7 @@ pub enum ActivationPolicy {
/// Additional methods on [`WindowAttributes`] that are specific to MacOS. /// Additional methods on [`WindowAttributes`] that are specific to MacOS.
/// ///
/// **Note:** Properties dealing with the titlebar will be overwritten by the /// **Note:** Properties dealing with the titlebar will be overwritten by the [`WindowAttributes::with_decorations`] method:
/// [`WindowAttributes::with_decorations`] method:
/// - `with_titlebar_transparent` /// - `with_titlebar_transparent`
/// - `with_title_hidden` /// - `with_title_hidden`
/// - `with_titlebar_hidden` /// - `with_titlebar_hidden`
@@ -275,7 +282,9 @@ impl WindowAttributesExtMacOS for WindowAttributes {
#[inline] #[inline]
fn with_tabbing_identifier(mut self, tabbing_identifier: &str) -> Self { fn with_tabbing_identifier(mut self, tabbing_identifier: &str) -> Self {
self.platform_specific.tabbing_identifier.replace(tabbing_identifier.to_string()); self.platform_specific
.tabbing_identifier
.replace(tabbing_identifier.to_string());
self self
} }
@@ -298,7 +307,7 @@ pub trait EventLoopBuilderExtMacOS {
/// ``` /// ```
/// use winit::event_loop::EventLoopBuilder; /// use winit::event_loop::EventLoopBuilder;
/// #[cfg(target_os = "macos")] /// #[cfg(target_os = "macos")]
/// use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS}; /// use winit::platform::macos::{EventLoopBuilderExtMacOS, ActivationPolicy};
/// ///
/// let mut builder = EventLoopBuilder::new(); /// let mut builder = EventLoopBuilder::new();
/// #[cfg(target_os = "macos")] /// #[cfg(target_os = "macos")]
@@ -374,18 +383,18 @@ impl MonitorHandleExtMacOS for MonitorHandle {
fn ns_screen(&self) -> Option<*mut c_void> { fn ns_screen(&self) -> Option<*mut c_void> {
// SAFETY: We only use the marker to get a pointer // SAFETY: We only use the marker to get a pointer
let mtm = unsafe { objc2_foundation::MainThreadMarker::new_unchecked() }; let mtm = unsafe { icrate::Foundation::MainThreadMarker::new_unchecked() };
self.inner.ns_screen(mtm).map(|s| objc2::rc::Retained::as_ptr(&s) as _) self.inner
.ns_screen(mtm)
.map(|s| objc2::rc::Id::as_ptr(&s) as _)
} }
} }
/// Additional methods on [`ActiveEventLoop`] that are specific to macOS. /// Additional methods on [`ActiveEventLoop`] that are specific to macOS.
pub trait ActiveEventLoopExtMacOS { pub trait ActiveEventLoopExtMacOS {
/// Hide the entire application. In most applications this is typically triggered with /// Hide the entire application. In most applications this is typically triggered with Command-H.
/// Command-H.
fn hide_application(&self); fn hide_application(&self);
/// Hide the other applications. In most applications this is typically triggered with /// Hide the other applications. In most applications this is typically triggered with Command+Option-H.
/// Command+Option-H.
fn hide_other_applications(&self); fn hide_other_applications(&self);
/// Set whether the system can automatically organize windows into tabs. /// Set whether the system can automatically organize windows into tabs.
/// ///

View File

@@ -51,5 +51,11 @@ pub mod pump_events;
))] ))]
pub mod modifier_supplement; pub mod modifier_supplement;
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, docsrs))] #[cfg(any(
windows_platform,
macos_platform,
x11_platform,
wayland_platform,
docsrs
))]
pub mod scancode; pub mod scancode;

View File

@@ -25,7 +25,10 @@ pub trait KeyEventExtModifierSupplement {
impl KeyEventExtModifierSupplement for KeyEvent { impl KeyEventExtModifierSupplement for KeyEvent {
#[inline] #[inline]
fn text_with_all_modifiers(&self) -> Option<&str> { fn text_with_all_modifiers(&self) -> Option<&str> {
self.platform_specific.text_with_all_modifiers.as_ref().map(|s| s.as_str()) self.platform_specific
.text_with_all_modifiers
.as_ref()
.map(|s| s.as_str())
} }
#[inline] #[inline]

View File

@@ -84,11 +84,12 @@ pub trait EventLoopExtPumpEvents {
/// ///
/// ## Platform-specific /// ## Platform-specific
/// ///
/// - **Windows**: The implementation will use `PeekMessage` when checking for window messages /// - **Windows**: The implementation will use `PeekMessage` when checking for
/// to avoid blocking your external event loop. /// window messages to avoid blocking your external event loop.
/// ///
/// - **MacOS**: The implementation works in terms of stopping the global application whenever /// - **MacOS**: The implementation works in terms of stopping the global application
/// the application `RunLoop` indicates that it is preparing to block and wait for new events. /// whenever the application `RunLoop` indicates that it is preparing to block
/// and wait for new events.
/// ///
/// This is very different to the polling APIs that are available on other /// This is very different to the polling APIs that are available on other
/// platforms (the lower level polling primitives on MacOS are private /// platforms (the lower level polling primitives on MacOS are private

View File

@@ -21,8 +21,8 @@ pub trait EventLoopExtRunOnDemand {
/// Run the application with the event loop on the calling thread. /// Run the application with the event loop on the calling thread.
/// ///
/// Unlike [`EventLoop::run_app`], this function accepts non-`'static` (i.e. non-`move`) /// Unlike [`EventLoop::run_app`], this function accepts non-`'static` (i.e. non-`move`) closures
/// closures and it is possible to return control back to the caller without /// and it is possible to return control back to the caller without
/// consuming the `EventLoop` (by using [`exit()`]) and /// consuming the `EventLoop` (by using [`exit()`]) and
/// so the event loop can be re-run after it has exit. /// so the event loop can be re-run after it has exit.
/// ///
@@ -55,13 +55,17 @@ pub trait EventLoopExtRunOnDemand {
/// - Android /// - Android
/// ///
/// # Unsupported Platforms /// # Unsupported Platforms
/// - **Web:** This API is fundamentally incompatible with the event-based way in which Web /// - **Web:** This API is fundamentally incompatible with the event-based way in which
/// browsers work because it's not possible to have a long-running external loop that would /// Web browsers work because it's not possible to have a long-running external
/// block the browser and there is nothing that can be polled to ask for new events. Events /// loop that would block the browser and there is nothing that can be
/// are delivered via callbacks based on an event loop that is internal to the browser itself. /// polled to ask for new events. Events are delivered via callbacks based
/// on an event loop that is internal to the browser itself.
/// - **iOS:** It's not possible to stop and start an `UIApplication` repeatedly on iOS. /// - **iOS:** It's not possible to stop and start an `UIApplication` repeatedly on iOS.
#[cfg_attr(not(web_platform), doc = "[^1]: `spawn()` is only available on `wasm` platforms.")] ///
#[rustfmt::skip] #[cfg_attr(
not(web_platform),
doc = "[^1]: `spawn()` is only available on `wasm` platforms."
)]
/// ///
/// [`exit()`]: ActiveEventLoop::exit() /// [`exit()`]: ActiveEventLoop::exit()
/// [`set_control_flow()`]: ActiveEventLoop::set_control_flow() /// [`set_control_flow()`]: ActiveEventLoop::set_control_flow()

View File

@@ -2,8 +2,8 @@ use crate::keyboard::{KeyCode, PhysicalKey};
// TODO: Describe what this value contains for each platform // TODO: Describe what this value contains for each platform
/// Additional methods for the [`PhysicalKey`] type that allow the user to access the /// Additional methods for the [`PhysicalKey`] type that allow the user to access the platform-specific
/// platform-specific scancode. /// scancode.
/// ///
/// [`PhysicalKey`]: crate::keyboard::PhysicalKey /// [`PhysicalKey`]: crate::keyboard::PhysicalKey
pub trait PhysicalKeyExtScancode { pub trait PhysicalKeyExtScancode {
@@ -23,7 +23,7 @@ pub trait PhysicalKeyExtScancode {
/// ///
/// ## Platform-specific /// ## Platform-specific
/// - **Wayland/X11**: A 32-bit linux scancode. When building from X11/Wayland keycode subtract /// - **Wayland/X11**: A 32-bit linux scancode. When building from X11/Wayland keycode subtract
/// `8` to get the value you wanted. /// `8` to get the value you wanted.
fn from_scancode(scancode: u32) -> PhysicalKey; fn from_scancode(scancode: u32) -> PhysicalKey;
} }

View File

@@ -13,9 +13,11 @@
//! * `wayland-csd-adwaita` (default). //! * `wayland-csd-adwaita` (default).
//! * `wayland-csd-adwaita-crossfont`. //! * `wayland-csd-adwaita-crossfont`.
//! * `wayland-csd-adwaita-notitle`. //! * `wayland-csd-adwaita-notitle`.
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}; use crate::{
use crate::monitor::MonitorHandle; event_loop::{ActiveEventLoop, EventLoopBuilder},
use crate::window::{Window, WindowAttributes}; monitor::MonitorHandle,
window::{Window, WindowAttributes},
};
pub use crate::window::Theme; pub use crate::window::Theme;
@@ -32,19 +34,6 @@ impl ActiveEventLoopExtWayland for ActiveEventLoop {
} }
} }
/// Additional methods on [`EventLoop`] that are specific to Wayland.
pub trait EventLoopExtWayland {
/// True if the [`EventLoop`] uses Wayland.
fn is_wayland(&self) -> bool;
}
impl<T: 'static> EventLoopExtWayland for EventLoop<T> {
#[inline]
fn is_wayland(&self) -> bool {
self.event_loop.is_wayland()
}
}
/// Additional methods on [`EventLoopBuilder`] that are specific to Wayland. /// Additional methods on [`EventLoopBuilder`] that are specific to Wayland.
pub trait EventLoopBuilderExtWayland { pub trait EventLoopBuilderExtWayland {
/// Force using Wayland. /// Force using Wayland.
@@ -91,8 +80,10 @@ pub trait WindowAttributesExtWayland {
impl WindowAttributesExtWayland for WindowAttributes { impl WindowAttributesExtWayland for WindowAttributes {
#[inline] #[inline]
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self { fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
self.platform_specific.name = self.platform_specific.name = Some(crate::platform_impl::ApplicationName::new(
Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into())); general.into(),
instance.into(),
));
self self
} }
} }

View File

@@ -30,8 +30,8 @@
//! The following APIs can't take them into account and will therefore provide inaccurate results: //! The following APIs can't take them into account and will therefore provide inaccurate results:
//! - [`WindowEvent::Resized`] and [`Window::(set_)inner_size()`] //! - [`WindowEvent::Resized`] and [`Window::(set_)inner_size()`]
//! - [`WindowEvent::Occluded`] //! - [`WindowEvent::Occluded`]
//! - [`WindowEvent::CursorMoved`], [`WindowEvent::CursorEntered`], [`WindowEvent::CursorLeft`], and //! - [`WindowEvent::CursorMoved`], [`WindowEvent::CursorEntered`], [`WindowEvent::CursorLeft`],
//! [`WindowEvent::Touch`]. //! and [`WindowEvent::Touch`].
//! - [`Window::set_outer_position()`] //! - [`Window::set_outer_position()`]
//! //!
//! [`WindowEvent::Resized`]: crate::event::WindowEvent::Resized //! [`WindowEvent::Resized`]: crate::event::WindowEvent::Resized
@@ -109,7 +109,11 @@ pub trait WindowAttributesExtWebSys {
/// In any case, the canvas won't be automatically inserted into the web page. /// In any case, the canvas won't be automatically inserted into the web page.
/// ///
/// [`None`] by default. /// [`None`] by default.
#[cfg_attr(not(web_platform), doc = "", doc = "[`HtmlCanvasElement`]: #only-available-on-wasm")] #[cfg_attr(
not(web_platform),
doc = "",
doc = "[`HtmlCanvasElement`]: #only-available-on-wasm"
)]
fn with_canvas(self, canvas: Option<HtmlCanvasElement>) -> Self; fn with_canvas(self, canvas: Option<HtmlCanvasElement>) -> Self;
/// Sets whether `event.preventDefault()` should be called on events on the /// Sets whether `event.preventDefault()` should be called on events on the
@@ -162,7 +166,10 @@ pub trait EventLoopExtWebSys {
/// Initializes the winit event loop. /// Initializes the winit event loop.
/// ///
/// Unlike /// Unlike
#[cfg_attr(all(web_platform, target_feature = "exception-handling"), doc = "`run_app()`")] #[cfg_attr(
all(web_platform, target_feature = "exception-handling"),
doc = "`run_app()`"
)]
#[cfg_attr( #[cfg_attr(
not(all(web_platform, target_feature = "exception-handling")), not(all(web_platform, target_feature = "exception-handling")),
doc = "[`run_app()`]" doc = "[`run_app()`]"
@@ -174,7 +181,6 @@ pub trait EventLoopExtWebSys {
/// by calling this function again. This can be useful if you want to recreate the event loop /// by calling this function again. This can be useful if you want to recreate the event loop
/// while the WebAssembly module is still loaded. For example, this can be used to recreate the /// while the WebAssembly module is still loaded. For example, this can be used to recreate the
/// event loop when switching between tabs on a single page application. /// event loop when switching between tabs on a single page application.
#[rustfmt::skip]
/// ///
#[cfg_attr( #[cfg_attr(
not(all(web_platform, target_feature = "exception-handling")), not(all(web_platform, target_feature = "exception-handling")),
@@ -297,7 +303,13 @@ impl CustomCursorExtWebSys for CustomCursor {
} }
fn from_url(url: String, hotspot_x: u16, hotspot_y: u16) -> CustomCursorSource { fn from_url(url: String, hotspot_x: u16, hotspot_y: u16) -> CustomCursorSource {
CustomCursorSource { inner: PlatformCustomCursorSource::Url { url, hotspot_x, hotspot_y } } CustomCursorSource {
inner: PlatformCustomCursorSource::Url {
url,
hotspot_x,
hotspot_y,
},
}
} }
fn from_animation( fn from_animation(
@@ -348,7 +360,9 @@ impl Future for CustomCursorFuture {
type Output = Result<CustomCursor, CustomCursorError>; type Output = Result<CustomCursor, CustomCursorError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(cx).map_ok(|cursor| CustomCursor { inner: cursor }) Pin::new(&mut self.0)
.poll(cx)
.map_ok(|cursor| CustomCursor { inner: cursor })
} }
} }
@@ -364,9 +378,10 @@ impl Display for CustomCursorError {
match self { match self {
Self::Blob => write!(f, "failed to create `Blob`"), Self::Blob => write!(f, "failed to create `Blob`"),
Self::Decode(error) => write!(f, "failed to decode image: {error}"), Self::Decode(error) => write!(f, "failed to decode image: {error}"),
Self::Animation => { Self::Animation => write!(
write!(f, "found `CustomCursor` that is an animation when building an animation") f,
}, "found `CustomCursor` that is an animation when building an animation"
),
} }
} }
} }

View File

@@ -2,15 +2,15 @@
//! //!
//! The supported OS version is Windows 7 or higher, though Windows 10 is //! The supported OS version is Windows 7 or higher, though Windows 10 is
//! tested regularly. //! tested regularly.
use std::borrow::Borrow; use std::{ffi::c_void, path::Path};
use std::ffi::c_void;
use std::path::Path;
use crate::dpi::PhysicalSize; use crate::{
use crate::event::DeviceId; dpi::PhysicalSize,
use crate::event_loop::EventLoopBuilder; event::DeviceId,
use crate::monitor::MonitorHandle; event_loop::EventLoopBuilder,
use crate::window::{BadIcon, Icon, Window, WindowAttributes}; monitor::MonitorHandle,
window::{BadIcon, Icon, Window, WindowAttributes},
};
/// Window Handle type used by Win32 API /// Window Handle type used by Win32 API
pub type HWND = isize; pub type HWND = isize;
@@ -57,11 +57,11 @@ pub enum BackdropType {
pub struct Color(u32); pub struct Color(u32);
impl Color { impl Color {
// Special constant only valid for the window border and therefore modeled using Option<Color>
// for user facing code
const NONE: Color = Color(0xfffffffe);
/// Use the system's default color /// Use the system's default color
pub const SYSTEM_DEFAULT: Color = Color(0xffffffff); pub const SYSTEM_DEFAULT: Color = Color(0xFFFFFFFF);
//Special constant only valid for the window border and therefore modeled using Option<Color> for user facing code
const NONE: Color = Color(0xFFFFFFFE);
/// Create a new color from the given RGB values /// Create a new color from the given RGB values
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
@@ -105,60 +105,6 @@ pub enum CornerPreference {
RoundSmall = 3, RoundSmall = 3,
} }
/// A wrapper around a [`Window`] that ignores thread-specific window handle limitations.
///
/// See [`WindowBorrowExtWindows::any_thread`] for more information.
#[derive(Debug)]
pub struct AnyThread<W>(W);
impl<W: Borrow<Window>> AnyThread<W> {
/// Get a reference to the inner window.
#[inline]
pub fn get_ref(&self) -> &Window {
self.0.borrow()
}
/// Get a reference to the inner object.
#[inline]
pub fn inner(&self) -> &W {
&self.0
}
/// Unwrap and get the inner window.
#[inline]
pub fn into_inner(self) -> W {
self.0
}
}
impl<W: Borrow<Window>> AsRef<Window> for AnyThread<W> {
fn as_ref(&self) -> &Window {
self.get_ref()
}
}
impl<W: Borrow<Window>> Borrow<Window> for AnyThread<W> {
fn borrow(&self) -> &Window {
self.get_ref()
}
}
impl<W: Borrow<Window>> std::ops::Deref for AnyThread<W> {
type Target = Window;
fn deref(&self) -> &Self::Target {
self.get_ref()
}
}
#[cfg(feature = "rwh_06")]
impl<W: Borrow<Window>> rwh_06::HasWindowHandle for AnyThread<W> {
fn window_handle(&self) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError> {
// SAFETY: The top level user has asserted this is only used safely.
unsafe { self.get_ref().window_handle_any_thread() }
}
}
/// Additional methods on `EventLoop` that are specific to Windows. /// Additional methods on `EventLoop` that are specific to Windows.
pub trait EventLoopBuilderExtWindows { pub trait EventLoopBuilderExtWindows {
/// Whether to allow the event loop to be created off of the main thread. /// Whether to allow the event loop to be created off of the main thread.
@@ -256,8 +202,8 @@ pub trait WindowExtWindows {
/// ///
/// A window must be enabled before it can be activated. /// A window must be enabled before it can be activated.
/// If an application has create a modal dialog box by disabling its owner window /// If an application has create a modal dialog box by disabling its owner window
/// (as described in [`WindowAttributesExtWindows::with_owner_window`]), the application must /// (as described in [`WindowAttributesExtWindows::with_owner_window`]), the application must enable
/// enable the owner window before destroying the dialog box. /// the owner window before destroying the dialog box.
/// Otherwise, another window will receive the keyboard focus and be activated. /// Otherwise, another window will receive the keyboard focus and be activated.
/// ///
/// If a child window is disabled, it is ignored when the system tries to determine which /// If a child window is disabled, it is ignored when the system tries to determine which
@@ -302,60 +248,6 @@ pub trait WindowExtWindows {
/// ///
/// Supported starting with Windows 11 Build 22000. /// Supported starting with Windows 11 Build 22000.
fn set_corner_preference(&self, preference: CornerPreference); fn set_corner_preference(&self, preference: CornerPreference);
/// Get the raw window handle for this [`Window`] without checking for thread affinity.
///
/// Window handles in Win32 have a property called "thread affinity" that ties them to their
/// origin thread. Some operations can only happen on the window's origin thread, while others
/// can be called from any thread. For example, [`SetWindowSubclass`] is not thread safe while
/// [`GetDC`] is thread safe.
///
/// In Rust terms, the window handle is `Send` sometimes but `!Send` other times.
///
/// Therefore, in order to avoid confusing threading errors, [`Window`] only returns the
/// window handle when the [`window_handle`] function is called from the thread that created
/// the window. In other cases, it returns an [`Unavailable`] error.
///
/// However in some cases you may already know that you are using the window handle for
/// operations that are guaranteed to be thread-safe. In which case this function aims
/// to provide an escape hatch so these functions are still accessible from other threads.
///
/// # Safety
///
/// It is the responsibility of the user to only pass the window handle into thread-safe
/// Win32 APIs.
///
/// [`SetWindowSubclass`]: https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-setwindowsubclass
/// [`GetDC`]: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdc
/// [`Window`]: crate::window::Window
/// [`window_handle`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/trait.HasWindowHandle.html#tymethod.window_handle
/// [`Unavailable`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/enum.HandleError.html#variant.Unavailable
///
/// ## Example
///
/// ```no_run
/// # use winit::window::Window;
/// # fn scope(window: Window) {
/// use std::thread;
/// use winit::platform::windows::WindowExtWindows;
/// use winit::raw_window_handle::HasWindowHandle;
///
/// // We can get the window handle on the current thread.
/// let handle = window.window_handle().unwrap();
///
/// // However, on another thread, we can't!
/// thread::spawn(move || {
/// assert!(window.window_handle().is_err());
///
/// // We can use this function as an escape hatch.
/// let handle = unsafe { window.window_handle_any_thread().unwrap() };
/// });
/// # }
/// ```
#[cfg(feature = "rwh_06")]
unsafe fn window_handle_any_thread(
&self,
) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError>;
} }
impl WindowExtWindows for Window { impl WindowExtWindows for Window {
@@ -391,10 +283,10 @@ impl WindowExtWindows for Window {
#[inline] #[inline]
fn set_title_background_color(&self, color: Option<Color>) { fn set_title_background_color(&self, color: Option<Color>) {
// The windows docs don't mention NONE as a valid options but it works in practice and is // The windows docs don't mention NONE as a valid options but it works in practice and is useful
// useful to circumvent the Windows option "Show accent color on title bars and // to circumvent the Windows option "Show accent color on title bars and window borders"
// window borders" self.window
self.window.set_title_background_color(color.unwrap_or(Color::NONE)) .set_title_background_color(color.unwrap_or(Color::NONE))
} }
#[inline] #[inline]
@@ -406,57 +298,15 @@ impl WindowExtWindows for Window {
fn set_corner_preference(&self, preference: CornerPreference) { fn set_corner_preference(&self, preference: CornerPreference) {
self.window.set_corner_preference(preference) self.window.set_corner_preference(preference)
} }
#[cfg(feature = "rwh_06")]
unsafe fn window_handle_any_thread(
&self,
) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError> {
unsafe {
let handle = self.window.rwh_06_no_thread_check()?;
// SAFETY: The handle is valid in this context.
Ok(rwh_06::WindowHandle::borrow_raw(handle))
}
}
} }
/// Additional methods for anything that dereference to [`Window`].
///
/// [`Window`]: crate::window::Window
pub trait WindowBorrowExtWindows: Borrow<Window> + Sized {
/// Create an object that allows accessing the inner window handle in a thread-unsafe way.
///
/// It is possible to call [`window_handle_any_thread`] to get around Windows's thread
/// affinity limitations. However, it may be desired to pass the [`Window`] into something
/// that requires the [`HasWindowHandle`] trait, while ignoring thread affinity limitations.
///
/// This function wraps anything that implements `Borrow<Window>` into a structure that
/// uses the inner window handle as a mean of implementing [`HasWindowHandle`]. It wraps
/// `Window`, `&Window`, `Arc<Window>`, and other reference types.
///
/// # Safety
///
/// It is the responsibility of the user to only pass the window handle into thread-safe
/// Win32 APIs.
///
/// [`window_handle_any_thread`]: WindowExtWindows::window_handle_any_thread
/// [`Window`]: crate::window::Window
/// [`HasWindowHandle`]: rwh_06::HasWindowHandle
unsafe fn any_thread(self) -> AnyThread<Self> {
AnyThread(self)
}
}
impl<W: Borrow<Window> + Sized> WindowBorrowExtWindows for W {}
/// Additional methods on `WindowAttributes` that are specific to Windows. /// Additional methods on `WindowAttributes` that are specific to Windows.
#[allow(rustdoc::broken_intra_doc_links)] #[allow(rustdoc::broken_intra_doc_links)]
pub trait WindowAttributesExtWindows { pub trait WindowAttributesExtWindows {
/// Set an owner to the window to be created. Can be used to create a dialog box, for example. /// Set an owner to the window to be created. Can be used to create a dialog box, for example.
/// This only works when [`WindowAttributes::with_parent_window`] isn't called or set to `None`. /// This only works when [`WindowAttributes::with_parent_window`] isn't called or set to `None`.
/// Can be used in combination with /// Can be used in combination with [`WindowExtWindows::set_enable(false)`][WindowExtWindows::set_enable]
/// [`WindowExtWindows::set_enable(false)`][WindowExtWindows::set_enable] on the owner /// on the owner window to create a modal dialog box.
/// window to create a modal dialog box.
/// ///
/// From MSDN: /// From MSDN:
/// - An owned window is always above its owner in the z-order. /// - An owned window is always above its owner in the z-order.
@@ -472,14 +322,17 @@ pub trait WindowAttributesExtWindows {
/// ///
/// The menu must have been manually created beforehand with [`CreateMenu`] or similar. /// The menu must have been manually created beforehand with [`CreateMenu`] or similar.
/// ///
/// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how /// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how the menus look.
/// the menus look. If you use this, it is recommended that you combine it with /// If you use this, it is recommended that you combine it with `with_theme(Some(Theme::Light))` to avoid a jarring effect.
/// `with_theme(Some(Theme::Light))` to avoid a jarring effect. ///
#[cfg_attr( #[cfg_attr(
windows_platform, platform_windows,
doc = "[`CreateMenu`]: windows_sys::Win32::UI::WindowsAndMessaging::CreateMenu" doc = "[`CreateMenu`]: windows_sys::Win32::UI::WindowsAndMessaging::CreateMenu"
)] )]
#[cfg_attr(not(windows_platform), doc = "[`CreateMenu`]: #only-available-on-windows")] #[cfg_attr(
not(platform_windows),
doc = "[`CreateMenu`]: #only-available-on-windows"
)]
fn with_menu(self, menu: HMENU) -> Self; fn with_menu(self, menu: HMENU) -> Self;
/// This sets `ICON_BIG`. A good ceiling here is 256x256. /// This sets `ICON_BIG`. A good ceiling here is 256x256.
@@ -488,12 +341,12 @@ pub trait WindowAttributesExtWindows {
/// This sets `WS_EX_NOREDIRECTIONBITMAP`. /// This sets `WS_EX_NOREDIRECTIONBITMAP`.
fn with_no_redirection_bitmap(self, flag: bool) -> Self; fn with_no_redirection_bitmap(self, flag: bool) -> Self;
/// Enables or disables drag and drop support (enabled by default). Will interfere with other /// Enables or disables drag and drop support (enabled by default). Will interfere with other crates
/// crates that use multi-threaded COM API (`CoInitializeEx` with `COINIT_MULTITHREADED` /// that use multi-threaded COM API (`CoInitializeEx` with `COINIT_MULTITHREADED` instead of
/// instead of `COINIT_APARTMENTTHREADED`) on the same thread. Note that winit may still /// `COINIT_APARTMENTTHREADED`) on the same thread. Note that winit may still attempt to initialize
/// attempt to initialize COM API regardless of this option. Currently only fullscreen mode /// COM API regardless of this option. Currently only fullscreen mode does that, but there may be more in the future.
/// does that, but there may be more in the future. If you need COM API with /// If you need COM API with `COINIT_MULTITHREADED` you must initialize it before calling any winit functions.
/// `COINIT_MULTITHREADED` you must initialize it before calling any winit functions. See <https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize#remarks> for more information. /// See <https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize#remarks> for more information.
fn with_drag_and_drop(self, flag: bool) -> Self; fn with_drag_and_drop(self, flag: bool) -> Self;
/// Whether show or hide the window icon in the taskbar. /// Whether show or hide the window icon in the taskbar.

View File

@@ -2,9 +2,11 @@
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}; use crate::{
use crate::monitor::MonitorHandle; event_loop::{ActiveEventLoop, EventLoopBuilder},
use crate::window::{Window, WindowAttributes}; monitor::MonitorHandle,
window::{Window, WindowAttributes},
};
use crate::dpi::Size; use crate::dpi::Size;
@@ -13,12 +15,11 @@ use crate::dpi::Size;
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum WindowType { pub enum WindowType {
/// A desktop feature. This can include a single window containing desktop icons with the same /// A desktop feature. This can include a single window containing desktop icons with the same dimensions as the
/// dimensions as the screen, allowing the desktop environment to have full control of the /// screen, allowing the desktop environment to have full control of the desktop, without the need for proxying
/// desktop, without the need for proxying root window clicks. /// root window clicks.
Desktop, Desktop,
/// A dock or panel feature. Typically a Window Manager would keep such windows on top of all /// A dock or panel feature. Typically a Window Manager would keep such windows on top of all other windows.
/// other windows.
Dock, Dock,
/// Toolbar windows. "Torn off" from the main application. /// Toolbar windows. "Torn off" from the main application.
Toolbar, Toolbar,
@@ -36,8 +37,8 @@ pub enum WindowType {
/// A popup menu that usually appears when the user right clicks on an object. /// A popup menu that usually appears when the user right clicks on an object.
/// This property is typically used on override-redirect windows. /// This property is typically used on override-redirect windows.
PopupMenu, PopupMenu,
/// A tooltip window. Usually used to show additional information when hovering over an object /// A tooltip window. Usually used to show additional information when hovering over an object with the cursor.
/// with the cursor. This property is typically used on override-redirect windows. /// This property is typically used on override-redirect windows.
Tooltip, Tooltip,
/// The window is a notification. /// The window is a notification.
/// This property is typically used on override-redirect windows. /// This property is typically used on override-redirect windows.
@@ -45,7 +46,7 @@ pub enum WindowType {
/// This should be used on the windows that are popped up by combo boxes. /// This should be used on the windows that are popped up by combo boxes.
/// This property is typically used on override-redirect windows. /// This property is typically used on override-redirect windows.
Combo, Combo,
/// This indicates the window is being dragged. /// This indicates the the window is being dragged.
/// This property is typically used on override-redirect windows. /// This property is typically used on override-redirect windows.
Dnd, Dnd,
/// This is a normal, top-level window. /// This is a normal, top-level window.
@@ -82,7 +83,10 @@ pub type XWindow = u32;
pub fn register_xlib_error_hook(hook: XlibErrorHook) { pub fn register_xlib_error_hook(hook: XlibErrorHook) {
// Append new hook. // Append new hook.
unsafe { unsafe {
crate::platform_impl::XLIB_ERROR_HOOKS.lock().unwrap().push(hook); crate::platform_impl::XLIB_ERROR_HOOKS
.lock()
.unwrap()
.push(hook);
} }
} }
@@ -99,19 +103,6 @@ impl ActiveEventLoopExtX11 for ActiveEventLoop {
} }
} }
/// Additional methods on [`EventLoop`] that are specific to X11.
pub trait EventLoopExtX11 {
/// True if the [`EventLoop`] uses X11.
fn is_x11(&self) -> bool;
}
impl<T: 'static> EventLoopExtX11 for EventLoop<T> {
#[inline]
fn is_x11(&self) -> bool {
!self.event_loop.is_wayland()
}
}
/// Additional methods on [`EventLoopBuilder`] that are specific to X11. /// Additional methods on [`EventLoopBuilder`] that are specific to X11.
pub trait EventLoopBuilderExtX11 { pub trait EventLoopBuilderExtX11 {
/// Force using X11. /// Force using X11.
@@ -153,8 +144,7 @@ pub trait WindowAttributesExtX11 {
/// Build window with the given `general` and `instance` names. /// Build window with the given `general` and `instance` names.
/// ///
/// The `general` sets general class of `WM_CLASS(STRING)`, while `instance` set the /// The `general` sets general class of `WM_CLASS(STRING)`, while `instance` set the
/// instance part of it. The resulted property looks like `WM_CLASS(STRING) = "instance", /// instance part of it. The resulted property looks like `WM_CLASS(STRING) = "instance", "general"`.
/// "general"`.
/// ///
/// For details about application ID conventions, see the /// For details about application ID conventions, see the
/// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id) /// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
@@ -212,8 +202,10 @@ impl WindowAttributesExtX11 for WindowAttributes {
#[inline] #[inline]
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self { fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
self.platform_specific.name = self.platform_specific.name = Some(crate::platform_impl::ApplicationName::new(
Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into())); general.into(),
instance.into(),
));
self self
} }

View File

@@ -1,5 +1,7 @@
use android_activity::input::{KeyAction, KeyEvent, KeyMapChar, Keycode}; use android_activity::{
use android_activity::AndroidApp; input::{KeyAction, KeyEvent, KeyMapChar, Keycode},
AndroidApp,
};
use crate::keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKey, NativeKeyCode, PhysicalKey}; use crate::keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKey, NativeKeyCode, PhysicalKey};
@@ -103,7 +105,7 @@ pub fn to_physical_key(keycode: Keycode) -> PhysicalKey {
Keycode::VolumeUp => KeyCode::AudioVolumeUp, Keycode::VolumeUp => KeyCode::AudioVolumeUp,
Keycode::VolumeDown => KeyCode::AudioVolumeDown, Keycode::VolumeDown => KeyCode::AudioVolumeDown,
Keycode::VolumeMute => KeyCode::AudioVolumeMute, Keycode::VolumeMute => KeyCode::AudioVolumeMute,
// Keycode::Mute => None, // Microphone mute //Keycode::Mute => None, // Microphone mute
Keycode::MediaPlayPause => KeyCode::MediaPlayPause, Keycode::MediaPlayPause => KeyCode::MediaPlayPause,
Keycode::MediaStop => KeyCode::MediaStop, Keycode::MediaStop => KeyCode::MediaStop,
Keycode::MediaNext => KeyCode::MediaTrackNext, Keycode::MediaNext => KeyCode::MediaTrackNext,
@@ -174,7 +176,7 @@ pub fn character_map_and_combine_key(
Err(err) => { Err(err) => {
tracing::warn!("Failed to look up `KeyCharacterMap` for device {device_id}: {err:?}"); tracing::warn!("Failed to look up `KeyCharacterMap` for device {device_id}: {err:?}");
return None; return None;
}, }
}; };
match key_map.get(key_event.key_code(), key_event.meta_state()) { match key_map.get(key_event.key_code(), key_event.meta_state()) {
@@ -186,12 +188,9 @@ pub fn character_map_and_combine_key(
Ok(Some(key)) => Some(key), Ok(Some(key)) => Some(key),
Ok(None) => None, Ok(None) => None,
Err(err) => { Err(err) => {
tracing::warn!( tracing::warn!("KeyEvent: Failed to combine 'dead key' accent '{accent}' with '{unicode}': {err:?}");
"KeyEvent: Failed to combine 'dead key' accent '{accent}' with \
'{unicode}': {err:?}"
);
None None
}, }
} }
} else { } else {
Some(unicode) Some(unicode)
@@ -201,23 +200,23 @@ pub fn character_map_and_combine_key(
} else { } else {
Some(KeyMapChar::Unicode(unicode)) Some(KeyMapChar::Unicode(unicode))
} }
}, }
Ok(KeyMapChar::CombiningAccent(accent)) => { Ok(KeyMapChar::CombiningAccent(accent)) => {
if key_event.action() == KeyAction::Down { if key_event.action() == KeyAction::Down {
*combining_accent = Some(accent); *combining_accent = Some(accent);
} }
Some(KeyMapChar::CombiningAccent(accent)) Some(KeyMapChar::CombiningAccent(accent))
}, }
Ok(KeyMapChar::None) => { Ok(KeyMapChar::None) => {
// Leave any combining_accent state in tact (seems to match how other // Leave any combining_accent state in tact (seems to match how other
// Android apps work) // Android apps work)
None None
}, }
Err(err) => { Err(err) => {
tracing::warn!("KeyEvent: Failed to get key map character: {err:?}"); tracing::warn!("KeyEvent: Failed to get key map character: {err:?}");
*combining_accent = None; *combining_accent = None;
None None
}, }
} }
} }

View File

@@ -1,10 +1,16 @@
use std::cell::Cell; #![cfg(android_platform)]
use std::collections::VecDeque;
use std::hash::Hash; use std::{
use std::marker::PhantomData; cell::Cell,
use std::sync::atomic::{AtomicBool, Ordering}; collections::VecDeque,
use std::sync::{mpsc, Arc, Mutex}; hash::Hash,
use std::time::{Duration, Instant}; marker::PhantomData,
sync::{
atomic::{AtomicBool, Ordering},
mpsc, Arc, Mutex,
},
time::{Duration, Instant},
};
use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction}; use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction};
use android_activity::{ use android_activity::{
@@ -12,24 +18,24 @@ use android_activity::{
}; };
use tracing::{debug, trace, warn}; use tracing::{debug, trace, warn};
use crate::cursor::Cursor; use crate::{
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size}; cursor::Cursor,
use crate::error; dpi::{PhysicalPosition, PhysicalSize, Position, Size},
use crate::error::EventLoopError; error,
use crate::event::{self, Force, InnerSizeWriter, StartCause}; event::{self, Force, InnerSizeWriter, StartCause},
use crate::event_loop::{self, ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents}; event_loop::{self, ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents},
use crate::platform::pump_events::PumpStatus; platform::pump_events::PumpStatus,
use crate::platform_impl::Fullscreen; window::{
use crate::window::{ self, CursorGrabMode, CustomCursor, CustomCursorSource, ImePurpose, ResizeDirection, Theme,
self, CursorGrabMode, CustomCursor, CustomCursorSource, ImePurpose, ResizeDirection, Theme, WindowButtons, WindowLevel,
WindowButtons, WindowLevel, },
}; };
use crate::{error::EventLoopError, platform_impl::Fullscreen};
mod keycodes; mod keycodes;
pub(crate) use crate::cursor::{ pub(crate) use crate::cursor::NoCustomCursor as PlatformCustomCursor;
NoCustomCursor as PlatformCustomCursor, NoCustomCursor as PlatformCustomCursorSource, pub(crate) use crate::cursor::NoCustomCursor as PlatformCustomCursorSource;
};
pub(crate) use crate::icon::NoIcon as PlatformIcon; pub(crate) use crate::icon::NoIcon as PlatformIcon;
static HAS_FOCUS: AtomicBool = AtomicBool::new(true); static HAS_FOCUS: AtomicBool = AtomicBool::new(true);
@@ -38,7 +44,9 @@ static HAS_FOCUS: AtomicBool = AtomicBool::new(true);
/// equates to an infinite timeout, not a zero timeout (so can't just use /// equates to an infinite timeout, not a zero timeout (so can't just use
/// `Option::min`) /// `Option::min`)
fn min_timeout(a: Option<Duration>, b: Option<Duration>) -> Option<Duration> { fn min_timeout(a: Option<Duration>, b: Option<Duration>) -> Option<Duration> {
a.map_or(b, |a_timeout| b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))) a.map_or(b, |a_timeout| {
b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))
})
} }
struct PeekableReceiver<T> { struct PeekableReceiver<T> {
@@ -50,7 +58,6 @@ impl<T> PeekableReceiver<T> {
pub fn from_recv(recv: mpsc::Receiver<T>) -> Self { pub fn from_recv(recv: mpsc::Receiver<T>) -> Self {
Self { recv, first: None } Self { recv, first: None }
} }
pub fn has_incoming(&mut self) -> bool { pub fn has_incoming(&mut self) -> bool {
if self.first.is_some() { if self.first.is_some() {
return true; return true;
@@ -59,15 +66,14 @@ impl<T> PeekableReceiver<T> {
Ok(v) => { Ok(v) => {
self.first = Some(v); self.first = Some(v);
true true
}, }
Err(mpsc::TryRecvError::Empty) => false, Err(mpsc::TryRecvError::Empty) => false,
Err(mpsc::TryRecvError::Disconnected) => { Err(mpsc::TryRecvError::Disconnected) => {
warn!("Channel was disconnected when checking incoming"); warn!("Channel was disconnected when checking incoming");
false false
}, }
} }
} }
pub fn try_recv(&mut self) -> Result<T, mpsc::TryRecvError> { pub fn try_recv(&mut self) -> Result<T, mpsc::TryRecvError> {
if let Some(first) = self.first.take() { if let Some(first) = self.first.take() {
return Ok(first); return Ok(first);
@@ -82,7 +88,9 @@ struct SharedFlagSetter {
} }
impl SharedFlagSetter { impl SharedFlagSetter {
pub fn set(&self) -> bool { pub fn set(&self) -> bool {
self.flag.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed).is_ok() self.flag
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
.is_ok()
} }
} }
@@ -96,13 +104,15 @@ struct SharedFlag {
// was queued and be able to read and clear the state atomically) // was queued and be able to read and clear the state atomically)
impl SharedFlag { impl SharedFlag {
pub fn new() -> Self { pub fn new() -> Self {
Self { flag: Arc::new(AtomicBool::new(false)) } Self {
flag: Arc::new(AtomicBool::new(false)),
}
} }
pub fn setter(&self) -> SharedFlagSetter { pub fn setter(&self) -> SharedFlagSetter {
SharedFlagSetter { flag: self.flag.clone() } SharedFlagSetter {
flag: self.flag.clone(),
}
} }
pub fn get_and_reset(&self) -> bool { pub fn get_and_reset(&self) -> bool {
self.flag.swap(false, std::sync::atomic::Ordering::AcqRel) self.flag.swap(false, std::sync::atomic::Ordering::AcqRel)
} }
@@ -116,9 +126,11 @@ pub struct RedrawRequester {
impl RedrawRequester { impl RedrawRequester {
fn new(flag: &SharedFlag, waker: AndroidAppWaker) -> Self { fn new(flag: &SharedFlag, waker: AndroidAppWaker) -> Self {
RedrawRequester { flag: flag.setter(), waker } RedrawRequester {
flag: flag.setter(),
waker,
}
} }
pub fn request_redraw(&self) { pub fn request_redraw(&self) {
if self.flag.set() { if self.flag.set() {
// Only explicitly try to wake up the main loop when the flag // Only explicitly try to wake up the main loop when the flag
@@ -136,7 +148,7 @@ pub struct EventLoop<T: 'static> {
window_target: event_loop::ActiveEventLoop, window_target: event_loop::ActiveEventLoop,
redraw_flag: SharedFlag, redraw_flag: SharedFlag,
user_events_sender: mpsc::Sender<T>, user_events_sender: mpsc::Sender<T>,
user_events_receiver: PeekableReceiver<T>, // must wake looper whenever something gets sent user_events_receiver: PeekableReceiver<T>, //must wake looper whenever something gets sent
loop_running: bool, // Dispatched `NewEvents<Init>` loop_running: bool, // Dispatched `NewEvents<Init>`
running: bool, running: bool,
pending_redraw: bool, pending_redraw: bool,
@@ -153,7 +165,10 @@ pub(crate) struct PlatformSpecificEventLoopAttributes {
impl Default for PlatformSpecificEventLoopAttributes { impl Default for PlatformSpecificEventLoopAttributes {
fn default() -> Self { fn default() -> Self {
Self { android_app: Default::default(), ignore_volume_keys: true } Self {
android_app: Default::default(),
ignore_volume_keys: true,
}
} }
} }
@@ -163,10 +178,7 @@ impl<T: 'static> EventLoop<T> {
) -> Result<Self, EventLoopError> { ) -> Result<Self, EventLoopError> {
let (user_events_sender, user_events_receiver) = mpsc::channel(); let (user_events_sender, user_events_receiver) = mpsc::channel();
let android_app = attributes.android_app.as_ref().expect( let android_app = attributes.android_app.as_ref().expect("An `AndroidApp` as passed to android_main() is required to create an `EventLoop` on Android");
"An `AndroidApp` as passed to android_main() is required to create an `EventLoop` on \
Android",
);
let redraw_flag = SharedFlag::new(); let redraw_flag = SharedFlag::new();
Ok(Self { Ok(Self {
@@ -213,15 +225,15 @@ impl<T: 'static> EventLoop<T> {
match event { match event {
MainEvent::InitWindow { .. } => { MainEvent::InitWindow { .. } => {
callback(event::Event::Resumed, self.window_target()); callback(event::Event::Resumed, self.window_target());
}, }
MainEvent::TerminateWindow { .. } => { MainEvent::TerminateWindow { .. } => {
callback(event::Event::Suspended, self.window_target()); callback(event::Event::Suspended, self.window_target());
}, }
MainEvent::WindowResized { .. } => resized = true, MainEvent::WindowResized { .. } => resized = true,
MainEvent::RedrawNeeded { .. } => pending_redraw = true, MainEvent::RedrawNeeded { .. } => pending_redraw = true,
MainEvent::ContentRectChanged { .. } => { MainEvent::ContentRectChanged { .. } => {
warn!("TODO: find a way to notify application of content rect change"); warn!("TODO: find a way to notify application of content rect change");
}, }
MainEvent::GainedFocus => { MainEvent::GainedFocus => {
HAS_FOCUS.store(true, Ordering::Relaxed); HAS_FOCUS.store(true, Ordering::Relaxed);
callback( callback(
@@ -231,7 +243,7 @@ impl<T: 'static> EventLoop<T> {
}, },
self.window_target(), self.window_target(),
); );
}, }
MainEvent::LostFocus => { MainEvent::LostFocus => {
HAS_FOCUS.store(false, Ordering::Relaxed); HAS_FOCUS.store(false, Ordering::Relaxed);
callback( callback(
@@ -241,7 +253,7 @@ impl<T: 'static> EventLoop<T> {
}, },
self.window_target(), self.window_target(),
); );
}, }
MainEvent::ConfigChanged { .. } => { MainEvent::ConfigChanged { .. } => {
let monitor = MonitorHandle::new(self.android_app.clone()); let monitor = MonitorHandle::new(self.android_app.clone());
let old_scale_factor = monitor.scale_factor(); let old_scale_factor = monitor.scale_factor();
@@ -261,43 +273,43 @@ impl<T: 'static> EventLoop<T> {
}; };
callback(event, self.window_target()); callback(event, self.window_target());
} }
}, }
MainEvent::LowMemory => { MainEvent::LowMemory => {
callback(event::Event::MemoryWarning, self.window_target()); callback(event::Event::MemoryWarning, self.window_target());
}, }
MainEvent::Start => { MainEvent::Start => {
// XXX: how to forward this state to applications? // XXX: how to forward this state to applications?
warn!("TODO: forward onStart notification to application"); warn!("TODO: forward onStart notification to application");
}, }
MainEvent::Resume { .. } => { MainEvent::Resume { .. } => {
debug!("App Resumed - is running"); debug!("App Resumed - is running");
self.running = true; self.running = true;
}, }
MainEvent::SaveState { .. } => { MainEvent::SaveState { .. } => {
// XXX: how to forward this state to applications? // XXX: how to forward this state to applications?
// XXX: also how do we expose state restoration to apps? // XXX: also how do we expose state restoration to apps?
warn!("TODO: forward saveState notification to application"); warn!("TODO: forward saveState notification to application");
}, }
MainEvent::Pause => { MainEvent::Pause => {
debug!("App Paused - stopped running"); debug!("App Paused - stopped running");
self.running = false; self.running = false;
}, }
MainEvent::Stop => { MainEvent::Stop => {
// XXX: how to forward this state to applications? // XXX: how to forward this state to applications?
warn!("TODO: forward onStop notification to application"); warn!("TODO: forward onStop notification to application");
}, }
MainEvent::Destroy => { MainEvent::Destroy => {
// XXX: maybe exit mainloop to drop things before being // XXX: maybe exit mainloop to drop things before being
// killed by the OS? // killed by the OS?
warn!("TODO: forward onDestroy notification to application"); warn!("TODO: forward onDestroy notification to application");
}, }
MainEvent::InsetsChanged { .. } => { MainEvent::InsetsChanged { .. } => {
// XXX: how to forward this state to applications? // XXX: how to forward this state to applications?
warn!("TODO: handle Android InsetsChanged notification"); warn!("TODO: handle Android InsetsChanged notification");
}, }
unknown => { unknown => {
trace!("Unknown MainEvent {unknown:?} (ignored)"); trace!("Unknown MainEvent {unknown:?} (ignored)");
}, }
} }
} else { } else {
trace!("No main event to handle"); trace!("No main event to handle");
@@ -319,7 +331,7 @@ impl<T: 'static> EventLoop<T> {
}, },
Err(err) => { Err(err) => {
tracing::warn!("Failed to get input events iterator: {err:?}"); tracing::warn!("Failed to get input events iterator: {err:?}");
}, }
} }
// Empty the user event buffer // Empty the user event buffer
@@ -380,13 +392,13 @@ impl<T: 'static> EventLoop<T> {
let phase = match motion_event.action() { let phase = match motion_event.action() {
MotionAction::Down | MotionAction::PointerDown => { MotionAction::Down | MotionAction::PointerDown => {
Some(event::TouchPhase::Started) Some(event::TouchPhase::Started)
}, }
MotionAction::Up | MotionAction::PointerUp => Some(event::TouchPhase::Ended), MotionAction::Up | MotionAction::PointerUp => Some(event::TouchPhase::Ended),
MotionAction::Move => Some(event::TouchPhase::Moved), MotionAction::Move => Some(event::TouchPhase::Moved),
MotionAction::Cancel => Some(event::TouchPhase::Cancelled), MotionAction::Cancel => Some(event::TouchPhase::Cancelled),
_ => { _ => {
None // TODO mouse events None // TODO mouse events
}, }
}; };
if let Some(phase) = phase { if let Some(phase) = phase {
let pointers: Box<dyn Iterator<Item = android_activity::input::Pointer<'_>>> = let pointers: Box<dyn Iterator<Item = android_activity::input::Pointer<'_>>> =
@@ -395,19 +407,18 @@ impl<T: 'static> EventLoop<T> {
Box::new(std::iter::once( Box::new(std::iter::once(
motion_event.pointer_at_index(motion_event.pointer_index()), motion_event.pointer_at_index(motion_event.pointer_index()),
)) ))
}, }
event::TouchPhase::Moved | event::TouchPhase::Cancelled => { event::TouchPhase::Moved | event::TouchPhase::Cancelled => {
Box::new(motion_event.pointers()) Box::new(motion_event.pointers())
}, }
}; };
for pointer in pointers { for pointer in pointers {
let location = let location = PhysicalPosition {
PhysicalPosition { x: pointer.x() as _, y: pointer.y() as _ }; x: pointer.x() as _,
trace!( y: pointer.y() as _,
"Input event {device_id:?}, {phase:?}, loc={location:?}, \ };
pointer={pointer:?}" trace!("Input event {device_id:?}, {phase:?}, loc={location:?}, pointer={pointer:?}");
);
let event = event::Event::WindowEvent { let event = event::Event::WindowEvent {
window_id, window_id,
event: event::WindowEvent::Touch(event::Touch { event: event::WindowEvent::Touch(event::Touch {
@@ -421,18 +432,17 @@ impl<T: 'static> EventLoop<T> {
callback(event, self.window_target()); callback(event, self.window_target());
} }
} }
}, }
InputEvent::KeyEvent(key) => { InputEvent::KeyEvent(key) => {
match key.key_code() { match key.key_code() {
// Flag keys related to volume as unhandled. While winit does not have a way for // Flag keys related to volume as unhandled. While winit does not have a way for applications
// applications to configure what keys to flag as handled, // to configure what keys to flag as handled, this appears to be a good default until winit
// this appears to be a good default until winit
// can be configured. // can be configured.
Keycode::VolumeUp | Keycode::VolumeDown | Keycode::VolumeMute Keycode::VolumeUp | Keycode::VolumeDown | Keycode::VolumeMute => {
if self.ignore_volume_keys => if self.ignore_volume_keys {
{ input_status = InputStatus::Unhandled
input_status = InputStatus::Unhandled }
}, }
keycode => { keycode => {
let state = match key.action() { let state = match key.action() {
KeyAction::Down => event::ElementState::Pressed, KeyAction::Down => event::ElementState::Pressed,
@@ -463,12 +473,12 @@ impl<T: 'static> EventLoop<T> {
}, },
}; };
callback(event, self.window_target()); callback(event, self.window_target());
}, }
} }
}, }
_ => { _ => {
warn!("Unknown android_activity input event {event:?}") warn!("Unknown android_activity input event {event:?}")
}, }
} }
input_status input_status
@@ -489,13 +499,13 @@ impl<T: 'static> EventLoop<T> {
match self.pump_events(None, &mut event_handler) { match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => { PumpStatus::Exit(0) => {
break Ok(()); break Ok(());
}, }
PumpStatus::Exit(code) => { PumpStatus::Exit(code) => {
break Err(EventLoopError::ExitFailure(code)); break Err(EventLoopError::ExitFailure(code));
}, }
_ => { _ => {
continue; continue;
}, }
} }
} }
} }
@@ -551,7 +561,7 @@ impl<T: 'static> EventLoop<T> {
ControlFlow::Poll => Some(Duration::ZERO), ControlFlow::Poll => Some(Duration::ZERO),
ControlFlow::WaitUntil(wait_deadline) => { ControlFlow::WaitUntil(wait_deadline) => {
Some(wait_deadline.saturating_duration_since(start)) Some(wait_deadline.saturating_duration_since(start))
}, }
}; };
min_timeout(control_flow_timeout, timeout) min_timeout(control_flow_timeout, timeout)
@@ -564,9 +574,8 @@ impl<T: 'static> EventLoop<T> {
match poll_event { match poll_event {
android_activity::PollEvent::Wake => { android_activity::PollEvent::Wake => {
// In the X11 backend it's noted that too many false-positive wake ups // In the X11 backend it's noted that too many false-positive wake ups
// would cause the event loop to run continuously. They handle this by // would cause the event loop to run continuously. They handle this by re-checking
// re-checking for pending events (assuming they cover all // for pending events (assuming they cover all valid reasons for a wake up).
// valid reasons for a wake up).
// //
// For now, user_events and redraw_requests are the only reasons to expect // For now, user_events and redraw_requests are the only reasons to expect
// a wake up here so we can ignore the wake up if there are no events/requests. // a wake up here so we can ignore the wake up if there are no events/requests.
@@ -577,26 +586,35 @@ impl<T: 'static> EventLoop<T> {
{ {
return; return;
} }
}, }
android_activity::PollEvent::Timeout => {}, android_activity::PollEvent::Timeout => {}
android_activity::PollEvent::Main(event) => { android_activity::PollEvent::Main(event) => {
main_event = Some(event); main_event = Some(event);
}, }
unknown_event => { unknown_event => {
warn!("Unknown poll event {unknown_event:?} (ignored)"); warn!("Unknown poll event {unknown_event:?} (ignored)");
}, }
} }
self.cause = match self.control_flow() { self.cause = match self.control_flow() {
ControlFlow::Poll => StartCause::Poll, ControlFlow::Poll => StartCause::Poll,
ControlFlow::Wait => StartCause::WaitCancelled { start, requested_resume: None }, ControlFlow::Wait => StartCause::WaitCancelled {
start,
requested_resume: None,
},
ControlFlow::WaitUntil(deadline) => { ControlFlow::WaitUntil(deadline) => {
if Instant::now() < deadline { if Instant::now() < deadline {
StartCause::WaitCancelled { start, requested_resume: Some(deadline) } StartCause::WaitCancelled {
start,
requested_resume: Some(deadline),
}
} else { } else {
StartCause::ResumeTimeReached { start, requested_resume: deadline } StartCause::ResumeTimeReached {
start,
requested_resume: deadline,
}
} }
}, }
}; };
self.single_iteration(main_event, &mut callback); self.single_iteration(main_event, &mut callback);
@@ -639,7 +657,9 @@ impl<T: 'static> Clone for EventLoopProxy<T> {
impl<T> EventLoopProxy<T> { impl<T> EventLoopProxy<T> {
pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed<T>> { pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed<T>> {
self.user_events_sender.send(event).map_err(|err| event_loop::EventLoopClosed(err.0))?; self.user_events_sender
.send(event)
.map_err(|err| event_loop::EventLoopClosed(err.0))?;
self.waker.wake(); self.waker.wake();
Ok(()) Ok(())
} }
@@ -659,7 +679,9 @@ impl ActiveEventLoop {
pub fn create_custom_cursor(&self, source: CustomCursorSource) -> CustomCursor { pub fn create_custom_cursor(&self, source: CustomCursorSource) -> CustomCursor {
let _ = source.inner; let _ = source.inner;
CustomCursor { inner: PlatformCustomCursor } CustomCursor {
inner: PlatformCustomCursor,
}
} }
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> { pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
@@ -682,7 +704,9 @@ impl ActiveEventLoop {
pub fn raw_display_handle_rwh_06( pub fn raw_display_handle_rwh_06(
&self, &self,
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> { ) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
Ok(rwh_06::RawDisplayHandle::Android(rwh_06::AndroidDisplayHandle::new())) Ok(rwh_06::RawDisplayHandle::Android(
rwh_06::AndroidDisplayHandle::new(),
))
} }
pub(crate) fn set_control_flow(&self, control_flow: ControlFlow) { pub(crate) fn set_control_flow(&self, control_flow: ControlFlow) {
@@ -774,7 +798,10 @@ impl Window {
) -> Result<Self, error::OsError> { ) -> Result<Self, error::OsError> {
// FIXME this ignores requested window attributes // FIXME this ignores requested window attributes
Ok(Self { app: el.app.clone(), redraw_requester: el.redraw_requester.clone() }) Ok(Self {
app: el.app.clone(),
redraw_requester: el.redraw_requester.clone(),
})
} }
pub(crate) fn maybe_queue_on_main(&self, f: impl FnOnce(&Self) + Send + 'static) { pub(crate) fn maybe_queue_on_main(&self, f: impl FnOnce(&Self) + Send + 'static) {
@@ -914,31 +941,41 @@ impl Window {
pub fn set_cursor(&self, _: Cursor) {} pub fn set_cursor(&self, _: Cursor) {}
pub fn set_cursor_position(&self, _: Position) -> Result<(), error::ExternalError> { pub fn set_cursor_position(&self, _: Position) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(error::NotSupportedError::new())) Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
} }
pub fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), error::ExternalError> { pub fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(error::NotSupportedError::new())) Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
} }
pub fn set_cursor_visible(&self, _: bool) {} pub fn set_cursor_visible(&self, _: bool) {}
pub fn drag_window(&self) -> Result<(), error::ExternalError> { pub fn drag_window(&self) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(error::NotSupportedError::new())) Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
} }
pub fn drag_resize_window( pub fn drag_resize_window(
&self, &self,
_direction: ResizeDirection, _direction: ResizeDirection,
) -> Result<(), error::ExternalError> { ) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(error::NotSupportedError::new())) Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
} }
#[inline] #[inline]
pub fn show_window_menu(&self, _position: Position) {} pub fn show_window_menu(&self, _position: Position) {}
pub fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), error::ExternalError> { pub fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(error::NotSupportedError::new())) Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
} }
#[cfg(feature = "rwh_04")] #[cfg(feature = "rwh_04")]
@@ -948,11 +985,7 @@ impl Window {
if let Some(native_window) = self.app.native_window().as_ref() { if let Some(native_window) = self.app.native_window().as_ref() {
native_window.raw_window_handle() native_window.raw_window_handle()
} else { } else {
panic!( panic!("Cannot get the native window, it's null and will always be null before Event::Resumed and after Event::Suspended. Make sure you only call this function between those events.");
"Cannot get the native window, it's null and will always be null before \
Event::Resumed and after Event::Suspended. Make sure you only call this function \
between those events."
);
} }
} }
@@ -963,11 +996,7 @@ impl Window {
if let Some(native_window) = self.app.native_window().as_ref() { if let Some(native_window) = self.app.native_window().as_ref() {
native_window.raw_window_handle() native_window.raw_window_handle()
} else { } else {
panic!( panic!("Cannot get the native window, it's null and will always be null before Event::Resumed and after Event::Suspended. Make sure you only call this function between those events.");
"Cannot get the native window, it's null and will always be null before \
Event::Resumed and after Event::Suspended. Make sure you only call this function \
between those events."
);
} }
} }
@@ -985,11 +1014,7 @@ impl Window {
if let Some(native_window) = self.app.native_window().as_ref() { if let Some(native_window) = self.app.native_window().as_ref() {
native_window.raw_window_handle() native_window.raw_window_handle()
} else { } else {
tracing::error!( tracing::error!("Cannot get the native window, it's null and will always be null before Event::Resumed and after Event::Suspended. Make sure you only call this function between those events.");
"Cannot get the native window, it's null and will always be null before \
Event::Resumed and after Event::Suspended. Make sure you only call this function \
between those events."
);
Err(rwh_06::HandleError::Unavailable) Err(rwh_06::HandleError::Unavailable)
} }
} }
@@ -998,7 +1023,9 @@ impl Window {
pub fn raw_display_handle_rwh_06( pub fn raw_display_handle_rwh_06(
&self, &self,
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> { ) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
Ok(rwh_06::RawDisplayHandle::Android(rwh_06::AndroidDisplayHandle::new())) Ok(rwh_06::RawDisplayHandle::Android(
rwh_06::AndroidDisplayHandle::new(),
))
} }
pub fn config(&self) -> ConfigurationRef { pub fn config(&self) -> ConfigurationRef {
@@ -1075,7 +1102,11 @@ impl MonitorHandle {
} }
pub fn scale_factor(&self) -> f64 { pub fn scale_factor(&self) -> f64 {
self.app.config().density().map(|dpi| dpi as f64 / 160.0).unwrap_or(1.0) self.app
.config()
.density()
.map(|dpi| dpi as f64 / 160.0)
.unwrap_or(1.0)
} }
pub fn refresh_rate_millihertz(&self) -> Option<u32> { pub fn refresh_rate_millihertz(&self) -> Option<u32> {

View File

@@ -1,9 +1,13 @@
use icrate::Foundation::{MainThreadMarker, NSObject, NSObjectProtocol};
use objc2::{declare_class, mutability, ClassType, DeclaredClass}; use objc2::{declare_class, mutability, ClassType, DeclaredClass};
use objc2_foundation::{MainThreadMarker, NSObject};
use objc2_ui_kit::UIApplication;
use super::app_state::{self, send_occluded_event_for_all_windows, EventWrapper}; use super::app_state::{self, EventWrapper};
use crate::event::Event; use super::uikit::{UIApplication, UIWindow};
use super::window::WinitUIWindow;
use crate::{
event::{Event, WindowEvent},
window::WindowId as RootWindowId,
};
declare_class!( declare_class!(
pub struct AppDelegate; pub struct AppDelegate;
@@ -38,17 +42,34 @@ declare_class!(
#[method(applicationWillEnterForeground:)] #[method(applicationWillEnterForeground:)]
fn will_enter_foreground(&self, application: &UIApplication) { fn will_enter_foreground(&self, application: &UIApplication) {
send_occluded_event_for_all_windows(application, false); self.send_occluded_event_for_all_windows(application, false);
} }
#[method(applicationDidEnterBackground:)] #[method(applicationDidEnterBackground:)]
fn did_enter_background(&self, application: &UIApplication) { fn did_enter_background(&self, application: &UIApplication) {
send_occluded_event_for_all_windows(application, true); self.send_occluded_event_for_all_windows(application, true);
} }
#[method(applicationWillTerminate:)] #[method(applicationWillTerminate:)]
fn will_terminate(&self, application: &UIApplication) { fn will_terminate(&self, application: &UIApplication) {
app_state::terminated(application); let mut events = Vec::new();
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
let window = unsafe {
let ptr: *const UIWindow = window;
let ptr: *const WinitUIWindow = ptr.cast();
&*ptr
};
events.push(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::Destroyed,
}));
}
}
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_events(mtm, events);
app_state::terminated(mtm);
} }
#[method(applicationDidReceiveMemoryWarning:)] #[method(applicationDidReceiveMemoryWarning:)]
@@ -58,3 +79,25 @@ declare_class!(
} }
} }
); );
impl AppDelegate {
fn send_occluded_event_for_all_windows(&self, application: &UIApplication, occluded: bool) {
let mut events = Vec::new();
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
let window = unsafe {
let ptr: *const UIWindow = window;
let ptr: *const WinitUIWindow = ptr.cast();
&*ptr
};
events.push(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::Occluded(occluded),
}));
}
}
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_events(mtm, events);
}
}

View File

@@ -1,11 +1,14 @@
#![deny(unused_results)] #![deny(unused_results)]
use std::cell::{RefCell, RefMut}; use std::{
use std::collections::HashSet; cell::{RefCell, RefMut},
use std::os::raw::c_void; collections::HashSet,
use std::sync::{Arc, Mutex, OnceLock}; fmt, mem,
use std::time::Instant; os::raw::c_void,
use std::{fmt, mem, ptr}; ptr,
sync::{Arc, Mutex, OnceLock},
time::Instant,
};
use core_foundation::base::CFRelease; use core_foundation::base::CFRelease;
use core_foundation::date::CFAbsoluteTimeGetCurrent; use core_foundation::date::CFAbsoluteTimeGetCurrent;
@@ -13,20 +16,21 @@ use core_foundation::runloop::{
kCFRunLoopCommonModes, CFRunLoopAddTimer, CFRunLoopGetMain, CFRunLoopRef, CFRunLoopTimerCreate, kCFRunLoopCommonModes, CFRunLoopAddTimer, CFRunLoopGetMain, CFRunLoopRef, CFRunLoopTimerCreate,
CFRunLoopTimerInvalidate, CFRunLoopTimerRef, CFRunLoopTimerSetNextFireDate, CFRunLoopTimerInvalidate, CFRunLoopTimerRef, CFRunLoopTimerSetNextFireDate,
}; };
use objc2::rc::Retained; use icrate::Foundation::{
CGRect, CGSize, MainThreadMarker, NSInteger, NSOperatingSystemVersion, NSProcessInfo,
};
use objc2::rc::Id;
use objc2::runtime::AnyObject; use objc2::runtime::AnyObject;
use objc2::{msg_send, sel}; use objc2::{msg_send, sel};
use objc2_foundation::{
CGRect, CGSize, MainThreadMarker, NSInteger, NSObjectProtocol, NSOperatingSystemVersion,
NSProcessInfo,
};
use objc2_ui_kit::{UIApplication, UICoordinateSpace, UIView, UIWindow};
use super::uikit::UIView;
use super::window::WinitUIWindow; use super::window::WinitUIWindow;
use crate::dpi::PhysicalSize; use crate::{
use crate::event::{Event, InnerSizeWriter, StartCause, WindowEvent}; dpi::PhysicalSize,
use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow}; event::{Event, InnerSizeWriter, StartCause, WindowEvent},
use crate::window::WindowId as RootWindowId; event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow},
window::WindowId as RootWindowId,
};
macro_rules! bug { macro_rules! bug {
($($msg:tt)*) => { ($($msg:tt)*) => {
@@ -72,7 +76,7 @@ pub(crate) enum EventWrapper {
#[derive(Debug)] #[derive(Debug)]
pub struct ScaleFactorChanged { pub struct ScaleFactorChanged {
pub(super) window: Retained<WinitUIWindow>, pub(super) window: Id<WinitUIWindow>,
pub(super) suggested_size: PhysicalSize<u32>, pub(super) suggested_size: PhysicalSize<u32>,
pub(super) scale_factor: f64, pub(super) scale_factor: f64,
} }
@@ -90,7 +94,13 @@ enum UserCallbackTransitionResult<'a> {
impl Event<HandlePendingUserEvents> { impl Event<HandlePendingUserEvents> {
fn is_redraw(&self) -> bool { fn is_redraw(&self) -> bool {
matches!(self, Event::WindowEvent { event: WindowEvent::RedrawRequested, .. }) matches!(
self,
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
}
)
} }
} }
@@ -99,25 +109,25 @@ impl Event<HandlePendingUserEvents> {
#[must_use = "dropping `AppStateImpl` without inspecting it is probably a bug"] #[must_use = "dropping `AppStateImpl` without inspecting it is probably a bug"]
enum AppStateImpl { enum AppStateImpl {
NotLaunched { NotLaunched {
queued_windows: Vec<Retained<WinitUIWindow>>, queued_windows: Vec<Id<WinitUIWindow>>,
queued_events: Vec<EventWrapper>, queued_events: Vec<EventWrapper>,
queued_gpu_redraws: HashSet<Retained<WinitUIWindow>>, queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
}, },
Launching { Launching {
queued_windows: Vec<Retained<WinitUIWindow>>, queued_windows: Vec<Id<WinitUIWindow>>,
queued_events: Vec<EventWrapper>, queued_events: Vec<EventWrapper>,
queued_handler: EventLoopHandler, queued_handler: EventLoopHandler,
queued_gpu_redraws: HashSet<Retained<WinitUIWindow>>, queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
}, },
ProcessingEvents { ProcessingEvents {
handler: EventLoopHandler, handler: EventLoopHandler,
queued_gpu_redraws: HashSet<Retained<WinitUIWindow>>, queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
active_control_flow: ControlFlow, active_control_flow: ControlFlow,
}, },
// special state to deal with reentrancy and prevent mutable aliasing. // special state to deal with reentrancy and prevent mutable aliasing.
InUserCallback { InUserCallback {
queued_events: Vec<EventWrapper>, queued_events: Vec<EventWrapper>,
queued_gpu_redraws: HashSet<Retained<WinitUIWindow>>, queued_gpu_redraws: HashSet<Id<WinitUIWindow>>,
}, },
ProcessingRedraws { ProcessingRedraws {
handler: EventLoopHandler, handler: EventLoopHandler,
@@ -206,7 +216,10 @@ impl AppState {
} }
fn has_launched(&self) -> bool { fn has_launched(&self) -> bool {
!matches!(self.state(), AppStateImpl::NotLaunched { .. } | AppStateImpl::Launching { .. }) !matches!(
self.state(),
AppStateImpl::NotLaunched { .. } | AppStateImpl::Launching { .. }
)
} }
fn has_terminated(&self) -> bool { fn has_terminated(&self) -> bool {
@@ -215,9 +228,11 @@ impl AppState {
fn will_launch_transition(&mut self, queued_handler: EventLoopHandler) { fn will_launch_transition(&mut self, queued_handler: EventLoopHandler) {
let (queued_windows, queued_events, queued_gpu_redraws) = match self.take_state() { let (queued_windows, queued_events, queued_gpu_redraws) = match self.take_state() {
AppStateImpl::NotLaunched { queued_windows, queued_events, queued_gpu_redraws } => { AppStateImpl::NotLaunched {
(queued_windows, queued_events, queued_gpu_redraws) queued_windows,
}, queued_events,
queued_gpu_redraws,
} => (queued_windows, queued_events, queued_gpu_redraws),
s => bug!("unexpected state {:?}", s), s => bug!("unexpected state {:?}", s),
}; };
self.set_state(AppStateImpl::Launching { self.set_state(AppStateImpl::Launching {
@@ -228,16 +243,19 @@ impl AppState {
}); });
} }
fn did_finish_launching_transition( fn did_finish_launching_transition(&mut self) -> (Vec<Id<WinitUIWindow>>, Vec<EventWrapper>) {
&mut self,
) -> (Vec<Retained<WinitUIWindow>>, Vec<EventWrapper>) {
let (windows, events, handler, queued_gpu_redraws) = match self.take_state() { let (windows, events, handler, queued_gpu_redraws) = match self.take_state() {
AppStateImpl::Launching { AppStateImpl::Launching {
queued_windows, queued_windows,
queued_events, queued_events,
queued_handler, queued_handler,
queued_gpu_redraws, queued_gpu_redraws,
} => (queued_windows, queued_events, queued_handler, queued_gpu_redraws), } => (
queued_windows,
queued_events,
queued_handler,
queued_gpu_redraws,
),
s => bug!("unexpected state {:?}", s), s => bug!("unexpected state {:?}", s),
}; };
self.set_state(AppStateImpl::ProcessingEvents { self.set_state(AppStateImpl::ProcessingEvents {
@@ -256,10 +274,17 @@ impl AppState {
} }
let (handler, event) = match (self.control_flow, self.take_state()) { let (handler, event) = match (self.control_flow, self.take_state()) {
(ControlFlow::Poll, AppStateImpl::PollFinished { waiting_handler }) => { (ControlFlow::Poll, AppStateImpl::PollFinished { waiting_handler }) => (
(waiting_handler, EventWrapper::StaticEvent(Event::NewEvents(StartCause::Poll))) waiting_handler,
}, EventWrapper::StaticEvent(Event::NewEvents(StartCause::Poll)),
(ControlFlow::Wait, AppStateImpl::Waiting { waiting_handler, start }) => ( ),
(
ControlFlow::Wait,
AppStateImpl::Waiting {
waiting_handler,
start,
},
) => (
waiting_handler, waiting_handler,
EventWrapper::StaticEvent(Event::NewEvents(StartCause::WaitCancelled { EventWrapper::StaticEvent(Event::NewEvents(StartCause::WaitCancelled {
start, start,
@@ -268,7 +293,10 @@ impl AppState {
), ),
( (
ControlFlow::WaitUntil(requested_resume), ControlFlow::WaitUntil(requested_resume),
AppStateImpl::Waiting { waiting_handler, start }, AppStateImpl::Waiting {
waiting_handler,
start,
},
) => { ) => {
let event = if Instant::now() >= requested_resume { let event = if Instant::now() >= requested_resume {
EventWrapper::StaticEvent(Event::NewEvents(StartCause::ResumeTimeReached { EventWrapper::StaticEvent(Event::NewEvents(StartCause::ResumeTimeReached {
@@ -282,7 +310,7 @@ impl AppState {
})) }))
}; };
(waiting_handler, event) (waiting_handler, event)
}, }
s => bug!("`EventHandler` unexpectedly woke up {:?}", s), s => bug!("`EventHandler` unexpectedly woke up {:?}", s),
}; };
@@ -298,9 +326,18 @@ impl AppState {
// If we're not able to process an event due to recursion or `Init` not having been sent out // If we're not able to process an event due to recursion or `Init` not having been sent out
// yet, then queue the events up. // yet, then queue the events up.
match self.state_mut() { match self.state_mut() {
&mut AppStateImpl::Launching { ref mut queued_events, .. } &mut AppStateImpl::Launching {
| &mut AppStateImpl::NotLaunched { ref mut queued_events, .. } ref mut queued_events,
| &mut AppStateImpl::InUserCallback { ref mut queued_events, .. } => { ..
}
| &mut AppStateImpl::NotLaunched {
ref mut queued_events,
..
}
| &mut AppStateImpl::InUserCallback {
ref mut queued_events,
..
} => {
// A lifetime cast: early returns are not currently handled well with NLL, but // A lifetime cast: early returns are not currently handled well with NLL, but
// polonius handles them well. This transmute is a safe workaround. // polonius handles them well. This transmute is a safe workaround.
return unsafe { return unsafe {
@@ -311,49 +348,60 @@ impl AppState {
queued_events, queued_events,
}) })
}; };
}, }
&mut AppStateImpl::ProcessingEvents { .. } &mut AppStateImpl::ProcessingEvents { .. }
| &mut AppStateImpl::ProcessingRedraws { .. } => {}, | &mut AppStateImpl::ProcessingRedraws { .. } => {}
s @ &mut AppStateImpl::PollFinished { .. } s @ &mut AppStateImpl::PollFinished { .. }
| s @ &mut AppStateImpl::Waiting { .. } | s @ &mut AppStateImpl::Waiting { .. }
| s @ &mut AppStateImpl::Terminated => { | s @ &mut AppStateImpl::Terminated => {
bug!("unexpected attempted to process an event {:?}", s) bug!("unexpected attempted to process an event {:?}", s)
}, }
} }
let (handler, queued_gpu_redraws, active_control_flow, processing_redraws) = match self let (handler, queued_gpu_redraws, active_control_flow, processing_redraws) =
.take_state() match self.take_state() {
{ AppStateImpl::Launching { .. }
AppStateImpl::Launching { .. } | AppStateImpl::NotLaunched { .. }
| AppStateImpl::NotLaunched { .. } | AppStateImpl::InUserCallback { .. } => unreachable!(),
| AppStateImpl::InUserCallback { .. } => unreachable!(), AppStateImpl::ProcessingEvents {
AppStateImpl::ProcessingEvents { handler, queued_gpu_redraws, active_control_flow } => { handler,
(handler, queued_gpu_redraws, active_control_flow, false) queued_gpu_redraws,
}, active_control_flow,
AppStateImpl::ProcessingRedraws { handler, active_control_flow } => { } => (handler, queued_gpu_redraws, active_control_flow, false),
(handler, Default::default(), active_control_flow, true) AppStateImpl::ProcessingRedraws {
}, handler,
AppStateImpl::PollFinished { .. } active_control_flow,
| AppStateImpl::Waiting { .. } } => (handler, Default::default(), active_control_flow, true),
| AppStateImpl::Terminated => unreachable!(), AppStateImpl::PollFinished { .. }
}; | AppStateImpl::Waiting { .. }
| AppStateImpl::Terminated => unreachable!(),
};
self.set_state(AppStateImpl::InUserCallback { self.set_state(AppStateImpl::InUserCallback {
queued_events: Vec::new(), queued_events: Vec::new(),
queued_gpu_redraws, queued_gpu_redraws,
}); });
UserCallbackTransitionResult::Success { handler, active_control_flow, processing_redraws } UserCallbackTransitionResult::Success {
handler,
active_control_flow,
processing_redraws,
}
} }
fn main_events_cleared_transition(&mut self) -> HashSet<Retained<WinitUIWindow>> { fn main_events_cleared_transition(&mut self) -> HashSet<Id<WinitUIWindow>> {
let (handler, queued_gpu_redraws, active_control_flow) = match self.take_state() { let (handler, queued_gpu_redraws, active_control_flow) = match self.take_state() {
AppStateImpl::ProcessingEvents { handler, queued_gpu_redraws, active_control_flow } => { AppStateImpl::ProcessingEvents {
(handler, queued_gpu_redraws, active_control_flow) handler,
}, queued_gpu_redraws,
active_control_flow,
} => (handler, queued_gpu_redraws, active_control_flow),
s => bug!("unexpected state {:?}", s), s => bug!("unexpected state {:?}", s),
}; };
self.set_state(AppStateImpl::ProcessingRedraws { handler, active_control_flow }); self.set_state(AppStateImpl::ProcessingRedraws {
handler,
active_control_flow,
});
queued_gpu_redraws queued_gpu_redraws
} }
@@ -362,9 +410,10 @@ impl AppState {
return; return;
} }
let (waiting_handler, old) = match self.take_state() { let (waiting_handler, old) = match self.take_state() {
AppStateImpl::ProcessingRedraws { handler, active_control_flow } => { AppStateImpl::ProcessingRedraws {
(handler, active_control_flow) handler,
}, active_control_flow,
} => (handler, active_control_flow),
s => bug!("unexpected state {:?}", s), s => bug!("unexpected state {:?}", s),
}; };
@@ -372,29 +421,41 @@ impl AppState {
match (old, new) { match (old, new) {
(ControlFlow::Wait, ControlFlow::Wait) => { (ControlFlow::Wait, ControlFlow::Wait) => {
let start = Instant::now(); let start = Instant::now();
self.set_state(AppStateImpl::Waiting { waiting_handler, start }); self.set_state(AppStateImpl::Waiting {
}, waiting_handler,
start,
});
}
(ControlFlow::WaitUntil(old_instant), ControlFlow::WaitUntil(new_instant)) (ControlFlow::WaitUntil(old_instant), ControlFlow::WaitUntil(new_instant))
if old_instant == new_instant => if old_instant == new_instant =>
{ {
let start = Instant::now(); let start = Instant::now();
self.set_state(AppStateImpl::Waiting { waiting_handler, start }); self.set_state(AppStateImpl::Waiting {
}, waiting_handler,
start,
});
}
(_, ControlFlow::Wait) => { (_, ControlFlow::Wait) => {
let start = Instant::now(); let start = Instant::now();
self.set_state(AppStateImpl::Waiting { waiting_handler, start }); self.set_state(AppStateImpl::Waiting {
waiting_handler,
start,
});
self.waker.stop() self.waker.stop()
}, }
(_, ControlFlow::WaitUntil(new_instant)) => { (_, ControlFlow::WaitUntil(new_instant)) => {
let start = Instant::now(); let start = Instant::now();
self.set_state(AppStateImpl::Waiting { waiting_handler, start }); self.set_state(AppStateImpl::Waiting {
waiting_handler,
start,
});
self.waker.start_at(new_instant) self.waker.start_at(new_instant)
}, }
// Unlike on macOS, handle Poll to Poll transition here to call the waker // Unlike on macOS, handle Poll to Poll transition here to call the waker
(_, ControlFlow::Poll) => { (_, ControlFlow::Poll) => {
self.set_state(AppStateImpl::PollFinished { waiting_handler }); self.set_state(AppStateImpl::PollFinished { waiting_handler });
self.waker.start() self.waker.start()
}, }
} }
} }
@@ -414,41 +475,54 @@ impl AppState {
} }
} }
pub(crate) fn set_key_window(mtm: MainThreadMarker, window: &Retained<WinitUIWindow>) { pub(crate) fn set_key_window(mtm: MainThreadMarker, window: &Id<WinitUIWindow>) {
let mut this = AppState::get_mut(mtm); let mut this = AppState::get_mut(mtm);
match this.state_mut() { match this.state_mut() {
&mut AppStateImpl::NotLaunched { ref mut queued_windows, .. } => { &mut AppStateImpl::NotLaunched {
return queued_windows.push(window.clone()) ref mut queued_windows,
}, ..
} => return queued_windows.push(window.clone()),
&mut AppStateImpl::ProcessingEvents { .. } &mut AppStateImpl::ProcessingEvents { .. }
| &mut AppStateImpl::InUserCallback { .. } | &mut AppStateImpl::InUserCallback { .. }
| &mut AppStateImpl::ProcessingRedraws { .. } => {}, | &mut AppStateImpl::ProcessingRedraws { .. } => {}
s @ &mut AppStateImpl::Launching { .. } s @ &mut AppStateImpl::Launching { .. }
| s @ &mut AppStateImpl::Waiting { .. } | s @ &mut AppStateImpl::Waiting { .. }
| s @ &mut AppStateImpl::PollFinished { .. } => bug!("unexpected state {:?}", s), | s @ &mut AppStateImpl::PollFinished { .. } => bug!("unexpected state {:?}", s),
&mut AppStateImpl::Terminated => { &mut AppStateImpl::Terminated => {
panic!("Attempt to create a `Window` after the app has terminated") panic!("Attempt to create a `Window` after the app has terminated")
}, }
} }
drop(this); drop(this);
window.makeKeyAndVisible(); window.makeKeyAndVisible();
} }
pub(crate) fn queue_gl_or_metal_redraw(mtm: MainThreadMarker, window: Retained<WinitUIWindow>) { pub(crate) fn queue_gl_or_metal_redraw(mtm: MainThreadMarker, window: Id<WinitUIWindow>) {
let mut this = AppState::get_mut(mtm); let mut this = AppState::get_mut(mtm);
match this.state_mut() { match this.state_mut() {
&mut AppStateImpl::NotLaunched { ref mut queued_gpu_redraws, .. } &mut AppStateImpl::NotLaunched {
| &mut AppStateImpl::Launching { ref mut queued_gpu_redraws, .. } ref mut queued_gpu_redraws,
| &mut AppStateImpl::ProcessingEvents { ref mut queued_gpu_redraws, .. } ..
| &mut AppStateImpl::InUserCallback { ref mut queued_gpu_redraws, .. } => { }
| &mut AppStateImpl::Launching {
ref mut queued_gpu_redraws,
..
}
| &mut AppStateImpl::ProcessingEvents {
ref mut queued_gpu_redraws,
..
}
| &mut AppStateImpl::InUserCallback {
ref mut queued_gpu_redraws,
..
} => {
let _ = queued_gpu_redraws.insert(window); let _ = queued_gpu_redraws.insert(window);
}, }
s @ &mut AppStateImpl::ProcessingRedraws { .. } s @ &mut AppStateImpl::ProcessingRedraws { .. }
| s @ &mut AppStateImpl::Waiting { .. } | s @ &mut AppStateImpl::Waiting { .. }
| s @ &mut AppStateImpl::PollFinished { .. } => bug!("unexpected state {:?}", s), | s @ &mut AppStateImpl::PollFinished { .. } => bug!("unexpected state {:?}", s),
&mut AppStateImpl::Terminated => { &mut AppStateImpl::Terminated => {
panic!("Attempt to create a `Window` after the app has terminated") panic!("Attempt to create a `Window` after the app has terminated")
}, }
} }
} }
@@ -492,8 +566,10 @@ pub fn did_finish_launching(mtm: MainThreadMarker) {
let (windows, events) = AppState::get_mut(mtm).did_finish_launching_transition(); let (windows, events) = AppState::get_mut(mtm).did_finish_launching_transition();
let events = std::iter::once(EventWrapper::StaticEvent(Event::NewEvents(StartCause::Init))) let events = std::iter::once(EventWrapper::StaticEvent(Event::NewEvents(
.chain(events); StartCause::Init,
)))
.chain(events);
handle_nonuser_events(mtm, events); handle_nonuser_events(mtm, events);
// the above window dance hack, could possibly trigger new windows to be created. // the above window dance hack, could possibly trigger new windows to be created.
@@ -533,7 +609,7 @@ pub(crate) fn handle_nonuser_events<I: IntoIterator<Item = EventWrapper>>(
UserCallbackTransitionResult::ReentrancyPrevented { queued_events } => { UserCallbackTransitionResult::ReentrancyPrevented { queued_events } => {
queued_events.extend(events); queued_events.extend(events);
return; return;
}, }
UserCallbackTransitionResult::Success { UserCallbackTransitionResult::Success {
handler, handler,
active_control_flow, active_control_flow,
@@ -554,7 +630,7 @@ pub(crate) fn handle_nonuser_events<I: IntoIterator<Item = EventWrapper>>(
); );
} }
handler.handle_event(event) handler.handle_event(event)
}, }
EventWrapper::ScaleFactorChanged(event) => handle_hidpi_proxy(&mut handler, event), EventWrapper::ScaleFactorChanged(event) => handle_hidpi_proxy(&mut handler, event),
} }
} }
@@ -562,16 +638,18 @@ pub(crate) fn handle_nonuser_events<I: IntoIterator<Item = EventWrapper>>(
loop { loop {
let mut this = AppState::get_mut(mtm); let mut this = AppState::get_mut(mtm);
let queued_events = match this.state_mut() { let queued_events = match this.state_mut() {
&mut AppStateImpl::InUserCallback { ref mut queued_events, queued_gpu_redraws: _ } => { &mut AppStateImpl::InUserCallback {
mem::take(queued_events) ref mut queued_events,
}, queued_gpu_redraws: _,
} => mem::take(queued_events),
s => bug!("unexpected state {:?}", s), s => bug!("unexpected state {:?}", s),
}; };
if queued_events.is_empty() { if queued_events.is_empty() {
let queued_gpu_redraws = match this.take_state() { let queued_gpu_redraws = match this.take_state() {
AppStateImpl::InUserCallback { queued_events: _, queued_gpu_redraws } => { AppStateImpl::InUserCallback {
queued_gpu_redraws queued_events: _,
}, queued_gpu_redraws,
} => queued_gpu_redraws,
_ => unreachable!(), _ => unreachable!(),
}; };
this.app_state = Some(if processing_redraws { this.app_state = Some(if processing_redraws {
@@ -579,9 +657,16 @@ pub(crate) fn handle_nonuser_events<I: IntoIterator<Item = EventWrapper>>(
queued_gpu_redraws.is_empty(), queued_gpu_redraws.is_empty(),
"redraw queued while processing redraws" "redraw queued while processing redraws"
); );
AppStateImpl::ProcessingRedraws { handler, active_control_flow } AppStateImpl::ProcessingRedraws {
handler,
active_control_flow,
}
} else { } else {
AppStateImpl::ProcessingEvents { handler, queued_gpu_redraws, active_control_flow } AppStateImpl::ProcessingEvents {
handler,
queued_gpu_redraws,
active_control_flow,
}
}); });
break; break;
} }
@@ -594,13 +679,12 @@ pub(crate) fn handle_nonuser_events<I: IntoIterator<Item = EventWrapper>>(
tracing::info!("processing `RedrawRequested` during the main event loop"); tracing::info!("processing `RedrawRequested` during the main event loop");
} else if processing_redraws && !event.is_redraw() { } else if processing_redraws && !event.is_redraw() {
tracing::warn!( tracing::warn!(
"processing non-`RedrawRequested` event after the main event loop: \ "processing non-`RedrawRequested` event after the main event loop: {:#?}",
{:#?}",
event event
); );
} }
handler.handle_event(event) handler.handle_event(event)
}, }
EventWrapper::ScaleFactorChanged(event) => handle_hidpi_proxy(&mut handler, event), EventWrapper::ScaleFactorChanged(event) => handle_hidpi_proxy(&mut handler, event),
} }
} }
@@ -613,7 +697,7 @@ fn handle_user_events(mtm: MainThreadMarker) {
match this.try_user_callback_transition() { match this.try_user_callback_transition() {
UserCallbackTransitionResult::ReentrancyPrevented { .. } => { UserCallbackTransitionResult::ReentrancyPrevented { .. } => {
bug!("unexpected attempted to process an event") bug!("unexpected attempted to process an event")
}, }
UserCallbackTransitionResult::Success { UserCallbackTransitionResult::Success {
handler, handler,
active_control_flow, active_control_flow,
@@ -630,16 +714,18 @@ fn handle_user_events(mtm: MainThreadMarker) {
loop { loop {
let mut this = AppState::get_mut(mtm); let mut this = AppState::get_mut(mtm);
let queued_events = match this.state_mut() { let queued_events = match this.state_mut() {
&mut AppStateImpl::InUserCallback { ref mut queued_events, queued_gpu_redraws: _ } => { &mut AppStateImpl::InUserCallback {
mem::take(queued_events) ref mut queued_events,
}, queued_gpu_redraws: _,
} => mem::take(queued_events),
s => bug!("unexpected state {:?}", s), s => bug!("unexpected state {:?}", s),
}; };
if queued_events.is_empty() { if queued_events.is_empty() {
let queued_gpu_redraws = match this.take_state() { let queued_gpu_redraws = match this.take_state() {
AppStateImpl::InUserCallback { queued_events: _, queued_gpu_redraws } => { AppStateImpl::InUserCallback {
queued_gpu_redraws queued_events: _,
}, queued_gpu_redraws,
} => queued_gpu_redraws,
_ => unreachable!(), _ => unreachable!(),
}; };
this.app_state = Some(AppStateImpl::ProcessingEvents { this.app_state = Some(AppStateImpl::ProcessingEvents {
@@ -662,35 +748,13 @@ fn handle_user_events(mtm: MainThreadMarker) {
} }
} }
pub(crate) fn send_occluded_event_for_all_windows(application: &UIApplication, occluded: bool) {
let mtm = MainThreadMarker::from(application);
let mut events = Vec::new();
#[allow(deprecated)]
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
let window = unsafe {
let ptr: *const UIWindow = window;
let ptr: *const WinitUIWindow = ptr.cast();
&*ptr
};
events.push(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::Occluded(occluded),
}));
}
}
handle_nonuser_events(mtm, events);
}
pub fn handle_main_events_cleared(mtm: MainThreadMarker) { pub fn handle_main_events_cleared(mtm: MainThreadMarker) {
let mut this = AppState::get_mut(mtm); let mut this = AppState::get_mut(mtm);
if !this.has_launched() || this.has_terminated() { if !this.has_launched() || this.has_terminated() {
return; return;
} }
match this.state_mut() { match this.state_mut() {
AppStateImpl::ProcessingEvents { .. } => {}, AppStateImpl::ProcessingEvents { .. } => {}
_ => bug!("`ProcessingRedraws` happened unexpectedly"), _ => bug!("`ProcessingRedraws` happened unexpectedly"),
}; };
drop(this); drop(this);
@@ -718,27 +782,7 @@ pub fn handle_events_cleared(mtm: MainThreadMarker) {
AppState::get_mut(mtm).events_cleared_transition(); AppState::get_mut(mtm).events_cleared_transition();
} }
pub(crate) fn terminated(application: &UIApplication) { pub fn terminated(mtm: MainThreadMarker) {
let mtm = MainThreadMarker::from(application);
let mut events = Vec::new();
#[allow(deprecated)]
for window in application.windows().iter() {
if window.is_kind_of::<WinitUIWindow>() {
// SAFETY: We just checked that the window is a `winit` window
let window = unsafe {
let ptr: *const UIWindow = window;
let ptr: *const WinitUIWindow = ptr.cast();
&*ptr
};
events.push(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::Destroyed,
}));
}
}
handle_nonuser_events(mtm, events);
let mut this = AppState::get_mut(mtm); let mut this = AppState::get_mut(mtm);
let mut handler = this.terminated_transition(); let mut handler = this.terminated_transition();
drop(this); drop(this);
@@ -747,7 +791,11 @@ pub(crate) fn terminated(application: &UIApplication) {
} }
fn handle_hidpi_proxy(handler: &mut EventLoopHandler, event: ScaleFactorChanged) { fn handle_hidpi_proxy(handler: &mut EventLoopHandler, event: ScaleFactorChanged) {
let ScaleFactorChanged { suggested_size, scale_factor, window } = event; let ScaleFactorChanged {
suggested_size,
scale_factor,
window,
} = event;
let new_inner_size = Arc::new(Mutex::new(suggested_size)); let new_inner_size = Arc::new(Mutex::new(suggested_size));
let event = Event::WindowEvent { let event = Event::WindowEvent {
window_id: RootWindowId(window.id()), window_id: RootWindowId(window.id()),
@@ -766,7 +814,7 @@ fn handle_hidpi_proxy(handler: &mut EventLoopHandler, event: ScaleFactorChanged)
view.setFrame(new_frame); view.setFrame(new_frame);
} }
fn get_view_and_screen_frame(window: &WinitUIWindow) -> (Retained<UIView>, CGRect) { fn get_view_and_screen_frame(window: &WinitUIWindow) -> (Id<UIView>, CGRect) {
let view_controller = window.rootViewController().unwrap(); let view_controller = window.rootViewController().unwrap();
let view = view_controller.view().unwrap(); let view = view_controller.view().unwrap();
let bounds = window.bounds(); let bounds = window.bounds();
@@ -798,7 +846,7 @@ impl EventLoopWaker {
// future, but that gets changed to fire immediately in did_finish_launching // future, but that gets changed to fire immediately in did_finish_launching
let timer = CFRunLoopTimerCreate( let timer = CFRunLoopTimerCreate(
ptr::null_mut(), ptr::null_mut(),
f64::MAX, std::f64::MAX,
0.000_000_1, 0.000_000_1,
0, 0,
0, 0,
@@ -812,11 +860,11 @@ impl EventLoopWaker {
} }
fn stop(&mut self) { fn stop(&mut self) {
unsafe { CFRunLoopTimerSetNextFireDate(self.timer, f64::MAX) } unsafe { CFRunLoopTimerSetNextFireDate(self.timer, std::f64::MAX) }
} }
fn start(&mut self) { fn start(&mut self) {
unsafe { CFRunLoopTimerSetNextFireDate(self.timer, f64::MIN) } unsafe { CFRunLoopTimerSetNextFireDate(self.timer, std::f64::MIN) }
} }
fn start_at(&mut self, instant: Instant) { fn start_at(&mut self, instant: Instant) {
@@ -902,21 +950,28 @@ fn meets_requirements(
} }
fn get_version() -> NSOperatingSystemVersion { fn get_version() -> NSOperatingSystemVersion {
let process_info = NSProcessInfo::processInfo(); unsafe {
let atleast_ios_8 = process_info.respondsToSelector(sel!(operatingSystemVersion)); let process_info = NSProcessInfo::processInfo();
// Winit requires atleast iOS 8 because no one has put the time into supporting earlier os let atleast_ios_8: bool = msg_send![
// versions. Older iOS versions are increasingly difficult to test. For example, Xcode 11 does &process_info,
// not support debugging on devices with an iOS version of less than 8. Another example, in respondsToSelector: sel!(operatingSystemVersion)
// order to use an iOS simulator older than iOS 8, you must download an older version of Xcode ];
// (<9), and at least Xcode 7 has been tested to not even run on macOS 10.15 - Xcode 8 might? // winit requires atleast iOS 8 because no one has put the time into supporting earlier os versions.
// // Older iOS versions are increasingly difficult to test. For example, Xcode 11 does not support
// The minimum required iOS version is likely to grow in the future. // debugging on devices with an iOS version of less than 8. Another example, in order to use an iOS
assert!(atleast_ios_8, "`winit` requires iOS version 8 or greater"); // simulator older than iOS 8, you must download an older version of Xcode (<9), and at least Xcode 7
process_info.operatingSystemVersion() // has been tested to not even run on macOS 10.15 - Xcode 8 might?
//
// The minimum required iOS version is likely to grow in the future.
assert!(atleast_ios_8, "`winit` requires iOS version 8 or greater");
process_info.operatingSystemVersion()
}
} }
pub fn os_capabilities() -> OSCapabilities { pub fn os_capabilities() -> OSCapabilities {
// Cache the version lookup for efficiency // Cache the version lookup for efficiency
static OS_CAPABILITIES: OnceLock<OSCapabilities> = OnceLock::new(); static OS_CAPABILITIES: OnceLock<OSCapabilities> = OnceLock::new();
OS_CAPABILITIES.get_or_init(|| OSCapabilities::from_os_version(get_version())).clone() OS_CAPABILITIES
.get_or_init(|| OSCapabilities::from_os_version(get_version()))
.clone()
} }

View File

@@ -1,8 +1,10 @@
use std::collections::VecDeque; use std::{
use std::ffi::{c_char, c_int, c_void}; collections::VecDeque,
use std::marker::PhantomData; ffi::c_void,
use std::ptr::{self, NonNull}; marker::PhantomData,
use std::sync::mpsc::{self, Receiver, Sender}; ptr,
sync::mpsc::{self, Receiver, Sender},
};
use core_foundation::base::{CFIndex, CFRelease}; use core_foundation::base::{CFIndex, CFRelease};
use core_foundation::runloop::{ use core_foundation::runloop::{
@@ -11,23 +13,26 @@ use core_foundation::runloop::{
CFRunLoopObserverCreate, CFRunLoopObserverRef, CFRunLoopSourceContext, CFRunLoopSourceCreate, CFRunLoopObserverCreate, CFRunLoopObserverRef, CFRunLoopSourceContext, CFRunLoopSourceCreate,
CFRunLoopSourceInvalidate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp, CFRunLoopSourceInvalidate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp,
}; };
use objc2::rc::Retained; use icrate::Foundation::{MainThreadMarker, NSString};
use objc2::{msg_send_id, ClassType}; use objc2::ClassType;
use objc2_foundation::{MainThreadMarker, NSString};
use objc2_ui_kit::{UIApplication, UIApplicationMain, UIDevice, UIScreen, UIUserInterfaceIdiom};
use crate::error::EventLoopError; use crate::{
use crate::event::Event; error::EventLoopError,
use crate::event_loop::{ event::Event,
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopClosed, event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopClosed,
},
platform::ios::Idiom,
platform_impl::platform::app_state::{EventLoopHandler, HandlePendingUserEvents},
window::{CustomCursor, CustomCursorSource},
}; };
use crate::platform::ios::Idiom;
use crate::platform_impl::platform::app_state::{EventLoopHandler, HandlePendingUserEvents};
use crate::window::{CustomCursor, CustomCursorSource};
use super::app_delegate::AppDelegate; use super::{app_delegate::AppDelegate, uikit::UIUserInterfaceIdiom};
use super::app_state::AppState;
use super::{app_state, monitor, MonitorHandle}; use super::{app_state, monitor, MonitorHandle};
use super::{
app_state::AppState,
uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen},
};
#[derive(Debug)] #[derive(Debug)]
pub struct ActiveEventLoop { pub struct ActiveEventLoop {
@@ -37,7 +42,9 @@ pub struct ActiveEventLoop {
impl ActiveEventLoop { impl ActiveEventLoop {
pub fn create_custom_cursor(&self, source: CustomCursorSource) -> CustomCursor { pub fn create_custom_cursor(&self, source: CustomCursorSource) -> CustomCursor {
let _ = source.inner; let _ = source.inner;
CustomCursor { inner: super::PlatformCustomCursor } CustomCursor {
inner: super::PlatformCustomCursor,
}
} }
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> { pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
@@ -45,8 +52,7 @@ impl ActiveEventLoop {
} }
pub fn primary_monitor(&self) -> Option<MonitorHandle> { pub fn primary_monitor(&self) -> Option<MonitorHandle> {
#[allow(deprecated)] Some(MonitorHandle::new(UIScreen::main(self.mtm)))
Some(MonitorHandle::new(UIScreen::mainScreen(self.mtm)))
} }
#[inline] #[inline]
@@ -63,7 +69,9 @@ impl ActiveEventLoop {
pub fn raw_display_handle_rwh_06( pub fn raw_display_handle_rwh_06(
&self, &self,
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> { ) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
Ok(rwh_06::RawDisplayHandle::UiKit(rwh_06::UiKitDisplayHandle::new())) Ok(rwh_06::RawDisplayHandle::UiKit(
rwh_06::UiKitDisplayHandle::new(),
))
} }
pub(crate) fn set_control_flow(&self, control_flow: ControlFlow) { pub(crate) fn set_control_flow(&self, control_flow: ControlFlow) {
@@ -118,7 +126,7 @@ fn map_user_event<T: 'static>(
for event in receiver.try_iter() { for event in receiver.try_iter() {
(handler)(Event::UserEvent(event), window_target); (handler)(Event::UserEvent(event), window_target);
} }
}, }
} }
} }
@@ -143,7 +151,8 @@ impl<T: 'static> EventLoop<T> {
unsafe { unsafe {
assert!( assert!(
!SINGLETON_INIT, !SINGLETON_INIT,
"Only one `EventLoop` is supported on iOS. `EventLoopProxy` might be helpful" "Only one `EventLoop` is supported on iOS. \
`EventLoopProxy` might be helpful"
); );
SINGLETON_INIT = true; SINGLETON_INIT = true;
} }
@@ -157,7 +166,10 @@ impl<T: 'static> EventLoop<T> {
mtm, mtm,
sender, sender,
receiver, receiver,
window_target: RootActiveEventLoop { p: ActiveEventLoop { mtm }, _marker: PhantomData }, window_target: RootActiveEventLoop {
p: ActiveEventLoop { mtm },
_marker: PhantomData,
},
}) })
} }
@@ -165,13 +177,12 @@ impl<T: 'static> EventLoop<T> {
where where
F: FnMut(Event<T>, &RootActiveEventLoop), F: FnMut(Event<T>, &RootActiveEventLoop),
{ {
let application: Option<Retained<UIApplication>> = let application = UIApplication::shared(self.mtm);
unsafe { msg_send_id![UIApplication::class(), sharedApplication] };
assert!( assert!(
application.is_none(), application.is_none(),
"\ "\
`EventLoop` cannot be `run` after a call to `UIApplicationMain` on iOS\nNote: \ `EventLoop` cannot be `run` after a call to `UIApplicationMain` on iOS\n\
`EventLoop::run_app` calls `UIApplicationMain` on iOS", Note: `EventLoop::run_app` calls `UIApplicationMain` on iOS",
); );
let handler = map_user_event(handler, self.receiver); let handler = map_user_event(handler, self.receiver);
@@ -183,23 +194,20 @@ impl<T: 'static> EventLoop<T> {
>(Box::new(handler)) >(Box::new(handler))
}; };
let handler = EventLoopHandler { handler, event_loop: self.window_target }; let handler = EventLoopHandler {
handler,
event_loop: self.window_target,
};
app_state::will_launch(self.mtm, handler); app_state::will_launch(self.mtm, handler);
// Ensure application delegate is initialized // Ensure application delegate is initialized
let _ = AppDelegate::class(); let _ = AppDelegate::class();
extern "C" {
// These functions are in crt_externs.h.
fn _NSGetArgc() -> *mut c_int;
fn _NSGetArgv() -> *mut *mut *mut c_char;
}
unsafe { unsafe {
UIApplicationMain( UIApplicationMain(
*_NSGetArgc(), 0,
NonNull::new(*_NSGetArgv()).unwrap(), ptr::null(),
None, None,
Some(&NSString::from_str(AppDelegate::NAME)), Some(&NSString::from_str(AppDelegate::NAME)),
) )
@@ -219,7 +227,7 @@ impl<T: 'static> EventLoop<T> {
// EventLoopExtIOS // EventLoopExtIOS
impl<T: 'static> EventLoop<T> { impl<T: 'static> EventLoop<T> {
pub fn idiom(&self) -> Idiom { pub fn idiom(&self) -> Idiom {
match UIDevice::currentDevice(self.mtm).userInterfaceIdiom() { match UIDevice::current(self.mtm).userInterfaceIdiom() {
UIUserInterfaceIdiom::Unspecified => Idiom::Unspecified, UIUserInterfaceIdiom::Unspecified => Idiom::Unspecified,
UIUserInterfaceIdiom::Phone => Idiom::Phone, UIUserInterfaceIdiom::Phone => Idiom::Phone,
UIUserInterfaceIdiom::Pad => Idiom::Pad, UIUserInterfaceIdiom::Pad => Idiom::Pad,
@@ -274,7 +282,8 @@ impl<T> EventLoopProxy<T> {
cancel: None, cancel: None,
perform: event_loop_proxy_handler, perform: event_loop_proxy_handler,
}; };
let source = CFRunLoopSourceCreate(ptr::null_mut(), CFIndex::MAX - 1, &mut context); let source =
CFRunLoopSourceCreate(ptr::null_mut(), CFIndex::max_value() - 1, &mut context);
CFRunLoopAddSource(rl, source, kCFRunLoopCommonModes); CFRunLoopAddSource(rl, source, kCFRunLoopCommonModes);
CFRunLoopWakeUp(rl); CFRunLoopWakeUp(rl);
@@ -283,7 +292,9 @@ impl<T> EventLoopProxy<T> {
} }
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> { pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.sender.send(event).map_err(|::std::sync::mpsc::SendError(x)| EventLoopClosed(x))?; self.sender
.send(event)
.map_err(|::std::sync::mpsc::SendError(x)| EventLoopClosed(x))?;
unsafe { unsafe {
// let the main thread know there's a new event // let the main thread know there's a new event
CFRunLoopSourceSignal(self.source); CFRunLoopSourceSignal(self.source);
@@ -296,8 +307,7 @@ impl<T> EventLoopProxy<T> {
fn setup_control_flow_observers() { fn setup_control_flow_observers() {
unsafe { unsafe {
// begin is queued with the highest priority to ensure it is processed before other // begin is queued with the highest priority to ensure it is processed before other observers
// observers
extern "C" fn control_flow_begin_handler( extern "C" fn control_flow_begin_handler(
_: CFRunLoopObserverRef, _: CFRunLoopObserverRef,
activity: CFRunLoopActivity, activity: CFRunLoopActivity,
@@ -331,7 +341,7 @@ fn setup_control_flow_observers() {
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
match activity { match activity {
kCFRunLoopBeforeWaiting => app_state::handle_main_events_cleared(mtm), kCFRunLoopBeforeWaiting => app_state::handle_main_events_cleared(mtm),
kCFRunLoopExit => {}, // may happen when running on macOS kCFRunLoopExit => {} // may happen when running on macOS
_ => unreachable!(), _ => unreachable!(),
} }
} }
@@ -346,7 +356,7 @@ fn setup_control_flow_observers() {
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
match activity { match activity {
kCFRunLoopBeforeWaiting => app_state::handle_events_cleared(mtm), kCFRunLoopBeforeWaiting => app_state::handle_events_cleared(mtm),
kCFRunLoopExit => {}, // may happen when running on macOS kCFRunLoopExit => {} // may happen when running on macOS
_ => unreachable!(), _ => unreachable!(),
} }
} }
@@ -357,7 +367,7 @@ fn setup_control_flow_observers() {
ptr::null_mut(), ptr::null_mut(),
kCFRunLoopAfterWaiting, kCFRunLoopAfterWaiting,
1, // repeat = true 1, // repeat = true
CFIndex::MIN, CFIndex::min_value(),
control_flow_begin_handler, control_flow_begin_handler,
ptr::null_mut(), ptr::null_mut(),
); );
@@ -377,7 +387,7 @@ fn setup_control_flow_observers() {
ptr::null_mut(), ptr::null_mut(),
kCFRunLoopExit | kCFRunLoopBeforeWaiting, kCFRunLoopExit | kCFRunLoopBeforeWaiting,
1, // repeat = true 1, // repeat = true
CFIndex::MAX, CFIndex::max_value(),
control_flow_end_handler, control_flow_end_handler,
ptr::null_mut(), ptr::null_mut(),
); );

View File

@@ -1,9 +1,11 @@
#![cfg(ios_platform)]
#![allow(clippy::let_unit_value)] #![allow(clippy::let_unit_value)]
mod app_delegate; mod app_delegate;
mod app_state; mod app_state;
mod event_loop; mod event_loop;
mod monitor; mod monitor;
mod uikit;
mod view; mod view;
mod view_controller; mod view_controller;
mod window; mod window;
@@ -12,15 +14,16 @@ use std::fmt;
use crate::event::DeviceId as RootDeviceId; use crate::event::DeviceId as RootDeviceId;
pub(crate) use self::event_loop::{ pub(crate) use self::{
ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle, event_loop::{
PlatformSpecificEventLoopAttributes, ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
}; PlatformSpecificEventLoopAttributes,
pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle}; },
pub(crate) use self::window::{PlatformSpecificWindowAttributes, Window, WindowId}; monitor::{MonitorHandle, VideoModeHandle},
pub(crate) use crate::cursor::{ window::{PlatformSpecificWindowAttributes, Window, WindowId},
NoCustomCursor as PlatformCustomCursor, NoCustomCursor as PlatformCustomCursorSource,
}; };
pub(crate) use crate::cursor::NoCustomCursor as PlatformCustomCursor;
pub(crate) use crate::cursor::NoCustomCursor as PlatformCustomCursorSource;
pub(crate) use crate::icon::NoIcon as PlatformIcon; pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen; pub(crate) use crate::platform_impl::Fullscreen;

View File

@@ -1,25 +1,31 @@
#![allow(clippy::unnecessary_cast)] #![allow(clippy::unnecessary_cast)]
use std::collections::{BTreeSet, VecDeque}; use std::{
use std::{fmt, hash, ptr}; collections::{BTreeSet, VecDeque},
fmt, hash, ptr,
};
use icrate::Foundation::{MainThreadBound, MainThreadMarker, NSInteger};
use objc2::mutability::IsRetainable; use objc2::mutability::IsRetainable;
use objc2::rc::Retained; use objc2::rc::Id;
use objc2::Message; use objc2::Message;
use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker, NSInteger};
use objc2_ui_kit::{UIScreen, UIScreenMode};
use crate::dpi::{PhysicalPosition, PhysicalSize}; use super::uikit::{UIScreen, UIScreenMode};
use crate::monitor::VideoModeHandle as RootVideoModeHandle; use crate::{
use crate::platform_impl::platform::app_state; dpi::{PhysicalPosition, PhysicalSize},
monitor::VideoModeHandle as RootVideoModeHandle,
platform_impl::platform::app_state,
};
// Workaround for `MainThreadBound` implementing almost no traits // Workaround for `MainThreadBound` implementing almost no traits
#[derive(Debug)] #[derive(Debug)]
struct MainThreadBoundDelegateImpls<T>(MainThreadBound<Retained<T>>); struct MainThreadBoundDelegateImpls<T>(MainThreadBound<Id<T>>);
impl<T: IsRetainable + Message> Clone for MainThreadBoundDelegateImpls<T> { impl<T: IsRetainable + Message> Clone for MainThreadBoundDelegateImpls<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(run_on_main(|mtm| MainThreadBound::new(Retained::clone(self.0.get(mtm)), mtm))) Self(MainThreadMarker::run_on_main(|mtm| {
MainThreadBound::new(Id::clone(self.0.get(mtm)), mtm)
}))
} }
} }
@@ -27,7 +33,7 @@ impl<T: IsRetainable + Message> hash::Hash for MainThreadBoundDelegateImpls<T> {
fn hash<H: hash::Hasher>(&self, state: &mut H) { fn hash<H: hash::Hasher>(&self, state: &mut H) {
// SAFETY: Marker only used to get the pointer // SAFETY: Marker only used to get the pointer
let mtm = unsafe { MainThreadMarker::new_unchecked() }; let mtm = unsafe { MainThreadMarker::new_unchecked() };
Retained::as_ptr(self.0.get(mtm)).hash(state); Id::as_ptr(self.0.get(mtm)).hash(state);
} }
} }
@@ -35,7 +41,7 @@ impl<T: IsRetainable + Message> PartialEq for MainThreadBoundDelegateImpls<T> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
// SAFETY: Marker only used to get the pointer // SAFETY: Marker only used to get the pointer
let mtm = unsafe { MainThreadMarker::new_unchecked() }; let mtm = unsafe { MainThreadMarker::new_unchecked() };
Retained::as_ptr(self.0.get(mtm)) == Retained::as_ptr(other.0.get(mtm)) Id::as_ptr(self.0.get(mtm)) == Id::as_ptr(other.0.get(mtm))
} }
} }
@@ -52,8 +58,8 @@ pub struct VideoModeHandle {
impl VideoModeHandle { impl VideoModeHandle {
fn new( fn new(
uiscreen: Retained<UIScreen>, uiscreen: Id<UIScreen>,
screen_mode: Retained<UIScreenMode>, screen_mode: Id<UIScreenMode>,
mtm: MainThreadMarker, mtm: MainThreadMarker,
) -> VideoModeHandle { ) -> VideoModeHandle {
let refresh_rate_millihertz = refresh_rate_millihertz(&uiscreen); let refresh_rate_millihertz = refresh_rate_millihertz(&uiscreen);
@@ -83,18 +89,18 @@ impl VideoModeHandle {
self.monitor.clone() self.monitor.clone()
} }
pub(super) fn screen_mode(&self, mtm: MainThreadMarker) -> &Retained<UIScreenMode> { pub(super) fn screen_mode(&self, mtm: MainThreadMarker) -> &Id<UIScreenMode> {
self.screen_mode.0.get(mtm) self.screen_mode.0.get(mtm)
} }
} }
pub struct MonitorHandle { pub struct MonitorHandle {
ui_screen: MainThreadBound<Retained<UIScreen>>, ui_screen: MainThreadBound<Id<UIScreen>>,
} }
impl Clone for MonitorHandle { impl Clone for MonitorHandle {
fn clone(&self) -> Self { fn clone(&self) -> Self {
run_on_main(|mtm| Self { MainThreadMarker::run_on_main(|mtm| Self {
ui_screen: MainThreadBound::new(self.ui_screen.get(mtm).clone(), mtm), ui_screen: MainThreadBound::new(self.ui_screen.get(mtm).clone(), mtm),
}) })
} }
@@ -140,22 +146,22 @@ impl fmt::Debug for MonitorHandle {
} }
impl MonitorHandle { impl MonitorHandle {
pub(crate) fn new(ui_screen: Retained<UIScreen>) -> Self { pub(crate) fn new(ui_screen: Id<UIScreen>) -> Self {
// Holding `Retained<UIScreen>` implies we're on the main thread. // Holding `Id<UIScreen>` implies we're on the main thread.
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
Self { ui_screen: MainThreadBound::new(ui_screen, mtm) } Self {
ui_screen: MainThreadBound::new(ui_screen, mtm),
}
} }
pub fn name(&self) -> Option<String> { pub fn name(&self) -> Option<String> {
run_on_main(|mtm| { MainThreadMarker::run_on_main(|mtm| {
#[allow(deprecated)] let main = UIScreen::main(mtm);
let main = UIScreen::mainScreen(mtm);
if *self.ui_screen(mtm) == main { if *self.ui_screen(mtm) == main {
Some("Primary".to_string()) Some("Primary".to_string())
} else if Some(self.ui_screen(mtm)) == main.mirroredScreen().as_ref() { } else if *self.ui_screen(mtm) == main.mirroredScreen() {
Some("Mirrored".to_string()) Some("Mirrored".to_string())
} else { } else {
#[allow(deprecated)]
UIScreen::screens(mtm) UIScreen::screens(mtm)
.iter() .iter()
.position(|rhs| rhs == &**self.ui_screen(mtm)) .position(|rhs| rhs == &**self.ui_screen(mtm))
@@ -165,25 +171,33 @@ impl MonitorHandle {
} }
pub fn size(&self) -> PhysicalSize<u32> { pub fn size(&self) -> PhysicalSize<u32> {
let bounds = self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeBounds()); let bounds = self
.ui_screen
.get_on_main(|ui_screen| ui_screen.nativeBounds());
PhysicalSize::new(bounds.size.width as u32, bounds.size.height as u32) PhysicalSize::new(bounds.size.width as u32, bounds.size.height as u32)
} }
pub fn position(&self) -> PhysicalPosition<i32> { pub fn position(&self) -> PhysicalPosition<i32> {
let bounds = self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeBounds()); let bounds = self
.ui_screen
.get_on_main(|ui_screen| ui_screen.nativeBounds());
(bounds.origin.x as f64, bounds.origin.y as f64).into() (bounds.origin.x as f64, bounds.origin.y as f64).into()
} }
pub fn scale_factor(&self) -> f64 { pub fn scale_factor(&self) -> f64 {
self.ui_screen.get_on_main(|ui_screen| ui_screen.nativeScale()) as f64 self.ui_screen
.get_on_main(|ui_screen| ui_screen.nativeScale()) as f64
} }
pub fn refresh_rate_millihertz(&self) -> Option<u32> { pub fn refresh_rate_millihertz(&self) -> Option<u32> {
Some(self.ui_screen.get_on_main(|ui_screen| refresh_rate_millihertz(ui_screen))) Some(
self.ui_screen
.get_on_main(|ui_screen| refresh_rate_millihertz(ui_screen)),
)
} }
pub fn video_modes(&self) -> impl Iterator<Item = VideoModeHandle> { pub fn video_modes(&self) -> impl Iterator<Item = VideoModeHandle> {
run_on_main(|mtm| { MainThreadMarker::run_on_main(|mtm| {
let ui_screen = self.ui_screen(mtm); let ui_screen = self.ui_screen(mtm);
// Use Ord impl of RootVideoModeHandle // Use Ord impl of RootVideoModeHandle
@@ -199,12 +213,12 @@ impl MonitorHandle {
}) })
} }
pub(crate) fn ui_screen(&self, mtm: MainThreadMarker) -> &Retained<UIScreen> { pub(crate) fn ui_screen(&self, mtm: MainThreadMarker) -> &Id<UIScreen> {
self.ui_screen.get(mtm) self.ui_screen.get(mtm)
} }
pub fn preferred_video_mode(&self) -> VideoModeHandle { pub fn preferred_video_mode(&self) -> VideoModeHandle {
run_on_main(|mtm| { MainThreadMarker::run_on_main(|mtm| {
VideoModeHandle::new( VideoModeHandle::new(
self.ui_screen(mtm).clone(), self.ui_screen(mtm).clone(),
self.ui_screen(mtm).preferredMode().unwrap(), self.ui_screen(mtm).preferredMode().unwrap(),
@@ -239,6 +253,8 @@ fn refresh_rate_millihertz(uiscreen: &UIScreen) -> u32 {
} }
pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque<MonitorHandle> { pub fn uiscreens(mtm: MainThreadMarker) -> VecDeque<MonitorHandle> {
#[allow(deprecated)] UIScreen::screens(mtm)
UIScreen::screens(mtm).into_iter().map(MonitorHandle::new).collect() .into_iter()
.map(MonitorHandle::new)
.collect()
} }

View File

@@ -0,0 +1,31 @@
use icrate::Foundation::{CGRect, MainThreadMarker, NSArray, NSObject};
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
use super::{UIResponder, UIWindow};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIApplication;
unsafe impl ClassType for UIApplication {
#[inherits(NSObject)]
type Super = UIResponder;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIApplication {
pub fn shared(_mtm: MainThreadMarker) -> Option<Id<Self>> {
unsafe { msg_send_id![Self::class(), sharedApplication] }
}
pub fn windows(&self) -> Id<NSArray<UIWindow>> {
unsafe { msg_send_id![self, windows] }
}
#[method(statusBarFrame)]
pub fn statusBarFrame(&self) -> CGRect;
}
);

View File

@@ -0,0 +1,12 @@
use icrate::Foundation::NSObject;
use objc2::{extern_class, mutability, ClassType};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UICoordinateSpace;
unsafe impl ClassType for UICoordinateSpace {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);

View File

@@ -0,0 +1,41 @@
use icrate::Foundation::{MainThreadMarker, NSInteger, NSObject};
use objc2::encode::{Encode, Encoding};
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIDevice;
unsafe impl ClassType for UIDevice {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIDevice {
pub fn current(_mtm: MainThreadMarker) -> Id<Self> {
unsafe { msg_send_id![Self::class(), currentDevice] }
}
#[method(userInterfaceIdiom)]
pub fn userInterfaceIdiom(&self) -> UIUserInterfaceIdiom;
}
);
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIUserInterfaceIdiom(NSInteger);
unsafe impl Encode for UIUserInterfaceIdiom {
const ENCODING: Encoding = NSInteger::ENCODING;
}
impl UIUserInterfaceIdiom {
pub const Unspecified: UIUserInterfaceIdiom = UIUserInterfaceIdiom(-1);
pub const Phone: UIUserInterfaceIdiom = UIUserInterfaceIdiom(0);
pub const Pad: UIUserInterfaceIdiom = UIUserInterfaceIdiom(1);
pub const TV: UIUserInterfaceIdiom = UIUserInterfaceIdiom(2);
pub const CarPlay: UIUserInterfaceIdiom = UIUserInterfaceIdiom(3);
}

View File

@@ -0,0 +1,12 @@
use icrate::Foundation::NSObject;
use objc2::{extern_class, mutability, ClassType};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIEvent;
unsafe impl ClassType for UIEvent {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);

View File

@@ -0,0 +1,14 @@
use icrate::Foundation::NSUInteger;
use objc2::encode::{Encode, Encoding};
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIRectEdge(pub NSUInteger);
impl UIRectEdge {
pub const NONE: Self = Self(0);
}
unsafe impl Encode for UIRectEdge {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

View File

@@ -0,0 +1,121 @@
use icrate::Foundation::{CGFloat, NSInteger, NSObject, NSUInteger};
use objc2::{
encode::{Encode, Encoding},
extern_class, extern_methods, mutability, ClassType,
};
// https://developer.apple.com/documentation/uikit/uigesturerecognizer
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIGestureRecognizer;
unsafe impl ClassType for UIGestureRecognizer {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIGestureRecognizer {
#[method(state)]
pub fn state(&self) -> UIGestureRecognizerState;
}
);
unsafe impl Encode for UIGestureRecognizer {
const ENCODING: Encoding = Encoding::Object;
}
// https://developer.apple.com/documentation/uikit/uigesturerecognizer/state
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIGestureRecognizerState(NSInteger);
unsafe impl Encode for UIGestureRecognizerState {
const ENCODING: Encoding = NSInteger::ENCODING;
}
#[allow(dead_code)]
impl UIGestureRecognizerState {
pub const Possible: Self = Self(0);
pub const Began: Self = Self(1);
pub const Changed: Self = Self(2);
pub const Ended: Self = Self(3);
pub const Cancelled: Self = Self(4);
pub const Failed: Self = Self(5);
}
// https://developer.apple.com/documentation/uikit/uipinchgesturerecognizer
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIPinchGestureRecognizer;
unsafe impl ClassType for UIPinchGestureRecognizer {
type Super = UIGestureRecognizer;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIPinchGestureRecognizer {
#[method(scale)]
pub fn scale(&self) -> CGFloat;
#[method(velocity)]
pub fn velocity(&self) -> CGFloat;
}
);
unsafe impl Encode for UIPinchGestureRecognizer {
const ENCODING: Encoding = Encoding::Object;
}
// https://developer.apple.com/documentation/uikit/uirotationgesturerecognizer
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIRotationGestureRecognizer;
unsafe impl ClassType for UIRotationGestureRecognizer {
type Super = UIGestureRecognizer;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIRotationGestureRecognizer {
#[method(rotation)]
pub fn rotation(&self) -> CGFloat;
#[method(velocity)]
pub fn velocity(&self) -> CGFloat;
}
);
unsafe impl Encode for UIRotationGestureRecognizer {
const ENCODING: Encoding = Encoding::Object;
}
// https://developer.apple.com/documentation/uikit/uitapgesturerecognizer
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UITapGestureRecognizer;
unsafe impl ClassType for UITapGestureRecognizer {
type Super = UIGestureRecognizer;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UITapGestureRecognizer {
#[method(setNumberOfTapsRequired:)]
pub fn setNumberOfTapsRequired(&self, number_of_taps_required: NSUInteger);
#[method(setNumberOfTouchesRequired:)]
pub fn setNumberOfTouchesRequired(&self, number_of_touches_required: NSUInteger);
}
);
unsafe impl Encode for UITapGestureRecognizer {
const ENCODING: Encoding = Encoding::Object;
}

View File

@@ -0,0 +1,52 @@
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
use std::os::raw::{c_char, c_int};
use icrate::Foundation::NSString;
mod application;
mod coordinate_space;
mod device;
mod event;
mod geometry;
mod gesture_recognizer;
mod responder;
mod screen;
mod screen_mode;
mod status_bar_style;
mod touch;
mod trait_collection;
mod view;
mod view_controller;
mod window;
pub(crate) use self::application::UIApplication;
pub(crate) use self::coordinate_space::UICoordinateSpace;
pub(crate) use self::device::{UIDevice, UIUserInterfaceIdiom};
pub(crate) use self::event::UIEvent;
pub(crate) use self::geometry::UIRectEdge;
pub(crate) use self::gesture_recognizer::{
UIGestureRecognizer, UIGestureRecognizerState, UIPinchGestureRecognizer,
UIRotationGestureRecognizer, UITapGestureRecognizer,
};
pub(crate) use self::responder::UIResponder;
pub(crate) use self::screen::{UIScreen, UIScreenOverscanCompensation};
pub(crate) use self::screen_mode::UIScreenMode;
pub(crate) use self::status_bar_style::UIStatusBarStyle;
pub(crate) use self::touch::{UITouch, UITouchPhase, UITouchType};
pub(crate) use self::trait_collection::{UIForceTouchCapability, UITraitCollection};
#[allow(unused_imports)]
pub(crate) use self::view::{UIEdgeInsets, UIView};
pub(crate) use self::view_controller::{UIInterfaceOrientationMask, UIViewController};
pub(crate) use self::window::UIWindow;
#[link(name = "UIKit", kind = "framework")]
extern "C" {
pub fn UIApplicationMain(
argc: c_int,
argv: *const c_char,
principalClassName: Option<&NSString>,
delegateClassName: Option<&NSString>,
) -> c_int;
}

View File

@@ -0,0 +1,12 @@
use icrate::Foundation::NSObject;
use objc2::{extern_class, mutability, ClassType};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIResponder;
unsafe impl ClassType for UIResponder {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);

View File

@@ -0,0 +1,80 @@
use icrate::Foundation::{CGFloat, CGRect, MainThreadMarker, NSArray, NSInteger, NSObject};
use objc2::encode::{Encode, Encoding};
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
use super::{UICoordinateSpace, UIScreenMode};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIScreen;
unsafe impl ClassType for UIScreen {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIScreen {
pub fn main(_mtm: MainThreadMarker) -> Id<Self> {
unsafe { msg_send_id![Self::class(), mainScreen] }
}
pub fn screens(_mtm: MainThreadMarker) -> Id<NSArray<Self>> {
unsafe { msg_send_id![Self::class(), screens] }
}
#[method(bounds)]
pub fn bounds(&self) -> CGRect;
#[method(scale)]
pub fn scale(&self) -> CGFloat;
#[method(nativeBounds)]
pub fn nativeBounds(&self) -> CGRect;
#[method(nativeScale)]
pub fn nativeScale(&self) -> CGFloat;
#[method(maximumFramesPerSecond)]
pub fn maximumFramesPerSecond(&self) -> NSInteger;
pub fn mirroredScreen(&self) -> Id<Self> {
unsafe { msg_send_id![Self::class(), mirroredScreen] }
}
pub fn preferredMode(&self) -> Option<Id<UIScreenMode>> {
unsafe { msg_send_id![self, preferredMode] }
}
#[method(setCurrentMode:)]
pub fn setCurrentMode(&self, mode: Option<&UIScreenMode>);
pub fn availableModes(&self) -> Id<NSArray<UIScreenMode>> {
unsafe { msg_send_id![self, availableModes] }
}
#[method(setOverscanCompensation:)]
pub fn setOverscanCompensation(&self, overscanCompensation: UIScreenOverscanCompensation);
pub fn coordinateSpace(&self) -> Id<UICoordinateSpace> {
unsafe { msg_send_id![self, coordinateSpace] }
}
}
);
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIScreenOverscanCompensation(NSInteger);
unsafe impl Encode for UIScreenOverscanCompensation {
const ENCODING: Encoding = NSInteger::ENCODING;
}
#[allow(dead_code)]
impl UIScreenOverscanCompensation {
pub const Scale: Self = Self(0);
pub const InsetBounds: Self = Self(1);
pub const None: Self = Self(2);
}

View File

@@ -0,0 +1,19 @@
use icrate::Foundation::{CGSize, NSObject};
use objc2::{extern_class, extern_methods, mutability, ClassType};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIScreenMode;
unsafe impl ClassType for UIScreenMode {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIScreenMode {
#[method(size)]
pub fn size(&self) -> CGSize;
}
);

View File

@@ -0,0 +1,16 @@
use icrate::Foundation::NSInteger;
use objc2::encode::{Encode, Encoding};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UIStatusBarStyle {
#[default]
Default = 0,
LightContent = 1,
DarkContent = 3,
}
unsafe impl Encode for UIStatusBarStyle {
const ENCODING: Encoding = NSInteger::ENCODING;
}

View File

@@ -0,0 +1,65 @@
use icrate::Foundation::{CGFloat, CGPoint, NSInteger, NSObject};
use objc2::encode::{Encode, Encoding};
use objc2::{extern_class, extern_methods, mutability, ClassType};
use super::UIView;
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UITouch;
unsafe impl ClassType for UITouch {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UITouch {
#[method(locationInView:)]
pub fn locationInView(&self, view: Option<&UIView>) -> CGPoint;
#[method(type)]
pub fn type_(&self) -> UITouchType;
#[method(force)]
pub fn force(&self) -> CGFloat;
#[method(maximumPossibleForce)]
pub fn maximumPossibleForce(&self) -> CGFloat;
#[method(altitudeAngle)]
pub fn altitudeAngle(&self) -> CGFloat;
#[method(phase)]
pub fn phase(&self) -> UITouchPhase;
}
);
#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UITouchType {
Direct = 0,
Indirect,
Pencil,
}
unsafe impl Encode for UITouchType {
const ENCODING: Encoding = NSInteger::ENCODING;
}
#[derive(Debug)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UITouchPhase {
Began = 0,
Moved,
Stationary,
Ended,
Cancelled,
}
unsafe impl Encode for UITouchPhase {
const ENCODING: Encoding = NSInteger::ENCODING;
}

View File

@@ -0,0 +1,33 @@
use icrate::Foundation::{NSInteger, NSObject};
use objc2::encode::{Encode, Encoding};
use objc2::{extern_class, extern_methods, mutability, ClassType};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UITraitCollection;
unsafe impl ClassType for UITraitCollection {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UITraitCollection {
#[method(forceTouchCapability)]
pub fn forceTouchCapability(&self) -> UIForceTouchCapability;
}
);
#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UIForceTouchCapability {
Unknown = 0,
Unavailable,
Available,
}
unsafe impl Encode for UIForceTouchCapability {
const ENCODING: Encoding = NSInteger::ENCODING;
}

View File

@@ -0,0 +1,96 @@
use icrate::Foundation::{CGFloat, CGRect, NSObject};
use objc2::encode::{Encode, Encoding};
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
use super::{UICoordinateSpace, UIGestureRecognizer, UIResponder, UIViewController};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIView;
unsafe impl ClassType for UIView {
#[inherits(NSObject)]
type Super = UIResponder;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIView {
#[method(bounds)]
pub fn bounds(&self) -> CGRect;
#[method(setBounds:)]
pub fn setBounds(&self, value: CGRect);
#[method(frame)]
pub fn frame(&self) -> CGRect;
#[method(setFrame:)]
pub fn setFrame(&self, value: CGRect);
#[method(contentScaleFactor)]
pub fn contentScaleFactor(&self) -> CGFloat;
#[method(setContentScaleFactor:)]
pub fn setContentScaleFactor(&self, val: CGFloat);
#[method(setMultipleTouchEnabled:)]
pub fn setMultipleTouchEnabled(&self, val: bool);
pub fn rootViewController(&self) -> Option<Id<UIViewController>> {
unsafe { msg_send_id![self, rootViewController] }
}
#[method(setRootViewController:)]
pub fn setRootViewController(&self, rootViewController: Option<&UIViewController>);
#[method(convertRect:toCoordinateSpace:)]
pub fn convertRect_toCoordinateSpace(
&self,
rect: CGRect,
coordinateSpace: &UICoordinateSpace,
) -> CGRect;
#[method(convertRect:fromCoordinateSpace:)]
pub fn convertRect_fromCoordinateSpace(
&self,
rect: CGRect,
coordinateSpace: &UICoordinateSpace,
) -> CGRect;
#[method(safeAreaInsets)]
pub fn safeAreaInsets(&self) -> UIEdgeInsets;
#[method(setNeedsDisplay)]
pub fn setNeedsDisplay(&self);
#[method(addGestureRecognizer:)]
pub fn addGestureRecognizer(&self, gestureRecognizer: &UIGestureRecognizer);
#[method(removeGestureRecognizer:)]
pub fn removeGestureRecognizer(&self, gestureRecognizer: &UIGestureRecognizer);
}
);
#[repr(C)]
#[derive(Debug, Clone)]
pub struct UIEdgeInsets {
pub top: CGFloat,
pub left: CGFloat,
pub bottom: CGFloat,
pub right: CGFloat,
}
unsafe impl Encode for UIEdgeInsets {
const ENCODING: Encoding = Encoding::Struct(
"UIEdgeInsets",
&[
CGFloat::ENCODING,
CGFloat::ENCODING,
CGFloat::ENCODING,
CGFloat::ENCODING,
],
);
}

View File

@@ -0,0 +1,57 @@
use icrate::Foundation::{NSObject, NSUInteger};
use objc2::encode::{Encode, Encoding};
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
use super::{UIResponder, UIView};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIViewController;
unsafe impl ClassType for UIViewController {
#[inherits(NSObject)]
type Super = UIResponder;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIViewController {
#[method(attemptRotationToDeviceOrientation)]
pub fn attemptRotationToDeviceOrientation();
#[method(setNeedsStatusBarAppearanceUpdate)]
pub fn setNeedsStatusBarAppearanceUpdate(&self);
#[method(setNeedsUpdateOfHomeIndicatorAutoHidden)]
pub fn setNeedsUpdateOfHomeIndicatorAutoHidden(&self);
#[method(setNeedsUpdateOfScreenEdgesDeferringSystemGestures)]
pub fn setNeedsUpdateOfScreenEdgesDeferringSystemGestures(&self);
pub fn view(&self) -> Option<Id<UIView>> {
unsafe { msg_send_id![self, view] }
}
#[method(setView:)]
pub fn setView(&self, view: Option<&UIView>);
}
);
bitflags::bitflags! {
#[derive(Clone, Copy)]
pub struct UIInterfaceOrientationMask: NSUInteger {
const Portrait = 1 << 1;
const PortraitUpsideDown = 1 << 2;
const LandscapeRight = 1 << 3;
const LandscapeLeft = 1 << 4;
const Landscape = Self::LandscapeLeft.bits() | Self::LandscapeRight.bits();
const AllButUpsideDown = Self::Landscape.bits() | Self::Portrait.bits();
const All = Self::AllButUpsideDown.bits() | Self::PortraitUpsideDown.bits();
}
}
unsafe impl Encode for UIInterfaceOrientationMask {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

View File

@@ -0,0 +1,36 @@
use icrate::Foundation::NSObject;
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
use super::{UIResponder, UIScreen, UIView};
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIWindow;
unsafe impl ClassType for UIWindow {
#[inherits(UIResponder, NSObject)]
type Super = UIView;
type Mutability = mutability::InteriorMutable;
}
);
extern_methods!(
unsafe impl UIWindow {
pub fn screen(&self) -> Id<UIScreen> {
unsafe { msg_send_id![self, screen] }
}
#[method(setScreen:)]
pub fn setScreen(&self, screen: &UIScreen);
#[method(setHidden:)]
pub fn setHidden(&self, flag: bool);
#[method(makeKeyAndVisible)]
pub fn makeKeyAndVisible(&self);
#[method(isKeyWindow)]
pub fn isKeyWindow(&self) -> bool;
}
);

View File

@@ -1,34 +1,31 @@
#![allow(clippy::unnecessary_cast)] #![allow(clippy::unnecessary_cast)]
use std::cell::{Cell, RefCell}; use std::cell::RefCell;
use objc2::rc::Retained; use icrate::Foundation::{CGFloat, CGRect, MainThreadMarker, NSObject, NSSet};
use objc2::runtime::{NSObjectProtocol, ProtocolObject}; use objc2::rc::Id;
use objc2::{declare_class, msg_send, msg_send_id, mutability, sel, ClassType, DeclaredClass}; use objc2::runtime::AnyClass;
use objc2_foundation::{CGFloat, CGPoint, CGRect, MainThreadMarker, NSObject, NSSet}; use objc2::{
use objc2_ui_kit::{ declare_class, extern_methods, msg_send, msg_send_id, mutability, sel, ClassType, DeclaredClass,
UICoordinateSpace, UIEvent, UIForceTouchCapability, UIGestureRecognizer,
UIGestureRecognizerDelegate, UIGestureRecognizerState, UIPanGestureRecognizer,
UIPinchGestureRecognizer, UIResponder, UIRotationGestureRecognizer, UITapGestureRecognizer,
UITouch, UITouchPhase, UITouchType, UITraitEnvironment, UIView,
}; };
use super::app_state::{self, EventWrapper}; use super::app_state::{self, EventWrapper};
use super::uikit::{
UIEvent, UIForceTouchCapability, UIGestureRecognizerState, UIPinchGestureRecognizer,
UIResponder, UIRotationGestureRecognizer, UITapGestureRecognizer, UITouch, UITouchPhase,
UITouchType, UITraitCollection, UIView,
};
use super::window::WinitUIWindow; use super::window::WinitUIWindow;
use crate::dpi::PhysicalPosition; use crate::{
use crate::event::{Event, Force, Touch, TouchPhase, WindowEvent}; dpi::PhysicalPosition,
use crate::platform_impl::platform::DEVICE_ID; event::{Event, Force, Touch, TouchPhase, WindowEvent},
use crate::window::{WindowAttributes, WindowId as RootWindowId}; platform_impl::platform::DEVICE_ID,
window::{WindowAttributes, WindowId as RootWindowId},
};
pub struct WinitViewState { pub struct WinitViewState {
pinch_gesture_recognizer: RefCell<Option<Retained<UIPinchGestureRecognizer>>>, pinch_gesture_recognizer: RefCell<Option<Id<UIPinchGestureRecognizer>>>,
doubletap_gesture_recognizer: RefCell<Option<Retained<UITapGestureRecognizer>>>, doubletap_gesture_recognizer: RefCell<Option<Id<UITapGestureRecognizer>>>,
rotation_gesture_recognizer: RefCell<Option<Retained<UIRotationGestureRecognizer>>>, rotation_gesture_recognizer: RefCell<Option<Id<UIRotationGestureRecognizer>>>,
pan_gesture_recognizer: RefCell<Option<Retained<UIPanGestureRecognizer>>>,
// for iOS delta references the start of the Gesture
rotation_last_delta: Cell<CGFloat>,
pinch_last_delta: Cell<CGFloat>,
pan_last_delta: Cell<CGPoint>,
} }
declare_class!( declare_class!(
@@ -37,7 +34,7 @@ declare_class!(
unsafe impl ClassType for WinitView { unsafe impl ClassType for WinitView {
#[inherits(UIResponder, NSObject)] #[inherits(UIResponder, NSObject)]
type Super = UIView; type Super = UIView;
type Mutability = mutability::MainThreadOnly; type Mutability = mutability::InteriorMutable;
const NAME: &'static str = "WinitUIView"; const NAME: &'static str = "WinitUIView";
} }
@@ -170,23 +167,12 @@ declare_class!(
fn pinch_gesture(&self, recognizer: &UIPinchGestureRecognizer) { fn pinch_gesture(&self, recognizer: &UIPinchGestureRecognizer) {
let window = self.window().unwrap(); let window = self.window().unwrap();
let (phase, delta) = match recognizer.state() { let phase = match recognizer.state() {
UIGestureRecognizerState::Began => { UIGestureRecognizerState::Began => TouchPhase::Started,
self.ivars().pinch_last_delta.set(recognizer.scale()); UIGestureRecognizerState::Changed => TouchPhase::Moved,
(TouchPhase::Started, 0.0) UIGestureRecognizerState::Ended => TouchPhase::Ended,
}
UIGestureRecognizerState::Changed => {
let last_scale: f64 = self.ivars().pinch_last_delta.replace(recognizer.scale());
(TouchPhase::Moved, recognizer.scale() - last_scale)
}
UIGestureRecognizerState::Ended => {
let last_scale: f64 = self.ivars().pinch_last_delta.replace(0.0);
(TouchPhase::Moved, recognizer.scale() - last_scale)
}
UIGestureRecognizerState::Cancelled | UIGestureRecognizerState::Failed => { UIGestureRecognizerState::Cancelled | UIGestureRecognizerState::Failed => {
self.ivars().rotation_last_delta.set(0.0); TouchPhase::Cancelled
// Pass -delta so that action is reversed
(TouchPhase::Cancelled, -recognizer.scale())
} }
state => panic!("unexpected recognizer state: {:?}", state), state => panic!("unexpected recognizer state: {:?}", state),
}; };
@@ -195,7 +181,7 @@ declare_class!(
window_id: RootWindowId(window.id()), window_id: RootWindowId(window.id()),
event: WindowEvent::PinchGesture { event: WindowEvent::PinchGesture {
device_id: DEVICE_ID, device_id: DEVICE_ID,
delta: delta as f64, delta: recognizer.velocity() as _,
phase, phase,
}, },
}); });
@@ -225,88 +211,23 @@ declare_class!(
fn rotation_gesture(&self, recognizer: &UIRotationGestureRecognizer) { fn rotation_gesture(&self, recognizer: &UIRotationGestureRecognizer) {
let window = self.window().unwrap(); let window = self.window().unwrap();
let (phase, delta) = match recognizer.state() { let phase = match recognizer.state() {
UIGestureRecognizerState::Began => { UIGestureRecognizerState::Began => TouchPhase::Started,
self.ivars().rotation_last_delta.set(0.0); UIGestureRecognizerState::Changed => TouchPhase::Moved,
UIGestureRecognizerState::Ended => TouchPhase::Ended,
(TouchPhase::Started, 0.0)
}
UIGestureRecognizerState::Changed => {
let last_rotation = self.ivars().rotation_last_delta.replace(recognizer.rotation());
(TouchPhase::Moved, recognizer.rotation() - last_rotation)
}
UIGestureRecognizerState::Ended => {
let last_rotation = self.ivars().rotation_last_delta.replace(0.0);
(TouchPhase::Ended, recognizer.rotation() - last_rotation)
}
UIGestureRecognizerState::Cancelled | UIGestureRecognizerState::Failed => { UIGestureRecognizerState::Cancelled | UIGestureRecognizerState::Failed => {
self.ivars().rotation_last_delta.set(0.0); TouchPhase::Cancelled
// Pass -delta so that action is reversed
(TouchPhase::Cancelled, -recognizer.rotation())
} }
state => panic!("unexpected recognizer state: {:?}", state), state => panic!("unexpected recognizer state: {:?}", state),
}; };
// Make delta negative to match macos, convert to degrees // Flip the velocity to match macOS.
let delta = -recognizer.velocity() as _;
let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent { let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()), window_id: RootWindowId(window.id()),
event: WindowEvent::RotationGesture { event: WindowEvent::RotationGesture {
device_id: DEVICE_ID, device_id: DEVICE_ID,
delta: -delta.to_degrees() as _, delta,
phase,
},
});
let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(mtm, gesture_event);
}
#[method(panGesture:)]
fn pan_gesture(&self, recognizer: &UIPanGestureRecognizer) {
let window = self.window().unwrap();
let translation = recognizer.translationInView(Some(self));
let (phase, dx, dy) = match recognizer.state() {
UIGestureRecognizerState::Began => {
self.ivars().pan_last_delta.set(translation);
(TouchPhase::Started, 0.0, 0.0)
}
UIGestureRecognizerState::Changed => {
let last_pan: CGPoint = self.ivars().pan_last_delta.replace(translation);
let dx = translation.x - last_pan.x;
let dy = translation.y - last_pan.y;
(TouchPhase::Moved, dx, dy)
}
UIGestureRecognizerState::Ended => {
let last_pan: CGPoint = self.ivars().pan_last_delta.replace(CGPoint{x:0.0, y:0.0});
let dx = translation.x - last_pan.x;
let dy = translation.y - last_pan.y;
(TouchPhase::Ended, dx, dy)
}
UIGestureRecognizerState::Cancelled | UIGestureRecognizerState::Failed => {
let last_pan: CGPoint = self.ivars().pan_last_delta.replace(CGPoint{x:0.0, y:0.0});
// Pass -delta so that action is reversed
(TouchPhase::Cancelled, -last_pan.x, -last_pan.y)
}
state => panic!("unexpected recognizer state: {:?}", state),
};
let gesture_event = EventWrapper::StaticEvent(Event::WindowEvent {
window_id: RootWindowId(window.id()),
event: WindowEvent::PanGesture {
device_id: DEVICE_ID,
delta: PhysicalPosition::new(dx as _, dy as _),
phase, phase,
}, },
}); });
@@ -315,34 +236,37 @@ declare_class!(
app_state::handle_nonuser_event(mtm, gesture_event); app_state::handle_nonuser_event(mtm, gesture_event);
} }
} }
);
unsafe impl NSObjectProtocol for WinitView {} extern_methods!(
#[allow(non_snake_case)]
unsafe impl UIGestureRecognizerDelegate for WinitView { unsafe impl WinitView {
#[method(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)] fn window(&self) -> Option<Id<WinitUIWindow>> {
fn should_recognize_simultaneously(&self, _gesture_recognizer: &UIGestureRecognizer, _other_gesture_recognizer: &UIGestureRecognizer) -> bool { unsafe { msg_send_id![self, window] }
true
} }
unsafe fn traitCollection(&self) -> Id<UITraitCollection> {
msg_send_id![self, traitCollection]
}
// TODO: Allow the user to customize this
#[method(layerClass)]
pub(crate) fn layerClass() -> &'static AnyClass;
} }
); );
impl WinitView { impl WinitView {
pub(crate) fn new( pub(crate) fn new(
mtm: MainThreadMarker, _mtm: MainThreadMarker,
window_attributes: &WindowAttributes, window_attributes: &WindowAttributes,
frame: CGRect, frame: CGRect,
) -> Retained<Self> { ) -> Id<Self> {
let this = mtm.alloc().set_ivars(WinitViewState { let this = Self::alloc().set_ivars(WinitViewState {
pinch_gesture_recognizer: RefCell::new(None), pinch_gesture_recognizer: RefCell::new(None),
doubletap_gesture_recognizer: RefCell::new(None), doubletap_gesture_recognizer: RefCell::new(None),
rotation_gesture_recognizer: RefCell::new(None), rotation_gesture_recognizer: RefCell::new(None),
pan_gesture_recognizer: RefCell::new(None),
rotation_last_delta: Cell::new(0.0),
pinch_last_delta: Cell::new(0.0),
pan_last_delta: Cell::new(CGPoint { x: 0.0, y: 0.0 }),
}); });
let this: Retained<Self> = unsafe { msg_send_id![super(this), initWithFrame: frame] }; let this: Id<Self> = unsafe { msg_send_id![super(this), initWithFrame: frame] };
this.setMultipleTouchEnabled(true); this.setMultipleTouchEnabled(true);
@@ -353,23 +277,12 @@ impl WinitView {
this this
} }
fn window(&self) -> Option<Retained<WinitUIWindow>> {
// SAFETY: `WinitView`s are always installed in a `WinitUIWindow`
(**self).window().map(|window| unsafe { Retained::cast(window) })
}
pub(crate) fn recognize_pinch_gesture(&self, should_recognize: bool) { pub(crate) fn recognize_pinch_gesture(&self, should_recognize: bool) {
let mtm = MainThreadMarker::from(self);
if should_recognize { if should_recognize {
if self.ivars().pinch_gesture_recognizer.borrow().is_none() { if self.ivars().pinch_gesture_recognizer.borrow().is_none() {
let pinch = unsafe { let pinch: Id<UIPinchGestureRecognizer> = unsafe {
UIPinchGestureRecognizer::initWithTarget_action( msg_send_id![UIPinchGestureRecognizer::alloc(), initWithTarget: self, action: sel!(pinchGesture:)]
mtm.alloc(),
Some(self),
Some(sel!(pinchGesture:)),
)
}; };
pinch.setDelegate(Some(ProtocolObject::from_ref(self)));
self.addGestureRecognizer(&pinch); self.addGestureRecognizer(&pinch);
self.ivars().pinch_gesture_recognizer.replace(Some(pinch)); self.ivars().pinch_gesture_recognizer.replace(Some(pinch));
} }
@@ -378,45 +291,12 @@ impl WinitView {
} }
} }
pub(crate) fn recognize_pan_gesture(
&self,
should_recognize: bool,
minimum_number_of_touches: u8,
maximum_number_of_touches: u8,
) {
let mtm = MainThreadMarker::from(self);
if should_recognize {
if self.ivars().pan_gesture_recognizer.borrow().is_none() {
let pan = unsafe {
UIPanGestureRecognizer::initWithTarget_action(
mtm.alloc(),
Some(self),
Some(sel!(panGesture:)),
)
};
pan.setDelegate(Some(ProtocolObject::from_ref(self)));
pan.setMinimumNumberOfTouches(minimum_number_of_touches as _);
pan.setMaximumNumberOfTouches(maximum_number_of_touches as _);
self.addGestureRecognizer(&pan);
self.ivars().pan_gesture_recognizer.replace(Some(pan));
}
} else if let Some(recognizer) = self.ivars().pan_gesture_recognizer.take() {
self.removeGestureRecognizer(&recognizer);
}
}
pub(crate) fn recognize_doubletap_gesture(&self, should_recognize: bool) { pub(crate) fn recognize_doubletap_gesture(&self, should_recognize: bool) {
let mtm = MainThreadMarker::from(self);
if should_recognize { if should_recognize {
if self.ivars().doubletap_gesture_recognizer.borrow().is_none() { if self.ivars().doubletap_gesture_recognizer.borrow().is_none() {
let tap = unsafe { let tap: Id<UITapGestureRecognizer> = unsafe {
UITapGestureRecognizer::initWithTarget_action( msg_send_id![UITapGestureRecognizer::alloc(), initWithTarget: self, action: sel!(doubleTapGesture:)]
mtm.alloc(),
Some(self),
Some(sel!(doubleTapGesture:)),
)
}; };
tap.setDelegate(Some(ProtocolObject::from_ref(self)));
tap.setNumberOfTapsRequired(2); tap.setNumberOfTapsRequired(2);
tap.setNumberOfTouchesRequired(1); tap.setNumberOfTouchesRequired(1);
self.addGestureRecognizer(&tap); self.addGestureRecognizer(&tap);
@@ -428,19 +308,15 @@ impl WinitView {
} }
pub(crate) fn recognize_rotation_gesture(&self, should_recognize: bool) { pub(crate) fn recognize_rotation_gesture(&self, should_recognize: bool) {
let mtm = MainThreadMarker::from(self);
if should_recognize { if should_recognize {
if self.ivars().rotation_gesture_recognizer.borrow().is_none() { if self.ivars().rotation_gesture_recognizer.borrow().is_none() {
let rotation = unsafe { let rotation: Id<UIRotationGestureRecognizer> = unsafe {
UIRotationGestureRecognizer::initWithTarget_action( msg_send_id![UIRotationGestureRecognizer::alloc(), initWithTarget: self, action: sel!(rotationGesture:)]
mtm.alloc(),
Some(self),
Some(sel!(rotationGesture:)),
)
}; };
rotation.setDelegate(Some(ProtocolObject::from_ref(self)));
self.addGestureRecognizer(&rotation); self.addGestureRecognizer(&rotation);
self.ivars().rotation_gesture_recognizer.replace(Some(rotation)); self.ivars()
.rotation_gesture_recognizer
.replace(Some(rotation));
} }
} else if let Some(recognizer) = self.ivars().rotation_gesture_recognizer.take() { } else if let Some(recognizer) = self.ivars().rotation_gesture_recognizer.take() {
self.removeGestureRecognizer(&recognizer); self.removeGestureRecognizer(&recognizer);
@@ -453,9 +329,9 @@ impl WinitView {
let os_supports_force = app_state::os_capabilities().force_touch; let os_supports_force = app_state::os_capabilities().force_touch;
for touch in touches { for touch in touches {
let logical_location = touch.locationInView(None); let logical_location = touch.locationInView(None);
let touch_type = touch.r#type(); let touch_type = touch.type_();
let force = if os_supports_force { let force = if os_supports_force {
let trait_collection = self.traitCollection(); let trait_collection = unsafe { self.traitCollection() };
let touch_capability = trait_collection.forceTouchCapability(); let touch_capability = trait_collection.forceTouchCapability();
// Both the OS _and_ the device need to be checked for force touch support. // Both the OS _and_ the device need to be checked for force touch support.
if touch_capability == UIForceTouchCapability::Available if touch_capability == UIForceTouchCapability::Available
@@ -488,7 +364,7 @@ impl WinitView {
// 2 is UITouchPhase::Stationary and is not expected here // 2 is UITouchPhase::Stationary and is not expected here
UITouchPhase::Ended => TouchPhase::Ended, UITouchPhase::Ended => TouchPhase::Ended,
UITouchPhase::Cancelled => TouchPhase::Cancelled, UITouchPhase::Cancelled => TouchPhase::Cancelled,
_ => panic!("unexpected touch phase: {phase:?}"), _ => panic!("unexpected touch phase: {:?}", phase as i32),
}; };
let physical_location = { let physical_location = {

View File

@@ -1,16 +1,16 @@
use std::cell::Cell; use std::cell::Cell;
use objc2::rc::Retained; use icrate::Foundation::{MainThreadMarker, NSObject};
use objc2::rc::Id;
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass}; use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_foundation::{MainThreadMarker, NSObject};
use objc2_ui_kit::{ use super::app_state::{self};
use super::uikit::{
UIDevice, UIInterfaceOrientationMask, UIRectEdge, UIResponder, UIStatusBarStyle, UIDevice, UIInterfaceOrientationMask, UIRectEdge, UIResponder, UIStatusBarStyle,
UIUserInterfaceIdiom, UIView, UIViewController, UIUserInterfaceIdiom, UIView, UIViewController,
}; };
use crate::platform::ios::{ScreenEdge, StatusBarStyle};
use super::app_state::{self}; use crate::{platform::ios::ValidOrientations, window::WindowAttributes};
use crate::platform::ios::{ScreenEdge, StatusBarStyle, ValidOrientations};
use crate::window::WindowAttributes;
pub struct ViewControllerState { pub struct ViewControllerState {
prefers_status_bar_hidden: Cell<bool>, prefers_status_bar_hidden: Cell<bool>,
@@ -26,7 +26,7 @@ declare_class!(
unsafe impl ClassType for WinitViewController { unsafe impl ClassType for WinitViewController {
#[inherits(UIResponder, NSObject)] #[inherits(UIResponder, NSObject)]
type Super = UIViewController; type Super = UIViewController;
type Mutability = mutability::MainThreadOnly; type Mutability = mutability::InteriorMutable;
const NAME: &'static str = "WinitUIViewController"; const NAME: &'static str = "WinitUIViewController";
} }
@@ -97,10 +97,16 @@ impl WinitViewController {
pub(crate) fn set_preferred_screen_edges_deferring_system_gestures(&self, val: ScreenEdge) { pub(crate) fn set_preferred_screen_edges_deferring_system_gestures(&self, val: ScreenEdge) {
let val = { let val = {
assert_eq!(val.bits() & !ScreenEdge::ALL.bits(), 0, "invalid `ScreenEdge`"); assert_eq!(
val.bits() & !ScreenEdge::ALL.bits(),
0,
"invalid `ScreenEdge`"
);
UIRectEdge(val.bits().into()) UIRectEdge(val.bits().into())
}; };
self.ivars().preferred_screen_edges_deferring_system_gestures.set(val); self.ivars()
.preferred_screen_edges_deferring_system_gestures
.set(val);
let os_capabilities = app_state::os_capabilities(); let os_capabilities = app_state::os_capabilities();
if os_capabilities.defer_system_gestures { if os_capabilities.defer_system_gestures {
self.setNeedsUpdateOfScreenEdgesDeferringSystemGestures(); self.setNeedsUpdateOfScreenEdgesDeferringSystemGestures();
@@ -114,46 +120,52 @@ impl WinitViewController {
mtm: MainThreadMarker, mtm: MainThreadMarker,
valid_orientations: ValidOrientations, valid_orientations: ValidOrientations,
) { ) {
let mask = match (valid_orientations, UIDevice::currentDevice(mtm).userInterfaceIdiom()) { let mask = match (
valid_orientations,
UIDevice::current(mtm).userInterfaceIdiom(),
) {
(ValidOrientations::LandscapeAndPortrait, UIUserInterfaceIdiom::Phone) => { (ValidOrientations::LandscapeAndPortrait, UIUserInterfaceIdiom::Phone) => {
UIInterfaceOrientationMask::AllButUpsideDown UIInterfaceOrientationMask::AllButUpsideDown
}, }
(ValidOrientations::LandscapeAndPortrait, _) => UIInterfaceOrientationMask::All, (ValidOrientations::LandscapeAndPortrait, _) => UIInterfaceOrientationMask::All,
(ValidOrientations::Landscape, _) => UIInterfaceOrientationMask::Landscape, (ValidOrientations::Landscape, _) => UIInterfaceOrientationMask::Landscape,
(ValidOrientations::Portrait, UIUserInterfaceIdiom::Phone) => { (ValidOrientations::Portrait, UIUserInterfaceIdiom::Phone) => {
UIInterfaceOrientationMask::Portrait UIInterfaceOrientationMask::Portrait
}, }
(ValidOrientations::Portrait, _) => { (ValidOrientations::Portrait, _) => {
UIInterfaceOrientationMask::Portrait UIInterfaceOrientationMask::Portrait
| UIInterfaceOrientationMask::PortraitUpsideDown | UIInterfaceOrientationMask::PortraitUpsideDown
}, }
}; };
self.ivars().supported_orientations.set(mask); self.ivars().supported_orientations.set(mask);
#[allow(deprecated)] UIViewController::attemptRotationToDeviceOrientation();
UIViewController::attemptRotationToDeviceOrientation(mtm);
} }
pub(crate) fn new( pub(crate) fn new(
mtm: MainThreadMarker, mtm: MainThreadMarker,
window_attributes: &WindowAttributes, window_attributes: &WindowAttributes,
view: &UIView, view: &UIView,
) -> Retained<Self> { ) -> Id<Self> {
// These are set properly below, we just to set them to something in the meantime. // These are set properly below, we just to set them to something in the meantime.
let this = mtm.alloc().set_ivars(ViewControllerState { let this = Self::alloc().set_ivars(ViewControllerState {
prefers_status_bar_hidden: Cell::new(false), prefers_status_bar_hidden: Cell::new(false),
preferred_status_bar_style: Cell::new(UIStatusBarStyle::Default), preferred_status_bar_style: Cell::new(UIStatusBarStyle::Default),
prefers_home_indicator_auto_hidden: Cell::new(false), prefers_home_indicator_auto_hidden: Cell::new(false),
supported_orientations: Cell::new(UIInterfaceOrientationMask::All), supported_orientations: Cell::new(UIInterfaceOrientationMask::All),
preferred_screen_edges_deferring_system_gestures: Cell::new(UIRectEdge::empty()), preferred_screen_edges_deferring_system_gestures: Cell::new(UIRectEdge::NONE),
}); });
let this: Retained<Self> = unsafe { msg_send_id![super(this), init] }; let this: Id<Self> = unsafe { msg_send_id![super(this), init] };
this.set_prefers_status_bar_hidden( this.set_prefers_status_bar_hidden(
window_attributes.platform_specific.prefers_status_bar_hidden, window_attributes
.platform_specific
.prefers_status_bar_hidden,
); );
this.set_preferred_status_bar_style( this.set_preferred_status_bar_style(
window_attributes.platform_specific.preferred_status_bar_style, window_attributes
.platform_specific
.preferred_status_bar_style,
); );
this.set_supported_interface_orientations( this.set_supported_interface_orientations(
@@ -162,11 +174,15 @@ impl WinitViewController {
); );
this.set_prefers_home_indicator_auto_hidden( this.set_prefers_home_indicator_auto_hidden(
window_attributes.platform_specific.prefers_home_indicator_hidden, window_attributes
.platform_specific
.prefers_home_indicator_hidden,
); );
this.set_preferred_screen_edges_deferring_system_gestures( this.set_preferred_screen_edges_deferring_system_gestures(
window_attributes.platform_specific.preferred_screen_edges_deferring_system_gestures, window_attributes
.platform_specific
.preferred_screen_edges_deferring_system_gestures,
); );
this.setView(Some(view)); this.setView(Some(view));

View File

@@ -2,33 +2,30 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use objc2::rc::Retained; use icrate::Foundation::{CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker};
use objc2::rc::Id;
use objc2::runtime::{AnyObject, NSObject}; use objc2::runtime::{AnyObject, NSObject};
use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass}; use objc2::{class, declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_foundation::{
CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker, NSObjectProtocol,
};
use objc2_ui_kit::{
UIApplication, UICoordinateSpace, UIResponder, UIScreen, UIScreenOverscanCompensation,
UIViewController, UIWindow,
};
use tracing::{debug, warn}; use tracing::{debug, warn};
use super::app_state::EventWrapper; use super::app_state::EventWrapper;
use super::uikit::{
UIApplication, UIResponder, UIScreen, UIScreenOverscanCompensation, UIViewController, UIWindow,
};
use super::view::WinitView; use super::view::WinitView;
use super::view_controller::WinitViewController; use super::view_controller::WinitViewController;
use crate::cursor::Cursor; use crate::{
use crate::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size}; cursor::Cursor,
use crate::error::{ExternalError, NotSupportedError, OsError as RootOsError}; dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
use crate::event::{Event, WindowEvent}; error::{ExternalError, NotSupportedError, OsError as RootOsError},
use crate::icon::Icon; event::{Event, WindowEvent},
use crate::platform::ios::{ScreenEdge, StatusBarStyle, ValidOrientations}; icon::Icon,
use crate::platform_impl::platform::{ platform::ios::{ScreenEdge, StatusBarStyle, ValidOrientations},
app_state, monitor, ActiveEventLoop, Fullscreen, MonitorHandle, platform_impl::platform::{app_state, monitor, ActiveEventLoop, Fullscreen, MonitorHandle},
}; window::{
use crate::window::{ CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, WindowAttributes,
CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, WindowAttributes, WindowButtons, WindowId as RootWindowId, WindowLevel,
WindowButtons, WindowId as RootWindowId, WindowLevel, },
}; };
declare_class!( declare_class!(
@@ -38,7 +35,7 @@ declare_class!(
unsafe impl ClassType for WinitUIWindow { unsafe impl ClassType for WinitUIWindow {
#[inherits(UIResponder, NSObject)] #[inherits(UIResponder, NSObject)]
type Super = UIWindow; type Super = UIWindow;
type Mutability = mutability::MainThreadOnly; type Mutability = mutability::InteriorMutable;
const NAME: &'static str = "WinitUIWindow"; const NAME: &'static str = "WinitUIWindow";
} }
@@ -79,8 +76,8 @@ impl WinitUIWindow {
window_attributes: &WindowAttributes, window_attributes: &WindowAttributes,
frame: CGRect, frame: CGRect,
view_controller: &UIViewController, view_controller: &UIViewController,
) -> Retained<Self> { ) -> Id<Self> {
let this: Retained<Self> = unsafe { msg_send_id![mtm.alloc(), initWithFrame: frame] }; let this: Id<Self> = unsafe { msg_send_id![Self::alloc(), initWithFrame: frame] };
this.setRootViewController(Some(view_controller)); this.setRootViewController(Some(view_controller));
@@ -90,11 +87,11 @@ impl WinitUIWindow {
let screen = monitor.ui_screen(mtm); let screen = monitor.ui_screen(mtm);
screen.setCurrentMode(Some(video_mode.screen_mode(mtm))); screen.setCurrentMode(Some(video_mode.screen_mode(mtm)));
this.setScreen(screen); this.setScreen(screen);
}, }
Some(Fullscreen::Borderless(Some(ref monitor))) => { Some(Fullscreen::Borderless(Some(ref monitor))) => {
let screen = monitor.ui_screen(mtm); let screen = monitor.ui_screen(mtm);
this.setScreen(screen); this.setScreen(screen);
}, }
_ => (), _ => (),
} }
@@ -107,9 +104,9 @@ impl WinitUIWindow {
} }
pub struct Inner { pub struct Inner {
window: Retained<WinitUIWindow>, window: Id<WinitUIWindow>,
view_controller: Retained<WinitViewController>, view_controller: Id<WinitViewController>,
view: Retained<WinitView>, view: Id<WinitView>,
gl_or_metal_backed: bool, gl_or_metal_backed: bool,
} }
@@ -138,13 +135,12 @@ impl Inner {
pub fn request_redraw(&self) { pub fn request_redraw(&self) {
if self.gl_or_metal_backed { if self.gl_or_metal_backed {
let mtm = MainThreadMarker::new().unwrap(); let mtm = MainThreadMarker::new().unwrap();
// `setNeedsDisplay` does nothing on UIViews which are directly backed by CAEAGLLayer or // `setNeedsDisplay` does nothing on UIViews which are directly backed by CAEAGLLayer or CAMetalLayer.
// CAMetalLayer. Ordinarily the OS sets up a bunch of UIKit state before // Ordinarily the OS sets up a bunch of UIKit state before calling drawRect: on a UIView, but when using
// calling drawRect: on a UIView, but when using raw or gl/metal for drawing // raw or gl/metal for drawing this work is completely avoided.
// this work is completely avoided.
// //
// The docs for `setNeedsDisplay` don't mention `CAMetalLayer`; however, this has been // The docs for `setNeedsDisplay` don't mention `CAMetalLayer`; however, this has been confirmed via
// confirmed via testing. // testing.
// //
// https://developer.apple.com/documentation/uikit/uiview/1622437-setneedsdisplay?language=objc // https://developer.apple.com/documentation/uikit/uiview/1622437-setneedsdisplay?language=objc
app_state::queue_gl_or_metal_redraw(mtm, self.window.clone()); app_state::queue_gl_or_metal_redraw(mtm, self.window.clone());
@@ -157,16 +153,20 @@ impl Inner {
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> { pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
let safe_area = self.safe_area_screen_space(); let safe_area = self.safe_area_screen_space();
let position = let position = LogicalPosition {
LogicalPosition { x: safe_area.origin.x as f64, y: safe_area.origin.y as f64 }; x: safe_area.origin.x as f64,
y: safe_area.origin.y as f64,
};
let scale_factor = self.scale_factor(); let scale_factor = self.scale_factor();
Ok(position.to_physical(scale_factor)) Ok(position.to_physical(scale_factor))
} }
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> { pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
let screen_frame = self.screen_frame(); let screen_frame = self.screen_frame();
let position = let position = LogicalPosition {
LogicalPosition { x: screen_frame.origin.x as f64, y: screen_frame.origin.y as f64 }; x: screen_frame.origin.x as f64,
y: screen_frame.origin.y as f64,
};
let scale_factor = self.scale_factor(); let scale_factor = self.scale_factor();
Ok(position.to_physical(scale_factor)) Ok(position.to_physical(scale_factor))
} }
@@ -176,7 +176,10 @@ impl Inner {
let position = physical_position.to_logical::<f64>(scale_factor); let position = physical_position.to_logical::<f64>(scale_factor);
let screen_frame = self.screen_frame(); let screen_frame = self.screen_frame();
let new_screen_frame = CGRect { let new_screen_frame = CGRect {
origin: CGPoint { x: position.x as _, y: position.y as _ }, origin: CGPoint {
x: position.x as _,
y: position.y as _,
},
size: screen_frame.size, size: screen_frame.size,
}; };
let bounds = self.rect_from_screen_space(new_screen_frame); let bounds = self.rect_from_screen_space(new_screen_frame);
@@ -304,15 +307,15 @@ impl Inner {
let uiscreen = video_mode.monitor.ui_screen(mtm); let uiscreen = video_mode.monitor.ui_screen(mtm);
uiscreen.setCurrentMode(Some(video_mode.screen_mode(mtm))); uiscreen.setCurrentMode(Some(video_mode.screen_mode(mtm)));
uiscreen.clone() uiscreen.clone()
}, }
Some(Fullscreen::Borderless(Some(monitor))) => monitor.ui_screen(mtm).clone(), Some(Fullscreen::Borderless(Some(monitor))) => monitor.ui_screen(mtm).clone(),
Some(Fullscreen::Borderless(None)) => { Some(Fullscreen::Borderless(None)) => {
self.current_monitor_inner().ui_screen(mtm).clone() self.current_monitor_inner().ui_screen(mtm).clone()
}, }
None => { None => {
warn!("`Window::set_fullscreen(None)` ignored on iOS"); warn!("`Window::set_fullscreen(None)` ignored on iOS");
return; return;
}, }
}; };
// this is pretty slow on iOS, so avoid doing it if we can // this is pretty slow on iOS, so avoid doing it if we can
@@ -397,8 +400,9 @@ impl Inner {
} }
pub fn primary_monitor(&self) -> Option<MonitorHandle> { pub fn primary_monitor(&self) -> Option<MonitorHandle> {
#[allow(deprecated)] Some(MonitorHandle::new(UIScreen::main(
Some(MonitorHandle::new(UIScreen::mainScreen(MainThreadMarker::new().unwrap()))) MainThreadMarker::new().unwrap(),
)))
} }
pub fn id(&self) -> WindowId { pub fn id(&self) -> WindowId {
@@ -408,18 +412,18 @@ impl Inner {
#[cfg(feature = "rwh_04")] #[cfg(feature = "rwh_04")]
pub fn raw_window_handle_rwh_04(&self) -> rwh_04::RawWindowHandle { pub fn raw_window_handle_rwh_04(&self) -> rwh_04::RawWindowHandle {
let mut window_handle = rwh_04::UiKitHandle::empty(); let mut window_handle = rwh_04::UiKitHandle::empty();
window_handle.ui_window = Retained::as_ptr(&self.window) as _; window_handle.ui_window = Id::as_ptr(&self.window) as _;
window_handle.ui_view = Retained::as_ptr(&self.view) as _; window_handle.ui_view = Id::as_ptr(&self.view) as _;
window_handle.ui_view_controller = Retained::as_ptr(&self.view_controller) as _; window_handle.ui_view_controller = Id::as_ptr(&self.view_controller) as _;
rwh_04::RawWindowHandle::UiKit(window_handle) rwh_04::RawWindowHandle::UiKit(window_handle)
} }
#[cfg(feature = "rwh_05")] #[cfg(feature = "rwh_05")]
pub fn raw_window_handle_rwh_05(&self) -> rwh_05::RawWindowHandle { pub fn raw_window_handle_rwh_05(&self) -> rwh_05::RawWindowHandle {
let mut window_handle = rwh_05::UiKitWindowHandle::empty(); let mut window_handle = rwh_05::UiKitWindowHandle::empty();
window_handle.ui_window = Retained::as_ptr(&self.window) as _; window_handle.ui_window = Id::as_ptr(&self.window) as _;
window_handle.ui_view = Retained::as_ptr(&self.view) as _; window_handle.ui_view = Id::as_ptr(&self.view) as _;
window_handle.ui_view_controller = Retained::as_ptr(&self.view_controller) as _; window_handle.ui_view_controller = Id::as_ptr(&self.view_controller) as _;
rwh_05::RawWindowHandle::UiKit(window_handle) rwh_05::RawWindowHandle::UiKit(window_handle)
} }
@@ -431,11 +435,11 @@ impl Inner {
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle { pub fn raw_window_handle_rwh_06(&self) -> rwh_06::RawWindowHandle {
let mut window_handle = rwh_06::UiKitWindowHandle::new({ let mut window_handle = rwh_06::UiKitWindowHandle::new({
let ui_view = Retained::as_ptr(&self.view) as _; let ui_view = Id::as_ptr(&self.view) as _;
std::ptr::NonNull::new(ui_view).expect("Retained<T> should never be null") std::ptr::NonNull::new(ui_view).expect("Id<T> should never be null")
}); });
window_handle.ui_view_controller = window_handle.ui_view_controller =
std::ptr::NonNull::new(Retained::as_ptr(&self.view_controller) as _); std::ptr::NonNull::new(Id::as_ptr(&self.view_controller) as _);
rwh_06::RawWindowHandle::UiKit(window_handle) rwh_06::RawWindowHandle::UiKit(window_handle)
} }
@@ -485,8 +489,7 @@ impl Window {
// TODO: transparency, visible // TODO: transparency, visible
#[allow(deprecated)] let main_screen = UIScreen::main(mtm);
let main_screen = UIScreen::mainScreen(mtm);
let fullscreen = window_attributes.fullscreen.clone().map(Into::into); let fullscreen = window_attributes.fullscreen.clone().map(Into::into);
let screen = match fullscreen { let screen = match fullscreen {
Some(Fullscreen::Exclusive(ref video_mode)) => video_mode.monitor.ui_screen(mtm), Some(Fullscreen::Exclusive(ref video_mode)) => video_mode.monitor.ui_screen(mtm),
@@ -502,16 +505,23 @@ impl Window {
let size = dim.to_logical::<f64>(scale_factor as f64); let size = dim.to_logical::<f64>(scale_factor as f64);
CGRect { CGRect {
origin: screen_bounds.origin, origin: screen_bounds.origin,
size: CGSize { width: size.width as _, height: size.height as _ }, size: CGSize {
width: size.width as _,
height: size.height as _,
},
} }
}, }
None => screen_bounds, None => screen_bounds,
}; };
let view = WinitView::new(mtm, &window_attributes, frame); let view = WinitView::new(mtm, &window_attributes, frame);
let gl_or_metal_backed = let gl_or_metal_backed = unsafe {
view.isKindOfClass(class!(CAMetalLayer)) || view.isKindOfClass(class!(CAEAGLLayer)); let layer_class = WinitView::layerClass();
let is_metal = msg_send![layer_class, isSubclassOfClass: class!(CAMetalLayer)];
let is_gl = msg_send![layer_class, isSubclassOfClass: class!(CAEAGLLayer)];
is_metal || is_gl
};
let view_controller = WinitViewController::new(mtm, &window_attributes, &view); let view_controller = WinitViewController::new(mtm, &window_attributes, &view);
let window = WinitUIWindow::new(mtm, &window_attributes, frame, &view_controller); let window = WinitUIWindow::new(mtm, &window_attributes, frame, &view_controller);
@@ -534,11 +544,13 @@ impl Window {
let window_id = RootWindowId(window.id()); let window_id = RootWindowId(window.id());
app_state::handle_nonuser_events( app_state::handle_nonuser_events(
mtm, mtm,
std::iter::once(EventWrapper::ScaleFactorChanged(app_state::ScaleFactorChanged { std::iter::once(EventWrapper::ScaleFactorChanged(
window: window.clone(), app_state::ScaleFactorChanged {
scale_factor, window: window.clone(),
suggested_size: size.to_physical(scale_factor), scale_factor,
})) suggested_size: size.to_physical(scale_factor),
},
))
.chain(std::iter::once(EventWrapper::StaticEvent( .chain(std::iter::once(EventWrapper::StaticEvent(
Event::WindowEvent { Event::WindowEvent {
window_id, window_id,
@@ -548,8 +560,15 @@ impl Window {
); );
} }
let inner = Inner { window, view_controller, view, gl_or_metal_backed }; let inner = Inner {
Ok(Window { inner: MainThreadBound::new(inner, mtm) }) window,
view_controller,
view,
gl_or_metal_backed,
};
Ok(Window {
inner: MainThreadBound::new(inner, mtm),
})
} }
pub(crate) fn maybe_queue_on_main(&self, f: impl FnOnce(&Inner) + Send + 'static) { pub(crate) fn maybe_queue_on_main(&self, f: impl FnOnce(&Inner) + Send + 'static) {
@@ -578,7 +597,9 @@ impl Window {
pub(crate) fn raw_display_handle_rwh_06( pub(crate) fn raw_display_handle_rwh_06(
&self, &self,
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> { ) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
Ok(rwh_06::RawDisplayHandle::UiKit(rwh_06::UiKitDisplayHandle::new())) Ok(rwh_06::RawDisplayHandle::UiKit(
rwh_06::UiKitDisplayHandle::new(),
))
} }
} }
@@ -601,11 +622,13 @@ impl Inner {
} }
pub fn set_prefers_home_indicator_hidden(&self, hidden: bool) { pub fn set_prefers_home_indicator_hidden(&self, hidden: bool) {
self.view_controller.set_prefers_home_indicator_auto_hidden(hidden); self.view_controller
.set_prefers_home_indicator_auto_hidden(hidden);
} }
pub fn set_preferred_screen_edges_deferring_system_gestures(&self, edges: ScreenEdge) { pub fn set_preferred_screen_edges_deferring_system_gestures(&self, edges: ScreenEdge) {
self.view_controller.set_preferred_screen_edges_deferring_system_gestures(edges); self.view_controller
.set_preferred_screen_edges_deferring_system_gestures(edges);
} }
pub fn set_prefers_status_bar_hidden(&self, hidden: bool) { pub fn set_prefers_status_bar_hidden(&self, hidden: bool) {
@@ -613,26 +636,14 @@ impl Inner {
} }
pub fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) { pub fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) {
self.view_controller.set_preferred_status_bar_style(status_bar_style); self.view_controller
.set_preferred_status_bar_style(status_bar_style);
} }
pub fn recognize_pinch_gesture(&self, should_recognize: bool) { pub fn recognize_pinch_gesture(&self, should_recognize: bool) {
self.view.recognize_pinch_gesture(should_recognize); self.view.recognize_pinch_gesture(should_recognize);
} }
pub fn recognize_pan_gesture(
&self,
should_recognize: bool,
minimum_number_of_touches: u8,
maximum_number_of_touches: u8,
) {
self.view.recognize_pan_gesture(
should_recognize,
minimum_number_of_touches,
maximum_number_of_touches,
);
}
pub fn recognize_doubletap_gesture(&self, should_recognize: bool) { pub fn recognize_doubletap_gesture(&self, should_recognize: bool) {
self.view.recognize_doubletap_gesture(should_recognize); self.view.recognize_doubletap_gesture(should_recognize);
} }
@@ -649,12 +660,14 @@ impl Inner {
fn rect_to_screen_space(&self, rect: CGRect) -> CGRect { fn rect_to_screen_space(&self, rect: CGRect) -> CGRect {
let screen_space = self.window.screen().coordinateSpace(); let screen_space = self.window.screen().coordinateSpace();
self.window.convertRect_toCoordinateSpace(rect, &screen_space) self.window
.convertRect_toCoordinateSpace(rect, &screen_space)
} }
fn rect_from_screen_space(&self, rect: CGRect) -> CGRect { fn rect_from_screen_space(&self, rect: CGRect) -> CGRect {
let screen_space = self.window.screen().coordinateSpace(); let screen_space = self.window.screen().coordinateSpace();
self.window.convertRect_fromCoordinateSpace(rect, &screen_space) self.window
.convertRect_fromCoordinateSpace(rect, &screen_space)
} }
fn safe_area_screen_space(&self) -> CGRect { fn safe_area_screen_space(&self) -> CGRect {
@@ -675,8 +688,9 @@ impl Inner {
} else { } else {
let screen_frame = self.rect_to_screen_space(bounds); let screen_frame = self.rect_to_screen_space(bounds);
let status_bar_frame = { let status_bar_frame = {
let app = UIApplication::sharedApplication(MainThreadMarker::new().unwrap()); let app = UIApplication::shared(MainThreadMarker::new().unwrap()).expect(
#[allow(deprecated)] "`Window::get_inner_position` cannot be called before `EventLoop::run_app` on iOS",
);
app.statusBarFrame() app.statusBarFrame()
}; };
let (y, height) = if screen_frame.origin.y > status_bar_frame.size.height { let (y, height) = if screen_frame.origin.y > status_bar_frame.size.height {
@@ -688,8 +702,14 @@ impl Inner {
(y, height) (y, height)
}; };
CGRect { CGRect {
origin: CGPoint { x: screen_frame.origin.x, y }, origin: CGPoint {
size: CGSize { width: screen_frame.size.width, height }, x: screen_frame.origin.x,
y,
},
size: CGSize {
width: screen_frame.size.width,
height,
},
} }
} }
} }
@@ -702,7 +722,9 @@ pub struct WindowId {
impl WindowId { impl WindowId {
pub const unsafe fn dummy() -> Self { pub const unsafe fn dummy() -> Self {
WindowId { window: std::ptr::null_mut() } WindowId {
window: std::ptr::null_mut(),
}
} }
} }
@@ -714,7 +736,9 @@ impl From<WindowId> for u64 {
impl From<u64> for WindowId { impl From<u64> for WindowId {
fn from(raw_id: u64) -> Self { fn from(raw_id: u64) -> Self {
Self { window: raw_id as _ } Self {
window: raw_id as _,
}
} }
} }
@@ -723,7 +747,9 @@ unsafe impl Sync for WindowId {}
impl From<&AnyObject> for WindowId { impl From<&AnyObject> for WindowId {
fn from(window: &AnyObject) -> WindowId { fn from(window: &AnyObject) -> WindowId {
WindowId { window: window as *const _ as _ } WindowId {
window: window as *const _ as _,
}
} }
} }

View File

@@ -6,7 +6,8 @@ use std::ops::Deref;
use std::os::unix::ffi::OsStringExt; use std::os::unix::ffi::OsStringExt;
use std::ptr::NonNull; use std::ptr::NonNull;
use super::{XkbContext, XKBCH}; use super::XkbContext;
use super::XKBCH;
use smol_str::SmolStr; use smol_str::SmolStr;
use xkbcommon_dl::{ use xkbcommon_dl::{
xkb_compose_compile_flags, xkb_compose_feed_result, xkb_compose_state, xkb_compose_state_flags, xkb_compose_compile_flags, xkb_compose_feed_result, xkb_compose_state, xkb_compose_state_flags,
@@ -90,7 +91,7 @@ impl XkbComposeState {
xkb_compose_feed_result::XKB_COMPOSE_FEED_IGNORED => ComposeStatus::Ignored, xkb_compose_feed_result::XKB_COMPOSE_FEED_IGNORED => ComposeStatus::Ignored,
xkb_compose_feed_result::XKB_COMPOSE_FEED_ACCEPTED => { xkb_compose_feed_result::XKB_COMPOSE_FEED_ACCEPTED => {
ComposeStatus::Accepted(self.status()) ComposeStatus::Accepted(self.status())
}, }
} }
} }

View File

@@ -296,7 +296,7 @@ pub fn physicalkey_to_scancode(key: PhysicalKey) -> Option<u32> {
NativeKeyCode::Xkb(raw) => Some(raw), NativeKeyCode::Xkb(raw) => Some(raw),
_ => None, _ => None,
}; };
}, }
}; };
match code { match code {
@@ -627,6 +627,7 @@ pub fn keysym_to_key(keysym: u32) -> Key {
// keysyms::ISO_First_Group_Lock => NamedKey::GroupFirstLock, // keysyms::ISO_First_Group_Lock => NamedKey::GroupFirstLock,
keysyms::ISO_Last_Group => NamedKey::GroupLast, keysyms::ISO_Last_Group => NamedKey::GroupLast,
// keysyms::ISO_Last_Group_Lock => NamedKey::GroupLastLock, // keysyms::ISO_Last_Group_Lock => NamedKey::GroupLastLock,
//
keysyms::ISO_Left_Tab => NamedKey::Tab, keysyms::ISO_Left_Tab => NamedKey::Tab,
// keysyms::ISO_Move_Line_Up => NamedKey::IsoMoveLineUp, // keysyms::ISO_Move_Line_Up => NamedKey::IsoMoveLineUp,
// keysyms::ISO_Move_Line_Down => NamedKey::IsoMoveLineDown, // keysyms::ISO_Move_Line_Down => NamedKey::IsoMoveLineDown,
@@ -808,15 +809,18 @@ pub fn keysym_to_key(keysym: u32) -> Key {
keysyms::XF86_Music => NamedKey::LaunchMusicPlayer, keysyms::XF86_Music => NamedKey::LaunchMusicPlayer,
// XF86_Battery..XF86_UWB // XF86_Battery..XF86_UWB
//
keysyms::XF86_AudioForward => NamedKey::MediaFastForward, keysyms::XF86_AudioForward => NamedKey::MediaFastForward,
// XF86_AudioRepeat // XF86_AudioRepeat
keysyms::XF86_AudioRandomPlay => NamedKey::RandomToggle, keysyms::XF86_AudioRandomPlay => NamedKey::RandomToggle,
keysyms::XF86_Subtitle => NamedKey::Subtitle, keysyms::XF86_Subtitle => NamedKey::Subtitle,
keysyms::XF86_AudioCycleTrack => NamedKey::MediaAudioTrack, keysyms::XF86_AudioCycleTrack => NamedKey::MediaAudioTrack,
// XF86_CycleAngle..XF86_Blue // XF86_CycleAngle..XF86_Blue
//
keysyms::XF86_Suspend => NamedKey::Standby, keysyms::XF86_Suspend => NamedKey::Standby,
keysyms::XF86_Hibernate => NamedKey::Hibernate, keysyms::XF86_Hibernate => NamedKey::Hibernate,
// XF86_TouchpadToggle..XF86_TouchpadOff // XF86_TouchpadToggle..XF86_TouchpadOff
//
keysyms::XF86_AudioMute => NamedKey::AudioVolumeMute, keysyms::XF86_AudioMute => NamedKey::AudioVolumeMute,
// XF86_Switch_VT_1..XF86_Switch_VT_12 // XF86_Switch_VT_1..XF86_Switch_VT_12
@@ -849,6 +853,7 @@ pub fn keysym_to_key(keysym: u32) -> Key {
keysyms::SUN_VideoLowerBrightness => NamedKey::BrightnessDown, keysyms::SUN_VideoLowerBrightness => NamedKey::BrightnessDown,
keysyms::SUN_VideoRaiseBrightness => NamedKey::BrightnessUp, keysyms::SUN_VideoRaiseBrightness => NamedKey::BrightnessUp,
// SunPowerSwitchShift // SunPowerSwitchShift
//
0 => return Key::Unidentified(NativeKey::Unidentified), 0 => return Key::Unidentified(NativeKey::Unidentified),
_ => return Key::Unidentified(NativeKey::Xkb(keysym)), _ => return Key::Unidentified(NativeKey::Xkb(keysym)),
}) })
@@ -963,7 +968,11 @@ impl XkbKeymap {
mod5: mod_index_for_name(keymap, b"Mod5\0"), mod5: mod_index_for_name(keymap, b"Mod5\0"),
}; };
Self { keymap, _mods_indices: mods_indices, _core_keyboard_id } Self {
keymap,
_mods_indices: mods_indices,
_core_keyboard_id,
}
} }
#[cfg(x11_platform)] #[cfg(x11_platform)]
@@ -1011,14 +1020,12 @@ impl Drop for XkbKeymap {
impl Deref for XkbKeymap { impl Deref for XkbKeymap {
type Target = NonNull<xkb_keymap>; type Target = NonNull<xkb_keymap>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.keymap &self.keymap
} }
} }
/// Modifier index in the keymap. /// Modifier index in the keymap.
#[cfg_attr(not(x11_platform), allow(dead_code))]
#[derive(Default, Debug, Clone, Copy)] #[derive(Default, Debug, Clone, Copy)]
pub struct ModsIndices { pub struct ModsIndices {
pub shift: Option<xkb_mod_index_t>, pub shift: Option<xkb_mod_index_t>,

View File

@@ -15,7 +15,8 @@ use xkbcommon_dl::{
#[cfg(x11_platform)] #[cfg(x11_platform)]
use {x11_dl::xlib_xcb::xcb_connection_t, xkbcommon_dl::x11::xkbcommon_x11_handle}; use {x11_dl::xlib_xcb::xcb_connection_t, xkbcommon_dl::x11::xkbcommon_x11_handle};
use crate::event::{ElementState, KeyEvent}; use crate::event::ElementState;
use crate::event::KeyEvent;
use crate::keyboard::{Key, KeyLocation}; use crate::keyboard::{Key, KeyLocation};
use crate::platform_impl::KeyEventExtra; use crate::platform_impl::KeyEventExtra;
@@ -142,7 +143,9 @@ impl Context {
#[cfg(x11_platform)] #[cfg(x11_platform)]
pub fn set_keymap_from_x11(&mut self, xcb: *mut xcb_connection_t) { pub fn set_keymap_from_x11(&mut self, xcb: *mut xcb_connection_t) {
let keymap = XkbKeymap::from_x11_keymap(&self.context, xcb, self.core_keyboard_id); let keymap = XkbKeymap::from_x11_keymap(&self.context, xcb, self.core_keyboard_id);
let state = keymap.as_ref().and_then(|keymap| XkbState::new_x11(xcb, keymap)); let state = keymap
.as_ref()
.and_then(|keymap| XkbState::new_x11(xcb, keymap));
if keymap.is_none() || state.is_none() { if keymap.is_none() || state.is_none() {
warn!("failed to update xkb keymap"); warn!("failed to update xkb keymap");
} }
@@ -157,7 +160,13 @@ impl Context {
let compose_state1 = self.compose_state1.as_mut(); let compose_state1 = self.compose_state1.as_mut();
let compose_state2 = self.compose_state2.as_mut(); let compose_state2 = self.compose_state2.as_mut();
let scratch_buffer = &mut self.scratch_buffer; let scratch_buffer = &mut self.scratch_buffer;
Some(KeyContext { state, keymap, compose_state1, compose_state2, scratch_buffer }) Some(KeyContext {
state,
keymap,
compose_state1,
compose_state2,
scratch_buffer,
})
} }
/// Key builder context with the user provided xkb state. /// Key builder context with the user provided xkb state.
@@ -172,7 +181,13 @@ impl Context {
let compose_state1 = self.compose_state1.as_mut(); let compose_state1 = self.compose_state1.as_mut();
let compose_state2 = self.compose_state2.as_mut(); let compose_state2 = self.compose_state2.as_mut();
let scratch_buffer = &mut self.scratch_buffer; let scratch_buffer = &mut self.scratch_buffer;
Some(KeyContext { state, keymap, compose_state1, compose_state2, scratch_buffer }) Some(KeyContext {
state,
keymap,
compose_state1,
compose_state2,
scratch_buffer,
})
} }
} }
@@ -199,9 +214,20 @@ impl<'a> KeyContext<'a> {
let (key_without_modifiers, _) = event.key_without_modifiers(); let (key_without_modifiers, _) = event.key_without_modifiers();
let text_with_all_modifiers = event.text_with_all_modifiers(); let text_with_all_modifiers = event.text_with_all_modifiers();
let platform_specific = KeyEventExtra { text_with_all_modifiers, key_without_modifiers }; let platform_specific = KeyEventExtra {
text_with_all_modifiers,
key_without_modifiers,
};
KeyEvent { physical_key, logical_key, text, location, state, repeat, platform_specific } KeyEvent {
physical_key,
logical_key,
text,
location,
state,
repeat,
platform_specific,
}
} }
fn keysym_to_utf8_raw(&mut self, keysym: u32) -> Option<SmolStr> { fn keysym_to_utf8_raw(&mut self, keysym: u32) -> Option<SmolStr> {
@@ -220,7 +246,10 @@ impl<'a> KeyContext<'a> {
} else if bytes_written == -1 { } else if bytes_written == -1 {
self.scratch_buffer.reserve(8); self.scratch_buffer.reserve(8);
} else { } else {
unsafe { self.scratch_buffer.set_len(bytes_written.try_into().unwrap()) }; unsafe {
self.scratch_buffer
.set_len(bytes_written.try_into().unwrap())
};
break; break;
} }
} }
@@ -252,7 +281,12 @@ impl<'a, 'b> KeyEventResults<'a, 'b> {
ComposeStatus::None ComposeStatus::None
}; };
KeyEventResults { context, keycode, keysym, compose } KeyEventResults {
context,
keycode,
keysym,
compose,
}
} }
pub fn key(&mut self) -> (Key, KeyLocation) { pub fn key(&mut self) -> (Key, KeyLocation) {
@@ -289,18 +323,23 @@ impl<'a, 'b> KeyEventResults<'a, 'b> {
} }
pub fn key_without_modifiers(&mut self) -> (Key, KeyLocation) { pub fn key_without_modifiers(&mut self) -> (Key, KeyLocation) {
// This will become a pointer to an array which libxkbcommon owns, so we don't need to // This will become a pointer to an array which libxkbcommon owns, so we don't need to deallocate it.
// deallocate it.
let layout = self.context.state.layout(self.keycode); let layout = self.context.state.layout(self.keycode);
let keysym = self.context.keymap.first_keysym_by_level(layout, self.keycode); let keysym = self
.context
.keymap
.first_keysym_by_level(layout, self.keycode);
match self.keysym_to_key(keysym) { match self.keysym_to_key(keysym) {
Ok((key, location)) => (key, location), Ok((key, location)) => (key, location),
Err((key, location)) => { Err((key, location)) => {
let key = let key = self
self.context.keysym_to_utf8_raw(keysym).map(Key::Character).unwrap_or(key); .context
.keysym_to_utf8_raw(keysym)
.map(Key::Character)
.unwrap_or(key);
(key, location) (key, location)
}, }
} }
} }
@@ -315,7 +354,8 @@ impl<'a, 'b> KeyEventResults<'a, 'b> {
} }
pub fn text(&mut self) -> Option<SmolStr> { pub fn text(&mut self) -> Option<SmolStr> {
self.composed_text().unwrap_or_else(|_| self.context.keysym_to_utf8_raw(self.keysym)) self.composed_text()
.unwrap_or_else(|_| self.context.keysym_to_utf8_raw(self.keysym))
} }
// The current behaviour makes it so composing a character overrides attempts to input a // The current behaviour makes it so composing a character overrides attempts to input a
@@ -324,7 +364,10 @@ impl<'a, 'b> KeyEventResults<'a, 'b> {
pub fn text_with_all_modifiers(&mut self) -> Option<SmolStr> { pub fn text_with_all_modifiers(&mut self) -> Option<SmolStr> {
match self.composed_text() { match self.composed_text() {
Ok(text) => text, Ok(text) => text,
Err(_) => self.context.state.get_utf8_raw(self.keycode, self.context.scratch_buffer), Err(_) => self
.context
.state
.get_utf8_raw(self.keycode, self.context.scratch_buffer),
} }
} }
@@ -334,7 +377,7 @@ impl<'a, 'b> KeyEventResults<'a, 'b> {
xkb_compose_status::XKB_COMPOSE_COMPOSED => { xkb_compose_status::XKB_COMPOSE_COMPOSED => {
let state = self.context.compose_state1.as_mut().unwrap(); let state = self.context.compose_state1.as_mut().unwrap();
Ok(state.get_string(self.context.scratch_buffer)) Ok(state.get_string(self.context.scratch_buffer))
}, }
xkb_compose_status::XKB_COMPOSE_COMPOSING xkb_compose_status::XKB_COMPOSE_COMPOSING
| xkb_compose_status::XKB_COMPOSE_CANCELLED => Ok(None), | xkb_compose_status::XKB_COMPOSE_CANCELLED => Ok(None),
xkb_compose_status::XKB_COMPOSE_NOTHING => Err(()), xkb_compose_status::XKB_COMPOSE_NOTHING => Err(()),
@@ -393,7 +436,10 @@ where
// The allocated buffer must include space for the null-terminator. // The allocated buffer must include space for the null-terminator.
scratch_buffer.reserve(size + 1); scratch_buffer.reserve(size + 1);
unsafe { unsafe {
let written = f(scratch_buffer.as_mut_ptr().cast(), scratch_buffer.capacity()); let written = f(
scratch_buffer.as_mut_ptr().cast(),
scratch_buffer.capacity(),
);
if usize::try_from(written).unwrap() != size { if usize::try_from(written).unwrap() != size {
// This will likely never happen. // This will likely never happen.
return None; return None;
@@ -410,7 +456,10 @@ fn byte_slice_to_smol_str(bytes: &[u8]) -> Option<SmolStr> {
std::str::from_utf8(bytes) std::str::from_utf8(bytes)
.map(SmolStr::new) .map(SmolStr::new)
.map_err(|e| { .map_err(|e| {
tracing::warn!("UTF-8 received from libxkbcommon ({:?}) was invalid: {e}", bytes) tracing::warn!(
"UTF-8 received from libxkbcommon ({:?}) was invalid: {e}",
bytes
)
}) })
.ok() .ok()
} }

View File

@@ -3,11 +3,10 @@
#[cfg(all(not(x11_platform), not(wayland_platform)))] #[cfg(all(not(x11_platform), not(wayland_platform)))]
compile_error!("Please select a feature to build for unix: `x11`, `wayland`"); compile_error!("Please select a feature to build for unix: `x11`, `wayland`");
use std::collections::VecDeque;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use std::{env, fmt}; use std::{collections::VecDeque, env, fmt};
#[cfg(x11_platform)] #[cfg(x11_platform)]
use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Mutex}; use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Mutex};
@@ -17,19 +16,22 @@ use smol_str::SmolStr;
#[cfg(x11_platform)] #[cfg(x11_platform)]
use self::x11::{X11Error, XConnection, XError, XNotSupported}; use self::x11::{X11Error, XConnection, XError, XNotSupported};
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError};
use crate::event_loop::{
ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopClosed,
};
use crate::icon::Icon;
use crate::keyboard::Key;
use crate::platform::pump_events::PumpStatus;
#[cfg(x11_platform)] #[cfg(x11_platform)]
use crate::platform::x11::{WindowType as XWindowType, XlibErrorHook}; use crate::platform::x11::{WindowType as XWindowType, XlibErrorHook};
use crate::window::{ use crate::window::{CustomCursor, CustomCursorSource};
ActivationToken, Cursor, CursorGrabMode, CustomCursor, CustomCursorSource, ImePurpose, use crate::{
ResizeDirection, Theme, UserAttentionType, WindowAttributes, WindowButtons, WindowLevel, dpi::{PhysicalPosition, PhysicalSize, Position, Size},
error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError},
event_loop::{
ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopClosed,
},
icon::Icon,
keyboard::Key,
platform::pump_events::PumpStatus,
window::{
ActivationToken, Cursor, CursorGrabMode, ImePurpose, ResizeDirection, Theme,
UserAttentionType, WindowAttributes, WindowButtons, WindowLevel,
},
}; };
pub(crate) use self::common::xkb::{physicalkey_to_scancode, scancode_to_physicalkey}; pub(crate) use self::common::xkb::{physicalkey_to_scancode, scancode_to_physicalkey};
@@ -90,7 +92,6 @@ pub struct X11WindowAttributes {
pub embed_window: Option<x11rb::protocol::xproto::Window>, pub embed_window: Option<x11rb::protocol::xproto::Window>,
} }
#[cfg_attr(not(x11_platform), allow(clippy::derivable_impls))]
impl Default for PlatformSpecificWindowAttributes { impl Default for PlatformSpecificWindowAttributes {
fn default() -> Self { fn default() -> Self {
Self { Self {
@@ -292,11 +293,11 @@ impl Window {
#[cfg(wayland_platform)] #[cfg(wayland_platform)]
ActiveEventLoop::Wayland(ref window_target) => { ActiveEventLoop::Wayland(ref window_target) => {
wayland::Window::new(window_target, attribs).map(Window::Wayland) wayland::Window::new(window_target, attribs).map(Window::Wayland)
}, }
#[cfg(x11_platform)] #[cfg(x11_platform)]
ActiveEventLoop::X(ref window_target) => { ActiveEventLoop::X(ref window_target) => {
x11::Window::new(window_target, attribs).map(Window::X) x11::Window::new(window_target, attribs).map(Window::X)
}, }
} }
} }
@@ -532,7 +533,6 @@ impl Window {
pub fn focus_window(&self) { pub fn focus_window(&self) {
x11_or_wayland!(match self; Window(w) => w.focus_window()) x11_or_wayland!(match self; Window(w) => w.focus_window())
} }
pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) { pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
x11_or_wayland!(match self; Window(w) => w.request_user_attention(request_type)) x11_or_wayland!(match self; Window(w) => w.request_user_attention(request_type))
} }
@@ -556,13 +556,17 @@ impl Window {
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> { pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
match self { match self {
#[cfg(x11_platform)] #[cfg(x11_platform)]
Window::X(ref window) => { Window::X(ref window) => window
window.available_monitors().into_iter().map(MonitorHandle::X).collect() .available_monitors()
}, .into_iter()
.map(MonitorHandle::X)
.collect(),
#[cfg(wayland_platform)] #[cfg(wayland_platform)]
Window::Wayland(ref window) => { Window::Wayland(ref window) => window
window.available_monitors().into_iter().map(MonitorHandle::Wayland).collect() .available_monitors()
}, .into_iter()
.map(MonitorHandle::Wayland)
.collect(),
} }
} }
@@ -721,8 +725,7 @@ impl<T: 'static> EventLoop<T> {
"Initializing the event loop outside of the main thread is a significant \ "Initializing the event loop outside of the main thread is a significant \
cross-platform compatibility hazard. If you absolutely need to create an \ cross-platform compatibility hazard. If you absolutely need to create an \
EventLoop on a different thread, you can use the \ EventLoop on a different thread, you can use the \
`EventLoopBuilderExtX11::any_thread` or `EventLoopBuilderExtWayland::any_thread` \ `EventLoopBuilderExtUnix::any_thread` function."
functions."
); );
} }
@@ -736,10 +739,12 @@ impl<T: 'static> EventLoop<T> {
.or_else(|| env::var("WAYLAND_SOCKET").ok()) .or_else(|| env::var("WAYLAND_SOCKET").ok())
.filter(|var| !var.is_empty()) .filter(|var| !var.is_empty())
.is_some(), .is_some(),
env::var("DISPLAY").map(|var| !var.is_empty()).unwrap_or(false), env::var("DISPLAY")
.map(|var| !var.is_empty())
.unwrap_or(false),
) { ) {
// User is forcing a backend. // User is forcing a backend.
(Some(backend), ..) => backend, (Some(backend), _, _) => backend,
// Wayland is present. // Wayland is present.
#[cfg(wayland_platform)] #[cfg(wayland_platform)]
(None, true, _) => Backend::Wayland, (None, true, _) => Backend::Wayland,
@@ -749,16 +754,14 @@ impl<T: 'static> EventLoop<T> {
// No backend is present. // No backend is present.
(_, wayland_display, x11_display) => { (_, wayland_display, x11_display) => {
let msg = if wayland_display && !cfg!(wayland_platform) { let msg = if wayland_display && !cfg!(wayland_platform) {
"DISPLAY is not set; note: enable the `winit/wayland` feature to support \ "DISPLAY is not set; note: enable the `winit/wayland` feature to support Wayland"
Wayland"
} else if x11_display && !cfg!(x11_platform) { } else if x11_display && !cfg!(x11_platform) {
"neither WAYLAND_DISPLAY nor WAYLAND_SOCKET is set; note: enable the \ "neither WAYLAND_DISPLAY nor WAYLAND_SOCKET is set; note: enable the `winit/x11` feature to support X11"
`winit/x11` feature to support X11"
} else { } else {
"neither WAYLAND_DISPLAY nor WAYLAND_SOCKET nor DISPLAY is set." "neither WAYLAND_DISPLAY nor WAYLAND_SOCKET nor DISPLAY is set."
}; };
return Err(EventLoopError::Os(os_error!(OsError::Misc(msg)))); return Err(EventLoopError::Os(os_error!(OsError::Misc(msg))));
}, }
}; };
// Create the display based on the backend. // Create the display based on the backend.
@@ -785,16 +788,6 @@ impl<T: 'static> EventLoop<T> {
Ok(EventLoop::X(x11::EventLoop::new(xconn))) Ok(EventLoop::X(x11::EventLoop::new(xconn)))
} }
#[inline]
pub fn is_wayland(&self) -> bool {
match *self {
#[cfg(wayland_platform)]
EventLoop::Wayland(_) => true,
#[cfg(x11_platform)]
_ => false,
}
}
pub fn create_proxy(&self) -> EventLoopProxy<T> { pub fn create_proxy(&self) -> EventLoopProxy<T> {
x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy) x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy)
} }
@@ -869,13 +862,14 @@ impl ActiveEventLoop {
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> { pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
match *self { match *self {
#[cfg(wayland_platform)] #[cfg(wayland_platform)]
ActiveEventLoop::Wayland(ref evlp) => { ActiveEventLoop::Wayland(ref evlp) => evlp
evlp.available_monitors().map(MonitorHandle::Wayland).collect() .available_monitors()
}, .map(MonitorHandle::Wayland)
.collect(),
#[cfg(x11_platform)] #[cfg(x11_platform)]
ActiveEventLoop::X(ref evlp) => { ActiveEventLoop::X(ref evlp) => {
evlp.available_monitors().map(MonitorHandle::X).collect() evlp.available_monitors().map(MonitorHandle::X).collect()
}, }
} }
} }
@@ -965,7 +959,7 @@ impl OwnedDisplayHandle {
xlib_handle.display = xconn.display.cast(); xlib_handle.display = xconn.display.cast();
xlib_handle.screen = xconn.default_screen_index() as _; xlib_handle.screen = xconn.default_screen_index() as _;
xlib_handle.into() xlib_handle.into()
}, }
#[cfg(wayland_platform)] #[cfg(wayland_platform)]
Self::Wayland(conn) => { Self::Wayland(conn) => {
@@ -974,7 +968,7 @@ impl OwnedDisplayHandle {
let mut wayland_handle = rwh_05::WaylandDisplayHandle::empty(); let mut wayland_handle = rwh_05::WaylandDisplayHandle::empty();
wayland_handle.display = conn.display().id().as_ptr() as *mut _; wayland_handle.display = conn.display().id().as_ptr() as *mut _;
wayland_handle.into() wayland_handle.into()
}, }
} }
} }
@@ -1001,7 +995,7 @@ impl OwnedDisplayHandle {
NonNull::new(conn.display().id().as_ptr().cast()).unwrap(), NonNull::new(conn.display().id().as_ptr().cast()).unwrap(),
) )
.into()) .into())
}, }
} }
} }
} }
@@ -1010,7 +1004,9 @@ impl OwnedDisplayHandle {
/// equates to an infinite timeout, not a zero timeout (so can't just use /// equates to an infinite timeout, not a zero timeout (so can't just use
/// `Option::min`) /// `Option::min`)
fn min_timeout(a: Option<Duration>, b: Option<Duration>) -> Option<Duration> { fn min_timeout(a: Option<Duration>, b: Option<Duration>) -> Option<Duration> {
a.map_or(b, |a_timeout| b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))) a.map_or(b, |a_timeout| {
b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))
})
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]

View File

@@ -12,7 +12,8 @@ use std::time::{Duration, Instant};
use sctk::reexports::calloop::Error as CalloopError; use sctk::reexports::calloop::Error as CalloopError;
use sctk::reexports::calloop_wayland_source::WaylandSource; use sctk::reexports::calloop_wayland_source::WaylandSource;
use sctk::reexports::client::{globals, Connection, QueueHandle}; use sctk::reexports::client::globals;
use sctk::reexports::client::{Connection, QueueHandle};
use crate::cursor::OnlyCursorImage; use crate::cursor::OnlyCursorImage;
use crate::dpi::LogicalSize; use crate::dpi::LogicalSize;
@@ -80,19 +81,26 @@ impl<T: 'static> EventLoop<T> {
let connection = map_err!(Connection::connect_to_env(), WaylandError::Connection)?; let connection = map_err!(Connection::connect_to_env(), WaylandError::Connection)?;
let (globals, mut event_queue) = let (globals, mut event_queue) = map_err!(
map_err!(globals::registry_queue_init(&connection), WaylandError::Global)?; globals::registry_queue_init(&connection),
WaylandError::Global
)?;
let queue_handle = event_queue.handle(); let queue_handle = event_queue.handle();
let event_loop = let event_loop = map_err!(
map_err!(calloop::EventLoop::<WinitState>::try_new(), WaylandError::Calloop)?; calloop::EventLoop::<WinitState>::try_new(),
WaylandError::Calloop
)?;
let mut winit_state = WinitState::new(&globals, &queue_handle, event_loop.handle()) let mut winit_state = WinitState::new(&globals, &queue_handle, event_loop.handle())
.map_err(|error| os_error!(error))?; .map_err(|error| os_error!(error))?;
// NOTE: do a roundtrip after binding the globals to prevent potential // NOTE: do a roundtrip after binding the globals to prevent potential
// races with the server. // races with the server.
map_err!(event_queue.roundtrip(&mut winit_state), WaylandError::Dispatch)?; map_err!(
event_queue.roundtrip(&mut winit_state),
WaylandError::Dispatch
)?;
// Register Wayland source. // Register Wayland source.
let wayland_source = WaylandSource::new(connection.clone(), event_queue); let wayland_source = WaylandSource::new(connection.clone(), event_queue);
@@ -109,7 +117,9 @@ impl<T: 'static> EventLoop<T> {
}); });
map_err!( map_err!(
event_loop.handle().register_dispatcher(wayland_dispatcher.clone()), event_loop
.handle()
.register_dispatcher(wayland_dispatcher.clone()),
WaylandError::Calloop WaylandError::Calloop
)?; )?;
@@ -119,12 +129,15 @@ impl<T: 'static> EventLoop<T> {
let (user_events_sender, user_events_channel) = calloop::channel::channel(); let (user_events_sender, user_events_channel) = calloop::channel::channel();
let result = event_loop let result = event_loop
.handle() .handle()
.insert_source(user_events_channel, move |event, _, winit_state: &mut WinitState| { .insert_source(
if let calloop::channel::Event::Msg(msg) = event { user_events_channel,
winit_state.dispatched_events = true; move |event, _, winit_state: &mut WinitState| {
pending_user_events_clone.borrow_mut().push(msg); if let calloop::channel::Event::Msg(msg) = event {
} winit_state.dispatched_events = true;
}) pending_user_events_clone.borrow_mut().push(msg);
}
},
)
.map_err(|error| error.error); .map_err(|error| error.error);
map_err!(result, WaylandError::Calloop)?; map_err!(result, WaylandError::Calloop)?;
@@ -137,10 +150,13 @@ impl<T: 'static> EventLoop<T> {
let result = event_loop let result = event_loop
.handle() .handle()
.insert_source(event_loop_awakener_source, move |_, _, winit_state: &mut WinitState| { .insert_source(
// Mark that we have something to dispatch. event_loop_awakener_source,
winit_state.dispatched_events = true; move |_, _, winit_state: &mut WinitState| {
}) // Mark that we have something to dispatch.
winit_state.dispatched_events = true;
},
)
.map_err(|error| error.error); .map_err(|error| error.error);
map_err!(result, WaylandError::Calloop)?; map_err!(result, WaylandError::Calloop)?;
@@ -181,13 +197,13 @@ impl<T: 'static> EventLoop<T> {
match self.pump_events(None, &mut event_handler) { match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => { PumpStatus::Exit(0) => {
break Ok(()); break Ok(());
}, }
PumpStatus::Exit(code) => { PumpStatus::Exit(code) => {
break Err(EventLoopError::ExitFailure(code)); break Err(EventLoopError::ExitFailure(code));
}, }
_ => { _ => {
continue; continue;
}, }
} }
}; };
@@ -240,7 +256,7 @@ impl<T: 'static> EventLoop<T> {
ControlFlow::Poll => Some(Duration::ZERO), ControlFlow::Poll => Some(Duration::ZERO),
ControlFlow::WaitUntil(wait_deadline) => { ControlFlow::WaitUntil(wait_deadline) => {
Some(wait_deadline.saturating_duration_since(start)) Some(wait_deadline.saturating_duration_since(start))
}, }
}; };
min_timeout(control_flow_timeout, timeout) min_timeout(control_flow_timeout, timeout)
}; };
@@ -258,12 +274,11 @@ impl<T: 'static> EventLoop<T> {
if let Err(error) = self.loop_dispatch(timeout) { if let Err(error) = self.loop_dispatch(timeout) {
// NOTE We exit on errors from dispatches, since if we've got protocol error // NOTE We exit on errors from dispatches, since if we've got protocol error
// libwayland-client/wayland-rs will inform us anyway, but crashing downstream is // libwayland-client/wayland-rs will inform us anyway, but crashing downstream is not
// not really an option. Instead we inform that the event loop got // really an option. Instead we inform that the event loop got destroyed. We may
// destroyed. We may communicate an error that something was // communicate an error that something was terminated, but winit doesn't provide us
// terminated, but winit doesn't provide us with an API to do that // with an API to do that via some event.
// via some event. Still, we set the exit code to the error's OS // Still, we set the exit code to the error's OS error code, or to 1 if not possible.
// error code, or to 1 if not possible.
let exit_code = error.raw_os_error().unwrap_or(1); let exit_code = error.raw_os_error().unwrap_or(1);
self.set_exit_code(exit_code); self.set_exit_code(exit_code);
return; return;
@@ -273,14 +288,23 @@ impl<T: 'static> EventLoop<T> {
// to be considered here // to be considered here
let cause = match self.control_flow() { let cause = match self.control_flow() {
ControlFlow::Poll => StartCause::Poll, ControlFlow::Poll => StartCause::Poll,
ControlFlow::Wait => StartCause::WaitCancelled { start, requested_resume: None }, ControlFlow::Wait => StartCause::WaitCancelled {
start,
requested_resume: None,
},
ControlFlow::WaitUntil(deadline) => { ControlFlow::WaitUntil(deadline) => {
if Instant::now() < deadline { if Instant::now() < deadline {
StartCause::WaitCancelled { start, requested_resume: Some(deadline) } StartCause::WaitCancelled {
start,
requested_resume: Some(deadline),
}
} else { } else {
StartCause::ResumeTimeReached { start, requested_resume: deadline } StartCause::ResumeTimeReached {
start,
requested_resume: deadline,
}
} }
}, }
}; };
// Reduce spurious wake-ups. // Reduce spurious wake-ups.
@@ -447,8 +471,13 @@ impl<T: 'static> EventLoop<T> {
return Some(WindowEvent::Destroyed); return Some(WindowEvent::Destroyed);
} }
let mut window = let mut window = state
state.windows.get_mut().get_mut(window_id).unwrap().lock().unwrap(); .windows
.get_mut()
.get_mut(window_id)
.unwrap()
.lock()
.unwrap();
if window.frame_callback_state() == FrameCallbackState::Requested { if window.frame_callback_state() == FrameCallbackState::Requested {
return None; return None;
@@ -456,8 +485,10 @@ impl<T: 'static> EventLoop<T> {
// Reset the frame callbacks state. // Reset the frame callbacks state.
window.frame_callback_reset(); window.frame_callback_reset();
let mut redraw_requested = let mut redraw_requested = window_requests
window_requests.get(window_id).unwrap().take_redraw_requested(); .get(window_id)
.unwrap()
.take_redraw_requested();
// Redraw the frame while at it. // Redraw the frame while at it.
redraw_requested |= window.refresh_frame(); redraw_requested |= window.refresh_frame();
@@ -467,7 +498,10 @@ impl<T: 'static> EventLoop<T> {
if let Some(event) = event { if let Some(event) = event {
callback( callback(
Event::WindowEvent { window_id: crate::window::WindowId(*window_id), event }, Event::WindowEvent {
window_id: crate::window::WindowId(*window_id),
event,
},
&self.window_target, &self.window_target,
); );
} }
@@ -498,7 +532,7 @@ impl<T: 'static> EventLoop<T> {
} }
refresh refresh
}, }
None => false, None => false,
}); });
} }
@@ -511,7 +545,7 @@ impl<T: 'static> EventLoop<T> {
match &self.window_target.p { match &self.window_target.p {
PlatformActiveEventLoop::Wayland(window_target) => { PlatformActiveEventLoop::Wayland(window_target) => {
window_target.event_loop_awakener.ping(); window_target.event_loop_awakener.ping();
}, }
#[cfg(x11_platform)] #[cfg(x11_platform)]
PlatformActiveEventLoop::X(_) => unreachable!(), PlatformActiveEventLoop::X(_) => unreachable!(),
} }
@@ -565,7 +599,9 @@ impl<T: 'static> EventLoop<T> {
let mut wayland_source = self.wayland_dispatcher.as_source_mut(); let mut wayland_source = self.wayland_dispatcher.as_source_mut();
let event_queue = wayland_source.queue(); let event_queue = wayland_source.queue();
event_queue.roundtrip(state).map_err(|error| { event_queue.roundtrip(state).map_err(|error| {
os_error!(OsError::WaylandError(Arc::new(WaylandError::Dispatch(error)))) os_error!(OsError::WaylandError(Arc::new(WaylandError::Dispatch(
error
))))
}) })
} }

View File

@@ -13,7 +13,9 @@ pub struct EventLoopProxy<T: 'static> {
impl<T: 'static> Clone for EventLoopProxy<T> { impl<T: 'static> Clone for EventLoopProxy<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
EventLoopProxy { user_events_sender: self.user_events_sender.clone() } EventLoopProxy {
user_events_sender: self.user_events_sender.clone(),
}
} }
} }
@@ -23,6 +25,8 @@ impl<T: 'static> EventLoopProxy<T> {
} }
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> { pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
self.user_events_sender.send(event).map_err(|SendError(error)| EventLoopClosed(error)) self.user_events_sender
.send(event)
.map_err(|SendError(error)| EventLoopClosed(error))
} }
} }

View File

@@ -38,7 +38,10 @@ impl EventSink {
/// Add new window event to a queue. /// Add new window event to a queue.
#[inline] #[inline]
pub fn push_window_event(&mut self, event: WindowEvent, window_id: WindowId) { pub fn push_window_event(&mut self, event: WindowEvent, window_id: WindowId) {
self.window_events.push(Event::WindowEvent { event, window_id: RootWindowId(window_id) }); self.window_events.push(Event::WindowEvent {
event,
window_id: RootWindowId(window_id),
});
} }
#[inline] #[inline]

View File

@@ -1,3 +1,5 @@
#![cfg(wayland_platform)]
//! Winit's Wayland backend. //! Winit's Wayland backend.
use std::fmt::Display; use std::fmt::Display;

View File

@@ -11,7 +11,11 @@ use super::event_loop::ActiveEventLoop;
impl ActiveEventLoop { impl ActiveEventLoop {
#[inline] #[inline]
pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> { pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
self.state.borrow().output_state.outputs().map(MonitorHandle::new) self.state
.borrow()
.output_state
.outputs()
.map(MonitorHandle::new)
} }
#[inline] #[inline]
@@ -48,7 +52,9 @@ impl MonitorHandle {
pub fn size(&self) -> PhysicalSize<u32> { pub fn size(&self) -> PhysicalSize<u32> {
let output_data = self.proxy.data::<OutputData>().unwrap(); let output_data = self.proxy.data::<OutputData>().unwrap();
let dimensions = output_data.with_output_info(|info| { let dimensions = output_data.with_output_info(|info| {
info.modes.iter().find_map(|mode| mode.current.then_some(mode.dimensions)) info.modes
.iter()
.find_map(|mode| mode.current.then_some(mode.dimensions))
}); });
match dimensions { match dimensions {
@@ -79,7 +85,9 @@ impl MonitorHandle {
pub fn refresh_rate_millihertz(&self) -> Option<u32> { pub fn refresh_rate_millihertz(&self) -> Option<u32> {
let output_data = self.proxy.data::<OutputData>().unwrap(); let output_data = self.proxy.data::<OutputData>().unwrap();
output_data.with_output_info(|info| { output_data.with_output_info(|info| {
info.modes.iter().find_map(|mode| mode.current.then_some(mode.refresh_rate as u32)) info.modes
.iter()
.find_map(|mode| mode.current.then_some(mode.refresh_rate as u32))
}) })
} }

View File

@@ -7,8 +7,9 @@ use calloop::timer::{TimeoutAction, Timer};
use calloop::{LoopHandle, RegistrationToken}; use calloop::{LoopHandle, RegistrationToken};
use tracing::warn; use tracing::warn;
use sctk::reexports::client::protocol::wl_keyboard::WlKeyboard;
use sctk::reexports::client::protocol::wl_keyboard::{ use sctk::reexports::client::protocol::wl_keyboard::{
Event as WlKeyboardEvent, KeyState as WlKeyState, KeymapFormat as WlKeymapFormat, WlKeyboard, Event as WlKeyboardEvent, KeyState as WlKeyState, KeymapFormat as WlKeymapFormat,
}; };
use sctk::reexports::client::protocol::wl_seat::WlSeat; use sctk::reexports::client::protocol::wl_seat::WlSeat;
use sctk::reexports::client::{Connection, Dispatch, Proxy, QueueHandle, WEnum}; use sctk::reexports::client::{Connection, Dispatch, Proxy, QueueHandle, WEnum};
@@ -41,16 +42,16 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
WEnum::Value(format) => match format { WEnum::Value(format) => match format {
WlKeymapFormat::NoKeymap => { WlKeymapFormat::NoKeymap => {
warn!("non-xkb compatible keymap") warn!("non-xkb compatible keymap")
}, }
WlKeymapFormat::XkbV1 => { WlKeymapFormat::XkbV1 => {
let context = &mut seat_state.keyboard_state.as_mut().unwrap().xkb_context; let context = &mut seat_state.keyboard_state.as_mut().unwrap().xkb_context;
context.set_keymap_from_fd(fd, size as usize); context.set_keymap_from_fd(fd, size as usize);
}, }
_ => unreachable!(), _ => unreachable!(),
}, },
WEnum::Unknown(value) => { WEnum::Unknown(value) => {
warn!("unknown keymap format 0x{:x}", value) warn!("unknown keymap format 0x{:x}", value)
}, }
}, },
WlKeyboardEvent::Enter { surface, .. } => { WlKeyboardEvent::Enter { surface, .. } => {
let window_id = wayland::make_wid(&surface); let window_id = wayland::make_wid(&surface);
@@ -62,7 +63,7 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
let was_unfocused = !window.has_focus(); let was_unfocused = !window.has_focus();
window.add_seat_focus(data.seat.id()); window.add_seat_focus(data.seat.id());
was_unfocused was_unfocused
}, }
None => return, None => return,
}; };
@@ -77,7 +78,9 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
// The keyboard focus is considered as general focus. // The keyboard focus is considered as general focus.
if was_unfocused { if was_unfocused {
state.events_sink.push_window_event(WindowEvent::Focused(true), window_id); state
.events_sink
.push_window_event(WindowEvent::Focused(true), window_id);
} }
// HACK: this is just for GNOME not fixing their ordering issue of modifiers. // HACK: this is just for GNOME not fixing their ordering issue of modifiers.
@@ -87,7 +90,7 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
window_id, window_id,
); );
} }
}, }
WlKeyboardEvent::Leave { surface, .. } => { WlKeyboardEvent::Leave { surface, .. } => {
let window_id = wayland::make_wid(&surface); let window_id = wayland::make_wid(&surface);
@@ -106,7 +109,7 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
let mut window = window.lock().unwrap(); let mut window = window.lock().unwrap();
window.remove_seat_focus(&data.seat.id()); window.remove_seat_focus(&data.seat.id());
window.has_focus() window.has_focus()
}, }
None => return, None => return,
}; };
@@ -121,10 +124,16 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
window_id, window_id,
); );
state.events_sink.push_window_event(WindowEvent::Focused(false), window_id); state
.events_sink
.push_window_event(WindowEvent::Focused(false), window_id);
} }
}, }
WlKeyboardEvent::Key { key, state: WEnum::Value(WlKeyState::Pressed), .. } => { WlKeyboardEvent::Key {
key,
state: WEnum::Value(WlKeyState::Pressed),
..
} => {
let key = key + 8; let key = key + 8;
key_input( key_input(
@@ -142,7 +151,12 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
RepeatInfo::Disable => return, RepeatInfo::Disable => return,
}; };
if !keyboard_state.xkb_context.keymap_mut().unwrap().key_repeats(key) { if !keyboard_state
.xkb_context
.keymap_mut()
.unwrap()
.key_repeats(key)
{
return; return;
} }
@@ -189,8 +203,12 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
} }
}) })
.ok(); .ok();
}, }
WlKeyboardEvent::Key { key, state: WEnum::Value(WlKeyState::Released), .. } => { WlKeyboardEvent::Key {
key,
state: WEnum::Value(WlKeyState::Released),
..
} => {
let key = key + 8; let key = key + 8;
key_input( key_input(
@@ -204,7 +222,11 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
let keyboard_state = seat_state.keyboard_state.as_mut().unwrap(); let keyboard_state = seat_state.keyboard_state.as_mut().unwrap();
if keyboard_state.repeat_info != RepeatInfo::Disable if keyboard_state.repeat_info != RepeatInfo::Disable
&& keyboard_state.xkb_context.keymap_mut().unwrap().key_repeats(key) && keyboard_state
.xkb_context
.keymap_mut()
.unwrap()
.key_repeats(key)
&& Some(key) == keyboard_state.current_repeat && Some(key) == keyboard_state.current_repeat
{ {
keyboard_state.current_repeat = None; keyboard_state.current_repeat = None;
@@ -212,9 +234,13 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
keyboard_state.loop_handle.remove(token); keyboard_state.loop_handle.remove(token);
} }
} }
}, }
WlKeyboardEvent::Modifiers { WlKeyboardEvent::Modifiers {
mods_depressed, mods_latched, mods_locked, group, .. mods_depressed,
mods_latched,
mods_locked,
group,
..
} => { } => {
let xkb_context = &mut seat_state.keyboard_state.as_mut().unwrap().xkb_context; let xkb_context = &mut seat_state.keyboard_state.as_mut().unwrap().xkb_context;
let xkb_state = match xkb_context.state_mut() { let xkb_state = match xkb_context.state_mut() {
@@ -231,14 +257,14 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
None => { None => {
seat_state.modifiers_pending = true; seat_state.modifiers_pending = true;
return; return;
}, }
}; };
state.events_sink.push_window_event( state.events_sink.push_window_event(
WindowEvent::ModifiersChanged(seat_state.modifiers.into()), WindowEvent::ModifiersChanged(seat_state.modifiers.into()),
window_id, window_id,
); );
}, }
WlKeyboardEvent::RepeatInfo { rate, delay } => { WlKeyboardEvent::RepeatInfo { rate, delay } => {
let keyboard_state = seat_state.keyboard_state.as_mut().unwrap(); let keyboard_state = seat_state.keyboard_state.as_mut().unwrap();
keyboard_state.repeat_info = if rate == 0 { keyboard_state.repeat_info = if rate == 0 {
@@ -253,7 +279,7 @@ impl Dispatch<WlKeyboard, KeyboardData, WinitState> for WinitState {
let delay = Duration::from_millis(delay as u64); let delay = Duration::from_millis(delay as u64);
RepeatInfo::Repeat { gap, delay } RepeatInfo::Repeat { gap, delay }
}; };
}, }
_ => unreachable!(), _ => unreachable!(),
} }
} }
@@ -327,7 +353,10 @@ impl Default for RepeatInfo {
/// ///
/// The values are picked based on the default in various compositors and Xorg. /// The values are picked based on the default in various compositors and Xorg.
fn default() -> Self { fn default() -> Self {
Self::Repeat { gap: Duration::from_millis(40), delay: Duration::from_millis(200) } Self::Repeat {
gap: Duration::from_millis(40),
delay: Duration::from_millis(200),
}
} }
} }
@@ -343,7 +372,10 @@ pub struct KeyboardData {
impl KeyboardData { impl KeyboardData {
pub fn new(seat: WlSeat) -> Self { pub fn new(seat: WlSeat) -> Self {
Self { window_id: Default::default(), seat } Self {
window_id: Default::default(),
seat,
}
} }
} }
@@ -365,7 +397,11 @@ fn key_input(
let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId)); let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId));
if let Some(mut key_context) = keyboard_state.xkb_context.key_context() { if let Some(mut key_context) = keyboard_state.xkb_context.key_context() {
let event = key_context.process_key_event(keycode, state, repeat); let event = key_context.process_key_event(keycode, state, repeat);
let event = WindowEvent::KeyboardInput { device_id, event, is_synthetic: false }; let event = WindowEvent::KeyboardInput {
device_id,
event,
is_synthetic: false,
};
event_sink.push_window_event(event, window_id); event_sink.push_window_event(event, window_id);
} }
} }

View File

@@ -81,12 +81,12 @@ impl SeatHandler for WinitState {
match capability { match capability {
SeatCapability::Touch if seat_state.touch.is_none() => { SeatCapability::Touch if seat_state.touch.is_none() => {
seat_state.touch = self.seat_state.get_touch(queue_handle, &seat).ok(); seat_state.touch = self.seat_state.get_touch(queue_handle, &seat).ok();
}, }
SeatCapability::Keyboard if seat_state.keyboard_state.is_none() => { SeatCapability::Keyboard if seat_state.keyboard_state.is_none() => {
let keyboard = seat.get_keyboard(queue_handle, KeyboardData::new(seat.clone())); let keyboard = seat.get_keyboard(queue_handle, KeyboardData::new(seat.clone()));
seat_state.keyboard_state = seat_state.keyboard_state =
Some(KeyboardState::new(keyboard, self.loop_handle.clone())); Some(KeyboardState::new(keyboard, self.loop_handle.clone()));
}, }
SeatCapability::Pointer if seat_state.pointer.is_none() => { SeatCapability::Pointer if seat_state.pointer.is_none() => {
let surface = self.compositor_state.create_surface(queue_handle); let surface = self.compositor_state.create_surface(queue_handle);
let surface_id = surface.id(); let surface_id = surface.id();
@@ -114,15 +114,19 @@ impl SeatHandler for WinitState {
let themed_pointer = Arc::new(themed_pointer); let themed_pointer = Arc::new(themed_pointer);
// Register cursor surface. // Register cursor surface.
self.pointer_surfaces.insert(surface_id, themed_pointer.clone()); self.pointer_surfaces
.insert(surface_id, themed_pointer.clone());
seat_state.pointer = Some(themed_pointer); seat_state.pointer = Some(themed_pointer);
}, }
_ => (), _ => (),
} }
if let Some(text_input_state) = if let Some(text_input_state) = seat_state
seat_state.text_input.is_none().then_some(self.text_input_state.as_ref()).flatten() .text_input
.is_none()
.then_some(self.text_input_state.as_ref())
.flatten()
{ {
seat_state.text_input = Some(Arc::new(text_input_state.get_text_input( seat_state.text_input = Some(Arc::new(text_input_state.get_text_input(
&seat, &seat,
@@ -152,7 +156,7 @@ impl SeatHandler for WinitState {
touch.release(); touch.release();
} }
} }
}, }
SeatCapability::Pointer => { SeatCapability::Pointer => {
if let Some(relative_pointer) = seat_state.relative_pointer.take() { if let Some(relative_pointer) = seat_state.relative_pointer.take() {
relative_pointer.destroy(); relative_pointer.destroy();
@@ -173,11 +177,11 @@ impl SeatHandler for WinitState {
pointer.pointer().release(); pointer.pointer().release();
} }
} }
}, }
SeatCapability::Keyboard => { SeatCapability::Keyboard => {
seat_state.keyboard_state = None; seat_state.keyboard_state = None;
self.on_keyboard_destroy(&seat.id()); self.on_keyboard_destroy(&seat.id());
}, }
_ => (), _ => (),
} }
} }
@@ -209,7 +213,8 @@ impl WinitState {
let had_focus = window.has_focus(); let had_focus = window.has_focus();
window.remove_seat_focus(seat); window.remove_seat_focus(seat);
if had_focus != window.has_focus() { if had_focus != window.has_focus() {
self.events_sink.push_window_event(WindowEvent::Focused(false), *window_id); self.events_sink
.push_window_event(WindowEvent::Focused(false), *window_id);
} }
} }
} }

View File

@@ -19,9 +19,8 @@ use sctk::reexports::csd_frame::FrameClick;
use sctk::compositor::SurfaceData; use sctk::compositor::SurfaceData;
use sctk::globals::GlobalData; use sctk::globals::GlobalData;
use sctk::seat::pointer::{ use sctk::seat::pointer::{PointerData, PointerDataExt};
PointerData, PointerDataExt, PointerEvent, PointerEventKind, PointerHandler, use sctk::seat::pointer::{PointerEvent, PointerEventKind, PointerHandler};
};
use sctk::seat::SeatState; use sctk::seat::SeatState;
use crate::dpi::{LogicalPosition, PhysicalPosition}; use crate::dpi::{LogicalPosition, PhysicalPosition};
@@ -82,14 +81,20 @@ impl PointerHandler for WinitState {
let _ = pointer.set_cursor(connection, icon); let _ = pointer.set_cursor(connection, icon);
} }
} }
}, }
PointerEventKind::Leave { .. } if parent_surface != surface => { PointerEventKind::Leave { .. } if parent_surface != surface => {
window.frame_point_left(); window.frame_point_left();
}, }
ref kind @ PointerEventKind::Press { button, serial, time } ref kind @ PointerEventKind::Press {
| ref kind @ PointerEventKind::Release { button, serial, time } button,
if parent_surface != surface => serial,
{ time,
}
| ref kind @ PointerEventKind::Release {
button,
serial,
time,
} if parent_surface != surface => {
let click = match wayland_button_to_winit(button) { let click = match wayland_button_to_winit(button) {
MouseButton::Left => FrameClick::Normal, MouseButton::Left => FrameClick::Normal,
MouseButton::Right => FrameClick::Alternate, MouseButton::Right => FrameClick::Alternate,
@@ -107,7 +112,7 @@ impl PointerHandler for WinitState {
window_id, window_id,
&mut self.window_compositor_updates, &mut self.window_compositor_updates,
); );
}, }
// Regular events on the main surface. // Regular events on the main surface.
PointerEventKind::Enter { .. } => { PointerEventKind::Enter { .. } => {
self.events_sink self.events_sink
@@ -121,10 +126,13 @@ impl PointerHandler for WinitState {
pointer.winit_data().inner.lock().unwrap().surface = Some(window_id); pointer.winit_data().inner.lock().unwrap().surface = Some(window_id);
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::CursorMoved { device_id, position }, WindowEvent::CursorMoved {
device_id,
position,
},
window_id, window_id,
); );
}, }
PointerEventKind::Leave { .. } => { PointerEventKind::Leave { .. } => {
if let Some(pointer) = seat_state.pointer.as_ref().map(Arc::downgrade) { if let Some(pointer) = seat_state.pointer.as_ref().map(Arc::downgrade) {
window.pointer_left(pointer); window.pointer_left(pointer);
@@ -135,17 +143,25 @@ impl PointerHandler for WinitState {
self.events_sink self.events_sink
.push_window_event(WindowEvent::CursorLeft { device_id }, window_id); .push_window_event(WindowEvent::CursorLeft { device_id }, window_id);
}, }
PointerEventKind::Motion { .. } => { PointerEventKind::Motion { .. } => {
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::CursorMoved { device_id, position }, WindowEvent::CursorMoved {
device_id,
position,
},
window_id, window_id,
); );
}, }
ref kind @ PointerEventKind::Press { button, serial, .. } ref kind @ PointerEventKind::Press { button, serial, .. }
| ref kind @ PointerEventKind::Release { button, serial, .. } => { | ref kind @ PointerEventKind::Release { button, serial, .. } => {
// Update the last button serial. // Update the last button serial.
pointer.winit_data().inner.lock().unwrap().latest_button_serial = serial; pointer
.winit_data()
.inner
.lock()
.unwrap()
.latest_button_serial = serial;
let button = wayland_button_to_winit(button); let button = wayland_button_to_winit(button);
let state = if matches!(kind, PointerEventKind::Press { .. }) { let state = if matches!(kind, PointerEventKind::Press { .. }) {
@@ -154,11 +170,19 @@ impl PointerHandler for WinitState {
ElementState::Released ElementState::Released
}; };
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::MouseInput { device_id, state, button }, WindowEvent::MouseInput {
device_id,
state,
button,
},
window_id, window_id,
); );
}, }
PointerEventKind::Axis { horizontal, vertical, .. } => { PointerEventKind::Axis {
horizontal,
vertical,
..
} => {
// Get the current phase. // Get the current phase.
let mut pointer_data = pointer.winit_data().inner.lock().unwrap(); let mut pointer_data = pointer.winit_data().inner.lock().unwrap();
@@ -199,10 +223,14 @@ impl PointerHandler for WinitState {
}; };
self.events_sink.push_window_event( self.events_sink.push_window_event(
WindowEvent::MouseWheel { device_id, delta, phase }, WindowEvent::MouseWheel {
device_id,
delta,
phase,
},
window_id, window_id,
) )
}, }
} }
} }
} }
@@ -379,7 +407,8 @@ pub trait WinitPointerDataExt {
impl WinitPointerDataExt for WlPointer { impl WinitPointerDataExt for WlPointer {
fn winit_data(&self) -> &WinitPointerData { fn winit_data(&self) -> &WinitPointerData {
self.data::<WinitPointerData>().expect("failed to get pointer data.") self.data::<WinitPointerData>()
.expect("failed to get pointer data.")
} }
} }
@@ -393,13 +422,14 @@ impl PointerConstraintsState {
queue_handle: &QueueHandle<WinitState>, queue_handle: &QueueHandle<WinitState>,
) -> Result<Self, BindError> { ) -> Result<Self, BindError> {
let pointer_constraints = globals.bind(queue_handle, 1..=1, GlobalData)?; let pointer_constraints = globals.bind(queue_handle, 1..=1, GlobalData)?;
Ok(Self { pointer_constraints }) Ok(Self {
pointer_constraints,
})
} }
} }
impl Deref for PointerConstraintsState { impl Deref for PointerConstraintsState {
type Target = ZwpPointerConstraintsV1; type Target = ZwpPointerConstraintsV1;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.pointer_constraints &self.pointer_constraints
} }

Some files were not shown because too many files have changed in this diff Show More