Compare commits
2 Commits
master
...
kchibisov/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d39dbf4a0 | ||
|
|
8e3951636a |
31
.github/CODEOWNERS
vendored
@@ -1,26 +1,31 @@
|
||||
# Android
|
||||
/winit-android @MarijnS95
|
||||
/src/platform/android.rs @MarijnS95
|
||||
/src/platform_impl/android @MarijnS95
|
||||
|
||||
# Apple (AppKit + UIKit)
|
||||
/winit-appkit @madsmtm
|
||||
/winit-uikit @madsmtm
|
||||
/winit-common/src/core_foundation @madsmtm
|
||||
/winit-common/src/event_handler.rs @madsmtm
|
||||
/src/platform/ios.rs @madsmtm
|
||||
/src/platform/macos.rs @madsmtm
|
||||
/src/platform_impl/apple @madsmtm
|
||||
|
||||
# XKB
|
||||
/winit-common/src/xkb @kchibisov
|
||||
# Unix
|
||||
/src/platform_impl/linux/mod.rs @kchibisov
|
||||
|
||||
# Wayland
|
||||
/winit-wayland @kchibisov
|
||||
/src/platform/wayland.rs @kchibisov
|
||||
/src/platform_impl/linux/wayland @kchibisov
|
||||
|
||||
# X11
|
||||
/winit-x11 @kchibisov
|
||||
/src/platform/x11.rs @kchibisov @notgull
|
||||
/src/platform_impl/linux/x11 @kchibisov @notgull
|
||||
|
||||
# Web
|
||||
/winit-web @daxpedda
|
||||
/src/platform/web.rs @daxpedda
|
||||
/src/platform_impl/web @daxpedda
|
||||
|
||||
# Windows (Win32) (UNOWNED)
|
||||
#/winit-win32
|
||||
# Windows
|
||||
/src/platform/windows.rs @notgull
|
||||
/src/platform_impl/windows @notgull
|
||||
|
||||
# Orbital (Redox OS)
|
||||
/winit-orbital @jackpot51
|
||||
/src/platform/orbital.rs @jackpot51
|
||||
/src/platform_impl/orbital @jackpot51
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
name: iOS bug
|
||||
description: Create an iOS/UIKit-specific bug report
|
||||
description: Create an iOS-specific bug report
|
||||
labels:
|
||||
- B - bug
|
||||
- DS - uikit
|
||||
- DS - ios
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
@@ -1,8 +1,8 @@
|
||||
name: macOS bug
|
||||
name: MacOS bug
|
||||
description: Create a macOS-specific bug report
|
||||
labels:
|
||||
- B - bug
|
||||
- DS - appkit
|
||||
- DS - macos
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
@@ -2,7 +2,7 @@ name: Windows bug
|
||||
description: Create a Windows-specific bug report
|
||||
labels:
|
||||
- B - bug
|
||||
- DS - win32
|
||||
- DS - windows
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
107
.github/workflows/ci.yml
vendored
@@ -55,7 +55,7 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
toolchain: [stable, nightly, '1.85']
|
||||
toolchain: [stable, nightly, '1.80']
|
||||
platform:
|
||||
# Note: Make sure that we test all the `docs.rs` targets defined in Cargo.toml!
|
||||
- { name: 'Windows 64bit MSVC', target: x86_64-pc-windows-msvc, os: windows-latest, }
|
||||
@@ -66,7 +66,7 @@ jobs:
|
||||
- { name: 'Linux 64bit', target: x86_64-unknown-linux-gnu, os: ubuntu-latest, }
|
||||
- { name: 'X11', target: x86_64-unknown-linux-gnu, os: ubuntu-latest, options: '--no-default-features --features=x11' }
|
||||
- { name: 'Wayland', target: x86_64-unknown-linux-gnu, os: ubuntu-latest, options: '--no-default-features --features=wayland,wayland-dlopen' }
|
||||
- { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package winit --features=android-native-activity', cmd: 'apk -- ' }
|
||||
- { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package=winit --features=android-native-activity', cmd: 'apk --' }
|
||||
- { name: 'Redox OS', target: x86_64-unknown-redox, os: ubuntu-latest, }
|
||||
- { name: 'macOS x86_64', target: x86_64-apple-darwin, os: macos-latest, }
|
||||
- { name: 'macOS Aarch64', target: aarch64-apple-darwin, os: macos-latest, }
|
||||
@@ -82,14 +82,14 @@ jobs:
|
||||
- toolchain: nightly
|
||||
platform: { name: 'Windows 32bit GNU' }
|
||||
# Android is tested on stable-3
|
||||
- toolchain: '1.85'
|
||||
- toolchain: '1.80'
|
||||
platform: { name: 'Android' }
|
||||
# Redox OS doesn't follow MSRV
|
||||
- toolchain: '1.85'
|
||||
- toolchain: '1.80'
|
||||
platform: { name: 'Redox OS' }
|
||||
include:
|
||||
- toolchain: '1.85'
|
||||
platform: { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package winit --features=android-native-activity', cmd: 'apk -- ' }
|
||||
- toolchain: '1.80'
|
||||
platform: { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package=winit --features=android-native-activity', cmd: 'apk --' }
|
||||
- toolchain: 'nightly'
|
||||
platform: { name: 'Web', target: wasm32-unknown-unknown, os: ubuntu-latest, test-options: -Zdoctest-xcompile }
|
||||
- toolchain: 'nightly'
|
||||
@@ -124,7 +124,7 @@ jobs:
|
||||
# the cache has been downloaded.
|
||||
#
|
||||
# This could be avoided if we added Cargo.lock to the repository.
|
||||
uses: actions/cache/restore@v5
|
||||
uses: actions/cache/restore@v4
|
||||
with:
|
||||
# https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci
|
||||
path: |
|
||||
@@ -136,7 +136,7 @@ jobs:
|
||||
|
||||
- name: Generate lockfile
|
||||
# Also updates the crates.io index
|
||||
run: cargo generate-lockfile && cargo update -p smol_str --precise 0.3.2 && cargo update -p unicode-segmentation --precise 1.12.0 && cargo update -p wayland-protocols --precise 0.32.12
|
||||
run: cargo generate-lockfile && cargo update -p ahash --precise 0.8.7 && cargo update -p bumpalo --precise 3.14.0
|
||||
|
||||
- name: Install GCC Multilib
|
||||
if: (matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')
|
||||
@@ -145,7 +145,7 @@ jobs:
|
||||
- name: Cache cargo-apk
|
||||
if: contains(matrix.platform.target, 'android')
|
||||
id: cargo-apk-cache
|
||||
uses: actions/cache@v5
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ~/.cargo/bin/cargo-apk
|
||||
# Change this key if we update the required cargo-apk version
|
||||
@@ -160,7 +160,7 @@ jobs:
|
||||
if: contains(matrix.platform.target, 'android') && (steps.cargo-apk-cache.outputs.cache-hit != 'true')
|
||||
run: cargo install cargo-apk --version=^0.9.7 --locked
|
||||
|
||||
- uses: taiki-e/cache-cargo-install-action@v3
|
||||
- uses: taiki-e/cache-cargo-install-action@v2
|
||||
if: contains(matrix.platform.target, 'wasm32') && matrix.toolchain == 'nightly'
|
||||
with:
|
||||
tool: wasm-bindgen-cli
|
||||
@@ -179,70 +179,23 @@ jobs:
|
||||
- name: Build crate
|
||||
run: cargo $CMD build $OPTIONS
|
||||
|
||||
- name: Test winit core
|
||||
run: cargo test -p winit-core
|
||||
|
||||
- name: Test winit Android
|
||||
if: contains(matrix.platform.target, 'android')
|
||||
run: cargo $CMD test -p winit-android --features native-activity --no-run
|
||||
|
||||
- name: Test winit Common (EventHandler)
|
||||
run: cargo $CMD test -p winit-common --features event-handler --no-run
|
||||
|
||||
- name: Test winit Common (CF)
|
||||
if: contains(matrix.platform.target, 'apple')
|
||||
run: cargo $CMD test -p winit-common --features core-foundation --no-run
|
||||
|
||||
- name: Test winit Common (XKB)
|
||||
if: contains(matrix.platform.target, 'linux-gnu')
|
||||
run: cargo $CMD test -p winit-common --features xkb,x11,wayland --no-run
|
||||
|
||||
- name: Test winit AppKit
|
||||
if: contains(matrix.platform.target, 'macos')
|
||||
run: cargo $CMD test -p winit-appkit $OPTIONS
|
||||
|
||||
- name: Test winit Orbital
|
||||
if: contains(matrix.platform.target, 'redox')
|
||||
run: cargo test -p winit-orbital
|
||||
|
||||
- name: Test winit UIKit
|
||||
if: contains(matrix.platform.target, 'ios')
|
||||
# TODO: Run on Simulator
|
||||
run: cargo $CMD test -p winit-uikit $OPTIONS --no-run
|
||||
|
||||
- name: Test winit Web
|
||||
if: contains(matrix.platform.target, 'wasm')
|
||||
run: cargo $CMD test -p winit-web $OPTIONS --no-run
|
||||
|
||||
- name: Test winit Win32
|
||||
if: contains(matrix.platform.target, 'windows')
|
||||
run: cargo $CMD test -p winit-win32 $OPTIONS
|
||||
|
||||
- name: Test winit X11
|
||||
if: contains(matrix.platform.target, 'linux-gnu')
|
||||
run: cargo $CMD test -p winit-x11 --target=${{ matrix.platform.target }}
|
||||
|
||||
- name: Test winit Wayland
|
||||
if: contains(matrix.platform.target, 'linux-gnu')
|
||||
run: cargo $CMD test -p winit-wayland --target=${{ matrix.platform.target }}
|
||||
|
||||
# Test only on Linux x86_64, so we avoid spending unnecessary CI hours.
|
||||
- name: Test dpi crate
|
||||
if: >
|
||||
contains(matrix.platform.name, 'Linux 64bit') &&
|
||||
matrix.toolchain != '1.85'
|
||||
matrix.toolchain != '1.80'
|
||||
run: cargo test -p dpi
|
||||
|
||||
|
||||
- name: Check dpi crate (no_std)
|
||||
if: >
|
||||
contains(matrix.platform.name, 'Linux 64bit') &&
|
||||
matrix.toolchain != '1.85'
|
||||
matrix.toolchain != '1.80'
|
||||
run: cargo check -p dpi --no-default-features
|
||||
|
||||
- name: Build tests
|
||||
if: >
|
||||
!contains(matrix.platform.target, 'redox') &&
|
||||
matrix.toolchain != '1.85'
|
||||
matrix.toolchain != '1.80'
|
||||
run: cargo $CMD test --no-run $OPTIONS
|
||||
|
||||
- name: Run tests
|
||||
@@ -251,7 +204,7 @@ jobs:
|
||||
!contains(matrix.platform.target, 'ios') &&
|
||||
(!contains(matrix.platform.target, 'wasm32') || matrix.toolchain == 'nightly') &&
|
||||
!contains(matrix.platform.target, 'redox') &&
|
||||
matrix.toolchain != '1.85'
|
||||
matrix.toolchain != '1.80'
|
||||
run: cargo $CMD test $OPTIONS
|
||||
|
||||
- name: Lint with clippy
|
||||
@@ -261,7 +214,7 @@ jobs:
|
||||
- name: Build tests with serde enabled
|
||||
if: >
|
||||
!contains(matrix.platform.target, 'redox') &&
|
||||
matrix.toolchain != '1.85'
|
||||
matrix.toolchain != '1.80'
|
||||
run: cargo $CMD test --no-run $OPTIONS $TEST_OPTIONS --features serde
|
||||
|
||||
- name: Run tests with serde enabled
|
||||
@@ -270,7 +223,7 @@ jobs:
|
||||
!contains(matrix.platform.target, 'ios') &&
|
||||
(!contains(matrix.platform.target, 'wasm32') || matrix.toolchain == 'nightly') &&
|
||||
!contains(matrix.platform.target, 'redox') &&
|
||||
matrix.toolchain != '1.85'
|
||||
matrix.toolchain != '1.80'
|
||||
run: cargo $CMD test $OPTIONS $TEST_OPTIONS --features serde
|
||||
|
||||
- name: Check docs.rs documentation
|
||||
@@ -281,7 +234,7 @@ jobs:
|
||||
|
||||
# See restore step above
|
||||
- name: Save cache of cargo folder
|
||||
uses: actions/cache/save@v5
|
||||
uses: actions/cache/save@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/registry/index/
|
||||
@@ -290,14 +243,30 @@ jobs:
|
||||
key: cargo-${{ matrix.toolchain }}-${{ matrix.platform.name }}-${{ hashFiles('Cargo.lock') }}
|
||||
|
||||
cargo-deny:
|
||||
name: Run cargo-deny
|
||||
name: Run cargo-deny on ${{ matrix.platform.name }}
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
# TODO: remove this matrix when https://github.com/EmbarkStudios/cargo-deny/issues/324 is resolved
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
platform:
|
||||
- { name: 'Android', target: aarch64-linux-android }
|
||||
- { name: 'iOS', target: aarch64-apple-ios }
|
||||
- { name: 'Linux', target: x86_64-unknown-linux-gnu }
|
||||
- { name: 'macOS', target: aarch64-apple-darwin }
|
||||
- { name: 'Redox OS', target: x86_64-unknown-redox }
|
||||
- { name: 'Web', target: wasm32-unknown-unknown }
|
||||
- { name: 'Windows GNU', target: x86_64-pc-windows-gnu }
|
||||
- { name: 'Windows MSVC', target: x86_64-pc-windows-msvc }
|
||||
|
||||
steps:
|
||||
- uses: taiki-e/checkout-action@v1
|
||||
- uses: EmbarkStudios/cargo-deny-action@v2
|
||||
with:
|
||||
command: check
|
||||
log-level: error
|
||||
arguments: --all-features --target ${{ matrix.platform.target }}
|
||||
|
||||
eslint:
|
||||
name: ESLint
|
||||
@@ -305,14 +274,14 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ./winit-web/src/script
|
||||
working-directory: ./src/platform_impl/web/script
|
||||
|
||||
steps:
|
||||
- uses: taiki-e/checkout-action@v1
|
||||
- name: Setup NPM
|
||||
run: npm install
|
||||
- name: Run ESLint
|
||||
run: npx eslint@9.38.0
|
||||
run: npx eslint
|
||||
|
||||
swc:
|
||||
name: Minimize JavaScript
|
||||
@@ -320,7 +289,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ./winit-web/src/script
|
||||
working-directory: ./src/platform_impl/web/script
|
||||
|
||||
steps:
|
||||
- uses: taiki-e/checkout-action@v1
|
||||
|
||||
8
.github/workflows/docs.yml
vendored
@@ -19,7 +19,7 @@ jobs:
|
||||
id-token: write
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
@@ -32,7 +32,7 @@ jobs:
|
||||
cargo doc --no-deps -Z rustdoc-map -Z rustdoc-scrape-examples --features=serde,mint,android-native-activity
|
||||
|
||||
- name: Setup Pages
|
||||
uses: actions/configure-pages@v6
|
||||
uses: actions/configure-pages@v5
|
||||
|
||||
- name: Fix permissions
|
||||
run: |
|
||||
@@ -41,10 +41,10 @@ jobs:
|
||||
done
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-pages-artifact@v4
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
path: target/doc
|
||||
|
||||
- name: Deploy to GitHub Pages
|
||||
id: deployment
|
||||
uses: actions/deploy-pages@v5
|
||||
uses: actions/deploy-pages@v4
|
||||
|
||||
@@ -3,6 +3,6 @@ Changelog entries should be put in the [`changelog::unreleased`].
|
||||
The changelog can also be viewed [on docs.rs][docs_rs] or [on the current
|
||||
master docs][master_docs].
|
||||
|
||||
[`changelog::unreleased`]: winit/src/changelog/unreleased.md
|
||||
[`changelog::unreleased`]: src/changelog/unreleased.md
|
||||
[docs_rs]: https://docs.rs/winit/latest/winit/changelog/index.html
|
||||
[master_docs]: https://rust-windowing.github.io/winit/winit/changelog/index.html
|
||||
|
||||
@@ -13,13 +13,6 @@ To save your time it's wise to check already opened [pull requests][prs] and
|
||||
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.
|
||||
|
||||
### Windows
|
||||
|
||||
To run the examples on Windows, you must have symlinks enabled, see
|
||||
[this][git-windows-symlinks].
|
||||
|
||||
[git-windows-symlinks]: https://gitforwindows.org/symbolic-links.html
|
||||
|
||||
### Submitting your work and handling review
|
||||
|
||||
All patches have to be sent on Github as [pull requests][prs]. To simplify your
|
||||
|
||||
432
Cargo.toml
@@ -1,97 +1,403 @@
|
||||
[workspace]
|
||||
default-members = ["winit"]
|
||||
members = ["dpi", "winit*"]
|
||||
resolver = "2"
|
||||
[package]
|
||||
authors = ["The winit contributors", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
|
||||
categories = ["gui"]
|
||||
description = "Cross-platform window creation library."
|
||||
documentation = "https://docs.rs/winit"
|
||||
edition.workspace = true
|
||||
include = [
|
||||
"/build.rs",
|
||||
"/docs",
|
||||
"/examples",
|
||||
"/FEATURES.md",
|
||||
"/LICENSE",
|
||||
"/src",
|
||||
"!/src/platform_impl/web/script",
|
||||
"/src/platform_impl/web/script/**/*.min.js",
|
||||
"/tests",
|
||||
]
|
||||
keywords = ["windowing"]
|
||||
license.workspace = true
|
||||
name = "winit"
|
||||
readme = "README.md"
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version = "0.30.10"
|
||||
|
||||
[workspace.package]
|
||||
edition = "2024"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/rust-windowing/winit"
|
||||
rust-version = "1.85"
|
||||
version = "0.31.0-beta.2"
|
||||
[package.metadata.docs.rs]
|
||||
features = [
|
||||
"serde",
|
||||
"mint",
|
||||
# Enabled to get docs to compile
|
||||
"android-native-activity",
|
||||
]
|
||||
# These are all tested in CI
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
targets = [
|
||||
# Windows
|
||||
"i686-pc-windows-msvc",
|
||||
"x86_64-pc-windows-msvc",
|
||||
# macOS
|
||||
"aarch64-apple-darwin",
|
||||
"x86_64-apple-darwin",
|
||||
# Unix (X11 & Wayland)
|
||||
"i686-unknown-linux-gnu",
|
||||
"x86_64-unknown-linux-gnu",
|
||||
# iOS
|
||||
"aarch64-apple-ios",
|
||||
# Android
|
||||
"aarch64-linux-android",
|
||||
# Web
|
||||
"wasm32-unknown-unknown",
|
||||
]
|
||||
|
||||
[workspace.dependencies]
|
||||
# Workspace dependencies.
|
||||
# `winit` has no version here to allow using it in dev deps for docs.
|
||||
winit = { path = "winit" }
|
||||
winit-android = { version = "=0.31.0-beta.2", path = "winit-android" }
|
||||
winit-appkit = { version = "=0.31.0-beta.2", path = "winit-appkit" }
|
||||
winit-common = { version = "=0.31.0-beta.2", path = "winit-common" }
|
||||
winit-core = { version = "=0.31.0-beta.2", path = "winit-core" }
|
||||
winit-orbital = { version = "=0.31.0-beta.2", path = "winit-orbital" }
|
||||
winit-uikit = { version = "=0.31.0-beta.2", path = "winit-uikit" }
|
||||
winit-wayland = { version = "=0.31.0-beta.2", path = "winit-wayland", default-features = false }
|
||||
winit-web = { version = "=0.31.0-beta.2", path = "winit-web" }
|
||||
winit-win32 = { version = "=0.31.0-beta.2", path = "winit-win32" }
|
||||
winit-x11 = { version = "=0.31.0-beta.2", path = "winit-x11" }
|
||||
# Features are documented in either `lib.rs` or under `winit::platform`.
|
||||
[features]
|
||||
android-game-activity = ["android-activity/game-activity"]
|
||||
android-native-activity = ["android-activity/native-activity"]
|
||||
default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
|
||||
mint = ["dpi/mint"]
|
||||
serde = ["dep:serde", "cursor-icon/serde", "smol_str/serde", "dpi/serde", "bitflags/serde"]
|
||||
wayland = [
|
||||
"wayland-client",
|
||||
"wayland-backend",
|
||||
"wayland-protocols",
|
||||
"wayland-protocols-plasma",
|
||||
"sctk",
|
||||
"ahash",
|
||||
"memmap2",
|
||||
]
|
||||
wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"]
|
||||
wayland-csd-adwaita-crossfont = ["sctk-adwaita", "sctk-adwaita/crossfont"]
|
||||
wayland-csd-adwaita-notitle = ["sctk-adwaita"]
|
||||
wayland-dlopen = ["wayland-backend/dlopen"]
|
||||
x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"]
|
||||
|
||||
# Core dependencies.
|
||||
bitflags = "2"
|
||||
[build-dependencies]
|
||||
cfg_aliases = "0.2.1"
|
||||
|
||||
[dependencies]
|
||||
bitflags = "2"
|
||||
cursor-icon = "1.1.0"
|
||||
dpi = { version = "0.1.2", path = "dpi" }
|
||||
keyboard-types = "0.8.0"
|
||||
mint = "0.5.6"
|
||||
rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"] }
|
||||
serde = { version = "1", features = ["serde_derive"] }
|
||||
serde = { workspace = true, optional = true }
|
||||
smol_str = "0.3"
|
||||
tracing = { version = "0.1.40", default-features = false }
|
||||
|
||||
# Dev dependencies.
|
||||
image = { version = "0.25.0", default-features = false }
|
||||
softbuffer = { version = "0.4.8", default-features = false, features = [
|
||||
[dev-dependencies]
|
||||
image = { version = "0.25.0", default-features = false, features = ["png"] }
|
||||
tracing = { version = "0.1.40", default-features = false, features = ["log"] }
|
||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
|
||||
|
||||
[target.'cfg(not(target_os = "android"))'.dev-dependencies]
|
||||
softbuffer = { version = "0.4.6", default-features = false, features = [
|
||||
"x11",
|
||||
"x11-dlopen",
|
||||
"wayland",
|
||||
"wayland-dlopen",
|
||||
] }
|
||||
tracing-subscriber = "0.3.18"
|
||||
|
||||
# Android dependencies.
|
||||
# Android
|
||||
[target.'cfg(target_os = "android")'.dependencies]
|
||||
android-activity = "0.6.0"
|
||||
ndk = { version = "0.9.0", features = ["rwh_06"], default-features = false }
|
||||
|
||||
# Apple dependencies.
|
||||
# AppKit or UIKit
|
||||
[target.'cfg(target_vendor = "apple")'.dependencies]
|
||||
block2 = "0.6.1"
|
||||
dispatch2 = { version = "0.3.0", default-features = false, features = ["std", "objc2"] }
|
||||
objc2 = { version = "0.6.1", features = ["relax-sign-encoding"] }
|
||||
objc2-app-kit = { version = "0.3.2", default-features = false }
|
||||
objc2-core-foundation = { version = "0.3.2", default-features = false }
|
||||
objc2-core-graphics = { version = "0.3.2", default-features = false }
|
||||
objc2-core-video = { version = "0.3.2", default-features = false }
|
||||
objc2-foundation = { version = "0.3.2", default-features = false }
|
||||
objc2-ui-kit = { version = "0.3.2", default-features = false }
|
||||
objc2 = "0.6.1"
|
||||
|
||||
# Windows dependencies.
|
||||
# AppKit
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
objc2-app-kit = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"objc2-core-foundation",
|
||||
"NSAppearance",
|
||||
"NSApplication",
|
||||
"NSBitmapImageRep",
|
||||
"NSButton",
|
||||
"NSColor",
|
||||
"NSControl",
|
||||
"NSCursor",
|
||||
"NSDragging",
|
||||
"NSEvent",
|
||||
"NSGraphics",
|
||||
"NSGraphicsContext",
|
||||
"NSImage",
|
||||
"NSImageRep",
|
||||
"NSMenu",
|
||||
"NSMenuItem",
|
||||
"NSOpenGLView",
|
||||
"NSPanel",
|
||||
"NSPasteboard",
|
||||
"NSResponder",
|
||||
"NSRunningApplication",
|
||||
"NSScreen",
|
||||
"NSTextInputClient",
|
||||
"NSTextInputContext",
|
||||
"NSToolbar",
|
||||
"NSView",
|
||||
"NSWindow",
|
||||
"NSWindowScripting",
|
||||
"NSWindowTabGroup",
|
||||
] }
|
||||
objc2-core-foundation = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"block2",
|
||||
"CFBase",
|
||||
"CFCGTypes",
|
||||
"CFData",
|
||||
"CFRunLoop",
|
||||
"CFString",
|
||||
"CFUUID",
|
||||
] }
|
||||
objc2-core-graphics = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"libc",
|
||||
"CGDirectDisplay",
|
||||
"CGDisplayConfiguration",
|
||||
"CGDisplayFade",
|
||||
"CGError",
|
||||
"CGRemoteOperation",
|
||||
"CGWindowLevel",
|
||||
] }
|
||||
objc2-core-video = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"objc2-core-graphics",
|
||||
"CVBase",
|
||||
"CVReturn",
|
||||
"CVDisplayLink",
|
||||
] }
|
||||
objc2-foundation = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"block2",
|
||||
"objc2-core-foundation",
|
||||
"NSArray",
|
||||
"NSAttributedString",
|
||||
"NSData",
|
||||
"NSDictionary",
|
||||
"NSDistributedNotificationCenter",
|
||||
"NSEnumerator",
|
||||
"NSGeometry",
|
||||
"NSKeyValueObserving",
|
||||
"NSNotification",
|
||||
"NSObjCRuntime",
|
||||
"NSOperation",
|
||||
"NSPathUtilities",
|
||||
"NSProcessInfo",
|
||||
"NSRunLoop",
|
||||
"NSString",
|
||||
"NSThread",
|
||||
"NSValue",
|
||||
] }
|
||||
|
||||
# UIKit
|
||||
[target.'cfg(all(target_vendor = "apple", not(target_os = "macos")))'.dependencies]
|
||||
objc2-core-foundation = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"CFCGTypes",
|
||||
"CFBase",
|
||||
"CFRunLoop",
|
||||
"CFString",
|
||||
] }
|
||||
objc2-foundation = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"block2",
|
||||
"objc2-core-foundation",
|
||||
"NSArray",
|
||||
"NSEnumerator",
|
||||
"NSGeometry",
|
||||
"NSObjCRuntime",
|
||||
"NSOperation",
|
||||
"NSString",
|
||||
"NSThread",
|
||||
"NSSet",
|
||||
] }
|
||||
objc2-ui-kit = { version = "0.3.1", default-features = false, features = [
|
||||
"std",
|
||||
"objc2-core-foundation",
|
||||
"UIApplication",
|
||||
"UIDevice",
|
||||
"UIEvent",
|
||||
"UIGeometry",
|
||||
"UIGestureRecognizer",
|
||||
"UITextInput",
|
||||
"UITextInputTraits",
|
||||
"UIOrientation",
|
||||
"UIPanGestureRecognizer",
|
||||
"UIPinchGestureRecognizer",
|
||||
"UIResponder",
|
||||
"UIRotationGestureRecognizer",
|
||||
"UIScreen",
|
||||
"UIScreenMode",
|
||||
"UITapGestureRecognizer",
|
||||
"UITouch",
|
||||
"UITraitCollection",
|
||||
"UIView",
|
||||
"UIViewController",
|
||||
"UIWindow",
|
||||
] }
|
||||
|
||||
# Windows
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
unicode-segmentation = "1.7.1"
|
||||
windows-sys = "0.61"
|
||||
windows-sys = { version = "0.59.0", features = [
|
||||
"Win32_Devices_HumanInterfaceDevice",
|
||||
"Win32_Foundation",
|
||||
"Win32_Globalization",
|
||||
"Win32_Graphics_Dwm",
|
||||
"Win32_Graphics_Gdi",
|
||||
"Win32_Media",
|
||||
"Win32_System_Com_StructuredStorage",
|
||||
"Win32_System_Com",
|
||||
"Win32_System_LibraryLoader",
|
||||
"Win32_System_Ole",
|
||||
"Win32_Security",
|
||||
"Win32_System_SystemInformation",
|
||||
"Win32_System_SystemServices",
|
||||
"Win32_System_Threading",
|
||||
"Win32_System_WindowsProgramming",
|
||||
"Win32_UI_Accessibility",
|
||||
"Win32_UI_Controls",
|
||||
"Win32_UI_HiDpi",
|
||||
"Win32_UI_Input_Ime",
|
||||
"Win32_UI_Input_KeyboardAndMouse",
|
||||
"Win32_UI_Input_Pointer",
|
||||
"Win32_UI_Input_Touch",
|
||||
"Win32_UI_Shell",
|
||||
"Win32_UI_TextServices",
|
||||
"Win32_UI_WindowsAndMessaging",
|
||||
] }
|
||||
|
||||
# Linux dependencies.
|
||||
bytemuck = { version = "1.13.1", default-features = false }
|
||||
calloop = "0.14.3"
|
||||
foldhash = { version = "0.2.0", default-features = false, features = ["std"] }
|
||||
# Linux
|
||||
[target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_vendor = "apple"))))'.dependencies]
|
||||
ahash = { version = "0.8.7", features = ["no-rng"], optional = true }
|
||||
bytemuck = { version = "1.13.1", default-features = false, optional = true }
|
||||
calloop = "0.13.0"
|
||||
libc = "0.2.64"
|
||||
memmap2 = "0.9.0"
|
||||
percent-encoding = "2.0"
|
||||
rustix = { version = "1.0.7", default-features = false }
|
||||
x11-dl = "2.19.1"
|
||||
x11rb = { version = "0.13.0", default-features = false }
|
||||
memmap2 = { version = "0.9.0", optional = true }
|
||||
percent-encoding = { version = "2.0", optional = true }
|
||||
rustix = { version = "0.38.4", default-features = false, features = [
|
||||
"std",
|
||||
"system",
|
||||
"thread",
|
||||
"process",
|
||||
] }
|
||||
sctk = { package = "smithay-client-toolkit", version = "0.19.2", default-features = false, features = [
|
||||
"calloop",
|
||||
], optional = true }
|
||||
sctk-adwaita = { version = "0.10.1", default-features = false, optional = true }
|
||||
wayland-backend = { version = "0.3.10", default-features = false, features = [
|
||||
"client_system",
|
||||
], optional = true }
|
||||
wayland-client = { version = "0.31.10", optional = true }
|
||||
wayland-protocols = { version = "0.32.8", features = ["staging"], optional = true }
|
||||
wayland-protocols-plasma = { version = "0.3.8", features = ["client"], optional = true }
|
||||
x11-dl = { version = "2.19.1", optional = true }
|
||||
x11rb = { version = "0.13.0", default-features = false, features = [
|
||||
"allow-unsafe-code",
|
||||
"cursor",
|
||||
"dl-libxcb",
|
||||
"randr",
|
||||
"resource_manager",
|
||||
"sync",
|
||||
"xinput",
|
||||
"xkb",
|
||||
], optional = true }
|
||||
xkbcommon-dl = "0.4.2"
|
||||
|
||||
# Orbital dependencies.
|
||||
libredox = "0.1.12"
|
||||
# Orbital
|
||||
[target.'cfg(target_os = "redox")'.dependencies]
|
||||
orbclient = { version = "0.3.47", default-features = false }
|
||||
redox_event = { package = "redox_event", version = "0.4.5" }
|
||||
redox_syscall = "0.5.7"
|
||||
|
||||
# Web dependencies.
|
||||
atomic-waker = "1"
|
||||
concurrent-queue = { version = "2", default-features = false }
|
||||
console_error_panic_hook = "0.1"
|
||||
# Web
|
||||
[target.'cfg(target_family = "wasm")'.dependencies]
|
||||
js-sys = "0.3.70"
|
||||
pin-project = "1"
|
||||
tracing-web = "0.1"
|
||||
wasm-bindgen = "0.2.93"
|
||||
wasm-bindgen-futures = "0.4.43"
|
||||
wasm-bindgen-test = "0.3"
|
||||
web-time = "1"
|
||||
web_sys = { package = "web-sys", version = "0.3.70" }
|
||||
web_sys = { package = "web-sys", version = "0.3.70", features = [
|
||||
"AbortController",
|
||||
"AbortSignal",
|
||||
"Blob",
|
||||
"BlobPropertyBag",
|
||||
"console",
|
||||
"CssStyleDeclaration",
|
||||
"Document",
|
||||
"DomException",
|
||||
"DomRect",
|
||||
"DomRectReadOnly",
|
||||
"Element",
|
||||
"Event",
|
||||
"EventTarget",
|
||||
"FocusEvent",
|
||||
"HtmlCanvasElement",
|
||||
"HtmlElement",
|
||||
"HtmlHtmlElement",
|
||||
"HtmlImageElement",
|
||||
"ImageBitmap",
|
||||
"ImageBitmapOptions",
|
||||
"ImageBitmapRenderingContext",
|
||||
"ImageData",
|
||||
"IntersectionObserver",
|
||||
"IntersectionObserverEntry",
|
||||
"KeyboardEvent",
|
||||
"MediaQueryList",
|
||||
"MessageChannel",
|
||||
"MessagePort",
|
||||
"Navigator",
|
||||
"Node",
|
||||
"OrientationLockType",
|
||||
"OrientationType",
|
||||
"PageTransitionEvent",
|
||||
"Permissions",
|
||||
"PermissionState",
|
||||
"PermissionStatus",
|
||||
"PointerEvent",
|
||||
"PremultiplyAlpha",
|
||||
"ResizeObserver",
|
||||
"ResizeObserverBoxOptions",
|
||||
"ResizeObserverEntry",
|
||||
"ResizeObserverOptions",
|
||||
"ResizeObserverSize",
|
||||
"Screen",
|
||||
"ScreenOrientation",
|
||||
"Url",
|
||||
"VisibilityState",
|
||||
"WheelEvent",
|
||||
"Window",
|
||||
"Worker",
|
||||
] }
|
||||
|
||||
[target.'cfg(all(target_family = "wasm", target_feature = "atomics"))'.dependencies]
|
||||
atomic-waker = "1"
|
||||
concurrent-queue = { version = "2", default-features = false }
|
||||
|
||||
[target.'cfg(target_family = "wasm")'.dev-dependencies]
|
||||
console_error_panic_hook = "0.1"
|
||||
tracing-web = "0.1"
|
||||
wasm-bindgen-test = "0.3"
|
||||
|
||||
[[example]]
|
||||
doc-scrape-examples = true
|
||||
name = "window"
|
||||
|
||||
[[example]]
|
||||
name = "child_window"
|
||||
|
||||
[workspace]
|
||||
members = ["dpi"]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.package]
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
repository = "https://github.com/rust-windowing/winit"
|
||||
rust-version = "1.80"
|
||||
|
||||
[workspace.dependencies]
|
||||
mint = "0.5.6"
|
||||
serde = { version = "1", features = ["serde_derive"] }
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
winit = "0.31.0-beta.2"
|
||||
winit = "0.30.10"
|
||||
```
|
||||
|
||||
## [Documentation](https://docs.rs/winit)
|
||||
@@ -39,7 +39,7 @@ For contributing guidelines see [CONTRIBUTING.md](./CONTRIBUTING.md).
|
||||
|
||||
## MSRV Policy
|
||||
|
||||
This crate's Minimum Supported Rust Version (MSRV) is **1.85**. Changes to
|
||||
This crate's Minimum Supported Rust Version (MSRV) is **1.80**. Changes to
|
||||
the MSRV will be accompanied by a minor version bump.
|
||||
|
||||
As a **tentative** policy, the upper bound of the MSRV is given by the following
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use cfg_aliases::cfg_aliases;
|
||||
|
||||
// Only relevant for examples and Winit, our usage of println! is fine here.
|
||||
#[allow(clippy::disallowed_macros)]
|
||||
fn main() {
|
||||
// Dummy invocation to enable change-tracking in build scripts.
|
||||
// The script doesn't depend on our code.
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
// Setup cfg aliases.
|
||||
39
clippy.toml
@@ -1,27 +1,16 @@
|
||||
# Using allow-invalid because this is platform-specific code
|
||||
disallowed-macros = [
|
||||
{ path = "std::print", reason = "works badly on web", replacement = "tracing::info" },
|
||||
{ path = "std::println", reason = "works badly on web", replacement = "tracing::info" },
|
||||
{ path = "std::eprint", reason = "works badly on web", replacement = "tracing::error" },
|
||||
{ path = "std::eprintln", reason = "works badly on web", replacement = "tracing::error" },
|
||||
{ path = "std::dbg", reason = "leftover debugging aid, remove it or use tracing" },
|
||||
]
|
||||
disallowed-methods = [
|
||||
{ allow-invalid = true, 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." },
|
||||
{ allow-invalid = true, path = "objc2_app_kit::NSWindow::setFrameTopLeftPoint", reason = "Not sufficient when working with Winit's coordinate system, use `flip_window_screen_coordinates` instead" },
|
||||
{ allow-invalid = true, path = "web_sys::Document::exit_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
||||
{ allow-invalid = true, path = "web_sys::Document::fullscreen_element", reason = "Doesn't account for compatibility with Safari" },
|
||||
{ allow-invalid = true, path = "web_sys::Element::request_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::height", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::set_height", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::set_width", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::width", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ allow-invalid = true, path = "web_sys::HtmlElement::style", reason = "cache this to reduce calls to JS" },
|
||||
{ allow-invalid = true, path = "web_sys::MouseEvent::buttons", reason = "Use `backend::event::cursor_buttons()` to avoid wrong conversions" },
|
||||
{ allow-invalid = true, path = "web_sys::MouseEvent::button", reason = "Use `backend::event::cursor_button()` to avoid wrong conversions" },
|
||||
{ allow-invalid = true, path = "web_sys::PointerEvent::pointer_type", reason = "Use `WebPointerType` to emit warnings" },
|
||||
{ allow-invalid = true, path = "web_sys::Window::document", reason = "cache this to reduce calls to JS" },
|
||||
{ allow-invalid = true, path = "web_sys::Window::get_computed_style", reason = "cache this to reduce calls to JS" },
|
||||
{ allow-invalid = true, path = "web_sys::Window::navigator", reason = "cache this to reduce calls to JS" },
|
||||
{ allow-invalid = true, path = "web_sys::window", reason = "is not available in every context" },
|
||||
{ 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 = "objc2_app_kit::NSWindow::setFrameTopLeftPoint", reason = "Not sufficient when working with Winit's coordinate system, use `flip_window_screen_coordinates` instead" },
|
||||
{ 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::Element::request_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
||||
{ path = "web_sys::HtmlCanvasElement::height", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ path = "web_sys::HtmlCanvasElement::set_height", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ path = "web_sys::HtmlCanvasElement::set_width", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ path = "web_sys::HtmlCanvasElement::width", reason = "Winit shouldn't touch the internal canvas size" },
|
||||
{ path = "web_sys::HtmlElement::style", reason = "cache this to reduce calls to JS" },
|
||||
{ path = "web_sys::Window::document", reason = "cache this to reduce calls to JS" },
|
||||
{ path = "web_sys::Window::get_computed_style", reason = "cache this to reduce calls to JS" },
|
||||
{ path = "web_sys::Window::navigator", reason = "cache this to reduce calls to JS" },
|
||||
{ path = "web_sys::window", reason = "is not available in every context" },
|
||||
]
|
||||
|
||||
79
deny.toml
@@ -1,25 +1,27 @@
|
||||
# https://embarkstudios.github.io/cargo-deny
|
||||
# cargo install cargo-deny
|
||||
# cargo update && cargo deny check
|
||||
# cargo update && cargo deny --target aarch64-apple-ios check
|
||||
# Note: running just `cargo deny check` without a `--target` will result in
|
||||
# false positives due to https://github.com/EmbarkStudios/cargo-deny/issues/324
|
||||
[graph]
|
||||
all-features = true
|
||||
exclude-dev = true
|
||||
targets = [
|
||||
"aarch64-apple-darwin",
|
||||
"aarch64-apple-ios",
|
||||
"aarch64-linux-android",
|
||||
"i686-pc-windows-gnu",
|
||||
"i686-pc-windows-msvc",
|
||||
"i686-unknown-linux-gnu",
|
||||
{ triple = "aarch64-apple-darwin" },
|
||||
{ triple = "aarch64-apple-ios" },
|
||||
{ triple = "aarch64-linux-android" },
|
||||
{ triple = "i686-pc-windows-gnu" },
|
||||
{ triple = "i686-pc-windows-msvc" },
|
||||
{ triple = "i686-unknown-linux-gnu" },
|
||||
{ triple = "wasm32-unknown-unknown", features = [
|
||||
"atomics",
|
||||
] },
|
||||
"x86_64-apple-darwin",
|
||||
"x86_64-apple-ios",
|
||||
"x86_64-pc-windows-gnu",
|
||||
"x86_64-pc-windows-msvc",
|
||||
"x86_64-unknown-linux-gnu",
|
||||
"x86_64-unknown-redox",
|
||||
{ triple = "x86_64-apple-darwin" },
|
||||
{ triple = "x86_64-apple-ios" },
|
||||
{ triple = "x86_64-pc-windows-gnu" },
|
||||
{ triple = "x86_64-pc-windows-msvc" },
|
||||
{ triple = "x86_64-unknown-linux-gnu" },
|
||||
{ triple = "x86_64-unknown-redox" },
|
||||
]
|
||||
|
||||
[licenses]
|
||||
@@ -30,32 +32,45 @@ allow = [
|
||||
"ISC", # https://tldrlegal.com/license/isc-license
|
||||
"MIT", # https://tldrlegal.com/license/mit-license
|
||||
"Unicode-3.0", # https://spdx.org/licenses/Unicode-3.0.html
|
||||
"Zlib", # https://spdx.org/licenses/Zlib.html
|
||||
"MPL-2.0", # https://www.mozilla.org/en-US/MPL/2.0/
|
||||
]
|
||||
confidence-threshold = 1.0
|
||||
private = { ignore = true }
|
||||
|
||||
[bans]
|
||||
multiple-versions = "deny"
|
||||
skip = [
|
||||
{ crate = "jni-sys@0.3", reason = "uses the semver trick to depend on v0.4, but `ndk` hasn't been updated to v0.4 yet" },
|
||||
{ crate = "thiserror@1.0", reason = "dep of `ndk` crate, yet to be updated" },
|
||||
{ crate = "thiserror-impl@1.0", reason = "dep of `thiserror`" },
|
||||
{ crate = "objc2@0.5", reason = "used by crossfont" },
|
||||
{ crate = "objc2-foundation@0.2", reason = "used by crossfont" },
|
||||
]
|
||||
skip-tree = [
|
||||
{ crate = "windows-sys", reason = "foundational but bumps fairly often, nothing we can do about it not having a shared version" },
|
||||
]
|
||||
skip = [{ crate = "bitflags@1", reason = "the ecosystem is in the process of migrating" }]
|
||||
wildcards = "allow" # at least until https://github.com/EmbarkStudios/cargo-deny/issues/241 is fixed
|
||||
|
||||
[bans.build]
|
||||
bypass = [
|
||||
{ crate = "android-activity", allow-globs = ["android-games-sdk/import-games-sdk.sh"] },
|
||||
{ crate = "freetype-sys", allow-globs = ["freetype2/*"] },
|
||||
# `crossfont` still depends (partially transitively) on `winapi`.
|
||||
{ crate = "winapi-i686-pc-windows-gnu", allow-globs = ["lib/lib*.a"] },
|
||||
{ crate = "winapi-x86_64-pc-windows-gnu", allow-globs = ["lib/lib*.a"] },
|
||||
]
|
||||
include-archives = true
|
||||
interpreted = "deny"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow = [
|
||||
{ path = "generate-bindings.sh", checksum = "268ec23248218d779e33853cdc60e2985e70214ff004716cd734270de1f6b561" },
|
||||
]
|
||||
crate = "android-activity"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow-globs = ["ci/*", "githooks/*"]
|
||||
crate = "zerocopy"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow-globs = ["freetype2/*"]
|
||||
crate = "freetype-sys"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow-globs = ["lib/*.a"]
|
||||
crate = "windows_i686_gnu"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow-globs = ["lib/*.lib"]
|
||||
crate = "windows_i686_msvc"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow-globs = ["lib/*.a"]
|
||||
crate = "windows_x86_64_gnu"
|
||||
|
||||
[[bans.build.bypass]]
|
||||
allow-globs = ["lib/*.lib"]
|
||||
crate = "windows_x86_64_msvc"
|
||||
|
||||
18
docs/ATTRIBUTION.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Image Attribution
|
||||
|
||||
These images are used in the documentation of `winit`.
|
||||
|
||||
## keyboard_*.svg
|
||||
|
||||
These files are a modified version of "[ANSI US QWERTY (Windows)](https://commons.wikimedia.org/wiki/File:ANSI_US_QWERTY_(Windows).svg)"
|
||||
by [Tomiĉo] (https://commons.wikimedia.org/wiki/User:Tomi%C4%89o). It was
|
||||
originally released under the [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.en)
|
||||
License. Minor modifications have been made by [John Nunley](https://github.com/notgull),
|
||||
which have been released under the same license as a derivative work.
|
||||
|
||||
## `coordinate-systems*`
|
||||
|
||||
These files are created by [Mads Marquart](https://github.com/madsmtm) using
|
||||
[draw.io](https://draw.io/), and compressed using [svgomg.net](https://svgomg.net/).
|
||||
|
||||
They are licensed under the [CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/) license.
|
||||
|
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
1
docs/res/keyboard_left_shift_key.svg
Normal file
|
After Width: | Height: | Size: 73 KiB |
1
docs/res/keyboard_numpad_1_key.svg
Normal file
|
After Width: | Height: | Size: 73 KiB |
1
docs/res/keyboard_right_shift_key.svg
Normal file
|
After Width: | Height: | Size: 73 KiB |
1
docs/res/keyboard_standard_1_key.svg
Normal file
|
After Width: | Height: | Size: 73 KiB |
236
dpi/src/lib.rs
@@ -64,7 +64,7 @@
|
||||
//! [points]: https://en.wikipedia.org/wiki/Point_(typography)
|
||||
//! [picas]: https://en.wikipedia.org/wiki/Pica_(typography)
|
||||
|
||||
#![cfg_attr(docsrs, feature(doc_cfg), doc(auto_cfg(hide(doc, docsrs))))]
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide), doc(cfg_hide(doc, docsrs)))]
|
||||
#![cfg_attr(feature = "std", forbid(unsafe_code))]
|
||||
#![no_std]
|
||||
|
||||
@@ -84,18 +84,36 @@ pub trait Pixel: Copy + Into<f64> {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! pixel_int_impl {
|
||||
($($t:ty),*) => {$(
|
||||
impl Pixel for $t {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as $t
|
||||
}
|
||||
}
|
||||
)*}
|
||||
impl Pixel for u8 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as u8
|
||||
}
|
||||
}
|
||||
impl Pixel for u16 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as u16
|
||||
}
|
||||
}
|
||||
impl Pixel for u32 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as u32
|
||||
}
|
||||
}
|
||||
impl Pixel for i8 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as i8
|
||||
}
|
||||
}
|
||||
impl Pixel for i16 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as i16
|
||||
}
|
||||
}
|
||||
impl Pixel for i32 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
round(f) as i32
|
||||
}
|
||||
}
|
||||
|
||||
pixel_int_impl!(u8, u16, u32, i8, i16, i32);
|
||||
|
||||
impl Pixel for f32 {
|
||||
fn from_f64(f: f64) -> Self {
|
||||
f as f32
|
||||
@@ -360,48 +378,6 @@ impl<P: Pixel> From<LogicalUnit<P>> for PixelUnit {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! vec2_from_impls {
|
||||
($t:ident, $a:ident, $b:ident, $mint_ty:ident) => {
|
||||
impl<P: Pixel, X: Pixel> From<(X, X)> for $t<P> {
|
||||
fn from(($a, $b): (X, X)) -> Self {
|
||||
Self::new($a.cast(), $b.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<$t<P>> for (X, X) {
|
||||
fn from(p: $t<P>) -> Self {
|
||||
(p.$a.cast(), p.$b.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<[X; 2]> for $t<P> {
|
||||
fn from([$a, $b]: [X; 2]) -> Self {
|
||||
Self::new($a.cast(), $b.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<$t<P>> for [X; 2] {
|
||||
fn from(p: $t<P>) -> Self {
|
||||
[p.$a.cast(), p.$b.cast()]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<mint::$mint_ty<P>> for $t<P> {
|
||||
fn from(p: mint::$mint_ty<P>) -> Self {
|
||||
Self::new(p.x, p.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<$t<P>> for mint::$mint_ty<P> {
|
||||
fn from(p: $t<P>) -> Self {
|
||||
Self { x: p.$a, y: p.$b }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// A position represented in logical pixels.
|
||||
///
|
||||
/// The position is stored as floats, so please be careful. Casting floats to integers truncates the
|
||||
@@ -444,7 +420,43 @@ impl<P: Pixel> LogicalPosition<P> {
|
||||
}
|
||||
}
|
||||
|
||||
vec2_from_impls!(LogicalPosition, x, y, Point2);
|
||||
impl<P: Pixel, X: Pixel> From<(X, X)> for LogicalPosition<P> {
|
||||
fn from((x, y): (X, X)) -> LogicalPosition<P> {
|
||||
LogicalPosition::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<LogicalPosition<P>> for (X, X) {
|
||||
fn from(p: LogicalPosition<P>) -> (X, X) {
|
||||
(p.x.cast(), p.y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<[X; 2]> for LogicalPosition<P> {
|
||||
fn from([x, y]: [X; 2]) -> LogicalPosition<P> {
|
||||
LogicalPosition::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<LogicalPosition<P>> for [X; 2] {
|
||||
fn from(p: LogicalPosition<P>) -> [X; 2] {
|
||||
[p.x.cast(), p.y.cast()]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<mint::Point2<P>> for LogicalPosition<P> {
|
||||
fn from(p: mint::Point2<P>) -> Self {
|
||||
Self::new(p.x, p.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<LogicalPosition<P>> for mint::Point2<P> {
|
||||
fn from(p: LogicalPosition<P>) -> Self {
|
||||
mint::Point2 { x: p.x, y: p.y }
|
||||
}
|
||||
}
|
||||
|
||||
/// A position represented in physical pixels.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
|
||||
@@ -484,7 +496,43 @@ impl<P: Pixel> PhysicalPosition<P> {
|
||||
}
|
||||
}
|
||||
|
||||
vec2_from_impls!(PhysicalPosition, x, y, Point2);
|
||||
impl<P: Pixel, X: Pixel> From<(X, X)> for PhysicalPosition<P> {
|
||||
fn from((x, y): (X, X)) -> PhysicalPosition<P> {
|
||||
PhysicalPosition::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<PhysicalPosition<P>> for (X, X) {
|
||||
fn from(p: PhysicalPosition<P>) -> (X, X) {
|
||||
(p.x.cast(), p.y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<[X; 2]> for PhysicalPosition<P> {
|
||||
fn from([x, y]: [X; 2]) -> PhysicalPosition<P> {
|
||||
PhysicalPosition::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<PhysicalPosition<P>> for [X; 2] {
|
||||
fn from(p: PhysicalPosition<P>) -> [X; 2] {
|
||||
[p.x.cast(), p.y.cast()]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<mint::Point2<P>> for PhysicalPosition<P> {
|
||||
fn from(p: mint::Point2<P>) -> Self {
|
||||
Self::new(p.x, p.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<PhysicalPosition<P>> for mint::Point2<P> {
|
||||
fn from(p: PhysicalPosition<P>) -> Self {
|
||||
mint::Point2 { x: p.x, y: p.y }
|
||||
}
|
||||
}
|
||||
|
||||
/// A size represented in logical pixels.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
|
||||
@@ -524,7 +572,43 @@ impl<P: Pixel> LogicalSize<P> {
|
||||
}
|
||||
}
|
||||
|
||||
vec2_from_impls!(LogicalSize, width, height, Vector2);
|
||||
impl<P: Pixel, X: Pixel> From<(X, X)> for LogicalSize<P> {
|
||||
fn from((x, y): (X, X)) -> LogicalSize<P> {
|
||||
LogicalSize::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<LogicalSize<P>> for (X, X) {
|
||||
fn from(s: LogicalSize<P>) -> (X, X) {
|
||||
(s.width.cast(), s.height.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<[X; 2]> for LogicalSize<P> {
|
||||
fn from([x, y]: [X; 2]) -> LogicalSize<P> {
|
||||
LogicalSize::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<LogicalSize<P>> for [X; 2] {
|
||||
fn from(s: LogicalSize<P>) -> [X; 2] {
|
||||
[s.width.cast(), s.height.cast()]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<mint::Vector2<P>> for LogicalSize<P> {
|
||||
fn from(v: mint::Vector2<P>) -> Self {
|
||||
Self::new(v.x, v.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<LogicalSize<P>> for mint::Vector2<P> {
|
||||
fn from(s: LogicalSize<P>) -> Self {
|
||||
mint::Vector2 { x: s.width, y: s.height }
|
||||
}
|
||||
}
|
||||
|
||||
/// A size represented in physical pixels.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
|
||||
@@ -561,7 +645,43 @@ impl<P: Pixel> PhysicalSize<P> {
|
||||
}
|
||||
}
|
||||
|
||||
vec2_from_impls!(PhysicalSize, width, height, Vector2);
|
||||
impl<P: Pixel, X: Pixel> From<(X, X)> for PhysicalSize<P> {
|
||||
fn from((x, y): (X, X)) -> PhysicalSize<P> {
|
||||
PhysicalSize::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<PhysicalSize<P>> for (X, X) {
|
||||
fn from(s: PhysicalSize<P>) -> (X, X) {
|
||||
(s.width.cast(), s.height.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<[X; 2]> for PhysicalSize<P> {
|
||||
fn from([x, y]: [X; 2]) -> PhysicalSize<P> {
|
||||
PhysicalSize::new(x.cast(), y.cast())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel, X: Pixel> From<PhysicalSize<P>> for [X; 2] {
|
||||
fn from(s: PhysicalSize<P>) -> [X; 2] {
|
||||
[s.width.cast(), s.height.cast()]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<mint::Vector2<P>> for PhysicalSize<P> {
|
||||
fn from(v: mint::Vector2<P>) -> Self {
|
||||
Self::new(v.x, v.y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mint")]
|
||||
impl<P: Pixel> From<PhysicalSize<P>> for mint::Vector2<P> {
|
||||
fn from(s: PhysicalSize<P>) -> Self {
|
||||
mint::Vector2 { x: s.width, y: s.height }
|
||||
}
|
||||
}
|
||||
|
||||
/// A size that's either physical or logical.
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
|
||||
@@ -1,48 +1,50 @@
|
||||
//! Simple winit application.
|
||||
//!
|
||||
//! Note that a real application accepting text input **should** support
|
||||
//! the IME interface. See the `ime` example.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt::Debug;
|
||||
#[cfg(not(android_platform))]
|
||||
use std::num::NonZeroU32;
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::{self, Receiver, Sender};
|
||||
#[cfg(not(web_platform))]
|
||||
use std::time::Instant;
|
||||
use std::sync::Arc;
|
||||
use std::{fmt, mem};
|
||||
|
||||
use ::tracing::{error, info};
|
||||
use cursor_icon::CursorIcon;
|
||||
#[cfg(not(android_platform))]
|
||||
use rwh_06::{DisplayHandle, HasDisplayHandle};
|
||||
#[cfg(not(android_platform))]
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::{error, info};
|
||||
#[cfg(web_platform)]
|
||||
use web_time::Instant;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::cursor::{Cursor, CustomCursor, CustomCursorSource};
|
||||
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
|
||||
use winit::error::RequestError;
|
||||
use winit::event::{DeviceEvent, DeviceId, MouseButton, MouseScrollDelta, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::icon::{Icon, RgbaIcon};
|
||||
use winit::event::{DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
|
||||
use winit::icon::RgbaIcon;
|
||||
use winit::keyboard::{Key, ModifiersState};
|
||||
use winit::monitor::Fullscreen;
|
||||
#[cfg(macos_platform)]
|
||||
use winit::platform::macos::{OptionAsAlt, WindowAttributesMacOS, WindowExtMacOS};
|
||||
use winit::platform::macos::{
|
||||
ApplicationHandlerExtMacOS, OptionAsAlt, WindowAttributesExtMacOS, WindowExtMacOS,
|
||||
};
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
use winit::platform::startup_notify::{self, EventLoopExtStartupNotify, WindowExtStartupNotify};
|
||||
#[cfg(wayland_platform)]
|
||||
use winit::platform::wayland::{ActiveEventLoopExtWayland, WindowAttributesWayland};
|
||||
use winit::platform::startup_notify::{
|
||||
self, EventLoopExtStartupNotify, WindowAttributesExtStartupNotify, WindowExtStartupNotify,
|
||||
};
|
||||
#[cfg(web_platform)]
|
||||
use winit::platform::web::{ActiveEventLoopExtWeb, WindowAttributesWeb};
|
||||
use winit::platform::web::{ActiveEventLoopExtWeb, WindowAttributesExtWeb};
|
||||
#[cfg(x11_platform)]
|
||||
use winit::platform::x11::{ActiveEventLoopExtX11, WindowAttributesX11};
|
||||
use winit::window::{CursorGrabMode, ResizeDirection, Theme, Window, WindowAttributes, WindowId};
|
||||
use winit_core::application::macos::ApplicationHandlerExtMacOS;
|
||||
use winit::platform::x11::WindowAttributesExtX11;
|
||||
use winit::window::{
|
||||
Cursor, CursorGrabMode, CustomCursor, CustomCursorSource, Icon, ResizeDirection, Theme, Window,
|
||||
WindowAttributes, WindowId,
|
||||
};
|
||||
|
||||
#[path = "util/tracing.rs"]
|
||||
mod tracing_init;
|
||||
mod tracing;
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
mod fill;
|
||||
|
||||
/// The amount of points to around the window for drag resize direction calculations.
|
||||
const BORDER_SIZE: f64 = 20.;
|
||||
@@ -51,7 +53,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[cfg(web_platform)]
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
tracing_init::init();
|
||||
tracing::init();
|
||||
|
||||
let event_loop = EventLoop::new()?;
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
@@ -90,12 +92,22 @@ struct Application {
|
||||
/// Drawing context.
|
||||
///
|
||||
/// With OpenGL it could be EGLDisplay.
|
||||
context: Context<OwnedDisplayHandle>,
|
||||
#[cfg(not(android_platform))]
|
||||
context: Option<Context<DisplayHandle<'static>>>,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
fn new(event_loop: &EventLoop, receiver: Receiver<Action>, sender: Sender<Action>) -> Self {
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
// SAFETY: we drop the context right before the event loop is stopped, thus making it safe.
|
||||
#[cfg(not(android_platform))]
|
||||
let context = Some(
|
||||
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
|
||||
// varies by WM, and on Windows, you still have to account for screen scaling. Here
|
||||
@@ -113,7 +125,15 @@ impl Application {
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
Self { receiver, sender, context, custom_cursors, icon, windows: Default::default() }
|
||||
Self {
|
||||
receiver,
|
||||
sender,
|
||||
#[cfg(not(android_platform))]
|
||||
context,
|
||||
custom_cursors,
|
||||
icon,
|
||||
windows: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_window(
|
||||
@@ -129,29 +149,43 @@ impl Application {
|
||||
.with_transparent(true)
|
||||
.with_window_icon(Some(self.icon.clone()));
|
||||
|
||||
#[cfg(x11_platform)]
|
||||
if event_loop.is_x11() {
|
||||
window_attributes = window_attributes
|
||||
.with_platform_attributes(Box::new(window_attributes_x11(event_loop)?));
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
if let Some(token) = event_loop.read_token_from_env() {
|
||||
startup_notify::reset_activation_token_env();
|
||||
info!("Using token {:?} to activate a window", token);
|
||||
window_attributes = window_attributes.with_activation_token(token);
|
||||
}
|
||||
|
||||
#[cfg(wayland_platform)]
|
||||
if event_loop.is_wayland() {
|
||||
window_attributes = window_attributes
|
||||
.with_platform_attributes(Box::new(window_attributes_wayland(event_loop)));
|
||||
#[cfg(x11_platform)]
|
||||
match std::env::var("X11_VISUAL_ID") {
|
||||
Ok(visual_id_str) => {
|
||||
info!("Using X11 visual id {visual_id_str}");
|
||||
let visual_id = visual_id_str.parse()?;
|
||||
window_attributes = window_attributes.with_x11_visual(visual_id);
|
||||
},
|
||||
Err(_) => info!("Set the X11_VISUAL_ID env variable to request specific X11 visual"),
|
||||
}
|
||||
|
||||
#[cfg(x11_platform)]
|
||||
match std::env::var("X11_SCREEN_ID") {
|
||||
Ok(screen_id_str) => {
|
||||
info!("Placing the window on X11 screen {screen_id_str}");
|
||||
let screen_id = screen_id_str.parse()?;
|
||||
window_attributes = window_attributes.with_x11_screen(screen_id);
|
||||
},
|
||||
Err(_) => info!(
|
||||
"Set the X11_SCREEN_ID env variable to place the window on non-default screen"
|
||||
),
|
||||
}
|
||||
|
||||
#[cfg(macos_platform)]
|
||||
if let Some(tab_id) = _tab_id {
|
||||
let window_attributes_macos =
|
||||
Box::new(WindowAttributesMacOS::default().with_tabbing_identifier(&tab_id));
|
||||
window_attributes = window_attributes.with_platform_attributes(window_attributes_macos);
|
||||
window_attributes = window_attributes.with_tabbing_identifier(&tab_id);
|
||||
}
|
||||
|
||||
#[cfg(web_platform)]
|
||||
{
|
||||
window_attributes =
|
||||
window_attributes.with_platform_attributes(Box::new(window_attributes_web()));
|
||||
window_attributes = window_attributes.with_append(true);
|
||||
}
|
||||
|
||||
let window = event_loop.create_window(window_attributes)?;
|
||||
@@ -178,15 +212,6 @@ impl Application {
|
||||
Action::DumpMonitors => self.dump_monitors(_event_loop),
|
||||
Action::Message => {
|
||||
info!("User wake up");
|
||||
for (id, window) in self.windows.iter() {
|
||||
if window.emit_surface_size {
|
||||
let size = window.window.surface_size();
|
||||
info!(
|
||||
"Window {id:?} has physical surface size {}x{}",
|
||||
size.width, size.height
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => unreachable!("Tried to execute invalid action without `WindowId`"),
|
||||
}
|
||||
@@ -226,13 +251,8 @@ impl Application {
|
||||
Action::ToggleSimpleFullscreen => {
|
||||
window.window.set_simple_fullscreen(!window.window.simple_fullscreen());
|
||||
},
|
||||
#[cfg(macos_platform)]
|
||||
Action::ToggleBorderlessGame => {
|
||||
let current = window.window.is_borderless_game();
|
||||
window.window.set_borderless_game(!current);
|
||||
info!("Borderless game: {}", !current);
|
||||
},
|
||||
Action::ToggleMaximize => window.toggle_maximize(),
|
||||
Action::ToggleImeInput => window.toggle_ime(),
|
||||
Action::Minimize => window.minimize(),
|
||||
Action::NextCursor => window.next_cursor(),
|
||||
Action::NextCustomCursor => {
|
||||
@@ -305,9 +325,6 @@ impl Application {
|
||||
window.continuous_redraw = !window.continuous_redraw;
|
||||
window.window.request_redraw();
|
||||
},
|
||||
Action::EmitSurfaceSize => {
|
||||
window.toggle_emit_surface_size();
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -479,9 +496,8 @@ impl ApplicationHandler for Application {
|
||||
let mods = window.modifiers;
|
||||
if let Some(action) = state
|
||||
.is_pressed()
|
||||
.then(|| button.mouse_button())
|
||||
.then(|| Self::process_mouse_binding(button.mouse_button(), &mods))
|
||||
.flatten()
|
||||
.and_then(|button| Self::process_mouse_binding(button, &mods))
|
||||
{
|
||||
self.handle_action_with_window(event_loop, window_id, action);
|
||||
}
|
||||
@@ -503,6 +519,16 @@ impl ApplicationHandler for Application {
|
||||
}
|
||||
}
|
||||
},
|
||||
WindowEvent::Ime(event) => match event {
|
||||
Ime::Enabled => info!("IME enabled for Window={window_id:?}"),
|
||||
Ime::Preedit(text, caret_pos) => {
|
||||
info!("Preedit: {}, with caret at {:?}", text, caret_pos);
|
||||
},
|
||||
Ime::Commit(text) => {
|
||||
info!("Committed: {}", text);
|
||||
},
|
||||
Ime::Disabled => info!("IME disabled for Window={window_id:?}"),
|
||||
},
|
||||
WindowEvent::PinchGesture { delta, .. } => {
|
||||
window.zoom += delta;
|
||||
let zoom = window.zoom;
|
||||
@@ -529,7 +555,15 @@ impl ApplicationHandler for Application {
|
||||
WindowEvent::DoubleTapGesture { .. } => {
|
||||
info!("Smart zoom");
|
||||
},
|
||||
_ => (),
|
||||
WindowEvent::TouchpadPressure { .. }
|
||||
| WindowEvent::DragLeft { .. }
|
||||
| WindowEvent::KeyboardInput { .. }
|
||||
| WindowEvent::PointerEntered { .. }
|
||||
| WindowEvent::DragEntered { .. }
|
||||
| WindowEvent::DragMoved { .. }
|
||||
| WindowEvent::DragDropped { .. }
|
||||
| WindowEvent::Destroyed
|
||||
| WindowEvent::Moved(_) => (),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -559,6 +593,7 @@ impl ApplicationHandler for Application {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> {
|
||||
Some(self)
|
||||
}
|
||||
@@ -570,6 +605,7 @@ impl Drop for Application {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
impl ApplicationHandlerExtMacOS for Application {
|
||||
fn standard_key_binding(
|
||||
&mut self,
|
||||
@@ -583,8 +619,13 @@ impl ApplicationHandlerExtMacOS for Application {
|
||||
|
||||
/// State of the window.
|
||||
struct WindowState {
|
||||
/// IME input.
|
||||
ime: bool,
|
||||
/// Render surface.
|
||||
surface: Surface<OwnedDisplayHandle, Arc<dyn Window>>,
|
||||
///
|
||||
/// NOTE: This surface must be dropped before the `Window`.
|
||||
#[cfg(not(android_platform))]
|
||||
surface: Surface<DisplayHandle<'static>, Arc<dyn Window>>,
|
||||
/// The actual winit Window.
|
||||
window: Arc<dyn Window>,
|
||||
/// The window theme we're drawing with.
|
||||
@@ -592,11 +633,10 @@ struct WindowState {
|
||||
/// Fill the window with animated color
|
||||
animated_fill_color: bool,
|
||||
/// The application start time. Used for color fill animation
|
||||
start_time: Instant,
|
||||
#[cfg(not(android_platform))]
|
||||
start_time: std::time::Instant,
|
||||
/// Redraw continuously
|
||||
continuous_redraw: bool,
|
||||
/// Periodically emit the surface size
|
||||
emit_surface_size: bool,
|
||||
/// Cursor position over the window.
|
||||
cursor_position: Option<PhysicalPosition<f64>>,
|
||||
/// Window modifiers state.
|
||||
@@ -625,13 +665,20 @@ impl WindowState {
|
||||
fn new(app: &Application, window: Box<dyn Window>) -> Result<Self, Box<dyn Error>> {
|
||||
let window: Arc<dyn Window> = Arc::from(window);
|
||||
|
||||
let surface = Surface::new(&app.context, Arc::clone(&window))?;
|
||||
// SAFETY: the surface is dropped before the `window` which provided it with handle, thus
|
||||
// it doesn't outlive it.
|
||||
#[cfg(not(android_platform))]
|
||||
let surface = Surface::new(app.context.as_ref().unwrap(), Arc::clone(&window))?;
|
||||
|
||||
let theme = window.theme().unwrap_or(Theme::Dark);
|
||||
info!("Theme: {theme:?}");
|
||||
let named_idx = 0;
|
||||
window.set_cursor(CURSORS[named_idx].into());
|
||||
|
||||
// Allow IME out of the box.
|
||||
let ime = true;
|
||||
window.set_ime_allowed(ime);
|
||||
|
||||
let size = window.surface_size();
|
||||
let mut state = Self {
|
||||
#[cfg(macos_platform)]
|
||||
@@ -639,13 +686,15 @@ impl WindowState {
|
||||
custom_idx: app.custom_cursors.as_ref().map(Vec::len).unwrap_or(1) - 1,
|
||||
cursor_grab: CursorGrabMode::None,
|
||||
named_idx,
|
||||
#[cfg(not(android_platform))]
|
||||
surface,
|
||||
window,
|
||||
theme,
|
||||
animated_fill_color: false,
|
||||
continuous_redraw: false,
|
||||
emit_surface_size: false,
|
||||
start_time: Instant::now(),
|
||||
#[cfg(not(android_platform))]
|
||||
start_time: std::time::Instant::now(),
|
||||
ime,
|
||||
cursor_position: Default::default(),
|
||||
cursor_hidden: Default::default(),
|
||||
modifiers: Default::default(),
|
||||
@@ -659,12 +708,23 @@ impl WindowState {
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
pub fn toggle_ime(&mut self) {
|
||||
self.ime = !self.ime;
|
||||
self.window.set_ime_allowed(self.ime);
|
||||
if let Some(position) = self.ime.then_some(self.cursor_position).flatten() {
|
||||
self.window.set_ime_cursor_area(position.into(), PhysicalSize::new(20, 20).into());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn minimize(&mut self) {
|
||||
self.window.set_minimized(true);
|
||||
}
|
||||
|
||||
pub fn cursor_moved(&mut self, position: PhysicalPosition<f64>) {
|
||||
self.cursor_position = Some(position);
|
||||
if self.ime {
|
||||
self.window.set_ime_cursor_area(position.into(), PhysicalSize::new(20, 20).into());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cursor_left(&mut self) {
|
||||
@@ -716,10 +776,6 @@ impl WindowState {
|
||||
self.window.set_fullscreen(fullscreen);
|
||||
}
|
||||
|
||||
fn toggle_emit_surface_size(&mut self) {
|
||||
self.emit_surface_size = !self.emit_surface_size;
|
||||
}
|
||||
|
||||
/// Cycle through the grab modes ignoring errors.
|
||||
fn cycle_cursor_grab(&mut self) {
|
||||
self.cursor_grab = match self.cursor_grab {
|
||||
@@ -816,12 +872,15 @@ impl WindowState {
|
||||
/// Resize the surface to the new size.
|
||||
fn resize(&mut self, size: PhysicalSize<u32>) {
|
||||
info!("Surface resized to {size:?}");
|
||||
let (width, height) = match (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) {
|
||||
(Some(width), Some(height)) => (width, height),
|
||||
_ => return,
|
||||
};
|
||||
self.surface.resize(width, height).expect("failed to resize inner buffer");
|
||||
|
||||
#[cfg(not(android_platform))]
|
||||
{
|
||||
let (width, height) = match (NonZeroU32::new(size.width), NonZeroU32::new(size.height))
|
||||
{
|
||||
(Some(width), Some(height)) => (width, height),
|
||||
_ => return,
|
||||
};
|
||||
self.surface.resize(width, height).expect("failed to resize inner buffer");
|
||||
}
|
||||
self.window.request_redraw();
|
||||
}
|
||||
|
||||
@@ -906,46 +965,42 @@ impl WindowState {
|
||||
}
|
||||
|
||||
/// Draw the window contents.
|
||||
#[cfg(not(android_platform))]
|
||||
fn draw(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
if self.occluded {
|
||||
info!("Skipping drawing occluded window={:?}", self.window.id());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if self.animated_fill_color {
|
||||
fill::fill_window_with_animated_color(&*self.window, self.start_time);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut buffer = self.surface.buffer_mut()?;
|
||||
|
||||
if self.animated_fill_color {
|
||||
// Fill the entire buffer with a single color.
|
||||
let time = self.start_time.elapsed().as_secs_f32() * 1.5;
|
||||
let blue = (time.sin() * 255.0) as u32;
|
||||
let green = ((time.cos() * 255.0) as u32) << 8;
|
||||
let red = ((1.0 - time.sin() * 255.0) as u32) << 16;
|
||||
let color = red | green | blue;
|
||||
buffer.fill(color);
|
||||
} else {
|
||||
// Draw a different color inside the safe area
|
||||
let surface_size = self.window.surface_size();
|
||||
let insets = self.window.safe_area();
|
||||
for y in 0..surface_size.height {
|
||||
for x in 0..surface_size.width {
|
||||
let index = y as usize * surface_size.width as usize + x as usize;
|
||||
if insets.left <= x
|
||||
&& x <= (surface_size.width - insets.right)
|
||||
&& insets.top <= y
|
||||
&& y <= (surface_size.height - insets.bottom)
|
||||
{
|
||||
// In safe area
|
||||
buffer[index] = match self.theme {
|
||||
Theme::Light => 0xffe8e8e8, // Light gray
|
||||
Theme::Dark => 0xff525252, // Medium gray
|
||||
};
|
||||
} else {
|
||||
// Outside safe area
|
||||
buffer[index] = match self.theme {
|
||||
Theme::Light => 0xffffffff, // White
|
||||
Theme::Dark => 0xff181818, // Dark gray
|
||||
};
|
||||
}
|
||||
// Draw a different color inside the safe area
|
||||
let surface_size = self.window.surface_size();
|
||||
let insets = self.window.safe_area();
|
||||
for y in 0..surface_size.height {
|
||||
for x in 0..surface_size.width {
|
||||
let index = y as usize * surface_size.width as usize + x as usize;
|
||||
if insets.left <= x
|
||||
&& x <= (surface_size.width - insets.right)
|
||||
&& insets.top <= y
|
||||
&& y <= (surface_size.height - insets.bottom)
|
||||
{
|
||||
// In safe area
|
||||
buffer[index] = match self.theme {
|
||||
Theme::Light => 0xffe8e8e8, // Light gray
|
||||
Theme::Dark => 0xff525252, // Medium gray
|
||||
};
|
||||
} else {
|
||||
// Outside safe area
|
||||
buffer[index] = match self.theme {
|
||||
Theme::Light => 0xffffffff, // White
|
||||
Theme::Dark => 0xff181818, // Dark gray
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -956,6 +1011,12 @@ impl WindowState {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(android_platform)]
|
||||
fn draw(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
info!("Drawing but without rendering...");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct Binding<T: Eq> {
|
||||
@@ -980,13 +1041,12 @@ enum Action {
|
||||
ToggleCursorVisibility,
|
||||
CreateNewWindow,
|
||||
ToggleResizeIncrements,
|
||||
ToggleImeInput,
|
||||
ToggleDecorations,
|
||||
ToggleResizable,
|
||||
ToggleFullscreen,
|
||||
#[cfg(macos_platform)]
|
||||
ToggleSimpleFullscreen,
|
||||
#[cfg(macos_platform)]
|
||||
ToggleBorderlessGame,
|
||||
ToggleMaximize,
|
||||
Minimize,
|
||||
NextCursor,
|
||||
@@ -1010,7 +1070,6 @@ enum Action {
|
||||
Message,
|
||||
ToggleAnimatedFillColor,
|
||||
ToggleContinuousRedraw,
|
||||
EmitSurfaceSize,
|
||||
}
|
||||
|
||||
impl Action {
|
||||
@@ -1019,13 +1078,12 @@ impl Action {
|
||||
Action::CloseWindow => "Close window",
|
||||
Action::ToggleCursorVisibility => "Hide cursor",
|
||||
Action::CreateNewWindow => "Create new window",
|
||||
Action::ToggleImeInput => "Toggle IME input",
|
||||
Action::ToggleDecorations => "Toggle decorations",
|
||||
Action::ToggleResizable => "Toggle window resizable state",
|
||||
Action::ToggleFullscreen => "Toggle fullscreen",
|
||||
#[cfg(macos_platform)]
|
||||
Action::ToggleSimpleFullscreen => "Toggle simple fullscreen",
|
||||
#[cfg(macos_platform)]
|
||||
Action::ToggleBorderlessGame => "Toggle borderless game mode",
|
||||
Action::ToggleMaximize => "Maximize",
|
||||
Action::Minimize => "Minimize",
|
||||
Action::ToggleResizeIncrements => "Use resize increments when resizing window",
|
||||
@@ -1058,7 +1116,6 @@ impl Action {
|
||||
Action::Message => "Prints a message through a user wake up",
|
||||
Action::ToggleAnimatedFillColor => "Toggle animated fill color",
|
||||
Action::ToggleContinuousRedraw => "Toggle continuous redraw",
|
||||
Action::EmitSurfaceSize => "Periodically print the surface size",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1130,70 +1187,15 @@ fn modifiers_to_string(mods: ModifiersState) -> String {
|
||||
mods_line
|
||||
}
|
||||
|
||||
fn mouse_button_to_string(button: MouseButton) -> Cow<'static, str> {
|
||||
fn mouse_button_to_string(button: MouseButton) -> &'static str {
|
||||
match button {
|
||||
MouseButton::Left => "LMB",
|
||||
MouseButton::Right => "RMB",
|
||||
MouseButton::Middle => "MMB",
|
||||
MouseButton::Back => "Back",
|
||||
MouseButton::Forward => "Forward",
|
||||
other => return format!("button {}", other as u8 + 1).into(),
|
||||
MouseButton::Other(_) => "",
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
#[cfg(web_platform)]
|
||||
fn window_attributes_web() -> WindowAttributesWeb {
|
||||
WindowAttributesWeb::default().with_append(true)
|
||||
}
|
||||
|
||||
#[cfg(wayland_platform)]
|
||||
fn window_attributes_wayland(event_loop: &dyn ActiveEventLoop) -> WindowAttributesWayland {
|
||||
let mut window_attributes = WindowAttributesWayland::default();
|
||||
|
||||
if let Some(token) = event_loop.read_token_from_env() {
|
||||
startup_notify::reset_activation_token_env();
|
||||
info!("Using token {:?} to activate a window", token);
|
||||
window_attributes = window_attributes.with_activation_token(token);
|
||||
}
|
||||
|
||||
window_attributes
|
||||
}
|
||||
|
||||
#[cfg(x11_platform)]
|
||||
fn window_attributes_x11(
|
||||
event_loop: &dyn ActiveEventLoop,
|
||||
) -> Result<WindowAttributesX11, Box<dyn Error>> {
|
||||
let mut window_attributes = WindowAttributesX11::default();
|
||||
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
if let Some(token) = event_loop.read_token_from_env() {
|
||||
startup_notify::reset_activation_token_env();
|
||||
info!("Using token {:?} to activate a window", token);
|
||||
window_attributes = window_attributes.with_activation_token(token);
|
||||
}
|
||||
|
||||
match std::env::var("X11_VISUAL_ID") {
|
||||
Ok(visual_id_str) => {
|
||||
info!("Using X11 visual id {visual_id_str}");
|
||||
let visual_id = visual_id_str.parse()?;
|
||||
window_attributes = window_attributes.with_x11_visual(visual_id);
|
||||
},
|
||||
Err(_) => info!("Set the X11_VISUAL_ID env variable to request specific X11 visual"),
|
||||
}
|
||||
|
||||
match std::env::var("X11_SCREEN_ID") {
|
||||
Ok(screen_id_str) => {
|
||||
info!("Placing the window on X11 screen {screen_id_str}");
|
||||
let screen_id = screen_id_str.parse()?;
|
||||
window_attributes = window_attributes.with_x11_screen(screen_id);
|
||||
},
|
||||
Err(_) => {
|
||||
info!("Set the X11_SCREEN_ID env variable to place the window on non-default screen")
|
||||
},
|
||||
}
|
||||
|
||||
Ok(window_attributes)
|
||||
}
|
||||
|
||||
/// Cursor list to cycle through.
|
||||
@@ -1242,6 +1244,7 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
|
||||
#[cfg(macos_platform)]
|
||||
Binding::new("F", ModifiersState::ALT, Action::ToggleSimpleFullscreen),
|
||||
Binding::new("D", ModifiersState::CONTROL, Action::ToggleDecorations),
|
||||
Binding::new("I", ModifiersState::CONTROL, Action::ToggleImeInput),
|
||||
Binding::new("L", ModifiersState::CONTROL, Action::CycleCursorGrab),
|
||||
Binding::new("P", ModifiersState::CONTROL, Action::ToggleResizeIncrements),
|
||||
Binding::new("R", ModifiersState::CONTROL, Action::ToggleResizable),
|
||||
@@ -1277,9 +1280,6 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
|
||||
Binding::new("T", ModifiersState::META, Action::CreateNewTab),
|
||||
#[cfg(macos_platform)]
|
||||
Binding::new("O", ModifiersState::CONTROL, Action::CycleOptionAsAlt),
|
||||
#[cfg(macos_platform)]
|
||||
Binding::new("B", ModifiersState::CONTROL, Action::ToggleBorderlessGame),
|
||||
Binding::new("S", ModifiersState::ALT, Action::EmitSurfaceSize),
|
||||
Binding::new("S", ModifiersState::CONTROL, Action::Message),
|
||||
];
|
||||
|
||||
@@ -3,12 +3,10 @@
|
||||
fn main() -> Result<(), impl std::error::Error> {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::info;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::dpi::{LogicalPosition, LogicalSize, Position};
|
||||
use winit::event::{ElementState, KeyEvent, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
|
||||
use winit::raw_window_handle::HasRawWindowHandle;
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
@@ -17,20 +15,18 @@ fn main() -> Result<(), impl std::error::Error> {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct WindowData {
|
||||
surface: Surface<OwnedDisplayHandle, Box<dyn Window>>,
|
||||
window: Box<dyn Window>,
|
||||
color: u32,
|
||||
}
|
||||
|
||||
impl WindowData {
|
||||
fn new(context: &Context<OwnedDisplayHandle>, window: Box<dyn Window>, color: u32) -> Self {
|
||||
let surface = Surface::new(context, window).unwrap();
|
||||
Self { surface, color }
|
||||
fn new(window: Box<dyn Window>, color: u32) -> Self {
|
||||
Self { window, color }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Default, Debug)]
|
||||
struct Application {
|
||||
context: Context<OwnedDisplayHandle>,
|
||||
parent_window_id: Option<WindowId>,
|
||||
windows: HashMap<WindowId, WindowData>,
|
||||
}
|
||||
@@ -42,10 +38,10 @@ fn main() -> Result<(), impl std::error::Error> {
|
||||
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
||||
.with_surface_size(LogicalSize::new(640.0f32, 480.0f32));
|
||||
let window = event_loop.create_window(attributes).unwrap();
|
||||
info!("Parent window id: {:?})", window.id());
|
||||
println!("Parent window id: {:?})", window.id());
|
||||
self.parent_window_id = Some(window.id());
|
||||
|
||||
self.windows.insert(window.id(), WindowData::new(&self.context, window, 0xffbbbbbb));
|
||||
self.windows.insert(window.id(), WindowData::new(window, 0xffbbbbbb));
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
@@ -60,12 +56,12 @@ fn main() -> Result<(), impl std::error::Error> {
|
||||
event_loop.exit();
|
||||
},
|
||||
WindowEvent::PointerEntered { device_id: _, .. } => {
|
||||
// On x11, log 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 by some key inputs.
|
||||
// the child windows are always placed at (0, 0) with size (200, 200) in the
|
||||
// parent window, so we also can see this log when we move
|
||||
// the cursor around (200, 200) in parent window.
|
||||
info!("cursor entered in the window {window_id:?}");
|
||||
println!("cursor entered in the window {window_id:?}");
|
||||
},
|
||||
WindowEvent::KeyboardInput {
|
||||
event: KeyEvent { state: ElementState::Pressed, .. },
|
||||
@@ -76,24 +72,18 @@ fn main() -> Result<(), impl std::error::Error> {
|
||||
0xff000000 + 3_u32.pow((child_index + 2).rem_euclid(16) as u32);
|
||||
|
||||
let parent_window = self.windows.get(&self.parent_window_id.unwrap()).unwrap();
|
||||
let child_window = spawn_child_window(
|
||||
parent_window.surface.window().as_ref(),
|
||||
event_loop,
|
||||
child_index,
|
||||
);
|
||||
let child_window =
|
||||
spawn_child_window(parent_window.window.as_ref(), event_loop, child_index);
|
||||
let child_id = child_window.id();
|
||||
info!("Child window created with id: {child_id:?}");
|
||||
self.windows.insert(
|
||||
child_id,
|
||||
WindowData::new(&self.context, child_window, child_color),
|
||||
);
|
||||
println!("Child window created with id: {child_id:?}");
|
||||
self.windows.insert(child_id, WindowData::new(child_window, child_color));
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
if let Some(window) = self.windows.get_mut(&window_id) {
|
||||
if let Some(window) = self.windows.get(&window_id) {
|
||||
if window_id == self.parent_window_id.unwrap() {
|
||||
fill::fill(&mut window.surface);
|
||||
fill::fill_window(window.window.as_ref());
|
||||
} else {
|
||||
fill::fill_with_color(&mut window.surface, window.color);
|
||||
fill::fill_window_with_color(window.window.as_ref(), window.color);
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -127,8 +117,7 @@ fn main() -> Result<(), impl std::error::Error> {
|
||||
}
|
||||
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
event_loop.run_app(Application { context, parent_window_id: None, windows: HashMap::new() })
|
||||
event_loop.run_app(Application::default())
|
||||
}
|
||||
|
||||
#[cfg(not(any(x11_platform, macos_platform, windows_platform)))]
|
||||
@@ -4,13 +4,12 @@ use std::thread;
|
||||
#[cfg(not(web_platform))]
|
||||
use std::time;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::{info, warn};
|
||||
use ::tracing::{info, warn};
|
||||
#[cfg(web_platform)]
|
||||
use web_time as time;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::{ElementState, KeyEvent, StartCause, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, OwnedDisplayHandle};
|
||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProvider};
|
||||
use winit::keyboard::{Key, NamedKey};
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
@@ -53,7 +52,7 @@ struct ControlFlowDemo {
|
||||
request_redraw: bool,
|
||||
wait_cancelled: bool,
|
||||
close_requested: bool,
|
||||
surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
|
||||
window: Option<Box<dyn Window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for ControlFlowDemo {
|
||||
@@ -70,10 +69,7 @@ impl ApplicationHandler for ControlFlowDemo {
|
||||
let window_attributes = WindowAttributes::default().with_title(
|
||||
"Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.",
|
||||
);
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
let surface = Surface::new(&context, window).unwrap();
|
||||
self.surface = Some(surface);
|
||||
self.window = Some(event_loop.create_window(window_attributes).unwrap());
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
@@ -116,9 +112,9 @@ impl ApplicationHandler for ControlFlowDemo {
|
||||
_ => (),
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
let surface = self.surface.as_mut().unwrap();
|
||||
surface.window().pre_present_notify();
|
||||
fill::fill(surface);
|
||||
let window = self.window.as_ref().unwrap();
|
||||
window.pre_present_notify();
|
||||
fill::fill_window(window.as_ref());
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
@@ -126,7 +122,7 @@ impl ApplicationHandler for ControlFlowDemo {
|
||||
|
||||
fn about_to_wait(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||
if self.request_redraw && !self.wait_cancelled && !self.close_requested {
|
||||
self.surface.as_ref().unwrap().window().request_redraw();
|
||||
self.window.as_ref().unwrap().request_redraw();
|
||||
}
|
||||
|
||||
match self.mode {
|
||||
|
Before Width: | Height: | Size: 159 B After Width: | Height: | Size: 159 B |
|
Before Width: | Height: | Size: 129 B After Width: | Height: | Size: 129 B |
|
Before Width: | Height: | Size: 229 B After Width: | Height: | Size: 229 B |
|
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
@@ -1,10 +1,8 @@
|
||||
use std::error::Error;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::info;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
@@ -17,24 +15,27 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let event_loop = EventLoop::new()?;
|
||||
|
||||
let app = Application::default();
|
||||
let app = Application::new();
|
||||
Ok(event_loop.run_app(app)?)
|
||||
}
|
||||
|
||||
/// Application state and event handling.
|
||||
#[derive(Default, Debug)]
|
||||
#[derive(Debug)]
|
||||
struct Application {
|
||||
surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
|
||||
window: Option<Box<dyn Window>>,
|
||||
}
|
||||
|
||||
impl Application {
|
||||
fn new() -> Self {
|
||||
Self { window: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for Application {
|
||||
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||
let window_attributes =
|
||||
WindowAttributes::default().with_title("Drag and drop files on me!");
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
let surface = Surface::new(&context, window).unwrap();
|
||||
self.surface = Some(surface);
|
||||
self.window = Some(event_loop.create_window(window_attributes).unwrap());
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
@@ -48,12 +49,12 @@ impl ApplicationHandler for Application {
|
||||
| WindowEvent::DragEntered { .. }
|
||||
| WindowEvent::DragMoved { .. }
|
||||
| WindowEvent::DragDropped { .. } => {
|
||||
info!("{event:?}");
|
||||
println!("{:?}", event);
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
let surface = self.surface.as_mut().unwrap();
|
||||
surface.window().pre_present_notify();
|
||||
fill::fill(surface);
|
||||
let window = self.window.as_ref().unwrap();
|
||||
window.pre_present_notify();
|
||||
fill::fill_window(window.as_ref());
|
||||
},
|
||||
WindowEvent::CloseRequested => {
|
||||
event_loop.exit();
|
||||
@@ -7,31 +7,24 @@ fn main() -> std::process::ExitCode {
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::info;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::pump_events::{EventLoopExtPumpEvents, PumpStatus};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||
use winit::platform::pump_events::{EventLoopExtPumpEvents, PumpStatus};
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
mod fill;
|
||||
#[path = "util/tracing.rs"]
|
||||
mod tracing;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct PumpDemo {
|
||||
surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
|
||||
window: Option<Box<dyn Window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for PumpDemo {
|
||||
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||
let window_attributes = WindowAttributes::default().with_title("A fantastic window!");
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
self.surface = Some(Surface::new(&context, window).unwrap());
|
||||
self.window = Some(event_loop.create_window(window_attributes).unwrap());
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
@@ -40,28 +33,28 @@ fn main() -> std::process::ExitCode {
|
||||
_window_id: WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
info!("{event:?}");
|
||||
println!("{event:?}");
|
||||
|
||||
let surface = match self.surface.as_mut() {
|
||||
Some(surface) => surface,
|
||||
let window = match self.window.as_ref() {
|
||||
Some(window) => window,
|
||||
None => return,
|
||||
};
|
||||
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::RedrawRequested => {
|
||||
fill::fill(surface);
|
||||
surface.window().request_redraw();
|
||||
fill::fill_window(window.as_ref());
|
||||
window.request_redraw();
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::init();
|
||||
|
||||
let mut event_loop = EventLoop::new().unwrap();
|
||||
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let mut app = PumpDemo::default();
|
||||
|
||||
loop {
|
||||
@@ -76,12 +69,12 @@ fn main() -> std::process::ExitCode {
|
||||
//
|
||||
// Since `pump_events` doesn't block it will be important to
|
||||
// throttle the loop in the app somehow.
|
||||
info!("Update()");
|
||||
println!("Update()");
|
||||
sleep(Duration::from_millis(16));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(ios_platform, web_platform, orbital_platform))]
|
||||
fn main() {
|
||||
panic!("This platform doesn't support pump_events.")
|
||||
println!("This platform doesn't support pump_events.");
|
||||
}
|
||||
@@ -1,47 +1,40 @@
|
||||
#![allow(clippy::single_match)]
|
||||
|
||||
// Limit this example to only compatible platforms.
|
||||
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, orbital_platform))]
|
||||
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform,))]
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use std::time::Duration;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::info;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::run_on_demand::EventLoopExtRunOnDemand;
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
mod fill;
|
||||
#[path = "util/tracing.rs"]
|
||||
mod tracing;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Default, Debug)]
|
||||
struct App {
|
||||
context: Context<OwnedDisplayHandle>,
|
||||
idx: usize,
|
||||
surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
|
||||
window_id: Option<WindowId>,
|
||||
window: Option<Box<dyn Window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
fn about_to_wait(&mut self, _event_loop: &dyn ActiveEventLoop) {
|
||||
if let Some(surface) = self.surface.as_ref() {
|
||||
surface.window().request_redraw();
|
||||
if let Some(window) = self.window.as_ref() {
|
||||
window.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||
let window_attributes = WindowAttributes::default()
|
||||
.with_title(format!("Fantastic window number {}!", self.idx))
|
||||
.with_title("Fantastic window number one!")
|
||||
.with_surface_size(winit::dpi::LogicalSize::new(128.0, 128.0));
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
self.window_id = Some(window.id());
|
||||
|
||||
let surface = Surface::new(&self.context, window).unwrap();
|
||||
self.surface = Some(surface);
|
||||
self.window = Some(window);
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
@@ -51,54 +44,56 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
event: WindowEvent,
|
||||
) {
|
||||
if event == WindowEvent::Destroyed && self.window_id == Some(window_id) {
|
||||
info!("Window {} Destroyed", self.idx);
|
||||
println!(
|
||||
"--------------------------------------------------------- Window {} Destroyed",
|
||||
self.idx
|
||||
);
|
||||
self.window_id = None;
|
||||
event_loop.exit();
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(surface) = self.surface.as_mut() else {
|
||||
return;
|
||||
let window = match self.window.as_mut() {
|
||||
Some(window) => window,
|
||||
None => return,
|
||||
};
|
||||
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
info!("Window {} CloseRequested", self.idx);
|
||||
self.surface = None;
|
||||
println!(
|
||||
"--------------------------------------------------------- Window {} \
|
||||
CloseRequested",
|
||||
self.idx
|
||||
);
|
||||
fill::cleanup_window(window.as_ref());
|
||||
self.window = None;
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
fill::fill(surface);
|
||||
fill::fill_window(window.as_ref());
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::init();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let mut event_loop = EventLoop::new().unwrap();
|
||||
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
let mut app = App { context, idx: 1, surface: None, window_id: None };
|
||||
let mut app = App { idx: 1, ..Default::default() };
|
||||
event_loop.run_app_on_demand(&mut app)?;
|
||||
|
||||
info!("Finished first loop");
|
||||
info!("Waiting 5 seconds");
|
||||
println!("--------------------------------------------------------- Finished first loop");
|
||||
println!("--------------------------------------------------------- Waiting 5 seconds");
|
||||
std::thread::sleep(Duration::from_secs(5));
|
||||
|
||||
app.idx += 1;
|
||||
event_loop.run_app_on_demand(&mut app)?;
|
||||
info!("Finished second loop");
|
||||
println!("--------------------------------------------------------- Finished second loop");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(any(
|
||||
windows_platform,
|
||||
macos_platform,
|
||||
x11_platform,
|
||||
wayland_platform,
|
||||
orbital_platform
|
||||
)))]
|
||||
#[cfg(not(any(windows_platform, macos_platform, x11_platform, wayland_platform,)))]
|
||||
fn main() {
|
||||
panic!("This example is not supported on this platform")
|
||||
println!("This example is not supported on this platform");
|
||||
}
|
||||
152
examples/util/fill.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
//! Fill the window buffer with a solid color.
|
||||
//!
|
||||
//! Launching a window without drawing to it has unpredictable results varying from platform to
|
||||
//! platform. In order to have well-defined examples, this module provides an easy way to
|
||||
//! fill the window buffer with a solid color.
|
||||
//!
|
||||
//! The `softbuffer` crate is used, largely because of its ease of use. `glutin` or `wgpu` could
|
||||
//! also be used to fill the window buffer, but they are more complicated to use.
|
||||
|
||||
#[allow(unused_imports)]
|
||||
pub use platform::cleanup_window;
|
||||
#[allow(unused_imports)]
|
||||
pub use platform::fill_window;
|
||||
#[allow(unused_imports)]
|
||||
pub use platform::fill_window_with_animated_color;
|
||||
#[allow(unused_imports)]
|
||||
pub use platform::fill_window_with_color;
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
mod platform {
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::mem;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use winit::window::{Window, WindowId};
|
||||
|
||||
thread_local! {
|
||||
// NOTE: You should never do things like that, create context and drop it before
|
||||
// you drop the event loop. We do this for brevity to not blow up examples. We use
|
||||
// ManuallyDrop to prevent destructors from running.
|
||||
//
|
||||
// A static, thread-local map of graphics contexts to open windows.
|
||||
static GC: ManuallyDrop<RefCell<Option<GraphicsContext>>> = const { ManuallyDrop::new(RefCell::new(None)) };
|
||||
}
|
||||
|
||||
/// The graphics context used to draw to a window.
|
||||
struct GraphicsContext {
|
||||
/// The global softbuffer context.
|
||||
context: RefCell<Context<&'static dyn Window>>,
|
||||
|
||||
/// The hash map of window IDs to surfaces.
|
||||
surfaces: HashMap<WindowId, Surface<&'static dyn Window, &'static dyn Window>>,
|
||||
}
|
||||
|
||||
impl GraphicsContext {
|
||||
fn new(w: &dyn Window) -> Self {
|
||||
Self {
|
||||
context: RefCell::new(
|
||||
Context::new(unsafe {
|
||||
mem::transmute::<&'_ dyn Window, &'static dyn Window>(w)
|
||||
})
|
||||
.expect("Failed to create a softbuffer context"),
|
||||
),
|
||||
surfaces: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_surface(
|
||||
&mut self,
|
||||
window: &dyn Window,
|
||||
) -> &mut Surface<&'static dyn Window, &'static dyn Window> {
|
||||
self.surfaces.entry(window.id()).or_insert_with(|| {
|
||||
Surface::new(&self.context.borrow(), unsafe {
|
||||
mem::transmute::<&'_ dyn Window, &'static dyn Window>(window)
|
||||
})
|
||||
.expect("Failed to create a softbuffer surface")
|
||||
})
|
||||
}
|
||||
|
||||
fn destroy_surface(&mut self, window: &dyn Window) {
|
||||
self.surfaces.remove(&window.id());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fill_window_with_color(window: &dyn Window, color: u32) {
|
||||
GC.with(|gc| {
|
||||
let size = window.surface_size();
|
||||
let (Some(width), Some(height)) =
|
||||
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Either get the last context used or create a new one.
|
||||
let mut gc = gc.borrow_mut();
|
||||
let surface =
|
||||
gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window);
|
||||
|
||||
// Fill a buffer with a solid color
|
||||
|
||||
surface.resize(width, height).expect("Failed to resize the softbuffer surface");
|
||||
|
||||
let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer");
|
||||
buffer.fill(color);
|
||||
buffer.present().expect("Failed to present the softbuffer buffer");
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn fill_window(window: &dyn Window) {
|
||||
fill_window_with_color(window, 0xff181818);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn fill_window_with_animated_color(window: &dyn Window, start: std::time::Instant) {
|
||||
let time = start.elapsed().as_secs_f32() * 1.5;
|
||||
let blue = (time.sin() * 255.0) as u32;
|
||||
let green = ((time.cos() * 255.0) as u32) << 8;
|
||||
let red = ((1.0 - time.sin() * 255.0) as u32) << 16;
|
||||
let color = red | green | blue;
|
||||
fill_window_with_color(window, color);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn cleanup_window(window: &dyn Window) {
|
||||
GC.with(|gc| {
|
||||
let mut gc = gc.borrow_mut();
|
||||
if let Some(context) = gc.as_mut() {
|
||||
context.destroy_surface(window);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "ios"))]
|
||||
mod platform {
|
||||
#[allow(dead_code)]
|
||||
pub fn fill_window(_window: &dyn winit::window::Window) {
|
||||
// No-op on mobile platforms.
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn fill_window_with_color(_window: &dyn winit::window::Window, _color: u32) {
|
||||
// No-op on mobile platforms.
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn fill_window_with_animated_color(
|
||||
_window: &dyn winit::window::Window,
|
||||
_start: std::time::Instant,
|
||||
) {
|
||||
// No-op on mobile platforms.
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn cleanup_window(_window: &dyn winit::window::Window) {
|
||||
// No-op on mobile platforms.
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,3 @@ pub fn init() {
|
||||
)
|
||||
.init();
|
||||
}
|
||||
|
||||
#[allow(unused_imports)]
|
||||
pub use ::tracing::*;
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
use softbuffer::{Context, Surface};
|
||||
use tracing::info;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
|
||||
#[cfg(web_platform)]
|
||||
use winit::platform::web::WindowAttributesWeb;
|
||||
use winit::platform::web::WindowAttributesExtWeb;
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
@@ -18,7 +16,7 @@ mod tracing;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct App {
|
||||
surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
|
||||
window: Option<Box<dyn Window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
@@ -26,27 +24,26 @@ impl ApplicationHandler for App {
|
||||
#[cfg(not(web_platform))]
|
||||
let window_attributes = WindowAttributes::default();
|
||||
#[cfg(web_platform)]
|
||||
let window_attributes = WindowAttributes::default()
|
||||
.with_platform_attributes(Box::new(WindowAttributesWeb::default().with_append(true)));
|
||||
let window = event_loop.create_window(window_attributes).expect("failed creating window");
|
||||
|
||||
let context =
|
||||
Context::new(event_loop.owned_display_handle()).expect("failed creating context");
|
||||
let surface = Surface::new(&context, window).expect("failed creating surface");
|
||||
self.surface = Some(surface);
|
||||
let window_attributes = WindowAttributes::default().with_append(true);
|
||||
self.window = match event_loop.create_window(window_attributes) {
|
||||
Ok(window) => Some(window),
|
||||
Err(err) => {
|
||||
eprintln!("error creating window: {err}");
|
||||
event_loop.exit();
|
||||
return;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &dyn ActiveEventLoop, _: WindowId, event: WindowEvent) {
|
||||
info!("{event:?}");
|
||||
println!("{event:?}");
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
info!("Close was requested; stopping");
|
||||
println!("Close was requested; stopping");
|
||||
event_loop.exit();
|
||||
},
|
||||
WindowEvent::SurfaceResized(surface_size) => {
|
||||
let surface = self.surface.as_mut().expect("resize event without a surface");
|
||||
fill::resize(surface, surface_size);
|
||||
surface.window().request_redraw();
|
||||
WindowEvent::SurfaceResized(_) => {
|
||||
self.window.as_ref().expect("resize event without a window").request_redraw();
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
// Redraw the application.
|
||||
@@ -55,15 +52,13 @@ impl ApplicationHandler for App {
|
||||
// this event rather than in AboutToWait, since rendering in here allows
|
||||
// the program to gracefully handle redraws requested by the OS.
|
||||
|
||||
let surface = self.surface.as_mut().expect("redraw event without a surface");
|
||||
let window = self.window.as_ref().expect("redraw request without a window");
|
||||
|
||||
// Notify that you're about to draw.
|
||||
surface.window().pre_present_notify();
|
||||
window.pre_present_notify();
|
||||
|
||||
// Draw.
|
||||
let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer");
|
||||
buffer.fill(0xff181818);
|
||||
buffer.present().expect("Failed to present the softbuffer buffer");
|
||||
fill::fill_window(window.as_ref());
|
||||
|
||||
// For contiguous redraw loop you can request a redraw from here.
|
||||
// window.request_redraw();
|
||||
@@ -3,37 +3,29 @@ use std::error::Error;
|
||||
|
||||
#[cfg(x11_platform)]
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
use softbuffer::{Context, Surface};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
|
||||
use winit::platform::x11::WindowAttributesX11;
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
|
||||
use winit::platform::x11::WindowAttributesExtX11;
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
mod fill;
|
||||
#[path = "util/tracing.rs"]
|
||||
mod tracing;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct XEmbedDemo {
|
||||
parent_window_id: u32,
|
||||
surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
|
||||
window: Option<Box<dyn Window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for XEmbedDemo {
|
||||
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||
let mut window_attributes = WindowAttributes::default()
|
||||
let window_attributes = WindowAttributes::default()
|
||||
.with_title("An embedded window!")
|
||||
.with_surface_size(winit::dpi::LogicalSize::new(128.0, 128.0));
|
||||
let x11_attrs =
|
||||
WindowAttributesX11::default().with_embed_parent_window(self.parent_window_id);
|
||||
window_attributes = window_attributes.with_platform_attributes(Box::new(x11_attrs));
|
||||
.with_surface_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||
.with_embed_parent_window(self.parent_window_id);
|
||||
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
let context = Context::new(event_loop.owned_display_handle()).unwrap();
|
||||
let surface = Surface::new(&context, window).unwrap();
|
||||
self.surface = Some(surface);
|
||||
self.window = Some(event_loop.create_window(window_attributes).unwrap());
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
@@ -42,19 +34,19 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
_window_id: WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
let window = self.window.as_ref().unwrap();
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::RedrawRequested => {
|
||||
let surface = self.surface.as_mut().unwrap();
|
||||
surface.window().pre_present_notify();
|
||||
fill::fill(surface);
|
||||
window.pre_present_notify();
|
||||
fill::fill_window(window.as_ref());
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &dyn ActiveEventLoop) {
|
||||
self.surface.as_ref().unwrap().window().request_redraw();
|
||||
self.window.as_ref().unwrap().request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,13 +56,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
.ok_or("Expected a 32-bit X11 window ID as the first argument.")?
|
||||
.parse::<u32>()?;
|
||||
|
||||
tracing::init();
|
||||
tracing_subscriber::fmt::init();
|
||||
let event_loop = EventLoop::new()?;
|
||||
|
||||
Ok(event_loop.run_app(XEmbedDemo { parent_window_id, surface: None })?)
|
||||
Ok(event_loop.run_app(XEmbedDemo { parent_window_id, window: None })?)
|
||||
}
|
||||
|
||||
#[cfg(not(x11_platform))]
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
panic!("This example is only supported on X11 platforms.")
|
||||
println!("This example is only supported on X11 platforms.");
|
||||
Ok(())
|
||||
}
|
||||
@@ -2,11 +2,21 @@
|
||||
|
||||
use crate::event::{DeviceEvent, DeviceId, StartCause, WindowEvent};
|
||||
use crate::event_loop::ActiveEventLoop;
|
||||
#[cfg(macos_platform)]
|
||||
use crate::platform::macos::ApplicationHandlerExtMacOS;
|
||||
use crate::window::WindowId;
|
||||
|
||||
pub mod macos;
|
||||
|
||||
/// The handler of application-level events.
|
||||
///
|
||||
/// See [the top-level docs] for example usage, and [`EventLoopProvider::run_app`] for an overview
|
||||
/// of when events are delivered.
|
||||
///
|
||||
/// This is [dropped] when the event loop is shut down. Note that this only works if you're passing
|
||||
/// the entire state to [`EventLoopProvider::run_app`] (passing `&mut app` won't work).
|
||||
///
|
||||
/// [the top-level docs]: crate
|
||||
/// [`EventLoopProvider::run_app`]: crate::event_loop::EventLoopProvider::run_app
|
||||
/// [dropped]: std::ops::Drop
|
||||
pub trait ApplicationHandler {
|
||||
/// Emitted when new events arrive from the OS to be processed.
|
||||
///
|
||||
@@ -125,9 +135,8 @@ pub trait ApplicationHandler {
|
||||
/// use std::thread;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// use winit::event_loop::EventLoop;
|
||||
/// use winit_core::application::ApplicationHandler;
|
||||
/// use winit_core::event_loop::ActiveEventLoop;
|
||||
/// use winit::application::ApplicationHandler;
|
||||
/// use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
|
||||
///
|
||||
/// struct MyApp {
|
||||
/// receiver: mpsc::Receiver<u64>,
|
||||
@@ -201,7 +210,9 @@ pub trait ApplicationHandler {
|
||||
|
||||
/// Emitted when the OS sends an event to a device.
|
||||
///
|
||||
/// Whether device events are delivered depends on the backend in use.
|
||||
/// For this to be called, it must be enabled with [`EventLoopProvider::listen_device_events`].
|
||||
///
|
||||
/// [`EventLoopProvider::listen_device_events`]: crate::event_loop::EventLoopProvider::listen_device_events
|
||||
fn device_event(
|
||||
&mut self,
|
||||
event_loop: &dyn ActiveEventLoop,
|
||||
@@ -339,8 +350,9 @@ pub trait ApplicationHandler {
|
||||
/// The macOS-specific handler.
|
||||
///
|
||||
/// The return value from this should not change at runtime.
|
||||
#[cfg(macos_platform)]
|
||||
#[inline(always)]
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn macos::ApplicationHandlerExtMacOS> {
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -407,8 +419,9 @@ impl<A: ?Sized + ApplicationHandler> ApplicationHandler for &mut A {
|
||||
(**self).memory_warning(event_loop);
|
||||
}
|
||||
|
||||
#[cfg(macos_platform)]
|
||||
#[inline]
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn macos::ApplicationHandlerExtMacOS> {
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> {
|
||||
(**self).macos_handler()
|
||||
}
|
||||
}
|
||||
@@ -475,8 +488,9 @@ impl<A: ?Sized + ApplicationHandler> ApplicationHandler for Box<A> {
|
||||
(**self).memory_warning(event_loop);
|
||||
}
|
||||
|
||||
#[cfg(macos_platform)]
|
||||
#[inline]
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn macos::ApplicationHandlerExtMacOS> {
|
||||
fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> {
|
||||
(**self).macos_handler()
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,44 @@
|
||||
## 0.31.0-beta.2
|
||||
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 `EventLoopExtRegister::register_app` for being explicit about how the event loop runs on Web.
|
||||
- Add `EventLoopExtNeverReturn::run_app_never_return` for being explicit about how the event loop runs on iOS.
|
||||
- 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`.
|
||||
```
|
||||
|
||||
### Changed
|
||||
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:
|
||||
|
||||
- On Web, avoid throwing an exception in `EventLoop::run_app`, instead preferring to return to the caller.
|
||||
This requires passing a `'static` application to ensure that the application state will live as long as necessary.
|
||||
- On Web, the event loop can now always be re-created once it has finished running.
|
||||
```md
|
||||
- Deprecate `Window` creation outside of `EventLoop::run`
|
||||
|
||||
### Fixed
|
||||
This was done to simply migration in the future. Consider the
|
||||
following code:
|
||||
|
||||
- Fixed panic when calling `Window::set_ime_allowed`.
|
||||
// Code snippet.
|
||||
|
||||
## 0.31.0-beta.1
|
||||
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
|
||||
|
||||
### Added
|
||||
|
||||
@@ -38,7 +61,7 @@
|
||||
- Add `MonitorHandle::current_video_mode()`.
|
||||
- Add `ApplicationHandlerExtMacOS` trait, and a `macos_handler` method to `ApplicationHandler` which returns a `dyn ApplicationHandlerExtMacOS` which allows for macOS specific extensions to winit.
|
||||
- Add a `standard_key_binding` method to the `ApplicationHandlerExtMacOS` trait. This allows handling of standard keybindings such as "go to end of line" on macOS.
|
||||
- On macOS, add `WindowExtMacOS::set_unified_titlebar` and `WindowAttributesMacOS::with_unified_titlebar`
|
||||
- On macOS, add `WindowExtMacOS::set_unified_titlebar` and `WindowAttributesExtMacOS::with_unified_titlebar`
|
||||
to use a larger style of titlebar.
|
||||
- Add `WindowId::into_raw()` and `from_raw()`.
|
||||
- Add `PointerKind`, `PointerSource`, `ButtonSource`, `FingerId`, `primary` and `position` to all
|
||||
@@ -54,17 +77,6 @@
|
||||
- Add `CustomCursorSource::Url`, `CustomCursorSource::from_animation`.
|
||||
- Implement `CustomIconProvider` for `RgbaIcon`.
|
||||
- Add `icon` module that exposes winit's icon API.
|
||||
- `VideoMode::new` to create a `VideoMode`.
|
||||
- `keyboard::ModifiersKey` to track which modifier is exactly pressed.
|
||||
- `ActivationToken::as_raw` to get a ref to raw token.
|
||||
- Each platform now has corresponding `WindowAttributes` struct instead of trait extension.
|
||||
- On Wayland, added implementation for `Window::set_window_icon`
|
||||
- On Wayland, added `PanGesture`, `PinchGesture`, and `RotationGesture`
|
||||
- Add `Window::request_ime_update` to atomically apply set of IME changes.
|
||||
- Add `Ime::DeleteSurrounding` to let the input method delete text.
|
||||
- Add more `ImePurpose` values.
|
||||
- Add `ImeHints` to request particular IME behaviour.
|
||||
- Add Pen input support on Wayland, Windows, and Web via new Pointer event.
|
||||
|
||||
### Changed
|
||||
|
||||
@@ -73,7 +85,7 @@
|
||||
- `ActiveEventLoop::create_window` now returns `Box<dyn Window>`.
|
||||
- `ApplicationHandler` now uses `dyn ActiveEventLoop`.
|
||||
- On Web, let events wake up event loop immediately when using `ControlFlow::Poll`.
|
||||
- Bump MSRV from `1.70` to `1.85`.
|
||||
- Bump MSRV from `1.70` to `1.80`.
|
||||
- Changed `ApplicationHandler::user_event` to `user_wake_up`, removing the
|
||||
generic user event.
|
||||
|
||||
@@ -155,7 +167,6 @@
|
||||
- Rename `VideoModeHandle` to `VideoMode`, now it only stores plain data.
|
||||
- Make `Fullscreen::Exclusive` contain `(MonitorHandle, VideoMode)`.
|
||||
- Reworked the file drag-and-drop API.
|
||||
- On macOS, the default menu uses the bundle name or falls back to the process name as before.
|
||||
|
||||
The `WindowEvent::DroppedFile`, `WindowEvent::HoveredFile` and `WindowEvent::HoveredFileCancelled`
|
||||
events have been removed, and replaced with `WindowEvent::DragEntered`, `WindowEvent::DragMoved`,
|
||||
@@ -184,12 +195,10 @@
|
||||
- Renamed "super" key to "meta", to match the naming in the W3C specification.
|
||||
`NamedKey::Super` still exists, but it's non-functional and deprecated, `NamedKey::Meta` should be used instead.
|
||||
- Move `IconExtWindows` into `WinIcon`.
|
||||
- Move `EventLoopExtPumpEvents` and `PumpStatus` from platform module to `winit::event_loop::pump_events`.
|
||||
- Move `EventLoopExtRunOnDemand` from platform module to `winit::event_loop::run_on_demand`.
|
||||
- Use `NamedKey`, `Code` and `Location` from the `keyboard-types` v0.8 crate.
|
||||
- Deprecate `Window::set_ime_allowed`, `Window::set_ime_cursor_area`, and `Window::set_ime_purpose`.
|
||||
- `Force::normalized()` now takes a `Option<ToolAngle>` to calculate the perpendicular force.
|
||||
- On Windows, don't confine cursor to center of window when grabbed and hidden.
|
||||
- `run_app_on_demand`/`pump_app_events` now accept `&mut dyn ApplicationHandler` instead of generic.
|
||||
- Moved common `EventLoop` methods like `run_app` into `EventLoopProvider` trait.
|
||||
- Moved `event_loop::EventLoop` into `platform::event_loop::EventLoop` keeping the old re-export in place.
|
||||
- `EventLoopProvider::run_app` now takes `Box<dyn ApplicationHandler` instead of owned generic.
|
||||
|
||||
### Removed
|
||||
|
||||
@@ -228,24 +237,11 @@
|
||||
the `Drop` impl on the application handler.
|
||||
- Remove `NamedKey::Space`, match on `Key::Character(" ")` instead.
|
||||
- Remove `PartialEq` impl for `WindowAttributes`.
|
||||
- `WindowAttributesExt*` platform extensions; use `WindowAttributes*` instead.
|
||||
- Remove `Force::Calibrated::altitude_angle` in favor of `ToolAngle::altitude`.
|
||||
|
||||
### Fixed
|
||||
|
||||
- On Orbital, `MonitorHandle::name()` now returns `None` instead of a dummy name.
|
||||
- On Orbital, implement `fullscreen`.
|
||||
- On iOS, fixed `SurfaceResized` and `Window::surface_size` not reporting the size of the actual surface.
|
||||
- On macOS, fixed the scancode conversion for audio volume keys.
|
||||
- On macOS, fixed the scancode conversion for `IntlBackslash`.
|
||||
- On macOS, fixed redundant `SurfaceResized` event at window creation.
|
||||
- On macOS, don't panic on monitors with unknown bit-depths.
|
||||
- On macOS, fixed crash when closing the window on macOS 26+.
|
||||
- On Windows, account for mouse wheel lines per scroll setting for `WindowEvent::MouseWheel`.
|
||||
- On Windows, `Window::theme` will return the correct theme after setting it through `Window::set_theme`.
|
||||
- On Windows, `Window::set_theme` will change the title bar color immediately now.
|
||||
- On Windows 11, prevent incorrect shifting when dragging window onto a monitor with different DPI.
|
||||
- On Windows, avoid returning `SurfaceResized` with size zero when an application is minimized. Let `Window::surface_size` return the pre-minimization window size even while minimized.
|
||||
- On Web, device events are emitted regardless of cursor type.
|
||||
- On Wayland, `axis_value120` scroll events now generate `MouseScrollDelta::LineDelta`
|
||||
- On X11, mouse scroll button events no longer cause duplicated `WindowEvent::MouseWheel` events.
|
||||
@@ -250,7 +250,7 @@
|
||||
- On Web, fix some `WindowBuilder` methods doing nothing.
|
||||
- On Web, fix some `Window` methods using incorrect HTML attributes instead of CSS properties.
|
||||
- On Web, fix the bfcache by not using the `beforeunload` event and map bfcache loading/unloading to `Suspended`/`Resumed` events.
|
||||
- On Web, fix touch input not gaining or losing focus.
|
||||
- On Web, fix touch input not gaining or loosing focus.
|
||||
- On Web, fix touch location to be as accurate as mouse position.
|
||||
- On Web, handle coalesced pointer events, which increases the resolution of pointer inputs.
|
||||
- On Web, implement `Window::focus_window()`.
|
||||
@@ -1,31 +1,3 @@
|
||||
## 0.30.13
|
||||
|
||||
### Added
|
||||
|
||||
- On Wayland, add `Window::set_resize_increments`.
|
||||
|
||||
### Fixed
|
||||
|
||||
- On macOS, fixed crash when dragging non-file content onto window.
|
||||
- On X11, fix `set_hittest` not working on some window managers.
|
||||
- On X11, fix debug mode overflow panic in `set_timestamp`.
|
||||
- On macOS, fix crash in `set_marked_text` when native Pinyin IME sends out-of-bounds `selected_range`.
|
||||
- On Windows, fix `WM_IME_SETCONTEXT` IME UI flag masking on `lParam`.
|
||||
- On Android, populate `KeyEvent::text` and `KeyEvent::text_with_all_modifiers` via `Key::to_text()`
|
||||
|
||||
## 0.30.12
|
||||
|
||||
### Fixed
|
||||
|
||||
- On macOS, fix crash on macOS 26 by using objc2's relax-sign-encoding feature.
|
||||
|
||||
## 0.30.11
|
||||
|
||||
### Fixed
|
||||
|
||||
- On Windows, fixed crash in should_apps_use_dark_mode() for Windows versions < 17763.
|
||||
- On Wayland, fixed `pump_events` driven loop deadlocking when loop was not drained before exit.
|
||||
|
||||
## 0.30.10
|
||||
|
||||
### Added
|
||||
@@ -5,10 +5,9 @@ use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use cursor_icon::CursorIcon;
|
||||
use cursor_icon::CursorIcon;
|
||||
|
||||
use crate::as_any::AsAny;
|
||||
use crate::utils::{impl_dyn_casting, AsAny};
|
||||
|
||||
/// The maximum width and height for a cursor when using [`CustomCursorSource::from_rgba`].
|
||||
pub const MAX_CURSOR_SIZE: u16 = 2048;
|
||||
@@ -51,10 +50,10 @@ impl From<CustomCursor> for Cursor {
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use winit_core::event_loop::ActiveEventLoop;
|
||||
/// # use winit_core::window::Window;
|
||||
/// # use winit::event_loop::ActiveEventLoop;
|
||||
/// # use winit::window::Window;
|
||||
/// # fn scope(event_loop: &dyn ActiveEventLoop, window: &dyn Window) {
|
||||
/// use winit_core::cursor::CustomCursorSource;
|
||||
/// use winit::window::CustomCursorSource;
|
||||
///
|
||||
/// let w = 10;
|
||||
/// let h = 10;
|
||||
@@ -76,7 +75,7 @@ impl From<CustomCursor> for Cursor {
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CustomCursor(pub Arc<dyn CustomCursorProvider>);
|
||||
pub struct CustomCursor(pub(crate) Arc<dyn CustomCursorProvider>);
|
||||
|
||||
pub trait CustomCursorProvider: AsAny + fmt::Debug + Send + Sync {
|
||||
/// Whether a cursor was backed by animation.
|
||||
@@ -236,6 +235,7 @@ impl fmt::Display for BadAnimation {
|
||||
impl Error for BadAnimation {}
|
||||
|
||||
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
|
||||
#[allow(dead_code)]
|
||||
pub struct CursorImage {
|
||||
pub(crate) rgba: Vec<u8>,
|
||||
pub(crate) width: u16,
|
||||
@@ -277,30 +277,6 @@ impl CursorImage {
|
||||
|
||||
Ok(CursorImage { rgba, width, height, hotspot_x, hotspot_y })
|
||||
}
|
||||
|
||||
pub fn buffer(&self) -> &[u8] {
|
||||
self.rgba.as_slice()
|
||||
}
|
||||
|
||||
pub fn buffer_mut(&mut self) -> &mut [u8] {
|
||||
self.rgba.as_mut_slice()
|
||||
}
|
||||
|
||||
pub fn width(&self) -> u16 {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> u16 {
|
||||
self.height
|
||||
}
|
||||
|
||||
pub fn hotspot_x(&self) -> u16 {
|
||||
self.hotspot_x
|
||||
}
|
||||
|
||||
pub fn hotspot_y(&self) -> u16 {
|
||||
self.hotspot_y
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
@@ -321,16 +297,4 @@ impl CursorAnimation {
|
||||
|
||||
Ok(Self { duration, cursors })
|
||||
}
|
||||
|
||||
pub fn duration(&self) -> Duration {
|
||||
self.duration
|
||||
}
|
||||
|
||||
pub fn cursors(&self) -> &[CustomCursor] {
|
||||
self.cursors.as_slice()
|
||||
}
|
||||
|
||||
pub fn into_raw(self) -> (Duration, Vec<CustomCursor>) {
|
||||
(self.duration, self.cursors)
|
||||
}
|
||||
}
|
||||
@@ -19,13 +19,7 @@ pub enum EventLoopError {
|
||||
impl fmt::Display for EventLoopError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::RecreationAttempt => {
|
||||
write!(
|
||||
f,
|
||||
"EventLoop can't be recreated, only a single instance of it is supported (for \
|
||||
cross-platform compatibility)"
|
||||
)
|
||||
},
|
||||
Self::RecreationAttempt => write!(f, "EventLoop can't be recreated"),
|
||||
Self::Os(err) => err.fmt(f),
|
||||
Self::ExitFailure(status) => write!(f, "Exit Failure: {status}"),
|
||||
Self::NotSupported(err) => err.fmt(f),
|
||||
@@ -35,7 +29,11 @@ impl fmt::Display for EventLoopError {
|
||||
|
||||
impl Error for EventLoopError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
if let Self::Os(err) = self { err.source() } else { None }
|
||||
if let Self::Os(err) = self {
|
||||
err.source()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +72,11 @@ impl Display for RequestError {
|
||||
}
|
||||
impl Error for RequestError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
if let Self::Os(err) = self { err.source() } else { None }
|
||||
if let Self::Os(err) = self {
|
||||
err.source()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +100,7 @@ pub struct NotSupportedError {
|
||||
}
|
||||
|
||||
impl NotSupportedError {
|
||||
pub fn new(reason: &'static str) -> Self {
|
||||
pub(crate) fn new(reason: &'static str) -> Self {
|
||||
Self { reason }
|
||||
}
|
||||
}
|
||||
@@ -119,7 +121,8 @@ pub struct OsError {
|
||||
}
|
||||
|
||||
impl OsError {
|
||||
pub fn new(
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn new(
|
||||
line: u32,
|
||||
file: &'static str,
|
||||
error: impl Into<Box<dyn Error + Send + Sync + 'static>>,
|
||||
@@ -141,5 +144,7 @@ impl Error for OsError {
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! os_error {
|
||||
($error:expr) => {{ crate::error::OsError::new(line!(), file!(), $error) }};
|
||||
($error:expr) => {{
|
||||
crate::error::OsError::new(line!(), file!(), $error)
|
||||
}};
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
//! The event enums and assorted supporting types.
|
||||
use std::cell::LazyCell;
|
||||
use std::cmp::Ordering;
|
||||
use std::f64;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Mutex, Weak};
|
||||
#[cfg(not(web_platform))]
|
||||
use std::time::Instant;
|
||||
|
||||
use dpi::{PhysicalPosition, PhysicalSize};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use smol_str::SmolStr;
|
||||
#[cfg(web_platform)]
|
||||
use web_time::Instant;
|
||||
|
||||
use crate::Instant;
|
||||
use crate::dpi::{PhysicalPosition, PhysicalSize};
|
||||
use crate::error::RequestError;
|
||||
use crate::event_loop::AsyncRequestSerial;
|
||||
use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState};
|
||||
@@ -46,6 +46,10 @@ pub enum StartCause {
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum WindowEvent {
|
||||
/// 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))]
|
||||
/// Delivered in response to [`request_activation_token`].
|
||||
///
|
||||
/// [`request_activation_token`]: crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token
|
||||
ActivationTokenDone { serial: AsyncRequestSerial, token: ActivationToken },
|
||||
|
||||
/// The size of the window's surface has changed.
|
||||
@@ -156,8 +160,6 @@ pub enum WindowEvent {
|
||||
Ime(Ime),
|
||||
|
||||
/// The pointer has moved on the window.
|
||||
///
|
||||
/// Should be emitted regardless of window focus.
|
||||
PointerMoved {
|
||||
device_id: Option<DeviceId>,
|
||||
|
||||
@@ -186,8 +188,6 @@ pub enum WindowEvent {
|
||||
},
|
||||
|
||||
/// The pointer has entered the window.
|
||||
///
|
||||
/// Should be emitted regardless of window focus.
|
||||
PointerEntered {
|
||||
device_id: Option<DeviceId>,
|
||||
|
||||
@@ -213,8 +213,6 @@ pub enum WindowEvent {
|
||||
},
|
||||
|
||||
/// The pointer has left the window.
|
||||
///
|
||||
/// Should be emitted regardless of window focus.
|
||||
PointerLeft {
|
||||
device_id: Option<DeviceId>,
|
||||
|
||||
@@ -269,25 +267,11 @@ pub enum WindowEvent {
|
||||
button: ButtonSource,
|
||||
},
|
||||
|
||||
/// Multi-finger hold gesture on the touchpad or touchscreen without movement.
|
||||
///
|
||||
/// The `phase` field indicates the lifecycle of the hold gesture:
|
||||
/// - `Started`: One or more fingers are in contact with the touchpad/touchscreen.
|
||||
/// - `Ended`: All fingers have been lifted from the touchpad/touchscreen.
|
||||
/// - `Cancelled`: The hold gesture was interrupted, for example when another finger touches the
|
||||
/// touchpad (causing a new `Started` event with more fingers), or when movement begins and
|
||||
/// transitions to other gestures like pinch, pan, or rotation.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - Only available on **Wayland**.
|
||||
HoldGesture { device_id: Option<DeviceId>, phase: TouchPhase },
|
||||
|
||||
/// Two-finger pinch gesture, often used for magnification.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - Only available on **macOS**, **iOS**, and **Wayland**.
|
||||
/// - Only available on **macOS** and **iOS**.
|
||||
/// - On iOS, not recognized by default. It must be enabled when needed.
|
||||
PinchGesture {
|
||||
device_id: Option<DeviceId>,
|
||||
@@ -303,7 +287,7 @@ pub enum WindowEvent {
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - Only available on **iOS** and **Wayland**.
|
||||
/// - Only available on **iOS**.
|
||||
/// - On iOS, not recognized by default. It must be enabled when needed.
|
||||
PanGesture {
|
||||
device_id: Option<DeviceId>,
|
||||
@@ -339,7 +323,7 @@ pub enum WindowEvent {
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - Only available on **macOS**, **iOS**, and **Wayland**.
|
||||
/// - Only available on **macOS** and **iOS**.
|
||||
/// - On iOS, not recognized by default. It must be enabled when needed.
|
||||
RotationGesture {
|
||||
device_id: Option<DeviceId>,
|
||||
@@ -458,7 +442,6 @@ pub enum PointerKind {
|
||||
///
|
||||
/// **macOS:** Unsupported.
|
||||
Touch(FingerId),
|
||||
TabletTool(TabletToolKind),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -509,13 +492,6 @@ pub enum PointerSource {
|
||||
/// force will be 0.5 when a button is pressed or 0.0 otherwise.
|
||||
force: Option<Force>,
|
||||
},
|
||||
TabletTool {
|
||||
/// Describes as which tool kind the interaction happened.
|
||||
kind: TabletToolKind,
|
||||
|
||||
/// Describes how the tool was held and used.
|
||||
data: TabletToolData,
|
||||
},
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -524,7 +500,6 @@ impl From<PointerSource> for PointerKind {
|
||||
match source {
|
||||
PointerSource::Mouse => Self::Mouse,
|
||||
PointerSource::Touch { finger_id, .. } => Self::Touch(finger_id),
|
||||
PointerSource::TabletTool { kind, .. } => Self::TabletTool(kind),
|
||||
PointerSource::Unknown => Self::Unknown,
|
||||
}
|
||||
}
|
||||
@@ -534,21 +509,8 @@ impl From<PointerSource> for PointerKind {
|
||||
///
|
||||
/// **Wayland/X11:** [`Unknown`](Self::Unknown) device types are converted to known variants by the
|
||||
/// system.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum ButtonSource {
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// ### macOS
|
||||
///
|
||||
/// Users may expect holding [<kbd>CTRL</kbd>](ModifiersState::CONTROL) while
|
||||
/// clicking [`MouseButton::Left`] to result in a "secondary" click, but the way these
|
||||
/// clicks behave natively is slightly different from how a physical secondary
|
||||
/// button press would, depending on the content under the cursor when clicked. If
|
||||
/// applications want this behavior they should implement it themselves by interpreting
|
||||
/// [`Left`](MouseButton::Left) clicks as secondary when
|
||||
/// [<kbd>CTRL</kbd>](ModifiersState::CONTROL) is held and their internal logic deems it
|
||||
/// appropriate for the content under the pointer.
|
||||
/// See also https://github.com/rust-windowing/winit/issues/4469.
|
||||
Mouse(MouseButton),
|
||||
/// See [`PointerSource::Touch`] for more details.
|
||||
///
|
||||
@@ -559,27 +521,25 @@ pub enum ButtonSource {
|
||||
finger_id: FingerId,
|
||||
force: Option<Force>,
|
||||
},
|
||||
TabletTool {
|
||||
kind: TabletToolKind,
|
||||
button: TabletToolButton,
|
||||
data: TabletToolData,
|
||||
},
|
||||
/// A pointer button of unknown source.
|
||||
///
|
||||
/// Codes are undefined and may not be reproducible across platforms or winit versions.
|
||||
Unknown(u16),
|
||||
}
|
||||
|
||||
impl ButtonSource {
|
||||
/// Try to convert a [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no
|
||||
/// Convert any [`ButtonSource`] to an equivalent [`MouseButton`]. If a pointer type has no
|
||||
/// special handling in an application, this method can be used to handle it like any generic
|
||||
/// mouse input.
|
||||
pub fn mouse_button(self) -> Option<MouseButton> {
|
||||
pub fn mouse_button(self) -> MouseButton {
|
||||
match self {
|
||||
ButtonSource::Mouse(mouse) => Some(mouse),
|
||||
ButtonSource::Touch { .. } => Some(MouseButton::Left),
|
||||
ButtonSource::TabletTool { button, .. } => button.into(),
|
||||
ButtonSource::Unknown(_) => None,
|
||||
ButtonSource::Mouse(mouse) => mouse,
|
||||
ButtonSource::Touch { .. } => MouseButton::Left,
|
||||
ButtonSource::Unknown(button) => match button {
|
||||
0 => MouseButton::Left,
|
||||
1 => MouseButton::Middle,
|
||||
2 => MouseButton::Right,
|
||||
3 => MouseButton::Back,
|
||||
4 => MouseButton::Forward,
|
||||
_ => MouseButton::Other(button),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -603,14 +563,16 @@ impl DeviceId {
|
||||
/// Convert the [`DeviceId`] into the underlying integer.
|
||||
///
|
||||
/// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic.
|
||||
pub const fn into_raw(self) -> i64 {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const fn into_raw(self) -> i64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Construct a [`DeviceId`] from the underlying integer.
|
||||
///
|
||||
/// This should only be called with integers returned from [`DeviceId::into_raw`].
|
||||
pub const fn from_raw(id: i64) -> Self {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const fn from_raw(id: i64) -> Self {
|
||||
Self(id)
|
||||
}
|
||||
}
|
||||
@@ -626,14 +588,16 @@ impl FingerId {
|
||||
/// Convert the [`FingerId`] into the underlying integer.
|
||||
///
|
||||
/// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic.
|
||||
pub const fn into_raw(self) -> usize {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const fn into_raw(self) -> usize {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Construct a [`FingerId`] from the underlying integer.
|
||||
///
|
||||
/// This should only be called with integers returned from [`FingerId::into_raw`].
|
||||
pub const fn from_raw(id: usize) -> Self {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const fn from_raw(id: usize) -> Self {
|
||||
Self(id)
|
||||
}
|
||||
}
|
||||
@@ -658,8 +622,17 @@ pub enum DeviceEvent {
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Only returns raw data, not OS accelerated, if [`CursorGrabMode::Locked`] is used
|
||||
/// and browser support is available.
|
||||
/// and browser support is available, see
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = "[`ActiveEventLoopExtWeb::is_cursor_lock_raw()`][crate::platform::web::ActiveEventLoopExtWeb::is_cursor_lock_raw()]."
|
||||
)]
|
||||
#[cfg_attr(
|
||||
not(web_platform),
|
||||
doc = "`ActiveEventLoopExtWeb::is_cursor_lock_raw()`."
|
||||
)]
|
||||
///
|
||||
#[rustfmt::skip]
|
||||
/// [`CursorGrabMode::Locked`]: crate::window::CursorGrabMode::Locked
|
||||
PointerMotion {
|
||||
/// (x, y) change in position in unspecified units.
|
||||
@@ -792,12 +765,12 @@ pub struct KeyEvent {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// In games, you often want to ignore repeated key events - this can be
|
||||
/// In games, you often want to ignore repated key events - this can be
|
||||
/// done by ignoring events where this property is set.
|
||||
///
|
||||
/// ```no_run
|
||||
/// use winit_core::event::{ElementState, KeyEvent, WindowEvent};
|
||||
/// use winit_core::keyboard::{KeyCode, PhysicalKey};
|
||||
/// use winit::event::{ElementState, KeyEvent, WindowEvent};
|
||||
/// use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
/// # let window_event = WindowEvent::RedrawRequested; // To make the example compile
|
||||
/// match window_event {
|
||||
/// WindowEvent::KeyboardInput {
|
||||
@@ -817,14 +790,13 @@ pub struct KeyEvent {
|
||||
/// ```
|
||||
pub repeat: bool,
|
||||
|
||||
/// Similar to [`text`][Self::text], except that this is affected by <kbd>Ctrl</kbd> and may
|
||||
/// produce ASCII control characters.
|
||||
/// Similar to [`text`][Self::text], except that this is affected by <kbd>Ctrl</kbd>.
|
||||
///
|
||||
/// For example, pressing <kbd>Ctrl</kbd>+<kbd>space</kbd> produces `Some("\x00")`.
|
||||
/// For example, pressing <kbd>Ctrl</kbd>+<kbd>a</kbd> produces `Some("\x01")`.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Android:** This field is always the same value as `text`.
|
||||
/// - **Android:** Unimplemented, this field is always the same value as `text`.
|
||||
/// - **iOS:** Unimplemented, this field is always the same value as `text`.
|
||||
/// - **Web:** Unsupported, this field is always the same value as `text`.
|
||||
pub text_with_all_modifiers: Option<SmolStr>,
|
||||
@@ -853,59 +825,54 @@ pub struct KeyEvent {
|
||||
pub struct Modifiers {
|
||||
pub(crate) state: ModifiersState,
|
||||
|
||||
// NOTE: Currently active modifiers keys (logically, but not necessarily physically, pressed).
|
||||
// NOTE: Currently pressed modifiers keys.
|
||||
//
|
||||
// The field providing a metadata, it shouldn't be used as a source of truth.
|
||||
pub(crate) pressed_mods: ModifiersKeys,
|
||||
}
|
||||
|
||||
impl Modifiers {
|
||||
/// Create a new modifiers from state and pressed mods.
|
||||
pub fn new(state: ModifiersState, pressed_mods: ModifiersKeys) -> Self {
|
||||
Self { state, pressed_mods }
|
||||
}
|
||||
|
||||
/// The logical state of the modifiers.
|
||||
/// The state of the modifiers.
|
||||
pub fn state(&self) -> ModifiersState {
|
||||
self.state
|
||||
}
|
||||
|
||||
/// The logical state of the left shift key.
|
||||
/// The state of the left shift key.
|
||||
pub fn lshift_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::LSHIFT)
|
||||
}
|
||||
|
||||
/// The logical state of the right shift key.
|
||||
/// The state of the right shift key.
|
||||
pub fn rshift_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::RSHIFT)
|
||||
}
|
||||
|
||||
/// The logical state of the left alt key.
|
||||
/// The state of the left alt key.
|
||||
pub fn lalt_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::LALT)
|
||||
}
|
||||
|
||||
/// The logical state of the right alt key.
|
||||
/// The state of the right alt key.
|
||||
pub fn ralt_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::RALT)
|
||||
}
|
||||
|
||||
/// The logical state of the left control key.
|
||||
/// The state of the left control key.
|
||||
pub fn lcontrol_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::LCONTROL)
|
||||
}
|
||||
|
||||
/// The logical state of the right control key.
|
||||
/// The state of the right control key.
|
||||
pub fn rcontrol_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::RCONTROL)
|
||||
}
|
||||
|
||||
/// The logical state of the left super key.
|
||||
/// The state of the left super key.
|
||||
pub fn lsuper_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::LMETA)
|
||||
}
|
||||
|
||||
/// The logical state of the right super key.
|
||||
/// The state of the right super key.
|
||||
pub fn rsuper_state(&self) -> ModifiersKeyState {
|
||||
self.mod_state(ModifiersKeys::RMETA)
|
||||
}
|
||||
@@ -927,8 +894,6 @@ impl From<ModifiersState> for Modifiers {
|
||||
|
||||
/// Describes [input method](https://en.wikipedia.org/wiki/Input_method) events.
|
||||
///
|
||||
/// The `Ime` events must be applied in the order they arrive.
|
||||
///
|
||||
/// This is also called a "composition event".
|
||||
///
|
||||
/// Most keypresses using a latin-like keyboard layout simply generate a
|
||||
@@ -985,7 +950,7 @@ pub enum Ime {
|
||||
/// position. When it's `None`, the cursor should be hidden. When `String` is an empty string
|
||||
/// this indicates that preedit was cleared.
|
||||
///
|
||||
/// The cursor position is byte-wise indexed, assuming UTF-8.
|
||||
/// The cursor position is byte-wise indexed.
|
||||
Preedit(String, Option<(usize, usize)>),
|
||||
|
||||
/// Notifies when text should be inserted into the editor widget.
|
||||
@@ -993,20 +958,6 @@ pub enum Ime {
|
||||
/// Right before this event winit will send empty [`Self::Preedit`] event.
|
||||
Commit(String),
|
||||
|
||||
/// Delete text surrounding the cursor or selection.
|
||||
///
|
||||
/// This event does not affect either the pre-edit string.
|
||||
/// This means that the application must first remove the pre-edit,
|
||||
/// then execute the deletion, then insert the removed text back.
|
||||
///
|
||||
/// This event assumes text is stored in UTF-8.
|
||||
DeleteSurrounding {
|
||||
/// Bytes to remove before the selection
|
||||
before_bytes: usize,
|
||||
/// Bytes to remove after the selection
|
||||
after_bytes: usize,
|
||||
},
|
||||
|
||||
/// Notifies when the IME was disabled.
|
||||
///
|
||||
/// After receiving this event you won't get any more [`Preedit`][Self::Preedit] or
|
||||
@@ -1020,24 +971,15 @@ pub enum Ime {
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
pub enum TouchPhase {
|
||||
/// Initial touch contact or gesture start, for example when one or more fingers touch the
|
||||
/// screen or touchpad.
|
||||
Started,
|
||||
/// The touch contact point changed, for example without lifting the finger.
|
||||
Moved,
|
||||
/// All touch contact points have been lifted from the touchscreen or touchpad.
|
||||
///
|
||||
/// This event is important as it should clear any state or event in flight that was
|
||||
/// generated by the preceding `Started` and `Moved` events.
|
||||
Ended,
|
||||
/// The event was cancelled and should cancel any event in flight and clear state.
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
/// Describes the force of a touch event
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[doc(alias = "Pressure")]
|
||||
pub enum Force {
|
||||
/// On iOS, the force is calibrated so that the same number corresponds to
|
||||
/// roughly the same amount of pressure on the screen regardless of the
|
||||
@@ -1048,7 +990,7 @@ pub enum Force {
|
||||
///
|
||||
/// The force reported by Apple Pencil is measured along the axis of the
|
||||
/// pencil. If you want a force perpendicular to the device, you need to
|
||||
/// calculate this value using the [`TabletToolAngle::altitude`] value.
|
||||
/// calculate this value using the `altitude_angle` value.
|
||||
force: f64,
|
||||
/// The maximum possible force for a touch.
|
||||
///
|
||||
@@ -1069,17 +1011,9 @@ impl Force {
|
||||
/// Instead of normalizing the force, you should prefer to handle
|
||||
/// [`Force::Calibrated`] so that the amount of force the user has to apply is
|
||||
/// consistent across devices.
|
||||
///
|
||||
/// Passing in a [`TabletToolAngle`], returns the perpendicular force.
|
||||
pub fn normalized(&self, angle: Option<TabletToolAngle>) -> f64 {
|
||||
pub fn normalized(&self) -> f64 {
|
||||
match self {
|
||||
Force::Calibrated { force, max_possible_force } => {
|
||||
let force = match angle {
|
||||
Some(TabletToolAngle { altitude, .. }) => force / altitude.sin(),
|
||||
None => *force,
|
||||
};
|
||||
force / max_possible_force
|
||||
},
|
||||
Force::Calibrated { force, max_possible_force } => force / max_possible_force,
|
||||
Force::Normalized(force) => *force,
|
||||
}
|
||||
}
|
||||
@@ -1091,260 +1025,6 @@ pub type AxisId = u32;
|
||||
/// Identifier for a specific button on some device.
|
||||
pub type ButtonId = u32;
|
||||
|
||||
/// Tablet of the tablet tool.
|
||||
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
#[non_exhaustive]
|
||||
pub enum TabletToolKind {
|
||||
#[default]
|
||||
Pen,
|
||||
Eraser,
|
||||
Brush,
|
||||
Pencil,
|
||||
Airbrush,
|
||||
Finger,
|
||||
Mouse,
|
||||
Lens,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
pub struct TabletToolData {
|
||||
/// The force applied to the tool against the surface.
|
||||
///
|
||||
/// When the force information is not available, [`None`] is returned.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Has no mechanism to detect support, so this will always be [`Some`].
|
||||
pub force: Option<Force>,
|
||||
/// Represents normalized tangential pressure, also known as barrel pressure. In the range of
|
||||
/// -1 to 1. 0 means no tangential pressure is applied. [`None`] means backend or device has no
|
||||
/// support.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Has no mechanism to detect support, so this will always be [`Some`] with a value
|
||||
/// of 0.
|
||||
pub tangential_force: Option<f32>,
|
||||
/// The clockwise rotation in degrees of a tool around its own major axis. E.g. twisting a pen
|
||||
/// around its length. In the range of 0 to 359. [`None`] means backend or device has no
|
||||
/// support.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Has no mechanism to detect support, so this will always be [`Some`] with a value
|
||||
/// of 0.
|
||||
pub twist: Option<u16>,
|
||||
/// The plane angle in degrees. [`None`] means backend or device has no support.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Has no mechanism to detect support, so this will always be [`Some`] with default
|
||||
/// values.
|
||||
pub tilt: Option<TabletToolTilt>,
|
||||
/// The angular position in radians. [`None`] means backend or device has no support.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Has no mechanism to detect device support, so this will always be [`Some`] with
|
||||
/// default values unless browser support is lacking.
|
||||
pub angle: Option<TabletToolAngle>,
|
||||
}
|
||||
|
||||
impl TabletToolData {
|
||||
/// Returns [`TabletToolTilt`] if present or calculates it from [`TabletToolAngle`].
|
||||
pub fn tilt(self) -> Option<TabletToolTilt> {
|
||||
if let Some(tilt) = self.tilt { Some(tilt) } else { self.angle.map(TabletToolAngle::tilt) }
|
||||
}
|
||||
|
||||
/// Returns [`TabletToolAngle`] if present or calculates it from [`TabletToolTilt`].
|
||||
pub fn angle(self) -> Option<TabletToolAngle> {
|
||||
if let Some(angle) = self.angle {
|
||||
Some(angle)
|
||||
} else {
|
||||
self.tilt.map(TabletToolTilt::angle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The plane angle in degrees of a tool.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
pub struct TabletToolTilt {
|
||||
/// The plane angle in degrees between the surface Y-Z plane and the plane containing the tool
|
||||
/// and the surface Y axis. Positive values are to the right. In the range of -90 to 90. 0
|
||||
/// means the tool is perpendicular to the surface and is the default.
|
||||
///
|
||||
/// 
|
||||
///
|
||||
/// <sub>
|
||||
/// For image attribution, see the
|
||||
/// <a href="https://github.com/rust-windowing/winit/blob/master/winit/docs/ATTRIBUTION.md">
|
||||
/// ATTRIBUTION.md
|
||||
/// </a>
|
||||
/// file.
|
||||
/// </sub>
|
||||
pub x: i8,
|
||||
/// The plane angle in degrees between the surface X-Z plane and the plane containing the tool
|
||||
/// and the surface X axis. Positive values are towards the user. In the range of -90 to
|
||||
/// 90. 0 means the tool is perpendicular to the surface and is the default.
|
||||
///
|
||||
/// 
|
||||
///
|
||||
/// <sub>
|
||||
/// For image attribution, see the
|
||||
/// <a href="https://github.com/rust-windowing/winit/blob/master/winit/docs/ATTRIBUTION.md">
|
||||
/// ATTRIBUTION.md
|
||||
/// </a>
|
||||
/// file.
|
||||
/// </sub>
|
||||
pub y: i8,
|
||||
}
|
||||
|
||||
impl TabletToolTilt {
|
||||
pub fn angle(self) -> TabletToolAngle {
|
||||
// See <https://www.w3.org/TR/2024/WD-pointerevents3-20240326/#converting-between-tiltx-tilty-and-altitudeangle-azimuthangle>.
|
||||
|
||||
use std::f64::consts::*;
|
||||
|
||||
const PI_0_5: f64 = FRAC_PI_2;
|
||||
const PI_1_5: f64 = 3. * FRAC_PI_2;
|
||||
const PI_2: f64 = 2. * PI;
|
||||
|
||||
let x = LazyCell::new(|| f64::from(self.x).to_radians());
|
||||
let y = LazyCell::new(|| f64::from(self.y).to_radians());
|
||||
|
||||
let mut azimuth = 0.;
|
||||
|
||||
if self.x == 0 {
|
||||
match self.y.cmp(&0) {
|
||||
Ordering::Greater => azimuth = PI_0_5,
|
||||
Ordering::Less => azimuth = PI_1_5,
|
||||
Ordering::Equal => (),
|
||||
}
|
||||
} else if self.y == 0 {
|
||||
if self.x < 0 {
|
||||
azimuth = PI;
|
||||
}
|
||||
} else if self.x.abs() == 90 || self.y.abs() == 90 {
|
||||
// not enough information to calculate azimuth
|
||||
azimuth = 0.;
|
||||
} else {
|
||||
// Non-boundary case: neither tiltX nor tiltY is equal to 0 or +-90
|
||||
azimuth = f64::atan2(y.tan(), x.tan());
|
||||
|
||||
if azimuth < 0. {
|
||||
azimuth += PI_2;
|
||||
}
|
||||
}
|
||||
|
||||
let altitude;
|
||||
|
||||
if self.x.abs() == 90 || self.y.abs() == 90 {
|
||||
altitude = 0.;
|
||||
} else if self.x == 0 {
|
||||
altitude = PI_0_5 - y.abs();
|
||||
} else if self.y == 0 {
|
||||
altitude = PI_0_5 - x.abs();
|
||||
} else {
|
||||
// Non-boundary case: neither tiltX nor tiltY is equal to 0 or +-90
|
||||
altitude = f64::atan(1. / f64::sqrt(x.tan().powi(2) + y.tan().powi(2)));
|
||||
}
|
||||
|
||||
TabletToolAngle { altitude, azimuth }
|
||||
}
|
||||
}
|
||||
|
||||
/// The angular position in radians of a tool.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
pub struct TabletToolAngle {
|
||||
/// The altitude angle in radians between the tools perpendicular position to the surface and
|
||||
/// the surface X-Y plane. In the range of 0, parallel to the surface, to π/2, perpendicular to
|
||||
/// the surface. π/2 means the tool is perpendicular to the surface and is the default.
|
||||
///
|
||||
/// 
|
||||
///
|
||||
/// <sub>
|
||||
/// For image attribution, see the
|
||||
/// <a href="https://github.com/rust-windowing/winit/blob/master/docs/res/ATTRIBUTION.md">
|
||||
/// ATTRIBUTION.md
|
||||
/// </a>
|
||||
/// file.
|
||||
/// </sub>
|
||||
pub altitude: f64,
|
||||
/// The azimuth angle in radiants representing the rotation between the major axis of the tool
|
||||
/// and the surface X-Y plane. In the range of 0, 3 o'clock, progressively increasing clockwise
|
||||
/// to 2π. 0 means the tool is at 3 o'clock or is perpendicular to the surface (`altitude` of
|
||||
/// π/2) and is the default.
|
||||
///
|
||||
/// 
|
||||
///
|
||||
/// <sub>
|
||||
/// For image attribution, see the
|
||||
/// <a href="https://github.com/rust-windowing/winit/blob/master/docs/res/ATTRIBUTION.md">
|
||||
/// ATTRIBUTION.md
|
||||
/// </a>
|
||||
/// file.
|
||||
/// </sub>
|
||||
pub azimuth: f64,
|
||||
}
|
||||
|
||||
impl Default for TabletToolAngle {
|
||||
fn default() -> Self {
|
||||
Self { altitude: f64::consts::FRAC_2_PI, azimuth: 0. }
|
||||
}
|
||||
}
|
||||
|
||||
impl TabletToolAngle {
|
||||
pub fn tilt(self) -> TabletToolTilt {
|
||||
// See <https://www.w3.org/TR/2024/WD-pointerevents3-20240326/#converting-between-tiltx-tilty-and-altitudeangle-azimuthangle>.
|
||||
|
||||
use std::f64::consts::*;
|
||||
|
||||
const PI_0_5: f64 = FRAC_PI_2;
|
||||
const PI_1_5: f64 = 3. * FRAC_PI_2;
|
||||
const PI_2: f64 = 2. * PI;
|
||||
|
||||
let mut x = 0.;
|
||||
let mut y = 0.;
|
||||
|
||||
if self.altitude == 0. {
|
||||
if self.azimuth == 0. || self.azimuth == PI_2 {
|
||||
x = FRAC_PI_2;
|
||||
} else if self.azimuth == PI_0_5 {
|
||||
y = FRAC_PI_2;
|
||||
} else if self.azimuth == PI {
|
||||
x = -FRAC_PI_2;
|
||||
} else if self.azimuth == PI_1_5 {
|
||||
y = -FRAC_PI_2;
|
||||
} else if self.azimuth > 0. && self.azimuth < PI_0_5 {
|
||||
x = FRAC_PI_2;
|
||||
y = FRAC_PI_2;
|
||||
} else if self.azimuth > PI_0_5 && self.azimuth < PI {
|
||||
x = -FRAC_PI_2;
|
||||
y = FRAC_PI_2;
|
||||
} else if self.azimuth > PI && self.azimuth < PI_1_5 {
|
||||
x = -FRAC_PI_2;
|
||||
y = -FRAC_PI_2;
|
||||
} else if self.azimuth > PI_1_5 && self.azimuth < PI_2 {
|
||||
x = FRAC_PI_2;
|
||||
y = -FRAC_PI_2;
|
||||
}
|
||||
}
|
||||
|
||||
if self.altitude != 0. {
|
||||
let altitude = self.altitude.tan();
|
||||
|
||||
x = f64::atan(f64::cos(self.azimuth) / altitude);
|
||||
y = f64::atan(f64::sin(self.azimuth) / altitude);
|
||||
}
|
||||
|
||||
TabletToolTilt { x: x.to_degrees().round() as i8, y: y.to_degrees().round() as i8 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Describes the input state of a key.
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
@@ -1360,138 +1040,23 @@ impl ElementState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Identifies a button of a mouse controller.
|
||||
/// Describes a button of a mouse controller.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// The first three buttons should be supported on all platforms.
|
||||
/// [`Self::Back`] and [`Self::Forward`] are supported on most platforms
|
||||
/// (when using a compatible mouse).
|
||||
///
|
||||
/// - **Android, iOS:** Currently not supported.
|
||||
/// - **Orbital:** Only left/right/middle buttons are supported at this time.
|
||||
/// - **Web, Windows:** Supports left/right/middle/back/forward buttons.
|
||||
/// - **Wayland:** Supports buttons 0..=15.
|
||||
/// - **macOS:** Supports all button variants.
|
||||
/// - **X11:** Technically supports further buttons than this (0..=250), these are emitted in
|
||||
/// `ButtonSource::Unknown`.
|
||||
/// **macOS:** `Back` and `Forward` might not work with all hardware.
|
||||
/// **Orbital:** `Back` and `Forward` are unsupported due to orbital not supporting them.
|
||||
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[repr(u8)]
|
||||
pub enum MouseButton {
|
||||
/// The primary (usually left) button
|
||||
Left = 0,
|
||||
/// The secondary (usually right) button
|
||||
Right = 1,
|
||||
/// The tertiary (usually middle) button
|
||||
Middle = 2,
|
||||
/// The first side button, frequently assigned a back function
|
||||
Back = 3,
|
||||
/// The second side button, frequently assigned a forward function
|
||||
Forward = 4,
|
||||
/// The sixth button
|
||||
Button6 = 5,
|
||||
/// The seventh button
|
||||
Button7 = 6,
|
||||
/// The eighth button
|
||||
Button8 = 7,
|
||||
/// The ninth button
|
||||
Button9 = 8,
|
||||
/// The tenth button
|
||||
Button10 = 9,
|
||||
/// The eleventh button
|
||||
Button11 = 10,
|
||||
/// The twelfth button
|
||||
Button12 = 11,
|
||||
/// The thirteenth button
|
||||
Button13 = 12,
|
||||
/// The fourteenth button
|
||||
Button14 = 13,
|
||||
/// The fifteenth button
|
||||
Button15 = 14,
|
||||
/// The sixteenth button
|
||||
Button16 = 15,
|
||||
Button17 = 16,
|
||||
Button18 = 17,
|
||||
Button19 = 18,
|
||||
Button20 = 19,
|
||||
Button21 = 20,
|
||||
Button22 = 21,
|
||||
Button23 = 22,
|
||||
Button24 = 23,
|
||||
Button25 = 24,
|
||||
Button26 = 25,
|
||||
Button27 = 26,
|
||||
Button28 = 27,
|
||||
Button29 = 28,
|
||||
Button30 = 29,
|
||||
Button31 = 30,
|
||||
Button32 = 31,
|
||||
}
|
||||
|
||||
impl MouseButton {
|
||||
/// Construct from a `u8` if within the range `0..=31`
|
||||
pub fn try_from_u8(b: u8) -> Option<MouseButton> {
|
||||
Some(match b {
|
||||
0 => MouseButton::Left,
|
||||
1 => MouseButton::Right,
|
||||
2 => MouseButton::Middle,
|
||||
3 => MouseButton::Back,
|
||||
4 => MouseButton::Forward,
|
||||
5 => MouseButton::Button6,
|
||||
6 => MouseButton::Button7,
|
||||
7 => MouseButton::Button8,
|
||||
8 => MouseButton::Button9,
|
||||
9 => MouseButton::Button10,
|
||||
10 => MouseButton::Button11,
|
||||
11 => MouseButton::Button12,
|
||||
12 => MouseButton::Button13,
|
||||
13 => MouseButton::Button14,
|
||||
14 => MouseButton::Button15,
|
||||
15 => MouseButton::Button16,
|
||||
16 => MouseButton::Button17,
|
||||
17 => MouseButton::Button18,
|
||||
18 => MouseButton::Button19,
|
||||
19 => MouseButton::Button20,
|
||||
20 => MouseButton::Button21,
|
||||
21 => MouseButton::Button22,
|
||||
22 => MouseButton::Button23,
|
||||
23 => MouseButton::Button24,
|
||||
24 => MouseButton::Button25,
|
||||
25 => MouseButton::Button26,
|
||||
26 => MouseButton::Button27,
|
||||
27 => MouseButton::Button28,
|
||||
28 => MouseButton::Button29,
|
||||
29 => MouseButton::Button30,
|
||||
30 => MouseButton::Button31,
|
||||
31 => MouseButton::Button32,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Describes a button of a tool, e.g. a pen.
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
pub enum TabletToolButton {
|
||||
Contact,
|
||||
Barrel,
|
||||
Left,
|
||||
Right,
|
||||
Middle,
|
||||
Back,
|
||||
Forward,
|
||||
Other(u16),
|
||||
}
|
||||
|
||||
impl From<TabletToolButton> for Option<MouseButton> {
|
||||
fn from(tool: TabletToolButton) -> Self {
|
||||
Some(match tool {
|
||||
TabletToolButton::Contact => MouseButton::Left,
|
||||
TabletToolButton::Barrel => MouseButton::Right,
|
||||
TabletToolButton::Other(1) => MouseButton::Middle,
|
||||
TabletToolButton::Other(3) => MouseButton::Back,
|
||||
TabletToolButton::Other(4) => MouseButton::Forward,
|
||||
TabletToolButton::Other(_) => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Describes a difference in the mouse scroll wheel state.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
@@ -1526,7 +1091,8 @@ pub struct SurfaceSizeWriter {
|
||||
}
|
||||
|
||||
impl SurfaceSizeWriter {
|
||||
pub fn new(new_surface_size: Weak<Mutex<PhysicalSize<u32>>>) -> Self {
|
||||
#[cfg(not(orbital_platform))]
|
||||
pub(crate) fn new(new_surface_size: Weak<Mutex<PhysicalSize<u32>>>) -> Self {
|
||||
Self { new_surface_size }
|
||||
}
|
||||
|
||||
@@ -1542,15 +1108,6 @@ impl SurfaceSizeWriter {
|
||||
Err(RequestError::Ignored)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the currently stashed surface size.
|
||||
pub fn surface_size(&self) -> Result<PhysicalSize<u32>, RequestError> {
|
||||
if let Some(inner) = self.new_surface_size.upgrade() {
|
||||
Ok(*inner.lock().unwrap())
|
||||
} else {
|
||||
Err(RequestError::Ignored)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for SurfaceSizeWriter {
|
||||
@@ -1565,8 +1122,7 @@ impl Eq for SurfaceSizeWriter {}
|
||||
mod tests {
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
use dpi::PhysicalPosition;
|
||||
|
||||
use crate::dpi::PhysicalPosition;
|
||||
use crate::event;
|
||||
|
||||
macro_rules! foreach_event {
|
||||
@@ -1622,7 +1178,7 @@ mod tests {
|
||||
primary: true,
|
||||
state: event::ElementState::Pressed,
|
||||
position: (0, 0).into(),
|
||||
button: event::ButtonSource::Unknown(0),
|
||||
button: event::MouseButton::Other(0).into(),
|
||||
});
|
||||
with_window_event(PointerButton {
|
||||
device_id: None,
|
||||
@@ -1675,85 +1231,16 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tilt_angle_conversions() {
|
||||
use std::f64::consts::*;
|
||||
|
||||
use event::{TabletToolAngle, TabletToolTilt};
|
||||
|
||||
// See <https://github.com/web-platform-tests/wpt/blob/5af3e9c2a2aba76ade00f0dbc3486e50a74a4506/pointerevents/pointerevent_tiltX_tiltY_to_azimuth_altitude.html#L11-L23>.
|
||||
const TILT_TO_ANGLE: &[(TabletToolTilt, TabletToolAngle)] = &[
|
||||
(TabletToolTilt { x: 0, y: 0 }, TabletToolAngle { altitude: FRAC_PI_2, azimuth: 0. }),
|
||||
(TabletToolTilt { x: 0, y: 90 }, TabletToolAngle { altitude: 0., azimuth: FRAC_PI_2 }),
|
||||
(TabletToolTilt { x: 0, y: -90 }, TabletToolAngle {
|
||||
altitude: 0.,
|
||||
azimuth: 3. * FRAC_PI_2,
|
||||
}),
|
||||
(TabletToolTilt { x: 90, y: 0 }, TabletToolAngle { altitude: 0., azimuth: 0. }),
|
||||
(TabletToolTilt { x: 90, y: 90 }, TabletToolAngle { altitude: 0., azimuth: 0. }),
|
||||
(TabletToolTilt { x: 90, y: -90 }, TabletToolAngle { altitude: 0., azimuth: 0. }),
|
||||
(TabletToolTilt { x: -90, y: 0 }, TabletToolAngle { altitude: 0., azimuth: PI }),
|
||||
(TabletToolTilt { x: -90, y: 90 }, TabletToolAngle { altitude: 0., azimuth: 0. }),
|
||||
(TabletToolTilt { x: -90, y: -90 }, TabletToolAngle { altitude: 0., azimuth: 0. }),
|
||||
(TabletToolTilt { x: 0, y: 45 }, TabletToolAngle {
|
||||
altitude: FRAC_PI_4,
|
||||
azimuth: FRAC_PI_2,
|
||||
}),
|
||||
(TabletToolTilt { x: 0, y: -45 }, TabletToolAngle {
|
||||
altitude: FRAC_PI_4,
|
||||
azimuth: 3. * FRAC_PI_2,
|
||||
}),
|
||||
(TabletToolTilt { x: 45, y: 0 }, TabletToolAngle { altitude: FRAC_PI_4, azimuth: 0. }),
|
||||
(TabletToolTilt { x: -45, y: 0 }, TabletToolAngle { altitude: FRAC_PI_4, azimuth: PI }),
|
||||
];
|
||||
|
||||
for (tilt, angle) in TILT_TO_ANGLE {
|
||||
assert_eq!(tilt.angle(), *angle, "{tilt:?}");
|
||||
}
|
||||
|
||||
// See <https://github.com/web-platform-tests/wpt/blob/5af3e9c2a2aba76ade00f0dbc3486e50a74a4506/pointerevents/pointerevent_tiltX_tiltY_to_azimuth_altitude.html#L38-L46>.
|
||||
const ANGLE_TO_TILT: &[(TabletToolAngle, TabletToolTilt)] = &[
|
||||
(TabletToolAngle { altitude: 0., azimuth: 0. }, TabletToolTilt { x: 90, y: 0 }),
|
||||
(TabletToolAngle { altitude: FRAC_PI_4, azimuth: 0. }, TabletToolTilt { x: 45, y: 0 }),
|
||||
(TabletToolAngle { altitude: FRAC_PI_2, azimuth: 0. }, TabletToolTilt { x: 0, y: 0 }),
|
||||
(TabletToolAngle { altitude: 0., azimuth: FRAC_PI_2 }, TabletToolTilt { x: 0, y: 90 }),
|
||||
(TabletToolAngle { altitude: FRAC_PI_4, azimuth: FRAC_PI_2 }, TabletToolTilt {
|
||||
x: 0,
|
||||
y: 45,
|
||||
}),
|
||||
(TabletToolAngle { altitude: 0., azimuth: PI }, TabletToolTilt { x: -90, y: 0 }),
|
||||
(TabletToolAngle { altitude: FRAC_PI_4, azimuth: PI }, TabletToolTilt { x: -45, y: 0 }),
|
||||
(TabletToolAngle { altitude: 0., azimuth: 3. * FRAC_PI_2 }, TabletToolTilt {
|
||||
x: 0,
|
||||
y: -90,
|
||||
}),
|
||||
(TabletToolAngle { altitude: FRAC_PI_4, azimuth: 3. * FRAC_PI_2 }, TabletToolTilt {
|
||||
x: 0,
|
||||
y: -45,
|
||||
}),
|
||||
];
|
||||
|
||||
for (angle, tilt) in ANGLE_TO_TILT {
|
||||
assert_eq!(angle.tilt(), *tilt, "{angle:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_force_normalize() {
|
||||
let force = event::Force::Normalized(0.0);
|
||||
assert_eq!(force.normalized(None), 0.0);
|
||||
assert_eq!(force.normalized(), 0.0);
|
||||
|
||||
let force2 = event::Force::Calibrated { force: 5.0, max_possible_force: 2.5 };
|
||||
assert_eq!(force2.normalized(None), 2.0);
|
||||
assert_eq!(force2.normalized(), 2.0);
|
||||
|
||||
let force3 = event::Force::Calibrated { force: 5.0, max_possible_force: 2.5 };
|
||||
assert_eq!(
|
||||
force3.normalized(Some(event::TabletToolAngle {
|
||||
altitude: std::f64::consts::PI / 2.0,
|
||||
azimuth: 0.
|
||||
})),
|
||||
2.0
|
||||
);
|
||||
assert_eq!(force3.normalized(), 2.0);
|
||||
}
|
||||
|
||||
#[allow(clippy::clone_on_copy)]
|
||||
@@ -1,39 +1,132 @@
|
||||
pub mod never_return;
|
||||
pub mod pump_events;
|
||||
pub mod register;
|
||||
pub mod run_on_demand;
|
||||
|
||||
use std::fmt::{self, Debug};
|
||||
use std::sync::Arc;
|
||||
//! The [`EventLoop`] struct and assorted supporting types, including
|
||||
//! [`ControlFlow`].
|
||||
//!
|
||||
//! If you want to send custom events to the event loop, use
|
||||
//! [`EventLoop::create_proxy`] to acquire an [`EventLoopProxy`] and call its
|
||||
//! [`wake_up`][EventLoopProxy::wake_up] method. Then during handling the wake up
|
||||
//! you can poll your event sources.
|
||||
//!
|
||||
//! See the root-level documentation for information on how to create and use an event loop to
|
||||
//! handle events.
|
||||
use std::fmt;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::time::Duration;
|
||||
use std::sync::Arc;
|
||||
#[cfg(not(web_platform))]
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use rwh_06::{DisplayHandle, HandleError, HasDisplayHandle};
|
||||
#[cfg(web_platform)]
|
||||
use web_time::{Duration, Instant};
|
||||
|
||||
use crate::Instant;
|
||||
use crate::as_any::AsAny;
|
||||
use crate::cursor::{CustomCursor, CustomCursorSource};
|
||||
use crate::error::RequestError;
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::error::{EventLoopError, RequestError};
|
||||
use crate::monitor::MonitorHandle;
|
||||
use crate::window::{Theme, Window, WindowAttributes};
|
||||
pub use crate::platform::event_loop::*;
|
||||
use crate::utils::{impl_dyn_casting, AsAny};
|
||||
use crate::window::{CustomCursor, CustomCursorSource, Theme, Window, WindowAttributes};
|
||||
|
||||
/// Common interface to describe event loop.
|
||||
pub trait EventLoopProvider: AsAny + fmt::Debug {
|
||||
/// Run the application with the event loop on the calling thread.
|
||||
///
|
||||
/// ## Event loop flow
|
||||
///
|
||||
/// This function internally handles the different parts of a traditional event-handling loop.
|
||||
/// You can imagine this method as being implemented like this:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// let mut start_cause = StartCause::Init;
|
||||
///
|
||||
/// // Run the event loop.
|
||||
/// while !event_loop.exiting() {
|
||||
/// // Wake up.
|
||||
/// app.new_events(event_loop, start_cause);
|
||||
///
|
||||
/// // Indicate that surfaces can now safely be created.
|
||||
/// if start_cause == StartCause::Init {
|
||||
/// app.can_create_surfaces(event_loop);
|
||||
/// }
|
||||
///
|
||||
/// // Handle proxy wake-up event.
|
||||
/// if event_loop.proxy_wake_up_set() {
|
||||
/// event_loop.proxy_wake_up_clear();
|
||||
/// app.proxy_wake_up(event_loop);
|
||||
/// }
|
||||
///
|
||||
/// // Handle actions done by the user / system such as moving the cursor, resizing the
|
||||
/// // window, changing the window theme, etc.
|
||||
/// for event in event_loop.events() {
|
||||
/// match event {
|
||||
/// window event => app.window_event(event_loop, window_id, event),
|
||||
/// device event => app.device_event(event_loop, device_id, event),
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // Handle redraws.
|
||||
/// for window_id in event_loop.pending_redraws() {
|
||||
/// app.window_event(event_loop, window_id, WindowEvent::RedrawRequested);
|
||||
/// }
|
||||
///
|
||||
/// // Done handling events, wait until we're woken up again.
|
||||
/// app.about_to_wait(event_loop);
|
||||
/// start_cause = event_loop.wait_if_necessary();
|
||||
/// }
|
||||
///
|
||||
/// // Finished running, drop application state.
|
||||
/// drop(app);
|
||||
/// ```
|
||||
///
|
||||
/// This is of course a very coarse-grained overview, and leaves out timing details like
|
||||
/// [`ControlFlow::WaitUntil`] and life-cycle methods like [`ApplicationHandler::resumed`], but
|
||||
/// it should give you an idea of how things fit together.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **iOS:** Will never return to the caller and so values not passed to this function will
|
||||
/// *not* be dropped before the process exits.
|
||||
/// - **Web:** Will _act_ as if it never returns to the caller by throwing a Javascript
|
||||
/// exception (that Rust doesn't see) that will also mean that the rest of the function is
|
||||
/// never executed and any values not passed to this function will *not* be dropped.
|
||||
///
|
||||
/// Web applications are recommended to use
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = " [`EventLoopExtWeb::spawn_app()`][crate::platform::web::EventLoopExtWeb::spawn_app()]"
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = " `EventLoopExtWeb::spawn_app()`")]
|
||||
/// [^1] instead of [`run_app()`] to avoid the need for the Javascript exception trick, and to
|
||||
/// make it clearer that the event loop runs asynchronously (via the browser's own,
|
||||
/// internal, event loop) and doesn't block the current thread of execution like it does
|
||||
/// on other platforms.
|
||||
///
|
||||
/// This function won't be available with `target_feature = "exception-handling"`.
|
||||
///
|
||||
/// [^1]: `spawn_app()` is only available on the Web platform.
|
||||
///
|
||||
/// [`set_control_flow()`]: ActiveEventLoop::set_control_flow()
|
||||
/// [`run_app()`]: Self::run_app()
|
||||
fn run_app(self, app: impl ApplicationHandler) -> Result<(), EventLoopError>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
pub trait ActiveEventLoop: AsAny + fmt::Debug {
|
||||
/// Creates an [`EventLoopProxy`] that can be used to dispatch user events
|
||||
/// to the main event loop, possibly from another thread.
|
||||
fn create_proxy(&self) -> EventLoopProxy;
|
||||
|
||||
/// Create the window.
|
||||
/// Gets a persistent reference to the underlying platform display.
|
||||
///
|
||||
/// Possible causes of error include denied permission, incompatible system, and lack of memory.
|
||||
/// See the [`OwnedDisplayHandle`] type for more information.
|
||||
fn owned_display_handle(&self) -> OwnedDisplayHandle;
|
||||
|
||||
/// Change if or when [`DeviceEvent`]s are captured.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
/// See [`ActiveEventLoop::listen_device_events`] for details.
|
||||
///
|
||||
/// - **Web:** The window is created but not inserted into the Web page automatically. Please
|
||||
/// see the Web platform module for more information.
|
||||
fn create_window(
|
||||
&self,
|
||||
window_attributes: WindowAttributes,
|
||||
) -> Result<Box<dyn Window>, RequestError>;
|
||||
/// [`DeviceEvent`]: crate::event::DeviceEvent
|
||||
fn listen_device_events(&self, allowed: DeviceEvents);
|
||||
|
||||
/// Sets the [`ControlFlow`].
|
||||
fn set_control_flow(&self, control_flow: ControlFlow);
|
||||
|
||||
/// Create custom cursor.
|
||||
///
|
||||
@@ -44,169 +137,9 @@ pub trait ActiveEventLoop: AsAny + fmt::Debug {
|
||||
&self,
|
||||
custom_cursor: CustomCursorSource,
|
||||
) -> Result<CustomCursor, RequestError>;
|
||||
|
||||
/// Returns the list of all the monitors available on the system.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Only returns the current monitor without `detailed monitor permissions`.
|
||||
fn available_monitors(&self) -> Box<dyn Iterator<Item = MonitorHandle>>;
|
||||
|
||||
/// Returns the primary monitor of the system.
|
||||
///
|
||||
/// Returns `None` if it can't identify any monitor as a primary one.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Wayland:** Always returns `None`.
|
||||
/// - **Web:** Always returns `None` without `detailed monitor permissions`.
|
||||
fn primary_monitor(&self) -> Option<MonitorHandle>;
|
||||
|
||||
/// Change if or when [`DeviceEvent`]s are captured.
|
||||
///
|
||||
/// Since the [`DeviceEvent`] capture can lead to high CPU usage for unfocused windows, winit
|
||||
/// will ignore them by default for unfocused windows on Linux/BSD. This method allows changing
|
||||
/// this at runtime to explicitly capture them again.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Wayland / macOS / iOS / Android / Orbital:** Unsupported.
|
||||
///
|
||||
/// [`DeviceEvent`]: crate::event::DeviceEvent
|
||||
fn listen_device_events(&self, allowed: DeviceEvents);
|
||||
|
||||
/// Returns the current system theme.
|
||||
///
|
||||
/// Returns `None` if it cannot be determined on the current platform.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **iOS / Android / Wayland / x11 / Orbital:** Unsupported.
|
||||
fn system_theme(&self) -> Option<Theme>;
|
||||
|
||||
/// Sets the [`ControlFlow`].
|
||||
fn set_control_flow(&self, control_flow: ControlFlow);
|
||||
|
||||
/// Gets the current [`ControlFlow`].
|
||||
fn control_flow(&self) -> ControlFlow;
|
||||
|
||||
/// Stop the event loop.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// ### iOS
|
||||
///
|
||||
/// It is not possible to programmatically exit/quit an application on iOS, so this function is
|
||||
/// a no-op there. See also [this technical Q&A][qa1561].
|
||||
///
|
||||
/// [qa1561]: https://developer.apple.com/library/archive/qa/qa1561/_index.html
|
||||
fn exit(&self);
|
||||
|
||||
/// Returns whether the [`ActiveEventLoop`] is about to stop.
|
||||
///
|
||||
/// Set by [`exit()`][Self::exit].
|
||||
fn exiting(&self) -> bool;
|
||||
|
||||
/// Gets a persistent reference to the underlying platform display.
|
||||
///
|
||||
/// See the [`OwnedDisplayHandle`] type for more information.
|
||||
fn owned_display_handle(&self) -> OwnedDisplayHandle;
|
||||
|
||||
/// Get the raw-window-handle handle.
|
||||
fn rwh_06_handle(&self) -> &dyn HasDisplayHandle;
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for dyn ActiveEventLoop + '_ {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
self.rwh_06_handle().display_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl_dyn_casting!(ActiveEventLoop);
|
||||
|
||||
/// Control the [`ActiveEventLoop`], possibly from a different thread, without referencing it
|
||||
/// directly.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct EventLoopProxy {
|
||||
pub(crate) proxy: Arc<dyn EventLoopProxyProvider>,
|
||||
}
|
||||
|
||||
impl EventLoopProxy {
|
||||
/// Wake up the [`ActiveEventLoop`], resulting in [`ApplicationHandler::proxy_wake_up()`] being
|
||||
/// called.
|
||||
///
|
||||
/// Calls to this method are coalesced into a single call to [`proxy_wake_up`], see the
|
||||
/// documentation on that for details.
|
||||
///
|
||||
/// If the event loop is no longer running, this is a no-op.
|
||||
///
|
||||
/// [`proxy_wake_up`]: crate::application::ApplicationHandler::proxy_wake_up
|
||||
/// [`ApplicationHandler::proxy_wake_up()`]: crate::application::ApplicationHandler::proxy_wake_up
|
||||
///
|
||||
/// # Platform-specific
|
||||
///
|
||||
/// - **Windows**: The wake-up may be ignored under high contention, see [#3687].
|
||||
///
|
||||
/// [#3687]: https://github.com/rust-windowing/winit/pull/3687
|
||||
pub fn wake_up(&self) {
|
||||
self.proxy.wake_up();
|
||||
}
|
||||
|
||||
pub fn new(proxy: Arc<dyn EventLoopProxyProvider>) -> Self {
|
||||
Self { proxy }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EventLoopProxyProvider: Send + Sync + Debug {
|
||||
/// See [`EventLoopProxy::wake_up`] for details.
|
||||
fn wake_up(&self);
|
||||
}
|
||||
|
||||
/// A proxy for the underlying display handle.
|
||||
///
|
||||
/// The purpose of this type is to provide a cheaply cloneable handle to the underlying
|
||||
/// display handle. This is often used by graphics APIs to connect to the underlying APIs.
|
||||
/// It is difficult to keep a handle to the underlying event loop type or the [`ActiveEventLoop`]
|
||||
/// type. In contrast, this type involves no lifetimes and can be persisted for as long as
|
||||
/// needed.
|
||||
///
|
||||
/// For all platforms, this is one of the following:
|
||||
///
|
||||
/// - A zero-sized type that is likely optimized out.
|
||||
/// - A reference-counted pointer to the underlying type.
|
||||
#[derive(Clone)]
|
||||
pub struct OwnedDisplayHandle {
|
||||
pub(crate) handle: Arc<dyn HasDisplayHandle + Send + Sync>,
|
||||
}
|
||||
|
||||
impl OwnedDisplayHandle {
|
||||
pub fn new(handle: Arc<dyn HasDisplayHandle + Send + Sync>) -> Self {
|
||||
Self { handle }
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for OwnedDisplayHandle {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
self.handle.display_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for OwnedDisplayHandle {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("OwnedDisplayHandle").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for OwnedDisplayHandle {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self.display_handle(), other.display_handle()) {
|
||||
(Ok(lhs), Ok(rhs)) => lhs == rhs,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for OwnedDisplayHandle {}
|
||||
impl_dyn_casting!(EventLoopProvider);
|
||||
|
||||
/// Set through [`ActiveEventLoop::set_control_flow()`].
|
||||
///
|
||||
@@ -253,6 +186,205 @@ impl ControlFlow {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ActiveEventLoop: AsAny + fmt::Debug {
|
||||
/// Creates an [`EventLoopProxy`] that can be used to dispatch user events
|
||||
/// to the main event loop, possibly from another thread.
|
||||
fn create_proxy(&self) -> EventLoopProxy;
|
||||
|
||||
/// Create the window.
|
||||
///
|
||||
/// Possible causes of error include denied permission, incompatible system, and lack of memory.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Web:** The window is created but not inserted into the Web page automatically. Please
|
||||
/// see the Web platform module for more information.
|
||||
fn create_window(
|
||||
&self,
|
||||
window_attributes: WindowAttributes,
|
||||
) -> Result<Box<dyn Window>, RequestError>;
|
||||
|
||||
/// Create custom cursor.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **iOS / Android / Orbital:** Unsupported.
|
||||
fn create_custom_cursor(
|
||||
&self,
|
||||
custom_cursor: CustomCursorSource,
|
||||
) -> Result<CustomCursor, RequestError>;
|
||||
|
||||
/// Returns the list of all the monitors available on the system.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Only returns the current monitor without
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]."
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = "detailed monitor permissions.")]
|
||||
fn available_monitors(&self) -> Box<dyn Iterator<Item = MonitorHandle>>;
|
||||
|
||||
/// Returns the primary monitor of the system.
|
||||
///
|
||||
/// Returns `None` if it can't identify any monitor as a primary one.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Wayland:** Always returns `None`.
|
||||
/// - **Web:** Always returns `None` without
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = " [detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]."
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = " detailed monitor permissions.")]
|
||||
fn primary_monitor(&self) -> Option<MonitorHandle>;
|
||||
|
||||
/// Change if or when [`DeviceEvent`]s are captured.
|
||||
///
|
||||
/// Since the [`DeviceEvent`] capture can lead to high CPU usage for unfocused windows, winit
|
||||
/// will ignore them by default for unfocused windows on Linux/BSD. This method allows changing
|
||||
/// this at runtime to explicitly capture them again.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Wayland / macOS / iOS / Android / Orbital:** Unsupported.
|
||||
///
|
||||
/// [`DeviceEvent`]: crate::event::DeviceEvent
|
||||
fn listen_device_events(&self, allowed: DeviceEvents);
|
||||
|
||||
/// Returns the current system theme.
|
||||
///
|
||||
/// Returns `None` if it cannot be determined on the current platform.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **iOS / Android / Wayland / x11 / Orbital:** Unsupported.
|
||||
fn system_theme(&self) -> Option<Theme>;
|
||||
|
||||
/// Sets the [`ControlFlow`].
|
||||
fn set_control_flow(&self, control_flow: ControlFlow);
|
||||
|
||||
/// Gets the current [`ControlFlow`].
|
||||
fn control_flow(&self) -> ControlFlow;
|
||||
|
||||
/// Stop the event loop.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// ### iOS
|
||||
///
|
||||
/// It is not possible to programmatically exit/quit an application on iOS, so this function is
|
||||
/// a no-op there. See also [this technical Q&A][qa1561].
|
||||
///
|
||||
/// [qa1561]: https://developer.apple.com/library/archive/qa/qa1561/_index.html
|
||||
fn exit(&self);
|
||||
|
||||
/// Returns whether the [`EventLoop`] is about to stop.
|
||||
///
|
||||
/// Set by [`exit()`][Self::exit].
|
||||
fn exiting(&self) -> bool;
|
||||
|
||||
/// Gets a persistent reference to the underlying platform display.
|
||||
///
|
||||
/// See the [`OwnedDisplayHandle`] type for more information.
|
||||
fn owned_display_handle(&self) -> OwnedDisplayHandle;
|
||||
|
||||
/// Get the raw-window-handle handle.
|
||||
fn rwh_06_handle(&self) -> &dyn HasDisplayHandle;
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for dyn ActiveEventLoop + '_ {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
self.rwh_06_handle().display_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl_dyn_casting!(ActiveEventLoop);
|
||||
|
||||
/// A proxy for the underlying display handle.
|
||||
///
|
||||
/// The purpose of this type is to provide a cheaply cloneable handle to the underlying
|
||||
/// display handle. This is often used by graphics APIs to connect to the underlying APIs.
|
||||
/// It is difficult to keep a handle to the [`EventLoop`] type or the [`ActiveEventLoop`]
|
||||
/// type. In contrast, this type involves no lifetimes and can be persisted for as long as
|
||||
/// needed.
|
||||
///
|
||||
/// For all platforms, this is one of the following:
|
||||
///
|
||||
/// - A zero-sized type that is likely optimized out.
|
||||
/// - A reference-counted pointer to the underlying type.
|
||||
#[derive(Clone)]
|
||||
pub struct OwnedDisplayHandle {
|
||||
pub(crate) handle: Arc<dyn HasDisplayHandle>,
|
||||
}
|
||||
|
||||
impl OwnedDisplayHandle {
|
||||
pub(crate) fn new(handle: Arc<dyn HasDisplayHandle>) -> Self {
|
||||
Self { handle }
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for OwnedDisplayHandle {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
self.handle.display_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for OwnedDisplayHandle {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("OwnedDisplayHandle").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for OwnedDisplayHandle {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match (self.display_handle(), other.display_handle()) {
|
||||
(Ok(lhs), Ok(rhs)) => lhs == rhs,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for OwnedDisplayHandle {}
|
||||
|
||||
pub(crate) trait EventLoopProxyProvider: Send + Sync + fmt::Debug {
|
||||
/// See [`EventLoopProxy::wake_up`] for details.
|
||||
fn wake_up(&self);
|
||||
}
|
||||
|
||||
/// Control the [`EventLoop`], possibly from a different thread, without referencing it directly.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct EventLoopProxy {
|
||||
pub(crate) proxy: Arc<dyn EventLoopProxyProvider>,
|
||||
}
|
||||
|
||||
impl EventLoopProxy {
|
||||
/// Wake up the [`EventLoop`], resulting in [`ApplicationHandler::proxy_wake_up()`] being
|
||||
/// called.
|
||||
///
|
||||
/// Calls to this method are coalesced into a single call to [`proxy_wake_up`], see the
|
||||
/// documentation on that for details.
|
||||
///
|
||||
/// If the event loop is no longer running, this is a no-op.
|
||||
///
|
||||
/// [`proxy_wake_up`]: ApplicationHandler::proxy_wake_up
|
||||
///
|
||||
/// # Platform-specific
|
||||
///
|
||||
/// - **Windows**: The wake-up may be ignored under high contention, see [#3687].
|
||||
///
|
||||
/// [#3687]: https://github.com/rust-windowing/winit/pull/3687
|
||||
pub fn wake_up(&self) {
|
||||
self.proxy.wake_up();
|
||||
}
|
||||
|
||||
pub(crate) fn new(proxy: Arc<dyn EventLoopProxyProvider>) -> Self {
|
||||
Self { proxy }
|
||||
}
|
||||
}
|
||||
|
||||
/// Control when device events are captured.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
@@ -281,7 +413,9 @@ pub struct AsyncRequestSerial {
|
||||
}
|
||||
|
||||
impl AsyncRequestSerial {
|
||||
pub fn get() -> Self {
|
||||
// TODO(kchibisov): Remove `cfg` when the clipboard will be added.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn get() -> Self {
|
||||
static CURRENT_SERIAL: AtomicUsize = AtomicUsize::new(0);
|
||||
// NOTE: We rely on wrap around here, while the user may just request
|
||||
// in the loop usize::MAX times that's issue is considered on them.
|
||||
@@ -1,29 +1,30 @@
|
||||
use std::error::Error;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::{fmt, io, mem};
|
||||
|
||||
use crate::as_any::AsAny;
|
||||
use crate::utils::{impl_dyn_casting, AsAny};
|
||||
|
||||
pub(crate) const PIXEL_SIZE: usize = mem::size_of::<u32>();
|
||||
pub(crate) const PIXEL_SIZE: usize = mem::size_of::<Pixel>();
|
||||
|
||||
/// An icon used for the window titlebar, taskbar, etc.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Icon(pub Arc<dyn IconProvider>);
|
||||
pub struct Icon(pub(crate) Arc<dyn IconProvider>);
|
||||
|
||||
// TODO remove that once split.
|
||||
pub trait IconProvider: AsAny + fmt::Debug + Send + Sync {}
|
||||
|
||||
impl Deref for Icon {
|
||||
type Target = dyn IconProvider;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl_dyn_casting!(IconProvider);
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Pixel {
|
||||
pub(crate) r: u8,
|
||||
pub(crate) g: u8,
|
||||
pub(crate) b: u8,
|
||||
pub(crate) a: u8,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// An error produced when using [`RgbaIcon::new`] with invalid arguments.
|
||||
pub enum BadIcon {
|
||||
@@ -60,7 +61,7 @@ impl fmt::Display for BadIcon {
|
||||
|
||||
impl Error for BadIcon {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RgbaIcon {
|
||||
pub(crate) width: u32,
|
||||
pub(crate) height: u32,
|
||||
1767
src/keyboard.rs
Normal file
@@ -26,20 +26,28 @@
|
||||
//! Some user activity, like mouse movement, can generate both a [`WindowEvent`] *and* a
|
||||
//! [`DeviceEvent`].
|
||||
//!
|
||||
//! You can retrieve events by calling [`EventLoop::run_app()`]. This function will dispatch events
|
||||
//! for every [`Window`] that was created with that particular [`EventLoop`].
|
||||
//! You can retrieve events by calling [`EventLoopProvider::run_app()`]. This function will dispatch
|
||||
//! events for every [`Window`] that was created with that particular [`EventLoop`].
|
||||
//!
|
||||
//! 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
|
||||
//! poorly on most other platforms. However, this model can be re-implemented to an extent with
|
||||
//! [`EventLoopExtPumpEvents::pump_app_events()`] [^1]. See that method's documentation for more
|
||||
//! reasons about why it's discouraged beyond compatibility reasons.
|
||||
#![cfg_attr(
|
||||
any(windows_platform, macos_platform, android_platform, x11_platform, wayland_platform),
|
||||
doc = "[`EventLoopExtPumpEvents::pump_app_events()`][platform::pump_events::EventLoopExtPumpEvents::pump_app_events()]"
|
||||
)]
|
||||
#![cfg_attr(
|
||||
not(any(windows_platform, macos_platform, android_platform, x11_platform, wayland_platform)),
|
||||
doc = "`EventLoopExtPumpEvents::pump_app_events()`"
|
||||
)]
|
||||
//! [^1]. See that method's documentation for more reasons about why
|
||||
//! it's discouraged beyond compatibility reasons.
|
||||
//!
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use winit::application::ApplicationHandler;
|
||||
//! use winit::event::WindowEvent;
|
||||
//! use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
||||
//! use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProvider};
|
||||
//! use winit::window::{Window, WindowId, WindowAttributes};
|
||||
//!
|
||||
//! #[derive(Default)]
|
||||
@@ -61,7 +69,7 @@
|
||||
//! id: WindowId,
|
||||
//! event: WindowEvent,
|
||||
//! ) {
|
||||
//! // Called by `EventLoop::run_app` when a new event happens on the window.
|
||||
//! // Called by `EventLoopProvider::run_app` when a new event happens on the window.
|
||||
//! match event {
|
||||
//! WindowEvent::CloseRequested => {
|
||||
//! println!("The close button was pressed; stopping");
|
||||
@@ -257,7 +265,7 @@
|
||||
//!
|
||||
//! [`EventLoop`]: event_loop::EventLoop
|
||||
//! [`EventLoop::new()`]: event_loop::EventLoop::new
|
||||
//! [`EventLoop::run_app()`]: event_loop::EventLoop::run_app
|
||||
//! [`EventLoopProvider::run_app()`]: event_loop::EventLoopProvider::run_app
|
||||
//! [`exit()`]: event_loop::ActiveEventLoop::exit
|
||||
//! [`Window`]: window::Window
|
||||
//! [`WindowId`]: window::WindowId
|
||||
@@ -268,7 +276,6 @@
|
||||
//! [`DeviceEvent`]: event::DeviceEvent
|
||||
//! [`raw_window_handle`]: ./window/struct.Window.html#method.raw_window_handle
|
||||
//! [`raw_display_handle`]: ./window/struct.Window.html#method.raw_display_handle
|
||||
//! [`EventLoopExtPumpEvents::pump_app_events()`]: crate::event_loop::pump_events::EventLoopExtPumpEvents::pump_app_events()
|
||||
//! [^1]: `EventLoopExtPumpEvents::pump_app_events()` is only available on Windows, macOS, Android, X11 and Wayland.
|
||||
|
||||
#![deny(rust_2018_idioms)]
|
||||
@@ -278,23 +285,30 @@
|
||||
#![cfg_attr(clippy, deny(warnings))]
|
||||
// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly
|
||||
// doc
|
||||
#![cfg_attr(docsrs, feature(doc_cfg), doc(auto_cfg(hide(doc, docsrs))))]
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide), doc(cfg_hide(doc, docsrs)))]
|
||||
#![allow(clippy::missing_safety_doc)]
|
||||
#![warn(clippy::uninlined_format_args)]
|
||||
// TODO: wasm-binding needs to be updated for that to be resolved, for now just silence it.
|
||||
#![cfg_attr(web_platform, allow(unknown_lints, renamed_and_removed_lints, wasm_c_abi))]
|
||||
#![cfg_attr(web_platform, allow(unknown_lints, wasm_c_abi))]
|
||||
|
||||
// Re-export DPI types so that users don't have to put it in Cargo.toml.
|
||||
#[doc(inline)]
|
||||
pub use dpi;
|
||||
pub use rwh_06 as raw_window_handle;
|
||||
|
||||
pub mod application;
|
||||
#[cfg(any(doc, doctest, test))]
|
||||
pub mod changelog;
|
||||
pub mod event_loop;
|
||||
pub use winit_core::{application, cursor, error, event, icon, keyboard, monitor, window};
|
||||
#[macro_use]
|
||||
mod os_error;
|
||||
pub mod error;
|
||||
mod cursor;
|
||||
pub mod event;
|
||||
pub mod event_loop;
|
||||
pub mod icon;
|
||||
pub mod keyboard;
|
||||
pub mod monitor;
|
||||
mod platform_impl;
|
||||
mod utils;
|
||||
pub mod window;
|
||||
|
||||
pub mod platform;
|
||||
@@ -11,9 +11,8 @@ use std::num::{NonZeroU16, NonZeroU32};
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
use dpi::{PhysicalPosition, PhysicalSize};
|
||||
|
||||
use crate::as_any::AsAny;
|
||||
use crate::dpi::{PhysicalPosition, PhysicalSize};
|
||||
use crate::utils::{impl_dyn_casting, AsAny};
|
||||
|
||||
/// Handle to a monitor.
|
||||
///
|
||||
@@ -29,13 +28,24 @@ use crate::as_any::AsAny;
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** A [`MonitorHandle`] created without `detailed monitor permissions`
|
||||
/// **Web:** A [`MonitorHandle`] created without
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]."
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = "detailed monitor permissions.")]
|
||||
/// will always represent the current monitor the browser window is in instead of a specific
|
||||
/// monitor.
|
||||
/// monitor. See
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = "[`MonitorHandleExtWeb::is_detailed()`][crate::platform::web::MonitorHandleExtWeb::is_detailed]"
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = "`MonitorHandleExtWeb::is_detailed()`")]
|
||||
/// to check.
|
||||
///
|
||||
/// [`Window`]: crate::window::Window
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MonitorHandle(pub Arc<dyn MonitorHandleProvider>);
|
||||
pub struct MonitorHandle(pub(crate) Arc<dyn MonitorHandleProvider>);
|
||||
|
||||
impl Deref for MonitorHandle {
|
||||
type Target = dyn MonitorHandleProvider;
|
||||
@@ -77,10 +87,14 @@ pub trait MonitorHandleProvider: AsAny + fmt::Debug + Send + Sync {
|
||||
///
|
||||
/// Returns `None` if the monitor doesn't exist anymore or the name couldn't be obtained.
|
||||
///
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Always returns [`None`] without `detailed monitor permissions`.
|
||||
/// **Web:** Always returns [`None`] without
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]."
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = "detailed monitor permissions.")]
|
||||
fn name(&self) -> Option<Cow<'_, str>>;
|
||||
|
||||
/// Returns the top-left corner position of the monitor in desktop coordinates.
|
||||
@@ -91,7 +105,12 @@ pub trait MonitorHandleProvider: AsAny + fmt::Debug + Send + Sync {
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// **Web:** Always returns [`None`] without `detailed monitor permissions`.
|
||||
/// **Web:** Always returns [`None`] without
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]."
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = "detailed monitor permissions.")]
|
||||
fn position(&self) -> Option<PhysicalPosition<i32>>;
|
||||
|
||||
/// Returns the scale factor of the underlying monitor. To map logical pixels to physical
|
||||
@@ -99,9 +118,19 @@ pub trait MonitorHandleProvider: AsAny + fmt::Debug + Send + Sync {
|
||||
///
|
||||
/// See the [`dpi`] module for more information.
|
||||
///
|
||||
/// - **Wayland:** May differ from [`Window::scale_factor`].
|
||||
/// - **Web:** Always returns `0.0` without `detailed_monitor_permissions`.
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **X11:** Can be overridden using the `WINIT_X11_SCALE_FACTOR` environment variable.
|
||||
/// - **Wayland:** May differ from [`Window::scale_factor`].
|
||||
/// - **Android:** Always returns 1.0.
|
||||
/// - **Web:** Always returns `0.0` without
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = " [detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]."
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = " detailed monitor permissions.")]
|
||||
///
|
||||
#[rustfmt::skip]
|
||||
/// [`Window::scale_factor`]: crate::window::Window::scale_factor
|
||||
fn scale_factor(&self) -> f64;
|
||||
|
||||
@@ -132,14 +161,6 @@ pub struct VideoMode {
|
||||
}
|
||||
|
||||
impl VideoMode {
|
||||
pub fn new(
|
||||
size: PhysicalSize<u32>,
|
||||
bit_depth: Option<NonZeroU16>,
|
||||
refresh_rate_millihertz: Option<NonZeroU32>,
|
||||
) -> Self {
|
||||
Self { size, bit_depth, refresh_rate_millihertz }
|
||||
}
|
||||
|
||||
/// Returns the resolution of this video mode. This **must not** be used to create your
|
||||
/// rendering surface. Use [`Window::surface_size()`] instead.
|
||||
///
|
||||
@@ -151,13 +172,6 @@ impl VideoMode {
|
||||
/// Returns the bit depth of this video mode, as in how many bits you have
|
||||
/// available per color. This is generally 24 bits or 32 bits on modern
|
||||
/// systems, depending on whether the alpha channel is counted or not.
|
||||
///
|
||||
/// # Platform-specific
|
||||
///
|
||||
/// - **macOS**: Video modes do not control the bit depth of the monitor, so this often defaults
|
||||
/// to 32.
|
||||
/// - **iOS**: Always returns `None`.
|
||||
/// - **Wayland**: Always returns `None`.
|
||||
pub fn bit_depth(&self) -> Option<NonZeroU16> {
|
||||
self.bit_depth
|
||||
}
|
||||
@@ -62,26 +62,17 @@
|
||||
//! If your application is currently based on `NativeActivity` via the `ndk-glue` crate and building
|
||||
//! with `cargo apk`, then the minimal changes would be:
|
||||
//! 1. Remove `ndk-glue` from your `Cargo.toml`
|
||||
//! 2. Enable the `"android-native-activity"` feature for Winit: `winit = { version =
|
||||
//! "0.31.0-beta.2", features = [ "android-native-activity" ] }`
|
||||
//! 2. Enable the `"android-native-activity"` feature for Winit: `winit = { version = "0.30.10",
|
||||
//! 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).
|
||||
//! 4. Pass a clone of the `AndroidApp` that your application receives to Winit when building your
|
||||
//! event loop (as shown above).
|
||||
#![cfg(target_os = "android")]
|
||||
|
||||
mod event_loop;
|
||||
mod keycodes;
|
||||
|
||||
use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
|
||||
use winit_core::window::Window as CoreWindow;
|
||||
|
||||
use self::activity::{AndroidApp, ConfigurationRef, Rect};
|
||||
pub use crate::event_loop::{
|
||||
ActiveEventLoop, EventLoop, EventLoopProxy, PlatformSpecificEventLoopAttributes,
|
||||
PlatformSpecificWindowAttributes, Window,
|
||||
};
|
||||
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
|
||||
use crate::window::{Window, WindowAttributes};
|
||||
|
||||
/// Additional methods on [`EventLoop`] that are specific to Android.
|
||||
pub trait EventLoopExtAndroid {
|
||||
@@ -89,6 +80,12 @@ pub trait EventLoopExtAndroid {
|
||||
fn android_app(&self) -> &AndroidApp;
|
||||
}
|
||||
|
||||
impl EventLoopExtAndroid for EventLoop {
|
||||
fn android_app(&self) -> &AndroidApp {
|
||||
&self.event_loop.android_app
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`ActiveEventLoop`] that are specific to Android.
|
||||
pub trait ActiveEventLoopExtAndroid {
|
||||
/// Get the [`AndroidApp`] which was used to create this event loop.
|
||||
@@ -102,25 +99,30 @@ pub trait WindowExtAndroid {
|
||||
fn config(&self) -> ConfigurationRef;
|
||||
}
|
||||
|
||||
impl WindowExtAndroid for dyn CoreWindow + '_ {
|
||||
impl WindowExtAndroid for dyn Window + '_ {
|
||||
fn content_rect(&self) -> Rect {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.content_rect()
|
||||
}
|
||||
|
||||
fn config(&self) -> ConfigurationRef {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.config()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveEventLoopExtAndroid for dyn CoreActiveEventLoop + '_ {
|
||||
impl ActiveEventLoopExtAndroid for dyn ActiveEventLoop + '_ {
|
||||
fn android_app(&self) -> &AndroidApp {
|
||||
let event_loop = self.cast_ref::<ActiveEventLoop>().unwrap();
|
||||
let event_loop = self.cast_ref::<crate::platform_impl::ActiveEventLoop>().unwrap();
|
||||
&event_loop.app
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`WindowAttributes`] that are specific to Android.
|
||||
pub trait WindowAttributesExtAndroid {}
|
||||
|
||||
impl WindowAttributesExtAndroid for WindowAttributes {}
|
||||
|
||||
pub trait EventLoopBuilderExtAndroid {
|
||||
/// Associates the [`AndroidApp`] that was passed to `android_main()` with the event loop
|
||||
///
|
||||
@@ -133,6 +135,18 @@ pub trait EventLoopBuilderExtAndroid {
|
||||
fn handle_volume_keys(&mut self) -> &mut Self;
|
||||
}
|
||||
|
||||
impl EventLoopBuilderExtAndroid for EventLoopBuilder {
|
||||
fn with_android_app(&mut self, app: AndroidApp) -> &mut Self {
|
||||
self.platform_specific.android_app = Some(app);
|
||||
self
|
||||
}
|
||||
|
||||
fn handle_volume_keys(&mut self) -> &mut Self {
|
||||
self.platform_specific.ignore_volume_keys = false;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-export of the `android_activity` API
|
||||
///
|
||||
/// Winit re-exports the `android_activity` API for convenience so that most
|
||||
@@ -161,5 +175,16 @@ pub mod activity {
|
||||
// feature enabled, so we avoid inlining it so that they're forced to view
|
||||
// it on the crate's own docs.rs page.
|
||||
#[doc(no_inline)]
|
||||
#[cfg(android_platform)]
|
||||
pub use android_activity::*;
|
||||
|
||||
#[cfg(not(android_platform))]
|
||||
#[doc(hidden)]
|
||||
pub struct Rect;
|
||||
#[cfg(not(android_platform))]
|
||||
#[doc(hidden)]
|
||||
pub struct ConfigurationRef;
|
||||
#[cfg(not(android_platform))]
|
||||
#[doc(hidden)]
|
||||
pub struct AndroidApp;
|
||||
}
|
||||
193
src/platform/event_loop.rs
Normal file
@@ -0,0 +1,193 @@
|
||||
use std::marker::PhantomData;
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use rwh_06::{DisplayHandle, HandleError, HasDisplayHandle};
|
||||
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::cursor::{CustomCursor, CustomCursorSource};
|
||||
use crate::error::{EventLoopError, RequestError};
|
||||
use crate::event_loop::{
|
||||
ControlFlow, DeviceEvents, EventLoopProvider, EventLoopProxy, OwnedDisplayHandle,
|
||||
};
|
||||
use crate::platform_impl;
|
||||
|
||||
/// Provides a way to retrieve events from the system and from the windows that were registered to
|
||||
/// the events loop.
|
||||
///
|
||||
/// An `EventLoop` can be seen more or less as a "context". Calling [`EventLoop::new`]
|
||||
/// initializes everything that will be required to create windows. For example on Linux creating
|
||||
/// an event loop opens a connection to the X or Wayland server.
|
||||
///
|
||||
/// 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
|
||||
/// forbidding it), as such it is neither [`Send`] nor [`Sync`]. If you need cross-thread access,
|
||||
/// 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.
|
||||
///
|
||||
/// [`Window`]: crate::window::Window
|
||||
#[derive(Debug)]
|
||||
pub struct EventLoop {
|
||||
pub(crate) event_loop: platform_impl::EventLoop,
|
||||
pub(crate) _marker: PhantomData<*mut ()>, // Not Send nor Sync
|
||||
}
|
||||
|
||||
impl EventLoop {
|
||||
/// Create the event loop.
|
||||
///
|
||||
/// This is an alias of `EventLoop::builder().build()`.
|
||||
#[inline]
|
||||
pub fn new() -> Result<EventLoop, EventLoopError> {
|
||||
Self::builder().build()
|
||||
}
|
||||
|
||||
/// Start building a new event loop.
|
||||
///
|
||||
/// This returns an [`EventLoopBuilder`], to allow configuring the event loop before creation.
|
||||
///
|
||||
/// To get the actual event loop, call [`build`][EventLoopBuilder::build] on that.
|
||||
#[inline]
|
||||
pub fn builder() -> EventLoopBuilder {
|
||||
EventLoopBuilder { platform_specific: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
impl EventLoopProvider for EventLoop {
|
||||
#[cfg(not(all(web_platform, target_feature = "exception-handling")))]
|
||||
fn run_app(self, app: impl ApplicationHandler) -> Result<(), EventLoopError> {
|
||||
self.event_loop.run_app(app)
|
||||
}
|
||||
|
||||
fn create_proxy(&self) -> EventLoopProxy {
|
||||
self.event_loop.window_target().create_proxy()
|
||||
}
|
||||
|
||||
fn owned_display_handle(&self) -> OwnedDisplayHandle {
|
||||
self.event_loop.window_target().owned_display_handle()
|
||||
}
|
||||
|
||||
fn listen_device_events(&self, allowed: DeviceEvents) {
|
||||
let _span = tracing::debug_span!(
|
||||
"winit::EventLoop::listen_device_events",
|
||||
allowed = ?allowed
|
||||
)
|
||||
.entered();
|
||||
self.event_loop.window_target().listen_device_events(allowed)
|
||||
}
|
||||
|
||||
fn set_control_flow(&self, control_flow: ControlFlow) {
|
||||
self.event_loop.window_target().set_control_flow(control_flow);
|
||||
}
|
||||
|
||||
fn create_custom_cursor(
|
||||
&self,
|
||||
custom_cursor: CustomCursorSource,
|
||||
) -> Result<CustomCursor, RequestError> {
|
||||
self.event_loop.window_target().create_custom_cursor(custom_cursor)
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for EventLoop {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
HasDisplayHandle::display_handle(self.event_loop.window_target().rwh_06_handle())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
impl AsFd for EventLoop {
|
||||
/// Get the underlying [EventLoop]'s `fd` which you can register
|
||||
/// into other event loop, like [`calloop`] or [`mio`]. When doing so, the
|
||||
/// loop must be polled with the [`pump_app_events`] API.
|
||||
///
|
||||
/// [`calloop`]: https://crates.io/crates/calloop
|
||||
/// [`mio`]: https://crates.io/crates/mio
|
||||
/// [`pump_app_events`]: crate::platform::pump_events::EventLoopExtPumpEvents::pump_app_events
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.event_loop.as_fd()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
impl AsRawFd for EventLoop {
|
||||
/// Get the underlying [EventLoop]'s raw `fd` which you can register
|
||||
/// into other event loop, like [`calloop`] or [`mio`]. When doing so, the
|
||||
/// loop must be polled with the [`pump_app_events`] API.
|
||||
///
|
||||
/// [`calloop`]: https://crates.io/crates/calloop
|
||||
/// [`mio`]: https://crates.io/crates/mio
|
||||
/// [`pump_app_events`]: crate::platform::pump_events::EventLoopExtPumpEvents::pump_app_events
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.event_loop.as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
/// Object that allows building the event loop.
|
||||
///
|
||||
/// This is used to make specifying options that affect the whole application
|
||||
/// easier. But note that constructing multiple event loops is not supported.
|
||||
///
|
||||
/// This can be created using [`EventLoop::builder`].
|
||||
#[derive(Default, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct EventLoopBuilder {
|
||||
pub(crate) platform_specific: platform_impl::PlatformSpecificEventLoopAttributes,
|
||||
}
|
||||
|
||||
static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
impl EventLoopBuilder {
|
||||
/// Builds a new event loop.
|
||||
///
|
||||
/// ***For cross-platform compatibility, the [`EventLoop`] must be created on the main thread,
|
||||
/// and only once per application.***
|
||||
///
|
||||
/// Calling this function will result in display backend initialisation.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Attempting to create the event loop off the main thread will panic. This
|
||||
/// restriction isn't strictly necessary on all platforms, but is imposed to
|
||||
/// eliminate any nasty surprises when porting to platforms that require it.
|
||||
/// `EventLoopBuilderExt::with_any_thread` functions are exposed in the relevant
|
||||
/// [`platform`] module if the target platform supports creating an event
|
||||
/// loop on any thread.
|
||||
///
|
||||
/// ## Platform-specific
|
||||
///
|
||||
/// - **Wayland/X11:** to prevent running under `Wayland` or `X11` unset `WAYLAND_DISPLAY` or
|
||||
/// `DISPLAY` respectively when building the event loop.
|
||||
/// - **Android:** must be configured with an `AndroidApp` from `android_main()` by calling
|
||||
/// [`.with_android_app(app)`] before calling `.build()`, otherwise it'll panic.
|
||||
///
|
||||
/// [`platform`]: crate::platform
|
||||
#[cfg_attr(
|
||||
android_platform,
|
||||
doc = "[`.with_android_app(app)`]: \
|
||||
crate::platform::android::EventLoopBuilderExtAndroid::with_android_app"
|
||||
)]
|
||||
#[cfg_attr(
|
||||
not(android_platform),
|
||||
doc = "[`.with_android_app(app)`]: #only-available-on-android"
|
||||
)]
|
||||
#[inline]
|
||||
pub fn build(&mut self) -> Result<EventLoop, EventLoopError> {
|
||||
let _span = tracing::debug_span!("winit::EventLoopBuilder::build").entered();
|
||||
|
||||
if EVENT_LOOP_CREATED.swap(true, Ordering::Relaxed) {
|
||||
return Err(EventLoopError::RecreationAttempt);
|
||||
}
|
||||
|
||||
// Certain platforms accept a mutable reference in their API.
|
||||
#[allow(clippy::unnecessary_mut_passed)]
|
||||
Ok(EventLoop {
|
||||
event_loop: platform_impl::EventLoop::new(&mut self.platform_specific)?,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(web_platform)]
|
||||
pub(crate) fn allow_event_loop_recreation() {
|
||||
EVENT_LOOP_CREATED.store(false, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
//! # Winit's UIKit (iOS/tvOS/visionOS) backend
|
||||
//! # iOS / UIKit
|
||||
//!
|
||||
//! Winit has [the same iOS version requirements as `rustc`][rustc-ios-version], although it's
|
||||
//! frequently only tested on newer iOS versions.
|
||||
@@ -98,25 +98,15 @@
|
||||
//! register an application delegate, so you can set up a custom one in a nib file instead.
|
||||
//!
|
||||
//! [app-delegate]: https://developer.apple.com/documentation/uikit/uiapplicationdelegate?language=objc
|
||||
#![cfg(target_vendor = "apple")] // TODO: Remove once `objc2` allows compiling on all platforms
|
||||
|
||||
mod app_state;
|
||||
mod event_loop;
|
||||
mod monitor;
|
||||
mod view;
|
||||
mod view_controller;
|
||||
mod window;
|
||||
|
||||
use std::os::raw::c_void;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use winit_core::monitor::{MonitorHandle, VideoMode};
|
||||
use winit_core::window::{PlatformWindowAttributes, Window};
|
||||
|
||||
pub use self::event_loop::{EventLoop, PlatformSpecificEventLoopAttributes};
|
||||
use self::monitor::MonitorHandle as UIKitMonitorHandle;
|
||||
use self::window::Window as UIKitWindow;
|
||||
use crate::monitor::{MonitorHandle, VideoMode};
|
||||
use crate::platform_impl::MonitorHandle as IosMonitorHandle;
|
||||
use crate::window::{Window, WindowAttributes};
|
||||
|
||||
/// Additional methods on [`Window`] that are specific to iOS.
|
||||
pub trait WindowExtIOS {
|
||||
@@ -221,25 +211,25 @@ pub trait WindowExtIOS {
|
||||
impl WindowExtIOS for dyn Window + '_ {
|
||||
#[inline]
|
||||
fn set_scale_factor(&self, scale_factor: f64) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_scale_factor(scale_factor));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_valid_orientations(&self, valid_orientations: ValidOrientations) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_valid_orientations(valid_orientations));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_prefers_home_indicator_hidden(&self, hidden: bool) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_prefers_home_indicator_hidden(hidden));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_preferred_screen_edges_deferring_system_gestures(&self, edges: ScreenEdge) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| {
|
||||
w.set_preferred_screen_edges_deferring_system_gestures(edges)
|
||||
});
|
||||
@@ -247,19 +237,19 @@ impl WindowExtIOS for dyn Window + '_ {
|
||||
|
||||
#[inline]
|
||||
fn set_prefers_status_bar_hidden(&self, hidden: bool) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_prefers_status_bar_hidden(hidden));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_preferred_status_bar_style(status_bar_style))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn recognize_pinch_gesture(&self, should_recognize: bool) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.recognize_pinch_gesture(should_recognize));
|
||||
}
|
||||
|
||||
@@ -270,7 +260,7 @@ impl WindowExtIOS for dyn Window + '_ {
|
||||
minimum_number_of_touches: u8,
|
||||
maximum_number_of_touches: u8,
|
||||
) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| {
|
||||
w.recognize_pan_gesture(
|
||||
should_recognize,
|
||||
@@ -282,29 +272,19 @@ impl WindowExtIOS for dyn Window + '_ {
|
||||
|
||||
#[inline]
|
||||
fn recognize_doubletap_gesture(&self, should_recognize: bool) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.recognize_doubletap_gesture(should_recognize));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn recognize_rotation_gesture(&self, should_recognize: bool) {
|
||||
let window = self.cast_ref::<UIKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.recognize_rotation_gesture(should_recognize));
|
||||
}
|
||||
}
|
||||
|
||||
/// Ios specific window attributes.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct WindowAttributesIos {
|
||||
pub(crate) scale_factor: Option<f64>,
|
||||
pub(crate) valid_orientations: ValidOrientations,
|
||||
pub(crate) prefers_home_indicator_hidden: bool,
|
||||
pub(crate) prefers_status_bar_hidden: bool,
|
||||
pub(crate) preferred_status_bar_style: StatusBarStyle,
|
||||
pub(crate) preferred_screen_edges_deferring_system_gestures: ScreenEdge,
|
||||
}
|
||||
|
||||
impl WindowAttributesIos {
|
||||
/// Additional methods on [`WindowAttributes`] that are specific to iOS.
|
||||
pub trait WindowAttributesExtIOS {
|
||||
/// Sets the [`contentScaleFactor`] of the underlying [`UIWindow`] to `scale_factor`.
|
||||
///
|
||||
/// The default value is device dependent, and it's recommended GLES or Metal applications set
|
||||
@@ -313,10 +293,7 @@ impl WindowAttributesIos {
|
||||
/// [`UIWindow`]: https://developer.apple.com/documentation/uikit/uiwindow?language=objc
|
||||
/// [`contentScaleFactor`]: https://developer.apple.com/documentation/uikit/uiview/1622657-contentscalefactor?language=objc
|
||||
/// [`MonitorHandleProvider::scale_factor()`]: crate::monitor::MonitorHandleProvider::scale_factor()
|
||||
pub fn with_scale_factor(mut self, scale_factor: f64) -> Self {
|
||||
self.scale_factor = Some(scale_factor);
|
||||
self
|
||||
}
|
||||
fn with_scale_factor(self, scale_factor: f64) -> Self;
|
||||
|
||||
/// Sets the valid orientations for the [`Window`].
|
||||
///
|
||||
@@ -324,10 +301,7 @@ impl WindowAttributesIos {
|
||||
///
|
||||
/// This sets the initial value returned by
|
||||
/// [`-[UIViewController supportedInterfaceOrientations]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations?language=objc).
|
||||
pub fn with_valid_orientations(mut self, valid_orientations: ValidOrientations) -> Self {
|
||||
self.valid_orientations = valid_orientations;
|
||||
self
|
||||
}
|
||||
fn with_valid_orientations(self, valid_orientations: ValidOrientations) -> Self;
|
||||
|
||||
/// Sets whether the [`Window`] prefers the home indicator hidden.
|
||||
///
|
||||
@@ -337,10 +311,7 @@ impl WindowAttributesIos {
|
||||
/// [`-[UIViewController prefersHomeIndicatorAutoHidden]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887510-prefershomeindicatorautohidden?language=objc).
|
||||
///
|
||||
/// This only has an effect on iOS 11.0+.
|
||||
pub fn with_prefers_home_indicator_hidden(mut self, hidden: bool) -> Self {
|
||||
self.prefers_home_indicator_hidden = hidden;
|
||||
self
|
||||
}
|
||||
fn with_prefers_home_indicator_hidden(self, hidden: bool) -> Self;
|
||||
|
||||
/// Sets the screen edges for which the system gestures will take a lower priority than the
|
||||
/// application's touch handling.
|
||||
@@ -349,13 +320,7 @@ impl WindowAttributesIos {
|
||||
/// [`-[UIViewController preferredScreenEdgesDeferringSystemGestures]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887512-preferredscreenedgesdeferringsys?language=objc).
|
||||
///
|
||||
/// This only has an effect on iOS 11.0+.
|
||||
pub fn with_preferred_screen_edges_deferring_system_gestures(
|
||||
mut self,
|
||||
edges: ScreenEdge,
|
||||
) -> Self {
|
||||
self.preferred_screen_edges_deferring_system_gestures = edges;
|
||||
self
|
||||
}
|
||||
fn with_preferred_screen_edges_deferring_system_gestures(self, edges: ScreenEdge) -> Self;
|
||||
|
||||
/// Sets whether the [`Window`] prefers the status bar hidden.
|
||||
///
|
||||
@@ -363,10 +328,7 @@ impl WindowAttributesIos {
|
||||
///
|
||||
/// This sets the initial value returned by
|
||||
/// [`-[UIViewController prefersStatusBarHidden]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621440-prefersstatusbarhidden?language=objc).
|
||||
pub fn with_prefers_status_bar_hidden(mut self, hidden: bool) -> Self {
|
||||
self.prefers_status_bar_hidden = hidden;
|
||||
self
|
||||
}
|
||||
fn with_prefers_status_bar_hidden(self, hidden: bool) -> Self;
|
||||
|
||||
/// Sets the style of the [`Window`]'s status bar.
|
||||
///
|
||||
@@ -374,15 +336,44 @@ impl WindowAttributesIos {
|
||||
///
|
||||
/// This sets the initial value returned by
|
||||
/// [`-[UIViewController preferredStatusBarStyle]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621416-preferredstatusbarstyle?language=objc),
|
||||
pub fn with_preferred_status_bar_style(mut self, status_bar_style: StatusBarStyle) -> Self {
|
||||
self.preferred_status_bar_style = status_bar_style;
|
||||
self
|
||||
}
|
||||
fn with_preferred_status_bar_style(self, status_bar_style: StatusBarStyle) -> Self;
|
||||
}
|
||||
|
||||
impl PlatformWindowAttributes for WindowAttributesIos {
|
||||
fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
|
||||
Box::from(self.clone())
|
||||
impl WindowAttributesExtIOS for WindowAttributes {
|
||||
#[inline]
|
||||
fn with_scale_factor(mut self, scale_factor: f64) -> Self {
|
||||
self.platform_specific.scale_factor = Some(scale_factor);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_valid_orientations(mut self, valid_orientations: ValidOrientations) -> Self {
|
||||
self.platform_specific.valid_orientations = valid_orientations;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_prefers_home_indicator_hidden(mut self, hidden: bool) -> Self {
|
||||
self.platform_specific.prefers_home_indicator_hidden = hidden;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_preferred_screen_edges_deferring_system_gestures(mut self, edges: ScreenEdge) -> Self {
|
||||
self.platform_specific.preferred_screen_edges_deferring_system_gestures = edges;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_prefers_status_bar_hidden(mut self, hidden: bool) -> Self {
|
||||
self.platform_specific.prefers_status_bar_hidden = hidden;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_preferred_status_bar_style(mut self, status_bar_style: StatusBarStyle) -> Self {
|
||||
self.platform_specific.preferred_status_bar_style = status_bar_style;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -404,13 +395,13 @@ impl MonitorHandleExtIOS for MonitorHandle {
|
||||
fn ui_screen(&self) -> *mut c_void {
|
||||
// SAFETY: The marker is only used to get the pointer of the screen
|
||||
let mtm = unsafe { objc2::MainThreadMarker::new_unchecked() };
|
||||
let monitor = self.cast_ref::<UIKitMonitorHandle>().unwrap();
|
||||
let monitor = self.cast_ref::<IosMonitorHandle>().unwrap();
|
||||
objc2::rc::Retained::as_ptr(monitor.ui_screen(mtm)) as *mut c_void
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn preferred_video_mode(&self) -> VideoMode {
|
||||
let monitor = self.cast_ref::<UIKitMonitorHandle>().unwrap();
|
||||
let monitor = self.cast_ref::<IosMonitorHandle>().unwrap();
|
||||
monitor.preferred_video_mode()
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,13 @@
|
||||
//! Instead, Winit guarantees that it will not register an application delegate, so the solution is
|
||||
//! to register your own application delegate, as outlined in the following example (see
|
||||
//! `objc2-app-kit` for more detailed information).
|
||||
//! ```
|
||||
#![cfg_attr(target_os = "macos", doc = "```")]
|
||||
#![cfg_attr(not(target_os = "macos"), doc = "```ignore")]
|
||||
//! use objc2::rc::Retained;
|
||||
//! use objc2::runtime::ProtocolObject;
|
||||
//! use objc2::{DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send};
|
||||
//! use objc2::{define_class, msg_send, DefinedClass, MainThreadMarker, MainThreadOnly};
|
||||
//! use objc2_app_kit::{NSApplication, NSApplicationDelegate};
|
||||
//! use objc2_foundation::{NSArray, NSObject, NSObjectProtocol, NSURL};
|
||||
//! use objc2_foundation::{NSArray, NSURL, NSObject, NSObjectProtocol};
|
||||
//! use winit::event_loop::EventLoop;
|
||||
//!
|
||||
//! define_class!(
|
||||
@@ -63,39 +64,17 @@
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
#![cfg(target_vendor = "apple")] // TODO: Remove once `objc2` allows compiling on all platforms
|
||||
|
||||
#[macro_use]
|
||||
mod util;
|
||||
|
||||
mod app;
|
||||
mod app_state;
|
||||
mod cursor;
|
||||
mod event;
|
||||
mod event_loop;
|
||||
mod ffi;
|
||||
mod menu;
|
||||
mod monitor;
|
||||
mod observer;
|
||||
mod view;
|
||||
mod window;
|
||||
mod window_delegate;
|
||||
|
||||
use std::os::raw::c_void;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[doc(inline)]
|
||||
pub use winit_core::application::macos::ApplicationHandlerExtMacOS;
|
||||
use winit_core::event_loop::ActiveEventLoop;
|
||||
use winit_core::monitor::MonitorHandle;
|
||||
use winit_core::window::{PlatformWindowAttributes, Window};
|
||||
|
||||
pub use self::event::{physicalkey_to_scancode, scancode_to_physicalkey};
|
||||
use self::event_loop::ActiveEventLoop as AppKitActiveEventLoop;
|
||||
pub use self::event_loop::{EventLoop, PlatformSpecificEventLoopAttributes};
|
||||
use self::monitor::MonitorHandle as AppKitMonitorHandle;
|
||||
use self::window::Window as AppKitWindow;
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::event_loop::{ActiveEventLoop, EventLoopBuilder};
|
||||
use crate::monitor::MonitorHandle;
|
||||
use crate::platform_impl::MonitorHandle as MacOsMonitorHandle;
|
||||
use crate::window::{Window, WindowAttributes, WindowId};
|
||||
|
||||
/// Additional methods on [`Window`] that are specific to MacOS.
|
||||
pub trait WindowExtMacOS {
|
||||
@@ -191,109 +170,109 @@ pub trait WindowExtMacOS {
|
||||
impl WindowExtMacOS for dyn Window + '_ {
|
||||
#[inline]
|
||||
fn simple_fullscreen(&self) -> bool {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.simple_fullscreen())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_simple_fullscreen(fullscreen))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn has_shadow(&self) -> bool {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.has_shadow())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_has_shadow(&self, has_shadow: bool) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_has_shadow(has_shadow));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_tabbing_identifier(&self, identifier: &str) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.set_tabbing_identifier(identifier))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn tabbing_identifier(&self) -> String {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.tabbing_identifier())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn select_next_tab(&self) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.select_next_tab());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn select_previous_tab(&self) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.select_previous_tab());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn select_tab_at_index(&self, index: usize) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.select_tab_at_index(index));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn num_tabs(&self) -> usize {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.num_tabs())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_document_edited(&self) -> bool {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.is_document_edited())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_document_edited(&self, edited: bool) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_document_edited(edited));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_option_as_alt(&self, option_as_alt: OptionAsAlt) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(move |w| w.set_option_as_alt(option_as_alt));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn option_as_alt(&self) -> OptionAsAlt {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.option_as_alt())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_borderless_game(&self, borderless_game: bool) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.set_borderless_game(borderless_game))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_borderless_game(&self) -> bool {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.is_borderless_game())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_unified_titlebar(&self, unified_titlebar: bool) {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.set_unified_titlebar(unified_titlebar))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unified_titlebar(&self) -> bool {
|
||||
let window = self.cast_ref::<AppKitWindow>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.maybe_wait_on_main(|w| w.unified_titlebar())
|
||||
}
|
||||
}
|
||||
@@ -313,7 +292,7 @@ pub enum ActivationPolicy {
|
||||
Prohibited,
|
||||
}
|
||||
|
||||
/// Window attributes 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
|
||||
/// [`WindowAttributes::with_decorations`] method:
|
||||
@@ -322,158 +301,127 @@ pub enum ActivationPolicy {
|
||||
/// - `with_titlebar_hidden`
|
||||
/// - `with_titlebar_buttons_hidden`
|
||||
/// - `with_fullsize_content_view`
|
||||
///
|
||||
/// [`WindowAttributes::with_decorations`]: crate::window::WindowAttributes::with_decorations
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct WindowAttributesMacOS {
|
||||
pub(crate) movable_by_window_background: bool,
|
||||
pub(crate) titlebar_transparent: bool,
|
||||
pub(crate) title_hidden: bool,
|
||||
pub(crate) titlebar_hidden: bool,
|
||||
pub(crate) titlebar_buttons_hidden: bool,
|
||||
pub(crate) fullsize_content_view: bool,
|
||||
pub(crate) disallow_hidpi: bool,
|
||||
pub(crate) has_shadow: bool,
|
||||
pub(crate) accepts_first_mouse: bool,
|
||||
pub(crate) tabbing_identifier: Option<String>,
|
||||
pub(crate) option_as_alt: OptionAsAlt,
|
||||
pub(crate) borderless_game: bool,
|
||||
pub(crate) unified_titlebar: bool,
|
||||
pub(crate) panel: bool,
|
||||
}
|
||||
|
||||
impl WindowAttributesMacOS {
|
||||
pub trait WindowAttributesExtMacOS {
|
||||
/// Enables click-and-drag behavior for the entire window, not just the titlebar.
|
||||
#[inline]
|
||||
pub fn with_movable_by_window_background(mut self, movable_by_window_background: bool) -> Self {
|
||||
self.movable_by_window_background = movable_by_window_background;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_movable_by_window_background(self, movable_by_window_background: bool) -> Self;
|
||||
/// Makes the titlebar transparent and allows the content to appear behind it.
|
||||
#[inline]
|
||||
pub fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> Self {
|
||||
self.titlebar_transparent = titlebar_transparent;
|
||||
self
|
||||
}
|
||||
|
||||
/// Hides the window titlebar.
|
||||
#[inline]
|
||||
pub fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> Self {
|
||||
self.titlebar_hidden = titlebar_hidden;
|
||||
self
|
||||
}
|
||||
|
||||
/// Hides the window titlebar buttons.
|
||||
#[inline]
|
||||
pub fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> Self {
|
||||
self.titlebar_buttons_hidden = titlebar_buttons_hidden;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_titlebar_transparent(self, titlebar_transparent: bool) -> Self;
|
||||
/// Hides the window title.
|
||||
#[inline]
|
||||
pub fn with_title_hidden(mut self, title_hidden: bool) -> Self {
|
||||
self.title_hidden = title_hidden;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_title_hidden(self, title_hidden: bool) -> Self;
|
||||
/// Hides the window titlebar.
|
||||
fn with_titlebar_hidden(self, titlebar_hidden: bool) -> Self;
|
||||
/// Hides the window titlebar buttons.
|
||||
fn with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> Self;
|
||||
/// Makes the window content appear behind the titlebar.
|
||||
#[inline]
|
||||
pub fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> Self {
|
||||
self.fullsize_content_view = fullsize_content_view;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_disallow_hidpi(mut self, disallow_hidpi: bool) -> Self {
|
||||
self.disallow_hidpi = disallow_hidpi;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_has_shadow(mut self, has_shadow: bool) -> Self {
|
||||
self.has_shadow = has_shadow;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_fullsize_content_view(self, fullsize_content_view: bool) -> Self;
|
||||
fn with_disallow_hidpi(self, disallow_hidpi: bool) -> Self;
|
||||
fn with_has_shadow(self, has_shadow: bool) -> Self;
|
||||
/// Window accepts click-through mouse events.
|
||||
#[inline]
|
||||
pub fn with_accepts_first_mouse(mut self, accepts_first_mouse: bool) -> Self {
|
||||
self.accepts_first_mouse = accepts_first_mouse;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_accepts_first_mouse(self, accepts_first_mouse: bool) -> Self;
|
||||
/// Defines the window tabbing identifier.
|
||||
///
|
||||
/// <https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier>
|
||||
#[inline]
|
||||
pub fn with_tabbing_identifier(mut self, tabbing_identifier: &str) -> Self {
|
||||
self.tabbing_identifier.replace(tabbing_identifier.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
fn with_tabbing_identifier(self, identifier: &str) -> Self;
|
||||
/// Set how the <kbd>Option</kbd> keys are interpreted.
|
||||
///
|
||||
/// See [`WindowExtMacOS::set_option_as_alt`] for details on what this means if set.
|
||||
#[inline]
|
||||
pub fn with_option_as_alt(mut self, option_as_alt: OptionAsAlt) -> Self {
|
||||
self.option_as_alt = option_as_alt;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_option_as_alt(self, option_as_alt: OptionAsAlt) -> Self;
|
||||
/// See [`WindowExtMacOS::set_borderless_game`] for details on what this means if set.
|
||||
#[inline]
|
||||
pub fn with_borderless_game(mut self, borderless_game: bool) -> Self {
|
||||
self.borderless_game = borderless_game;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_borderless_game(self, borderless_game: bool) -> Self;
|
||||
/// See [`WindowExtMacOS::set_unified_titlebar`] for details on what this means if set.
|
||||
#[inline]
|
||||
pub fn with_unified_titlebar(mut self, unified_titlebar: bool) -> Self {
|
||||
self.unified_titlebar = unified_titlebar;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_unified_titlebar(self, unified_titlebar: bool) -> Self;
|
||||
/// Use [`NSPanel`] window with [`NonactivatingPanel`] window style mask instead of
|
||||
/// [`NSWindow`].
|
||||
///
|
||||
/// [`NSWindow`]: https://developer.apple.com/documentation/appkit/NSWindow?language=objc
|
||||
/// [`NSPanel`]: https://developer.apple.com/documentation/appkit/NSPanel?language=objc
|
||||
/// [`NonactivatingPanel`]: https://developer.apple.com/documentation/appkit/nswindow/stylemask-swift.struct/nonactivatingpanel?language=objc
|
||||
fn with_panel(self, panel: bool) -> Self;
|
||||
}
|
||||
|
||||
impl WindowAttributesExtMacOS for WindowAttributes {
|
||||
#[inline]
|
||||
pub fn with_panel(mut self, panel: bool) -> Self {
|
||||
self.panel = panel;
|
||||
fn with_movable_by_window_background(mut self, movable_by_window_background: bool) -> Self {
|
||||
self.platform_specific.movable_by_window_background = movable_by_window_background;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WindowAttributesMacOS {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
movable_by_window_background: false,
|
||||
titlebar_transparent: false,
|
||||
title_hidden: false,
|
||||
titlebar_hidden: false,
|
||||
titlebar_buttons_hidden: false,
|
||||
fullsize_content_view: false,
|
||||
disallow_hidpi: false,
|
||||
has_shadow: true,
|
||||
accepts_first_mouse: true,
|
||||
tabbing_identifier: None,
|
||||
option_as_alt: Default::default(),
|
||||
borderless_game: false,
|
||||
unified_titlebar: false,
|
||||
panel: false,
|
||||
}
|
||||
fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> Self {
|
||||
self.platform_specific.titlebar_transparent = titlebar_transparent;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl PlatformWindowAttributes for WindowAttributesMacOS {
|
||||
fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
|
||||
Box::from(self.clone())
|
||||
#[inline]
|
||||
fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> Self {
|
||||
self.platform_specific.titlebar_hidden = titlebar_hidden;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> Self {
|
||||
self.platform_specific.titlebar_buttons_hidden = titlebar_buttons_hidden;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_title_hidden(mut self, title_hidden: bool) -> Self {
|
||||
self.platform_specific.title_hidden = title_hidden;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> Self {
|
||||
self.platform_specific.fullsize_content_view = fullsize_content_view;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_disallow_hidpi(mut self, disallow_hidpi: bool) -> Self {
|
||||
self.platform_specific.disallow_hidpi = disallow_hidpi;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_has_shadow(mut self, has_shadow: bool) -> Self {
|
||||
self.platform_specific.has_shadow = has_shadow;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_accepts_first_mouse(mut self, accepts_first_mouse: bool) -> Self {
|
||||
self.platform_specific.accepts_first_mouse = accepts_first_mouse;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_tabbing_identifier(mut self, tabbing_identifier: &str) -> Self {
|
||||
self.platform_specific.tabbing_identifier.replace(tabbing_identifier.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_option_as_alt(mut self, option_as_alt: OptionAsAlt) -> Self {
|
||||
self.platform_specific.option_as_alt = option_as_alt;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_borderless_game(mut self, borderless_game: bool) -> Self {
|
||||
self.platform_specific.borderless_game = borderless_game;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_unified_titlebar(mut self, unified_titlebar: bool) -> Self {
|
||||
self.platform_specific.unified_titlebar = unified_titlebar;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_panel(mut self, panel: bool) -> Self {
|
||||
self.platform_specific.panel = panel;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,6 +480,26 @@ pub trait EventLoopBuilderExtMacOS {
|
||||
fn with_activate_ignoring_other_apps(&mut self, ignore: bool) -> &mut Self;
|
||||
}
|
||||
|
||||
impl EventLoopBuilderExtMacOS for EventLoopBuilder {
|
||||
#[inline]
|
||||
fn with_activation_policy(&mut self, activation_policy: ActivationPolicy) -> &mut Self {
|
||||
self.platform_specific.activation_policy = Some(activation_policy);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_default_menu(&mut self, enable: bool) -> &mut Self {
|
||||
self.platform_specific.default_menu = enable;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_activate_ignoring_other_apps(&mut self, ignore: bool) -> &mut Self {
|
||||
self.platform_specific.activate_ignoring_other_apps = ignore;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`MonitorHandle`] that are specific to MacOS.
|
||||
pub trait MonitorHandleExtMacOS {
|
||||
/// Returns a pointer to the NSScreen representing this monitor.
|
||||
@@ -540,7 +508,7 @@ pub trait MonitorHandleExtMacOS {
|
||||
|
||||
impl MonitorHandleExtMacOS for MonitorHandle {
|
||||
fn ns_screen(&self) -> Option<*mut c_void> {
|
||||
let monitor = self.cast_ref::<AppKitMonitorHandle>().unwrap();
|
||||
let monitor = self.cast_ref::<MacOsMonitorHandle>().unwrap();
|
||||
// SAFETY: We only use the marker to get a pointer
|
||||
let mtm = unsafe { objc2::MainThreadMarker::new_unchecked() };
|
||||
monitor.ns_screen(mtm).map(|s| objc2::rc::Retained::as_ptr(&s) as _)
|
||||
@@ -565,26 +533,30 @@ pub trait ActiveEventLoopExtMacOS {
|
||||
|
||||
impl ActiveEventLoopExtMacOS for dyn ActiveEventLoop + '_ {
|
||||
fn hide_application(&self) {
|
||||
let event_loop =
|
||||
self.cast_ref::<AppKitActiveEventLoop>().expect("non macOS event loop on macOS");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non macOS event loop on macOS");
|
||||
event_loop.hide_application()
|
||||
}
|
||||
|
||||
fn hide_other_applications(&self) {
|
||||
let event_loop =
|
||||
self.cast_ref::<AppKitActiveEventLoop>().expect("non macOS event loop on macOS");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non macOS event loop on macOS");
|
||||
event_loop.hide_other_applications()
|
||||
}
|
||||
|
||||
fn set_allows_automatic_window_tabbing(&self, enabled: bool) {
|
||||
let event_loop =
|
||||
self.cast_ref::<AppKitActiveEventLoop>().expect("non macOS event loop on macOS");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non macOS event loop on macOS");
|
||||
event_loop.set_allows_automatic_window_tabbing(enabled);
|
||||
}
|
||||
|
||||
fn allows_automatic_window_tabbing(&self) -> bool {
|
||||
let event_loop =
|
||||
self.cast_ref::<AppKitActiveEventLoop>().expect("non macOS event loop on macOS");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non macOS event loop on macOS");
|
||||
event_loop.allows_automatic_window_tabbing()
|
||||
}
|
||||
}
|
||||
@@ -608,3 +580,52 @@ pub enum OptionAsAlt {
|
||||
#[default]
|
||||
None,
|
||||
}
|
||||
|
||||
/// Additional events on [`ApplicationHandler`] that are specific to macOS.
|
||||
///
|
||||
/// This can be registered with [`ApplicationHandler::macos_handler`].
|
||||
pub trait ApplicationHandlerExtMacOS: ApplicationHandler {
|
||||
/// The system interpreted a keypress as a standard key binding command.
|
||||
///
|
||||
/// Examples include inserting tabs and newlines, or moving the insertion point, see
|
||||
/// [`NSStandardKeyBindingResponding`] for the full list of key bindings. They are often text
|
||||
/// editing related.
|
||||
///
|
||||
/// This corresponds to the [`doCommandBySelector:`] method on `NSTextInputClient`.
|
||||
///
|
||||
/// The `action` parameter contains the string representation of the selector. Examples include
|
||||
/// `"insertBacktab:"`, `"indent:"` and `"noop:"`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// impl ApplicationHandlerExtMacOS for App {
|
||||
/// fn standard_key_binding(
|
||||
/// &mut self,
|
||||
/// event_loop: &dyn ActiveEventLoop,
|
||||
/// window_id: WindowId,
|
||||
/// action: &str,
|
||||
/// ) {
|
||||
/// match action {
|
||||
/// "moveBackward:" => self.cursor.position -= 1,
|
||||
/// "moveForward:" => self.cursor.position += 1,
|
||||
/// _ => {} // Ignore other actions
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// [`NSStandardKeyBindingResponding`]: https://developer.apple.com/documentation/appkit/nsstandardkeybindingresponding?language=objc
|
||||
/// [`doCommandBySelector:`]: https://developer.apple.com/documentation/appkit/nstextinputclient/1438256-docommandbyselector?language=objc
|
||||
#[doc(alias = "doCommandBySelector:")]
|
||||
fn standard_key_binding(
|
||||
&mut self,
|
||||
event_loop: &dyn ActiveEventLoop,
|
||||
window_id: WindowId,
|
||||
action: &str,
|
||||
) {
|
||||
let _ = event_loop;
|
||||
let _ = window_id;
|
||||
let _ = action;
|
||||
}
|
||||
}
|
||||
48
src/platform/mod.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
//! Contains traits with platform-specific methods in them.
|
||||
//!
|
||||
//! Only the modules corresponding to the platform you're compiling to will be available.
|
||||
|
||||
#[cfg(android_platform)]
|
||||
pub mod android;
|
||||
#[cfg(ios_platform)]
|
||||
pub mod ios;
|
||||
#[cfg(macos_platform)]
|
||||
pub mod macos;
|
||||
#[cfg(orbital_platform)]
|
||||
pub mod orbital;
|
||||
#[cfg(any(x11_platform, wayland_platform))]
|
||||
pub mod startup_notify;
|
||||
#[cfg(wayland_platform)]
|
||||
pub mod wayland;
|
||||
#[cfg(web_platform)]
|
||||
pub mod web;
|
||||
#[cfg(windows_platform)]
|
||||
pub mod windows;
|
||||
#[cfg(x11_platform)]
|
||||
pub mod x11;
|
||||
|
||||
#[allow(unused_imports)]
|
||||
#[cfg(any(
|
||||
windows_platform,
|
||||
macos_platform,
|
||||
android_platform,
|
||||
x11_platform,
|
||||
wayland_platform,
|
||||
docsrs,
|
||||
))]
|
||||
pub mod run_on_demand;
|
||||
|
||||
#[cfg(any(
|
||||
windows_platform,
|
||||
macos_platform,
|
||||
android_platform,
|
||||
x11_platform,
|
||||
wayland_platform,
|
||||
docsrs,
|
||||
))]
|
||||
pub mod pump_events;
|
||||
|
||||
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, docsrs))]
|
||||
pub mod scancode;
|
||||
|
||||
pub mod event_loop;
|
||||
6
src/platform/orbital.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
//! # Orbital / Redox OS
|
||||
//!
|
||||
//! Redox OS has some functionality not yet present that will be implemented
|
||||
//! when its orbital display server provides it.
|
||||
|
||||
// There are no Orbital specific traits yet.
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::event_loop::EventLoop;
|
||||
|
||||
#[allow(rustdoc::broken_intra_doc_links)] // FIXME(madsmtm): Fix these.
|
||||
/// Additional methods on [`EventLoop`] for pumping events within an external event loop
|
||||
pub trait EventLoopExtPumpEvents {
|
||||
/// Pump the `EventLoop` to check for and dispatch pending events.
|
||||
@@ -99,11 +99,23 @@ pub trait EventLoopExtPumpEvents {
|
||||
/// If you render outside of Winit you are likely to see window resizing artifacts
|
||||
/// since MacOS expects applications to render synchronously during any `drawRect`
|
||||
/// callback.
|
||||
fn pump_app_events<A: ApplicationHandler>(
|
||||
fn pump_app_events(
|
||||
&mut self,
|
||||
timeout: Option<Duration>,
|
||||
app: A,
|
||||
) -> PumpStatus;
|
||||
app: impl ApplicationHandler,
|
||||
) -> PumpStatus
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
impl EventLoopExtPumpEvents for EventLoop {
|
||||
fn pump_app_events(
|
||||
&mut self,
|
||||
timeout: Option<Duration>,
|
||||
app: impl ApplicationHandler,
|
||||
) -> PumpStatus {
|
||||
self.event_loop.pump_app_events(timeout, app)
|
||||
}
|
||||
}
|
||||
|
||||
/// The return status for `pump_events`
|
||||
@@ -1,18 +1,19 @@
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::error::EventLoopError;
|
||||
use crate::event_loop::EventLoop;
|
||||
#[cfg(doc)]
|
||||
use crate::{
|
||||
event_loop::{ActiveEventLoop, pump_events::EventLoopExtPumpEvents},
|
||||
window::Window,
|
||||
event_loop::ActiveEventLoop, platform::pump_events::EventLoopExtPumpEvents, window::Window,
|
||||
};
|
||||
|
||||
/// Additional methods on [`EventLoop`] to return control flow to the caller.
|
||||
pub trait EventLoopExtRunOnDemand {
|
||||
/// Run the application with the event loop on the calling thread.
|
||||
///
|
||||
/// Unlike [`EventLoop::run_app`], this function accepts non-`'static` (i.e. non-`move`)
|
||||
/// state and it is possible to return control back to the caller without consuming the
|
||||
/// `EventLoop` (by using [`exit()`]) and so the event loop can be re-run after it has exit.
|
||||
/// Unlike [`EventLoopProvider::run_app()`], this function accepts non-`'static` (i.e.
|
||||
/// non-`move`) closures and it is possible to return control back to the caller without
|
||||
/// consuming the `EventLoop` (by using [`exit()`]) and
|
||||
/// so the event loop can be re-run after it has exit.
|
||||
///
|
||||
/// It's expected that each run of the loop will be for orthogonal instantiations of your
|
||||
/// Winit application, but internally each instantiation may re-use some common window
|
||||
@@ -29,11 +30,18 @@ pub trait EventLoopExtRunOnDemand {
|
||||
///
|
||||
/// # Caveats
|
||||
/// - This extension isn't available on all platforms, since it's not always possible to return
|
||||
/// to the caller (specifically this is impossible on iOS and Web).
|
||||
/// to the caller (specifically this is impossible on iOS and Web - though with the Web
|
||||
/// backend it is possible to use
|
||||
#[cfg_attr(
|
||||
web_platform,
|
||||
doc = " [`EventLoopExtWeb::spawn_app()`][crate::platform::web::EventLoopExtWeb::spawn_app()]"
|
||||
)]
|
||||
#[cfg_attr(not(web_platform), doc = " `EventLoopExtWeb::spawn_app()`")]
|
||||
/// [^1] more than once instead).
|
||||
/// - No [`Window`] state can be carried between separate runs of the event loop.
|
||||
///
|
||||
/// You are strongly encouraged to use [`EventLoop::run_app()`] for portability, unless you
|
||||
/// specifically need the ability to re-run a single event loop more than once
|
||||
/// You are strongly encouraged to use [`EventLoopProvider::run_app()`] for portability, unless
|
||||
/// you specifically need the ability to re-run a single event loop more than once
|
||||
///
|
||||
/// # Supported Platforms
|
||||
/// - Windows
|
||||
@@ -48,7 +56,31 @@ pub trait EventLoopExtRunOnDemand {
|
||||
/// 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.
|
||||
///
|
||||
/// [^1]: `spawn_app()` is only available on the Web platforms.
|
||||
///
|
||||
/// [`exit()`]: ActiveEventLoop::exit()
|
||||
/// [`set_control_flow()`]: ActiveEventLoop::set_control_flow()
|
||||
fn run_app_on_demand<A: ApplicationHandler>(&mut self, app: A) -> Result<(), EventLoopError>;
|
||||
/// [`EventLoopProvider::run_app()`]: crate::event_loop::EventLoopProvider::run_app
|
||||
fn run_app_on_demand(&mut self, app: impl ApplicationHandler) -> Result<(), EventLoopError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
impl EventLoopExtRunOnDemand for EventLoop {
|
||||
fn run_app_on_demand(&mut self, app: impl ApplicationHandler) -> Result<(), EventLoopError> {
|
||||
self.event_loop.run_app_on_demand(app)
|
||||
}
|
||||
}
|
||||
|
||||
/// ```compile_fail
|
||||
/// use winit::event_loop::EventLoop;
|
||||
/// use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
|
||||
///
|
||||
/// let mut event_loop = EventLoop::new().unwrap();
|
||||
/// event_loop.run_on_demand(|_, _| {
|
||||
/// // Attempt to run the event loop re-entrantly; this must fail.
|
||||
/// event_loop.run_on_demand(|_, _| {});
|
||||
/// });
|
||||
/// ```
|
||||
#[allow(dead_code)]
|
||||
fn test_run_on_demand_cannot_access_event_loop() {}
|
||||
@@ -27,7 +27,7 @@ use crate::error::{NotSupportedError, RequestError};
|
||||
use crate::event_loop::{ActiveEventLoop, AsyncRequestSerial};
|
||||
#[cfg(wayland_platform)]
|
||||
use crate::platform::wayland::ActiveEventLoopExtWayland;
|
||||
use crate::window::{ActivationToken, Window};
|
||||
use crate::window::{ActivationToken, Window, WindowAttributes};
|
||||
|
||||
/// The variable which is used mostly on X11.
|
||||
const X11_VAR: &str = "DESKTOP_STARTUP_ID";
|
||||
@@ -80,7 +80,7 @@ impl WindowExtStartupNotify for dyn Window + '_ {
|
||||
}
|
||||
|
||||
#[cfg(x11_platform)]
|
||||
if let Some(window) = self.cast_ref::<crate::platform_impl::x11::Window>() {
|
||||
if let Some(window) = self.cast_ref::<crate::platform_impl::x11::window::Window>() {
|
||||
return window.request_activation_token();
|
||||
}
|
||||
|
||||
@@ -88,24 +88,26 @@ impl WindowExtStartupNotify for dyn Window + '_ {
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowAttributesExtStartupNotify for WindowAttributes {
|
||||
fn with_activation_token(mut self, token: ActivationToken) -> Self {
|
||||
self.platform_specific.activation_token = Some(token);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove the activation environment variables from the current process.
|
||||
///
|
||||
/// This is wise to do before running child processes,
|
||||
/// which may not to support the activation token.
|
||||
pub fn reset_activation_token_env() {
|
||||
unsafe {
|
||||
env::remove_var(X11_VAR);
|
||||
env::remove_var(WAYLAND_VAR);
|
||||
}
|
||||
env::remove_var(X11_VAR);
|
||||
env::remove_var(WAYLAND_VAR);
|
||||
}
|
||||
|
||||
/// Set environment variables responsible for activation token.
|
||||
///
|
||||
/// This could be used before running daemon processes.
|
||||
pub fn set_activation_token_env(token: ActivationToken) {
|
||||
let token = token.into_raw();
|
||||
unsafe {
|
||||
env::set_var(X11_VAR, &token);
|
||||
env::set_var(WAYLAND_VAR, token);
|
||||
}
|
||||
env::set_var(X11_VAR, &token.token);
|
||||
env::set_var(WAYLAND_VAR, token.token);
|
||||
}
|
||||
111
src/platform/wayland.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
//! # Wayland
|
||||
//!
|
||||
//! **Note:** Windows don't appear on Wayland until you draw/present to them.
|
||||
//!
|
||||
//! By default, Winit loads system libraries using `dlopen`. This can be
|
||||
//! disabled by disabling the `"wayland-dlopen"` cargo feature.
|
||||
//!
|
||||
//! ## Client-side decorations
|
||||
//!
|
||||
//! Winit provides client-side decorations by default, but the behaviour can
|
||||
//! be controlled with the following feature flags:
|
||||
//!
|
||||
//! * `wayland-csd-adwaita` (default).
|
||||
//! * `wayland-csd-adwaita-crossfont`.
|
||||
//! * `wayland-csd-adwaita-notitle`.
|
||||
|
||||
use std::ffi::c_void;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
|
||||
use crate::platform_impl::wayland::Window;
|
||||
pub use crate::window::Theme;
|
||||
use crate::window::{Window as CoreWindow, WindowAttributes};
|
||||
|
||||
/// Additional methods on [`ActiveEventLoop`] that are specific to Wayland.
|
||||
pub trait ActiveEventLoopExtWayland {
|
||||
/// True if the [`ActiveEventLoop`] uses Wayland.
|
||||
fn is_wayland(&self) -> bool;
|
||||
}
|
||||
|
||||
impl ActiveEventLoopExtWayland for dyn ActiveEventLoop + '_ {
|
||||
#[inline]
|
||||
fn is_wayland(&self) -> bool {
|
||||
self.cast_ref::<crate::platform_impl::wayland::ActiveEventLoop>().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`EventLoop`] that are specific to Wayland.
|
||||
pub trait EventLoopExtWayland {
|
||||
/// True if the [`EventLoop`] uses Wayland.
|
||||
fn is_wayland(&self) -> bool;
|
||||
}
|
||||
|
||||
impl EventLoopExtWayland for EventLoop {
|
||||
#[inline]
|
||||
fn is_wayland(&self) -> bool {
|
||||
self.event_loop.is_wayland()
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`EventLoopBuilder`] that are specific to Wayland.
|
||||
pub trait EventLoopBuilderExtWayland {
|
||||
/// Force using Wayland.
|
||||
fn with_wayland(&mut self) -> &mut Self;
|
||||
|
||||
/// Whether to allow the event loop to be created off of the main thread.
|
||||
///
|
||||
/// By default, the window is only allowed to be created on the main
|
||||
/// thread, to make platform compatibility easier.
|
||||
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
|
||||
}
|
||||
|
||||
impl EventLoopBuilderExtWayland for EventLoopBuilder {
|
||||
#[inline]
|
||||
fn with_wayland(&mut self) -> &mut Self {
|
||||
self.platform_specific.forced_backend = Some(crate::platform_impl::Backend::Wayland);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
|
||||
self.platform_specific.any_thread = any_thread;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`Window`] that are specific to Wayland.
|
||||
///
|
||||
/// [`Window`]: crate::window::Window
|
||||
pub trait WindowExtWayland {
|
||||
/// Returns `xdg_toplevel` of the window or [`None`] if the window is X11 window.
|
||||
fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;
|
||||
}
|
||||
|
||||
impl WindowExtWayland for dyn CoreWindow + '_ {
|
||||
#[inline]
|
||||
fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
|
||||
self.cast_ref::<Window>()?.xdg_toplevel()
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`WindowAttributes`] that are specific to Wayland.
|
||||
pub trait WindowAttributesExtWayland {
|
||||
/// Build window with the given name.
|
||||
///
|
||||
/// The `general` name sets an application ID, which should match the `.desktop`
|
||||
/// file distributed with your program. The `instance` is a `no-op`.
|
||||
///
|
||||
/// 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)
|
||||
fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
|
||||
}
|
||||
|
||||
impl WindowAttributesExtWayland for WindowAttributes {
|
||||
#[inline]
|
||||
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
|
||||
self.platform_specific.name =
|
||||
Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into()));
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,10 @@
|
||||
//! yourself.
|
||||
//!
|
||||
//! [canvas]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement
|
||||
//! [with_canvas]: WindowAttributesWeb::with_canvas
|
||||
//! [with_canvas]: WindowAttributesExtWeb::with_canvas
|
||||
//! [get]: WindowExtWeb::canvas
|
||||
//! [insert]: WindowAttributesWeb::with_append
|
||||
//! [wasm_bindgen]: https://docs.rs/wasm-bindgen
|
||||
//! [insert]: WindowAttributesExtWeb::with_append
|
||||
#![cfg_attr(not(web_platform), doc = "[wasm_bindgen]: https://docs.rs/wasm-bindgen")]
|
||||
//! [Rust and WebAssembly book]: https://rustwasm.github.io/book
|
||||
//!
|
||||
//! ## CSS properties
|
||||
@@ -41,39 +41,6 @@
|
||||
//! [`WindowEvent::PointerLeft`]: crate::event::WindowEvent::PointerLeft
|
||||
//! [`Window::set_outer_position()`]: crate::window::Window::set_outer_position
|
||||
|
||||
// Brief introduction to the internals of the Web backend:
|
||||
// The Web backend used to support both wasm-bindgen and stdweb as methods of binding to the
|
||||
// environment. Because they are both supporting the same underlying APIs, the actual Web bindings
|
||||
// are cordoned off into backend abstractions, which present the thinnest unifying layer possible.
|
||||
//
|
||||
// When adding support for new events or interactions with the browser, first consult trusted
|
||||
// documentation (such as MDN) to ensure it is well-standardised and supported across many browsers.
|
||||
// Once you have decided on the relevant Web APIs, add support to both backends.
|
||||
//
|
||||
// The backend is used by the rest of the module to implement Winit's business logic, which forms
|
||||
// the rest of the code. 'device', 'error', 'monitor', and 'window' define Web-specific structures
|
||||
// for winit's cross-platform structures. They are all relatively simple translations.
|
||||
//
|
||||
// The event_loop module handles listening for and processing events. 'Proxy' implements
|
||||
// EventLoopProxy and 'WindowTarget' implements ActiveEventLoop. WindowTarget also handles
|
||||
// registering the event handlers. The 'Execution' struct in the 'runner' module handles taking
|
||||
// incoming events (from the registered handlers) and ensuring they are passed to the user in a
|
||||
// compliant way.
|
||||
|
||||
macro_rules! os_error {
|
||||
($error:expr) => {{ winit_core::error::OsError::new(line!(), file!(), $error) }};
|
||||
}
|
||||
|
||||
mod r#async;
|
||||
mod cursor;
|
||||
mod event;
|
||||
pub(crate) mod event_loop;
|
||||
mod lock;
|
||||
pub(crate) mod main_thread;
|
||||
mod monitor;
|
||||
pub(crate) mod web_sys;
|
||||
pub(crate) mod window;
|
||||
|
||||
use std::cell::Ref;
|
||||
use std::error::Error;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
@@ -82,26 +49,29 @@ use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use ::web_sys::HtmlCanvasElement;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use winit_core::cursor::{CustomCursor, CustomCursorSource};
|
||||
use winit_core::error::NotSupportedError;
|
||||
use winit_core::event_loop::ActiveEventLoop;
|
||||
use winit_core::monitor::MonitorHandleProvider;
|
||||
use winit_core::window::{PlatformWindowAttributes, Window};
|
||||
#[cfg(web_platform)]
|
||||
use web_sys::HtmlCanvasElement;
|
||||
|
||||
pub use self::event_loop::{EventLoop, PlatformSpecificEventLoopAttributes};
|
||||
use self::web_sys as backend;
|
||||
use self::window::Window as WebWindow;
|
||||
use crate::cursor::CustomCursorFuture as PlatformCustomCursorFuture;
|
||||
use crate::event_loop::ActiveEventLoop as WebActiveEventLoop;
|
||||
use crate::main_thread::{MainThreadMarker, MainThreadSafe};
|
||||
use crate::monitor::{
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::cursor::CustomCursorSource;
|
||||
use crate::error::NotSupportedError;
|
||||
use crate::event_loop::{ActiveEventLoop, EventLoop};
|
||||
use crate::monitor::MonitorHandleProvider;
|
||||
use crate::platform_impl::MonitorHandle as WebMonitorHandle;
|
||||
#[cfg(web_platform)]
|
||||
use crate::platform_impl::{
|
||||
CustomCursorFuture as PlatformCustomCursorFuture,
|
||||
HasMonitorPermissionFuture as PlatformHasMonitorPermissionFuture,
|
||||
MonitorHandle as WebMonitorHandle, MonitorPermissionFuture as PlatformMonitorPermissionFuture,
|
||||
MonitorPermissionFuture as PlatformMonitorPermissionFuture,
|
||||
OrientationLockFuture as PlatformOrientationLockFuture,
|
||||
};
|
||||
use crate::window::{CustomCursor, Window, WindowAttributes};
|
||||
|
||||
#[cfg(not(web_platform))]
|
||||
#[doc(hidden)]
|
||||
pub struct HtmlCanvasElement;
|
||||
|
||||
pub trait WindowExtWeb {
|
||||
/// Only returns the canvas if called from inside the window context (the
|
||||
@@ -135,51 +105,37 @@ pub trait WindowExtWeb {
|
||||
impl WindowExtWeb for dyn Window + '_ {
|
||||
#[inline]
|
||||
fn canvas(&self) -> Option<Ref<'_, HtmlCanvasElement>> {
|
||||
self.cast_ref::<WebWindow>().expect("non Web window on Web").canvas()
|
||||
self.cast_ref::<crate::platform_impl::Window>().expect("non Web window on Web").canvas()
|
||||
}
|
||||
|
||||
fn prevent_default(&self) -> bool {
|
||||
self.cast_ref::<WebWindow>().expect("non Web window on Web").prevent_default()
|
||||
self.cast_ref::<crate::platform_impl::Window>()
|
||||
.expect("non Web window on Web")
|
||||
.prevent_default()
|
||||
}
|
||||
|
||||
fn set_prevent_default(&self, prevent_default: bool) {
|
||||
self.cast_ref::<WebWindow>()
|
||||
self.cast_ref::<crate::platform_impl::Window>()
|
||||
.expect("non Web window on Web")
|
||||
.set_prevent_default(prevent_default)
|
||||
}
|
||||
|
||||
fn is_cursor_lock_raw(&self) -> bool {
|
||||
self.cast_ref::<WebWindow>().expect("non Web window on Web").is_cursor_lock_raw()
|
||||
self.cast_ref::<crate::platform_impl::Window>()
|
||||
.expect("non Web window on Web")
|
||||
.is_cursor_lock_raw()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct WindowAttributesWeb {
|
||||
pub(crate) canvas: Option<Arc<MainThreadSafe<backend::RawCanvasType>>>,
|
||||
pub(crate) prevent_default: bool,
|
||||
pub(crate) focusable: bool,
|
||||
pub(crate) append: bool,
|
||||
}
|
||||
|
||||
impl WindowAttributesWeb {
|
||||
pub trait WindowAttributesExtWeb {
|
||||
/// Pass an [`HtmlCanvasElement`] to be used for this [`Window`]. If [`None`],
|
||||
/// the default one will be created.
|
||||
/// [`WindowAttributes::default()`] will create one.
|
||||
///
|
||||
/// In any case, the canvas won't be automatically inserted into the Web page.
|
||||
///
|
||||
/// [`None`] by default.
|
||||
pub fn with_canvas(mut self, canvas: Option<HtmlCanvasElement>) -> Self {
|
||||
match canvas {
|
||||
Some(canvas) => {
|
||||
let main_thread = MainThreadMarker::new()
|
||||
.expect("received a `HtmlCanvasElement` outside the window context");
|
||||
self.canvas = Some(Arc::new(MainThreadSafe::new(main_thread, canvas)));
|
||||
},
|
||||
None => self.canvas = None,
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
#[cfg_attr(not(web_platform), doc = "", doc = "[`HtmlCanvasElement`]: #only-available-on-wasm")]
|
||||
fn with_canvas(self, canvas: Option<HtmlCanvasElement>) -> Self;
|
||||
|
||||
/// Sets whether `event.preventDefault()` should be called on events on the
|
||||
/// canvas that have side effects.
|
||||
@@ -187,55 +143,68 @@ impl WindowAttributesWeb {
|
||||
/// See [`WindowExtWeb::set_prevent_default()`] for more details.
|
||||
///
|
||||
/// Enabled by default.
|
||||
pub fn with_prevent_default(mut self, prevent_default: bool) -> Self {
|
||||
self.prevent_default = prevent_default;
|
||||
self
|
||||
}
|
||||
fn with_prevent_default(self, prevent_default: bool) -> Self;
|
||||
|
||||
/// Whether the canvas should be focusable using the tab key. This is necessary to capture
|
||||
/// canvas keyboard events.
|
||||
///
|
||||
/// Enabled by default.
|
||||
pub fn with_focusable(mut self, focusable: bool) -> Self {
|
||||
self.focusable = focusable;
|
||||
self
|
||||
}
|
||||
fn with_focusable(self, focusable: bool) -> Self;
|
||||
|
||||
/// On window creation, append the canvas element to the Web page if it isn't already.
|
||||
///
|
||||
/// Disabled by default.
|
||||
pub fn with_append(mut self, append: bool) -> Self {
|
||||
self.append = append;
|
||||
fn with_append(self, append: bool) -> Self;
|
||||
}
|
||||
|
||||
impl WindowAttributesExtWeb for WindowAttributes {
|
||||
fn with_canvas(mut self, canvas: Option<HtmlCanvasElement>) -> Self {
|
||||
self.platform_specific.set_canvas(canvas);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl PlatformWindowAttributes for WindowAttributesWeb {
|
||||
fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
|
||||
Box::from(self.clone())
|
||||
fn with_prevent_default(mut self, prevent_default: bool) -> Self {
|
||||
self.platform_specific.prevent_default = prevent_default;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for WindowAttributesWeb {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
(match (&self.canvas, &other.canvas) {
|
||||
(Some(this), Some(other)) => Arc::ptr_eq(this, other),
|
||||
(None, None) => true,
|
||||
_ => false,
|
||||
}) && self.prevent_default.eq(&other.prevent_default)
|
||||
&& self.focusable.eq(&other.focusable)
|
||||
&& self.append.eq(&other.append)
|
||||
fn with_focusable(mut self, focusable: bool) -> Self {
|
||||
self.platform_specific.focusable = focusable;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WindowAttributesWeb {
|
||||
fn default() -> Self {
|
||||
Self { canvas: None, prevent_default: true, focusable: true, append: false }
|
||||
fn with_append(mut self, append: bool) -> Self {
|
||||
self.platform_specific.append = append;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on `EventLoop` that are specific to the Web.
|
||||
pub trait EventLoopExtWeb {
|
||||
/// Initializes the winit event loop.
|
||||
///
|
||||
/// Unlike
|
||||
#[cfg_attr(all(web_platform, target_feature = "exception-handling"), doc = "`run_app()`")]
|
||||
#[cfg_attr(
|
||||
not(all(web_platform, target_feature = "exception-handling")),
|
||||
doc = "[`run_app()`]"
|
||||
)]
|
||||
/// [^1], this returns immediately, and doesn't throw an exception in order to
|
||||
/// satisfy its [`!`] return type.
|
||||
///
|
||||
/// Once the event loop has been destroyed, it's possible to reinitialize another 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
|
||||
/// event loop when switching between tabs on a single page application.
|
||||
#[rustfmt::skip]
|
||||
///
|
||||
#[cfg_attr(
|
||||
not(all(web_platform, target_feature = "exception-handling")),
|
||||
doc = "[`run_app()`]: crate::event_loop::EventLoopProvider::run_app()"
|
||||
)]
|
||||
/// [^1]: `run_app()` is _not_ available on Wasm when the target supports `exception-handling`.
|
||||
fn spawn_app<A: ApplicationHandler + 'static>(self, app: A);
|
||||
|
||||
/// Sets the strategy for [`ControlFlow::Poll`].
|
||||
///
|
||||
/// See [`PollStrategy`].
|
||||
@@ -293,6 +262,40 @@ pub trait EventLoopExtWeb {
|
||||
fn has_detailed_monitor_permission(&self) -> HasMonitorPermissionFuture;
|
||||
}
|
||||
|
||||
impl EventLoopExtWeb for EventLoop {
|
||||
fn spawn_app<A: ApplicationHandler + 'static>(self, app: A) {
|
||||
self.event_loop.spawn_app(app);
|
||||
}
|
||||
|
||||
fn set_poll_strategy(&self, strategy: PollStrategy) {
|
||||
self.event_loop.set_poll_strategy(strategy);
|
||||
}
|
||||
|
||||
fn poll_strategy(&self) -> PollStrategy {
|
||||
self.event_loop.poll_strategy()
|
||||
}
|
||||
|
||||
fn set_wait_until_strategy(&self, strategy: WaitUntilStrategy) {
|
||||
self.event_loop.set_wait_until_strategy(strategy);
|
||||
}
|
||||
|
||||
fn wait_until_strategy(&self) -> WaitUntilStrategy {
|
||||
self.event_loop.wait_until_strategy()
|
||||
}
|
||||
|
||||
fn has_multiple_screens(&self) -> Result<bool, NotSupportedError> {
|
||||
self.event_loop.has_multiple_screens()
|
||||
}
|
||||
|
||||
fn request_detailed_monitor_permission(&self) -> MonitorPermissionFuture {
|
||||
MonitorPermissionFuture(self.event_loop.request_detailed_monitor_permission())
|
||||
}
|
||||
|
||||
fn has_detailed_monitor_permission(&self) -> HasMonitorPermissionFuture {
|
||||
HasMonitorPermissionFuture(self.event_loop.has_detailed_monitor_permission())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ActiveEventLoopExtWeb {
|
||||
/// Sets the strategy for [`ControlFlow::Poll`].
|
||||
///
|
||||
@@ -361,55 +364,73 @@ pub trait ActiveEventLoopExtWeb {
|
||||
impl ActiveEventLoopExtWeb for dyn ActiveEventLoop + '_ {
|
||||
#[inline]
|
||||
fn create_custom_cursor_async(&self, source: CustomCursorSource) -> CustomCursorFuture {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.create_custom_cursor_async(source)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_poll_strategy(&self, strategy: PollStrategy) {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.set_poll_strategy(strategy);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_strategy(&self) -> PollStrategy {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.poll_strategy()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_wait_until_strategy(&self, strategy: WaitUntilStrategy) {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.set_wait_until_strategy(strategy);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn wait_until_strategy(&self) -> WaitUntilStrategy {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.wait_until_strategy()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_cursor_lock_raw(&self) -> bool {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.is_cursor_lock_raw()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn has_multiple_screens(&self) -> Result<bool, NotSupportedError> {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.has_multiple_screens()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn request_detailed_monitor_permission(&self) -> MonitorPermissionFuture {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
MonitorPermissionFuture(event_loop.request_detailed_monitor_permission())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn has_detailed_monitor_permission(&self) -> bool {
|
||||
let event_loop = self.cast_ref::<WebActiveEventLoop>().expect("non Web event loop on Web");
|
||||
let event_loop = self
|
||||
.cast_ref::<crate::platform_impl::ActiveEventLoop>()
|
||||
.expect("non Web event loop on Web");
|
||||
event_loop.has_detailed_monitor_permission()
|
||||
}
|
||||
}
|
||||
@@ -465,6 +486,9 @@ pub enum WaitUntilStrategy {
|
||||
Worker,
|
||||
}
|
||||
|
||||
#[cfg(not(web_platform))]
|
||||
struct PlatformCustomCursorFuture;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CustomCursorFuture(pub(crate) PlatformCustomCursorFuture);
|
||||
|
||||
@@ -494,6 +518,9 @@ impl Display for CustomCursorError {
|
||||
|
||||
impl Error for CustomCursorError {}
|
||||
|
||||
#[cfg(not(web_platform))]
|
||||
struct PlatformMonitorPermissionFuture;
|
||||
|
||||
/// Can be dropped without aborting the request for detailed monitor permissions.
|
||||
#[derive(Debug)]
|
||||
pub struct MonitorPermissionFuture(pub(crate) PlatformMonitorPermissionFuture);
|
||||
@@ -536,6 +563,9 @@ impl Display for MonitorPermissionError {
|
||||
|
||||
impl Error for MonitorPermissionError {}
|
||||
|
||||
#[cfg(not(web_platform))]
|
||||
struct PlatformHasMonitorPermissionFuture;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HasMonitorPermissionFuture(PlatformHasMonitorPermissionFuture);
|
||||
|
||||
@@ -651,6 +681,9 @@ pub enum OrientationLock {
|
||||
},
|
||||
}
|
||||
|
||||
#[cfg(not(web_platform))]
|
||||
struct PlatformOrientationLockFuture;
|
||||
|
||||
/// Can be dropped without aborting the request to lock the screen.
|
||||
#[derive(Debug)]
|
||||
pub struct OrientationLockFuture(PlatformOrientationLockFuture);
|
||||
@@ -1,44 +1,24 @@
|
||||
//! # Winit Win32 / Windows backend
|
||||
//! # Windows
|
||||
//!
|
||||
//! The supported OS version is Windows 7 or higher, though Windows 10 is
|
||||
//! tested regularly.
|
||||
#![cfg(target_os = "windows")] // FIXME(madsmtm): Allow compiling on all platforms.
|
||||
|
||||
#[macro_use]
|
||||
mod util;
|
||||
mod dark_mode;
|
||||
mod definitions;
|
||||
mod dpi;
|
||||
mod drop_handler;
|
||||
mod event_loop;
|
||||
mod icon;
|
||||
mod ime;
|
||||
mod keyboard;
|
||||
mod keyboard_layout;
|
||||
mod monitor;
|
||||
mod raw_input;
|
||||
mod window;
|
||||
mod window_state;
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use std::ffi::c_void;
|
||||
use std::ops::Deref;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ::dpi::PhysicalSize;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(windows_platform)]
|
||||
use windows_sys::Win32::Foundation::HANDLE;
|
||||
use winit_core::event::DeviceId;
|
||||
use winit_core::icon::{BadIcon, Icon};
|
||||
use winit_core::window::{PlatformWindowAttributes, Window as CoreWindow};
|
||||
|
||||
pub use self::event_loop::{EventLoop, PlatformSpecificEventLoopAttributes};
|
||||
use self::icon::{RaiiIcon, SelectedCursor};
|
||||
pub use self::keyboard::{physicalkey_to_scancode, scancode_to_physicalkey};
|
||||
pub use self::monitor::{MonitorHandle, VideoModeHandle};
|
||||
pub use self::window::Window;
|
||||
use crate::dpi::PhysicalSize;
|
||||
use crate::event::DeviceId;
|
||||
use crate::event_loop::EventLoopBuilder;
|
||||
use crate::icon::BadIcon;
|
||||
use crate::platform_impl::RaiiIcon;
|
||||
use crate::window::{Icon, Window, WindowAttributes};
|
||||
|
||||
/// Window Handle type used by Win32 API
|
||||
pub type HWND = *mut c_void;
|
||||
@@ -140,24 +120,24 @@ pub enum CornerPreference {
|
||||
///
|
||||
/// See [`WindowBorrowExtWindows::any_thread`] for more information.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AnyThread<W: CoreWindow>(W);
|
||||
pub struct AnyThread<W: Window>(W);
|
||||
|
||||
impl<W: CoreWindow> AnyThread<W> {
|
||||
impl<W: Window> AnyThread<W> {
|
||||
/// Get a reference to the inner window.
|
||||
#[inline]
|
||||
pub fn get_ref(&self) -> &dyn CoreWindow {
|
||||
pub fn get_ref(&self) -> &dyn Window {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: CoreWindow> Deref for AnyThread<W> {
|
||||
impl<W: Window> Deref for AnyThread<W> {
|
||||
type Target = W;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: CoreWindow> rwh_06::HasWindowHandle for AnyThread<W> {
|
||||
impl<W: 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() }
|
||||
@@ -232,13 +212,36 @@ pub trait EventLoopBuilderExtWindows {
|
||||
F: FnMut(*const c_void) -> bool + 'static;
|
||||
}
|
||||
|
||||
impl EventLoopBuilderExtWindows for EventLoopBuilder {
|
||||
#[inline]
|
||||
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
|
||||
self.platform_specific.any_thread = any_thread;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_dpi_aware(&mut self, dpi_aware: bool) -> &mut Self {
|
||||
self.platform_specific.dpi_aware = dpi_aware;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_msg_hook<F>(&mut self, callback: F) -> &mut Self
|
||||
where
|
||||
F: FnMut(*const c_void) -> bool + 'static,
|
||||
{
|
||||
self.platform_specific.msg_hook = Some(Box::new(callback));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on `Window` that are specific to Windows.
|
||||
pub trait WindowExtWindows {
|
||||
/// Enables or disables mouse and keyboard input to the specified window.
|
||||
///
|
||||
/// A window must be enabled before it can be activated.
|
||||
/// If an application has create a modal dialog box by disabling its owner window
|
||||
/// (as described in [`WindowAttributesWindows::with_owner_window`]), the application must
|
||||
/// (as described in [`WindowAttributesExtWindows::with_owner_window`]), the application must
|
||||
/// enable the owner window before destroying the dialog box.
|
||||
/// Otherwise, another window will receive the keyboard focus and be activated.
|
||||
///
|
||||
@@ -285,15 +288,6 @@ pub trait WindowExtWindows {
|
||||
/// Supported starting with Windows 11 Build 22000.
|
||||
fn set_corner_preference(&self, preference: CornerPreference);
|
||||
|
||||
/// Sets if the reported [`winit_core::event::WindowEvent::MouseWheel`] event
|
||||
/// should account for scroll speed system settings.
|
||||
///
|
||||
/// The default scroll speed on Windows is 3 lines/characters per scroll,
|
||||
/// this will be 1 if you set it to false.
|
||||
///
|
||||
/// The default is `true`.
|
||||
fn set_use_system_scroll_speed(&self, should_use: bool);
|
||||
|
||||
/// 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
|
||||
@@ -349,40 +343,40 @@ pub trait WindowExtWindows {
|
||||
) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError>;
|
||||
}
|
||||
|
||||
impl WindowExtWindows for dyn CoreWindow + '_ {
|
||||
impl WindowExtWindows for dyn Window + '_ {
|
||||
#[inline]
|
||||
fn set_enable(&self, enabled: bool) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_enable(enabled)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_taskbar_icon(&self, taskbar_icon: Option<Icon>) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_taskbar_icon(taskbar_icon)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_skip_taskbar(&self, skip: bool) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_skip_taskbar(skip)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_undecorated_shadow(&self, shadow: bool) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_undecorated_shadow(shadow)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_system_backdrop(&self, backdrop_type: BackdropType) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_system_backdrop(backdrop_type)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_border_color(&self, color: Option<Color>) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_border_color(color.unwrap_or(Color::NONE))
|
||||
}
|
||||
|
||||
@@ -391,31 +385,26 @@ impl WindowExtWindows for dyn CoreWindow + '_ {
|
||||
// The windows docs don't mention NONE as a valid options but it works in practice and is
|
||||
// useful to circumvent the Windows option "Show accent color on title bars and
|
||||
// window borders"
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_title_background_color(color.unwrap_or(Color::NONE))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_title_text_color(&self, color: Color) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_title_text_color(color)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_corner_preference(&self, preference: CornerPreference) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
window.set_corner_preference(preference)
|
||||
}
|
||||
|
||||
fn set_use_system_scroll_speed(&self, should_use: bool) {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
window.set_use_system_scroll_speed(should_use)
|
||||
}
|
||||
|
||||
unsafe fn window_handle_any_thread(
|
||||
&self,
|
||||
) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError> {
|
||||
let window = self.cast_ref::<Window>().unwrap();
|
||||
let window = self.cast_ref::<crate::platform_impl::Window>().unwrap();
|
||||
unsafe {
|
||||
let handle = window.rwh_06_no_thread_check()?;
|
||||
|
||||
@@ -428,7 +417,7 @@ impl WindowExtWindows for dyn CoreWindow + '_ {
|
||||
/// Additional methods for anything that dereference to [`Window`].
|
||||
///
|
||||
/// [`Window`]: crate::window::Window
|
||||
pub trait WindowBorrowExtWindows: Borrow<dyn CoreWindow> + Sized {
|
||||
pub trait WindowBorrowExtWindows: Borrow<dyn 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
|
||||
@@ -449,59 +438,17 @@ pub trait WindowBorrowExtWindows: Borrow<dyn CoreWindow> + Sized {
|
||||
/// [`window_handle_any_thread`]: WindowExtWindows::window_handle_any_thread
|
||||
unsafe fn any_thread(self) -> AnyThread<Self>
|
||||
where
|
||||
Self: CoreWindow,
|
||||
Self: Window,
|
||||
{
|
||||
AnyThread(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Borrow<dyn CoreWindow> + Sized> WindowBorrowExtWindows for W {}
|
||||
impl<W: Borrow<dyn Window> + Sized> WindowBorrowExtWindows for W {}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct WindowAttributesWindows {
|
||||
pub(crate) owner: Option<HWND>,
|
||||
pub(crate) menu: Option<HMENU>,
|
||||
pub(crate) taskbar_icon: Option<Icon>,
|
||||
pub(crate) no_redirection_bitmap: bool,
|
||||
pub(crate) drag_and_drop: bool,
|
||||
pub(crate) skip_taskbar: bool,
|
||||
pub(crate) class_name: String,
|
||||
pub(crate) decoration_shadow: bool,
|
||||
pub(crate) backdrop_type: BackdropType,
|
||||
pub(crate) clip_children: bool,
|
||||
pub(crate) border_color: Option<Color>,
|
||||
pub(crate) title_background_color: Option<Color>,
|
||||
pub(crate) title_text_color: Option<Color>,
|
||||
pub(crate) corner_preference: Option<CornerPreference>,
|
||||
pub(crate) use_system_wheel_speed: bool,
|
||||
}
|
||||
|
||||
impl Default for WindowAttributesWindows {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
owner: None,
|
||||
menu: None,
|
||||
taskbar_icon: None,
|
||||
no_redirection_bitmap: false,
|
||||
drag_and_drop: true,
|
||||
skip_taskbar: false,
|
||||
class_name: "Window Class".to_string(),
|
||||
decoration_shadow: false,
|
||||
backdrop_type: BackdropType::default(),
|
||||
clip_children: true,
|
||||
border_color: None,
|
||||
title_background_color: None,
|
||||
title_text_color: None,
|
||||
corner_preference: None,
|
||||
use_system_wheel_speed: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for WindowAttributesWindows {}
|
||||
unsafe impl Sync for WindowAttributesWindows {}
|
||||
|
||||
impl WindowAttributesWindows {
|
||||
/// Additional methods on `WindowAttributes` that are specific to Windows.
|
||||
#[allow(rustdoc::broken_intra_doc_links)]
|
||||
pub trait WindowAttributesExtWindows {
|
||||
/// 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`.
|
||||
/// Can be used in combination with
|
||||
@@ -514,12 +461,7 @@ impl WindowAttributesWindows {
|
||||
/// - An owned window is hidden when its owner is minimized.
|
||||
///
|
||||
/// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
|
||||
///
|
||||
/// [`WindowAttributes::with_parent_window`]: winit_core::window::WindowAttributes::with_parent_window
|
||||
pub fn with_owner_window(mut self, parent: HWND) -> Self {
|
||||
self.owner = Some(parent);
|
||||
self
|
||||
}
|
||||
fn with_owner_window(self, parent: HWND) -> Self;
|
||||
|
||||
/// Sets a menu on the window to be created.
|
||||
///
|
||||
@@ -530,24 +472,18 @@ impl WindowAttributesWindows {
|
||||
/// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how
|
||||
/// the menus look. If you use this, it is recommended that you combine it with
|
||||
/// `with_theme(Some(Theme::Light))` to avoid a jarring effect.
|
||||
///
|
||||
/// [`CreateMenu`]: windows_sys::Win32::UI::WindowsAndMessaging::CreateMenu"
|
||||
pub fn with_menu(mut self, menu: HMENU) -> Self {
|
||||
self.menu = Some(menu);
|
||||
self
|
||||
}
|
||||
#[cfg_attr(
|
||||
windows_platform,
|
||||
doc = "[`CreateMenu`]: windows_sys::Win32::UI::WindowsAndMessaging::CreateMenu"
|
||||
)]
|
||||
#[cfg_attr(not(windows_platform), doc = "[`CreateMenu`]: #only-available-on-windows")]
|
||||
fn with_menu(self, menu: HMENU) -> Self;
|
||||
|
||||
/// This sets `ICON_BIG`. A good ceiling here is 256x256.
|
||||
pub fn with_taskbar_icon(mut self, taskbar_icon: Option<Icon>) -> Self {
|
||||
self.taskbar_icon = taskbar_icon;
|
||||
self
|
||||
}
|
||||
fn with_taskbar_icon(self, taskbar_icon: Option<Icon>) -> Self;
|
||||
|
||||
/// This sets `WS_EX_NOREDIRECTIONBITMAP`.
|
||||
pub fn with_no_redirection_bitmap(mut self, flag: bool) -> Self {
|
||||
self.no_redirection_bitmap = flag;
|
||||
self
|
||||
}
|
||||
fn with_no_redirection_bitmap(self, flag: bool) -> Self;
|
||||
|
||||
/// Enables or disables drag and drop support (enabled by default). Will interfere with other
|
||||
/// crates that use multi-threaded COM API (`CoInitializeEx` with `COINIT_MULTITHREADED`
|
||||
@@ -555,94 +491,132 @@ impl WindowAttributesWindows {
|
||||
/// attempt to initialize COM API regardless of this option. Currently only fullscreen mode
|
||||
/// does that, but there may be more in the future. If you need COM API with
|
||||
/// `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.
|
||||
pub fn with_drag_and_drop(mut self, flag: bool) -> Self {
|
||||
self.drag_and_drop = flag;
|
||||
self
|
||||
}
|
||||
fn with_drag_and_drop(self, flag: bool) -> Self;
|
||||
|
||||
/// Whether show or hide the window icon in the taskbar.
|
||||
pub fn with_skip_taskbar(mut self, skip: bool) -> Self {
|
||||
self.skip_taskbar = skip;
|
||||
self
|
||||
}
|
||||
fn with_skip_taskbar(self, skip: bool) -> Self;
|
||||
|
||||
/// Customize the window class name.
|
||||
pub fn with_class_name<S: Into<String>>(mut self, class_name: S) -> Self {
|
||||
self.class_name = class_name.into();
|
||||
self
|
||||
}
|
||||
fn with_class_name<S: Into<String>>(self, class_name: S) -> Self;
|
||||
|
||||
/// Shows or hides the background drop shadow for undecorated windows.
|
||||
///
|
||||
/// The shadow is hidden by default.
|
||||
/// Enabling the shadow causes a thin 1px line to appear on the top of the window.
|
||||
pub fn with_undecorated_shadow(mut self, shadow: bool) -> Self {
|
||||
self.decoration_shadow = shadow;
|
||||
self
|
||||
}
|
||||
fn with_undecorated_shadow(self, shadow: bool) -> Self;
|
||||
|
||||
/// Sets system-drawn backdrop type.
|
||||
///
|
||||
/// Requires Windows 11 build 22523+.
|
||||
pub fn with_system_backdrop(mut self, backdrop_type: BackdropType) -> Self {
|
||||
self.backdrop_type = backdrop_type;
|
||||
self
|
||||
}
|
||||
fn with_system_backdrop(self, backdrop_type: BackdropType) -> Self;
|
||||
|
||||
/// This sets or removes `WS_CLIPCHILDREN` style.
|
||||
pub fn with_clip_children(mut self, flag: bool) -> Self {
|
||||
self.clip_children = flag;
|
||||
self
|
||||
}
|
||||
fn with_clip_children(self, flag: bool) -> Self;
|
||||
|
||||
/// Sets the color of the window border.
|
||||
///
|
||||
/// Supported starting with Windows 11 Build 22000.
|
||||
pub fn with_border_color(mut self, color: Option<Color>) -> Self {
|
||||
self.border_color = Some(color.unwrap_or(Color::NONE));
|
||||
self
|
||||
}
|
||||
fn with_border_color(self, color: Option<Color>) -> Self;
|
||||
|
||||
/// Sets the background color of the title bar.
|
||||
///
|
||||
/// Supported starting with Windows 11 Build 22000.
|
||||
pub fn with_title_background_color(mut self, color: Option<Color>) -> Self {
|
||||
self.title_background_color = Some(color.unwrap_or(Color::NONE));
|
||||
self
|
||||
}
|
||||
fn with_title_background_color(self, color: Option<Color>) -> Self;
|
||||
|
||||
/// Sets the color of the window title.
|
||||
///
|
||||
/// Supported starting with Windows 11 Build 22000.
|
||||
pub fn with_title_text_color(mut self, color: Color) -> Self {
|
||||
self.title_text_color = Some(color);
|
||||
self
|
||||
}
|
||||
fn with_title_text_color(self, color: Color) -> Self;
|
||||
|
||||
/// Sets the preferred style of the window corners.
|
||||
///
|
||||
/// Supported starting with Windows 11 Build 22000.
|
||||
pub fn with_corner_preference(mut self, corners: CornerPreference) -> Self {
|
||||
self.corner_preference = Some(corners);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets if the reported [`winit_core::event::WindowEvent::MouseWheel`] event
|
||||
/// should account for scroll speed system settings.
|
||||
///
|
||||
/// The default scroll speed on Windows is 3 lines/characters per scroll,
|
||||
/// this will be 1 if you set it to false.
|
||||
///
|
||||
/// The default is `true`.
|
||||
pub fn with_use_system_scroll_speed(mut self, should_use: bool) -> Self {
|
||||
self.use_system_wheel_speed = should_use;
|
||||
self
|
||||
}
|
||||
fn with_corner_preference(self, corners: CornerPreference) -> Self;
|
||||
}
|
||||
|
||||
impl PlatformWindowAttributes for WindowAttributesWindows {
|
||||
fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
|
||||
Box::from(self.clone())
|
||||
impl WindowAttributesExtWindows for WindowAttributes {
|
||||
#[inline]
|
||||
fn with_owner_window(mut self, parent: HWND) -> Self {
|
||||
self.platform_specific.owner = Some(parent);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_menu(mut self, menu: HMENU) -> Self {
|
||||
self.platform_specific.menu = Some(menu);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_taskbar_icon(mut self, taskbar_icon: Option<Icon>) -> Self {
|
||||
self.platform_specific.taskbar_icon = taskbar_icon;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_no_redirection_bitmap(mut self, flag: bool) -> Self {
|
||||
self.platform_specific.no_redirection_bitmap = flag;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_drag_and_drop(mut self, flag: bool) -> Self {
|
||||
self.platform_specific.drag_and_drop = flag;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_skip_taskbar(mut self, skip: bool) -> Self {
|
||||
self.platform_specific.skip_taskbar = skip;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_class_name<S: Into<String>>(mut self, class_name: S) -> Self {
|
||||
self.platform_specific.class_name = class_name.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_undecorated_shadow(mut self, shadow: bool) -> Self {
|
||||
self.platform_specific.decoration_shadow = shadow;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_system_backdrop(mut self, backdrop_type: BackdropType) -> Self {
|
||||
self.platform_specific.backdrop_type = backdrop_type;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_clip_children(mut self, flag: bool) -> Self {
|
||||
self.platform_specific.clip_children = flag;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_border_color(mut self, color: Option<Color>) -> Self {
|
||||
self.platform_specific.border_color = Some(color.unwrap_or(Color::NONE));
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_title_background_color(mut self, color: Option<Color>) -> Self {
|
||||
self.platform_specific.title_background_color = Some(color.unwrap_or(Color::NONE));
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_title_text_color(mut self, color: Color) -> Self {
|
||||
self.platform_specific.title_text_color = Some(color);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_corner_preference(mut self, corners: CornerPreference) -> Self {
|
||||
self.platform_specific.corner_preference = Some(corners);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -654,10 +628,15 @@ pub trait DeviceIdExtWindows {
|
||||
fn persistent_identifier(&self) -> Option<String>;
|
||||
}
|
||||
|
||||
#[cfg(windows_platform)]
|
||||
impl DeviceIdExtWindows for DeviceId {
|
||||
fn persistent_identifier(&self) -> Option<String> {
|
||||
let raw_id = self.into_raw();
|
||||
if raw_id != 0 { raw_input::get_raw_input_device_name(raw_id as HANDLE) } else { None }
|
||||
if raw_id != 0 {
|
||||
crate::platform_impl::raw_input::get_raw_input_device_name(raw_id as HANDLE)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -754,7 +733,7 @@ impl WinIcon {
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// # use winit::platform::windows::WinIcon;
|
||||
/// # use winit::icon::Icon;
|
||||
/// # use winit::window::Icon;
|
||||
/// assert!(WinIcon::from_resource_name("0", None).is_ok());
|
||||
/// assert!(WinIcon::from_resource(0, None).is_err());
|
||||
/// ```
|
||||
@@ -1,30 +1,10 @@
|
||||
//! # X11
|
||||
|
||||
use dpi::Size;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
|
||||
use winit_core::window::{ActivationToken, PlatformWindowAttributes, Window as CoreWindow};
|
||||
|
||||
pub use crate::event_loop::{ActiveEventLoop, EventLoop};
|
||||
pub use crate::window::Window;
|
||||
|
||||
macro_rules! os_error {
|
||||
($error:expr) => {{ winit_core::error::OsError::new(line!(), file!(), $error) }};
|
||||
}
|
||||
|
||||
mod activation;
|
||||
mod atoms;
|
||||
mod dnd;
|
||||
mod event_loop;
|
||||
mod event_processor;
|
||||
pub mod ffi;
|
||||
mod ime;
|
||||
mod monitor;
|
||||
mod util;
|
||||
mod window;
|
||||
mod xdisplay;
|
||||
mod xsettings;
|
||||
use crate::dpi::Size;
|
||||
use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
|
||||
use crate::window::{Window as CoreWindow, WindowAttributes};
|
||||
|
||||
/// X window type. Maps directly to
|
||||
/// [`_NET_WM_WINDOW_TYPE`](https://specifications.freedesktop.org/wm-spec/wm-spec-1.5.html).
|
||||
@@ -99,21 +79,19 @@ pub type XWindow = u32;
|
||||
#[inline]
|
||||
pub fn register_xlib_error_hook(hook: XlibErrorHook) {
|
||||
// Append new hook.
|
||||
crate::event_loop::XLIB_ERROR_HOOKS.lock().unwrap().push(hook);
|
||||
crate::platform_impl::x11::XLIB_ERROR_HOOKS.lock().unwrap().push(hook);
|
||||
}
|
||||
|
||||
/// Additional methods on [`ActiveEventLoop`] that are specific to X11.
|
||||
///
|
||||
/// [`ActiveEventLoop`]: winit_core::event_loop::ActiveEventLoop
|
||||
pub trait ActiveEventLoopExtX11 {
|
||||
/// True if the event loop uses X11.
|
||||
/// True if the [`ActiveEventLoop`] uses X11.
|
||||
fn is_x11(&self) -> bool;
|
||||
}
|
||||
|
||||
impl ActiveEventLoopExtX11 for dyn CoreActiveEventLoop + '_ {
|
||||
impl ActiveEventLoopExtX11 for dyn ActiveEventLoop + '_ {
|
||||
#[inline]
|
||||
fn is_x11(&self) -> bool {
|
||||
self.cast_ref::<ActiveEventLoop>().is_some()
|
||||
self.cast_ref::<crate::platform_impl::x11::ActiveEventLoop>().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +101,14 @@ pub trait EventLoopExtX11 {
|
||||
fn is_x11(&self) -> bool;
|
||||
}
|
||||
|
||||
/// Additional methods when building event loop that are specific to X11.
|
||||
impl EventLoopExtX11 for EventLoop {
|
||||
#[inline]
|
||||
fn is_x11(&self) -> bool {
|
||||
!self.event_loop.is_wayland()
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`EventLoopBuilder`] that are specific to X11.
|
||||
pub trait EventLoopBuilderExtX11 {
|
||||
/// Force using X11.
|
||||
fn with_x11(&mut self) -> &mut Self;
|
||||
@@ -135,6 +120,20 @@ pub trait EventLoopBuilderExtX11 {
|
||||
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
|
||||
}
|
||||
|
||||
impl EventLoopBuilderExtX11 for EventLoopBuilder {
|
||||
#[inline]
|
||||
fn with_x11(&mut self) -> &mut Self {
|
||||
self.platform_specific.forced_backend = Some(crate::platform_impl::Backend::X);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
|
||||
self.platform_specific.any_thread = any_thread;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional methods on [`Window`] that are specific to X11.
|
||||
///
|
||||
/// [`Window`]: crate::window::Window
|
||||
@@ -142,52 +141,12 @@ pub trait WindowExtX11 {}
|
||||
|
||||
impl WindowExtX11 for dyn CoreWindow {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct ApplicationName {
|
||||
pub(crate) general: String,
|
||||
pub(crate) instance: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct WindowAttributesX11 {
|
||||
pub(crate) name: Option<ApplicationName>,
|
||||
pub(crate) activation_token: Option<ActivationToken>,
|
||||
pub(crate) visual_id: Option<XVisualID>,
|
||||
pub(crate) screen_id: Option<i32>,
|
||||
pub(crate) base_size: Option<Size>,
|
||||
pub(crate) override_redirect: bool,
|
||||
pub(crate) x11_window_types: Vec<WindowType>,
|
||||
|
||||
/// The parent window to embed this window into.
|
||||
pub(crate) embed_window: Option<XWindow>,
|
||||
}
|
||||
|
||||
impl Default for WindowAttributesX11 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: None,
|
||||
activation_token: None,
|
||||
visual_id: None,
|
||||
screen_id: None,
|
||||
base_size: None,
|
||||
override_redirect: false,
|
||||
x11_window_types: vec![WindowType::Normal],
|
||||
embed_window: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowAttributesX11 {
|
||||
/// Additional methods on [`WindowAttributes`] that are specific to X11.
|
||||
pub trait WindowAttributesExtX11 {
|
||||
/// Create this window with a specific X11 visual.
|
||||
pub fn with_x11_visual(mut self, visual_id: XVisualID) -> Self {
|
||||
self.visual_id = Some(visual_id);
|
||||
self
|
||||
}
|
||||
fn with_x11_visual(self, visual_id: XVisualID) -> Self;
|
||||
|
||||
pub fn with_x11_screen(mut self, screen_id: i32) -> Self {
|
||||
self.screen_id = Some(screen_id);
|
||||
self
|
||||
}
|
||||
fn with_x11_screen(self, screen_id: i32) -> Self;
|
||||
|
||||
/// Build window with the given `general` and `instance` names.
|
||||
///
|
||||
@@ -197,39 +156,27 @@ impl WindowAttributesX11 {
|
||||
///
|
||||
/// 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)
|
||||
pub fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
|
||||
self.name = Some(ApplicationName { general: general.into(), instance: instance.into() });
|
||||
self
|
||||
}
|
||||
fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
|
||||
|
||||
/// Build window with override-redirect flag; defaults to false.
|
||||
pub fn with_override_redirect(mut self, override_redirect: bool) -> Self {
|
||||
self.override_redirect = override_redirect;
|
||||
self
|
||||
}
|
||||
fn with_override_redirect(self, override_redirect: bool) -> Self;
|
||||
|
||||
/// Build window with `_NET_WM_WINDOW_TYPE` hints; defaults to `Normal`.
|
||||
pub fn with_x11_window_type(mut self, x11_window_types: Vec<WindowType>) -> Self {
|
||||
self.x11_window_types = x11_window_types;
|
||||
self
|
||||
}
|
||||
fn with_x11_window_type(self, x11_window_type: Vec<WindowType>) -> Self;
|
||||
|
||||
/// Build window with base size hint.
|
||||
///
|
||||
/// ```
|
||||
/// # use winit::dpi::{LogicalSize, PhysicalSize};
|
||||
/// # use winit::window::{Window, WindowAttributes};
|
||||
/// # use winit::platform::x11::WindowAttributesX11;
|
||||
/// # use winit::platform::x11::WindowAttributesExtX11;
|
||||
/// // Specify the size in logical dimensions like this:
|
||||
/// WindowAttributesX11::default().with_base_size(LogicalSize::new(400.0, 200.0));
|
||||
/// WindowAttributes::default().with_base_size(LogicalSize::new(400.0, 200.0));
|
||||
///
|
||||
/// // Or specify the size in physical dimensions like this:
|
||||
/// WindowAttributesX11::default().with_base_size(PhysicalSize::new(400, 200));
|
||||
/// WindowAttributes::default().with_base_size(PhysicalSize::new(400, 200));
|
||||
/// ```
|
||||
pub fn with_base_size<S: Into<Size>>(mut self, base_size: S) -> Self {
|
||||
self.base_size = Some(base_size.into());
|
||||
self
|
||||
}
|
||||
fn with_base_size<S: Into<Size>>(self, base_size: S) -> Self;
|
||||
|
||||
/// Embed this window into another parent window.
|
||||
///
|
||||
@@ -238,28 +185,57 @@ impl WindowAttributesX11 {
|
||||
/// ```no_run
|
||||
/// use winit::window::{Window, WindowAttributes};
|
||||
/// use winit::event_loop::ActiveEventLoop;
|
||||
/// use winit::platform::x11::{XWindow, WindowAttributesX11};
|
||||
/// use winit::platform::x11::{XWindow, WindowAttributesExtX11};
|
||||
/// # fn create_window(event_loop: &dyn ActiveEventLoop) -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// let parent_window_id = std::env::args().nth(1).unwrap().parse::<XWindow>()?;
|
||||
/// let window_x11_attributes = WindowAttributesX11::default().with_embed_parent_window(parent_window_id);
|
||||
/// let window_attributes = WindowAttributes::default().with_platform_attributes(Box::new(window_x11_attributes));
|
||||
/// let window_attributes = WindowAttributes::default().with_embed_parent_window(parent_window_id);
|
||||
/// let window = event_loop.create_window(window_attributes)?;
|
||||
/// # Ok(()) }
|
||||
/// ```
|
||||
pub fn with_embed_parent_window(mut self, parent_window_id: XWindow) -> Self {
|
||||
self.embed_window = Some(parent_window_id);
|
||||
fn with_embed_parent_window(self, parent_window_id: XWindow) -> Self;
|
||||
}
|
||||
|
||||
impl WindowAttributesExtX11 for WindowAttributes {
|
||||
#[inline]
|
||||
fn with_x11_visual(mut self, visual_id: XVisualID) -> Self {
|
||||
self.platform_specific.x11.visual_id = Some(visual_id);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_activation_token(mut self, token: ActivationToken) -> Self {
|
||||
self.activation_token = Some(token);
|
||||
fn with_x11_screen(mut self, screen_id: i32) -> Self {
|
||||
self.platform_specific.x11.screen_id = Some(screen_id);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
|
||||
self.platform_specific.name =
|
||||
Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_override_redirect(mut self, override_redirect: bool) -> Self {
|
||||
self.platform_specific.x11.override_redirect = override_redirect;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_x11_window_type(mut self, x11_window_types: Vec<WindowType>) -> Self {
|
||||
self.platform_specific.x11.x11_window_types = x11_window_types;
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_base_size<S: Into<Size>>(mut self, base_size: S) -> Self {
|
||||
self.platform_specific.x11.base_size = Some(base_size.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn with_embed_parent_window(mut self, parent_window_id: XWindow) -> Self {
|
||||
self.platform_specific.x11.embed_window = Some(parent_window_id);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl PlatformWindowAttributes for WindowAttributesX11 {
|
||||
fn box_clone(&self) -> Box<dyn PlatformWindowAttributes> {
|
||||
Box::from(self.clone())
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
use android_activity::AndroidApp;
|
||||
use android_activity::input::{KeyAction, KeyEvent, KeyMapChar, Keycode};
|
||||
use winit_core::keyboard::{
|
||||
Key, KeyCode, KeyLocation, NamedKey, NativeKey, NativeKeyCode, PhysicalKey,
|
||||
};
|
||||
use android_activity::AndroidApp;
|
||||
|
||||
use crate::keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKey, NativeKeyCode, PhysicalKey};
|
||||
|
||||
pub fn to_physical_key(keycode: Keycode) -> PhysicalKey {
|
||||
PhysicalKey::Code(match keycode {
|
||||
@@ -109,7 +108,6 @@ pub fn to_physical_key(keycode: Keycode) -> PhysicalKey {
|
||||
Keycode::MediaStop => KeyCode::MediaStop,
|
||||
Keycode::MediaNext => KeyCode::MediaTrackNext,
|
||||
Keycode::MediaPrevious => KeyCode::MediaTrackPrevious,
|
||||
Keycode::MediaEject => KeyCode::Eject,
|
||||
|
||||
Keycode::Plus => KeyCode::Equal,
|
||||
Keycode::Minus => KeyCode::Minus,
|
||||
@@ -132,11 +130,7 @@ pub fn to_physical_key(keycode: Keycode) -> PhysicalKey {
|
||||
// These are exactly the same
|
||||
Keycode::ScrollLock => KeyCode::ScrollLock,
|
||||
|
||||
Keycode::Eisu => KeyCode::Lang2,
|
||||
Keycode::Muhenkan => KeyCode::NonConvert,
|
||||
Keycode::Henkan => KeyCode::Convert,
|
||||
Keycode::Yen => KeyCode::IntlYen,
|
||||
Keycode::Ro => KeyCode::IntlRo,
|
||||
Keycode::Kana => KeyCode::Lang1,
|
||||
Keycode::KatakanaHiragana => KeyCode::KanaMode,
|
||||
|
||||
@@ -159,14 +153,6 @@ pub fn to_physical_key(keycode: Keycode) -> PhysicalKey {
|
||||
Keycode::Sleep => KeyCode::Sleep, // what about SoftSleep?
|
||||
Keycode::Wakeup => KeyCode::WakeUp,
|
||||
|
||||
Keycode::CapsLock => KeyCode::CapsLock,
|
||||
Keycode::Help => KeyCode::Help,
|
||||
|
||||
Keycode::Back => KeyCode::BrowserBack,
|
||||
Keycode::Forward => KeyCode::BrowserForward,
|
||||
Keycode::Refresh => KeyCode::BrowserRefresh,
|
||||
Keycode::Search => KeyCode::BrowserSearch,
|
||||
|
||||
keycode => return PhysicalKey::Unidentified(NativeKeyCode::Android(keycode.into())),
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
use std::cell::Cell;
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
@@ -9,26 +8,26 @@ use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction};
|
||||
use android_activity::{
|
||||
AndroidApp, AndroidAppWaker, ConfigurationRef, InputStatus, MainEvent, Rect,
|
||||
};
|
||||
use dpi::{PhysicalInsets, PhysicalPosition, PhysicalSize, Position, Size};
|
||||
use tracing::{debug, trace, warn};
|
||||
use winit_core::application::ApplicationHandler;
|
||||
use winit_core::cursor::{Cursor, CustomCursor, CustomCursorSource};
|
||||
use winit_core::error::{EventLoopError, NotSupportedError, RequestError};
|
||||
use winit_core::event::{self, DeviceId, FingerId, Force, StartCause, SurfaceSizeWriter};
|
||||
use winit_core::event_loop::pump_events::PumpStatus;
|
||||
use winit_core::event_loop::{
|
||||
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::cursor::Cursor;
|
||||
use crate::dpi::{PhysicalInsets, PhysicalPosition, PhysicalSize, Position, Size};
|
||||
use crate::error::{EventLoopError, NotSupportedError, RequestError};
|
||||
use crate::event::{self, DeviceId, FingerId, Force, StartCause, SurfaceSizeWriter};
|
||||
use crate::event_loop::{
|
||||
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
|
||||
EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider,
|
||||
OwnedDisplayHandle as CoreOwnedDisplayHandle,
|
||||
};
|
||||
use winit_core::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle};
|
||||
use winit_core::window::{
|
||||
self, CursorGrabMode, ImeCapabilities, ImePurpose, ImeRequest, ImeRequestError,
|
||||
ResizeDirection, Theme, Window as CoreWindow, WindowAttributes, WindowButtons, WindowId,
|
||||
WindowLevel,
|
||||
use crate::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle};
|
||||
use crate::platform::pump_events::PumpStatus;
|
||||
use crate::window::{
|
||||
self, CursorGrabMode, CustomCursor, CustomCursorSource, ImePurpose, ResizeDirection, Theme,
|
||||
Window as CoreWindow, WindowAttributes, WindowButtons, WindowId, WindowLevel,
|
||||
};
|
||||
|
||||
use crate::keycodes;
|
||||
mod keycodes;
|
||||
|
||||
static HAS_FOCUS: AtomicBool = AtomicBool::new(true);
|
||||
|
||||
@@ -44,7 +43,7 @@ struct SharedFlagSetter {
|
||||
flag: Arc<AtomicBool>,
|
||||
}
|
||||
impl SharedFlagSetter {
|
||||
fn set(&self) -> bool {
|
||||
pub fn set(&self) -> bool {
|
||||
self.flag.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed).is_ok()
|
||||
}
|
||||
}
|
||||
@@ -59,21 +58,21 @@ struct SharedFlag {
|
||||
// we just need to know at the start of a main loop iteration if a redraw
|
||||
// was queued and be able to read and clear the state atomically)
|
||||
impl SharedFlag {
|
||||
fn new() -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self { flag: Arc::new(AtomicBool::new(false)) }
|
||||
}
|
||||
|
||||
fn setter(&self) -> SharedFlagSetter {
|
||||
pub fn setter(&self) -> SharedFlagSetter {
|
||||
SharedFlagSetter { flag: self.flag.clone() }
|
||||
}
|
||||
|
||||
fn get_and_reset(&self) -> bool {
|
||||
pub fn get_and_reset(&self) -> bool {
|
||||
self.flag.swap(false, std::sync::atomic::Ordering::AcqRel)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RedrawRequester {
|
||||
pub struct RedrawRequester {
|
||||
flag: SharedFlagSetter,
|
||||
waker: AndroidAppWaker,
|
||||
}
|
||||
@@ -89,7 +88,7 @@ impl RedrawRequester {
|
||||
RedrawRequester { flag: flag.setter(), waker }
|
||||
}
|
||||
|
||||
fn request_redraw(&self) {
|
||||
pub fn request_redraw(&self) {
|
||||
if self.flag.set() {
|
||||
// Only explicitly try to wake up the main loop when the flag
|
||||
// value changes
|
||||
@@ -100,7 +99,7 @@ impl RedrawRequester {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EventLoop {
|
||||
pub android_app: AndroidApp,
|
||||
pub(crate) android_app: AndroidApp,
|
||||
window_target: ActiveEventLoop,
|
||||
redraw_flag: SharedFlag,
|
||||
loop_running: bool, // Dispatched `NewEvents<Init>`
|
||||
@@ -113,9 +112,9 @@ pub struct EventLoop {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PlatformSpecificEventLoopAttributes {
|
||||
pub android_app: Option<AndroidApp>,
|
||||
pub ignore_volume_keys: bool,
|
||||
pub(crate) struct PlatformSpecificEventLoopAttributes {
|
||||
pub(crate) android_app: Option<AndroidApp>,
|
||||
pub(crate) ignore_volume_keys: bool,
|
||||
}
|
||||
|
||||
impl Default for PlatformSpecificEventLoopAttributes {
|
||||
@@ -128,13 +127,9 @@ impl Default for PlatformSpecificEventLoopAttributes {
|
||||
const GLOBAL_WINDOW: WindowId = WindowId::from_raw(0);
|
||||
|
||||
impl EventLoop {
|
||||
pub fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
|
||||
static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false);
|
||||
if EVENT_LOOP_CREATED.swap(true, Ordering::Relaxed) {
|
||||
// For better cross-platformness.
|
||||
return Err(EventLoopError::RecreationAttempt);
|
||||
}
|
||||
|
||||
pub(crate) fn new(
|
||||
attributes: &PlatformSpecificEventLoopAttributes,
|
||||
) -> Result<Self, EventLoopError> {
|
||||
let android_app = attributes.android_app.as_ref().expect(
|
||||
"An `AndroidApp` as passed to android_main() is required to create an `EventLoop` on \
|
||||
Android",
|
||||
@@ -164,7 +159,7 @@ impl EventLoop {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn window_target(&self) -> &dyn RootActiveEventLoop {
|
||||
pub(crate) fn window_target(&self) -> &dyn RootActiveEventLoop {
|
||||
&self.window_target
|
||||
}
|
||||
|
||||
@@ -473,23 +468,16 @@ impl EventLoop {
|
||||
&mut self.combining_accent,
|
||||
);
|
||||
|
||||
let logical_key = keycodes::to_logical(key_char, keycode);
|
||||
let text = if state == event::ElementState::Pressed {
|
||||
logical_key.to_text().map(smol_str::SmolStr::new)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let event = event::WindowEvent::KeyboardInput {
|
||||
device_id: Some(DeviceId::from_raw(key.device_id() as i64)),
|
||||
event: event::KeyEvent {
|
||||
state,
|
||||
physical_key: keycodes::to_physical_key(keycode),
|
||||
logical_key,
|
||||
logical_key: keycodes::to_logical(key_char, keycode),
|
||||
location: keycodes::to_location(keycode),
|
||||
repeat: key.repeat_count() > 0,
|
||||
text: text.clone(),
|
||||
text_with_all_modifiers: text,
|
||||
text: None,
|
||||
text_with_all_modifiers: None,
|
||||
key_without_modifiers: keycodes::to_logical(key_char, keycode),
|
||||
},
|
||||
is_synthetic: false,
|
||||
@@ -507,6 +495,10 @@ impl EventLoop {
|
||||
input_status
|
||||
}
|
||||
|
||||
pub fn run_app<A: ApplicationHandler>(mut self, app: A) -> Result<(), EventLoopError> {
|
||||
self.run_app_on_demand(app)
|
||||
}
|
||||
|
||||
pub fn run_app_on_demand<A: ApplicationHandler>(
|
||||
&mut self,
|
||||
mut app: A,
|
||||
@@ -760,9 +752,8 @@ impl rwh_06::HasDisplayHandle for OwnedDisplayHandle {
|
||||
pub struct PlatformSpecificWindowAttributes;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Window {
|
||||
pub(crate) struct Window {
|
||||
app: AndroidApp,
|
||||
ime_capabilities: Mutex<Option<ImeCapabilities>>,
|
||||
redraw_requester: RedrawRequester,
|
||||
}
|
||||
|
||||
@@ -773,18 +764,14 @@ impl Window {
|
||||
) -> Result<Self, RequestError> {
|
||||
// FIXME this ignores requested window attributes
|
||||
|
||||
Ok(Self {
|
||||
app: el.app.clone(),
|
||||
ime_capabilities: Default::default(),
|
||||
redraw_requester: el.redraw_requester.clone(),
|
||||
})
|
||||
Ok(Self { app: el.app.clone(), redraw_requester: el.redraw_requester.clone() })
|
||||
}
|
||||
|
||||
pub(crate) fn config(&self) -> ConfigurationRef {
|
||||
pub fn config(&self) -> ConfigurationRef {
|
||||
self.app.config()
|
||||
}
|
||||
|
||||
pub(crate) fn content_rect(&self) -> Rect {
|
||||
pub fn content_rect(&self) -> Rect {
|
||||
self.app.content_rect()
|
||||
}
|
||||
|
||||
@@ -941,37 +928,16 @@ impl CoreWindow for Window {
|
||||
|
||||
fn set_window_level(&self, _level: WindowLevel) {}
|
||||
|
||||
fn set_window_icon(&self, _window_icon: Option<winit_core::icon::Icon>) {}
|
||||
fn set_window_icon(&self, _window_icon: Option<crate::icon::Icon>) {}
|
||||
|
||||
fn set_ime_cursor_area(&self, _position: Position, _size: Size) {}
|
||||
|
||||
fn request_ime_update(&self, request: ImeRequest) -> Result<(), ImeRequestError> {
|
||||
let mut current_caps = self.ime_capabilities.lock().unwrap();
|
||||
match request {
|
||||
ImeRequest::Enable(enable) => {
|
||||
let (capabilities, _) = enable.into_raw();
|
||||
if current_caps.is_some() {
|
||||
return Err(ImeRequestError::AlreadyEnabled);
|
||||
}
|
||||
*current_caps = Some(capabilities);
|
||||
self.app.show_soft_input(true);
|
||||
},
|
||||
ImeRequest::Update(_) => {
|
||||
if current_caps.is_none() {
|
||||
return Err(ImeRequestError::NotEnabled);
|
||||
}
|
||||
},
|
||||
ImeRequest::Disable => {
|
||||
*current_caps = None;
|
||||
self.app.hide_soft_input(true);
|
||||
},
|
||||
fn set_ime_allowed(&self, allowed: bool) {
|
||||
if allowed {
|
||||
self.app.show_soft_input(true);
|
||||
} else {
|
||||
self.app.hide_soft_input(true);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ime_capabilities(&self) -> Option<ImeCapabilities> {
|
||||
*self.ime_capabilities.lock().unwrap()
|
||||
}
|
||||
|
||||
fn set_ime_purpose(&self, _purpose: ImePurpose) {}
|
||||
@@ -1034,6 +1000,16 @@ impl CoreWindow for Window {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct OsError;
|
||||
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
impl Display for OsError {
|
||||
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
write!(fmt, "Android OS Error")
|
||||
}
|
||||
}
|
||||
|
||||
fn screen_size(app: &AndroidApp) -> PhysicalSize<u32> {
|
||||
if let Some(native_window) = app.native_window() {
|
||||
PhysicalSize::new(native_window.width() as _, native_window.height() as _)
|
||||
@@ -1,18 +1,17 @@
|
||||
#![allow(clippy::unnecessary_cast)]
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::mem;
|
||||
use std::rc::Rc;
|
||||
use std::{mem, ptr};
|
||||
|
||||
use dispatch2::MainThreadBound;
|
||||
use objc2::runtime::{Imp, Sel};
|
||||
use objc2::sel;
|
||||
use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType};
|
||||
use objc2_foundation::MainThreadMarker;
|
||||
use tracing::trace_span;
|
||||
use winit_core::event::{DeviceEvent, ElementState};
|
||||
|
||||
use super::app_state::AppState;
|
||||
use crate::event::{DeviceEvent, ElementState};
|
||||
|
||||
type SendEvent = extern "C-unwind" fn(&NSApplication, Sel, &NSEvent);
|
||||
|
||||
@@ -22,10 +21,6 @@ static ORIGINAL: MainThreadBound<Cell<Option<SendEvent>>> = {
|
||||
};
|
||||
|
||||
extern "C-unwind" fn send_event(app: &NSApplication, sel: Sel, event: &NSEvent) {
|
||||
// This can be a bit noisy, since `event` is fairly large. Note that you can use
|
||||
// `RUST_LOG='trace,winit_appkit::app=warn'` if you're debugging and want TRACE-level logs but
|
||||
// not this.
|
||||
let _entered = trace_span!("sendEvent:", ?event).entered();
|
||||
let mtm = MainThreadMarker::from(app);
|
||||
|
||||
// Normally, holding Cmd + any key never sends us a `keyUp` event for that key.
|
||||
@@ -35,8 +30,8 @@ extern "C-unwind" fn send_event(app: &NSApplication, sel: Sel, event: &NSEvent)
|
||||
// For posterity, there are some undocumented event types
|
||||
// (https://github.com/servo/cocoa-rs/issues/155)
|
||||
// but that doesn't really matter here.
|
||||
let event_type = event.r#type();
|
||||
let modifier_flags = event.modifierFlags();
|
||||
let event_type = unsafe { event.r#type() };
|
||||
let modifier_flags = unsafe { event.modifierFlags() };
|
||||
if event_type == NSEventType::KeyUp && modifier_flags.contains(NSEventModifierFlags::Command) {
|
||||
if let Some(key_window) = app.keyWindow() {
|
||||
key_window.sendEvent(event);
|
||||
@@ -80,7 +75,9 @@ pub(crate) fn override_send_event(global_app: &NSApplication) {
|
||||
let overridden = unsafe { mem::transmute::<SendEvent, Imp>(send_event) };
|
||||
|
||||
// If we've already overridden the method, don't do anything.
|
||||
if ptr::fn_addr_eq(overridden, method.implementation()) {
|
||||
// FIXME(madsmtm): Use `std::ptr::fn_addr_eq` (Rust 1.85) once available in MSRV.
|
||||
#[allow(unknown_lints, unpredictable_function_pointer_comparisons)]
|
||||
if overridden == method.implementation() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -101,15 +98,15 @@ pub(crate) fn override_send_event(global_app: &NSApplication) {
|
||||
}
|
||||
|
||||
fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
|
||||
let event_type = event.r#type();
|
||||
let event_type = unsafe { event.r#type() };
|
||||
#[allow(non_upper_case_globals)]
|
||||
match event_type {
|
||||
NSEventType::MouseMoved
|
||||
| NSEventType::LeftMouseDragged
|
||||
| NSEventType::OtherMouseDragged
|
||||
| NSEventType::RightMouseDragged => {
|
||||
let delta_x = event.deltaX() as f64;
|
||||
let delta_y = event.deltaY() as f64;
|
||||
let delta_x = unsafe { event.deltaX() } as f64;
|
||||
let delta_y = unsafe { event.deltaY() } as f64;
|
||||
|
||||
if delta_x != 0.0 || delta_y != 0.0 {
|
||||
app_state.maybe_queue_with_handler(move |app, event_loop| {
|
||||
@@ -120,7 +117,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
|
||||
}
|
||||
},
|
||||
NSEventType::LeftMouseDown | NSEventType::RightMouseDown | NSEventType::OtherMouseDown => {
|
||||
let button = event.buttonNumber() as u32;
|
||||
let button = unsafe { event.buttonNumber() } as u32;
|
||||
app_state.maybe_queue_with_handler(move |app, event_loop| {
|
||||
app.device_event(event_loop, None, DeviceEvent::Button {
|
||||
button,
|
||||
@@ -129,7 +126,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
|
||||
});
|
||||
},
|
||||
NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => {
|
||||
let button = event.buttonNumber() as u32;
|
||||
let button = unsafe { event.buttonNumber() } as u32;
|
||||
app_state.maybe_queue_with_handler(move |app, event_loop| {
|
||||
app.device_event(event_loop, None, DeviceEvent::Button {
|
||||
button,
|
||||
@@ -144,7 +141,7 @@ fn maybe_dispatch_device_event(app_state: &Rc<AppState>, event: &NSEvent) {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use objc2::rc::Retained;
|
||||
use objc2::{ClassType, define_class, msg_send};
|
||||
use objc2::{define_class, msg_send, ClassType};
|
||||
use objc2_app_kit::NSResponder;
|
||||
use objc2_foundation::NSObject;
|
||||
|
||||
@@ -156,14 +153,16 @@ mod tests {
|
||||
let Some(mtm) = MainThreadMarker::new() else { return };
|
||||
|
||||
// Create a new application, without making it the shared application.
|
||||
let app = NSApplication::new(mtm);
|
||||
let app = unsafe { NSApplication::new(mtm) };
|
||||
override_send_event(&app);
|
||||
// Test calling twice works.
|
||||
override_send_event(&app);
|
||||
|
||||
// FIXME(madsmtm): Can't test this yet, need some way to mock AppState.
|
||||
// let event = super::super::event::dummy_event().unwrap();
|
||||
// app.sendEvent(&event)
|
||||
// unsafe {
|
||||
// let event = super::super::event::dummy_event().unwrap();
|
||||
// app.sendEvent(&event)
|
||||
// }
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cell::{Cell, OnceCell, RefCell};
|
||||
use std::mem;
|
||||
use std::rc::Rc;
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -8,16 +8,16 @@ use dispatch2::MainThreadBound;
|
||||
use objc2::MainThreadMarker;
|
||||
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSRunningApplication};
|
||||
use objc2_foundation::NSNotification;
|
||||
use winit_common::core_foundation::{EventLoopProxy, MainRunLoop};
|
||||
use winit_common::event_handler::EventHandler;
|
||||
use winit_core::application::ApplicationHandler;
|
||||
use winit_core::event::{StartCause, WindowEvent};
|
||||
use winit_core::event_loop::ControlFlow;
|
||||
use winit_core::window::WindowId;
|
||||
|
||||
use super::event_loop::{ActiveEventLoop, notify_windows_of_exit, stop_app_immediately};
|
||||
use super::super::event_handler::EventHandler;
|
||||
use super::super::event_loop_proxy::EventLoopProxy;
|
||||
use super::event_loop::{notify_windows_of_exit, stop_app_immediately, ActiveEventLoop, PanicInfo};
|
||||
use super::menu;
|
||||
use super::observer::EventLoopWaker;
|
||||
use super::observer::{EventLoopWaker, RunLoop};
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::event::{StartCause, WindowEvent};
|
||||
use crate::event_loop::ControlFlow;
|
||||
use crate::window::WindowId;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct AppState {
|
||||
@@ -25,7 +25,7 @@ pub(super) struct AppState {
|
||||
activation_policy: Option<NSApplicationActivationPolicy>,
|
||||
default_menu: bool,
|
||||
activate_ignoring_other_apps: bool,
|
||||
run_loop: MainRunLoop,
|
||||
run_loop: RunLoop,
|
||||
event_loop_proxy: Arc<EventLoopProxy>,
|
||||
event_handler: EventHandler,
|
||||
stop_on_launch: Cell<bool>,
|
||||
@@ -58,7 +58,7 @@ impl AppState {
|
||||
activation_policy: Option<NSApplicationActivationPolicy>,
|
||||
default_menu: bool,
|
||||
activate_ignoring_other_apps: bool,
|
||||
) -> Option<Rc<Self>> {
|
||||
) -> Rc<Self> {
|
||||
let event_loop_proxy = Arc::new(EventLoopProxy::new(mtm, move || {
|
||||
Self::get(mtm).with_handler(|app, event_loop| app.proxy_wake_up(event_loop));
|
||||
}));
|
||||
@@ -68,7 +68,7 @@ impl AppState {
|
||||
activation_policy,
|
||||
default_menu,
|
||||
activate_ignoring_other_apps,
|
||||
run_loop: MainRunLoop::get(mtm),
|
||||
run_loop: RunLoop::main(mtm),
|
||||
event_loop_proxy,
|
||||
event_handler: EventHandler::new(),
|
||||
stop_on_launch: Cell::new(false),
|
||||
@@ -85,7 +85,8 @@ impl AppState {
|
||||
pending_redraw: RefCell::new(vec![]),
|
||||
});
|
||||
|
||||
GLOBAL.get(mtm).set(this.clone()).ok().and(Some(this))
|
||||
GLOBAL.get(mtm).set(this.clone()).expect("application state can only be set once");
|
||||
this
|
||||
}
|
||||
|
||||
pub fn get(mtm: MainThreadMarker) -> Rc<Self> {
|
||||
@@ -99,6 +100,7 @@ impl AppState {
|
||||
// NOTE: This notification will, globally, only be emitted once,
|
||||
// no matter how many `EventLoop`s the user creates.
|
||||
pub fn did_finish_launching(self: &Rc<Self>, _notification: &NSNotification) {
|
||||
trace_scope!("NSApplicationDidFinishLaunchingNotification");
|
||||
self.is_launched.set(true);
|
||||
|
||||
let app = NSApplication::sharedApplication(self.mtm);
|
||||
@@ -116,7 +118,7 @@ impl AppState {
|
||||
// - https://github.com/rust-windowing/winit/issues/261
|
||||
// - https://github.com/rust-windowing/winit/issues/3958
|
||||
let is_bundled =
|
||||
NSRunningApplication::currentApplication().bundleIdentifier().is_some();
|
||||
unsafe { NSRunningApplication::currentApplication().bundleIdentifier().is_some() };
|
||||
if !is_bundled {
|
||||
app.setActivationPolicy(NSApplicationActivationPolicy::Regular);
|
||||
}
|
||||
@@ -153,6 +155,7 @@ impl AppState {
|
||||
}
|
||||
|
||||
pub fn will_terminate(self: &Rc<Self>, _notification: &NSNotification) {
|
||||
trace_scope!("NSApplicationWillTerminateNotification");
|
||||
let app = NSApplication::sharedApplication(self.mtm);
|
||||
notify_windows_of_exit(&app);
|
||||
self.event_handler.terminate();
|
||||
@@ -263,7 +266,7 @@ impl AppState {
|
||||
if !pending_redraw.contains(&window_id) {
|
||||
pending_redraw.push(window_id);
|
||||
}
|
||||
self.run_loop.wake_up();
|
||||
self.run_loop.wakeup();
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
@@ -306,10 +309,13 @@ impl AppState {
|
||||
}
|
||||
|
||||
// Called by RunLoopObserver after finishing waiting for new events
|
||||
pub fn wakeup(self: &Rc<Self>) {
|
||||
pub fn wakeup(self: &Rc<Self>, panic_info: Weak<PanicInfo>) {
|
||||
let panic_info = panic_info
|
||||
.upgrade()
|
||||
.expect("The panic info must exist here. This failure indicates a developer error.");
|
||||
|
||||
// Return when in event handler due to https://github.com/rust-windowing/winit/issues/1779
|
||||
// (we have registered to observe all modes, including modal event loops).
|
||||
if !self.event_handler.ready() || !self.is_running() {
|
||||
if panic_info.is_panicking() || !self.event_handler.ready() || !self.is_running() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -335,10 +341,15 @@ impl AppState {
|
||||
}
|
||||
|
||||
// Called by RunLoopObserver before waiting for new events
|
||||
pub fn cleared(self: &Rc<Self>) {
|
||||
pub fn cleared(self: &Rc<Self>, panic_info: Weak<PanicInfo>) {
|
||||
let panic_info = panic_info
|
||||
.upgrade()
|
||||
.expect("The panic info must exist here. This failure indicates a developer error.");
|
||||
|
||||
// Return when in event handler due to https://github.com/rust-windowing/winit/issues/1779
|
||||
// (we have registered to observe all modes, including modal event loops).
|
||||
if !self.event_handler.ready() || !self.is_running() {
|
||||
// XXX: how does it make sense that `event_handler.ready()` can ever return `false` here if
|
||||
// we're about to return to the `CFRunLoop` to poll for new events?
|
||||
if panic_info.is_panicking() || !self.event_handler.ready() || !self.is_running() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4,16 +4,15 @@ use std::sync::OnceLock;
|
||||
|
||||
use objc2::rc::Retained;
|
||||
use objc2::runtime::Sel;
|
||||
use objc2::{AllocAnyThread, ClassType, available, msg_send, sel};
|
||||
use objc2_app_kit::{
|
||||
NSBitmapImageRep, NSCursor, NSCursorFrameResizeDirections, NSCursorFrameResizePosition,
|
||||
NSDeviceRGBColorSpace, NSImage,
|
||||
};
|
||||
use objc2::{available, msg_send, sel, AllocAnyThread, ClassType};
|
||||
use objc2_app_kit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSImage};
|
||||
use objc2_foundation::{
|
||||
NSData, NSDictionary, NSNumber, NSObject, NSPoint, NSSize, NSString, ns_string,
|
||||
ns_string, NSData, NSDictionary, NSNumber, NSObject, NSPoint, NSSize, NSString,
|
||||
};
|
||||
use winit_core::cursor::{CursorIcon, CursorImage, CustomCursorProvider, CustomCursorSource};
|
||||
use winit_core::error::{NotSupportedError, RequestError};
|
||||
|
||||
use crate::cursor::{CursorImage, CustomCursorProvider, CustomCursorSource};
|
||||
use crate::error::{NotSupportedError, RequestError};
|
||||
use crate::window::CursorIcon;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct CustomCursor(pub(crate) Retained<NSCursor>);
|
||||
@@ -34,7 +33,7 @@ impl CustomCursor {
|
||||
let cursor = match cursor {
|
||||
CustomCursorSource::Image(cursor_image) => cursor_image,
|
||||
CustomCursorSource::Animation { .. } | CustomCursorSource::Url { .. } => {
|
||||
return Err(NotSupportedError::new("unsupported cursor kind").into());
|
||||
return Err(NotSupportedError::new("unsupported cursor kind").into())
|
||||
},
|
||||
};
|
||||
|
||||
@@ -43,8 +42,8 @@ impl CustomCursor {
|
||||
}
|
||||
|
||||
pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Result<Retained<NSCursor>, RequestError> {
|
||||
let width = cursor.width();
|
||||
let height = cursor.height();
|
||||
let width = cursor.width;
|
||||
let height = cursor.height;
|
||||
|
||||
let bitmap = unsafe {
|
||||
NSBitmapImageRep::initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(
|
||||
@@ -61,14 +60,15 @@ pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Result<Retained<NSCurso
|
||||
32,
|
||||
)
|
||||
}.ok_or_else(|| os_error!("parent view should be installed in a window"))?;
|
||||
let bitmap_data =
|
||||
unsafe { slice::from_raw_parts_mut(bitmap.bitmapData(), cursor.buffer().len()) };
|
||||
bitmap_data.copy_from_slice(cursor.buffer());
|
||||
let bitmap_data = unsafe { slice::from_raw_parts_mut(bitmap.bitmapData(), cursor.rgba.len()) };
|
||||
bitmap_data.copy_from_slice(&cursor.rgba);
|
||||
|
||||
let image = NSImage::initWithSize(NSImage::alloc(), NSSize::new(width.into(), height.into()));
|
||||
image.addRepresentation(&bitmap);
|
||||
let image = unsafe {
|
||||
NSImage::initWithSize(NSImage::alloc(), NSSize::new(width.into(), height.into()))
|
||||
};
|
||||
unsafe { image.addRepresentation(&bitmap) };
|
||||
|
||||
let hotspot = NSPoint::new(cursor.hotspot_x() as f64, cursor.hotspot_y() as f64);
|
||||
let hotspot = NSPoint::new(cursor.hotspot_x as f64, cursor.hotspot_y as f64);
|
||||
|
||||
Ok(NSCursor::initWithImage_hotSpot(NSCursor::alloc(), &image, hotspot))
|
||||
}
|
||||
@@ -141,9 +141,12 @@ unsafe fn load_webkit_cursor(name: &NSString) -> Retained<NSCursor> {
|
||||
|
||||
// TODO: Handle PLists better
|
||||
let info_path = cursor_path.stringByAppendingPathComponent(ns_string!("info.plist"));
|
||||
#[allow(deprecated)]
|
||||
let info: Retained<NSDictionary<NSObject, NSObject>> =
|
||||
unsafe { NSDictionary::dictionaryWithContentsOfFile(&info_path) }.unwrap();
|
||||
let info: Retained<NSDictionary<NSObject, NSObject>> = unsafe {
|
||||
msg_send![
|
||||
<NSDictionary<NSObject, NSObject>>::class(),
|
||||
dictionaryWithContentsOfFile: &*info_path,
|
||||
]
|
||||
};
|
||||
let mut x = 0.0;
|
||||
if let Some(n) = info.objectForKey(ns_string!("hotx")) {
|
||||
if let Ok(n) = n.downcast::<NSNumber>() {
|
||||
@@ -207,155 +210,23 @@ pub(crate) fn cursor_from_icon(icon: CursorIcon) -> Retained<NSCursor> {
|
||||
CursorIcon::NotAllowed | CursorIcon::NoDrop => NSCursor::operationNotAllowedCursor(),
|
||||
CursorIcon::ContextMenu => NSCursor::contextualMenuCursor(),
|
||||
CursorIcon::Crosshair => NSCursor::crosshairCursor(),
|
||||
CursorIcon::EResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::Right,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
NSCursor::resizeRightCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::NResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::Top,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
NSCursor::resizeUpCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::WResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::Left,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
NSCursor::resizeLeftCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::SResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::Bottom,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
NSCursor::resizeDownCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::EwResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::Right,
|
||||
NSCursorFrameResizeDirections::All,
|
||||
)
|
||||
} else {
|
||||
NSCursor::resizeLeftRightCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::NsResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::Top,
|
||||
NSCursorFrameResizeDirections::All,
|
||||
)
|
||||
} else {
|
||||
NSCursor::resizeUpDownCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::NeResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::TopRight,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
_windowResizeNorthEastCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::NwResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::TopLeft,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
_windowResizeNorthWestCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::SeResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::BottomRight,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
_windowResizeSouthEastCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::SwResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::BottomLeft,
|
||||
NSCursorFrameResizeDirections::Outward,
|
||||
)
|
||||
} else {
|
||||
_windowResizeSouthWestCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::NeswResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::TopRight,
|
||||
NSCursorFrameResizeDirections::All,
|
||||
)
|
||||
} else {
|
||||
_windowResizeNorthEastSouthWestCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::NwseResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::frameResizeCursorFromPosition_inDirections(
|
||||
NSCursorFrameResizePosition::TopLeft,
|
||||
NSCursorFrameResizeDirections::All,
|
||||
)
|
||||
} else {
|
||||
_windowResizeNorthWestSouthEastCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::ColResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::columnResizeCursor()
|
||||
} else {
|
||||
NSCursor::resizeLeftRightCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::RowResize => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::rowResizeCursor()
|
||||
} else {
|
||||
NSCursor::resizeUpDownCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::ZoomIn => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::zoomInCursor()
|
||||
} else {
|
||||
_zoomInCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::ZoomOut => {
|
||||
if available!(macos = 15.0) {
|
||||
NSCursor::zoomOutCursor()
|
||||
} else {
|
||||
_zoomOutCursor()
|
||||
}
|
||||
},
|
||||
CursorIcon::EResize => NSCursor::resizeRightCursor(),
|
||||
CursorIcon::NResize => NSCursor::resizeUpCursor(),
|
||||
CursorIcon::WResize => NSCursor::resizeLeftCursor(),
|
||||
CursorIcon::SResize => NSCursor::resizeDownCursor(),
|
||||
CursorIcon::EwResize | CursorIcon::ColResize => NSCursor::resizeLeftRightCursor(),
|
||||
CursorIcon::NsResize | CursorIcon::RowResize => NSCursor::resizeUpDownCursor(),
|
||||
CursorIcon::Help => _helpCursor(),
|
||||
CursorIcon::ZoomIn if available!(macos = 15.0) => unsafe { NSCursor::zoomInCursor() },
|
||||
CursorIcon::ZoomIn => _zoomInCursor(),
|
||||
CursorIcon::ZoomOut if available!(macos = 15.0) => unsafe { NSCursor::zoomOutCursor() },
|
||||
CursorIcon::ZoomOut => _zoomOutCursor(),
|
||||
CursorIcon::NeResize => _windowResizeNorthEastCursor(),
|
||||
CursorIcon::NwResize => _windowResizeNorthWestCursor(),
|
||||
CursorIcon::SeResize => _windowResizeSouthEastCursor(),
|
||||
CursorIcon::SwResize => _windowResizeSouthWestCursor(),
|
||||
CursorIcon::NeswResize => _windowResizeNorthEastSouthWestCursor(),
|
||||
CursorIcon::NwseResize => _windowResizeNorthWestSouthEastCursor(),
|
||||
// This is the wrong semantics for `Wait`, but it's the same as
|
||||
// what's used in Safari and Chrome.
|
||||
CursorIcon::Wait | CursorIcon::Progress => busyButClickableCursor(),
|
||||
@@ -6,14 +6,14 @@ use objc2_app_kit::{NSEvent, NSEventModifierFlags, NSEventSubtype, NSEventType};
|
||||
use objc2_core_foundation::{CFData, CFRetained};
|
||||
use objc2_foundation::NSPoint;
|
||||
use smol_str::SmolStr;
|
||||
use winit_core::event::{ElementState, KeyEvent, Modifiers};
|
||||
use winit_core::keyboard::{
|
||||
|
||||
use super::ffi;
|
||||
use crate::event::{ElementState, KeyEvent, Modifiers};
|
||||
use crate::keyboard::{
|
||||
Key, KeyCode, KeyLocation, ModifiersKeys, ModifiersState, NamedKey, NativeKey, NativeKeyCode,
|
||||
PhysicalKey,
|
||||
};
|
||||
|
||||
use super::ffi;
|
||||
|
||||
/// Ignores ALL modifiers.
|
||||
pub fn get_modifierless_char(scancode: u16) -> Key {
|
||||
let Some(ptr) = NonNull::new(unsafe { ffi::TISCopyCurrentKeyboardLayoutInputSource() }) else {
|
||||
@@ -67,7 +67,9 @@ pub fn get_modifierless_char(scancode: u16) -> Key {
|
||||
|
||||
// Ignores all modifiers except for SHIFT (yes, even ALT is ignored).
|
||||
fn get_logical_key_char(ns_event: &NSEvent, modifierless_chars: &str) -> Key {
|
||||
let string = ns_event.charactersIgnoringModifiers().map(|s| s.to_string()).unwrap_or_default();
|
||||
let string = unsafe { ns_event.charactersIgnoringModifiers() }
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_default();
|
||||
if string.is_empty() {
|
||||
// Probably a dead key
|
||||
let first_char = modifierless_chars.chars().next();
|
||||
@@ -83,7 +85,7 @@ pub(crate) fn create_key_event(ns_event: &NSEvent, is_press: bool, is_repeat: bo
|
||||
use ElementState::{Pressed, Released};
|
||||
let state = if is_press { Pressed } else { Released };
|
||||
|
||||
let scancode = ns_event.keyCode();
|
||||
let scancode = unsafe { ns_event.keyCode() };
|
||||
let mut physical_key = scancode_to_physicalkey(scancode as u32);
|
||||
|
||||
// NOTE: The logical key should heed both SHIFT and ALT if possible.
|
||||
@@ -93,7 +95,7 @@ pub(crate) fn create_key_event(ns_event: &NSEvent, is_press: bool, is_repeat: bo
|
||||
// * Pressing CTRL SHIFT A: logical key should also be "A"
|
||||
// This is not easy to tease out of `NSEvent`, but we do our best.
|
||||
|
||||
let characters = ns_event.characters().map(|s| s.to_string()).unwrap_or_default();
|
||||
let characters = unsafe { ns_event.characters() }.map(|s| s.to_string()).unwrap_or_default();
|
||||
let text_with_all_modifiers = if characters.is_empty() {
|
||||
None
|
||||
} else {
|
||||
@@ -109,13 +111,13 @@ pub(crate) fn create_key_event(ns_event: &NSEvent, is_press: bool, is_repeat: bo
|
||||
// `get_modifierless_char/key_without_modifiers` ignores ALL modifiers.
|
||||
let key_without_modifiers = get_modifierless_char(scancode);
|
||||
|
||||
let modifiers = ns_event.modifierFlags();
|
||||
let modifiers = unsafe { ns_event.modifierFlags() };
|
||||
let has_ctrl = modifiers.contains(NSEventModifierFlags::Control);
|
||||
let has_cmd = modifiers.contains(NSEventModifierFlags::Command);
|
||||
|
||||
let logical_key = match text_with_all_modifiers.as_ref() {
|
||||
// Only checking for ctrl and cmd here, not checking for alt because we DO want to
|
||||
// include its effect in the key. For example if -on the German layout- one
|
||||
// include its effect in the key. For example if -on the Germay layout- one
|
||||
// presses alt+8, the logical key should be "{"
|
||||
// Also not checking if this is a release event because then this issue would
|
||||
// still affect the key release.
|
||||
@@ -300,15 +302,15 @@ const NX_DEVICERALTKEYMASK: NSEventModifierFlags = NSEventModifierFlags(0x000000
|
||||
const NX_DEVICERCTLKEYMASK: NSEventModifierFlags = NSEventModifierFlags(0x00002000);
|
||||
|
||||
pub(super) fn lalt_pressed(event: &NSEvent) -> bool {
|
||||
event.modifierFlags().contains(NX_DEVICELALTKEYMASK)
|
||||
unsafe { event.modifierFlags() }.contains(NX_DEVICELALTKEYMASK)
|
||||
}
|
||||
|
||||
pub(super) fn ralt_pressed(event: &NSEvent) -> bool {
|
||||
event.modifierFlags().contains(NX_DEVICERALTKEYMASK)
|
||||
unsafe { event.modifierFlags() }.contains(NX_DEVICERALTKEYMASK)
|
||||
}
|
||||
|
||||
pub(super) fn event_mods(event: &NSEvent) -> Modifiers {
|
||||
let flags = event.modifierFlags();
|
||||
let flags = unsafe { event.modifierFlags() };
|
||||
let mut state = ModifiersState::empty();
|
||||
let mut pressed_mods = ModifiersKeys::empty();
|
||||
|
||||
@@ -328,11 +330,12 @@ pub(super) fn event_mods(event: &NSEvent) -> Modifiers {
|
||||
pressed_mods.set(ModifiersKeys::LMETA, flags.contains(NX_DEVICELCMDKEYMASK));
|
||||
pressed_mods.set(ModifiersKeys::RMETA, flags.contains(NX_DEVICERCMDKEYMASK));
|
||||
|
||||
Modifiers::new(state, pressed_mods)
|
||||
Modifiers { state, pressed_mods }
|
||||
}
|
||||
|
||||
pub(super) fn dummy_event() -> Option<Retained<NSEvent>> {
|
||||
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
|
||||
unsafe {
|
||||
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
|
||||
NSEventType::ApplicationDefined,
|
||||
NSPoint::new(0.0, 0.0),
|
||||
NSEventModifierFlags(0),
|
||||
@@ -343,9 +346,10 @@ pub(super) fn dummy_event() -> Option<Retained<NSEvent>> {
|
||||
0,
|
||||
0,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32> {
|
||||
pub(crate) fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32> {
|
||||
let code = match physical_key {
|
||||
PhysicalKey::Code(code) => code,
|
||||
PhysicalKey::Unidentified(_) => return None,
|
||||
@@ -477,7 +481,7 @@ pub fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
||||
pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
|
||||
// Follows what Chromium and Firefox do:
|
||||
// https://chromium.googlesource.com/chromium/src.git/+/3e1a26c44c024d97dc9a4c09bbc6a2365398ca2c/ui/events/keycodes/dom/dom_code_data.inc
|
||||
// https://searchfox.org/mozilla-central/rev/c597e9c789ad36af84a0370d395be066b7dc94f4/widget/NativeKeyToDOMCodeName.h
|
||||
@@ -1,38 +1,75 @@
|
||||
use std::rc::Rc;
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::fmt;
|
||||
use std::panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe};
|
||||
use std::rc::{Rc, Weak};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use objc2::rc::{Retained, autoreleasepool};
|
||||
use objc2::rc::{autoreleasepool, Retained};
|
||||
use objc2::runtime::ProtocolObject;
|
||||
use objc2::{MainThreadMarker, available};
|
||||
use objc2::{available, MainThreadMarker};
|
||||
use objc2_app_kit::{
|
||||
NSApplication, NSApplicationActivationPolicy, NSApplicationDidFinishLaunchingNotification,
|
||||
NSApplicationWillTerminateNotification, NSWindow,
|
||||
};
|
||||
use objc2_core_foundation::{CFIndex, CFRunLoopActivity, kCFRunLoopCommonModes};
|
||||
use objc2_foundation::{NSNotificationCenter, NSObjectProtocol};
|
||||
use rwh_06::HasDisplayHandle;
|
||||
use tracing::debug_span;
|
||||
use winit_common::core_foundation::{MainRunLoop, MainRunLoopObserver, tracing_observers};
|
||||
use winit_common::foundation::create_observer;
|
||||
use winit_core::application::ApplicationHandler;
|
||||
use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource};
|
||||
use winit_core::error::{EventLoopError, RequestError};
|
||||
use winit_core::event_loop::pump_events::PumpStatus;
|
||||
use winit_core::event_loop::{
|
||||
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
|
||||
EventLoopProxy as CoreEventLoopProxy, OwnedDisplayHandle as CoreOwnedDisplayHandle,
|
||||
};
|
||||
use winit_core::monitor::MonitorHandle as CoreMonitorHandle;
|
||||
use winit_core::window::Theme;
|
||||
|
||||
use super::super::notification_center::create_observer;
|
||||
use super::app::override_send_event;
|
||||
use super::app_state::AppState;
|
||||
use super::cursor::CustomCursor;
|
||||
use super::event::dummy_event;
|
||||
use super::monitor;
|
||||
use crate::ActivationPolicy;
|
||||
use crate::window::Window;
|
||||
use super::observer::setup_control_flow_observers;
|
||||
use crate::application::ApplicationHandler;
|
||||
use crate::error::{EventLoopError, RequestError};
|
||||
use crate::event_loop::{
|
||||
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
|
||||
EventLoopProxy as CoreEventLoopProxy, OwnedDisplayHandle as CoreOwnedDisplayHandle,
|
||||
};
|
||||
use crate::monitor::MonitorHandle as CoreMonitorHandle;
|
||||
use crate::platform::macos::ActivationPolicy;
|
||||
use crate::platform::pump_events::PumpStatus;
|
||||
use crate::platform_impl::Window;
|
||||
use crate::window::{CustomCursor as CoreCustomCursor, CustomCursorSource, Theme};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct PanicInfo {
|
||||
inner: Cell<Option<Box<dyn Any + Send + 'static>>>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for PanicInfo {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("PanicInfo").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
// WARNING:
|
||||
// As long as this struct is used through its `impl`, it is UnwindSafe.
|
||||
// (If `get_mut` is called on `inner`, unwind safety may get broken.)
|
||||
impl UnwindSafe for PanicInfo {}
|
||||
impl RefUnwindSafe for PanicInfo {}
|
||||
impl PanicInfo {
|
||||
pub fn is_panicking(&self) -> bool {
|
||||
let inner = self.inner.take();
|
||||
let result = inner.is_some();
|
||||
self.inner.set(inner);
|
||||
result
|
||||
}
|
||||
|
||||
/// Overwrites the current state if the current state is not panicking
|
||||
pub fn set_panic(&self, p: Box<dyn Any + Send + 'static>) {
|
||||
if !self.is_panicking() {
|
||||
self.inner.set(Some(p));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take(&self) -> Option<Box<dyn Any + Send + 'static>> {
|
||||
self.inner.take()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ActiveEventLoop {
|
||||
@@ -65,8 +102,8 @@ impl RootActiveEventLoop for ActiveEventLoop {
|
||||
|
||||
fn create_window(
|
||||
&self,
|
||||
window_attributes: winit_core::window::WindowAttributes,
|
||||
) -> Result<Box<dyn winit_core::window::Window>, RequestError> {
|
||||
window_attributes: crate::window::WindowAttributes,
|
||||
) -> Result<Box<dyn crate::window::Window>, RequestError> {
|
||||
Ok(Box::new(Window::new(self, window_attributes)?))
|
||||
}
|
||||
|
||||
@@ -85,7 +122,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
|
||||
)
|
||||
}
|
||||
|
||||
fn primary_monitor(&self) -> Option<winit_core::monitor::MonitorHandle> {
|
||||
fn primary_monitor(&self) -> Option<crate::monitor::MonitorHandle> {
|
||||
let monitor = monitor::primary_monitor();
|
||||
Some(CoreMonitorHandle(Arc::new(monitor)))
|
||||
}
|
||||
@@ -145,6 +182,7 @@ pub struct EventLoop {
|
||||
app_state: Rc<AppState>,
|
||||
|
||||
window_target: ActiveEventLoop,
|
||||
panic_info: Rc<PanicInfo>,
|
||||
|
||||
// Since macOS 10.11, we no longer need to remove the observers before they are deallocated;
|
||||
// the system instead cleans it up next time it would have posted a notification to it.
|
||||
@@ -152,17 +190,13 @@ pub struct EventLoop {
|
||||
// Though we do still need to keep the observers around to prevent them from being deallocated.
|
||||
_did_finish_launching_observer: Retained<ProtocolObject<dyn NSObjectProtocol>>,
|
||||
_will_terminate_observer: Retained<ProtocolObject<dyn NSObjectProtocol>>,
|
||||
|
||||
_tracing_observers: Option<(MainRunLoopObserver, MainRunLoopObserver)>,
|
||||
_before_waiting_observer: MainRunLoopObserver,
|
||||
_after_waiting_observer: MainRunLoopObserver,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PlatformSpecificEventLoopAttributes {
|
||||
pub activation_policy: Option<ActivationPolicy>,
|
||||
pub default_menu: bool,
|
||||
pub activate_ignoring_other_apps: bool,
|
||||
pub(crate) struct PlatformSpecificEventLoopAttributes {
|
||||
pub(crate) activation_policy: Option<ActivationPolicy>,
|
||||
pub(crate) default_menu: bool,
|
||||
pub(crate) activate_ignoring_other_apps: bool,
|
||||
}
|
||||
|
||||
impl Default for PlatformSpecificEventLoopAttributes {
|
||||
@@ -172,7 +206,9 @@ impl Default for PlatformSpecificEventLoopAttributes {
|
||||
}
|
||||
|
||||
impl EventLoop {
|
||||
pub fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
|
||||
pub(crate) fn new(
|
||||
attributes: &PlatformSpecificEventLoopAttributes,
|
||||
) -> Result<Self, EventLoopError> {
|
||||
let mtm = MainThreadMarker::new()
|
||||
.expect("on macOS, `EventLoop` must be created on the main thread!");
|
||||
|
||||
@@ -188,8 +224,7 @@ impl EventLoop {
|
||||
activation_policy,
|
||||
attributes.default_menu,
|
||||
attributes.activate_ignoring_other_apps,
|
||||
)
|
||||
.ok_or_else(|| EventLoopError::RecreationAttempt)?;
|
||||
);
|
||||
|
||||
// Initialize the application (if it has not already been).
|
||||
let app = NSApplication::sharedApplication(mtm);
|
||||
@@ -197,7 +232,7 @@ impl EventLoop {
|
||||
// Override `sendEvent:` on the application to forward to our application state.
|
||||
override_send_event(&app);
|
||||
|
||||
let center = NSNotificationCenter::defaultCenter();
|
||||
let center = unsafe { NSNotificationCenter::defaultCenter() };
|
||||
|
||||
let weak_app_state = Rc::downgrade(&app_state);
|
||||
let _did_finish_launching_observer = create_observer(
|
||||
@@ -205,7 +240,6 @@ impl EventLoop {
|
||||
// `applicationDidFinishLaunching:`
|
||||
unsafe { NSApplicationDidFinishLaunchingNotification },
|
||||
move |notification| {
|
||||
let _entered = debug_span!("NSApplicationDidFinishLaunchingNotification").entered();
|
||||
if let Some(app_state) = weak_app_state.upgrade() {
|
||||
app_state.did_finish_launching(notification);
|
||||
}
|
||||
@@ -218,55 +252,22 @@ impl EventLoop {
|
||||
// `applicationWillTerminate:`
|
||||
unsafe { NSApplicationWillTerminateNotification },
|
||||
move |notification| {
|
||||
let _entered = debug_span!("NSApplicationWillTerminateNotification").entered();
|
||||
if let Some(app_state) = weak_app_state.upgrade() {
|
||||
app_state.will_terminate(notification);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let main_loop = MainRunLoop::get(mtm);
|
||||
let mode = unsafe { kCFRunLoopCommonModes }.unwrap();
|
||||
|
||||
// Tracing observers have the lowest and highest orderings.
|
||||
let _tracing_observers = tracing_observers(mtm).inspect(|(start, end)| {
|
||||
main_loop.add_observer(start, mode);
|
||||
main_loop.add_observer(end, mode);
|
||||
});
|
||||
|
||||
let app_state_clone = Rc::clone(&app_state);
|
||||
let _before_waiting_observer = MainRunLoopObserver::new(
|
||||
mtm,
|
||||
CFRunLoopActivity::BeforeWaiting,
|
||||
true,
|
||||
// Queued with the second-lowest priority (tracing observers use the lowest) to ensure
|
||||
// it is processed after other observers.
|
||||
CFIndex::MAX - 1,
|
||||
move |_| app_state_clone.cleared(),
|
||||
);
|
||||
main_loop.add_observer(&_before_waiting_observer, mode);
|
||||
|
||||
let app_state_clone = Rc::clone(&app_state);
|
||||
let _after_waiting_observer = MainRunLoopObserver::new(
|
||||
mtm,
|
||||
CFRunLoopActivity::AfterWaiting,
|
||||
true,
|
||||
// Queued with the second-highest priority (tracing observers use the highest) to
|
||||
// ensure it is processed before other observers.
|
||||
CFIndex::MIN + 1,
|
||||
move |_| app_state_clone.wakeup(),
|
||||
);
|
||||
main_loop.add_observer(&_after_waiting_observer, mode);
|
||||
let panic_info: Rc<PanicInfo> = Default::default();
|
||||
setup_control_flow_observers(mtm, Rc::downgrade(&panic_info));
|
||||
|
||||
Ok(EventLoop {
|
||||
app,
|
||||
app_state: app_state.clone(),
|
||||
window_target: ActiveEventLoop { app_state, mtm },
|
||||
panic_info,
|
||||
_did_finish_launching_observer,
|
||||
_will_terminate_observer,
|
||||
_tracing_observers,
|
||||
_before_waiting_observer,
|
||||
_after_waiting_observer,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -274,6 +275,10 @@ impl EventLoop {
|
||||
&self.window_target
|
||||
}
|
||||
|
||||
pub fn run_app<A: ApplicationHandler>(mut self, app: A) -> Result<(), EventLoopError> {
|
||||
self.run_app_on_demand(app)
|
||||
}
|
||||
|
||||
// NB: we don't base this on `pump_events` because for `MacOs` we can't support
|
||||
// `pump_events` elegantly (we just ask to run the loop for a "short" amount of
|
||||
// time and so a layered implementation would end up using a lot of CPU due to
|
||||
@@ -300,6 +305,15 @@ impl EventLoop {
|
||||
// NOTE: Make sure to not run the application re-entrantly, as that'd be confusing.
|
||||
self.app.run();
|
||||
|
||||
// While the app is running it's possible that we catch a panic
|
||||
// to avoid unwinding across an objective-c ffi boundary, which
|
||||
// will lead to us stopping the `NSApplication` and saving the
|
||||
// `PanicInfo` so that we can resume the unwind at a controlled,
|
||||
// safe point in time.
|
||||
if let Some(panic) = self.panic_info.take() {
|
||||
resume_unwind(panic);
|
||||
}
|
||||
|
||||
self.app_state.internal_exit()
|
||||
})
|
||||
});
|
||||
@@ -355,6 +369,15 @@ impl EventLoop {
|
||||
self.app.run();
|
||||
}
|
||||
|
||||
// While the app is running it's possible that we catch a panic
|
||||
// to avoid unwinding across an objective-c ffi boundary, which
|
||||
// will lead to us stopping the application and saving the
|
||||
// `PanicInfo` so that we can resume the unwind at a controlled,
|
||||
// safe point in time.
|
||||
if let Some(panic) = self.panic_info.take() {
|
||||
resume_unwind(panic);
|
||||
}
|
||||
|
||||
if self.app_state.exiting() {
|
||||
self.app_state.internal_exit();
|
||||
PumpStatus::Exit(0)
|
||||
@@ -399,3 +422,29 @@ pub(super) fn notify_windows_of_exit(app: &NSApplication) {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// Catches panics that happen inside `f` and when a panic
|
||||
/// happens, stops the `sharedApplication`
|
||||
#[inline]
|
||||
pub fn stop_app_on_panic<F: FnOnce() -> R + UnwindSafe, R>(
|
||||
mtm: MainThreadMarker,
|
||||
panic_info: Weak<PanicInfo>,
|
||||
f: F,
|
||||
) -> Option<R> {
|
||||
match catch_unwind(f) {
|
||||
Ok(r) => Some(r),
|
||||
Err(e) => {
|
||||
// It's important that we set the panic before requesting a `stop`
|
||||
// because some callback are still called during the `stop` message
|
||||
// and we need to know in those callbacks if the application is currently
|
||||
// panicking
|
||||
{
|
||||
let panic_info = panic_info.upgrade().unwrap();
|
||||
panic_info.set_panic(e);
|
||||
}
|
||||
let app = NSApplication::sharedApplication(mtm);
|
||||
stop_app_immediately(&app);
|
||||
None
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,13 @@ use std::ffi::c_void;
|
||||
|
||||
use objc2::ffi::NSInteger;
|
||||
use objc2::runtime::AnyObject;
|
||||
use objc2_core_foundation::{CFString, CFUUID, cf_type};
|
||||
use objc2_core_foundation::{cf_type, CFString, CFUUID};
|
||||
use objc2_core_graphics::CGDirectDisplayID;
|
||||
|
||||
pub const IO16BitDirectPixels: &str = "-RRRRRGGGGGBBBBB";
|
||||
pub const IO32BitDirectPixels: &str = "--------RRRRRRRRGGGGGGGGBBBBBBBB";
|
||||
|
||||
pub const kIO30BitDirectPixels: &str = "--RRRRRRRRRRGGGGGGGGGGBBBBBBBBBB";
|
||||
pub const kIO64BitDirectPixels: &str = "-16R16G16B16";
|
||||
|
||||
// `CGDisplayCreateUUIDFromDisplayID` comes from the `ColorSync` framework.
|
||||
// However, that framework was only introduced "publicly" in macOS 10.13.
|
||||
@@ -22,14 +22,14 @@ pub const kIO64BitDirectPixels: &str = "-16R16G16B16";
|
||||
// `ApplicationServices`, see:
|
||||
// https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html#//apple_ref/doc/uid/TP40001067-CH210-BBCFFIEG
|
||||
#[link(name = "ApplicationServices", kind = "framework")]
|
||||
unsafe extern "C" {
|
||||
extern "C" {
|
||||
pub fn CGDisplayCreateUUIDFromDisplayID(display: CGDirectDisplayID) -> *mut CFUUID;
|
||||
|
||||
pub fn CGDisplayGetDisplayIDFromUUID(uuid: &CFUUID) -> CGDirectDisplayID;
|
||||
}
|
||||
|
||||
#[link(name = "CoreGraphics", kind = "framework")]
|
||||
unsafe extern "C" {
|
||||
extern "C" {
|
||||
// Wildly used private APIs; Apple uses them for their Terminal.app.
|
||||
pub fn CGSMainConnectionID() -> *mut AnyObject;
|
||||
pub fn CGSSetWindowBackgroundBlurRadius(
|
||||
@@ -60,7 +60,7 @@ pub const kUCKeyActionDisplay: u16 = 3;
|
||||
pub const kUCKeyTranslateNoDeadKeysMask: OptionBits = 1;
|
||||
|
||||
#[link(name = "Carbon", kind = "framework")]
|
||||
unsafe extern "C" {
|
||||
extern "C" {
|
||||
pub static kTISPropertyUnicodeKeyLayoutData: &'static CFString;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
@@ -1,8 +1,8 @@
|
||||
use objc2::rc::Retained;
|
||||
use objc2::runtime::Sel;
|
||||
use objc2::{MainThreadMarker, sel};
|
||||
use objc2::{sel, MainThreadMarker};
|
||||
use objc2_app_kit::{NSApplication, NSEventModifierFlags, NSMenu, NSMenuItem};
|
||||
use objc2_foundation::{NSBundle, NSProcessInfo, NSString, ns_string};
|
||||
use objc2_foundation::{ns_string, NSProcessInfo, NSString};
|
||||
|
||||
struct KeyEquivalent<'a> {
|
||||
key: &'a NSString,
|
||||
@@ -16,10 +16,7 @@ pub fn initialize(app: &NSApplication) {
|
||||
menubar.addItem(&app_menu_item);
|
||||
|
||||
let app_menu = NSMenu::new(mtm);
|
||||
let process_name = match NSBundle::mainBundle().name() {
|
||||
Some(bundle_name) => bundle_name,
|
||||
None => NSProcessInfo::processInfo().processName(),
|
||||
};
|
||||
let process_name = NSProcessInfo::processInfo().processName();
|
||||
|
||||
// About menu item
|
||||
let about_item_title = ns_string!("About ").stringByAppendingString(&process_name);
|
||||
@@ -82,7 +79,7 @@ pub fn initialize(app: &NSApplication) {
|
||||
app_menu.addItem(&quit_item);
|
||||
app_menu_item.setSubmenu(Some(&app_menu));
|
||||
|
||||
app.setServicesMenu(Some(&services_menu));
|
||||
unsafe { app.setServicesMenu(Some(&services_menu)) };
|
||||
app.setMainMenu(Some(&menubar));
|
||||
}
|
||||
|
||||
23
src/platform_impl/apple/appkit/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
#[macro_use]
|
||||
mod util;
|
||||
|
||||
mod app;
|
||||
mod app_state;
|
||||
mod cursor;
|
||||
mod event;
|
||||
mod event_loop;
|
||||
mod ffi;
|
||||
mod menu;
|
||||
mod monitor;
|
||||
mod observer;
|
||||
mod view;
|
||||
mod window;
|
||||
mod window_delegate;
|
||||
|
||||
pub(crate) use self::event::{physicalkey_to_scancode, scancode_to_physicalkey};
|
||||
pub(crate) use self::event_loop::{
|
||||
ActiveEventLoop, EventLoop, PlatformSpecificEventLoopAttributes,
|
||||
};
|
||||
pub(crate) use self::monitor::MonitorHandle;
|
||||
pub(crate) use self::window::Window;
|
||||
pub(crate) use self::window_delegate::PlatformSpecificWindowAttributes;
|
||||
@@ -6,22 +6,22 @@ use std::ptr::NonNull;
|
||||
use std::{fmt, ptr};
|
||||
|
||||
use dispatch2::run_on_main;
|
||||
use dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
|
||||
use objc2::MainThreadMarker;
|
||||
use objc2::rc::Retained;
|
||||
use objc2::MainThreadMarker;
|
||||
use objc2_app_kit::NSScreen;
|
||||
use objc2_core_foundation::{CFArray, CFRetained, CFUUID};
|
||||
use objc2_core_graphics::{
|
||||
CGDirectDisplayID, CGDisplayBounds, CGDisplayCopyAllDisplayModes, CGDisplayCopyDisplayMode,
|
||||
CGDisplayMode, CGDisplayModelNumber, CGGetActiveDisplayList, CGMainDisplayID,
|
||||
};
|
||||
use objc2_core_video::{CVDisplayLink, CVTimeFlags, kCVReturnSuccess};
|
||||
use objc2_foundation::{NSNumber, NSPoint, NSRect, ns_string};
|
||||
use objc2_core_video::{kCVReturnSuccess, CVDisplayLink, CVTimeFlags};
|
||||
use objc2_foundation::{ns_string, NSNumber, NSPoint, NSRect};
|
||||
use tracing::warn;
|
||||
use winit_core::monitor::{MonitorHandleProvider, VideoMode};
|
||||
|
||||
use super::ffi;
|
||||
use super::util::cgerr;
|
||||
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
|
||||
use crate::monitor::{MonitorHandleProvider, VideoMode};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VideoModeHandle {
|
||||
@@ -65,33 +65,31 @@ impl VideoModeHandle {
|
||||
native_mode: NativeDisplayMode,
|
||||
refresh_rate_millihertz: Option<NonZeroU32>,
|
||||
) -> Self {
|
||||
// The bit-depth is basically always 32 since macOS 10.12.
|
||||
#[allow(deprecated)]
|
||||
let pixel_encoding =
|
||||
CGDisplayMode::pixel_encoding(Some(&native_mode.0)).unwrap().to_string();
|
||||
let bit_depth = if pixel_encoding.eq_ignore_ascii_case(ffi::IO32BitDirectPixels) {
|
||||
NonZeroU16::new(32)
|
||||
} else if pixel_encoding.eq_ignore_ascii_case(ffi::IO16BitDirectPixels) {
|
||||
NonZeroU16::new(16)
|
||||
} else if pixel_encoding.eq_ignore_ascii_case(ffi::kIO30BitDirectPixels) {
|
||||
NonZeroU16::new(30)
|
||||
} else if pixel_encoding.eq_ignore_ascii_case(ffi::kIO64BitDirectPixels) {
|
||||
NonZeroU16::new(64)
|
||||
} else {
|
||||
warn!(?pixel_encoding, "unknown bit depth");
|
||||
None
|
||||
};
|
||||
unsafe {
|
||||
#[allow(deprecated)]
|
||||
let pixel_encoding =
|
||||
CGDisplayMode::pixel_encoding(Some(&native_mode.0)).unwrap().to_string();
|
||||
let bit_depth = if pixel_encoding.eq_ignore_ascii_case(ffi::IO32BitDirectPixels) {
|
||||
32
|
||||
} else if pixel_encoding.eq_ignore_ascii_case(ffi::IO16BitDirectPixels) {
|
||||
16
|
||||
} else if pixel_encoding.eq_ignore_ascii_case(ffi::kIO30BitDirectPixels) {
|
||||
30
|
||||
} else {
|
||||
unimplemented!()
|
||||
};
|
||||
|
||||
let mode = VideoMode::new(
|
||||
PhysicalSize::new(
|
||||
CGDisplayMode::pixel_width(Some(&native_mode.0)) as u32,
|
||||
CGDisplayMode::pixel_height(Some(&native_mode.0)) as u32,
|
||||
),
|
||||
bit_depth,
|
||||
refresh_rate_millihertz,
|
||||
);
|
||||
let mode = VideoMode {
|
||||
size: PhysicalSize::new(
|
||||
CGDisplayMode::pixel_width(Some(&native_mode.0)) as u32,
|
||||
CGDisplayMode::pixel_height(Some(&native_mode.0)) as u32,
|
||||
),
|
||||
refresh_rate_millihertz,
|
||||
bit_depth: NonZeroU16::new(bit_depth),
|
||||
};
|
||||
|
||||
Self { mode, monitor: monitor.clone(), native_mode }
|
||||
VideoModeHandle { mode, monitor: monitor.clone(), native_mode }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,11 +133,11 @@ impl MonitorHandle {
|
||||
|
||||
fn refresh_rate_millihertz(&self) -> Option<NonZeroU32> {
|
||||
let current_display_mode =
|
||||
NativeDisplayMode(CGDisplayCopyDisplayMode(self.display_id()).unwrap());
|
||||
NativeDisplayMode(unsafe { CGDisplayCopyDisplayMode(self.display_id()) }.unwrap());
|
||||
refresh_rate_millihertz(self.display_id(), ¤t_display_mode)
|
||||
}
|
||||
|
||||
pub fn video_mode_handles(&self) -> impl Iterator<Item = VideoModeHandle> + 'static {
|
||||
pub fn video_mode_handles(&self) -> impl Iterator<Item = VideoModeHandle> {
|
||||
let refresh_rate_millihertz = self.refresh_rate_millihertz();
|
||||
let monitor = self.clone();
|
||||
|
||||
@@ -157,7 +155,7 @@ impl MonitorHandle {
|
||||
};
|
||||
|
||||
modes.into_iter().map(move |mode| {
|
||||
let cg_refresh_rate_hertz = CGDisplayMode::refresh_rate(Some(&mode));
|
||||
let cg_refresh_rate_hertz = unsafe { CGDisplayMode::refresh_rate(Some(&mode)) };
|
||||
|
||||
// CGDisplayModeGetRefreshRate returns 0.0 for any display that
|
||||
// isn't a CRT
|
||||
@@ -200,7 +198,7 @@ impl MonitorHandleProvider for MonitorHandle {
|
||||
//
|
||||
// <https://github.com/glfw/glfw/blob/57cbded0760a50b9039ee0cb3f3c14f60145567c/src/cocoa_monitor.m#L44-L126>
|
||||
fn name(&self) -> Option<std::borrow::Cow<'_, str>> {
|
||||
let screen_num = CGDisplayModelNumber(self.display_id());
|
||||
let screen_num = unsafe { CGDisplayModelNumber(self.display_id()) };
|
||||
Some(format!("Monitor #{screen_num}").into())
|
||||
}
|
||||
|
||||
@@ -208,7 +206,7 @@ impl MonitorHandleProvider for MonitorHandle {
|
||||
// This is already in screen coordinates. If we were using `NSScreen`,
|
||||
// then a conversion would've been needed:
|
||||
// flip_window_screen_coordinates(self.ns_screen(mtm)?.frame())
|
||||
let bounds = CGDisplayBounds(self.display_id());
|
||||
let bounds = unsafe { CGDisplayBounds(self.display_id()) };
|
||||
let position = LogicalPosition::new(bounds.origin.x, bounds.origin.y);
|
||||
Some(position.to_physical(self.scale_factor()))
|
||||
}
|
||||
@@ -223,7 +221,8 @@ impl MonitorHandleProvider for MonitorHandle {
|
||||
}
|
||||
|
||||
fn current_video_mode(&self) -> Option<VideoMode> {
|
||||
let mode = NativeDisplayMode(CGDisplayCopyDisplayMode(self.display_id()).unwrap());
|
||||
let mode =
|
||||
NativeDisplayMode(unsafe { CGDisplayCopyDisplayMode(self.display_id()) }.unwrap());
|
||||
let refresh_rate_millihertz = refresh_rate_millihertz(self.display_id(), &mode);
|
||||
Some(VideoModeHandle::new(self.clone(), mode, refresh_rate_millihertz).mode)
|
||||
}
|
||||
@@ -261,7 +260,7 @@ pub fn available_monitors() -> VecDeque<MonitorHandle> {
|
||||
|
||||
pub fn primary_monitor() -> MonitorHandle {
|
||||
// Display ID just fetched from `CGMainDisplayID`, should be fine to unwrap.
|
||||
MonitorHandle::new(CGMainDisplayID()).expect("invalid display ID")
|
||||
MonitorHandle::new(unsafe { CGMainDisplayID() }).expect("invalid display ID")
|
||||
}
|
||||
|
||||
impl fmt::Debug for MonitorHandle {
|
||||
@@ -312,38 +311,40 @@ pub(crate) fn flip_window_screen_coordinates(frame: NSRect) -> NSPoint {
|
||||
// It is intentional that we use `CGMainDisplayID` (as opposed to
|
||||
// `NSScreen::mainScreen`), because that's what the screen coordinates
|
||||
// are relative to, no matter which display the window is currently on.
|
||||
let main_screen_height = CGDisplayBounds(CGMainDisplayID()).size.height;
|
||||
let main_screen_height = unsafe { CGDisplayBounds(CGMainDisplayID()) }.size.height;
|
||||
|
||||
let y = main_screen_height - frame.size.height - frame.origin.y;
|
||||
NSPoint::new(frame.origin.x, y)
|
||||
}
|
||||
|
||||
fn refresh_rate_millihertz(id: CGDirectDisplayID, mode: &NativeDisplayMode) -> Option<NonZeroU32> {
|
||||
let refresh_rate = CGDisplayMode::refresh_rate(Some(&mode.0));
|
||||
if refresh_rate > 0.0 {
|
||||
return NonZeroU32::new((refresh_rate * 1000.0).round() as u32);
|
||||
}
|
||||
unsafe {
|
||||
let refresh_rate = CGDisplayMode::refresh_rate(Some(&mode.0));
|
||||
if refresh_rate > 0.0 {
|
||||
return NonZeroU32::new((refresh_rate * 1000.0).round() as u32);
|
||||
}
|
||||
|
||||
let mut display_link = std::ptr::null_mut();
|
||||
#[allow(deprecated)]
|
||||
if unsafe { CVDisplayLink::create_with_cg_display(id, NonNull::from(&mut display_link)) }
|
||||
!= kCVReturnSuccess
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let display_link = unsafe { CFRetained::from_raw(NonNull::new(display_link).unwrap()) };
|
||||
#[allow(deprecated)]
|
||||
let time = display_link.nominal_output_video_refresh_period();
|
||||
let mut display_link = std::ptr::null_mut();
|
||||
#[allow(deprecated)]
|
||||
if CVDisplayLink::create_with_cg_display(id, NonNull::from(&mut display_link))
|
||||
!= kCVReturnSuccess
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let display_link = CFRetained::from_raw(NonNull::new(display_link).unwrap());
|
||||
#[allow(deprecated)]
|
||||
let time = display_link.nominal_output_video_refresh_period();
|
||||
|
||||
// This value is indefinite if an invalid display link was specified
|
||||
if time.flags & CVTimeFlags::IsIndefinite.0 != 0 {
|
||||
return None;
|
||||
}
|
||||
// This value is indefinite if an invalid display link was specified
|
||||
if time.flags & CVTimeFlags::IsIndefinite.0 != 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
(time.timeScale as i64)
|
||||
.checked_div(time.timeValue)
|
||||
.map(|v| (v * 1000) as u32)
|
||||
.and_then(NonZeroU32::new)
|
||||
(time.timeScale as i64)
|
||||
.checked_div(time.timeValue)
|
||||
.map(|v| (v * 1000) as u32)
|
||||
.and_then(NonZeroU32::new)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -352,10 +353,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn uuid_stable() {
|
||||
let primary_id = CGMainDisplayID();
|
||||
|
||||
let handle_a = MonitorHandle::new(primary_id).unwrap();
|
||||
let handle_b = MonitorHandle::new(primary_id).unwrap();
|
||||
let handle_a = MonitorHandle::new(1).unwrap();
|
||||
let handle_b = MonitorHandle::new(1).unwrap();
|
||||
assert_eq!(handle_a, handle_b);
|
||||
assert_eq!(handle_a.display_id(), handle_b.display_id());
|
||||
assert_eq!(handle_a.uuid(), handle_b.uuid());
|
||||
@@ -370,10 +369,8 @@ mod tests {
|
||||
/// Test the MonitorHandle::new fallback.
|
||||
#[test]
|
||||
fn monitorhandle_from_zero() {
|
||||
let primary_id = CGMainDisplayID();
|
||||
|
||||
let handle0 = MonitorHandle::new(0).unwrap();
|
||||
let handle1 = MonitorHandle::new(primary_id).unwrap();
|
||||
let handle1 = MonitorHandle::new(1).unwrap();
|
||||
assert_eq!(handle0, handle1);
|
||||
assert_eq!(handle0.display_id(), handle1.display_id());
|
||||
assert_eq!(handle0.uuid(), handle1.uuid());
|
||||
@@ -394,7 +391,7 @@ mod tests {
|
||||
// Assertion failed: (did_initialize), function CGS_REQUIRE_INIT, file CGInitialization.c, line 44.
|
||||
// ```
|
||||
// See https://github.com/JXA-Cookbook/JXA-Cookbook/issues/27#issuecomment-277517668
|
||||
let _ = CGMainDisplayID();
|
||||
let _ = unsafe { CGMainDisplayID() };
|
||||
|
||||
let handle = MonitorHandle(CFUUID::new(None).unwrap());
|
||||
assert_eq!(handle.display_id(), 0);
|
||||
286
src/platform_impl/apple/appkit/observer.rs
Normal file
@@ -0,0 +1,286 @@
|
||||
//! Utilities for working with `CFRunLoop`.
|
||||
//!
|
||||
//! See Apple's documentation on Run Loops for details:
|
||||
//! <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html>
|
||||
use std::cell::Cell;
|
||||
use std::ffi::c_void;
|
||||
use std::panic::{AssertUnwindSafe, UnwindSafe};
|
||||
use std::ptr;
|
||||
use std::rc::Weak;
|
||||
use std::time::Instant;
|
||||
|
||||
use objc2::MainThreadMarker;
|
||||
use objc2_core_foundation::{
|
||||
kCFRunLoopCommonModes, kCFRunLoopDefaultMode, CFAbsoluteTimeGetCurrent, CFIndex, CFRetained,
|
||||
CFRunLoop, CFRunLoopActivity, CFRunLoopObserver, CFRunLoopObserverCallBack,
|
||||
CFRunLoopObserverContext, CFRunLoopTimer,
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
use super::app_state::AppState;
|
||||
use super::event_loop::{stop_app_on_panic, PanicInfo};
|
||||
|
||||
unsafe fn control_flow_handler<F>(panic_info: *mut c_void, f: F)
|
||||
where
|
||||
F: FnOnce(Weak<PanicInfo>) + UnwindSafe,
|
||||
{
|
||||
let info_from_raw = unsafe { Weak::from_raw(panic_info as *mut PanicInfo) };
|
||||
// Asserting unwind safety on this type should be fine because `PanicInfo` is
|
||||
// `RefUnwindSafe` and `Rc<T>` is `UnwindSafe` if `T` is `RefUnwindSafe`.
|
||||
let panic_info = AssertUnwindSafe(Weak::clone(&info_from_raw));
|
||||
// `from_raw` takes ownership of the data behind the pointer.
|
||||
// But if this scope takes ownership of the weak pointer, then
|
||||
// the weak pointer will get free'd at the end of the scope.
|
||||
// However we want to keep that weak reference around after the function.
|
||||
std::mem::forget(info_from_raw);
|
||||
|
||||
let mtm = MainThreadMarker::new().unwrap();
|
||||
stop_app_on_panic(mtm, Weak::clone(&panic_info), move || {
|
||||
let _ = &panic_info;
|
||||
f(panic_info.0)
|
||||
});
|
||||
}
|
||||
|
||||
// begin is queued with the highest priority to ensure it is processed before other observers
|
||||
extern "C-unwind" fn control_flow_begin_handler(
|
||||
_: *mut CFRunLoopObserver,
|
||||
activity: CFRunLoopActivity,
|
||||
panic_info: *mut c_void,
|
||||
) {
|
||||
unsafe {
|
||||
control_flow_handler(panic_info, |panic_info| {
|
||||
#[allow(non_upper_case_globals)]
|
||||
match activity {
|
||||
CFRunLoopActivity::AfterWaiting => {
|
||||
// trace!("Triggered `CFRunLoopAfterWaiting`");
|
||||
AppState::get(MainThreadMarker::new().unwrap()).wakeup(panic_info);
|
||||
// trace!("Completed `CFRunLoopAfterWaiting`");
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// end is queued with the lowest priority to ensure it is processed after other observers
|
||||
// without that, LoopExiting would get sent after AboutToWait
|
||||
extern "C-unwind" fn control_flow_end_handler(
|
||||
_: *mut CFRunLoopObserver,
|
||||
activity: CFRunLoopActivity,
|
||||
panic_info: *mut c_void,
|
||||
) {
|
||||
unsafe {
|
||||
control_flow_handler(panic_info, |panic_info| {
|
||||
#[allow(non_upper_case_globals)]
|
||||
match activity {
|
||||
CFRunLoopActivity::BeforeWaiting => {
|
||||
// trace!("Triggered `CFRunLoopBeforeWaiting`");
|
||||
AppState::get(MainThreadMarker::new().unwrap()).cleared(panic_info);
|
||||
// trace!("Completed `CFRunLoopBeforeWaiting`");
|
||||
},
|
||||
CFRunLoopActivity::Exit => (), /* unimplemented!(), // not expected to ever happen */
|
||||
_ => unreachable!(),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RunLoop(CFRetained<CFRunLoop>);
|
||||
|
||||
impl RunLoop {
|
||||
pub fn main(mtm: MainThreadMarker) -> Self {
|
||||
// SAFETY: We have a MainThreadMarker here, which means we know we're on the main thread, so
|
||||
// scheduling (and scheduling a non-`Send` block) to that thread is allowed.
|
||||
let _ = mtm;
|
||||
RunLoop(CFRunLoop::main().unwrap())
|
||||
}
|
||||
|
||||
pub fn wakeup(&self) {
|
||||
self.0.wake_up();
|
||||
}
|
||||
|
||||
unsafe fn add_observer(
|
||||
&self,
|
||||
flags: CFRunLoopActivity,
|
||||
// The lower the value, the sooner this will run
|
||||
priority: CFIndex,
|
||||
handler: CFRunLoopObserverCallBack,
|
||||
context: *mut CFRunLoopObserverContext,
|
||||
) {
|
||||
let observer =
|
||||
unsafe { CFRunLoopObserver::new(None, flags.0, true, priority, handler, context) }
|
||||
.unwrap();
|
||||
self.0.add_observer(Some(&observer), unsafe { kCFRunLoopCommonModes });
|
||||
}
|
||||
|
||||
/// Submit a closure to run on the main thread as the next step in the run loop, before other
|
||||
/// event sources are processed.
|
||||
///
|
||||
/// This is used for running event handlers, as those are not allowed to run re-entrantly.
|
||||
///
|
||||
/// # Implementation
|
||||
///
|
||||
/// This queuing could be implemented in the following several ways with subtle differences in
|
||||
/// timing. This list is sorted in rough order in which they are run:
|
||||
///
|
||||
/// 1. Using `CFRunLoopPerformBlock` or `-[NSRunLoop performBlock:]`.
|
||||
///
|
||||
/// 2. Using `-[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]` or wrapping the
|
||||
/// event in `NSEvent` and posting that to `-[NSApplication postEvent:atStart:]` (both
|
||||
/// creates a custom `CFRunLoopSource`, and signals that to wake up the main event loop).
|
||||
///
|
||||
/// a. `atStart = true`.
|
||||
///
|
||||
/// b. `atStart = false`.
|
||||
///
|
||||
/// 3. `dispatch_async` or `dispatch_async_f`. Note that this may appear before 2b, it does not
|
||||
/// respect the ordering that runloop events have.
|
||||
///
|
||||
/// We choose the first one, both for ease-of-implementation, but mostly for consistency, as we
|
||||
/// want the event to be queued in a way that preserves the order the events originally arrived
|
||||
/// in.
|
||||
///
|
||||
/// As an example, let's assume that we receive two events from the user, a mouse click which we
|
||||
/// handled by queuing it, and a window resize which we handled immediately. If we allowed
|
||||
/// AppKit to choose the ordering when queuing the mouse event, it might get put in the back of
|
||||
/// the queue, and the events would appear out of order to the user of Winit. So we must instead
|
||||
/// put the event at the very front of the queue, to be handled as soon as possible after
|
||||
/// handling whatever event it's currently handling.
|
||||
pub fn queue_closure(&self, closure: impl FnOnce() + 'static) {
|
||||
// Convert `FnOnce()` to `Block<dyn Fn()>`.
|
||||
let closure = Cell::new(Some(closure));
|
||||
let block = block2::RcBlock::new(move || {
|
||||
if let Some(closure) = closure.take() {
|
||||
closure()
|
||||
} else {
|
||||
error!("tried to execute queued closure on main thread twice");
|
||||
}
|
||||
});
|
||||
|
||||
// There are a few common modes (`kCFRunLoopCommonModes`) defined by Cocoa:
|
||||
// - `NSDefaultRunLoopMode`, alias of `kCFRunLoopDefaultMode`.
|
||||
// - `NSEventTrackingRunLoopMode`, used when mouse-dragging and live-resizing a window.
|
||||
// - `NSModalPanelRunLoopMode`, used when running a modal inside the Winit event loop.
|
||||
// - `NSConnectionReplyMode`: TODO.
|
||||
//
|
||||
// We only want to run event handlers in the default mode, as we support running a blocking
|
||||
// modal inside a Winit event handler (see [#1779]) which outrules the modal panel mode, and
|
||||
// resizing such panel window enters the event tracking run loop mode, so we can't directly
|
||||
// trigger events inside that mode either.
|
||||
//
|
||||
// Any events that are queued while running a modal or when live-resizing will instead wait,
|
||||
// and be delivered to the application afterwards.
|
||||
//
|
||||
// [#1779]: https://github.com/rust-windowing/winit/issues/1779
|
||||
let mode = unsafe { kCFRunLoopDefaultMode.unwrap() };
|
||||
|
||||
// SAFETY: The runloop is valid, the mode is a `CFStringRef`, and the block is `'static`.
|
||||
unsafe { self.0.perform_block(Some(mode), Some(&block)) }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup_control_flow_observers(mtm: MainThreadMarker, panic_info: Weak<PanicInfo>) {
|
||||
let run_loop = RunLoop::main(mtm);
|
||||
unsafe {
|
||||
let mut context = CFRunLoopObserverContext {
|
||||
info: Weak::into_raw(panic_info) as *mut _,
|
||||
version: 0,
|
||||
retain: None,
|
||||
release: None,
|
||||
copyDescription: None,
|
||||
};
|
||||
run_loop.add_observer(
|
||||
CFRunLoopActivity::AfterWaiting,
|
||||
CFIndex::MIN,
|
||||
Some(control_flow_begin_handler),
|
||||
&mut context as *mut _,
|
||||
);
|
||||
run_loop.add_observer(
|
||||
CFRunLoopActivity::Exit | CFRunLoopActivity::BeforeWaiting,
|
||||
CFIndex::MAX,
|
||||
Some(control_flow_end_handler),
|
||||
&mut context as *mut _,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EventLoopWaker {
|
||||
timer: CFRetained<CFRunLoopTimer>,
|
||||
|
||||
/// An arbitrary instant in the past, that will trigger an immediate wake
|
||||
/// We save this as the `next_fire_date` for consistency so we can
|
||||
/// easily check if the next_fire_date needs updating.
|
||||
start_instant: Instant,
|
||||
|
||||
/// This is what the `NextFireDate` has been set to.
|
||||
/// `None` corresponds to `waker.stop()` and `start_instant` is used
|
||||
/// for `waker.start()`
|
||||
next_fire_date: Option<Instant>,
|
||||
}
|
||||
|
||||
impl Drop for EventLoopWaker {
|
||||
fn drop(&mut self) {
|
||||
self.timer.invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
impl EventLoopWaker {
|
||||
pub(crate) fn new() -> Self {
|
||||
extern "C-unwind" fn wakeup_main_loop(_timer: *mut CFRunLoopTimer, _info: *mut c_void) {}
|
||||
unsafe {
|
||||
// Create a timer with a 0.1µs interval (1ns does not work) to mimic polling.
|
||||
// It is initially setup with a first fire time really far into the
|
||||
// future, but that gets changed to fire immediately in did_finish_launching
|
||||
let timer = CFRunLoopTimer::new(
|
||||
None,
|
||||
f64::MAX,
|
||||
0.000_000_1,
|
||||
0,
|
||||
0,
|
||||
Some(wakeup_main_loop),
|
||||
ptr::null_mut(),
|
||||
)
|
||||
.unwrap();
|
||||
CFRunLoop::main().unwrap().add_timer(Some(&timer), kCFRunLoopCommonModes);
|
||||
Self { timer, start_instant: Instant::now(), next_fire_date: None }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
if self.next_fire_date.is_some() {
|
||||
self.next_fire_date = None;
|
||||
self.timer.set_next_fire_date(f64::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(&mut self) {
|
||||
if self.next_fire_date != Some(self.start_instant) {
|
||||
self.next_fire_date = Some(self.start_instant);
|
||||
self.timer.set_next_fire_date(f64::MIN);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_at(&mut self, instant: Option<Instant>) {
|
||||
let now = Instant::now();
|
||||
match instant {
|
||||
Some(instant) if now >= instant => {
|
||||
self.start();
|
||||
},
|
||||
Some(instant) => {
|
||||
if self.next_fire_date != Some(instant) {
|
||||
self.next_fire_date = Some(instant);
|
||||
let current = CFAbsoluteTimeGetCurrent();
|
||||
let duration = instant - now;
|
||||
let fsecs = duration.subsec_nanos() as f64 / 1_000_000_000.0
|
||||
+ duration.as_secs() as f64;
|
||||
self.timer.set_next_fire_date(current + fsecs);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
self.stop();
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/platform_impl/apple/appkit/util.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use objc2_core_graphics::CGError;
|
||||
use tracing::trace;
|
||||
|
||||
use crate::error::OsError;
|
||||
|
||||
macro_rules! trace_scope {
|
||||
($s:literal) => {
|
||||
let _crate =
|
||||
$crate::platform_impl::platform::appkit::util::TraceGuard::new(module_path!(), $s);
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) struct TraceGuard {
|
||||
module_path: &'static str,
|
||||
called_from_fn: &'static str,
|
||||
}
|
||||
|
||||
impl TraceGuard {
|
||||
#[inline]
|
||||
pub(crate) fn new(module_path: &'static str, called_from_fn: &'static str) -> Self {
|
||||
trace!(target = module_path, "Triggered `{}`", called_from_fn);
|
||||
Self { module_path, called_from_fn }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TraceGuard {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
trace!(target = self.module_path, "Completed `{}`", self.called_from_fn);
|
||||
}
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
pub(crate) fn cgerr(err: CGError) -> Result<(), OsError> {
|
||||
if err == CGError::Success {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(os_error!(format!("CGError {err:?}")))
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,20 @@
|
||||
#![allow(clippy::unnecessary_cast)]
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::ptr;
|
||||
use std::rc::Rc;
|
||||
|
||||
use dpi::{LogicalPosition, PhysicalSize};
|
||||
use objc2::rc::Retained;
|
||||
use objc2::runtime::{AnyObject, Sel};
|
||||
use objc2::{AnyThread, DefinedClass, MainThreadMarker, define_class, msg_send};
|
||||
use objc2::{define_class, msg_send, DefinedClass, MainThreadMarker};
|
||||
use objc2_app_kit::{
|
||||
NSApplication, NSCursor, NSEvent, NSEventPhase, NSResponder, NSTextInputClient, NSTrackingArea,
|
||||
NSTrackingAreaOptions, NSView, NSViewLayerContentsRedrawPolicy, NSWindow,
|
||||
NSApplication, NSCursor, NSEvent, NSEventPhase, NSResponder, NSTextInputClient,
|
||||
NSTrackingRectTag, NSView, NSWindow,
|
||||
};
|
||||
use objc2_core_foundation::CGRect;
|
||||
use objc2_foundation::{
|
||||
NSArray, NSAttributedString, NSAttributedStringKey, NSCopying, NSMutableAttributedString,
|
||||
NSNotFound, NSObject, NSPoint, NSRange, NSRect, NSSize, NSString, NSUInteger,
|
||||
};
|
||||
use tracing::{debug_span, trace_span};
|
||||
use winit_core::event::{
|
||||
DeviceEvent, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta,
|
||||
PointerKind, PointerSource, TouchPhase, WindowEvent,
|
||||
};
|
||||
use winit_core::keyboard::{Key, KeyCode, KeyLocation, ModifiersState, NamedKey};
|
||||
use winit_core::window::ImeCapabilities;
|
||||
|
||||
use super::app_state::AppState;
|
||||
use super::cursor::{default_cursor, invisible_cursor};
|
||||
@@ -31,7 +23,13 @@ use super::event::{
|
||||
scancode_to_physicalkey,
|
||||
};
|
||||
use super::window::window_id;
|
||||
use crate::OptionAsAlt;
|
||||
use crate::dpi::{LogicalPosition, LogicalSize};
|
||||
use crate::event::{
|
||||
DeviceEvent, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta,
|
||||
PointerKind, PointerSource, TouchPhase, WindowEvent,
|
||||
};
|
||||
use crate::keyboard::{Key, KeyCode, KeyLocation, ModifiersState, NamedKey};
|
||||
use crate::platform::macos::OptionAsAlt;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CursorState {
|
||||
@@ -119,17 +117,19 @@ pub struct ViewState {
|
||||
ime_size: Cell<NSSize>,
|
||||
modifiers: Cell<Modifiers>,
|
||||
phys_modifiers: RefCell<HashMap<Key, ModLocationMask>>,
|
||||
tracking_rect: Cell<Option<NSTrackingRectTag>>,
|
||||
ime_state: Cell<ImeState>,
|
||||
input_source: RefCell<String>,
|
||||
|
||||
/// True iff the application wants IME events.
|
||||
///
|
||||
/// Can be set using `set_ime_allowed`
|
||||
ime_capabilities: Cell<Option<ImeCapabilities>>,
|
||||
ime_allowed: Cell<bool>,
|
||||
|
||||
/// True if the current key event should be forwarded
|
||||
/// to the application, even during IME
|
||||
forward_key_to_app: Cell<bool>,
|
||||
|
||||
marked_text: RefCell<Retained<NSMutableAttributedString>>,
|
||||
accepts_first_mouse: bool,
|
||||
|
||||
@@ -151,35 +151,49 @@ define_class!(
|
||||
true
|
||||
}
|
||||
|
||||
#[unsafe(method(viewDidMoveToWindow))]
|
||||
fn view_did_move_to_window(&self) {
|
||||
trace_scope!("viewDidMoveToWindow");
|
||||
if let Some(tracking_rect) = self.ivars().tracking_rect.take() {
|
||||
self.removeTrackingRect(tracking_rect);
|
||||
}
|
||||
|
||||
let rect = self.frame();
|
||||
let tracking_rect = unsafe {
|
||||
self.addTrackingRect_owner_userData_assumeInside(rect, self, ptr::null_mut(), false)
|
||||
};
|
||||
assert_ne!(tracking_rect, 0, "failed adding tracking rect");
|
||||
self.ivars().tracking_rect.set(Some(tracking_rect));
|
||||
}
|
||||
|
||||
// Not a normal method on `NSView`, it's triggered by `NSViewFrameDidChangeNotification`.
|
||||
#[unsafe(method(viewFrameDidChangeNotification:))]
|
||||
fn frame_did_change(&self, _notification: Option<&AnyObject>) {
|
||||
let _entered = debug_span!("NSViewFrameDidChangeNotification").entered();
|
||||
trace_scope!("NSViewFrameDidChangeNotification");
|
||||
if let Some(tracking_rect) = self.ivars().tracking_rect.take() {
|
||||
self.removeTrackingRect(tracking_rect);
|
||||
}
|
||||
|
||||
let rect = self.frame();
|
||||
let tracking_rect = unsafe {
|
||||
self.addTrackingRect_owner_userData_assumeInside(rect, self, ptr::null_mut(), false)
|
||||
};
|
||||
assert_ne!(tracking_rect, 0, "failed adding tracking rect");
|
||||
self.ivars().tracking_rect.set(Some(tracking_rect));
|
||||
|
||||
// Emit resize event here rather than from windowDidResize because:
|
||||
// 1. When a new window is created as a tab, the frame size may change without a window
|
||||
// resize occurring.
|
||||
// 2. Even when a window resize does occur on a new tabbed window, it contains the wrong
|
||||
// size (includes tab height).
|
||||
self.surface_resized();
|
||||
// During live resize, AppKit may not let the normal event loop reach its next redraw
|
||||
// point before stretching the current layer contents. Redraw immediately after the
|
||||
// app has observed the new surface size.
|
||||
self.redraw_during_live_resize();
|
||||
}
|
||||
|
||||
#[unsafe(method(viewDidChangeBackingProperties))]
|
||||
fn view_did_change_backing_properties(&self) {
|
||||
let _entered = debug_span!("viewDidChangeBackingProperties").entered();
|
||||
// Moving between displays or changing scale can alter the drawable backing size
|
||||
// without a matching frame-size change.
|
||||
self.surface_resized();
|
||||
self.redraw_during_live_resize();
|
||||
let logical_size = LogicalSize::new(rect.size.width as f64, rect.size.height as f64);
|
||||
let size = logical_size.to_physical::<u32>(self.scale_factor());
|
||||
self.queue_event(WindowEvent::SurfaceResized(size));
|
||||
}
|
||||
|
||||
#[unsafe(method(drawRect:))]
|
||||
fn draw_rect(&self, _rect: NSRect) {
|
||||
let _entered = debug_span!("drawRect:").entered();
|
||||
trace_scope!("drawRect:");
|
||||
|
||||
self.ivars().app_state.handle_redraw(window_id(&self.window()));
|
||||
|
||||
@@ -188,7 +202,7 @@ define_class!(
|
||||
|
||||
#[unsafe(method(acceptsFirstResponder))]
|
||||
fn accepts_first_responder(&self) -> bool {
|
||||
let _entered = trace_span!("acceptsFirstResponder").entered();
|
||||
trace_scope!("acceptsFirstResponder");
|
||||
true
|
||||
}
|
||||
|
||||
@@ -202,13 +216,13 @@ define_class!(
|
||||
// extension for using `NSTouchBar`
|
||||
#[unsafe(method_id(touchBar))]
|
||||
fn touch_bar(&self) -> Option<Retained<NSObject>> {
|
||||
let _entered = debug_span!("touchBar").entered();
|
||||
trace_scope!("touchBar");
|
||||
None
|
||||
}
|
||||
|
||||
#[unsafe(method(resetCursorRects))]
|
||||
fn reset_cursor_rects(&self) {
|
||||
let _entered = debug_span!("resetCursorRects").entered();
|
||||
trace_scope!("resetCursorRects");
|
||||
let bounds = self.bounds();
|
||||
let cursor_state = self.ivars().cursor_state.borrow();
|
||||
// We correctly invoke `addCursorRect` only from inside `resetCursorRects`
|
||||
@@ -223,13 +237,13 @@ define_class!(
|
||||
unsafe impl NSTextInputClient for WinitView {
|
||||
#[unsafe(method(hasMarkedText))]
|
||||
fn has_marked_text(&self) -> bool {
|
||||
let _entered = debug_span!("hasMarkedText").entered();
|
||||
trace_scope!("hasMarkedText");
|
||||
self.ivars().marked_text.borrow().length() > 0
|
||||
}
|
||||
|
||||
#[unsafe(method(markedRange))]
|
||||
fn marked_range(&self) -> NSRange {
|
||||
let _entered = debug_span!("markedRange").entered();
|
||||
trace_scope!("markedRange");
|
||||
let length = self.ivars().marked_text.borrow().length();
|
||||
if length > 0 {
|
||||
NSRange::new(0, length)
|
||||
@@ -241,7 +255,7 @@ define_class!(
|
||||
|
||||
#[unsafe(method(selectedRange))]
|
||||
fn selected_range(&self) -> NSRange {
|
||||
let _entered = debug_span!("selectedRange").entered();
|
||||
trace_scope!("selectedRange");
|
||||
// Documented to return `{NSNotFound, 0}` if there is no selection.
|
||||
NSRange::new(NSNotFound as NSUInteger, 0)
|
||||
}
|
||||
@@ -254,7 +268,7 @@ define_class!(
|
||||
_replacement_range: NSRange,
|
||||
) {
|
||||
// TODO: Use _replacement_range, requires changing the event to report surrounding text.
|
||||
let _entered = debug_span!("setMarkedText:selectedRange:replacementRange:").entered();
|
||||
trace_scope!("setMarkedText:selectedRange:replacementRange:");
|
||||
|
||||
let (marked_text, string) = if let Some(string) =
|
||||
string.downcast_ref::<NSAttributedString>()
|
||||
@@ -276,38 +290,33 @@ define_class!(
|
||||
self.queue_event(WindowEvent::Ime(Ime::Enabled));
|
||||
}
|
||||
|
||||
if self.hasMarkedText() {
|
||||
if unsafe { self.hasMarkedText() } {
|
||||
self.ivars().ime_state.set(ImeState::Preedit);
|
||||
} else {
|
||||
// In case the preedit was cleared, set IME into the Ground state.
|
||||
self.ivars().ime_state.set(ImeState::Ground);
|
||||
}
|
||||
|
||||
let string = string.to_string();
|
||||
let cursor_range = if string.is_empty() {
|
||||
// An empty string basically means that there's no preedit, so indicate that by
|
||||
// sending a `None` cursor range.
|
||||
None
|
||||
} else {
|
||||
// Convert the selected range from UTF-16 code unit indices to UTF-8 byte
|
||||
// offsets. `utf16_to_utf8_offset` is defensive: it snaps an offset that would
|
||||
// split a surrogate pair down to the character boundary and clamps an
|
||||
// out-of-bounds offset to the string length, so no `NSRangeException` is
|
||||
// possible and the resulting range can never be inverted (`lower <= upper`).
|
||||
// IMEs are known to send both mid-surrogate and out-of-bounds offsets (e.g.
|
||||
// native Pinyin, see https://github.com/alacritty/alacritty/issues/8791).
|
||||
let lowerbound_utf8 = utf16_to_utf8_offset(&string, selected_range.location);
|
||||
let upperbound_utf8 = utf16_to_utf8_offset(&string, selected_range.end());
|
||||
// Convert the selected range from UTF-16 indices to UTF-8 indices.
|
||||
let sub_string_a = unsafe { string.substringToIndex(selected_range.location) };
|
||||
let sub_string_b = unsafe { string.substringToIndex(selected_range.end()) };
|
||||
let lowerbound_utf8 = sub_string_a.len();
|
||||
let upperbound_utf8 = sub_string_b.len();
|
||||
Some((lowerbound_utf8, upperbound_utf8))
|
||||
};
|
||||
|
||||
// Send WindowEvent for updating marked text
|
||||
self.queue_event(WindowEvent::Ime(Ime::Preedit(string, cursor_range)));
|
||||
self.queue_event(WindowEvent::Ime(Ime::Preedit(string.to_string(), cursor_range)));
|
||||
}
|
||||
|
||||
#[unsafe(method(unmarkText))]
|
||||
fn unmark_text(&self) {
|
||||
let _entered = debug_span!("unmarkText").entered();
|
||||
trace_scope!("unmarkText");
|
||||
*self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new();
|
||||
|
||||
let input_context = self.inputContext().expect("input context");
|
||||
@@ -324,7 +333,7 @@ define_class!(
|
||||
|
||||
#[unsafe(method_id(validAttributesForMarkedText))]
|
||||
fn valid_attributes_for_marked_text(&self) -> Retained<NSArray<NSAttributedStringKey>> {
|
||||
let _entered = trace_span!("validAttributesForMarkedText").entered();
|
||||
trace_scope!("validAttributesForMarkedText");
|
||||
NSArray::new()
|
||||
}
|
||||
|
||||
@@ -334,14 +343,13 @@ define_class!(
|
||||
_range: NSRange,
|
||||
_actual_range: *mut NSRange,
|
||||
) -> Option<Retained<NSAttributedString>> {
|
||||
let _entered =
|
||||
trace_span!("attributedSubstringForProposedRange:actualRange:").entered();
|
||||
trace_scope!("attributedSubstringForProposedRange:actualRange:");
|
||||
None
|
||||
}
|
||||
|
||||
#[unsafe(method(characterIndexForPoint:))]
|
||||
fn character_index_for_point(&self, _point: NSPoint) -> NSUInteger {
|
||||
let _entered = debug_span!("characterIndexForPoint:").entered();
|
||||
trace_scope!("characterIndexForPoint:");
|
||||
0
|
||||
}
|
||||
|
||||
@@ -351,23 +359,16 @@ define_class!(
|
||||
_range: NSRange,
|
||||
_actual_range: *mut NSRange,
|
||||
) -> NSRect {
|
||||
let _entered = debug_span!("firstRectForCharacterRange:actualRange:").entered();
|
||||
|
||||
// Guard when the view is no longer in a window during teardown.
|
||||
let Some(window) = (**self).window() else {
|
||||
return CGRect::ZERO;
|
||||
};
|
||||
|
||||
// Return value is expected to be in screen coordinates, so we need a conversion
|
||||
trace_scope!("firstRectForCharacterRange:actualRange:");
|
||||
let rect = NSRect::new(self.ivars().ime_position.get(), self.ivars().ime_size.get());
|
||||
let view_rect = self.convertRect_toView(rect, None);
|
||||
window.convertRectToScreen(view_rect)
|
||||
// Return value is expected to be in screen coordinates, so we need a conversion here
|
||||
self.window().convertRectToScreen(self.convertRect_toView(rect, None))
|
||||
}
|
||||
|
||||
#[unsafe(method(insertText:replacementRange:))]
|
||||
fn insert_text(&self, string: &NSObject, _replacement_range: NSRange) {
|
||||
// TODO: Use _replacement_range, requires changing the event to report surrounding text.
|
||||
let _entered = debug_span!("insertText:replacementRange:").entered();
|
||||
trace_scope!("insertText:replacementRange:");
|
||||
|
||||
let string = if let Some(string) = string.downcast_ref::<NSAttributedString>() {
|
||||
string.string().to_string()
|
||||
@@ -381,7 +382,7 @@ define_class!(
|
||||
let is_control = string.chars().next().is_some_and(|c| c.is_control());
|
||||
|
||||
// Commit only if we have marked text.
|
||||
if self.hasMarkedText() && self.is_ime_enabled() && !is_control {
|
||||
if unsafe { self.hasMarkedText() } && self.is_ime_enabled() && !is_control {
|
||||
self.queue_event(WindowEvent::Ime(Ime::Preedit(String::new(), None)));
|
||||
self.queue_event(WindowEvent::Ime(Ime::Commit(string)));
|
||||
self.ivars().ime_state.set(ImeState::Committed);
|
||||
@@ -392,7 +393,7 @@ define_class!(
|
||||
// "human readable" character happens, i.e. newlines, tabs, and Ctrl+C.
|
||||
#[unsafe(method(doCommandBySelector:))]
|
||||
fn do_command_by_selector(&self, command: Sel) {
|
||||
let _entered = debug_span!("doCommandBySelector:").entered();
|
||||
trace_scope!("doCommandBySelector:");
|
||||
|
||||
// We shouldn't forward any character from just committed text, since we'll end up
|
||||
// sending it twice with some IMEs like Korean one. We'll also always send
|
||||
@@ -404,7 +405,8 @@ define_class!(
|
||||
|
||||
self.ivars().forward_key_to_app.set(true);
|
||||
|
||||
if self.hasMarkedText() && self.ivars().ime_state.get() == ImeState::Preedit {
|
||||
if unsafe { self.hasMarkedText() } && self.ivars().ime_state.get() == ImeState::Preedit
|
||||
{
|
||||
// Leave preedit so that we also report the key-up for this key.
|
||||
self.ivars().ime_state.set(ImeState::Ground);
|
||||
}
|
||||
@@ -431,7 +433,7 @@ define_class!(
|
||||
impl WinitView {
|
||||
#[unsafe(method(keyDown:))]
|
||||
fn key_down(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("keyDown:").entered();
|
||||
trace_scope!("keyDown:");
|
||||
{
|
||||
let mut prev_input_source = self.ivars().input_source.borrow_mut();
|
||||
let current_input_source = self.current_input_source();
|
||||
@@ -454,9 +456,9 @@ define_class!(
|
||||
// we must send the `KeyboardInput` event during IME if it triggered
|
||||
// `doCommandBySelector`. (doCommandBySelector means that the keyboard input
|
||||
// is not handled by IME and should be handled by the application)
|
||||
if self.ivars().ime_capabilities.get().is_some() {
|
||||
if self.ivars().ime_allowed.get() {
|
||||
let events_for_nsview = NSArray::from_slice(&[&*event]);
|
||||
self.interpretKeyEvents(&events_for_nsview);
|
||||
unsafe { self.interpretKeyEvents(&events_for_nsview) };
|
||||
|
||||
// If the text was committed we must treat the next keyboard event as IME related.
|
||||
if self.ivars().ime_state.get() == ImeState::Committed {
|
||||
@@ -479,7 +481,7 @@ define_class!(
|
||||
};
|
||||
|
||||
if !had_ime_input || self.ivars().forward_key_to_app.get() {
|
||||
let key_event = create_key_event(&event, true, event.isARepeat());
|
||||
let key_event = create_key_event(&event, true, unsafe { event.isARepeat() });
|
||||
self.queue_event(WindowEvent::KeyboardInput {
|
||||
device_id: None,
|
||||
event: key_event,
|
||||
@@ -490,7 +492,7 @@ define_class!(
|
||||
|
||||
#[unsafe(method(keyUp:))]
|
||||
fn key_up(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("keyUp:").entered();
|
||||
trace_scope!("keyUp:");
|
||||
|
||||
let event = replace_event(event, self.option_as_alt());
|
||||
self.update_modifiers(&event, false);
|
||||
@@ -507,14 +509,14 @@ define_class!(
|
||||
|
||||
#[unsafe(method(flagsChanged:))]
|
||||
fn flags_changed(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("flagsChanged:").entered();
|
||||
trace_scope!("flagsChanged:");
|
||||
|
||||
self.update_modifiers(event, true);
|
||||
}
|
||||
|
||||
#[unsafe(method(insertTab:))]
|
||||
fn insert_tab(&self, _sender: Option<&AnyObject>) {
|
||||
let _entered = debug_span!("insertTab:").entered();
|
||||
trace_scope!("insertTab:");
|
||||
let window = self.window();
|
||||
if let Some(first_responder) = window.firstResponder() {
|
||||
if *first_responder == ***self {
|
||||
@@ -525,7 +527,7 @@ define_class!(
|
||||
|
||||
#[unsafe(method(insertBackTab:))]
|
||||
fn insert_back_tab(&self, _sender: Option<&AnyObject>) {
|
||||
let _entered = debug_span!("insertBackTab:").entered();
|
||||
trace_scope!("insertBackTab:");
|
||||
let window = self.window();
|
||||
if let Some(first_responder) = window.firstResponder() {
|
||||
if *first_responder == ***self {
|
||||
@@ -539,14 +541,14 @@ define_class!(
|
||||
#[unsafe(method(cancelOperation:))]
|
||||
fn cancel_operation(&self, _sender: Option<&AnyObject>) {
|
||||
let mtm = MainThreadMarker::from(self);
|
||||
let _entered = debug_span!("cancelOperation:").entered();
|
||||
trace_scope!("cancelOperation:");
|
||||
|
||||
let event = NSApplication::sharedApplication(mtm)
|
||||
.currentEvent()
|
||||
.expect("could not find current event");
|
||||
|
||||
self.update_modifiers(&event, false);
|
||||
let event = create_key_event(&event, true, event.isARepeat());
|
||||
let event = create_key_event(&event, true, unsafe { event.isARepeat() });
|
||||
|
||||
self.queue_event(WindowEvent::KeyboardInput {
|
||||
device_id: None,
|
||||
@@ -568,73 +570,71 @@ define_class!(
|
||||
|
||||
#[unsafe(method(mouseDown:))]
|
||||
fn mouse_down(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("mouseDown:").entered();
|
||||
trace_scope!("mouseDown:");
|
||||
self.mouse_motion(event);
|
||||
self.mouse_click(event, ElementState::Pressed);
|
||||
}
|
||||
|
||||
#[unsafe(method(mouseUp:))]
|
||||
fn mouse_up(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("mouseUp:").entered();
|
||||
trace_scope!("mouseUp:");
|
||||
self.mouse_motion(event);
|
||||
self.mouse_click(event, ElementState::Released);
|
||||
}
|
||||
|
||||
#[unsafe(method(rightMouseDown:))]
|
||||
fn right_mouse_down(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("rightMouseDown:").entered();
|
||||
trace_scope!("rightMouseDown:");
|
||||
self.mouse_motion(event);
|
||||
self.mouse_click(event, ElementState::Pressed);
|
||||
}
|
||||
|
||||
#[unsafe(method(rightMouseUp:))]
|
||||
fn right_mouse_up(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("rightMouseUp:").entered();
|
||||
trace_scope!("rightMouseUp:");
|
||||
self.mouse_motion(event);
|
||||
self.mouse_click(event, ElementState::Released);
|
||||
}
|
||||
|
||||
#[unsafe(method(otherMouseDown:))]
|
||||
fn other_mouse_down(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("otherMouseDown:").entered();
|
||||
trace_scope!("otherMouseDown:");
|
||||
self.mouse_motion(event);
|
||||
self.mouse_click(event, ElementState::Pressed);
|
||||
}
|
||||
|
||||
#[unsafe(method(otherMouseUp:))]
|
||||
fn other_mouse_up(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("otherMouseUp:").entered();
|
||||
trace_scope!("otherMouseUp:");
|
||||
self.mouse_motion(event);
|
||||
self.mouse_click(event, ElementState::Released);
|
||||
}
|
||||
|
||||
// No tracing on these because that would be overly verbose
|
||||
|
||||
#[unsafe(method(mouseMoved:))]
|
||||
fn mouse_moved(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("mouseMoved:").entered();
|
||||
self.mouse_motion(event);
|
||||
}
|
||||
|
||||
#[unsafe(method(mouseDragged:))]
|
||||
fn mouse_dragged(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("mouseDragged:").entered();
|
||||
self.mouse_motion(event);
|
||||
}
|
||||
|
||||
#[unsafe(method(rightMouseDragged:))]
|
||||
fn right_mouse_dragged(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("rightMouseDragged:").entered();
|
||||
self.mouse_motion(event);
|
||||
}
|
||||
|
||||
#[unsafe(method(otherMouseDragged:))]
|
||||
fn other_mouse_dragged(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("otherMouseDragged:").entered();
|
||||
self.mouse_motion(event);
|
||||
}
|
||||
|
||||
#[unsafe(method(mouseEntered:))]
|
||||
fn mouse_entered(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("mouseEntered:").entered();
|
||||
trace_scope!("mouseEntered:");
|
||||
|
||||
let position = self.mouse_view_point(event).to_physical(self.scale_factor());
|
||||
|
||||
@@ -648,7 +648,7 @@ define_class!(
|
||||
|
||||
#[unsafe(method(mouseExited:))]
|
||||
fn mouse_exited(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("mouseExited:").entered();
|
||||
trace_scope!("mouseExited:");
|
||||
|
||||
let position = self.mouse_view_point(event).to_physical(self.scale_factor());
|
||||
|
||||
@@ -662,13 +662,13 @@ define_class!(
|
||||
|
||||
#[unsafe(method(scrollWheel:))]
|
||||
fn scroll_wheel(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("scrollWheel:").entered();
|
||||
trace_scope!("scrollWheel:");
|
||||
|
||||
self.mouse_motion(event);
|
||||
|
||||
let delta = {
|
||||
let (x, y) = (event.scrollingDeltaX(), event.scrollingDeltaY());
|
||||
if event.hasPreciseScrollingDeltas() {
|
||||
let (x, y) = unsafe { (event.scrollingDeltaX(), event.scrollingDeltaY()) };
|
||||
if unsafe { event.hasPreciseScrollingDeltas() } {
|
||||
let delta = LogicalPosition::new(x, y).to_physical(self.scale_factor());
|
||||
MouseScrollDelta::PixelDelta(delta)
|
||||
} else {
|
||||
@@ -681,10 +681,10 @@ define_class!(
|
||||
// momentum phase is recorded (or rather, the started/ended cases of the
|
||||
// momentum phase) then we report the touch phase.
|
||||
#[allow(non_upper_case_globals)]
|
||||
let phase = match event.momentumPhase() {
|
||||
let phase = match unsafe { event.momentumPhase() } {
|
||||
NSEventPhase::MayBegin | NSEventPhase::Began => TouchPhase::Started,
|
||||
NSEventPhase::Ended | NSEventPhase::Cancelled => TouchPhase::Ended,
|
||||
_ => match event.phase() {
|
||||
_ => match unsafe { event.phase() } {
|
||||
NSEventPhase::MayBegin | NSEventPhase::Began => TouchPhase::Started,
|
||||
NSEventPhase::Ended | NSEventPhase::Cancelled => TouchPhase::Ended,
|
||||
_ => TouchPhase::Moved,
|
||||
@@ -701,12 +701,12 @@ define_class!(
|
||||
|
||||
#[unsafe(method(magnifyWithEvent:))]
|
||||
fn magnify_with_event(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("magnifyWithEvent:").entered();
|
||||
trace_scope!("magnifyWithEvent:");
|
||||
|
||||
self.mouse_motion(event);
|
||||
|
||||
#[allow(non_upper_case_globals)]
|
||||
let phase = match event.phase() {
|
||||
let phase = match unsafe { event.phase() } {
|
||||
NSEventPhase::Began => TouchPhase::Started,
|
||||
NSEventPhase::Changed => TouchPhase::Moved,
|
||||
NSEventPhase::Cancelled => TouchPhase::Cancelled,
|
||||
@@ -716,14 +716,14 @@ define_class!(
|
||||
|
||||
self.queue_event(WindowEvent::PinchGesture {
|
||||
device_id: None,
|
||||
delta: event.magnification(),
|
||||
delta: unsafe { event.magnification() },
|
||||
phase,
|
||||
});
|
||||
}
|
||||
|
||||
#[unsafe(method(smartMagnifyWithEvent:))]
|
||||
fn smart_magnify_with_event(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("smartMagnifyWithEvent:").entered();
|
||||
trace_scope!("smartMagnifyWithEvent:");
|
||||
|
||||
self.mouse_motion(event);
|
||||
|
||||
@@ -732,12 +732,12 @@ define_class!(
|
||||
|
||||
#[unsafe(method(rotateWithEvent:))]
|
||||
fn rotate_with_event(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("rotateWithEvent:").entered();
|
||||
trace_scope!("rotateWithEvent:");
|
||||
|
||||
self.mouse_motion(event);
|
||||
|
||||
#[allow(non_upper_case_globals)]
|
||||
let phase = match event.phase() {
|
||||
let phase = match unsafe { event.phase() } {
|
||||
NSEventPhase::Began => TouchPhase::Started,
|
||||
NSEventPhase::Changed => TouchPhase::Moved,
|
||||
NSEventPhase::Cancelled => TouchPhase::Cancelled,
|
||||
@@ -747,19 +747,19 @@ define_class!(
|
||||
|
||||
self.queue_event(WindowEvent::RotationGesture {
|
||||
device_id: None,
|
||||
delta: event.rotation(),
|
||||
delta: unsafe { event.rotation() },
|
||||
phase,
|
||||
});
|
||||
}
|
||||
|
||||
#[unsafe(method(pressureChangeWithEvent:))]
|
||||
fn pressure_change_with_event(&self, event: &NSEvent) {
|
||||
let _entered = debug_span!("pressureChangeWithEvent:").entered();
|
||||
trace_scope!("pressureChangeWithEvent:");
|
||||
|
||||
self.queue_event(WindowEvent::TouchpadPressure {
|
||||
device_id: None,
|
||||
pressure: event.pressure(),
|
||||
stage: event.stage() as i64,
|
||||
pressure: unsafe { event.pressure() },
|
||||
stage: unsafe { event.stage() } as i64,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -768,13 +768,13 @@ define_class!(
|
||||
// https://github.com/chromium/chromium/blob/a86a8a6bcfa438fa3ac2eba6f02b3ad1f8e0756f/ui/views/cocoa/bridged_content_view.mm#L816
|
||||
#[unsafe(method(_wantsKeyDownForEvent:))]
|
||||
fn wants_key_down_for_event(&self, _event: &NSEvent) -> bool {
|
||||
let _entered = debug_span!("_wantsKeyDownForEvent:").entered();
|
||||
trace_scope!("_wantsKeyDownForEvent:");
|
||||
true
|
||||
}
|
||||
|
||||
#[unsafe(method(acceptsFirstMouse:))]
|
||||
fn accepts_first_mouse(&self, _event: &NSEvent) -> bool {
|
||||
let _entered = debug_span!("acceptsFirstMouse:").entered();
|
||||
trace_scope!("acceptsFirstMouse:");
|
||||
self.ivars().accepts_first_mouse
|
||||
}
|
||||
}
|
||||
@@ -794,65 +794,19 @@ impl WinitView {
|
||||
ime_size: Default::default(),
|
||||
modifiers: Default::default(),
|
||||
phys_modifiers: Default::default(),
|
||||
tracking_rect: Default::default(),
|
||||
ime_state: Default::default(),
|
||||
input_source: Default::default(),
|
||||
ime_capabilities: Default::default(),
|
||||
ime_allowed: Default::default(),
|
||||
forward_key_to_app: Default::default(),
|
||||
marked_text: Default::default(),
|
||||
accepts_first_mouse,
|
||||
option_as_alt: Cell::new(option_as_alt),
|
||||
});
|
||||
let this: Retained<Self> = unsafe { msg_send![super(this), init] };
|
||||
|
||||
*this.ivars().input_source.borrow_mut() = this.current_input_source();
|
||||
|
||||
// Ask AppKit to redisplay the layer while the view is being resized so layer-backed
|
||||
// surfaces keep painting.
|
||||
this.setLayerContentsRedrawPolicy(NSViewLayerContentsRedrawPolicy::DuringViewResize);
|
||||
|
||||
// `MouseEnteredAndExited` enables receiving events through `mouseEntered:` and
|
||||
// `mouseExited:`.
|
||||
//
|
||||
// `MouseMoved` enables receiving events through `mouseMoved:`
|
||||
//
|
||||
// We do not set `CursorUpdate` because it is part of the "flexible" alternative to
|
||||
// `cursorRect` based cursor image updates, and we currently still use
|
||||
// `cursorRect`s. We also can't really switch to this approach because "The
|
||||
// cursorUpdate(with:) message is not sent when the NSTrackingCursorUpdate option is
|
||||
// specified along with [`ActiveAlways`]."
|
||||
//
|
||||
// `ActiveAlways` indicates we want to receive events when the window is not
|
||||
// focused ("key window" in Cocoa terms), which matches the behavior on other
|
||||
// platforms.
|
||||
//
|
||||
// We do not set `AssumeInside` because we want to avoid emitting `Left` events without a
|
||||
// correspondering `Entered` to our consumers, and not setting this flag tells AppKit to
|
||||
// handle this for us by synthesizing entry and exit events in some cases.
|
||||
//
|
||||
// `InVisibleRect` instructs the tracking area's `owner` (our `NSView`) to ignore the value
|
||||
// we provide in `rect` and keep the tracking area's bounds up to date with the
|
||||
// current view bounds automatically.
|
||||
//
|
||||
// We do not set `EnabledDuringMouseDrag` to match the platform behavior on Windows
|
||||
// and Wayland, since neither emit events while being dragged over with an empty
|
||||
// cursor without focus.
|
||||
//
|
||||
// See also https://developer.apple.com/documentation/appkit/nstrackingareaoptions.
|
||||
|
||||
// Safety: the type of `owner` should be `NSView` and is.
|
||||
// The type of `user_info` is irrelevant because it is None.
|
||||
this.addTrackingArea(&*unsafe {
|
||||
NSTrackingArea::initWithRect_options_owner_userInfo(
|
||||
NSTrackingArea::alloc(),
|
||||
NSRect::ZERO,
|
||||
NSTrackingAreaOptions::MouseEnteredAndExited
|
||||
| NSTrackingAreaOptions::MouseMoved
|
||||
| NSTrackingAreaOptions::ActiveAlways
|
||||
| NSTrackingAreaOptions::InVisibleRect,
|
||||
Some(&this),
|
||||
None,
|
||||
)
|
||||
});
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
@@ -867,37 +821,6 @@ impl WinitView {
|
||||
});
|
||||
}
|
||||
|
||||
fn surface_resized(&self) {
|
||||
let Some(window) = (**self).window() else {
|
||||
return;
|
||||
};
|
||||
let size = self.surface_size();
|
||||
let window_id = window_id(&window);
|
||||
self.ivars().app_state.maybe_queue_with_handler(move |app, event_loop| {
|
||||
app.window_event(event_loop, window_id, WindowEvent::SurfaceResized(size));
|
||||
});
|
||||
}
|
||||
|
||||
/// Returns the drawable size from the view's backing-coordinate bounds.
|
||||
pub(super) fn surface_size(&self) -> PhysicalSize<u32> {
|
||||
// The view bounds are authoritative for full-size content views and during live resize.
|
||||
// Deriving this from the window frame can exclude custom titlebar content or be stale.
|
||||
let backing_bounds = self.convertRectToBacking(self.bounds());
|
||||
PhysicalSize::new(
|
||||
backing_bounds.size.width.round().max(0.0) as u32,
|
||||
backing_bounds.size.height.round().max(0.0) as u32,
|
||||
)
|
||||
}
|
||||
|
||||
fn redraw_during_live_resize(&self) {
|
||||
let Some(window) = (**self).window() else {
|
||||
return;
|
||||
};
|
||||
if window.inLiveResize() {
|
||||
self.ivars().app_state.handle_redraw(window_id(&window));
|
||||
}
|
||||
}
|
||||
|
||||
fn scale_factor(&self) -> f64 {
|
||||
self.window().backingScaleFactor() as f64
|
||||
}
|
||||
@@ -935,37 +858,23 @@ impl WinitView {
|
||||
false
|
||||
}
|
||||
}
|
||||
pub(super) fn enable_ime(&self, capabilities: ImeCapabilities) {
|
||||
// This seems reasonable but the prior behavior of `set_ime_allowed` doesn't do this
|
||||
// (it was also broken but let's not break things worse)
|
||||
|
||||
// if self.ivars().ime_capabilities.get().is_none() {
|
||||
// self.ivars().ime_state.set(ImeState::Ground);
|
||||
// }
|
||||
pub(super) fn set_ime_allowed(&self, ime_allowed: bool) {
|
||||
if self.ivars().ime_allowed.get() == ime_allowed {
|
||||
return;
|
||||
}
|
||||
self.ivars().ime_allowed.set(ime_allowed);
|
||||
if self.ivars().ime_allowed.get() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear markedText
|
||||
*self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new();
|
||||
|
||||
// why are we disabling things in an enable fn? who knows. it's what the previous one did
|
||||
// though
|
||||
if self.ivars().ime_state.get() != ImeState::Disabled {
|
||||
self.ivars().ime_state.set(ImeState::Disabled);
|
||||
self.queue_event(WindowEvent::Ime(Ime::Disabled));
|
||||
}
|
||||
self.ivars().ime_capabilities.set(Some(capabilities));
|
||||
*self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new();
|
||||
}
|
||||
pub(super) fn disable_ime(&self) {
|
||||
// see above
|
||||
self.ivars().ime_capabilities.set(None);
|
||||
if self.ivars().ime_state.get() != ImeState::Disabled {
|
||||
self.ivars().ime_state.set(ImeState::Disabled);
|
||||
self.queue_event(WindowEvent::Ime(Ime::Disabled));
|
||||
}
|
||||
// we probably don't need to do this, but again this mirrors the prior behavior of
|
||||
// `set_ime_allowed`
|
||||
*self.ivars().marked_text.borrow_mut() = NSMutableAttributedString::new();
|
||||
}
|
||||
|
||||
pub(super) fn ime_capabilities(&self) -> Option<ImeCapabilities> {
|
||||
self.ivars().ime_capabilities.get()
|
||||
}
|
||||
|
||||
pub(super) fn set_ime_cursor_area(&self, position: NSPoint, size: NSSize) {
|
||||
@@ -1008,8 +917,8 @@ impl WinitView {
|
||||
// later will work though, since the flags are attached to the event and contain valid
|
||||
// information.
|
||||
'send_event: {
|
||||
if is_flags_changed_event && ns_event.keyCode() != 0 {
|
||||
let scancode = ns_event.keyCode();
|
||||
if is_flags_changed_event && unsafe { ns_event.keyCode() } != 0 {
|
||||
let scancode = unsafe { ns_event.keyCode() };
|
||||
let physical_key = scancode_to_physicalkey(scancode as u32);
|
||||
|
||||
let logical_key = code_to_key(physical_key, scancode);
|
||||
@@ -1141,7 +1050,7 @@ impl WinitView {
|
||||
|| view_point.x > frame.size.width
|
||||
|| view_point.y > frame.size.height
|
||||
{
|
||||
let mouse_buttons_down = NSEvent::pressedMouseButtons();
|
||||
let mouse_buttons_down = unsafe { NSEvent::pressedMouseButtons() };
|
||||
if mouse_buttons_down == 0 {
|
||||
// Point is outside of the client area (view) and no buttons are pressed
|
||||
return;
|
||||
@@ -1159,7 +1068,7 @@ impl WinitView {
|
||||
}
|
||||
|
||||
fn mouse_view_point(&self, event: &NSEvent) -> LogicalPosition<f64> {
|
||||
let window_point = event.locationInWindow();
|
||||
let window_point = unsafe { event.locationInWindow() };
|
||||
let view_point = self.convertPoint_fromView(window_point, None);
|
||||
|
||||
LogicalPosition::new(view_point.x, view_point.y)
|
||||
@@ -1173,18 +1082,21 @@ fn mouse_button(event: &NSEvent) -> MouseButton {
|
||||
// For the other events, it's always set to 0.
|
||||
// MacOS only defines the left, right and middle buttons, 3..=31 are left as generic buttons,
|
||||
// but 3 and 4 are very commonly used as Back and Forward by hardware vendors and applications.
|
||||
let b: isize = event.buttonNumber();
|
||||
b.try_into()
|
||||
.ok()
|
||||
.and_then(MouseButton::try_from_u8)
|
||||
.expect("expected MacOS button number in the range 0..=31")
|
||||
match unsafe { event.buttonNumber() } {
|
||||
0 => MouseButton::Left,
|
||||
1 => MouseButton::Right,
|
||||
2 => MouseButton::Middle,
|
||||
3 => MouseButton::Back,
|
||||
4 => MouseButton::Forward,
|
||||
n => MouseButton::Other(n as u16),
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: to get option as alt working we need to rewrite events
|
||||
// we're getting from the operating system, which makes it
|
||||
// impossible to provide such events as extra in `KeyEvent`.
|
||||
fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained<NSEvent> {
|
||||
let ev_mods = event_mods(event).state();
|
||||
let ev_mods = event_mods(event).state;
|
||||
let ignore_alt_characters = match option_as_alt {
|
||||
OptionAsAlt::OnlyLeft if lalt_pressed(event) => true,
|
||||
OptionAsAlt::OnlyRight if ralt_pressed(event) => true,
|
||||
@@ -1194,10 +1106,12 @@ fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained<NSEven
|
||||
&& !ev_mods.meta_key();
|
||||
|
||||
if ignore_alt_characters {
|
||||
let ns_chars =
|
||||
event.charactersIgnoringModifiers().expect("expected characters to be non-null");
|
||||
let ns_chars = unsafe {
|
||||
event.charactersIgnoringModifiers().expect("expected characters to be non-null")
|
||||
};
|
||||
|
||||
NSEvent::keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode(
|
||||
unsafe {
|
||||
NSEvent::keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode(
|
||||
event.r#type(),
|
||||
event.locationInWindow(),
|
||||
event.modifierFlags(),
|
||||
@@ -1210,96 +1124,8 @@ fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained<NSEven
|
||||
event.keyCode(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
} else {
|
||||
event.copy()
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a UTF-16 code unit offset into the corresponding UTF-8 byte offset within `s`.
|
||||
///
|
||||
/// IMEs are not required to send well-formed offsets, so this is defensive: an offset that
|
||||
/// would split a surrogate pair is snapped down to the start of that character, and an
|
||||
/// out-of-bounds offset is clamped to the end of the string (e.g. native Pinyin sends
|
||||
/// out-of-bounds indices, see <https://github.com/alacritty/alacritty/issues/8791>).
|
||||
///
|
||||
/// The mapping is monotone non-decreasing, so applying it to the location and end of an
|
||||
/// `NSRange` (where `location <= end`) can never produce an inverted byte range.
|
||||
fn utf16_to_utf8_offset(s: &str, utf16_offset: usize) -> usize {
|
||||
let mut utf16_pos = 0;
|
||||
for (utf8_pos, ch) in s.char_indices() {
|
||||
if utf16_pos >= utf16_offset {
|
||||
return utf8_pos;
|
||||
}
|
||||
utf16_pos += ch.len_utf16();
|
||||
// The target offset lands strictly inside this character's UTF-16 representation,
|
||||
// i.e. it splits a surrogate pair: snap down to the character boundary.
|
||||
if utf16_pos > utf16_offset {
|
||||
return utf8_pos;
|
||||
}
|
||||
}
|
||||
s.len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Apply the UTF-16 -> UTF-8 conversion to both ends of a `selectedRange {loc, len}`,
|
||||
/// mirroring what `set_marked_text` does for the emitted `Ime::Preedit` cursor range.
|
||||
fn convert(s: &str, loc: usize, len: usize) -> (usize, usize) {
|
||||
(utf16_to_utf8_offset(s, loc), utf16_to_utf8_offset(s, loc + len))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mid_surrogate_offset_snaps_down() {
|
||||
// "😀a": 😀 is one char = 2 UTF-16 units = 4 UTF-8 bytes; offset 1 is mid-pair.
|
||||
assert_eq!(utf16_to_utf8_offset("\u{1F600}a", 1), 0);
|
||||
// Offset 2 is the boundary just after the pair.
|
||||
assert_eq!(utf16_to_utf8_offset("\u{1F600}a", 2), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_longer_inverted() {
|
||||
// "a😀b" with selectedRange {1,1}: previously emitted (1, 0) -- lower > upper, a
|
||||
// slice-panic vector. The boundary-snapping conversion keeps lower <= upper.
|
||||
assert_eq!(convert("a\u{1F600}b", 1, 1), (1, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix_preserved_on_mid_pair_collapse() {
|
||||
// "a😀b" with selectedRange {2,0}: previously collapsed to (0, 0), discarding the
|
||||
// valid "a" prefix; now snaps to the char boundary after "a".
|
||||
assert_eq!(convert("a\u{1F600}b", 2, 0), (1, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_bounds_clamps_to_len() {
|
||||
// Subsumes the #4494 `.min(len)` clamp: an out-of-bounds offset maps to the string
|
||||
// length instead of triggering an NSRangeException.
|
||||
assert_eq!(convert("\u{1F600}a", 99, 0), (5, 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn well_formed_inputs_are_identity() {
|
||||
// The common case (well-formed boundary indices) must be byte-for-byte unchanged.
|
||||
assert_eq!(convert("a\u{1F600}b", 3, 0), (5, 5));
|
||||
assert_eq!(convert("a\u{1F600}b", 4, 0), (6, 6));
|
||||
// BMP multi-byte (Japanese): each char is 1 UTF-16 unit and 3 UTF-8 bytes.
|
||||
assert_eq!(convert("\u{3053}\u{3093}", 1, 1), (3, 6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn monotone_non_decreasing() {
|
||||
// Sweep every UTF-16 offset (including out-of-bounds) over a string mixing BMP and
|
||||
// non-BMP characters and assert the conversion never goes backwards, which is what
|
||||
// guarantees `lower <= upper` for any `NSRange`.
|
||||
let s = "a\u{1F600}b\u{3053}\u{1F4A9}c";
|
||||
let mut prev = 0;
|
||||
for off in 0..=20 {
|
||||
let cur = utf16_to_utf8_offset(s, off);
|
||||
assert!(cur >= prev, "non-monotone at offset {off}: {cur} < {prev}");
|
||||
assert!(cur <= s.len(), "offset {off} mapped past end: {cur} > {}", s.len());
|
||||
prev = cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,22 +4,19 @@ use std::sync::Arc;
|
||||
|
||||
use dispatch2::MainThreadBound;
|
||||
use dpi::{Position, Size};
|
||||
use objc2::rc::{Retained, autoreleasepool};
|
||||
use objc2::{MainThreadMarker, Message, define_class};
|
||||
use objc2::rc::{autoreleasepool, Retained};
|
||||
use objc2::{define_class, MainThreadMarker, Message};
|
||||
use objc2_app_kit::{NSPanel, NSResponder, NSWindow};
|
||||
use objc2_foundation::NSObject;
|
||||
use tracing::trace_span;
|
||||
use winit_core::cursor::Cursor;
|
||||
use winit_core::error::RequestError;
|
||||
use winit_core::icon::Icon;
|
||||
use winit_core::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle};
|
||||
use winit_core::window::{
|
||||
ImeCapabilities, ImeRequest, ImeRequestError, Theme, UserAttentionType, Window as CoreWindow,
|
||||
WindowAttributes, WindowButtons, WindowId, WindowLevel,
|
||||
};
|
||||
|
||||
use super::event_loop::ActiveEventLoop;
|
||||
use super::window_delegate::WindowDelegate;
|
||||
use crate::error::RequestError;
|
||||
use crate::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle};
|
||||
use crate::window::{
|
||||
Cursor, Icon, ImePurpose, Theme, UserAttentionType, Window as CoreWindow, WindowAttributes,
|
||||
WindowButtons, WindowId, WindowLevel,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Window {
|
||||
@@ -94,7 +91,7 @@ impl rwh_06::HasWindowHandle for Window {
|
||||
}
|
||||
|
||||
impl CoreWindow for Window {
|
||||
fn id(&self) -> winit_core::window::WindowId {
|
||||
fn id(&self) -> crate::window::WindowId {
|
||||
self.maybe_wait_on_main(|delegate| delegate.id())
|
||||
}
|
||||
|
||||
@@ -234,12 +231,16 @@ impl CoreWindow for Window {
|
||||
self.maybe_wait_on_main(|delegate| delegate.set_window_icon(window_icon));
|
||||
}
|
||||
|
||||
fn request_ime_update(&self, request: ImeRequest) -> Result<(), ImeRequestError> {
|
||||
self.maybe_wait_on_main(|delegate| delegate.request_ime_update(request))
|
||||
fn set_ime_cursor_area(&self, position: Position, size: Size) {
|
||||
self.maybe_wait_on_main(|delegate| delegate.set_ime_cursor_area(position, size));
|
||||
}
|
||||
|
||||
fn ime_capabilities(&self) -> Option<ImeCapabilities> {
|
||||
self.maybe_wait_on_main(|delegate| delegate.ime_capabilities())
|
||||
fn set_ime_allowed(&self, allowed: bool) {
|
||||
self.maybe_wait_on_main(|delegate| delegate.set_ime_allowed(allowed));
|
||||
}
|
||||
|
||||
fn set_ime_purpose(&self, purpose: ImePurpose) {
|
||||
self.maybe_wait_on_main(|delegate| delegate.set_ime_purpose(purpose));
|
||||
}
|
||||
|
||||
fn focus_window(&self) {
|
||||
@@ -278,10 +279,7 @@ impl CoreWindow for Window {
|
||||
self.maybe_wait_on_main(|delegate| delegate.set_cursor_position(position))
|
||||
}
|
||||
|
||||
fn set_cursor_grab(
|
||||
&self,
|
||||
mode: winit_core::window::CursorGrabMode,
|
||||
) -> Result<(), RequestError> {
|
||||
fn set_cursor_grab(&self, mode: crate::window::CursorGrabMode) -> Result<(), RequestError> {
|
||||
self.maybe_wait_on_main(|delegate| delegate.set_cursor_grab(mode))
|
||||
}
|
||||
|
||||
@@ -295,7 +293,7 @@ impl CoreWindow for Window {
|
||||
|
||||
fn drag_resize_window(
|
||||
&self,
|
||||
direction: winit_core::window::ResizeDirection,
|
||||
direction: crate::window::ResizeDirection,
|
||||
) -> Result<(), RequestError> {
|
||||
Ok(self.maybe_wait_on_main(|delegate| delegate.drag_resize_window(direction))?)
|
||||
}
|
||||
@@ -351,13 +349,13 @@ define_class!(
|
||||
impl WinitWindow {
|
||||
#[unsafe(method(canBecomeMainWindow))]
|
||||
fn can_become_main_window(&self) -> bool {
|
||||
let _entered = trace_span!("canBecomeMainWindow").entered();
|
||||
trace_scope!("canBecomeMainWindow");
|
||||
true
|
||||
}
|
||||
|
||||
#[unsafe(method(canBecomeKeyWindow))]
|
||||
fn can_become_key_window(&self) -> bool {
|
||||
let _entered = trace_span!("canBecomeKeyWindow").entered();
|
||||
trace_scope!("canBecomeKeyWindow");
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -375,7 +373,7 @@ define_class!(
|
||||
// it doesn't if window doesn't have NSWindowStyleMask::Titled
|
||||
#[unsafe(method(canBecomeKeyWindow))]
|
||||
fn can_become_key_window(&self) -> bool {
|
||||
let _entered = trace_span!("canBecomeKeyWindow").entered();
|
||||
trace_scope!("canBecomeKeyWindow");
|
||||
true
|
||||
}
|
||||
}
|
||||