mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-27 15:13:13 -04:00
Compare commits
211 Commits
dependabot
...
v0.29.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9135eb4024 | ||
|
|
23b3c127fd | ||
|
|
b343f45500 | ||
|
|
572d61f9ba | ||
|
|
87fc19826b | ||
|
|
11d1b7a980 | ||
|
|
5ca810ba8f | ||
|
|
2d1607b3f7 | ||
|
|
a32e232020 | ||
|
|
9b03bb7276 | ||
|
|
e39596151c | ||
|
|
5289b4f206 | ||
|
|
380dc4c451 | ||
|
|
6fbdbce6dd | ||
|
|
cafcaa2cdc | ||
|
|
e00204e626 | ||
|
|
a5b89bfe5a | ||
|
|
44052a093e | ||
|
|
40cee238e2 | ||
|
|
3dc5c42387 | ||
|
|
8c4a6ddcb4 | ||
|
|
5011a67f6d | ||
|
|
d621ab5018 | ||
|
|
966c033a6c | ||
|
|
1681410ca8 | ||
|
|
a82327c73f | ||
|
|
e71f765dea | ||
|
|
0738528931 | ||
|
|
8119c72d64 | ||
|
|
43f29f0481 | ||
|
|
266219f27f | ||
|
|
7449534ba2 | ||
|
|
7aa202b872 | ||
|
|
06cec065d4 | ||
|
|
f968e64ac8 | ||
|
|
a97309690e | ||
|
|
dec45ce0ff | ||
|
|
f709ac667f | ||
|
|
ecbe04caa7 | ||
|
|
7103514ae8 | ||
|
|
8b5aa33a88 | ||
|
|
7a3b486965 | ||
|
|
fc9c78cb56 | ||
|
|
525219716c | ||
|
|
7f851fe433 | ||
|
|
5dea2a4734 | ||
|
|
821fc63a9c | ||
|
|
8e9a3d2dd3 | ||
|
|
70a77b8534 | ||
|
|
a5bb6d67f7 | ||
|
|
33a2e4cebd | ||
|
|
0ee26986d8 | ||
|
|
ec41dddd0d | ||
|
|
7a872903a4 | ||
|
|
d82886bddc | ||
|
|
08edda1b0b | ||
|
|
7de33bca40 | ||
|
|
40ba9a7ce7 | ||
|
|
0656c54c3b | ||
|
|
74fcf7f9c0 | ||
|
|
0bc8f5e33a | ||
|
|
f6cc6c1472 | ||
|
|
20384d2f02 | ||
|
|
cdee616812 | ||
|
|
f58fb69446 | ||
|
|
6b445219c1 | ||
|
|
18b8569161 | ||
|
|
08b0464ac3 | ||
|
|
df2f5adfba | ||
|
|
99f86d729f | ||
|
|
d06deeecf6 | ||
|
|
e6d2fd7287 | ||
|
|
f2edd23542 | ||
|
|
70e6ddd210 | ||
|
|
f3fb27c17b | ||
|
|
75b463a368 | ||
|
|
ea8604e175 | ||
|
|
b1bd0f77fb | ||
|
|
1fded249d0 | ||
|
|
349a3e7b8c | ||
|
|
f2d277e599 | ||
|
|
8d5d612456 | ||
|
|
5788319632 | ||
|
|
976023bfc0 | ||
|
|
0f9b95814e | ||
|
|
112dcc808a | ||
|
|
4a381fb1db | ||
|
|
8339ddf368 | ||
|
|
b41f01c990 | ||
|
|
570f3101e5 | ||
|
|
3923c59fd8 | ||
|
|
75ae402a24 | ||
|
|
4385c17cbb | ||
|
|
3af256260e | ||
|
|
d9363219e1 | ||
|
|
a52a6d47ca | ||
|
|
ec83de3938 | ||
|
|
43d6eac871 | ||
|
|
1f101b2654 | ||
|
|
709929fcab | ||
|
|
220a2d32d5 | ||
|
|
c5cef46060 | ||
|
|
367a2ae057 | ||
|
|
e038597e81 | ||
|
|
27cd20739d | ||
|
|
8d9fd3d3d6 | ||
|
|
56427e47a7 | ||
|
|
c744b9aea5 | ||
|
|
ef9ed71f1b | ||
|
|
dda8053bd3 | ||
|
|
779212da33 | ||
|
|
28552c9cc1 | ||
|
|
48647b506f | ||
|
|
42243ce288 | ||
|
|
84d9bfd59e | ||
|
|
cd5c1fb724 | ||
|
|
8455f3415e | ||
|
|
c59d6bc809 | ||
|
|
4681133eca | ||
|
|
b278aa859f | ||
|
|
ee4ec43cf3 | ||
|
|
25b629f117 | ||
|
|
4b30f9ce22 | ||
|
|
2428224c09 | ||
|
|
2d9b852a95 | ||
|
|
246d53d5a1 | ||
|
|
865afd22be | ||
|
|
05130cb329 | ||
|
|
a1a6f7baf9 | ||
|
|
f160a6003c | ||
|
|
a24d092fa1 | ||
|
|
a8a0462c0d | ||
|
|
647c320ca7 | ||
|
|
5144337253 | ||
|
|
7f1aaa652d | ||
|
|
00b5de0a68 | ||
|
|
80d1e49354 | ||
|
|
07dd45f8e3 | ||
|
|
4e6ce00ec5 | ||
|
|
65c2482d74 | ||
|
|
ba2bfd064f | ||
|
|
08ad3f19e2 | ||
|
|
e3fbfd6792 | ||
|
|
c40af0062b | ||
|
|
511bf53889 | ||
|
|
7451c4b88c | ||
|
|
42ecef7b31 | ||
|
|
5d9ce7f5f4 | ||
|
|
ef5b71d658 | ||
|
|
4ab36f336c | ||
|
|
2791cbd65e | ||
|
|
03bf83f45e | ||
|
|
02870202cb | ||
|
|
c268922def | ||
|
|
61b921c466 | ||
|
|
794d0c1f73 | ||
|
|
8ce58c7053 | ||
|
|
cff9b01052 | ||
|
|
7e9dc147d8 | ||
|
|
d7827b36d3 | ||
|
|
5b90a4e194 | ||
|
|
281077a0d8 | ||
|
|
d21395bb3f | ||
|
|
f69616ac2c | ||
|
|
645b1ff00f | ||
|
|
3925281652 | ||
|
|
3bf0fa9ec8 | ||
|
|
7de2bc7ae6 | ||
|
|
3f44eb1fd9 | ||
|
|
456c735bfe | ||
|
|
973e6ad400 | ||
|
|
07652c76fb | ||
|
|
7d93c34e42 | ||
|
|
a2e1a0ac19 | ||
|
|
f1a64b3155 | ||
|
|
f8ffa314d0 | ||
|
|
e28974bc04 | ||
|
|
93f5f1ac3c | ||
|
|
a02c680a87 | ||
|
|
6bb62d0b13 | ||
|
|
fae4cbd2aa | ||
|
|
7a954c7e08 | ||
|
|
164dce2b8a | ||
|
|
0ba4283c29 | ||
|
|
62b4ba8b50 | ||
|
|
afebe2e7d1 | ||
|
|
0efcfaf5a9 | ||
|
|
d86ce9de9f | ||
|
|
7fa7cea700 | ||
|
|
36ebad3246 | ||
|
|
912c45e9f7 | ||
|
|
e2c71a4422 | ||
|
|
b50d9a0228 | ||
|
|
692f15c49f | ||
|
|
5366694db2 | ||
|
|
7962271faa | ||
|
|
78b5f2feb8 | ||
|
|
4baab2d93e | ||
|
|
79385ecd1f | ||
|
|
8d18043a3c | ||
|
|
297c3f80eb | ||
|
|
1d80005b91 | ||
|
|
fab0f62c5a | ||
|
|
d83188befd | ||
|
|
b9d89e97ed | ||
|
|
5fa4b8f003 | ||
|
|
7a4ce631bd | ||
|
|
8d5f82f0c0 | ||
|
|
08fe32eac3 | ||
|
|
1cddc96a0b | ||
|
|
84ef89eb1c |
@@ -1,9 +1,2 @@
|
|||||||
# Allow rust-analyzer and local `cargo doc` invocations to pick up unreleased changelog entries
|
[alias]
|
||||||
#
|
run-wasm = ["run", "--release", "--package", "run-wasm", "--"]
|
||||||
# Note that these flags are (intentionally) not included when building from the downloaded crate.
|
|
||||||
[build]
|
|
||||||
rustdocflags = ["--cfg=unreleased_changelogs"]
|
|
||||||
rustflags = ["--cfg=unreleased_changelogs"]
|
|
||||||
|
|
||||||
[target.wasm32-unknown-unknown]
|
|
||||||
runner = "wasm-bindgen-test-runner"
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs
|
|
||||||
|
|
||||||
# chore(rustfmt): use nightly
|
|
||||||
7b0c7b6cb2c62767ca0c73c857b299883f55a883
|
|
||||||
# Rustfmt: use `group_imports`
|
|
||||||
2665c120981af548433645c6383b3580dd8f8fc4
|
|
||||||
# Use Taplo for TOML formatting
|
|
||||||
3398ebe467c43ccfd91916c5b81ff3c68f598556
|
|
||||||
44
.github/CODEOWNERS
vendored
44
.github/CODEOWNERS
vendored
@@ -1,26 +1,40 @@
|
|||||||
|
# Core maintainers:
|
||||||
|
# - @msiglreith
|
||||||
|
# - @kchibisov
|
||||||
|
# - @madsmtm
|
||||||
|
# - @maroider
|
||||||
|
|
||||||
# Android
|
# Android
|
||||||
/winit-android @MarijnS95
|
/src/platform/android.rs @msiglreith
|
||||||
|
/src/platform_impl/android @msiglreith
|
||||||
|
|
||||||
# Apple (AppKit + UIKit)
|
# iOS
|
||||||
/winit-appkit @madsmtm
|
/src/platform/ios.rs @madsmtm
|
||||||
/winit-uikit @madsmtm
|
/src/platform_impl/ios @madsmtm
|
||||||
/winit-common/src/core_foundation @madsmtm
|
|
||||||
/winit-common/src/event_handler.rs @madsmtm
|
|
||||||
|
|
||||||
# XKB
|
# Unix
|
||||||
/winit-common/src/xkb @kchibisov
|
/src/platform_impl/linux/mod.rs @kchibisov
|
||||||
|
|
||||||
# Wayland
|
# Wayland
|
||||||
/winit-wayland @kchibisov
|
/src/platform/wayland.rs @kchibisov
|
||||||
|
/src/platform_impl/linux/wayland @kchibisov
|
||||||
|
|
||||||
# X11
|
# X11
|
||||||
/winit-x11 @kchibisov
|
/src/platform/x11.rs @kchibisov
|
||||||
|
/src/platform_impl/linux/x11 @kchibisov
|
||||||
|
|
||||||
# Web
|
# macOS
|
||||||
/winit-web @daxpedda
|
/src/platform/macos.rs @madsmtm
|
||||||
|
/src/platform_impl/macos @madsmtm
|
||||||
|
|
||||||
# Windows (Win32) (UNOWNED)
|
# Web (no maintainer)
|
||||||
#/winit-win32
|
/src/platform/web.rs @daxpedda
|
||||||
|
/src/platform_impl/web @daxpedda
|
||||||
|
|
||||||
|
# Windows
|
||||||
|
/src/platform/windows.rs @msiglreith
|
||||||
|
/src/platform_impl/windows @msiglreith
|
||||||
|
|
||||||
# Orbital (Redox OS)
|
# Orbital (Redox OS)
|
||||||
/winit-orbital @jackpot51
|
/src/platform/orbital.rs @jackpot51
|
||||||
|
/src/platform_impl/orbital @jackpot51
|
||||||
|
|||||||
4
.github/ISSUE_TEMPLATE/blank_issue.md
vendored
4
.github/ISSUE_TEMPLATE/blank_issue.md
vendored
@@ -1,4 +0,0 @@
|
|||||||
---
|
|
||||||
name: Blank Issue
|
|
||||||
about: Create a blank issue.
|
|
||||||
---
|
|
||||||
36
.github/ISSUE_TEMPLATE/bug_android.yml
vendored
36
.github/ISSUE_TEMPLATE/bug_android.yml
vendored
@@ -1,36 +0,0 @@
|
|||||||
name: Android bug
|
|
||||||
description: Create an Android-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - android
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: device
|
|
||||||
attributes:
|
|
||||||
label: Device and Android version
|
|
||||||
description: Which devices and Android versions are you seeing the problem on?
|
|
||||||
placeholder: |
|
|
||||||
Samsung Galaxy Z running Android Pie (API level 28),
|
|
||||||
Samsung Galaxy Z running Android 14 (API level 34),
|
|
||||||
Pixel 8 running Android 14 (API level 34)
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
37
.github/ISSUE_TEMPLATE/bug_appkit.yml
vendored
37
.github/ISSUE_TEMPLATE/bug_appkit.yml
vendored
@@ -1,37 +0,0 @@
|
|||||||
name: macOS bug
|
|
||||||
description: Create a macOS-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - appkit
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: os-version
|
|
||||||
attributes:
|
|
||||||
label: macOS version
|
|
||||||
description: What version of macOS are you using? Please paste in the output of `sw_vers`.
|
|
||||||
placeholder: |
|
|
||||||
ProductName: macOS
|
|
||||||
ProductVersion: 14.2.1
|
|
||||||
BuildVersion: 23C71
|
|
||||||
render: shell
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
37
.github/ISSUE_TEMPLATE/bug_uikit.yml
vendored
37
.github/ISSUE_TEMPLATE/bug_uikit.yml
vendored
@@ -1,37 +0,0 @@
|
|||||||
name: iOS bug
|
|
||||||
description: Create an iOS/UIKit-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - uikit
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: device
|
|
||||||
attributes:
|
|
||||||
label: Device and iOS version
|
|
||||||
description: Which devices and iOS versions are you seeing the problem on?
|
|
||||||
placeholder: |
|
|
||||||
iPhone 15 running iOS 14.0,
|
|
||||||
iPhone 15 running iOS 17.0,
|
|
||||||
MacBook Pro M2 Mac Catalyst running macOS 14.2,
|
|
||||||
iPhone simulator running iOS 17.0
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
41
.github/ISSUE_TEMPLATE/bug_wayland.yml
vendored
41
.github/ISSUE_TEMPLATE/bug_wayland.yml
vendored
@@ -1,41 +0,0 @@
|
|||||||
name: Wayland bug
|
|
||||||
description: Create a Wayland-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - wayland
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: debug
|
|
||||||
attributes:
|
|
||||||
label: Debugging output
|
|
||||||
description: Output of a binary run with `WAYLAND_DEBUG=1`
|
|
||||||
placeholder: |
|
|
||||||
[1234.5678] -> wl_display@1.get_registry(new id wl_registry@2)
|
|
||||||
[1234.5678] -> wl_display@1.sync(new id wl_callback@3)
|
|
||||||
...
|
|
||||||
render: shell
|
|
||||||
- type: checkboxes
|
|
||||||
attributes:
|
|
||||||
label: Window isn't shown unless you draw
|
|
||||||
options:
|
|
||||||
- label: I understand that windows aren't shown on Wayland unless I draw and present to them.
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
51
.github/ISSUE_TEMPLATE/bug_web.yml
vendored
51
.github/ISSUE_TEMPLATE/bug_web.yml
vendored
@@ -1,51 +0,0 @@
|
|||||||
name: Web bug
|
|
||||||
description: Create a Web-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - web
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: dropdown
|
|
||||||
id: browsers
|
|
||||||
attributes:
|
|
||||||
label: Tested browsers
|
|
||||||
description: What browsers are you seeing the problem on?
|
|
||||||
options:
|
|
||||||
- Firefox
|
|
||||||
- Chrome
|
|
||||||
- Microsoft Edge
|
|
||||||
- Safari 13
|
|
||||||
- Safari 14
|
|
||||||
- Safari 15
|
|
||||||
- Safari 16
|
|
||||||
- Safari 17
|
|
||||||
- Safari (newer than listed)
|
|
||||||
multiple: true
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: device
|
|
||||||
attributes:
|
|
||||||
label: Tested devices
|
|
||||||
description: Which device(s) are you using?
|
|
||||||
placeholder: 'iPhone 15, Lenovo ThinkPad X1, MacBook Pro M2, Samsung Galaxy Z, ...'
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
35
.github/ISSUE_TEMPLATE/bug_win32.yml
vendored
35
.github/ISSUE_TEMPLATE/bug_win32.yml
vendored
@@ -1,35 +0,0 @@
|
|||||||
name: Windows bug
|
|
||||||
description: Create a Windows-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - win32
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: os-version
|
|
||||||
attributes:
|
|
||||||
label: Windows version
|
|
||||||
description: What version of Windows are you using? Please paste in the output of the `ver` command.
|
|
||||||
placeholder: |
|
|
||||||
Microsoft Windows [Version 10.0.19042.2251]
|
|
||||||
render: shell
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
32
.github/ISSUE_TEMPLATE/bug_x11.yml
vendored
32
.github/ISSUE_TEMPLATE/bug_x11.yml
vendored
@@ -1,32 +0,0 @@
|
|||||||
name: X11 bug
|
|
||||||
description: Create a X11-specific bug report
|
|
||||||
labels:
|
|
||||||
- B - bug
|
|
||||||
- DS - x11
|
|
||||||
body:
|
|
||||||
- type: markdown
|
|
||||||
attributes:
|
|
||||||
value: |
|
|
||||||
Thanks for taking the time to fill out this bug report!
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem you're having
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: os-info
|
|
||||||
attributes:
|
|
||||||
label: OS and window mananger
|
|
||||||
description: Which operating system and window manager are you using?
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: textarea
|
|
||||||
id: winit-version
|
|
||||||
attributes:
|
|
||||||
label: Winit version
|
|
||||||
description: What version of Winit are you using?
|
|
||||||
placeholder: 0.29.11
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
5
.github/ISSUE_TEMPLATE/config.yml
vendored
5
.github/ISSUE_TEMPLATE/config.yml
vendored
@@ -1,5 +0,0 @@
|
|||||||
blank_issues_enabled: true
|
|
||||||
contact_links:
|
|
||||||
- name: Question
|
|
||||||
url: https://matrix.to/#/#rust-windowing:matrix.org
|
|
||||||
about: Please ask questions on the Matrix channel.
|
|
||||||
26
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
26
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
@@ -1,26 +0,0 @@
|
|||||||
name: Feature request
|
|
||||||
description: Propose a new feature
|
|
||||||
labels:
|
|
||||||
- S - enhancement
|
|
||||||
body:
|
|
||||||
- type: textarea
|
|
||||||
id: description
|
|
||||||
attributes:
|
|
||||||
label: Description
|
|
||||||
description: Description of the problem does this solve or what need does it fill? Please be mindful of the [scope](https://github.com/rust-windowing/winit/blob/master/FEATURES.md) of Winit.
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: dropdown
|
|
||||||
id: platforms
|
|
||||||
attributes:
|
|
||||||
label: Relevant platforms
|
|
||||||
description: On which platforms is this feature relevant?
|
|
||||||
options:
|
|
||||||
- Windows
|
|
||||||
- macOS
|
|
||||||
- Wayland
|
|
||||||
- X11
|
|
||||||
- Web
|
|
||||||
- iOS
|
|
||||||
- Android
|
|
||||||
multiple: true
|
|
||||||
3
.github/PULL_REQUEST_TEMPLATE.md
vendored
3
.github/PULL_REQUEST_TEMPLATE.md
vendored
@@ -1,4 +1,5 @@
|
|||||||
- [ ] Tested on all platforms changed
|
- [ ] Tested on all platforms changed
|
||||||
- [ ] Added an entry to the `changelog` module if knowledge of this change could be valuable to users
|
- [ ] Added an entry to `CHANGELOG.md` if knowledge of this change could be valuable to users
|
||||||
- [ ] Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
|
- [ ] Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
|
||||||
- [ ] Created or updated an example program if it would help users understand this functionality
|
- [ ] Created or updated an example program if it would help users understand this functionality
|
||||||
|
- [ ] Updated [feature matrix](https://github.com/rust-windowing/winit/blob/master/FEATURES.md), if new features were added or implemented
|
||||||
|
|||||||
22
.github/dependabot.yaml
vendored
22
.github/dependabot.yaml
vendored
@@ -1,22 +0,0 @@
|
|||||||
version: 2
|
|
||||||
|
|
||||||
updates:
|
|
||||||
- package-ecosystem: github-actions
|
|
||||||
directory: /
|
|
||||||
schedule:
|
|
||||||
interval: daily
|
|
||||||
groups:
|
|
||||||
github-actions:
|
|
||||||
patterns:
|
|
||||||
- "*"
|
|
||||||
|
|
||||||
- package-ecosystem: npm
|
|
||||||
directory: src/platform_impl/web/script
|
|
||||||
schedule:
|
|
||||||
interval: daily
|
|
||||||
groups:
|
|
||||||
github-actions:
|
|
||||||
patterns:
|
|
||||||
- '*'
|
|
||||||
labels:
|
|
||||||
- "DS - web"
|
|
||||||
237
.github/workflows/ci.yml
vendored
237
.github/workflows/ci.yml
vendored
@@ -10,44 +10,13 @@ jobs:
|
|||||||
name: Check formatting
|
name: Check formatting
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: taiki-e/checkout-action@v1
|
- uses: actions/checkout@v3
|
||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
with:
|
with:
|
||||||
components: rustfmt
|
components: rustfmt
|
||||||
- name: Check Formatting
|
- name: Check Formatting
|
||||||
run: cargo fmt -- --check
|
run: cargo fmt -- --check
|
||||||
|
|
||||||
taplo:
|
|
||||||
name: Taplo
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: taiki-e/checkout-action@v1
|
|
||||||
- name: Install Taplo
|
|
||||||
uses: taiki-e/install-action@v2
|
|
||||||
with:
|
|
||||||
tool: taplo-cli
|
|
||||||
- name: Run Taplo
|
|
||||||
run: taplo fmt --check
|
|
||||||
|
|
||||||
typos:
|
|
||||||
name: Check for typos
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 30
|
|
||||||
steps:
|
|
||||||
- uses: taiki-e/checkout-action@v1
|
|
||||||
- uses: taiki-e/install-action@v2
|
|
||||||
with:
|
|
||||||
tool: typos-cli
|
|
||||||
- name: run typos
|
|
||||||
run: typos
|
|
||||||
- name: Typos info
|
|
||||||
if: failure()
|
|
||||||
run: |
|
|
||||||
echo 'To fix typos, please run `typos -w`'
|
|
||||||
echo 'To check for a diff, run `typos`'
|
|
||||||
echo 'You can find typos here: https://crates.io/crates/typos'
|
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
name: Test ${{ matrix.toolchain }} ${{ matrix.platform.name }}
|
name: Test ${{ matrix.toolchain }} ${{ matrix.platform.name }}
|
||||||
runs-on: ${{ matrix.platform.os }}
|
runs-on: ${{ matrix.platform.os }}
|
||||||
@@ -55,7 +24,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
toolchain: [stable, nightly, '1.85']
|
toolchain: [stable, nightly, '1.65.0']
|
||||||
platform:
|
platform:
|
||||||
# Note: Make sure that we test all the `docs.rs` targets defined in Cargo.toml!
|
# 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, }
|
- { name: 'Windows 64bit MSVC', target: x86_64-pc-windows-msvc, os: windows-latest, }
|
||||||
@@ -66,42 +35,19 @@ jobs:
|
|||||||
- { name: 'Linux 64bit', target: x86_64-unknown-linux-gnu, os: ubuntu-latest, }
|
- { 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: '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: '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: 'Redox OS', target: x86_64-unknown-redox, os: ubuntu-latest, }
|
||||||
- { name: 'macOS x86_64', target: x86_64-apple-darwin, os: macos-latest, }
|
- { name: 'macOS', target: x86_64-apple-darwin, os: macos-latest, }
|
||||||
- { name: 'macOS Aarch64', target: aarch64-apple-darwin, os: macos-latest, }
|
|
||||||
- { name: 'iOS x86_64', target: x86_64-apple-ios, os: macos-latest, }
|
- { name: 'iOS x86_64', target: x86_64-apple-ios, os: macos-latest, }
|
||||||
- { name: 'iOS Aarch64', target: aarch64-apple-ios, os: macos-latest, }
|
- { name: 'iOS Aarch64', target: aarch64-apple-ios, os: macos-latest, }
|
||||||
- { name: 'Web', target: wasm32-unknown-unknown, os: ubuntu-latest, }
|
- { name: 'web', target: wasm32-unknown-unknown, os: ubuntu-latest, }
|
||||||
exclude:
|
exclude:
|
||||||
# Web on nightly needs extra arguments
|
|
||||||
|
|
||||||
- toolchain: nightly
|
|
||||||
platform: { name: 'Web' }
|
|
||||||
# Rustup is broken.
|
|
||||||
- toolchain: nightly
|
|
||||||
platform: { name: 'Windows 32bit GNU' }
|
|
||||||
# Android is tested on stable-3
|
# Android is tested on stable-3
|
||||||
- toolchain: '1.85'
|
- toolchain: '1.65.0'
|
||||||
platform: { name: 'Android' }
|
platform: { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package=winit --features=android-native-activity', cmd: 'apk --' }
|
||||||
# Redox OS doesn't follow MSRV
|
|
||||||
- toolchain: '1.85'
|
|
||||||
platform: { name: 'Redox OS' }
|
|
||||||
include:
|
include:
|
||||||
- toolchain: '1.85'
|
- toolchain: '1.69.0'
|
||||||
platform: { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package winit --features=android-native-activity', cmd: 'apk -- ' }
|
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'
|
|
||||||
platform: {
|
|
||||||
name: 'Web Atomic',
|
|
||||||
target: wasm32-unknown-unknown,
|
|
||||||
os: ubuntu-latest,
|
|
||||||
options: '-Zbuild-std=panic_abort,std',
|
|
||||||
test-options: -Zdoctest-xcompile,
|
|
||||||
rustflags: '-Ctarget-feature=+atomics,+bulk-memory',
|
|
||||||
components: rust-src,
|
|
||||||
}
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
# Set more verbose terminal output
|
# Set more verbose terminal output
|
||||||
@@ -109,22 +55,21 @@ jobs:
|
|||||||
RUST_BACKTRACE: 1
|
RUST_BACKTRACE: 1
|
||||||
|
|
||||||
# Faster compilation and error on warnings
|
# Faster compilation and error on warnings
|
||||||
RUSTFLAGS: '--codegen=debuginfo=0 --deny=warnings ${{ matrix.platform.rustflags }}'
|
RUSTFLAGS: '--codegen=debuginfo=0 --deny=warnings'
|
||||||
RUSTDOCFLAGS: ${{ matrix.platform.rustflags }}
|
RUSTDOCFLAGS: '--deny=warnings'
|
||||||
|
|
||||||
OPTIONS: --target=${{ matrix.platform.target }} ${{ matrix.platform.options }}
|
OPTIONS: --target=${{ matrix.platform.target }} ${{ matrix.platform.options }}
|
||||||
TEST_OPTIONS: ${{ matrix.platform.test-options }}
|
|
||||||
CMD: ${{ matrix.platform.cmd }}
|
CMD: ${{ matrix.platform.cmd }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: taiki-e/checkout-action@v1
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Restore cache of cargo folder
|
- name: Restore cache of cargo folder
|
||||||
# We use `restore` and later `save`, so that we can create the key after
|
# We use `restore` and later `save`, so that we can create the key after
|
||||||
# the cache has been downloaded.
|
# the cache has been downloaded.
|
||||||
#
|
#
|
||||||
# This could be avoided if we added Cargo.lock to the repository.
|
# This could be avoided if we added Cargo.lock to the repository.
|
||||||
uses: actions/cache/restore@v6
|
uses: actions/cache/restore@v3
|
||||||
with:
|
with:
|
||||||
# https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci
|
# https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci
|
||||||
path: |
|
path: |
|
||||||
@@ -136,7 +81,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Generate lockfile
|
- name: Generate lockfile
|
||||||
# Also updates the crates.io index
|
# 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
|
||||||
|
|
||||||
- name: Install GCC Multilib
|
- name: Install GCC Multilib
|
||||||
if: (matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')
|
if: (matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')
|
||||||
@@ -145,7 +90,7 @@ jobs:
|
|||||||
- name: Cache cargo-apk
|
- name: Cache cargo-apk
|
||||||
if: contains(matrix.platform.target, 'android')
|
if: contains(matrix.platform.target, 'android')
|
||||||
id: cargo-apk-cache
|
id: cargo-apk-cache
|
||||||
uses: actions/cache@v6
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/.cargo/bin/cargo-apk
|
path: ~/.cargo/bin/cargo-apk
|
||||||
# Change this key if we update the required cargo-apk version
|
# Change this key if we update the required cargo-apk version
|
||||||
@@ -160,128 +105,55 @@ jobs:
|
|||||||
if: contains(matrix.platform.target, 'android') && (steps.cargo-apk-cache.outputs.cache-hit != 'true')
|
if: contains(matrix.platform.target, 'android') && (steps.cargo-apk-cache.outputs.cache-hit != 'true')
|
||||||
run: cargo install cargo-apk --version=^0.9.7 --locked
|
run: cargo install cargo-apk --version=^0.9.7 --locked
|
||||||
|
|
||||||
- uses: taiki-e/cache-cargo-install-action@v3
|
|
||||||
if: contains(matrix.platform.target, 'wasm32') && matrix.toolchain == 'nightly'
|
|
||||||
with:
|
|
||||||
tool: wasm-bindgen-cli
|
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@master
|
- uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: ${{ matrix.toolchain }}${{ matrix.platform.host }}
|
toolchain: ${{ matrix.toolchain }}${{ matrix.platform.host }}
|
||||||
targets: ${{ matrix.platform.target }}
|
targets: ${{ matrix.platform.target }}
|
||||||
components: clippy, ${{ matrix.platform.components }}
|
components: clippy
|
||||||
|
|
||||||
- name: Check documentation
|
- name: Check documentation
|
||||||
run: cargo doc --no-deps $OPTIONS --document-private-items
|
run: cargo doc --no-deps $OPTIONS --document-private-items
|
||||||
env:
|
|
||||||
RUSTDOCFLAGS: '--deny=warnings ${{ matrix.platform.rustflags }}'
|
|
||||||
|
|
||||||
- name: Build crate
|
- name: Build crate
|
||||||
run: cargo $CMD build $OPTIONS
|
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'
|
|
||||||
run: cargo test -p dpi
|
|
||||||
|
|
||||||
- name: Check dpi crate (no_std)
|
|
||||||
if: >
|
|
||||||
contains(matrix.platform.name, 'Linux 64bit') &&
|
|
||||||
matrix.toolchain != '1.85'
|
|
||||||
run: cargo check -p dpi --no-default-features
|
|
||||||
|
|
||||||
- name: Build tests
|
- name: Build tests
|
||||||
if: >
|
if: >
|
||||||
!contains(matrix.platform.target, 'redox') &&
|
!contains(matrix.platform.target, 'redox') &&
|
||||||
matrix.toolchain != '1.85'
|
matrix.toolchain != '1.65.0'
|
||||||
run: cargo $CMD test --no-run $OPTIONS
|
run: cargo $CMD test --no-run $OPTIONS
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
if: >
|
if: >
|
||||||
!contains(matrix.platform.target, 'android') &&
|
!contains(matrix.platform.target, 'android') &&
|
||||||
!contains(matrix.platform.target, 'ios') &&
|
!contains(matrix.platform.target, 'ios') &&
|
||||||
(!contains(matrix.platform.target, 'wasm32') || matrix.toolchain == 'nightly') &&
|
!contains(matrix.platform.target, 'wasm32') &&
|
||||||
!contains(matrix.platform.target, 'redox') &&
|
!contains(matrix.platform.target, 'redox') &&
|
||||||
matrix.toolchain != '1.85'
|
matrix.toolchain != '1.65.0'
|
||||||
run: cargo $CMD test $OPTIONS
|
run: cargo $CMD test $OPTIONS
|
||||||
|
|
||||||
- name: Lint with clippy
|
- name: Lint with clippy
|
||||||
if: (matrix.toolchain == 'stable') && !contains(matrix.platform.options, '--no-default-features')
|
if: (matrix.toolchain == 'stable') && !contains(matrix.platform.options, '--no-default-features')
|
||||||
run: cargo clippy --all-targets $OPTIONS $TEST_OPTIONS -- -Dwarnings
|
run: cargo clippy --all-targets $OPTIONS -- -Dwarnings
|
||||||
|
|
||||||
- name: Build tests with serde enabled
|
- name: Build tests with serde enabled
|
||||||
if: >
|
if: >
|
||||||
!contains(matrix.platform.target, 'redox') &&
|
!contains(matrix.platform.target, 'redox') &&
|
||||||
matrix.toolchain != '1.85'
|
matrix.toolchain != '1.65.0'
|
||||||
run: cargo $CMD test --no-run $OPTIONS $TEST_OPTIONS --features serde
|
run: cargo $CMD test --no-run $OPTIONS --features serde
|
||||||
|
|
||||||
- name: Run tests with serde enabled
|
- name: Run tests with serde enabled
|
||||||
if: >
|
if: >
|
||||||
!contains(matrix.platform.target, 'android') &&
|
!contains(matrix.platform.target, 'android') &&
|
||||||
!contains(matrix.platform.target, 'ios') &&
|
!contains(matrix.platform.target, 'ios') &&
|
||||||
(!contains(matrix.platform.target, 'wasm32') || matrix.toolchain == 'nightly') &&
|
!contains(matrix.platform.target, 'wasm32') &&
|
||||||
!contains(matrix.platform.target, 'redox') &&
|
!contains(matrix.platform.target, 'redox') &&
|
||||||
matrix.toolchain != '1.85'
|
matrix.toolchain != '1.65.0'
|
||||||
run: cargo $CMD test $OPTIONS $TEST_OPTIONS --features serde
|
run: cargo $CMD test $OPTIONS --features serde
|
||||||
|
|
||||||
- name: Check docs.rs documentation
|
|
||||||
if: matrix.toolchain == 'nightly'
|
|
||||||
run: cargo doc --no-deps $OPTIONS --features=serde,mint,android-native-activity
|
|
||||||
env:
|
|
||||||
RUSTDOCFLAGS: '--deny=warnings ${{ matrix.platform.rustflags }} --cfg=docsrs --cfg=unreleased_changelogs'
|
|
||||||
|
|
||||||
# See restore step above
|
# See restore step above
|
||||||
- name: Save cache of cargo folder
|
- name: Save cache of cargo folder
|
||||||
uses: actions/cache/save@v6
|
uses: actions/cache/save@v3
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.cargo/registry/index/
|
~/.cargo/registry/index/
|
||||||
@@ -290,45 +162,26 @@ jobs:
|
|||||||
key: cargo-${{ matrix.toolchain }}-${{ matrix.platform.name }}-${{ hashFiles('Cargo.lock') }}
|
key: cargo-${{ matrix.toolchain }}-${{ matrix.platform.name }}-${{ hashFiles('Cargo.lock') }}
|
||||||
|
|
||||||
cargo-deny:
|
cargo-deny:
|
||||||
name: Run cargo-deny
|
name: Run cargo-deny on ${{ matrix.platform.name }}
|
||||||
runs-on: ubuntu-latest
|
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: x86_64-apple-darwin }
|
||||||
|
- { name: 'Redox OS', target: x86_64-unknown-redox }
|
||||||
|
- { name: 'web', target: wasm32-unknown-unknown }
|
||||||
|
- { name: 'Windows', target: x86_64-pc-windows-gnu }
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: taiki-e/checkout-action@v1
|
- uses: actions/checkout@v3
|
||||||
- uses: EmbarkStudios/cargo-deny-action@v2
|
- uses: EmbarkStudios/cargo-deny-action@v1
|
||||||
with:
|
with:
|
||||||
|
command: check
|
||||||
log-level: error
|
log-level: error
|
||||||
|
arguments: --all-features --target ${{ matrix.platform.target }}
|
||||||
eslint:
|
|
||||||
name: ESLint
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
defaults:
|
|
||||||
run:
|
|
||||||
working-directory: ./winit-web/src/script
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: taiki-e/checkout-action@v1
|
|
||||||
- name: Setup NPM
|
|
||||||
run: npm install
|
|
||||||
- name: Run ESLint
|
|
||||||
run: npx eslint@9.38.0
|
|
||||||
|
|
||||||
swc:
|
|
||||||
name: Minimize JavaScript
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
defaults:
|
|
||||||
run:
|
|
||||||
working-directory: ./winit-web/src/script
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: taiki-e/checkout-action@v1
|
|
||||||
- name: Install SWC
|
|
||||||
run: sudo npm i -g @swc/cli
|
|
||||||
- name: Run SWC
|
|
||||||
run: |
|
|
||||||
swc . --ignore node_modules,**/*.d.ts --only **/*.ts -d . --out-file-extension min.js
|
|
||||||
- name: Check for diff
|
|
||||||
run: |
|
|
||||||
[[ -z $(git status -s) ]]
|
|
||||||
|
|||||||
50
.github/workflows/docs.yml
vendored
50
.github/workflows/docs.yml
vendored
@@ -1,50 +0,0 @@
|
|||||||
name: Docs
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [master]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
docs:
|
|
||||||
name: Documentation
|
|
||||||
environment:
|
|
||||||
name: github-pages
|
|
||||||
url: ${{ steps.deployment.outputs.page_url }}winit
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
pages: write
|
|
||||||
id-token: write
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v7
|
|
||||||
|
|
||||||
- uses: dtolnay/rust-toolchain@master
|
|
||||||
with:
|
|
||||||
toolchain: nightly
|
|
||||||
|
|
||||||
- name: Run Rustdoc
|
|
||||||
env:
|
|
||||||
RUSTDOCFLAGS: --crate-version master --cfg=docsrs --cfg=unreleased_changelogs
|
|
||||||
run: |
|
|
||||||
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
|
|
||||||
|
|
||||||
- name: Fix permissions
|
|
||||||
run: |
|
|
||||||
chmod -c -R +rX "target/doc" | while read line; do
|
|
||||||
echo "::warning title=Invalid file permissions automatically fixed::$line"
|
|
||||||
done
|
|
||||||
|
|
||||||
- name: Upload artifact
|
|
||||||
uses: actions/upload-pages-artifact@v5
|
|
||||||
with:
|
|
||||||
path: target/doc
|
|
||||||
|
|
||||||
- name: Deploy to GitHub Pages
|
|
||||||
id: deployment
|
|
||||||
uses: actions/deploy-pages@v5
|
|
||||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -3,8 +3,8 @@ target/
|
|||||||
rls/
|
rls/
|
||||||
.vscode/
|
.vscode/
|
||||||
*~
|
*~
|
||||||
|
*.wasm
|
||||||
|
*.ts
|
||||||
|
*.js
|
||||||
#*#
|
#*#
|
||||||
.DS_Store
|
.DS_Store
|
||||||
# NPM package used to run ESLint.
|
|
||||||
/src/platform_impl/web/script/node_modules
|
|
||||||
/src/platform_impl/web/script/package-lock.json
|
|
||||||
|
|||||||
1190
CHANGELOG.md
1190
CHANGELOG.md
File diff suppressed because it is too large
Load Diff
@@ -1,6 +0,0 @@
|
|||||||
# Code of Conduct
|
|
||||||
|
|
||||||
The `rust-windowing` project adheres to the [Rust Code of Conduct]. This
|
|
||||||
describes the minimum behavior expected from all contributors.
|
|
||||||
|
|
||||||
[Rust Code of Conduct]: https://www.rust-lang.org/policies/code-of-conduct
|
|
||||||
179
CONTRIBUTING.md
179
CONTRIBUTING.md
@@ -1,162 +1,71 @@
|
|||||||
# Contribution Guidelines
|
# Winit Contributing Guidelines
|
||||||
|
|
||||||
This document contains guidelines for contributing code to winit. It has to be
|
## Scope
|
||||||
followed in order for your patch to be approved and applied.
|
[See `FEATURES.md`](./FEATURES.md). When requesting or implementing a new Winit feature, you should
|
||||||
|
consider whether or not it's directly related to window creation or input handling. If it isn't, it
|
||||||
|
may be worth creating a separate crate that extends Winit's API to add that functionality.
|
||||||
|
|
||||||
## Contributing
|
|
||||||
|
|
||||||
Anyone can contribute to winit, however given that it's a cross platform
|
## Reporting an issue
|
||||||
windowing toolkit getting certain changes incorporated could be challenging.
|
|
||||||
|
|
||||||
To save your time it's wise to check already opened [pull requests][prs] and
|
When reporting an issue, in order to help the maintainers understand what the problem is, please make
|
||||||
[issues][issues]. In general, bug fixes and missing implementations are always
|
your description of the issue as detailed as possible:
|
||||||
accepted, however larger new API proposals should go into the issue first. When
|
|
||||||
in doubt contact us on [Matrix][matrix] or via opening an issue.
|
|
||||||
|
|
||||||
### Windows
|
- if it is a bug, please provide a clear explanation of what happens, what should happen, and how to
|
||||||
|
reproduce the issue, ideally by providing a minimal program exhibiting the problem
|
||||||
|
- if it is a feature request, please provide a clear argumentation about why you believe this feature
|
||||||
|
should be supported by winit
|
||||||
|
|
||||||
To run the examples on Windows, you must have symlinks enabled, see
|
## Making a pull request
|
||||||
[this][git-windows-symlinks].
|
|
||||||
|
|
||||||
[git-windows-symlinks]: https://gitforwindows.org/symbolic-links.html
|
When making a code contribution to winit, before opening your pull request, please make sure that:
|
||||||
|
|
||||||
### Submitting your work and handling review
|
- your patch builds with Winit's minimal supported rust version - Rust 1.65.
|
||||||
|
- you tested your modifications on all the platforms impacted, or if not possible, detail which platforms
|
||||||
|
were not tested, and what should be tested, so that a maintainer or another contributor can test them
|
||||||
|
- you updated any relevant documentation in winit
|
||||||
|
- you left comments in your code explaining any part that is not straightforward, so that the
|
||||||
|
maintainers and future contributors don't have to try to guess what your code is supposed to do
|
||||||
|
- your PR adds an entry to the changelog file if the introduced change is relevant to winit users.
|
||||||
|
|
||||||
All patches have to be sent on Github as [pull requests][prs]. To simplify your
|
You needn't worry about the added entry causing conflicts, the maintainer that merges the PR will
|
||||||
life during review it's recommended to check the "give contributors write access
|
handle those for you when merging (see below).
|
||||||
to the branch" checkbox.
|
- if your PR affects the platform compatibility of one or more features or adds another feature, the
|
||||||
|
relevant sections in [`FEATURES.md`](https://github.com/rust-windowing/winit/blob/master/FEATURES.md#features)
|
||||||
|
should be updated.
|
||||||
|
|
||||||
We use unstable Rustfmt options across the project, so please run
|
Once your PR is open, you can ask for a review by a maintainer of your platform. Winit's merging policy
|
||||||
`cargo +nightly fmt` before submitting your work. If you are unable to do so,
|
is that a PR must be approved by at least two maintainers of winit before being merged, including
|
||||||
the maintainers can do it for you before merging, just state so in your pull
|
at least a maintainer of the platform (a maintainer making a PR themselves counts as approving it).
|
||||||
request description. For details on how to use nightly, consult [the
|
|
||||||
documentation][toolchains].
|
|
||||||
|
|
||||||
When editing markdown files (`.md`) they must be wrapped at 80 characters.
|
Once your PR is deemed ready, the merging maintainer will take care of resolving conflicts in
|
||||||
|
`CHANGELOG.md` (but you must resolve other conflicts yourself). Doing this requires that you check the
|
||||||
|
"give contributors write access to the branch" checkbox when creating the PR.
|
||||||
|
|
||||||
[toolchains]: https://rust-lang.github.io/rustup/concepts/toolchains.html
|
## Maintainers & Testers
|
||||||
|
|
||||||
#### Handling review
|
The current maintainers are listed in the [CODEOWNERS](.github/CODEOWNERS) file.
|
||||||
|
|
||||||
During the review process certain events could require an action from your side,
|
If you are interested in being pinged when testing is needed for a specific platform, please add yourself to the [Testers and Contributors](https://github.com/rust-windowing/winit/wiki/Testers-and-Contributors) table!
|
||||||
common patterns and reactions are described below.
|
|
||||||
|
|
||||||
_Event:_ The CI fails to build, but it looks like it is not your fault. Not
|
## Release process
|
||||||
communicating so could result in maintainers not looking into your patch,
|
|
||||||
since they may assume that you're still working on it.\
|
|
||||||
_Desired behaviour:_ Write a message saying roughly the following "The CI
|
|
||||||
failure is unrelated", so that the maintainers will fix it for you.
|
|
||||||
|
|
||||||
_Event:_ Maintainer requested changes to your PR.\
|
|
||||||
_Desired behavior:_ Once you address the request, you should re-request a review
|
|
||||||
with GitHub's UI. If you don't agree with what maintainer suggested, you
|
|
||||||
should state your objections and re-request the review. That will indicate that
|
|
||||||
the ball is on maintainer's side.
|
|
||||||
|
|
||||||
_Event:_ You've opened a PR, but maintainer shortly after commented that they
|
|
||||||
want to work on that themselves.\
|
|
||||||
_Desired behavior:_ Discuss with the maintainer regarding their plans if they
|
|
||||||
were not outlined in the initial response, because such response means that they
|
|
||||||
are not interested in reviewing your code. Such thing could happen when
|
|
||||||
underestimating complexity of the task you're solving or when your patch
|
|
||||||
mandate certain downstream designs. In general, the maintainer will likely
|
|
||||||
close your PR in order to prevent work being done on it.
|
|
||||||
|
|
||||||
[prs]: https://github.com/rust-windowing/winit/pulls
|
|
||||||
[issues]: https://github.com/rust-windowing/winit/issues
|
|
||||||
[matrix]: https://matrix.to/#/#rust-windowing:matrix.org
|
|
||||||
|
|
||||||
## Maintainers
|
|
||||||
|
|
||||||
Winit has plenty of maintainers with different backgrounds, different time
|
|
||||||
available to work on Winit, and reasons to be winit maintainer in the first
|
|
||||||
place. To ensure that Winit's code quality does not decrease over time and to
|
|
||||||
make it easier to teach new maintainers the "winit way of doing things" the
|
|
||||||
common policies and routines are defined in this section.
|
|
||||||
|
|
||||||
The current maintainers for each platform are listed in [this file][CODEOWNERS].
|
|
||||||
|
|
||||||
### Contributions handling
|
|
||||||
|
|
||||||
The maintainers must ensure that the external contributions meet Winit's
|
|
||||||
quality standards. If it's not, it **is the maintainer's responsibility** to
|
|
||||||
bring it on par, which includes:
|
|
||||||
|
|
||||||
- Ensure that formatting is consistent and `CHANGELOG` messages are clear
|
|
||||||
for the end users.
|
|
||||||
- Improve the commit message, so it'll be easier for other maintainers to
|
|
||||||
understand the motivation without going through all the discussions on the
|
|
||||||
particular patch/PR.
|
|
||||||
- Ensure that the proposed patch doesn't break platform parity. If the
|
|
||||||
breakage is desired by contributor, an issue should be opened to discuss
|
|
||||||
with other maintainers before merging.
|
|
||||||
- Always fix CI issues before merging if they don't originate from the
|
|
||||||
submitted work.
|
|
||||||
|
|
||||||
However, maintainers should give some leeway for external contributors, so they
|
|
||||||
don't feel discouraged contributing, for example:
|
|
||||||
|
|
||||||
- Suggest a patch to resolve style issues, if it's the only issue with the
|
|
||||||
submitted work. Keep in mind that pushing the resolution yourself is not
|
|
||||||
desired, because the contributor might not agree with what you did.
|
|
||||||
- Be more explicit on how things should be done if you don't like the
|
|
||||||
approach.
|
|
||||||
- Suggest to finish the PR for them if they're absent for a while and you need
|
|
||||||
the proposed changes to move forward with something. In such cases the
|
|
||||||
maintainer must preserve attribution with `Co-authored-by`, `Suggested-by`,
|
|
||||||
or keep the original committer.
|
|
||||||
- Rebase their work for them when massive changes to the Winit codebase were
|
|
||||||
introduced.
|
|
||||||
|
|
||||||
When reviewing code of other maintainers all of the above is on the maintainer
|
|
||||||
who submitted the patch. Interested maintainers could help push the work over
|
|
||||||
the finish line, but teaching other maintainers should be preferred.
|
|
||||||
|
|
||||||
For a _regular_ contributor to winit, the maintainer should slowly start
|
|
||||||
requiring contributor to match *maintainer* quality standards when writing
|
|
||||||
patches and commit messages.
|
|
||||||
|
|
||||||
### Contributing
|
|
||||||
|
|
||||||
When submitting a patch, the maintainer should follow the general contributing
|
|
||||||
guidelines, however greater attention to detail is expected in this case.
|
|
||||||
|
|
||||||
To make life simpler for other maintainers it's suggested to create your branch
|
|
||||||
under the project repository instead of your own fork. The naming scheme is
|
|
||||||
`github_user_name/branch_name`. Doing so will make your work easier to rebase
|
|
||||||
for other maintainers when you're absent.
|
|
||||||
|
|
||||||
### Administrative Actions
|
|
||||||
|
|
||||||
Some things (such as changing required CI steps, adding contributors, ...)
|
|
||||||
require administrative permissions. If you don't have those, ask about the
|
|
||||||
change in an issue. If you have the permissions, discuss it with at least one
|
|
||||||
other admin before making the change.
|
|
||||||
|
|
||||||
### Release process
|
|
||||||
|
|
||||||
Given that winit is a widely used library, we should be able to make a patch
|
Given that winit is a widely used library, we should be able to make a patch
|
||||||
releases at any time we want without blocking the development of new features.
|
releases at any time we want without blocking the development of new features.
|
||||||
|
|
||||||
To achieve these goals, a new branch is created for every new release. Releases
|
To achieve these goals, a new branch is created for every new release. Releases and later patch releases are committed and tagged in this branch.
|
||||||
and later patch releases are committed and tagged in this branch.
|
|
||||||
|
|
||||||
The exact steps for an exemplary `0.2.0` release might look like this:
|
The exact steps for an exemplary `0.2.0` release might look like this:
|
||||||
1. Initially, the version on the latest master is `0.1.0`
|
1. Initially, the version on the latest master is `0.1.0`
|
||||||
2. A new `v0.2.x` branch is created for the release
|
2. A new `v0.2.x` branch is created for the release
|
||||||
3. Update released `cfg_attr` in `src/changelog/mod.rs` to `v0.2.md`
|
3. In the branch, the version is bumped to `v0.2.0`
|
||||||
4. Move entries from `src/changelog/unreleased.md` into
|
4. The new commit in the branch is tagged `v0.2.0`
|
||||||
`src/changelog/v0.2.md`
|
5. The version is pushed to crates.io
|
||||||
5. In the branch, the version is bumped to `v0.2.0`
|
6. A GitHub release is created for the `v0.2.0` tag
|
||||||
6. The new commit in the branch is tagged `v0.2.0`
|
7. On master, the version is bumped to `0.2.0`, and the CHANGELOG is updated
|
||||||
7. The version is pushed to crates.io
|
|
||||||
8. A GitHub release is created for the `v0.2.0` tag
|
|
||||||
9. On master, the version is bumped to `0.2.0`, and the changelog is updated
|
|
||||||
|
|
||||||
When doing a patch release, the process is similar:
|
When doing a patch release, the process is similar:
|
||||||
1. Initially, the version of the latest release is `0.2.0`
|
1. Initially, the version of the latest release is `0.2.0`
|
||||||
2. Checkout the `v0.2.x` branch
|
2. Checkout the `v0.2.x` branch
|
||||||
3. Cherry-pick the required non-breaking changes into the `v0.2.x`
|
3. Cherry-pick the required non-breaking changes into the `v0.2.x`
|
||||||
4. Follow steps 4-9 of the regular release example
|
4. Follow steps 3-7 of the regular release example
|
||||||
|
|
||||||
[CODEOWNERS]: .github/CODEOWNERS
|
|
||||||
|
|||||||
291
Cargo.toml
291
Cargo.toml
@@ -1,97 +1,226 @@
|
|||||||
[workspace]
|
[package]
|
||||||
default-members = ["winit"]
|
name = "winit"
|
||||||
members = ["dpi", "winit*"]
|
version = "0.29.7"
|
||||||
resolver = "2"
|
authors = ["The winit contributors", "Pierre Krieger <pierre.krieger1708@gmail.com>"]
|
||||||
|
description = "Cross-platform window creation library."
|
||||||
[workspace.package]
|
edition = "2021"
|
||||||
edition = "2024"
|
keywords = ["windowing"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
|
readme = "README.md"
|
||||||
repository = "https://github.com/rust-windowing/winit"
|
repository = "https://github.com/rust-windowing/winit"
|
||||||
rust-version = "1.85"
|
documentation = "https://docs.rs/winit"
|
||||||
version = "0.31.0-beta.2"
|
categories = ["gui"]
|
||||||
|
rust-version = "1.65.0"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[package.metadata.docs.rs]
|
||||||
# Workspace dependencies.
|
features = [
|
||||||
# `winit` has no version here to allow using it in dev deps for docs.
|
"rwh_04",
|
||||||
winit = { path = "winit" }
|
"rwh_05",
|
||||||
winit-android = { version = "=0.31.0-beta.2", path = "winit-android" }
|
"rwh_06",
|
||||||
winit-appkit = { version = "=0.31.0-beta.2", path = "winit-appkit" }
|
"serde",
|
||||||
winit-common = { version = "=0.31.0-beta.2", path = "winit-common" }
|
# Enabled to get docs to compile
|
||||||
winit-core = { version = "=0.31.0-beta.2", path = "winit-core" }
|
"android-native-activity",
|
||||||
winit-orbital = { version = "=0.31.0-beta.2", path = "winit-orbital" }
|
]
|
||||||
winit-uikit = { version = "=0.31.0-beta.2", path = "winit-uikit" }
|
default-target = "x86_64-unknown-linux-gnu"
|
||||||
winit-wayland = { version = "=0.31.0-beta.2", path = "winit-wayland", default-features = false }
|
# These are all tested in CI
|
||||||
winit-web = { version = "=0.31.0-beta.2", path = "winit-web" }
|
targets = [
|
||||||
winit-win32 = { version = "=0.31.0-beta.2", path = "winit-win32" }
|
# Windows
|
||||||
winit-x11 = { version = "=0.31.0-beta.2", path = "winit-x11" }
|
"i686-pc-windows-msvc",
|
||||||
|
"x86_64-pc-windows-msvc",
|
||||||
|
# macOS
|
||||||
|
"x86_64-apple-darwin",
|
||||||
|
# Unix (X11 & Wayland)
|
||||||
|
"i686-unknown-linux-gnu",
|
||||||
|
"x86_64-unknown-linux-gnu",
|
||||||
|
# iOS
|
||||||
|
"x86_64-apple-ios",
|
||||||
|
# Android
|
||||||
|
"aarch64-linux-android",
|
||||||
|
# WebAssembly
|
||||||
|
"wasm32-unknown-unknown",
|
||||||
|
]
|
||||||
|
rustdoc-args = ["--cfg", "docsrs"]
|
||||||
|
|
||||||
# Core dependencies.
|
[features]
|
||||||
|
default = ["rwh_06", "x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
|
||||||
|
x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"]
|
||||||
|
wayland = ["wayland-client", "wayland-backend", "wayland-protocols", "wayland-protocols-plasma", "sctk", "ahash", "memmap2"]
|
||||||
|
wayland-dlopen = ["wayland-backend/dlopen"]
|
||||||
|
wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"]
|
||||||
|
wayland-csd-adwaita-crossfont = ["sctk-adwaita", "sctk-adwaita/crossfont"]
|
||||||
|
wayland-csd-adwaita-notitle = ["sctk-adwaita"]
|
||||||
|
android-native-activity = ["android-activity/native-activity"]
|
||||||
|
android-game-activity = ["android-activity/game-activity"]
|
||||||
|
serde = ["dep:serde", "cursor-icon/serde", "smol_str/serde"]
|
||||||
|
rwh_04 = ["dep:rwh_04", "ndk/rwh_04"]
|
||||||
|
rwh_05 = ["dep:rwh_05", "ndk/rwh_05"]
|
||||||
|
rwh_06 = ["dep:rwh_06", "ndk/rwh_06"]
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
cfg_aliases = "0.1.1"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
bitflags = "2"
|
bitflags = "2"
|
||||||
cfg_aliases = "0.2.1"
|
|
||||||
cursor-icon = "1.1.0"
|
cursor-icon = "1.1.0"
|
||||||
dpi = { version = "0.1.2", path = "dpi" }
|
log = "0.4"
|
||||||
keyboard-types = "0.8.0"
|
mint = { version = "0.5.6", optional = true }
|
||||||
mint = "0.5.6"
|
once_cell = "1.12"
|
||||||
rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"] }
|
rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true }
|
||||||
serde = { version = "1", features = ["serde_derive"] }
|
rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = ["std"], optional = true }
|
||||||
smol_str = "0.3"
|
rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"], optional = true }
|
||||||
tracing = { version = "0.1.40", default-features = false }
|
serde = { version = "1", optional = true, features = ["serde_derive"] }
|
||||||
|
smol_str = "0.2.0"
|
||||||
|
|
||||||
# Dev dependencies.
|
[dev-dependencies]
|
||||||
image = { version = "0.25.0", default-features = false }
|
image = { version = "0.24.0", default-features = false, features = ["png"] }
|
||||||
softbuffer = { version = "0.4.8", default-features = false, features = [
|
simple_logger = { version = "4.2.0", default_features = false }
|
||||||
"x11",
|
winit = { path = ".", features = ["rwh_05"] }
|
||||||
"x11-dlopen",
|
|
||||||
"wayland",
|
|
||||||
"wayland-dlopen",
|
|
||||||
] }
|
|
||||||
tracing-subscriber = "0.3.18"
|
|
||||||
|
|
||||||
# Android dependencies.
|
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies]
|
||||||
android-activity = "0.6.0"
|
softbuffer = "0.3.0"
|
||||||
ndk = { version = "0.9.0", features = ["rwh_06"], default-features = false }
|
|
||||||
|
|
||||||
# Apple dependencies.
|
[target.'cfg(target_os = "android")'.dependencies]
|
||||||
block2 = "0.6.1"
|
android-activity = "0.5.0"
|
||||||
dispatch2 = { version = "0.3.0", default-features = false, features = ["std", "objc2"] }
|
ndk = { version = "0.8.0", default-features = false }
|
||||||
objc2 = { version = "0.6.1", features = ["relax-sign-encoding"] }
|
ndk-sys = "0.5.0"
|
||||||
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 }
|
|
||||||
|
|
||||||
# Windows dependencies.
|
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
|
||||||
|
core-foundation = "0.9.3"
|
||||||
|
objc2 = "0.4.1"
|
||||||
|
|
||||||
|
[target.'cfg(target_os = "macos")'.dependencies]
|
||||||
|
core-graphics = "0.23.1"
|
||||||
|
|
||||||
|
[target.'cfg(target_os = "macos")'.dependencies.icrate]
|
||||||
|
version = "0.0.4"
|
||||||
|
features = [
|
||||||
|
"dispatch",
|
||||||
|
"Foundation",
|
||||||
|
"Foundation_NSArray",
|
||||||
|
"Foundation_NSAttributedString",
|
||||||
|
"Foundation_NSMutableAttributedString",
|
||||||
|
"Foundation_NSData",
|
||||||
|
"Foundation_NSDictionary",
|
||||||
|
"Foundation_NSString",
|
||||||
|
"Foundation_NSProcessInfo",
|
||||||
|
"Foundation_NSThread",
|
||||||
|
"Foundation_NSNumber",
|
||||||
|
]
|
||||||
|
|
||||||
|
[target.'cfg(target_os = "ios")'.dependencies.icrate]
|
||||||
|
version = "0.0.4"
|
||||||
|
features = [
|
||||||
|
"dispatch",
|
||||||
|
"Foundation",
|
||||||
|
"Foundation_NSArray",
|
||||||
|
"Foundation_NSString",
|
||||||
|
"Foundation_NSProcessInfo",
|
||||||
|
"Foundation_NSThread",
|
||||||
|
"Foundation_NSSet",
|
||||||
|
]
|
||||||
|
|
||||||
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
unicode-segmentation = "1.7.1"
|
unicode-segmentation = "1.7.1"
|
||||||
windows-sys = "0.61"
|
|
||||||
|
|
||||||
# Linux dependencies.
|
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
|
||||||
bytemuck = { version = "1.13.1", default-features = false }
|
version = "0.48"
|
||||||
calloop = "0.14.3"
|
features = [
|
||||||
foldhash = { version = "0.2.0", default-features = false, features = ["std"] }
|
"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_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",
|
||||||
|
]
|
||||||
|
|
||||||
|
[target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_os = "ios", target_os = "macos"))))'.dependencies]
|
||||||
|
ahash = { version = "0.8.3", features = ["no-rng"], optional = true }
|
||||||
|
bytemuck = { version = "1.13.1", default-features = false, optional = true }
|
||||||
|
calloop = "0.12.3"
|
||||||
libc = "0.2.64"
|
libc = "0.2.64"
|
||||||
memmap2 = "0.9.0"
|
memmap2 = { version = "0.9.0", optional = true }
|
||||||
percent-encoding = "2.0"
|
percent-encoding = { version = "2.0", optional = true }
|
||||||
rustix = { version = "1.0.7", default-features = false }
|
rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "thread", "process"] }
|
||||||
x11-dl = "2.19.1"
|
sctk = { package = "smithay-client-toolkit", version = "0.18.0", default-features = false, features = ["calloop"], optional = true }
|
||||||
x11rb = { version = "0.13.0", default-features = false }
|
sctk-adwaita = { version = "0.8.0", default_features = false, optional = true }
|
||||||
xkbcommon-dl = "0.4.2"
|
wayland-backend = { version = "0.3.0", default_features = false, features = ["client_system"], optional = true }
|
||||||
|
wayland-client = { version = "0.31.1", optional = true }
|
||||||
|
wayland-protocols = { version = "0.31.0", features = [ "staging"], optional = true }
|
||||||
|
wayland-protocols-plasma = { version = "0.2.0", features = [ "client" ], optional = true }
|
||||||
|
x11-dl = { version = "2.18.5", optional = true }
|
||||||
|
x11rb = { version = "0.13.0", default-features = false, features = ["allow-unsafe-code", "dl-libxcb", "randr", "resource_manager", "xinput", "xkb"], optional = true }
|
||||||
|
xkbcommon-dl = "0.4.0"
|
||||||
|
|
||||||
# Orbital dependencies.
|
[target.'cfg(target_os = "redox")'.dependencies]
|
||||||
libredox = "0.1.12"
|
orbclient = { version = "0.3.42", default-features = false }
|
||||||
orbclient = { version = "0.3.47", default-features = false }
|
redox_syscall = "0.3"
|
||||||
redox_event = { package = "redox_event", version = "0.4.5" }
|
|
||||||
|
|
||||||
# Web dependencies.
|
[target.'cfg(target_family = "wasm")'.dependencies.web_sys]
|
||||||
|
package = "web-sys"
|
||||||
|
version = "0.3.64"
|
||||||
|
features = [
|
||||||
|
'AbortController',
|
||||||
|
'AbortSignal',
|
||||||
|
'console',
|
||||||
|
'CssStyleDeclaration',
|
||||||
|
'Document',
|
||||||
|
'DomRect',
|
||||||
|
'DomRectReadOnly',
|
||||||
|
'Element',
|
||||||
|
'Event',
|
||||||
|
'EventTarget',
|
||||||
|
'FocusEvent',
|
||||||
|
'HtmlCanvasElement',
|
||||||
|
'HtmlElement',
|
||||||
|
'IntersectionObserver',
|
||||||
|
'IntersectionObserverEntry',
|
||||||
|
'KeyboardEvent',
|
||||||
|
'MediaQueryList',
|
||||||
|
'MessageChannel',
|
||||||
|
'MessagePort',
|
||||||
|
'Node',
|
||||||
|
'PageTransitionEvent',
|
||||||
|
'PointerEvent',
|
||||||
|
'ResizeObserver',
|
||||||
|
'ResizeObserverBoxOptions',
|
||||||
|
'ResizeObserverEntry',
|
||||||
|
'ResizeObserverOptions',
|
||||||
|
'ResizeObserverSize',
|
||||||
|
'VisibilityState',
|
||||||
|
'Window',
|
||||||
|
'WheelEvent'
|
||||||
|
]
|
||||||
|
|
||||||
|
[target.'cfg(target_family = "wasm")'.dependencies]
|
||||||
atomic-waker = "1"
|
atomic-waker = "1"
|
||||||
concurrent-queue = { version = "2", default-features = false }
|
js-sys = "0.3.64"
|
||||||
console_error_panic_hook = "0.1"
|
wasm-bindgen = "0.2"
|
||||||
js-sys = "0.3.70"
|
wasm-bindgen-futures = "0.4"
|
||||||
pin-project = "1"
|
web-time = "0.2"
|
||||||
tracing-web = "0.1"
|
|
||||||
wasm-bindgen = "0.2.93"
|
[target.'cfg(target_family = "wasm")'.dev-dependencies]
|
||||||
wasm-bindgen-futures = "0.4.43"
|
console_log = "1"
|
||||||
wasm-bindgen-test = "0.3"
|
web-sys = { version = "0.3.22", features = ['CanvasRenderingContext2d'] }
|
||||||
web-time = "1"
|
|
||||||
web_sys = { package = "web-sys", version = "0.3.70" }
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"run-wasm",
|
||||||
|
]
|
||||||
|
|||||||
203
FEATURES.md
203
FEATURES.md
@@ -3,8 +3,8 @@
|
|||||||
Winit aims to expose an interface that abstracts over window creation and input handling and can
|
Winit aims to expose an interface that abstracts over window creation and input handling and can
|
||||||
be used to create both games and applications. It supports the following main graphical platforms:
|
be used to create both games and applications. It supports the following main graphical platforms:
|
||||||
- Desktop
|
- Desktop
|
||||||
- Windows
|
- Windows 7+ (10+ is tested regularly)
|
||||||
- macOS
|
- macOS 10.7+ (10.14+ is tested regularly)
|
||||||
- Unix
|
- Unix
|
||||||
- via X11
|
- via X11
|
||||||
- via Wayland
|
- via Wayland
|
||||||
@@ -13,6 +13,9 @@ be used to create both games and applications. It supports the following main gr
|
|||||||
- iOS
|
- iOS
|
||||||
- Android
|
- Android
|
||||||
- Web
|
- Web
|
||||||
|
- Chrome
|
||||||
|
- Firefox
|
||||||
|
- Safari 13.1+
|
||||||
|
|
||||||
Most platforms expose capabilities that cannot be meaningfully transposed onto others. Winit does not
|
Most platforms expose capabilities that cannot be meaningfully transposed onto others. Winit does not
|
||||||
aim to support every single feature of every platform, but rather to abstract over the common features
|
aim to support every single feature of every platform, but rather to abstract over the common features
|
||||||
@@ -47,3 +50,199 @@ through the implementation work necessary to function on all platforms. When one
|
|||||||
gets implemented across all platforms, a PR can be opened to upgrade the feature to a core feature.
|
gets implemented across all platforms, a PR can be opened to upgrade the feature to a core feature.
|
||||||
If that gets accepted, the platform-specific functions get deprecated and become permanently
|
If that gets accepted, the platform-specific functions get deprecated and become permanently
|
||||||
exposed through the core, cross-platform API.
|
exposed through the core, cross-platform API.
|
||||||
|
|
||||||
|
# Features
|
||||||
|
|
||||||
|
## Extending this section
|
||||||
|
|
||||||
|
If your PR makes notable changes to Winit's features, please update this section as follows:
|
||||||
|
|
||||||
|
- If your PR adds a new feature, add a brief description to the relevant section. If the feature is a core
|
||||||
|
feature, add a row to the feature matrix and describe what platforms the feature has been implemented on.
|
||||||
|
|
||||||
|
- If your PR begins a new API rework, add a row to the `Pending API Reworks` table. If the PR implements the
|
||||||
|
API rework on all relevant platforms, please move it to the `Completed API Reworks` table.
|
||||||
|
|
||||||
|
- If your PR implements an already-existing feature on a new platform, either mark the feature as *completed*,
|
||||||
|
or mark it as *mostly completed* and link to an issue describing the problems with the implementation.
|
||||||
|
|
||||||
|
## Core
|
||||||
|
|
||||||
|
### Windowing
|
||||||
|
- **Window initialization**: Winit allows the creation of a window
|
||||||
|
- **Providing pointer to init OpenGL**: Winit provides the necessary pointers to initialize a working opengl context
|
||||||
|
- **Providing pointer to init Vulkan**: Same as OpenGL but for Vulkan
|
||||||
|
- **Window decorations**: The windows created by winit are properly decorated, and the decorations can
|
||||||
|
be deactivated
|
||||||
|
- **Window decorations toggle**: Decorations can be turned on or off after window creation
|
||||||
|
- **Window resizing**: The windows created by winit can be resized and generate the appropriate events
|
||||||
|
when they are. The application can precisely control its window size if desired.
|
||||||
|
- **Window resize increments**: When the window gets resized, the application can choose to snap the window's
|
||||||
|
size to specific values.
|
||||||
|
- **Window transparency**: Winit allows the creation of windows with a transparent background.
|
||||||
|
- **Window maximization**: The windows created by winit can be maximized upon creation.
|
||||||
|
- **Window maximization toggle**: The windows created by winit can be maximized and unmaximized after
|
||||||
|
creation.
|
||||||
|
- **Window minimization**: The windows created by winit can be minimized after creation.
|
||||||
|
- **Fullscreen**: The windows created by winit can be put into fullscreen mode.
|
||||||
|
- **Fullscreen toggle**: The windows created by winit can be switched to and from fullscreen after
|
||||||
|
creation.
|
||||||
|
- **Exclusive fullscreen**: Winit allows changing the video mode of the monitor
|
||||||
|
for fullscreen windows and, if applicable, captures the monitor for exclusive
|
||||||
|
use by this application.
|
||||||
|
- **HiDPI support**: Winit assists developers in appropriately scaling HiDPI content.
|
||||||
|
- **Popup / modal windows**: Windows can be created relative to the client area of other windows, and parent
|
||||||
|
windows can be disabled in favor of popup windows. This feature also guarantees that popup windows
|
||||||
|
get drawn above their owner.
|
||||||
|
|
||||||
|
|
||||||
|
### System Information
|
||||||
|
- **Monitor list**: Retrieve the list of monitors and their metadata, including which one is primary.
|
||||||
|
- **Video mode query**: Monitors can be queried for their supported fullscreen video modes (consisting of resolution, refresh rate, and bit depth).
|
||||||
|
|
||||||
|
### Input Handling
|
||||||
|
- **Mouse events**: Generating mouse events associated with pointer motion, click, and scrolling events.
|
||||||
|
- **Mouse set location**: Forcibly changing the location of the pointer.
|
||||||
|
- **Cursor locking**: Locking the cursor inside the window so it cannot move.
|
||||||
|
- **Cursor confining**: Confining the cursor to the window bounds so it cannot leave them.
|
||||||
|
- **Cursor icon**: Changing the cursor icon or hiding the cursor.
|
||||||
|
- **Cursor hittest**: Handle or ignore mouse events for a window.
|
||||||
|
- **Touch events**: Single-touch events.
|
||||||
|
- **Touch pressure**: Touch events contain information about the amount of force being applied.
|
||||||
|
- **Multitouch**: Multi-touch events, including cancellation of a gesture.
|
||||||
|
- **Keyboard events**: Properly processing keyboard events using the user-specified keymap and
|
||||||
|
translating keypresses into UTF-8 characters, handling dead keys and IMEs.
|
||||||
|
- **Drag & Drop**: Dragging content into winit, detecting when content enters, drops, or if the drop is cancelled.
|
||||||
|
- **Raw Device Events**: Capturing input from input devices without any OS filtering.
|
||||||
|
- **Gamepad/Joystick events**: Capturing input from gamepads and joysticks.
|
||||||
|
- **Device movement events**: Capturing input from the device gyroscope and accelerometer.
|
||||||
|
|
||||||
|
## Platform
|
||||||
|
### Windows
|
||||||
|
* Setting the name of the internal window class
|
||||||
|
* Setting the taskbar icon
|
||||||
|
* Setting the parent window
|
||||||
|
* Setting a menu bar
|
||||||
|
* `WS_EX_NOREDIRECTIONBITMAP` support
|
||||||
|
* Theme the title bar according to Windows 10 Dark Mode setting or set a preferred theme
|
||||||
|
|
||||||
|
### macOS
|
||||||
|
* Window activation policy
|
||||||
|
* Window movable by background
|
||||||
|
* Transparent titlebar
|
||||||
|
* Hidden titlebar
|
||||||
|
* Hidden titlebar buttons
|
||||||
|
* Full-size content view
|
||||||
|
* Accepts first mouse
|
||||||
|
* Set a preferred theme and get current theme.
|
||||||
|
|
||||||
|
### Unix
|
||||||
|
* Window urgency
|
||||||
|
* X11 Window Class
|
||||||
|
* X11 Override Redirect Flag
|
||||||
|
* GTK Theme Variant
|
||||||
|
* Base window size
|
||||||
|
* Setting the X11 parent window
|
||||||
|
|
||||||
|
### iOS
|
||||||
|
* `winit` has a minimum OS requirement of iOS 8
|
||||||
|
* Get the `UIScreen` object pointer
|
||||||
|
* Setting the `UIView` hidpi factor
|
||||||
|
* Valid orientations
|
||||||
|
* Home indicator visibility
|
||||||
|
* Status bar visibility and style
|
||||||
|
* Deferring system gestures
|
||||||
|
* Getting the device idiom
|
||||||
|
* Getting the preferred video mode
|
||||||
|
|
||||||
|
### Web
|
||||||
|
* Get if the systems preferred color scheme is "dark"
|
||||||
|
|
||||||
|
## Usability
|
||||||
|
* `serde`: Enables serialization/deserialization of certain types with Serde. (Maintainer: @Osspial)
|
||||||
|
|
||||||
|
## Compatibility Matrix
|
||||||
|
|
||||||
|
Legend:
|
||||||
|
|
||||||
|
- ✔️: Works as intended
|
||||||
|
- ▢: Mostly works, but some bugs are known
|
||||||
|
- ❌: Missing feature or large bugs making it unusable
|
||||||
|
- **N/A**: Not applicable for this platform
|
||||||
|
- ❓: Unknown status
|
||||||
|
|
||||||
|
### Windowing
|
||||||
|
|Feature |Windows|MacOS |Linux x11 |Linux Wayland |Android|iOS |Web |Redox OS|
|
||||||
|
|-------------------------------- | ----- | ---- | ------- | ----------- | ----- | ----- | -------- | ------ |
|
||||||
|
|Window initialization |✔️ |✔️ |▢[#5] |✔️ |▢[#33]|▢[#33] |✔️ |✔️ |
|
||||||
|
|Providing pointer to init OpenGL |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |**N/A**|✔️ |
|
||||||
|
|Providing pointer to init Vulkan |✔️ |✔️ |✔️ |✔️ |✔️ |❓ |**N/A**|**N/A** |
|
||||||
|
|Window decorations |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|✔️ |
|
||||||
|
|Window decorations toggle |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** |
|
||||||
|
|Window resizing |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|✔️ |✔️ |
|
||||||
|
|Window resize increments |❌ |✔️ |✔️ |❌ |**N/A**|**N/A**|**N/A**|**N/A** |
|
||||||
|
|Window transparency |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|N/A |✔️ |
|
||||||
|
|Window blur |❌ |❌ |❌ |✔️ |**N/A**|**N/A**|N/A |❌ |
|
||||||
|
|Window maximization |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** |
|
||||||
|
|Window maximization toggle |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** |
|
||||||
|
|Window minimization |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A**|**N/A** |
|
||||||
|
|Fullscreen |✔️ |✔️ |✔️ |✔️ |**N/A**|✔️ |✔️ |**N/A** |
|
||||||
|
|Fullscreen toggle |✔️ |✔️ |✔️ |✔️ |**N/A**|✔️ |✔️ |**N/A** |
|
||||||
|
|Exclusive fullscreen |✔️ |✔️ |✔️ |**N/A** |❌ |✔️ |**N/A**|**N/A** |
|
||||||
|
|HiDPI support |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |❌ |
|
||||||
|
|Popup windows |❌ |❌ |❌ |❌ |❌ |❌ |**N/A**|**N/A** |
|
||||||
|
|
||||||
|
### System information
|
||||||
|
|Feature |Windows|MacOS |Linux x11|Linux Wayland|Android|iOS |Web |Redox OS|
|
||||||
|
|---------------- | ----- | ---- | ------- | ----------- | ----- | ------- | -------- | ------ |
|
||||||
|
|Monitor list |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |**N/A**|❌ |
|
||||||
|
|Video mode query |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |**N/A**|❌ |
|
||||||
|
|
||||||
|
### Input handling
|
||||||
|
|Feature |Windows |MacOS |Linux x11|Linux Wayland|Android|iOS |Web |Redox OS|
|
||||||
|
|----------------------- | ----- | ---- | ------- | ----------- | ----- | ----- | -------- | ------ |
|
||||||
|
|Mouse events |✔️ |▢[#63] |✔️ |✔️ |**N/A**|**N/A**|✔️ |✔️ |
|
||||||
|
|Mouse set location |✔️ |✔️ |✔️ |✔️(when locked) |**N/A**|**N/A**|**N/A**|**N/A** |
|
||||||
|
|Cursor locking |❌ |✔️ |❌ |✔️ |**N/A**|**N/A**|✔️ |❌ |
|
||||||
|
|Cursor confining |✔️ |❌ |✔️ |✔️ |**N/A**|**N/A**|❌ |❌ |
|
||||||
|
|Cursor icon |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|✔️ |**N/A** |
|
||||||
|
|Cursor hittest |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|❌ |❌ |
|
||||||
|
|Touch events |✔️ |❌ |✔️ |✔️ |✔️ |✔️ |✔️ |**N/A** |
|
||||||
|
|Touch pressure |✔️ |❌ |❌ |❌ |❌ |✔️ |✔️ |**N/A** |
|
||||||
|
|Multitouch |✔️ |❌ |✔️ |✔️ |✔️ |✔️ |❌ |**N/A** |
|
||||||
|
|Keyboard events |✔️ |✔️ |✔️ |✔️ |✔️ |❌ |✔️ |✔️ |
|
||||||
|
|Drag & Drop |▢[#720] |▢[#720] |▢[#720] |▢[#720] |**N/A**|**N/A**|❓ |**N/A** |
|
||||||
|
|Raw Device Events |▢[#750] |▢[#750] |▢[#750] |❌ |❌ |❌ |❓ |**N/A** |
|
||||||
|
|Gamepad/Joystick events |❌[#804] |❌ |❌ |❌ |❌ |❌ |❓ |**N/A** |
|
||||||
|
|Device movement events |❓ |❓ |❓ |❓ |❌ |❌ |❓ |**N/A** |
|
||||||
|
|Drag window with cursor |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|**N/A** |**N/A** |
|
||||||
|
|Resize with cursor |✔️ |❌ |✔️ |✔️ |**N/A**|**N/A**|**N/A** |**N/A** |
|
||||||
|
|
||||||
|
### Pending API Reworks
|
||||||
|
Changes in the API that have been agreed upon but aren't implemented across all platforms.
|
||||||
|
|
||||||
|
|Feature |Windows|MacOS |Linux x11|Linux Wayland|Android|iOS |Web |Redox OS|
|
||||||
|
|------------------------------ | ----- | ---- | ------- | ----------- | ----- | ----- | -------- | ------ |
|
||||||
|
|New API for HiDPI ([#315] [#319]) |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |❓ |❓ |
|
||||||
|
|Event Loop 2.0 ([#459]) |✔️ |✔️ |✔️ |✔️ |✔️ |✔️ |❓ |✔️ |
|
||||||
|
|Keyboard Input 2.0 ([#753]) |✔️ |✔️ |✔️ |✔️ |✔️ |❌ |✔️ |✔️ |
|
||||||
|
|
||||||
|
### Completed API Reworks
|
||||||
|
|Feature |Windows|MacOS |Linux x11|Linux Wayland|Android|iOS |Web |Redox OS|
|
||||||
|
|------------------------------ | ----- | ---- | ------- | ----------- | ----- | ----- | -------- | ------ |
|
||||||
|
|
||||||
|
[#165]: https://github.com/rust-windowing/winit/issues/165
|
||||||
|
[#219]: https://github.com/rust-windowing/winit/issues/219
|
||||||
|
[#242]: https://github.com/rust-windowing/winit/issues/242
|
||||||
|
[#306]: https://github.com/rust-windowing/winit/issues/306
|
||||||
|
[#315]: https://github.com/rust-windowing/winit/issues/315
|
||||||
|
[#319]: https://github.com/rust-windowing/winit/issues/319
|
||||||
|
[#33]: https://github.com/rust-windowing/winit/issues/33
|
||||||
|
[#459]: https://github.com/rust-windowing/winit/issues/459
|
||||||
|
[#5]: https://github.com/rust-windowing/winit/issues/5
|
||||||
|
[#63]: https://github.com/rust-windowing/winit/issues/63
|
||||||
|
[#720]: https://github.com/rust-windowing/winit/issues/720
|
||||||
|
[#721]: https://github.com/rust-windowing/winit/issues/721
|
||||||
|
[#750]: https://github.com/rust-windowing/winit/issues/750
|
||||||
|
[#753]: https://github.com/rust-windowing/winit/issues/753
|
||||||
|
[#804]: https://github.com/rust-windowing/winit/issues/804
|
||||||
|
|||||||
142
README.md
142
README.md
@@ -2,13 +2,11 @@
|
|||||||
|
|
||||||
[](https://crates.io/crates/winit)
|
[](https://crates.io/crates/winit)
|
||||||
[](https://docs.rs/winit)
|
[](https://docs.rs/winit)
|
||||||
[](https://rust-windowing.github.io/winit/winit/index.html)
|
|
||||||
[](https://github.com/rust-windowing/winit/actions)
|
[](https://github.com/rust-windowing/winit/actions)
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
winit = "0.31.0-beta.2"
|
winit = "0.29.7"
|
||||||
```
|
```
|
||||||
|
|
||||||
## [Documentation](https://docs.rs/winit)
|
## [Documentation](https://docs.rs/winit)
|
||||||
@@ -19,9 +17,10 @@ For features _outside_ the scope of winit, see [Are we GUI Yet?](https://arewegu
|
|||||||
|
|
||||||
## Contact Us
|
## Contact Us
|
||||||
|
|
||||||
Join us in our [](https://matrix.to/#/#rust-windowing:matrix.org) room.
|
Join us in any of these:
|
||||||
|
|
||||||
The maintainers have a meeting every friday at UTC 15. The meeting notes can be found [here](https://hackmd.io/@winit-meetings).
|
[](https://matrix.to/#/#rust-windowing:matrix.org)
|
||||||
|
[](https://web.libera.chat/#winit)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -33,13 +32,17 @@ Winit is designed to be a low-level brick in a hierarchy of libraries. Consequen
|
|||||||
show something on the window you need to use the platform-specific getters provided by winit, or
|
show something on the window you need to use the platform-specific getters provided by winit, or
|
||||||
another library.
|
another library.
|
||||||
|
|
||||||
## CONTRIBUTING
|
### Cargo Features
|
||||||
|
|
||||||
For contributing guidelines see [CONTRIBUTING.md](./CONTRIBUTING.md).
|
Winit provides the following features, which can be enabled in your `Cargo.toml` file:
|
||||||
|
* `serde`: Enables serialization/deserialization of certain types with [Serde](https://crates.io/crates/serde).
|
||||||
|
* `x11` (enabled by default): On Unix platform, compiles with the X11 backend
|
||||||
|
* `wayland` (enabled by default): On Unix platform, compiles with the Wayland backend
|
||||||
|
* `mint`: Enables mint (math interoperability standard types) conversions.
|
||||||
|
|
||||||
## MSRV Policy
|
## 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.65**. Changes to
|
||||||
the MSRV will be accompanied by a minor version bump.
|
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
|
As a **tentative** policy, the upper bound of the MSRV is given by the following
|
||||||
@@ -54,15 +57,12 @@ Where `sid` is the current version of `rustc` provided by [Debian Sid], and
|
|||||||
|
|
||||||
[Debian Sid]: https://packages.debian.org/sid/rustc
|
[Debian Sid]: https://packages.debian.org/sid/rustc
|
||||||
|
|
||||||
An exception is made for the Android platform, where a higher Rust version
|
The exception is for the Android platform, where a higher Rust version
|
||||||
must be used for certain Android features. In this case, the MSRV will be
|
must be used for certain Android features. In this case, the MSRV will be
|
||||||
capped at the latest stable version of Rust minus three. This inconsistency is
|
capped at the latest stable version of Rust minus three. This inconsistency is
|
||||||
not reflected in Cargo metadata, as it is not powerful enough to expose this
|
not reflected in Cargo metadata, as it is not powerful enough to expose this
|
||||||
restriction.
|
restriction.
|
||||||
|
|
||||||
Redox OS is also not covered by this MSRV policy, as it requires a Rust nightly
|
|
||||||
toolchain to compile.
|
|
||||||
|
|
||||||
All crates in the [`rust-windowing`] organizations have the
|
All crates in the [`rust-windowing`] organizations have the
|
||||||
same MSRV policy.
|
same MSRV policy.
|
||||||
|
|
||||||
@@ -70,10 +70,118 @@ same MSRV policy.
|
|||||||
|
|
||||||
### Platform-specific usage
|
### Platform-specific usage
|
||||||
|
|
||||||
Check out the [`winit::platform`](https://docs.rs/winit/latest/winit/platform/index.html) module for platform-specific usage.
|
#### Wayland
|
||||||
|
|
||||||
### Repository License
|
Note that windows don't appear on Wayland until you draw/present to them.
|
||||||
|
|
||||||
Note that the license in `LICENSE` doesn't apply in full to the DPI package [./dpi](./dpi).
|
#### WebAssembly
|
||||||
Full details can be found in that folder's README.
|
|
||||||
<!-- This doesn't apply to users of the Winit crate, but this is also the repository level README -->
|
To run the web example: `cargo run-wasm --example web`
|
||||||
|
|
||||||
|
Winit supports compiling to the `wasm32-unknown-unknown` target with `web-sys`.
|
||||||
|
|
||||||
|
On the web platform, a Winit window is backed by a `<canvas>` element. You can
|
||||||
|
either [provide Winit with a `<canvas>` element][web with_canvas], or [let Winit
|
||||||
|
create a `<canvas>` element which you can then retrieve][web canvas getter] and
|
||||||
|
insert it into the DOM yourself.
|
||||||
|
|
||||||
|
For the example code using Winit with WebAssembly, check out the [web example]. For
|
||||||
|
information on using Rust on WebAssembly, check out the [Rust and WebAssembly
|
||||||
|
book].
|
||||||
|
|
||||||
|
[web with_canvas]: https://docs.rs/winit/latest/wasm32-unknown-unknown/winit/platform/web/trait.WindowBuilderExtWebSys.html#tymethod.with_canvas
|
||||||
|
[web canvas getter]: https://docs.rs/winit/latest/wasm32-unknown-unknown/winit/platform/web/trait.WindowExtWebSys.html#tymethod.canvas
|
||||||
|
[web example]: ./examples/web.rs
|
||||||
|
[Rust and WebAssembly book]: https://rustwasm.github.io/book/
|
||||||
|
|
||||||
|
#### Android
|
||||||
|
|
||||||
|
The Android backend builds on (and exposes types from) the [`ndk`](https://docs.rs/ndk/latest/ndk/) crate.
|
||||||
|
|
||||||
|
Native Android applications need some form of "glue" crate that is responsible
|
||||||
|
for defining the main entry point for your Rust application as well as tracking
|
||||||
|
various life-cycle events and synchronizing with the main JVM thread.
|
||||||
|
|
||||||
|
Winit uses the [android-activity](https://github.com/rib/android-activity) as a
|
||||||
|
glue crate (prior to `0.28` it used
|
||||||
|
[ndk-glue](https://github.com/rust-windowing/android-ndk-rs/tree/master/ndk-glue)).
|
||||||
|
|
||||||
|
The version of the glue crate that your application depends on _must_ match the
|
||||||
|
version that Winit depends on because the glue crate is responsible for your
|
||||||
|
application's main entry point. If Cargo resolves multiple versions, they will
|
||||||
|
clash.
|
||||||
|
|
||||||
|
`winit` glue compatibility table:
|
||||||
|
|
||||||
|
| winit | ndk-glue |
|
||||||
|
| :---: | :--------------------------: |
|
||||||
|
| 0.29 | `android-activity = "0.5"` |
|
||||||
|
| 0.28 | `android-activity = "0.4"` |
|
||||||
|
| 0.27 | `ndk-glue = "0.7"` |
|
||||||
|
| 0.26 | `ndk-glue = "0.5"` |
|
||||||
|
| 0.25 | `ndk-glue = "0.3"` |
|
||||||
|
| 0.24 | `ndk-glue = "0.2"` |
|
||||||
|
|
||||||
|
The recommended way to avoid a conflict with the glue version is to avoid explicitly
|
||||||
|
depending on the `android-activity` crate, and instead consume the API that
|
||||||
|
is re-exported by Winit under `winit::platform::android::activity::*`
|
||||||
|
|
||||||
|
Running on an Android device needs a dynamic system library. Add this to Cargo.toml:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[lib]
|
||||||
|
name = "main"
|
||||||
|
crate-type = ["cdylib"]
|
||||||
|
```
|
||||||
|
|
||||||
|
All Android applications are based on an `Activity` subclass, and the
|
||||||
|
`android-activity` crate is designed to support different choices for this base
|
||||||
|
class. Your application _must_ specify the base class it needs via a feature flag:
|
||||||
|
|
||||||
|
| Base Class | Feature Flag | Notes |
|
||||||
|
| :--------------: | :---------------: | :-----: |
|
||||||
|
| `NativeActivity` | `android-native-activity` | Built-in to Android - it is possible to use without compiling any Java or Kotlin code. Java or Kotlin code may be needed to subclass `NativeActivity` to access some platform features. It does not derive from the [`AndroidAppCompat`] base class.|
|
||||||
|
| [`GameActivity`] | `android-game-activity` | Derives from [`AndroidAppCompat`], a defacto standard `Activity` base class that helps support a wider range of Android versions. Requires a build system that can compile Java or Kotlin and fetch Android dependencies from a [Maven repository][agdk_jetpack] (or link with an embedded [release][agdk_releases] of [`GameActivity`]) |
|
||||||
|
|
||||||
|
[`GameActivity`]: https://developer.android.com/games/agdk/game-activity
|
||||||
|
[`GameTextInput`]: https://developer.android.com/games/agdk/add-support-for-text-input
|
||||||
|
[`AndroidAppCompat`]: https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity
|
||||||
|
[agdk_jetpack]: https://developer.android.com/jetpack/androidx/releases/games
|
||||||
|
[agdk_releases]: https://developer.android.com/games/agdk/download#agdk-libraries
|
||||||
|
[Gradle]: https://developer.android.com/studio/build
|
||||||
|
|
||||||
|
For more details, refer to these `android-activity` [example applications](https://github.com/rib/android-activity/tree/main/examples).
|
||||||
|
|
||||||
|
##### Converting from `ndk-glue` to `android-activity`
|
||||||
|
|
||||||
|
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.29.7", 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).
|
||||||
|
|
||||||
|
#### MacOS
|
||||||
|
|
||||||
|
A lot of functionality expects the application to be ready before you start
|
||||||
|
doing anything; this includes creating windows, fetching monitors, drawing,
|
||||||
|
and so on, see issues [#2238], [#2051] and [#2087].
|
||||||
|
|
||||||
|
If you encounter problems, you should try doing your initialization inside
|
||||||
|
`Event::Resumed`.
|
||||||
|
|
||||||
|
#### iOS
|
||||||
|
|
||||||
|
Similar to macOS, iOS's main `UIApplicationMain` does some init work that's required
|
||||||
|
by all UI-related code (see issue [#1705]). It would be best to consider creating your windows
|
||||||
|
inside `Event::Resumed`.
|
||||||
|
|
||||||
|
|
||||||
|
[#2238]: https://github.com/rust-windowing/winit/issues/2238
|
||||||
|
[#2051]: https://github.com/rust-windowing/winit/issues/2051
|
||||||
|
[#2087]: https://github.com/rust-windowing/winit/issues/2087
|
||||||
|
[#1705]: https://github.com/rust-windowing/winit/issues/1705
|
||||||
|
|
||||||
|
#### Redox OS
|
||||||
|
|
||||||
|
Redox OS has some functionality not yet present that will be implemented when
|
||||||
|
its orbital display server provides it.
|
||||||
|
|||||||
24
build.rs
Normal file
24
build.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use cfg_aliases::cfg_aliases;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// The script doesn't depend on our code
|
||||||
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
|
||||||
|
// Setup cfg aliases
|
||||||
|
cfg_aliases! {
|
||||||
|
// Systems.
|
||||||
|
android_platform: { target_os = "android" },
|
||||||
|
wasm_platform: { all(target_family = "wasm", not(target_os = "emscripten")) },
|
||||||
|
macos_platform: { target_os = "macos" },
|
||||||
|
ios_platform: { target_os = "ios" },
|
||||||
|
windows_platform: { target_os = "windows" },
|
||||||
|
apple: { any(target_os = "ios", target_os = "macos") },
|
||||||
|
free_unix: { all(unix, not(apple), not(android_platform), not(target_os = "emscripten")) },
|
||||||
|
redox: { target_os = "redox" },
|
||||||
|
|
||||||
|
// Native displays.
|
||||||
|
x11_platform: { all(feature = "x11", free_unix, not(wasm), not(redox)) },
|
||||||
|
wayland_platform: { all(feature = "wayland", free_unix, not(wasm), not(redox)) },
|
||||||
|
orbital_platform: { redox },
|
||||||
|
}
|
||||||
|
}
|
||||||
36
clippy.toml
36
clippy.toml
@@ -1,27 +1,13 @@
|
|||||||
# 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 = [
|
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." },
|
{ path = "web_sys::window", reason = "is not available in every context" },
|
||||||
{ 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" },
|
{ path = "web_sys::HtmlCanvasElement::width", reason = "Winit shouldn't touch the internal canvas size" },
|
||||||
{ allow-invalid = true, path = "web_sys::Document::exit_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
{ path = "web_sys::HtmlCanvasElement::height", reason = "Winit shouldn't touch the internal canvas size" },
|
||||||
{ allow-invalid = true, path = "web_sys::Document::fullscreen_element", reason = "Doesn't account for compatibility with Safari" },
|
{ path = "web_sys::HtmlCanvasElement::set_width", reason = "Winit shouldn't touch the internal canvas size" },
|
||||||
{ allow-invalid = true, path = "web_sys::Element::request_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
{ path = "web_sys::HtmlCanvasElement::set_height", reason = "Winit shouldn't touch the internal canvas size" },
|
||||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::height", reason = "Winit shouldn't touch the internal canvas size" },
|
{ path = "web_sys::Window::document", reason = "cache this to reduce calls to JS" },
|
||||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::set_height", reason = "Winit shouldn't touch the internal canvas size" },
|
{ path = "web_sys::Window::get_computed_style", reason = "cache this to reduce calls to JS" },
|
||||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::set_width", reason = "Winit shouldn't touch the internal canvas size" },
|
{ path = "web_sys::Element::request_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
||||||
{ allow-invalid = true, path = "web_sys::HtmlCanvasElement::width", reason = "Winit shouldn't touch the internal canvas size" },
|
{ path = "web_sys::Document::exit_fullscreen", reason = "Doesn't account for compatibility with Safari" },
|
||||||
{ allow-invalid = true, path = "web_sys::HtmlElement::style", reason = "cache this to reduce calls to JS" },
|
{ path = "web_sys::Document::fullscreen_element", reason = "Doesn't account for compatibility with Safari" },
|
||||||
{ allow-invalid = true, path = "web_sys::MouseEvent::buttons", reason = "Use `backend::event::cursor_buttons()` to avoid wrong conversions" },
|
{ path = "icrate::AppKit::NSView::visibleRect", reason = "We expose a render target to the user, and visibility is not really relevant to that (and can break if you don't use the rectangle position as well). Use `frame` instead." },
|
||||||
{ 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" },
|
|
||||||
]
|
]
|
||||||
|
|||||||
103
deny.toml
103
deny.toml
@@ -1,61 +1,64 @@
|
|||||||
# https://embarkstudios.github.io/cargo-deny
|
# https://embarkstudios.github.io/cargo-deny/
|
||||||
# cargo install cargo-deny
|
# cargo install cargo-deny
|
||||||
# cargo update && cargo deny check
|
# cargo update && cargo deny --all-features --log-level error --target aarch64-apple-ios check
|
||||||
[graph]
|
# Note: running just `cargo deny check` without a `--target` will result in
|
||||||
all-features = true
|
# false positives due to https://github.com/EmbarkStudios/cargo-deny/issues/324
|
||||||
exclude-dev = true
|
|
||||||
targets = [
|
targets = [
|
||||||
"aarch64-apple-darwin",
|
{ triple = "aarch64-apple-ios" },
|
||||||
"aarch64-apple-ios",
|
{ triple = "aarch64-linux-android" },
|
||||||
"aarch64-linux-android",
|
{ triple = "i686-pc-windows-gnu" },
|
||||||
"i686-pc-windows-gnu",
|
{ triple = "i686-pc-windows-msvc" },
|
||||||
"i686-pc-windows-msvc",
|
{ triple = "i686-unknown-linux-gnu" },
|
||||||
"i686-unknown-linux-gnu",
|
{ triple = "wasm32-unknown-unknown" },
|
||||||
{ triple = "wasm32-unknown-unknown", features = [
|
{ triple = "x86_64-apple-darwin" },
|
||||||
"atomics",
|
{ triple = "x86_64-apple-ios" },
|
||||||
] },
|
{ triple = "x86_64-pc-windows-gnu" },
|
||||||
"x86_64-apple-darwin",
|
{ triple = "x86_64-pc-windows-msvc" },
|
||||||
"x86_64-apple-ios",
|
{ triple = "x86_64-unknown-linux-gnu" },
|
||||||
"x86_64-pc-windows-gnu",
|
{ triple = "x86_64-unknown-redox" },
|
||||||
"x86_64-pc-windows-msvc",
|
|
||||||
"x86_64-unknown-linux-gnu",
|
|
||||||
"x86_64-unknown-redox",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[licenses]
|
|
||||||
allow = [
|
[advisories]
|
||||||
"Apache-2.0", # https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)
|
vulnerability = "deny"
|
||||||
"BSD-2-Clause", # https://tldrlegal.com/license/bsd-2-clause-license-(freebsd)
|
unmaintained = "warn"
|
||||||
"BSD-3-Clause", # https://tldrlegal.com/license/bsd-3-clause-license-(revised)
|
yanked = "deny"
|
||||||
"ISC", # https://tldrlegal.com/license/isc-license
|
ignore = []
|
||||||
"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]
|
[bans]
|
||||||
multiple-versions = "deny"
|
multiple-versions = "deny"
|
||||||
|
wildcards = "allow" # at least until https://github.com/EmbarkStudios/cargo-deny/issues/241 is fixed
|
||||||
|
deny = []
|
||||||
skip = [
|
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" },
|
{ name = "raw-window-handle" }, # we intentionally have multiple versions of this
|
||||||
{ crate = "thiserror@1.0", reason = "dep of `ndk` crate, yet to be updated" },
|
{ name = "bitflags" }, # the ecosystem is in the process of migrating.
|
||||||
{ crate = "thiserror-impl@1.0", reason = "dep of `thiserror`" },
|
{ name = "libloading" }, # x11rb uses a different version until the next update
|
||||||
{ crate = "objc2@0.5", reason = "used by crossfont" },
|
{ name = "redox_syscall" }, # https://gitlab.redox-os.org/redox-os/orbclient/-/issues/46
|
||||||
{ 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-tree = []
|
||||||
|
|
||||||
[bans.build]
|
|
||||||
bypass = [
|
[licenses]
|
||||||
{ crate = "android-activity", allow-globs = ["android-games-sdk/import-games-sdk.sh"] },
|
private = { ignore = true }
|
||||||
{ crate = "freetype-sys", allow-globs = ["freetype2/*"] },
|
unlicensed = "deny"
|
||||||
# `crossfont` still depends (partially transitively) on `winapi`.
|
allow-osi-fsf-free = "neither"
|
||||||
{ crate = "winapi-i686-pc-windows-gnu", allow-globs = ["lib/lib*.a"] },
|
confidence-threshold = 0.92 # We want really high confidence when inferring licenses from text
|
||||||
{ crate = "winapi-x86_64-pc-windows-gnu", allow-globs = ["lib/lib*.a"] },
|
copyleft = "deny"
|
||||||
|
allow = [
|
||||||
|
"Apache-2.0 WITH LLVM-exception", # https://spdx.org/licenses/LLVM-exception.html
|
||||||
|
"Apache-2.0", # https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)
|
||||||
|
"BSD-2-Clause", # https://tldrlegal.com/license/bsd-2-clause-license-(freebsd)
|
||||||
|
"BSD-3-Clause", # https://tldrlegal.com/license/bsd-3-clause-license-(revised)
|
||||||
|
"BSL-1.0", # https://tldrlegal.com/license/boost-software-license-1.0-explained
|
||||||
|
"CC0-1.0", # https://creativecommons.org/publicdomain/zero/1.0/
|
||||||
|
"ISC", # https://tldrlegal.com/license/-isc-license
|
||||||
|
"LicenseRef-UFL-1.0", # https://tldrlegal.com/license/ubuntu-font-license,-1.0 - no official SPDX, see https://github.com/emilk/egui/issues/2321
|
||||||
|
"MIT-0", # https://choosealicense.com/licenses/mit-0/
|
||||||
|
"MIT", # https://tldrlegal.com/license/mit-license
|
||||||
|
"MPL-2.0", # https://www.mozilla.org/en-US/MPL/2.0/FAQ/ - see Q11. Used by webpki-roots on Linux.
|
||||||
|
"OFL-1.1", # https://spdx.org/licenses/OFL-1.1.html
|
||||||
|
"OpenSSL", # https://www.openssl.org/source/license.html - used on Linux
|
||||||
|
"Unicode-DFS-2016", # https://spdx.org/licenses/Unicode-DFS-2016.html
|
||||||
|
"Zlib", # https://tldrlegal.com/license/zlib-libpng-license-(zlib)
|
||||||
]
|
]
|
||||||
include-archives = true
|
|
||||||
interpreted = "deny"
|
|
||||||
|
|||||||
11
docs/res/ATTRIBUTION.md
Normal file
11
docs/res/ATTRIBUTION.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# 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.
|
||||||
1
docs/res/keyboard_left_shift_key.svg
Normal file
1
docs/res/keyboard_left_shift_key.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 73 KiB |
1
docs/res/keyboard_numpad_1_key.svg
Normal file
1
docs/res/keyboard_numpad_1_key.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 73 KiB |
1
docs/res/keyboard_right_shift_key.svg
Normal file
1
docs/res/keyboard_right_shift_key.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 73 KiB |
1
docs/res/keyboard_standard_1_key.svg
Normal file
1
docs/res/keyboard_standard_1_key.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 73 KiB |
@@ -1,25 +0,0 @@
|
|||||||
# Changelog
|
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
|
||||||
|
|
||||||
Please keep one empty line before and after all headers. (This is required for
|
|
||||||
`git` to produce a conflict when a release is made while a PR is open and the
|
|
||||||
PR's changelog entry would go into the wrong section).
|
|
||||||
|
|
||||||
And please only add new entries to the top of this list, right below the `#
|
|
||||||
Unreleased` header.
|
|
||||||
|
|
||||||
## Unreleased
|
|
||||||
|
|
||||||
## 0.1.2
|
|
||||||
|
|
||||||
- Added `Insets`, `LogicalInsets` and `PhysicalInsets` types.
|
|
||||||
- Make `no_std` compatible. If you use this functionality, DPI's license has changed.
|
|
||||||
|
|
||||||
## 0.1.1
|
|
||||||
|
|
||||||
- Derive `Debug`, `Copy`, `Clone`, `PartialEq`, `Serialize`, `Deserialize` traits for `PixelUnit`.
|
|
||||||
|
|
||||||
## 0.1.0
|
|
||||||
|
|
||||||
- Add `LogicalUnit`, `PhysicalUnit` and `PixelUnit` types and related functions.
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
[package]
|
|
||||||
categories = ["gui"]
|
|
||||||
description = "Types for handling UI scaling"
|
|
||||||
edition.workspace = true
|
|
||||||
keywords = ["DPI", "HiDPI", "scale-factor"]
|
|
||||||
# N.B. This is "AND", because of the imported libm code.
|
|
||||||
license = "Apache-2.0 AND MIT"
|
|
||||||
name = "dpi"
|
|
||||||
repository.workspace = true
|
|
||||||
rust-version.workspace = true
|
|
||||||
version = "0.1.2"
|
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["std"]
|
|
||||||
|
|
||||||
mint = ["dep:mint"]
|
|
||||||
serde = ["dep:serde"]
|
|
||||||
|
|
||||||
# Access mathematical functions using the standard library implementations
|
|
||||||
std = []
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
mint = { workspace = true, optional = true }
|
|
||||||
serde = { workspace = true, optional = true }
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
features = ["mint", "serde"]
|
|
||||||
# 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",
|
|
||||||
]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../LICENSE
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
rust-lang/libm as a whole is available for use under the MIT license:
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
This Rust library contains the following copyrights:
|
|
||||||
|
|
||||||
Copyright (c) 2018 Jorge Aparicio
|
|
||||||
|
|
||||||
Portions of this software are derived from third-party works licensed under
|
|
||||||
terms compatible with the above MIT license:
|
|
||||||
|
|
||||||
* musl libc https://www.musl-libc.org/. This library contains the following
|
|
||||||
copyright:
|
|
||||||
|
|
||||||
Copyright © 2005-2020 Rich Felker, et al.
|
|
||||||
|
|
||||||
* The CORE-MATH project https://core-math.gitlabpages.inria.fr/. CORE-MATH
|
|
||||||
routines are available under the MIT license on a per-file basis.
|
|
||||||
|
|
||||||
The musl libc COPYRIGHT file also includes the following notice relevant to
|
|
||||||
math portions of the library:
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
Much of the math library code (src/math/* and src/complex/*) is
|
|
||||||
Copyright © 1993,2004 Sun Microsystems or
|
|
||||||
Copyright © 2003-2011 David Schultz or
|
|
||||||
Copyright © 2003-2009 Steven G. Kargl or
|
|
||||||
Copyright © 2003-2009 Bruce D. Evans or
|
|
||||||
Copyright © 2008 Stephen L. Moshier or
|
|
||||||
Copyright © 2017-2018 Arm Limited
|
|
||||||
and labelled as such in comments in the individual source files. All
|
|
||||||
have been licensed under extremely permissive terms.
|
|
||||||
------------------------------------------------------------------------------
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
# DPI
|
|
||||||
|
|
||||||
Full docs can be found on docs.rs.
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
Most of DPI is licensed under the Apache License, Version 2.0 ([LICENSE](LICENSE)).
|
|
||||||
All files except for `src/libm.rs` (and `LICENSE-LIBM-MIT`) are available solely under that license.
|
|
||||||
|
|
||||||
For its `no_std` support, DPI uses code from the [libm](https://crates.io/crates/libm) crate.
|
|
||||||
This is in the `libm.rs` file, and is licensed solely under the MIT Licence ([LICENSE-LIBM-MIT](LICENSE-LIBM-MIT)).
|
|
||||||
That file contains details of all potentially applicable copyright notices.
|
|
||||||
This is feature gated to only be included if you disable the `std` feature, otherwise it will not be compiled into your final binary
|
|
||||||
(and so these license terms will not apply).
|
|
||||||
|
|
||||||
Overall, this means that the license for this crate depends on what features you have enabled.
|
|
||||||
If you enable the `std` feature, then DPI uses only code available under the Apache-2.0 license, and so can be used under the terms of that license.
|
|
||||||
However, if you disable the `std` feature, then both these licenses must be followed to use the crate as a whole.
|
|
||||||
1223
dpi/src/lib.rs
1223
dpi/src/lib.rs
File diff suppressed because it is too large
Load Diff
@@ -1,56 +0,0 @@
|
|||||||
// Copyright (c) 2018 Jorge Aparicio
|
|
||||||
// Copyright © 2005-2020 Rich Felker, et al.
|
|
||||||
// Copyright © 1993,2004 Sun Microsystems or
|
|
||||||
// Copyright © 2003-2011 David Schultz or
|
|
||||||
// Copyright © 2003-2009 Steven G. Kargl or
|
|
||||||
// Copyright © 2003-2009 Bruce D. Evans or
|
|
||||||
// Copyright © 2008 Stephen L. Moshier or
|
|
||||||
// Copyright © 2017-2018 Arm Limited
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
// This file is licensed solely under the terms discussed in LICENSE-LIBM-MIT at the crate root.
|
|
||||||
// See the package-level README for full details.
|
|
||||||
|
|
||||||
// Taken from https://github.com/rust-lang/libm/blob/master/src/math/mod.rs#L1
|
|
||||||
macro_rules! force_eval {
|
|
||||||
($e:expr) => {
|
|
||||||
unsafe { ::core::ptr::read_volatile(&$e) }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Taken from https://github.com/rust-lang/libm/blob/libm-v0.2.11/src/math/round.rs
|
|
||||||
pub(crate) fn round(x: f64) -> f64 {
|
|
||||||
trunc(x + copysign(0.5 - 0.25 * f64::EPSILON, x))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adapted from: https://github.com/rust-lang/libm/blob/libm-v0.2.11/src/math/trunc.rs#L8-L12
|
|
||||||
#[allow(clippy::needless_late_init /*, reason = "The original libm code uses this style" */)]
|
|
||||||
fn trunc(x: f64) -> f64 {
|
|
||||||
let x1p120 = f64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120
|
|
||||||
|
|
||||||
let mut i: u64 = x.to_bits();
|
|
||||||
let mut e: i64 = ((i >> 52) & 0x7ff) as i64 - 0x3ff + 12;
|
|
||||||
let m: u64;
|
|
||||||
|
|
||||||
if e >= 52 + 12 {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
if e < 12 {
|
|
||||||
e = 1;
|
|
||||||
}
|
|
||||||
m = -1i64 as u64 >> e;
|
|
||||||
if (i & m) == 0 {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
force_eval!(x + x1p120);
|
|
||||||
i &= !m;
|
|
||||||
f64::from_bits(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Taken from https://github.com/rust-lang/libm/blob/libm-v0.2.11/src/math/copysign.rs
|
|
||||||
fn copysign(x: f64, y: f64) -> f64 {
|
|
||||||
let mut ux = x.to_bits();
|
|
||||||
let uy = y.to_bits();
|
|
||||||
ux &= (!0) >> 1;
|
|
||||||
ux |= uy & (1 << 63);
|
|
||||||
f64::from_bits(ux)
|
|
||||||
}
|
|
||||||
97
examples/child_window.rs
Normal file
97
examples/child_window.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
#[cfg(all(
|
||||||
|
feature = "rwh_06",
|
||||||
|
any(x11_platform, macos_platform, windows_platform)
|
||||||
|
))]
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[cfg(all(
|
||||||
|
feature = "rwh_06",
|
||||||
|
any(x11_platform, macos_platform, windows_platform)
|
||||||
|
))]
|
||||||
|
#[allow(deprecated)]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
dpi::{LogicalPosition, LogicalSize, Position},
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::{EventLoop, EventLoopWindowTarget},
|
||||||
|
raw_window_handle::HasRawWindowHandle,
|
||||||
|
window::{Window, WindowBuilder, WindowId},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn spawn_child_window(
|
||||||
|
parent: &Window,
|
||||||
|
event_loop: &EventLoopWindowTarget<()>,
|
||||||
|
windows: &mut HashMap<WindowId, Window>,
|
||||||
|
) {
|
||||||
|
let parent = parent.raw_window_handle().unwrap();
|
||||||
|
let mut builder = WindowBuilder::new()
|
||||||
|
.with_title("child window")
|
||||||
|
.with_inner_size(LogicalSize::new(200.0f32, 200.0f32))
|
||||||
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
||||||
|
.with_visible(true);
|
||||||
|
// `with_parent_window` is unsafe. Parent window must be a valid window.
|
||||||
|
builder = unsafe { builder.with_parent_window(Some(parent)) };
|
||||||
|
let child_window = builder.build(event_loop).unwrap();
|
||||||
|
|
||||||
|
let id = child_window.id();
|
||||||
|
windows.insert(id, child_window);
|
||||||
|
println!("child window created with id: {id:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut windows = HashMap::new();
|
||||||
|
|
||||||
|
let event_loop: EventLoop<()> = EventLoop::new().unwrap();
|
||||||
|
let parent_window = WindowBuilder::new()
|
||||||
|
.with_title("parent window")
|
||||||
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
||||||
|
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("parent window: {parent_window:?})");
|
||||||
|
|
||||||
|
event_loop.run(move |event: Event<()>, elwt| {
|
||||||
|
if let Event::WindowEvent { event, window_id } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
windows.clear();
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
WindowEvent::CursorEntered { device_id: _ } => {
|
||||||
|
// 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 arround (200, 200) in parent window.
|
||||||
|
println!("cursor entered in the window {window_id:?}");
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
spawn_child_window(&parent_window, elwt, &mut windows);
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
if let Some(window) = windows.get(&window_id) {
|
||||||
|
fill::fill_window(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(all(
|
||||||
|
feature = "rwh_06",
|
||||||
|
any(x11_platform, macos_platform, windows_platform)
|
||||||
|
)))]
|
||||||
|
fn main() {
|
||||||
|
panic!("This example is supported only on x11, macOS, and Windows, with the `rwh_06` feature enabled.");
|
||||||
|
}
|
||||||
128
examples/control_flow.rs
Normal file
128
examples/control_flow.rs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use std::thread;
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
use std::time;
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
use web_time as time;
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
keyboard::{Key, NamedKey},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum Mode {
|
||||||
|
Wait,
|
||||||
|
WaitUntil,
|
||||||
|
Poll,
|
||||||
|
}
|
||||||
|
|
||||||
|
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
|
||||||
|
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
println!("Press '1' to switch to Wait mode.");
|
||||||
|
println!("Press '2' to switch to WaitUntil mode.");
|
||||||
|
println!("Press '3' to switch to Poll mode.");
|
||||||
|
println!("Press 'R' to toggle request_redraw() calls.");
|
||||||
|
println!("Press 'Esc' to close the window.");
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut mode = Mode::Wait;
|
||||||
|
let mut request_redraw = false;
|
||||||
|
let mut wait_cancelled = false;
|
||||||
|
let mut close_requested = false;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
use winit::event::StartCause;
|
||||||
|
println!("{event:?}");
|
||||||
|
match event {
|
||||||
|
Event::NewEvents(start_cause) => {
|
||||||
|
wait_cancelled = match start_cause {
|
||||||
|
StartCause::WaitCancelled { .. } => mode == Mode::WaitUntil,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::WindowEvent { event, .. } => match event {
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
close_requested = true;
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: key,
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => match key.as_ref() {
|
||||||
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
||||||
|
// See the `key_binding` example
|
||||||
|
Key::Character("1") => {
|
||||||
|
mode = Mode::Wait;
|
||||||
|
println!("\nmode: {mode:?}\n");
|
||||||
|
}
|
||||||
|
Key::Character("2") => {
|
||||||
|
mode = Mode::WaitUntil;
|
||||||
|
println!("\nmode: {mode:?}\n");
|
||||||
|
}
|
||||||
|
Key::Character("3") => {
|
||||||
|
mode = Mode::Poll;
|
||||||
|
println!("\nmode: {mode:?}\n");
|
||||||
|
}
|
||||||
|
Key::Character("r") => {
|
||||||
|
request_redraw = !request_redraw;
|
||||||
|
println!("\nrequest_redraw: {request_redraw}\n");
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::Escape) => {
|
||||||
|
close_requested = true;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::AboutToWait => {
|
||||||
|
if request_redraw && !wait_cancelled && !close_requested {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
match mode {
|
||||||
|
Mode::Wait => elwt.set_control_flow(ControlFlow::Wait),
|
||||||
|
Mode::WaitUntil => {
|
||||||
|
if !wait_cancelled {
|
||||||
|
elwt.set_control_flow(ControlFlow::WaitUntil(
|
||||||
|
time::Instant::now() + WAIT_TIME,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::Poll => {
|
||||||
|
thread::sleep(POLL_SLEEP_TIME);
|
||||||
|
elwt.set_control_flow(ControlFlow::Poll);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if close_requested {
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
88
examples/cursor.rs
Normal file
88
examples/cursor.rs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::{CursorIcon, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
window.set_title("A fantastic window!");
|
||||||
|
|
||||||
|
let mut cursor_idx = 0;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]);
|
||||||
|
window.set_cursor_icon(CURSORS[cursor_idx]);
|
||||||
|
if cursor_idx < CURSORS.len() - 1 {
|
||||||
|
cursor_idx += 1;
|
||||||
|
} else {
|
||||||
|
cursor_idx = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const CURSORS: &[CursorIcon] = &[
|
||||||
|
CursorIcon::Default,
|
||||||
|
CursorIcon::Crosshair,
|
||||||
|
CursorIcon::Pointer,
|
||||||
|
CursorIcon::Move,
|
||||||
|
CursorIcon::Text,
|
||||||
|
CursorIcon::Wait,
|
||||||
|
CursorIcon::Help,
|
||||||
|
CursorIcon::Progress,
|
||||||
|
CursorIcon::NotAllowed,
|
||||||
|
CursorIcon::ContextMenu,
|
||||||
|
CursorIcon::Cell,
|
||||||
|
CursorIcon::VerticalText,
|
||||||
|
CursorIcon::Alias,
|
||||||
|
CursorIcon::Copy,
|
||||||
|
CursorIcon::NoDrop,
|
||||||
|
CursorIcon::Grab,
|
||||||
|
CursorIcon::Grabbing,
|
||||||
|
CursorIcon::AllScroll,
|
||||||
|
CursorIcon::ZoomIn,
|
||||||
|
CursorIcon::ZoomOut,
|
||||||
|
CursorIcon::EResize,
|
||||||
|
CursorIcon::NResize,
|
||||||
|
CursorIcon::NeResize,
|
||||||
|
CursorIcon::NwResize,
|
||||||
|
CursorIcon::SResize,
|
||||||
|
CursorIcon::SeResize,
|
||||||
|
CursorIcon::SwResize,
|
||||||
|
CursorIcon::WResize,
|
||||||
|
CursorIcon::EwResize,
|
||||||
|
CursorIcon::NsResize,
|
||||||
|
CursorIcon::NeswResize,
|
||||||
|
CursorIcon::NwseResize,
|
||||||
|
CursorIcon::ColResize,
|
||||||
|
CursorIcon::RowResize,
|
||||||
|
];
|
||||||
73
examples/cursor_grab.rs
Normal file
73
examples/cursor_grab.rs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::{Key, ModifiersState, NamedKey},
|
||||||
|
window::{CursorGrabMode, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Super Cursor Grab'n'Hide Simulator 9000")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut modifiers = ModifiersState::default();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| match event {
|
||||||
|
Event::WindowEvent { event, .. } => match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: key,
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let result = match key {
|
||||||
|
Key::Named(NamedKey::Escape) => {
|
||||||
|
elwt.exit();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Key::Character(ch) => match ch.to_lowercase().as_str() {
|
||||||
|
"g" => window.set_cursor_grab(CursorGrabMode::Confined),
|
||||||
|
"l" => window.set_cursor_grab(CursorGrabMode::Locked),
|
||||||
|
"a" => window.set_cursor_grab(CursorGrabMode::None),
|
||||||
|
"h" => {
|
||||||
|
window.set_cursor_visible(modifiers.shift_key());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
_ => Ok(()),
|
||||||
|
},
|
||||||
|
_ => Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(err) = result {
|
||||||
|
println!("error: {err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::ModifiersChanged(new) => modifiers = new.state(),
|
||||||
|
WindowEvent::RedrawRequested => fill::fill_window(&window),
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::DeviceEvent { event, .. } => match event {
|
||||||
|
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {delta:?}"),
|
||||||
|
DeviceEvent::Button { button, state } => match state {
|
||||||
|
ElementState::Pressed => println!("mouse button {button} pressed"),
|
||||||
|
ElementState::Released => println!("mouse button {button} released"),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
})
|
||||||
|
}
|
||||||
62
examples/custom_events.rs
Normal file
62
examples/custom_events.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoopBuilder,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum CustomEvent {
|
||||||
|
Timer,
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoopBuilder::<CustomEvent>::with_user_event()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// `EventLoopProxy` allows you to dispatch custom events to the main Winit event
|
||||||
|
// loop from any thread.
|
||||||
|
let event_loop_proxy = event_loop.create_proxy();
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
// Wake up the `event_loop` once every second and dispatch a custom event
|
||||||
|
// from a different thread.
|
||||||
|
loop {
|
||||||
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||||
|
event_loop_proxy.send_event(CustomEvent::Timer).ok();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| match event {
|
||||||
|
Event::UserEvent(event) => println!("user event: {event:?}"),
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
..
|
||||||
|
} => elwt.exit(),
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
fn main() {
|
||||||
|
panic!("This example is not supported on web.");
|
||||||
|
}
|
||||||
105
examples/drag_window.rs
Normal file
105
examples/drag_window.rs
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::Key,
|
||||||
|
window::{Window, WindowBuilder, WindowId},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window_1 = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
let window_2 = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
|
||||||
|
let mut switched = false;
|
||||||
|
let mut entered_id = window_2.id();
|
||||||
|
let mut cursor_location = None;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| match event {
|
||||||
|
Event::NewEvents(StartCause::Init) => {
|
||||||
|
eprintln!("Switch which window is to be dragged by pressing \"x\".")
|
||||||
|
}
|
||||||
|
Event::WindowEvent { event, window_id } => match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::CursorMoved { position, .. } => cursor_location = Some(position),
|
||||||
|
WindowEvent::MouseInput { state, button, .. } => {
|
||||||
|
let window = if (window_id == window_1.id() && switched)
|
||||||
|
|| (window_id == window_2.id() && !switched)
|
||||||
|
{
|
||||||
|
&window_2
|
||||||
|
} else {
|
||||||
|
&window_1
|
||||||
|
};
|
||||||
|
|
||||||
|
match (button, state) {
|
||||||
|
(MouseButton::Left, ElementState::Pressed) => window.drag_window().unwrap(),
|
||||||
|
(MouseButton::Right, ElementState::Released) => {
|
||||||
|
if let Some(position) = cursor_location {
|
||||||
|
window.show_window_menu(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::CursorEntered { .. } => {
|
||||||
|
entered_id = window_id;
|
||||||
|
name_windows(entered_id, switched, &window_1, &window_2)
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Released,
|
||||||
|
logical_key: Key::Character(c),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => match c.as_str() {
|
||||||
|
"x" => {
|
||||||
|
switched = !switched;
|
||||||
|
name_windows(entered_id, switched, &window_1, &window_2);
|
||||||
|
println!("Switched!")
|
||||||
|
}
|
||||||
|
"d" => {
|
||||||
|
let window = if (window_id == window_1.id() && switched)
|
||||||
|
|| (window_id == window_2.id() && !switched)
|
||||||
|
{
|
||||||
|
&window_2
|
||||||
|
} else {
|
||||||
|
&window_1
|
||||||
|
};
|
||||||
|
|
||||||
|
window.set_decorations(!window.is_decorated());
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
if window_id == window_1.id() {
|
||||||
|
fill::fill_window(&window_1);
|
||||||
|
} else if window_id == window_2.id() {
|
||||||
|
fill::fill_window(&window_2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name_windows(window_id: WindowId, switched: bool, window_1: &Window, window_2: &Window) {
|
||||||
|
let (drag_target, other) =
|
||||||
|
if (window_id == window_1.id() && switched) || (window_id == window_2.id() && !switched) {
|
||||||
|
(&window_2, &window_1)
|
||||||
|
} else {
|
||||||
|
(&window_1, &window_2)
|
||||||
|
};
|
||||||
|
drag_target.set_title("drag target");
|
||||||
|
other.set_title("winit window");
|
||||||
|
}
|
||||||
56
examples/focus.rs
Normal file
56
examples/focus.rs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
//! Example for focusing a window.
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
use std::time;
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
use web_time as time;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, StartCause, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut deadline = time::Instant::now() + time::Duration::from_secs(3);
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
match event {
|
||||||
|
Event::NewEvents(StartCause::ResumeTimeReached { .. }) => {
|
||||||
|
// Timeout reached; focus the window.
|
||||||
|
println!("Re-focusing the window.");
|
||||||
|
deadline += time::Duration::from_secs(3);
|
||||||
|
window.focus_window();
|
||||||
|
}
|
||||||
|
Event::WindowEvent { event, window_id } if window_id == window.id() => match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
// Notify the windowing system that we'll be presenting to the window.
|
||||||
|
window.pre_present_notify();
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::AboutToWait => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
elwt.set_control_flow(winit::event_loop::ControlFlow::WaitUntil(deadline));
|
||||||
|
})
|
||||||
|
}
|
||||||
163
examples/fullscreen.rs
Normal file
163
examples/fullscreen.rs
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::dpi::PhysicalSize;
|
||||||
|
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
|
||||||
|
use winit::event_loop::EventLoop;
|
||||||
|
use winit::keyboard::{Key, NamedKey};
|
||||||
|
use winit::window::{Fullscreen, WindowBuilder};
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use winit::platform::macos::WindowExtMacOS;
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let mut decorations = true;
|
||||||
|
let mut minimized = false;
|
||||||
|
let mut with_min_size = false;
|
||||||
|
let mut with_max_size = false;
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Hello world!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut monitor_index = 0;
|
||||||
|
let mut monitor = event_loop
|
||||||
|
.available_monitors()
|
||||||
|
.next()
|
||||||
|
.expect("no monitor found!");
|
||||||
|
println!("Monitor: {:?}", monitor.name());
|
||||||
|
|
||||||
|
let mut mode_index = 0;
|
||||||
|
let mut mode = monitor.video_modes().next().expect("no mode found");
|
||||||
|
println!("Mode: {mode}");
|
||||||
|
|
||||||
|
println!("Keys:");
|
||||||
|
println!("- Esc\tExit");
|
||||||
|
println!("- F\tToggle exclusive fullscreen mode");
|
||||||
|
println!("- B\tToggle borderless mode");
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
println!("- C\tToggle simple fullscreen mode");
|
||||||
|
println!("- S\tNext screen");
|
||||||
|
println!("- M\tNext mode for this screen");
|
||||||
|
println!("- D\tToggle window decorations");
|
||||||
|
println!("- X\tMaximize window");
|
||||||
|
println!("- Z\tMinimize window");
|
||||||
|
println!("- I\tToggle mIn size limit");
|
||||||
|
println!("- A\tToggle mAx size limit");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: key,
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => match key {
|
||||||
|
Key::Named(NamedKey::Escape) => elwt.exit(),
|
||||||
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
||||||
|
// See the `key_binding` example
|
||||||
|
Key::Character(ch) => match ch.to_lowercase().as_str() {
|
||||||
|
"f" | "b" if window.fullscreen().is_some() => {
|
||||||
|
window.set_fullscreen(None);
|
||||||
|
}
|
||||||
|
"f" => {
|
||||||
|
let fullscreen = Some(Fullscreen::Exclusive(mode.clone()));
|
||||||
|
println!("Setting mode: {fullscreen:?}");
|
||||||
|
window.set_fullscreen(fullscreen);
|
||||||
|
}
|
||||||
|
"b" => {
|
||||||
|
let fullscreen = Some(Fullscreen::Borderless(Some(monitor.clone())));
|
||||||
|
println!("Setting mode: {fullscreen:?}");
|
||||||
|
window.set_fullscreen(fullscreen);
|
||||||
|
}
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
"c" => {
|
||||||
|
window.set_simple_fullscreen(!window.simple_fullscreen());
|
||||||
|
}
|
||||||
|
"s" => {
|
||||||
|
monitor_index += 1;
|
||||||
|
if let Some(mon) = elwt.available_monitors().nth(monitor_index) {
|
||||||
|
monitor = mon;
|
||||||
|
} else {
|
||||||
|
monitor_index = 0;
|
||||||
|
monitor =
|
||||||
|
elwt.available_monitors().next().expect("no monitor found!");
|
||||||
|
}
|
||||||
|
println!("Monitor: {:?}", monitor.name());
|
||||||
|
|
||||||
|
mode_index = 0;
|
||||||
|
mode = monitor.video_modes().next().expect("no mode found");
|
||||||
|
println!("Mode: {mode}");
|
||||||
|
}
|
||||||
|
"m" => {
|
||||||
|
mode_index += 1;
|
||||||
|
if let Some(m) = monitor.video_modes().nth(mode_index) {
|
||||||
|
mode = m;
|
||||||
|
} else {
|
||||||
|
mode_index = 0;
|
||||||
|
mode = monitor.video_modes().next().expect("no mode found");
|
||||||
|
}
|
||||||
|
println!("Mode: {mode}");
|
||||||
|
}
|
||||||
|
"d" => {
|
||||||
|
decorations = !decorations;
|
||||||
|
window.set_decorations(decorations);
|
||||||
|
}
|
||||||
|
"x" => {
|
||||||
|
let is_maximized = window.is_maximized();
|
||||||
|
window.set_maximized(!is_maximized);
|
||||||
|
}
|
||||||
|
"z" => {
|
||||||
|
minimized = !minimized;
|
||||||
|
window.set_minimized(minimized);
|
||||||
|
}
|
||||||
|
"i" => {
|
||||||
|
with_min_size = !with_min_size;
|
||||||
|
let min_size = if with_min_size {
|
||||||
|
Some(PhysicalSize::new(100, 100))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
window.set_min_inner_size(min_size);
|
||||||
|
eprintln!(
|
||||||
|
"Min: {with_min_size}: {min_size:?} => {:?}",
|
||||||
|
window.inner_size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
"a" => {
|
||||||
|
with_max_size = !with_max_size;
|
||||||
|
let max_size = if with_max_size {
|
||||||
|
Some(PhysicalSize::new(200, 200))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
window.set_max_inner_size(max_size);
|
||||||
|
eprintln!(
|
||||||
|
"Max: {with_max_size}: {max_size:?} => {:?}",
|
||||||
|
window.inner_size()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
86
examples/handling_close.rs
Normal file
86
examples/handling_close.rs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::Key,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Your faithful window")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut close_requested = false;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
// `CloseRequested` is sent when the close button on the window is pressed (or
|
||||||
|
// through whatever other mechanisms the window manager provides for closing a
|
||||||
|
// window). If you don't handle this event, the close button won't actually do
|
||||||
|
// anything.
|
||||||
|
|
||||||
|
// A common thing to do here is prompt the user if they have unsaved work.
|
||||||
|
// Creating a proper dialog box for that is far beyond the scope of this
|
||||||
|
// example, so here we'll just respond to the Y and N keys.
|
||||||
|
println!("Are you ready to bid your window farewell? [Y/N]");
|
||||||
|
close_requested = true;
|
||||||
|
|
||||||
|
// In applications where you can safely close the window without further
|
||||||
|
// action from the user, this is generally where you'd handle cleanup before
|
||||||
|
// closing the window. How to close the window is detailed in the handler for
|
||||||
|
// the Y key.
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: key,
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
||||||
|
// See the `key_binding` example
|
||||||
|
match key.as_ref() {
|
||||||
|
Key::Character("y") => {
|
||||||
|
if close_requested {
|
||||||
|
// This is where you'll want to do any cleanup you need.
|
||||||
|
println!("Buh-bye!");
|
||||||
|
|
||||||
|
// For a single-window application like this, you'd normally just
|
||||||
|
// break out of the event loop here. If you wanted to keep running the
|
||||||
|
// event loop (i.e. if it's a multi-window application), you need to
|
||||||
|
// drop the window. That closes it, and results in `Destroyed` being
|
||||||
|
// sent.
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Key::Character("n") => {
|
||||||
|
if close_requested {
|
||||||
|
println!("Your window will continue to stay by your side.");
|
||||||
|
close_requested = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
94
examples/ime.rs
Normal file
94
examples/ime.rs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use log::LevelFilter;
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
dpi::{PhysicalPosition, PhysicalSize},
|
||||||
|
event::{ElementState, Event, Ime, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::NamedKey,
|
||||||
|
window::{ImePurpose, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new()
|
||||||
|
.with_level(LevelFilter::Trace)
|
||||||
|
.init()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("IME position will system default");
|
||||||
|
println!("Click to set IME position to cursor's");
|
||||||
|
println!("Press F2 to toggle IME. See the documentation of `set_ime_allowed` for more info");
|
||||||
|
println!("Press F3 to cycle through IME purposes.");
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(256f64, 128f64))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut ime_purpose = ImePurpose::Normal;
|
||||||
|
let mut ime_allowed = true;
|
||||||
|
window.set_ime_allowed(ime_allowed);
|
||||||
|
|
||||||
|
let mut may_show_ime = false;
|
||||||
|
let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
|
||||||
|
let mut ime_pos = PhysicalPosition::new(0.0, 0.0);
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
|
cursor_position = position;
|
||||||
|
}
|
||||||
|
WindowEvent::MouseInput {
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
println!(
|
||||||
|
"Setting ime position to {}, {}",
|
||||||
|
cursor_position.x, cursor_position.y
|
||||||
|
);
|
||||||
|
ime_pos = cursor_position;
|
||||||
|
if may_show_ime {
|
||||||
|
window.set_ime_cursor_area(ime_pos, PhysicalSize::new(10, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::Ime(event) => {
|
||||||
|
println!("{event:?}");
|
||||||
|
may_show_ime = event != Ime::Disabled;
|
||||||
|
if may_show_ime {
|
||||||
|
window.set_ime_cursor_area(ime_pos, PhysicalSize::new(10, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput { event, .. } => {
|
||||||
|
println!("key: {event:?}");
|
||||||
|
|
||||||
|
if event.state == ElementState::Pressed && event.logical_key == NamedKey::F2 {
|
||||||
|
ime_allowed = !ime_allowed;
|
||||||
|
window.set_ime_allowed(ime_allowed);
|
||||||
|
println!("\nIME allowed: {ime_allowed}\n");
|
||||||
|
}
|
||||||
|
if event.state == ElementState::Pressed && event.logical_key == NamedKey::F3 {
|
||||||
|
ime_purpose = match ime_purpose {
|
||||||
|
ImePurpose::Normal => ImePurpose::Password,
|
||||||
|
ImePurpose::Password => ImePurpose::Terminal,
|
||||||
|
_ => ImePurpose::Normal,
|
||||||
|
};
|
||||||
|
window.set_ime_purpose(ime_purpose);
|
||||||
|
println!("\nIME purpose: {ime_purpose:?}\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
62
examples/key_binding.rs
Normal file
62
examples/key_binding.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
|
||||||
|
use winit::{
|
||||||
|
dpi::LogicalSize,
|
||||||
|
event::{ElementState, Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::{Key, ModifiersState},
|
||||||
|
// WARNING: This is not available on all platforms (for example on the web).
|
||||||
|
platform::modifier_supplement::KeyEventExtModifierSupplement,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
|
||||||
|
fn main() {
|
||||||
|
println!("This example is not supported on this platform");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_inner_size(LogicalSize::new(400.0, 200.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut modifiers = ModifiersState::default();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::ModifiersChanged(new) => {
|
||||||
|
modifiers = new.state();
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput { event, .. } => {
|
||||||
|
if event.state == ElementState::Pressed && !event.repeat {
|
||||||
|
match event.key_without_modifiers().as_ref() {
|
||||||
|
Key::Character("1") => {
|
||||||
|
if modifiers.shift_key() {
|
||||||
|
println!("Shift + 1 | logical_key: {:?}", event.logical_key);
|
||||||
|
} else {
|
||||||
|
println!("1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
}
|
||||||
58
examples/monitor_list.rs
Normal file
58
examples/monitor_list.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::dpi::{PhysicalPosition, PhysicalSize};
|
||||||
|
use winit::monitor::MonitorHandle;
|
||||||
|
use winit::{event_loop::EventLoop, window::WindowBuilder};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
|
||||||
|
if let Some(mon) = window.primary_monitor() {
|
||||||
|
print_info("Primary output", mon);
|
||||||
|
}
|
||||||
|
|
||||||
|
for mon in window.available_monitors() {
|
||||||
|
if Some(&mon) == window.primary_monitor().as_ref() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!();
|
||||||
|
print_info("Output", mon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_info(intro: &str, monitor: MonitorHandle) {
|
||||||
|
if let Some(name) = monitor.name() {
|
||||||
|
println!("{intro}: {name}");
|
||||||
|
} else {
|
||||||
|
println!("{intro}: [no name]");
|
||||||
|
}
|
||||||
|
|
||||||
|
let PhysicalSize { width, height } = monitor.size();
|
||||||
|
print!(" Current mode: {width}x{height}");
|
||||||
|
if let Some(m_hz) = monitor.refresh_rate_millihertz() {
|
||||||
|
println!(" @ {}.{} Hz", m_hz / 1000, m_hz % 1000);
|
||||||
|
} else {
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let PhysicalPosition { x, y } = monitor.position();
|
||||||
|
println!(" Position: {x},{y}");
|
||||||
|
|
||||||
|
println!(" Scale factor: {}", monitor.scale_factor());
|
||||||
|
|
||||||
|
println!(" Available modes (width x height x bit-depth):");
|
||||||
|
for mode in monitor.video_modes() {
|
||||||
|
let PhysicalSize { width, height } = mode.size();
|
||||||
|
let bits = mode.bit_depth();
|
||||||
|
let m_hz = mode.refresh_rate_millihertz();
|
||||||
|
println!(
|
||||||
|
" {width}x{height}x{bits} @ {}.{} Hz",
|
||||||
|
m_hz / 1000,
|
||||||
|
m_hz % 1000
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
65
examples/mouse_wheel.rs
Normal file
65
examples/mouse_wheel.rs
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Mouse Wheel events")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
r"
|
||||||
|
When using so called 'natural scrolling' (scrolling that acts like on a touch screen), this is what to expect:
|
||||||
|
|
||||||
|
Moving your finger downwards on a scroll wheel should make the window move down, and you should see a positive Y scroll value.
|
||||||
|
|
||||||
|
When moving fingers on a trackpad down and to the right, you should see positive X and Y deltas, and the window should move down and to the right.
|
||||||
|
|
||||||
|
With reverse scrolling, you should see the inverse behavior.
|
||||||
|
|
||||||
|
In both cases the example window should move like the content of a scroll area in any other application.
|
||||||
|
|
||||||
|
In other words, the deltas indicate the direction in which to move the content (in this case the window)."
|
||||||
|
);
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::MouseWheel { delta, .. } => match delta {
|
||||||
|
winit::event::MouseScrollDelta::LineDelta(x, y) => {
|
||||||
|
println!("mouse wheel Line Delta: ({x},{y})");
|
||||||
|
let pixels_per_line = 120.0;
|
||||||
|
let mut pos = window.outer_position().unwrap();
|
||||||
|
pos.x += (x * pixels_per_line) as i32;
|
||||||
|
pos.y += (y * pixels_per_line) as i32;
|
||||||
|
window.set_outer_position(pos)
|
||||||
|
}
|
||||||
|
winit::event::MouseScrollDelta::PixelDelta(p) => {
|
||||||
|
println!("mouse wheel Pixel Delta: ({},{})", p.x, p.y);
|
||||||
|
let mut pos = window.outer_position().unwrap();
|
||||||
|
pos.x += p.x as i32;
|
||||||
|
pos.y += p.y as i32;
|
||||||
|
window.set_outer_position(pos)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
209
examples/multithreaded.rs
Normal file
209
examples/multithreaded.rs
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::{Key, ModifiersState, NamedKey},
|
||||||
|
window::{CursorGrabMode, CursorIcon, Fullscreen, WindowBuilder, WindowLevel},
|
||||||
|
};
|
||||||
|
|
||||||
|
const WINDOW_COUNT: usize = 3;
|
||||||
|
const WINDOW_SIZE: PhysicalSize<u32> = PhysicalSize::new(600, 400);
|
||||||
|
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
let mut window_senders = HashMap::with_capacity(WINDOW_COUNT);
|
||||||
|
for _ in 0..WINDOW_COUNT {
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_inner_size(WINDOW_SIZE)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut video_modes: Vec<_> = window.current_monitor().unwrap().video_modes().collect();
|
||||||
|
let mut video_mode_id = 0usize;
|
||||||
|
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
window_senders.insert(window.id(), tx);
|
||||||
|
let mut modifiers = ModifiersState::default();
|
||||||
|
thread::spawn(move || {
|
||||||
|
while let Ok(event) = rx.recv() {
|
||||||
|
match event {
|
||||||
|
WindowEvent::Moved { .. } => {
|
||||||
|
// We need to update our chosen video mode if the window
|
||||||
|
// was moved to an another monitor, so that the window
|
||||||
|
// appears on this monitor instead when we go fullscreen
|
||||||
|
let previous_video_mode = video_modes.get(video_mode_id).cloned();
|
||||||
|
video_modes = window.current_monitor().unwrap().video_modes().collect();
|
||||||
|
video_mode_id = video_mode_id.min(video_modes.len());
|
||||||
|
let video_mode = video_modes.get(video_mode_id);
|
||||||
|
|
||||||
|
// Different monitors may support different video modes,
|
||||||
|
// and the index we chose previously may now point to a
|
||||||
|
// completely different video mode, so notify the user
|
||||||
|
if video_mode != previous_video_mode.as_ref() {
|
||||||
|
println!(
|
||||||
|
"Window moved to another monitor, picked video mode: {}",
|
||||||
|
video_modes.get(video_mode_id).unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::ModifiersChanged(new) => {
|
||||||
|
modifiers = new.state();
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Released,
|
||||||
|
logical_key: key,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
use NamedKey::{ArrowLeft, ArrowRight};
|
||||||
|
window.set_title(&format!("{key:?}"));
|
||||||
|
let state = !modifiers.shift_key();
|
||||||
|
match key {
|
||||||
|
// Cycle through video modes
|
||||||
|
Key::Named(ArrowRight) | Key::Named(ArrowLeft) => {
|
||||||
|
if key == ArrowLeft {
|
||||||
|
video_mode_id = video_mode_id.saturating_sub(1);
|
||||||
|
} else if key == ArrowRight {
|
||||||
|
video_mode_id = (video_modes.len() - 1).min(video_mode_id + 1);
|
||||||
|
}
|
||||||
|
println!("Picking video mode: {}", video_modes[video_mode_id]);
|
||||||
|
}
|
||||||
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
||||||
|
// See the `key_binding` example
|
||||||
|
Key::Character(ch) => match ch.to_lowercase().as_str() {
|
||||||
|
"1" => window.set_window_level(WindowLevel::AlwaysOnTop),
|
||||||
|
"2" => window.set_window_level(WindowLevel::AlwaysOnBottom),
|
||||||
|
"3" => window.set_window_level(WindowLevel::Normal),
|
||||||
|
"c" => window.set_cursor_icon(match state {
|
||||||
|
true => CursorIcon::Progress,
|
||||||
|
false => CursorIcon::Default,
|
||||||
|
}),
|
||||||
|
"d" => window.set_decorations(!state),
|
||||||
|
"f" => window.set_fullscreen(match (state, modifiers.alt_key()) {
|
||||||
|
(true, false) => Some(Fullscreen::Borderless(None)),
|
||||||
|
(true, true) => Some(Fullscreen::Exclusive(
|
||||||
|
video_modes[video_mode_id].clone(),
|
||||||
|
)),
|
||||||
|
(false, _) => None,
|
||||||
|
}),
|
||||||
|
"l" if state => {
|
||||||
|
if let Err(err) = window.set_cursor_grab(CursorGrabMode::Locked)
|
||||||
|
{
|
||||||
|
println!("error: {err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"g" if state => {
|
||||||
|
if let Err(err) =
|
||||||
|
window.set_cursor_grab(CursorGrabMode::Confined)
|
||||||
|
{
|
||||||
|
println!("error: {err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"g" | "l" if !state => {
|
||||||
|
if let Err(err) = window.set_cursor_grab(CursorGrabMode::None) {
|
||||||
|
println!("error: {err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"h" => window.set_cursor_visible(!state),
|
||||||
|
"i" => {
|
||||||
|
println!("Info:");
|
||||||
|
println!("-> outer_position : {:?}", window.outer_position());
|
||||||
|
println!("-> inner_position : {:?}", window.inner_position());
|
||||||
|
println!("-> outer_size : {:?}", window.outer_size());
|
||||||
|
println!("-> inner_size : {:?}", window.inner_size());
|
||||||
|
println!("-> fullscreen : {:?}", window.fullscreen());
|
||||||
|
}
|
||||||
|
"l" => window.set_min_inner_size(match state {
|
||||||
|
true => Some(WINDOW_SIZE),
|
||||||
|
false => None,
|
||||||
|
}),
|
||||||
|
"m" => window.set_maximized(state),
|
||||||
|
"p" => window.set_outer_position({
|
||||||
|
let mut position = window.outer_position().unwrap();
|
||||||
|
let sign = if state { 1 } else { -1 };
|
||||||
|
position.x += 10 * sign;
|
||||||
|
position.y += 10 * sign;
|
||||||
|
position
|
||||||
|
}),
|
||||||
|
"q" => window.request_redraw(),
|
||||||
|
"r" => window.set_resizable(state),
|
||||||
|
"s" => {
|
||||||
|
let _ = window.request_inner_size(match state {
|
||||||
|
true => PhysicalSize::new(
|
||||||
|
WINDOW_SIZE.width + 100,
|
||||||
|
WINDOW_SIZE.height + 100,
|
||||||
|
),
|
||||||
|
false => WINDOW_SIZE,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
"w" => {
|
||||||
|
if let Size::Physical(size) = WINDOW_SIZE.into() {
|
||||||
|
window
|
||||||
|
.set_cursor_position(Position::Physical(
|
||||||
|
PhysicalPosition::new(
|
||||||
|
size.width as i32 / 2,
|
||||||
|
size.height as i32 / 2,
|
||||||
|
),
|
||||||
|
))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"z" => {
|
||||||
|
window.set_visible(false);
|
||||||
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
window.set_visible(true);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if window_senders.is_empty() {
|
||||||
|
elwt.exit()
|
||||||
|
}
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent { event, window_id } => match event {
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::Destroyed
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Released,
|
||||||
|
logical_key: Key::Named(NamedKey::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
window_senders.remove(&window_id);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if let Some(tx) = window_senders.get(&window_id) {
|
||||||
|
tx.send(event).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
fn main() {
|
||||||
|
panic!("Example not supported on Wasm");
|
||||||
|
}
|
||||||
64
examples/multiwindow.rs
Normal file
64
examples/multiwindow.rs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::{Key, NamedKey},
|
||||||
|
window::Window,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let mut windows = HashMap::new();
|
||||||
|
for _ in 0..3 {
|
||||||
|
let window = Window::new(&event_loop).unwrap();
|
||||||
|
println!("Opened a new window: {:?}", window.id());
|
||||||
|
windows.insert(window.id(), window);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Press N to open a new window.");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, window_id } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
println!("Window {window_id:?} has received the signal to close");
|
||||||
|
|
||||||
|
// This drops the window, causing it to close.
|
||||||
|
windows.remove(&window_id);
|
||||||
|
|
||||||
|
if windows.is_empty() {
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event,
|
||||||
|
is_synthetic: false,
|
||||||
|
..
|
||||||
|
} if event.state == ElementState::Pressed => match event.logical_key {
|
||||||
|
Key::Named(NamedKey::Escape) => elwt.exit(),
|
||||||
|
Key::Character(c) if c == "n" || c == "N" => {
|
||||||
|
let window = Window::new(elwt).unwrap();
|
||||||
|
println!("Opened a new window: {:?}", window.id());
|
||||||
|
windows.insert(window.id(), window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
if let Some(window) = windows.get(&window_id) {
|
||||||
|
fill::fill_window(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
42
examples/request_redraw.rs
Normal file
42
examples/request_redraw.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
println!("{event:?}");
|
||||||
|
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::MouseInput {
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
println!("\nredrawing!\n");
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
59
examples/request_redraw_threaded.rs
Normal file
59
examples/request_redraw_threaded.rs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
use std::{sync::Arc, thread, time};
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = {
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
Arc::new(window)
|
||||||
|
};
|
||||||
|
|
||||||
|
thread::spawn({
|
||||||
|
let window = window.clone();
|
||||||
|
move || loop {
|
||||||
|
thread::sleep(time::Duration::from_secs(1));
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
println!("{event:?}");
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
..
|
||||||
|
} => elwt.exit(),
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
println!("\nredrawing!\n");
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
fn main() {
|
||||||
|
unimplemented!() // `Window` can't be sent between threads
|
||||||
|
}
|
||||||
54
examples/resizable.rs
Normal file
54
examples/resizable.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
dpi::LogicalSize,
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::{KeyCode, PhysicalKey},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let mut resizable = false;
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Hit space to toggle resizability.")
|
||||||
|
.with_inner_size(LogicalSize::new(600.0, 300.0))
|
||||||
|
.with_min_inner_size(LogicalSize::new(400.0, 200.0))
|
||||||
|
.with_max_inner_size(LogicalSize::new(800.0, 400.0))
|
||||||
|
.with_resizable(resizable)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
physical_key: PhysicalKey::Code(KeyCode::Space),
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
resizable = !resizable;
|
||||||
|
println!("Resizable: {resizable}");
|
||||||
|
window.set_resizable(resizable);
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
}
|
||||||
116
examples/startup_notification.rs
Normal file
116
examples/startup_notification.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
//! Demonstrates the use of startup notifications on Linux.
|
||||||
|
|
||||||
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
|
#[path = "./util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
|
mod example {
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
|
||||||
|
use winit::event_loop::EventLoop;
|
||||||
|
use winit::platform::startup_notify::{
|
||||||
|
EventLoopExtStartupNotify, WindowBuilderExtStartupNotify, WindowExtStartupNotify,
|
||||||
|
};
|
||||||
|
use winit::window::{Window, WindowBuilder, WindowId};
|
||||||
|
|
||||||
|
pub(super) fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
// Create the event loop and get the activation token.
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
let mut current_token = match event_loop.read_token_from_env() {
|
||||||
|
Some(token) => Some(token),
|
||||||
|
None => {
|
||||||
|
println!("No startup notification token found in environment.");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut windows: HashMap<WindowId, Rc<Window>> = HashMap::new();
|
||||||
|
let mut counter = 0;
|
||||||
|
let mut create_first_window = false;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
match event {
|
||||||
|
Event::Resumed => create_first_window = true,
|
||||||
|
|
||||||
|
Event::WindowEvent { window_id, event } => match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key,
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if logical_key == "n" {
|
||||||
|
if let Some(window) = windows.get(&window_id) {
|
||||||
|
// Request a new activation token on this window.
|
||||||
|
// Once we get it we will use it to create a window.
|
||||||
|
window
|
||||||
|
.request_activation_token()
|
||||||
|
.expect("Failed to request activation token.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
// Remove the window from the map.
|
||||||
|
windows.remove(&window_id);
|
||||||
|
if windows.is_empty() {
|
||||||
|
elwt.exit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowEvent::ActivationTokenDone { token, .. } => {
|
||||||
|
current_token = Some(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
if let Some(window) = windows.get(&window_id) {
|
||||||
|
super::fill::fill_window(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
// See if we've passed the deadline.
|
||||||
|
if current_token.is_some() || create_first_window {
|
||||||
|
// Create the initial window.
|
||||||
|
let window = {
|
||||||
|
let mut builder =
|
||||||
|
WindowBuilder::new().with_title(format!("Window {}", counter));
|
||||||
|
|
||||||
|
if let Some(token) = current_token.take() {
|
||||||
|
println!("Creating a window with token {token:?}");
|
||||||
|
builder = builder.with_activation_token(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
Rc::new(builder.build(elwt).unwrap())
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add the window to the map.
|
||||||
|
windows.insert(window.id(), window.clone());
|
||||||
|
|
||||||
|
counter += 1;
|
||||||
|
create_first_window = false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
example::main()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(x11_platform, wayland_platform)))]
|
||||||
|
fn main() {
|
||||||
|
println!("This example is only supported on X11 and Wayland platforms.");
|
||||||
|
}
|
||||||
68
examples/theme.rs
Normal file
68
examples/theme.rs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::Key,
|
||||||
|
window::{Theme, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_theme(Some(Theme::Dark))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("Initial theme: {:?}", window.theme());
|
||||||
|
println!("debugging keys:");
|
||||||
|
println!(" (A) Automatic theme");
|
||||||
|
println!(" (L) Light theme");
|
||||||
|
println!(" (D) Dark theme");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { window_id, event } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::ThemeChanged(theme) if window_id == window.id() => {
|
||||||
|
println!("Theme is changed: {theme:?}")
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: key,
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => match key.as_ref() {
|
||||||
|
Key::Character("A" | "a") => {
|
||||||
|
println!("Theme was: {:?}", window.theme());
|
||||||
|
window.set_theme(None);
|
||||||
|
}
|
||||||
|
Key::Character("L" | "l") => {
|
||||||
|
println!("Theme was: {:?}", window.theme());
|
||||||
|
window.set_theme(Some(Theme::Light));
|
||||||
|
}
|
||||||
|
Key::Character("D" | "d") => {
|
||||||
|
println!("Theme was: {:?}", window.theme());
|
||||||
|
window.set_theme(Some(Theme::Dark));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
println!("\nredrawing!\n");
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
54
examples/timer.rs
Normal file
54
examples/timer.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
use std::time::Instant;
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
use web_time::Instant;
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, StartCause, WindowEvent},
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let timer_length = Duration::new(1, 0);
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
println!("{event:?}");
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::NewEvents(StartCause::Init) => {
|
||||||
|
elwt.set_control_flow(ControlFlow::WaitUntil(Instant::now() + timer_length));
|
||||||
|
}
|
||||||
|
Event::NewEvents(StartCause::ResumeTimeReached { .. }) => {
|
||||||
|
elwt.set_control_flow(ControlFlow::WaitUntil(Instant::now() + timer_length));
|
||||||
|
println!("\nTimer\n");
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
..
|
||||||
|
} => elwt.exit(),
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
50
examples/touchpad_gestures.rs
Normal file
50
examples/touchpad_gestures.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Touchpad gestures")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("Only supported on macOS at the moment.");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::TouchpadMagnify { delta, .. } => {
|
||||||
|
if delta > 0.0 {
|
||||||
|
println!("Zoomed in {delta}");
|
||||||
|
} else {
|
||||||
|
println!("Zoomed out {delta}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::SmartMagnify { .. } => {
|
||||||
|
println!("Smart zoom");
|
||||||
|
}
|
||||||
|
WindowEvent::TouchpadRotate { delta, .. } => {
|
||||||
|
if delta > 0.0 {
|
||||||
|
println!("Rotated counterclockwise {delta}");
|
||||||
|
} else {
|
||||||
|
println!("Rotated clockwise {delta}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
38
examples/transparent.rs
Normal file
38
examples/transparent.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_decorations(false)
|
||||||
|
.with_transparent(true)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
window.set_title("A fantastic window!");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
println!("{event:?}");
|
||||||
|
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
116
examples/util/fill.rs
Normal file
116
examples/util/fill.rs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
//! 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;
|
||||||
|
pub use platform::fill_window;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "rwh_05", not(any(target_os = "android", target_os = "ios"))))]
|
||||||
|
mod platform {
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::mem::ManuallyDrop;
|
||||||
|
use std::num::NonZeroU32;
|
||||||
|
|
||||||
|
use softbuffer::{Context, Surface};
|
||||||
|
use winit::window::Window;
|
||||||
|
use winit::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>>> = ManuallyDrop::new(RefCell::new(None));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The graphics context used to draw to a window.
|
||||||
|
struct GraphicsContext {
|
||||||
|
/// The global softbuffer context.
|
||||||
|
context: Context,
|
||||||
|
|
||||||
|
/// The hash map of window IDs to surfaces.
|
||||||
|
surfaces: HashMap<WindowId, Surface>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GraphicsContext {
|
||||||
|
fn new(w: &Window) -> Self {
|
||||||
|
Self {
|
||||||
|
context: unsafe { Context::new(w) }.expect("Failed to create a softbuffer context"),
|
||||||
|
surfaces: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_surface(&mut self, window: &Window) -> &mut Surface {
|
||||||
|
self.surfaces.entry(window.id()).or_insert_with(|| {
|
||||||
|
unsafe { Surface::new(&self.context, window) }
|
||||||
|
.expect("Failed to create a softbuffer surface")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn destroy_surface(&mut self, window: &Window) {
|
||||||
|
self.surfaces.remove(&window.id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fill_window(window: &Window) {
|
||||||
|
GC.with(|gc| {
|
||||||
|
let size = window.inner_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.
|
||||||
|
const DARK_GRAY: u32 = 0xFF181818;
|
||||||
|
|
||||||
|
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(DARK_GRAY);
|
||||||
|
buffer
|
||||||
|
.present()
|
||||||
|
.expect("Failed to present the softbuffer buffer");
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn cleanup_window(window: &Window) {
|
||||||
|
GC.with(|gc| {
|
||||||
|
let mut gc = gc.borrow_mut();
|
||||||
|
if let Some(context) = gc.as_mut() {
|
||||||
|
context.destroy_surface(window);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(all(feature = "rwh_05", not(any(target_os = "android", target_os = "ios")))))]
|
||||||
|
mod platform {
|
||||||
|
pub fn fill_window(_window: &winit::window::Window) {
|
||||||
|
// No-op on mobile platforms.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn cleanup_window(_window: &winit::window::Window) {
|
||||||
|
// No-op on mobile platforms.
|
||||||
|
}
|
||||||
|
}
|
||||||
22
examples/video_modes.rs
Normal file
22
examples/video_modes.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::event_loop::EventLoop;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
let monitor = match event_loop.primary_monitor() {
|
||||||
|
Some(monitor) => monitor,
|
||||||
|
None => {
|
||||||
|
println!("No primary monitor detected.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Listing available video modes:");
|
||||||
|
|
||||||
|
for mode in monitor.video_modes() {
|
||||||
|
println!("{mode}");
|
||||||
|
}
|
||||||
|
}
|
||||||
147
examples/web.rs
Normal file
147
examples/web.rs
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
#![allow(clippy::disallowed_methods, clippy::single_match)]
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::Key,
|
||||||
|
window::{Fullscreen, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let builder = WindowBuilder::new().with_title("A fantastic window!");
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
let builder = {
|
||||||
|
use winit::platform::web::WindowBuilderExtWebSys;
|
||||||
|
builder.with_append(true)
|
||||||
|
};
|
||||||
|
let window = builder.build(&event_loop).unwrap();
|
||||||
|
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
let log_list = wasm::insert_canvas_and_create_log_list(&window);
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
wasm::log_event(&log_list, &event);
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => elwt.exit(),
|
||||||
|
Event::AboutToWait => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
window_id,
|
||||||
|
event:
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Character(c),
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
},
|
||||||
|
} if window_id == window.id() && c == "f" => {
|
||||||
|
if window.fullscreen().is_some() {
|
||||||
|
window.set_fullscreen(None);
|
||||||
|
} else {
|
||||||
|
window.set_fullscreen(Some(Fullscreen::Borderless(None)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
mod wasm {
|
||||||
|
use std::num::NonZeroU32;
|
||||||
|
|
||||||
|
use softbuffer::{Surface, SurfaceExtWeb};
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
window::Window,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[wasm_bindgen(start)]
|
||||||
|
pub fn run() {
|
||||||
|
console_log::init_with_level(log::Level::Debug).expect("error initializing logger");
|
||||||
|
|
||||||
|
#[allow(clippy::main_recursion)]
|
||||||
|
let _ = super::main();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_canvas_and_create_log_list(window: &Window) -> web_sys::Element {
|
||||||
|
use winit::platform::web::WindowExtWebSys;
|
||||||
|
|
||||||
|
let canvas = window.canvas().unwrap();
|
||||||
|
let mut surface = Surface::from_canvas(canvas.clone()).unwrap();
|
||||||
|
surface
|
||||||
|
.resize(
|
||||||
|
NonZeroU32::new(canvas.width()).unwrap(),
|
||||||
|
NonZeroU32::new(canvas.height()).unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let mut buffer = surface.buffer_mut().unwrap();
|
||||||
|
buffer.fill(0xFFF0000);
|
||||||
|
buffer.present().unwrap();
|
||||||
|
|
||||||
|
let window = web_sys::window().unwrap();
|
||||||
|
let document = window.document().unwrap();
|
||||||
|
let body = document.body().unwrap();
|
||||||
|
|
||||||
|
let style = &canvas.style();
|
||||||
|
style.set_property("margin", "50px").unwrap();
|
||||||
|
// Use to test interactions with border and padding.
|
||||||
|
//style.set_property("border", "50px solid black").unwrap();
|
||||||
|
//style.set_property("padding", "50px").unwrap();
|
||||||
|
|
||||||
|
let log_header = document.create_element("h2").unwrap();
|
||||||
|
log_header.set_text_content(Some("Event Log"));
|
||||||
|
body.append_child(&log_header).unwrap();
|
||||||
|
|
||||||
|
let log_list = document.create_element("ul").unwrap();
|
||||||
|
body.append_child(&log_list).unwrap();
|
||||||
|
log_list
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_event(log_list: &web_sys::Element, event: &Event<()>) {
|
||||||
|
log::debug!("{:?}", event);
|
||||||
|
|
||||||
|
// Getting access to browser logs requires a lot of setup on mobile devices.
|
||||||
|
// So we implement this basic logging system into the page to give developers an easy alternative.
|
||||||
|
// As a bonus its also kind of handy on desktop.
|
||||||
|
let event = match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => None,
|
||||||
|
Event::WindowEvent { event, .. } => Some(format!("{event:?}")),
|
||||||
|
Event::Resumed | Event::Suspended => Some(format!("{event:?}")),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
if let Some(event) = event {
|
||||||
|
let window = web_sys::window().unwrap();
|
||||||
|
let document = window.document().unwrap();
|
||||||
|
let log = document.create_element("li").unwrap();
|
||||||
|
|
||||||
|
let date = js_sys::Date::new_0();
|
||||||
|
log.set_text_content(Some(&format!(
|
||||||
|
"{:02}:{:02}:{:02}.{:03}: {event}",
|
||||||
|
date.get_hours(),
|
||||||
|
date.get_minutes(),
|
||||||
|
date.get_seconds(),
|
||||||
|
date.get_milliseconds(),
|
||||||
|
)));
|
||||||
|
|
||||||
|
log_list
|
||||||
|
.insert_before(&log, log_list.first_child().as_ref())
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
102
examples/web_aspect_ratio.rs
Normal file
102
examples/web_aspect_ratio.rs
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#![allow(clippy::disallowed_methods)]
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
println!("This example must be run with cargo run-wasm --example web_aspect_ratio")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
mod wasm {
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
use web_sys::HtmlCanvasElement;
|
||||||
|
use winit::{
|
||||||
|
dpi::PhysicalSize,
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
platform::web::WindowBuilderExtWebSys,
|
||||||
|
window::{Window, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
const EXPLANATION: &str = "
|
||||||
|
This example draws a circle in the middle of a 4/1 aspect ratio canvas which acts as a useful demonstration of winit's resize handling on web.
|
||||||
|
Even when the browser window is resized or aspect-ratio of the canvas changed the circle should always:
|
||||||
|
* Fill the entire width or height of the canvas (whichever is smaller) without exceeding it.
|
||||||
|
* Be perfectly round
|
||||||
|
* Not be blurry or pixelated (there is no antialiasing so you may still see jagged edges depending on the DPI of your monitor)
|
||||||
|
|
||||||
|
Currently winit does not handle resizes on web so the circle is rendered incorrectly.
|
||||||
|
This example demonstrates the desired future functionality which will possibly be provided by https://github.com/rust-windowing/winit/pull/2074
|
||||||
|
";
|
||||||
|
|
||||||
|
#[wasm_bindgen(start)]
|
||||||
|
pub fn run() {
|
||||||
|
console_log::init_with_level(log::Level::Debug).expect("error initializing logger");
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
// When running in a non-wasm environment this would set the window size to 100x100.
|
||||||
|
// However in this example it just sets a default initial size of 100x100 that is immediately overwritten due to the layout + styling of the page.
|
||||||
|
.with_inner_size(PhysicalSize::new(100, 100))
|
||||||
|
.with_append(true)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let canvas = create_canvas(&window);
|
||||||
|
|
||||||
|
// Render once with the size info we currently have
|
||||||
|
render_circle(&canvas, window.inner_size());
|
||||||
|
|
||||||
|
let _ = event_loop.run(move |event, _| match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::Resized(resize),
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => {
|
||||||
|
render_circle(&canvas, resize);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_canvas(window: &Window) -> HtmlCanvasElement {
|
||||||
|
use winit::platform::web::WindowExtWebSys;
|
||||||
|
|
||||||
|
let web_window = web_sys::window().unwrap();
|
||||||
|
let document = web_window.document().unwrap();
|
||||||
|
let body = document.body().unwrap();
|
||||||
|
|
||||||
|
// Set a background color for the canvas to make it easier to tell the where the canvas is for debugging purposes.
|
||||||
|
let canvas = window.canvas().unwrap();
|
||||||
|
canvas
|
||||||
|
.style()
|
||||||
|
.set_css_text("display: block; background-color: crimson; margin: auto; width: 50%; aspect-ratio: 4 / 1;");
|
||||||
|
|
||||||
|
let explanation = document.create_element("pre").unwrap();
|
||||||
|
explanation.set_text_content(Some(EXPLANATION));
|
||||||
|
body.append_child(&explanation).unwrap();
|
||||||
|
|
||||||
|
canvas
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_circle(canvas: &HtmlCanvasElement, size: PhysicalSize<u32>) {
|
||||||
|
log::info!("rendering circle with canvas size: {:?}", size);
|
||||||
|
let context = canvas
|
||||||
|
.get_context("2d")
|
||||||
|
.unwrap()
|
||||||
|
.unwrap()
|
||||||
|
.dyn_into::<web_sys::CanvasRenderingContext2d>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
context.begin_path();
|
||||||
|
context
|
||||||
|
.arc(
|
||||||
|
size.width as f64 / 2.0,
|
||||||
|
size.height as f64 / 2.0,
|
||||||
|
size.width.min(size.height) as f64 / 2.0,
|
||||||
|
0.0,
|
||||||
|
std::f64::consts::PI * 2.0,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
context.fill();
|
||||||
|
}
|
||||||
|
}
|
||||||
43
examples/window.rs
Normal file
43
examples/window.rs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
println!("{event:?}");
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent { event, window_id } if window_id == window.id() => match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
// Notify the windowing system that we'll be presenting to the window.
|
||||||
|
window.pre_present_notify();
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::AboutToWait => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
68
examples/window_buttons.rs
Normal file
68
examples/window_buttons.rs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
// This example is used by developers to test various window functions.
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
dpi::LogicalSize,
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::{DeviceEvents, EventLoop},
|
||||||
|
keyboard::Key,
|
||||||
|
window::{WindowBuilder, WindowButtons},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_inner_size(LogicalSize::new(300.0, 300.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
eprintln!("Window Button keys:");
|
||||||
|
eprintln!(" (F) Toggle close button");
|
||||||
|
eprintln!(" (G) Toggle maximize button");
|
||||||
|
eprintln!(" (H) Toggle minimize button");
|
||||||
|
|
||||||
|
event_loop.listen_device_events(DeviceEvents::Always);
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { window_id, event } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: key,
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => match key.as_ref() {
|
||||||
|
Key::Character("F" | "f") => {
|
||||||
|
let buttons = window.enabled_buttons();
|
||||||
|
window.set_enabled_buttons(buttons ^ WindowButtons::CLOSE);
|
||||||
|
}
|
||||||
|
Key::Character("G" | "g") => {
|
||||||
|
let buttons = window.enabled_buttons();
|
||||||
|
window.set_enabled_buttons(buttons ^ WindowButtons::MAXIMIZE);
|
||||||
|
}
|
||||||
|
Key::Character("H" | "h") => {
|
||||||
|
let buttons = window.enabled_buttons();
|
||||||
|
window.set_enabled_buttons(buttons ^ WindowButtons::MINIMIZE);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::CloseRequested if window_id == window.id() => elwt.exit(),
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
137
examples/window_debug.rs
Normal file
137
examples/window_debug.rs
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
// This example is used by developers to test various window functions.
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
dpi::{LogicalSize, PhysicalSize},
|
||||||
|
event::{DeviceEvent, ElementState, Event, KeyEvent, RawKeyEvent, WindowEvent},
|
||||||
|
event_loop::{DeviceEvents, EventLoop},
|
||||||
|
keyboard::{Key, KeyCode, PhysicalKey},
|
||||||
|
window::{Fullscreen, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_inner_size(LogicalSize::new(100.0, 100.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
eprintln!("debugging keys:");
|
||||||
|
eprintln!(" (E) Enter exclusive fullscreen");
|
||||||
|
eprintln!(" (F) Toggle borderless fullscreen");
|
||||||
|
eprintln!(" (P) Toggle borderless fullscreen on system's preffered monitor");
|
||||||
|
eprintln!(" (M) Toggle minimized");
|
||||||
|
eprintln!(" (Q) Quit event loop");
|
||||||
|
eprintln!(" (V) Toggle visibility");
|
||||||
|
eprintln!(" (X) Toggle maximized");
|
||||||
|
|
||||||
|
let mut minimized = false;
|
||||||
|
let mut visible = true;
|
||||||
|
|
||||||
|
event_loop.listen_device_events(DeviceEvents::Always);
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
match event {
|
||||||
|
// This used to use the virtual key, but the new API
|
||||||
|
// only provides the `physical_key` (`Code`).
|
||||||
|
Event::DeviceEvent {
|
||||||
|
event:
|
||||||
|
DeviceEvent::Key(RawKeyEvent {
|
||||||
|
physical_key,
|
||||||
|
state: ElementState::Released,
|
||||||
|
..
|
||||||
|
}),
|
||||||
|
..
|
||||||
|
} => match physical_key {
|
||||||
|
PhysicalKey::Code(KeyCode::KeyM) => {
|
||||||
|
if minimized {
|
||||||
|
minimized = !minimized;
|
||||||
|
window.set_minimized(minimized);
|
||||||
|
window.focus_window();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PhysicalKey::Code(KeyCode::KeyV) => {
|
||||||
|
if !visible {
|
||||||
|
visible = !visible;
|
||||||
|
window.set_visible(visible);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::WindowEvent { window_id, event } => match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
logical_key: Key::Character(key_str),
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => match key_str.as_ref() {
|
||||||
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
||||||
|
// See the `key_binding` example
|
||||||
|
"e" => {
|
||||||
|
fn area(size: PhysicalSize<u32>) -> u32 {
|
||||||
|
size.width * size.height
|
||||||
|
}
|
||||||
|
|
||||||
|
let monitor = window.current_monitor().unwrap();
|
||||||
|
if let Some(mode) = monitor
|
||||||
|
.video_modes()
|
||||||
|
.max_by(|a, b| area(a.size()).cmp(&area(b.size())))
|
||||||
|
{
|
||||||
|
window.set_fullscreen(Some(Fullscreen::Exclusive(mode)));
|
||||||
|
} else {
|
||||||
|
eprintln!("no video modes available");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"f" => {
|
||||||
|
if window.fullscreen().is_some() {
|
||||||
|
window.set_fullscreen(None);
|
||||||
|
} else {
|
||||||
|
let monitor = window.current_monitor();
|
||||||
|
window.set_fullscreen(Some(Fullscreen::Borderless(monitor)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"p" => {
|
||||||
|
if window.fullscreen().is_some() {
|
||||||
|
window.set_fullscreen(None);
|
||||||
|
} else {
|
||||||
|
window.set_fullscreen(Some(Fullscreen::Borderless(None)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"m" => {
|
||||||
|
minimized = !minimized;
|
||||||
|
window.set_minimized(minimized);
|
||||||
|
}
|
||||||
|
"q" => {
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
"v" => {
|
||||||
|
visible = !visible;
|
||||||
|
window.set_visible(visible);
|
||||||
|
}
|
||||||
|
"x" => {
|
||||||
|
let is_maximized = window.is_maximized();
|
||||||
|
window.set_maximized(!is_maximized);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::CloseRequested if window_id == window.id() => elwt.exit(),
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
149
examples/window_drag_resize.rs
Normal file
149
examples/window_drag_resize.rs
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
//! Demonstrates capability to create in-app draggable regions for client-side decoration support.
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::Key,
|
||||||
|
window::{CursorIcon, ResizeDirection, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
const BORDER: f64 = 8.0;
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(600.0, 400.0))
|
||||||
|
.with_min_inner_size(winit::dpi::LogicalSize::new(400.0, 200.0))
|
||||||
|
.with_decorations(false)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut border = false;
|
||||||
|
let mut cursor_location = None;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| match event {
|
||||||
|
Event::NewEvents(StartCause::Init) => {
|
||||||
|
eprintln!("Press 'B' to toggle borderless")
|
||||||
|
}
|
||||||
|
Event::WindowEvent { event, .. } => match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
|
if !window.is_decorated() {
|
||||||
|
let new_location =
|
||||||
|
cursor_resize_direction(window.inner_size(), position, BORDER);
|
||||||
|
|
||||||
|
if new_location != cursor_location {
|
||||||
|
cursor_location = new_location;
|
||||||
|
window.set_cursor_icon(cursor_direction_icon(cursor_location))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowEvent::MouseInput {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
button: MouseButton::Left,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if let Some(dir) = cursor_location {
|
||||||
|
let _res = window.drag_resize_window(dir);
|
||||||
|
} else if !window.is_decorated() {
|
||||||
|
let _res = window.drag_window();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Released,
|
||||||
|
logical_key: Key::Character(c),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} if matches!(c.as_ref(), "B" | "b") => {
|
||||||
|
border = !border;
|
||||||
|
window.set_decorations(border);
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cursor_direction_icon(resize_direction: Option<ResizeDirection>) -> CursorIcon {
|
||||||
|
match resize_direction {
|
||||||
|
Some(resize_direction) => match resize_direction {
|
||||||
|
ResizeDirection::East => CursorIcon::EResize,
|
||||||
|
ResizeDirection::North => CursorIcon::NResize,
|
||||||
|
ResizeDirection::NorthEast => CursorIcon::NeResize,
|
||||||
|
ResizeDirection::NorthWest => CursorIcon::NwResize,
|
||||||
|
ResizeDirection::South => CursorIcon::SResize,
|
||||||
|
ResizeDirection::SouthEast => CursorIcon::SeResize,
|
||||||
|
ResizeDirection::SouthWest => CursorIcon::SwResize,
|
||||||
|
ResizeDirection::West => CursorIcon::WResize,
|
||||||
|
},
|
||||||
|
None => CursorIcon::Default,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cursor_resize_direction(
|
||||||
|
win_size: winit::dpi::PhysicalSize<u32>,
|
||||||
|
position: winit::dpi::PhysicalPosition<f64>,
|
||||||
|
border_size: f64,
|
||||||
|
) -> Option<ResizeDirection> {
|
||||||
|
enum XDirection {
|
||||||
|
West,
|
||||||
|
East,
|
||||||
|
Default,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum YDirection {
|
||||||
|
North,
|
||||||
|
South,
|
||||||
|
Default,
|
||||||
|
}
|
||||||
|
|
||||||
|
let xdir = if position.x < border_size {
|
||||||
|
XDirection::West
|
||||||
|
} else if position.x > (win_size.width as f64 - border_size) {
|
||||||
|
XDirection::East
|
||||||
|
} else {
|
||||||
|
XDirection::Default
|
||||||
|
};
|
||||||
|
|
||||||
|
let ydir = if position.y < border_size {
|
||||||
|
YDirection::North
|
||||||
|
} else if position.y > (win_size.height as f64 - border_size) {
|
||||||
|
YDirection::South
|
||||||
|
} else {
|
||||||
|
YDirection::Default
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(match xdir {
|
||||||
|
XDirection::West => match ydir {
|
||||||
|
YDirection::North => ResizeDirection::NorthWest,
|
||||||
|
YDirection::South => ResizeDirection::SouthWest,
|
||||||
|
YDirection::Default => ResizeDirection::West,
|
||||||
|
},
|
||||||
|
|
||||||
|
XDirection::East => match ydir {
|
||||||
|
YDirection::North => ResizeDirection::NorthEast,
|
||||||
|
YDirection::South => ResizeDirection::SouthEast,
|
||||||
|
YDirection::Default => ResizeDirection::East,
|
||||||
|
},
|
||||||
|
|
||||||
|
XDirection::Default => match ydir {
|
||||||
|
YDirection::North => ResizeDirection::North,
|
||||||
|
YDirection::South => ResizeDirection::South,
|
||||||
|
YDirection::Default => return None,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
60
examples/window_icon.rs
Normal file
60
examples/window_icon.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::{Icon, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().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 we use 32px,
|
||||||
|
// since it seems to work well enough in most cases. Be careful about going too high, or
|
||||||
|
// you'll be bitten by the low-quality downscaling built into the WM.
|
||||||
|
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
|
||||||
|
|
||||||
|
let icon = load_icon(Path::new(path));
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("An iconic window!")
|
||||||
|
// At present, this only does anything on Windows and X11, so if you want to save load
|
||||||
|
// time, you can put icon loading behind a function that returns `None` on other platforms.
|
||||||
|
.with_window_icon(Some(icon))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::DroppedFile(path) => {
|
||||||
|
window.set_window_icon(Some(load_icon(&path)));
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => fill::fill_window(&window),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_icon(path: &Path) -> Icon {
|
||||||
|
let (icon_rgba, icon_width, icon_height) = {
|
||||||
|
let image = image::open(path)
|
||||||
|
.expect("Failed to open icon path")
|
||||||
|
.into_rgba8();
|
||||||
|
let (width, height) = image.dimensions();
|
||||||
|
let rgba = image.into_raw();
|
||||||
|
(rgba, width, height)
|
||||||
|
};
|
||||||
|
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
|
||||||
|
}
|
||||||
93
examples/window_on_demand.rs
Normal file
93
examples/window_on_demand.rs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
// Limit this example to only compatible platforms.
|
||||||
|
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform,))]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
error::EventLoopError,
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
platform::run_on_demand::EventLoopExtRunOnDemand,
|
||||||
|
window::{Window, WindowBuilder, WindowId},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct App {
|
||||||
|
window_id: Option<WindowId>,
|
||||||
|
window: Option<Window>,
|
||||||
|
}
|
||||||
|
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let mut event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
fn run_app(event_loop: &mut EventLoop<()>, idx: usize) -> Result<(), EventLoopError> {
|
||||||
|
let mut app = App::default();
|
||||||
|
|
||||||
|
event_loop.run_on_demand(move |event, elwt| {
|
||||||
|
println!("Run {idx}: {:?}", event);
|
||||||
|
|
||||||
|
if let Some(window) = &app.window {
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
window_id,
|
||||||
|
} if window.id() == window_id => {
|
||||||
|
println!("--------------------------------------------------------- Window {idx} CloseRequested");
|
||||||
|
fill::cleanup_window(window);
|
||||||
|
app.window = None;
|
||||||
|
}
|
||||||
|
Event::AboutToWait => window.request_redraw(),
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
fill::fill_window(window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
} else if let Some(id) = app.window_id {
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::Destroyed,
|
||||||
|
window_id,
|
||||||
|
} if id == window_id => {
|
||||||
|
println!("--------------------------------------------------------- Window {idx} Destroyed");
|
||||||
|
app.window_id = None;
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
} else if let Event::Resumed = event {
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Fantastic window number one!")
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||||
|
.build(elwt)
|
||||||
|
.unwrap();
|
||||||
|
app.window_id = Some(window.id());
|
||||||
|
app.window = Some(window);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
run_app(&mut event_loop, 1)?;
|
||||||
|
|
||||||
|
println!("--------------------------------------------------------- Finished first loop");
|
||||||
|
println!("--------------------------------------------------------- Waiting 5 seconds");
|
||||||
|
std::thread::sleep(Duration::from_secs(5));
|
||||||
|
|
||||||
|
let ret = run_app(&mut event_loop, 2);
|
||||||
|
println!("--------------------------------------------------------- Finished second loop");
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(windows_platform, macos_platform, x11_platform, wayland_platform,)))]
|
||||||
|
fn main() {
|
||||||
|
println!("This example is not supported on this platform");
|
||||||
|
}
|
||||||
72
examples/window_option_as_alt.rs
Normal file
72
examples/window_option_as_alt.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use winit::platform::macos::{OptionAsAlt, WindowExtMacOS};
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use winit::{
|
||||||
|
event::ElementState,
|
||||||
|
event::{Event, MouseButton, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
/// Prints the keyboard events characters received when option_is_alt is true versus false.
|
||||||
|
/// A left mouse click will toggle option_is_alt.
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
window.set_ime_allowed(true);
|
||||||
|
|
||||||
|
let mut option_as_alt = window.option_as_alt();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => elwt.exit(),
|
||||||
|
Event::WindowEvent { event, .. } => match event {
|
||||||
|
WindowEvent::MouseInput {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
button: MouseButton::Left,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
option_as_alt = match option_as_alt {
|
||||||
|
OptionAsAlt::None => OptionAsAlt::OnlyLeft,
|
||||||
|
OptionAsAlt::OnlyLeft => OptionAsAlt::OnlyRight,
|
||||||
|
OptionAsAlt::OnlyRight => OptionAsAlt::Both,
|
||||||
|
OptionAsAlt::Both => OptionAsAlt::None,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Received Mouse click, toggling option_as_alt to: {option_as_alt:?}");
|
||||||
|
window.set_option_as_alt(option_as_alt);
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput { .. } => println!("KeyboardInput: {event:?}"),
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::AboutToWait => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
fn main() {
|
||||||
|
println!("This example is only supported on MacOS");
|
||||||
|
}
|
||||||
74
examples/window_pump_events.rs
Normal file
74
examples/window_pump_events.rs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
// Limit this example to only compatible platforms.
|
||||||
|
#[cfg(any(
|
||||||
|
windows_platform,
|
||||||
|
macos_platform,
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform,
|
||||||
|
android_platform,
|
||||||
|
))]
|
||||||
|
fn main() -> std::process::ExitCode {
|
||||||
|
use std::{process::ExitCode, thread::sleep, time::Duration};
|
||||||
|
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
platform::pump_events::{EventLoopExtPumpEvents, PumpStatus},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
let mut event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
'main: loop {
|
||||||
|
let timeout = Some(Duration::ZERO);
|
||||||
|
let status = event_loop.pump_events(timeout, |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, .. } = &event {
|
||||||
|
// Print only Window events to reduce noise
|
||||||
|
println!("{event:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => elwt.exit(),
|
||||||
|
Event::AboutToWait => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if let PumpStatus::Exit(exit_code) = status {
|
||||||
|
break 'main ExitCode::from(exit_code as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sleep for 1/60 second to simulate application work
|
||||||
|
//
|
||||||
|
// Since `pump_events` doesn't block it will be important to
|
||||||
|
// throttle the loop in the app somehow.
|
||||||
|
println!("Update()");
|
||||||
|
sleep(Duration::from_millis(16));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(ios_platform, wasm_platform, orbital_platform))]
|
||||||
|
fn main() {
|
||||||
|
println!("This platform doesn't support pump_events.");
|
||||||
|
}
|
||||||
52
examples/window_resize_increments.rs
Normal file
52
examples/window_resize_increments.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
use log::debug;
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
dpi::LogicalSize,
|
||||||
|
event::{ElementState, Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::NamedKey,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("A fantastic window!")
|
||||||
|
.with_inner_size(LogicalSize::new(128.0, 128.0))
|
||||||
|
.with_resize_increments(LogicalSize::new(25.0, 25.0))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut has_increments = true;
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| match event {
|
||||||
|
Event::WindowEvent { event, window_id } if window_id == window.id() => match event {
|
||||||
|
WindowEvent::CloseRequested => elwt.exit(),
|
||||||
|
WindowEvent::KeyboardInput { event, .. }
|
||||||
|
if event.logical_key == NamedKey::Space
|
||||||
|
&& event.state == ElementState::Released =>
|
||||||
|
{
|
||||||
|
has_increments = !has_increments;
|
||||||
|
|
||||||
|
let new_increments = match window.resize_increments() {
|
||||||
|
Some(_) => None,
|
||||||
|
None => Some(LogicalSize::new(25.0, 25.0)),
|
||||||
|
};
|
||||||
|
debug!("Had increments: {}", new_increments.is_none());
|
||||||
|
window.set_resize_increments(new_increments);
|
||||||
|
}
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
Event::AboutToWait => window.request_redraw(),
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
})
|
||||||
|
}
|
||||||
107
examples/window_tabbing.rs
Normal file
107
examples/window_tabbing.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use std::{collections::HashMap, num::NonZeroUsize};
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
use winit::{
|
||||||
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
keyboard::{Key, NamedKey},
|
||||||
|
platform::macos::{WindowBuilderExtMacOS, WindowExtMacOS},
|
||||||
|
window::{Window, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let mut windows = HashMap::new();
|
||||||
|
let window = Window::new(&event_loop).unwrap();
|
||||||
|
println!("Opened a new window: {:?}", window.id());
|
||||||
|
windows.insert(window.id(), window);
|
||||||
|
|
||||||
|
println!("Press N to open a new window.");
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
if let Event::WindowEvent { event, window_id } = event {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested => {
|
||||||
|
println!("Window {window_id:?} has received the signal to close");
|
||||||
|
|
||||||
|
// This drops the window, causing it to close.
|
||||||
|
windows.remove(&window_id);
|
||||||
|
|
||||||
|
if windows.is_empty() {
|
||||||
|
elwt.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::Resized(_) => {
|
||||||
|
if let Some(window) = windows.get(&window_id) {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
logical_key,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
is_synthetic: false,
|
||||||
|
..
|
||||||
|
} => match logical_key.as_ref() {
|
||||||
|
Key::Character("t") => {
|
||||||
|
let tabbing_id = windows.get(&window_id).unwrap().tabbing_identifier();
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_tabbing_identifier(&tabbing_id)
|
||||||
|
.build(elwt)
|
||||||
|
.unwrap();
|
||||||
|
println!("Added a new tab: {:?}", window.id());
|
||||||
|
windows.insert(window.id(), window);
|
||||||
|
}
|
||||||
|
Key::Character("w") => {
|
||||||
|
let _ = windows.remove(&window_id);
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::ArrowRight) => {
|
||||||
|
windows.get(&window_id).unwrap().select_next_tab();
|
||||||
|
}
|
||||||
|
Key::Named(NamedKey::ArrowLeft) => {
|
||||||
|
windows.get(&window_id).unwrap().select_previous_tab();
|
||||||
|
}
|
||||||
|
Key::Character(ch) => {
|
||||||
|
if let Ok(index) = ch.parse::<NonZeroUsize>() {
|
||||||
|
let index = index.get();
|
||||||
|
// Select the last tab when pressing `9`.
|
||||||
|
let window = windows.get(&window_id).unwrap();
|
||||||
|
if index == 9 {
|
||||||
|
window.select_tab_at_index(window.num_tabs() - 1)
|
||||||
|
} else {
|
||||||
|
window.select_tab_at_index(index - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
WindowEvent::RedrawRequested => {
|
||||||
|
if let Some(window) = windows.get(&window_id) {
|
||||||
|
fill::fill_window(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
fn main() {
|
||||||
|
println!("This example is only supported on MacOS");
|
||||||
|
}
|
||||||
70
examples/x11_embed.rs
Normal file
70
examples/x11_embed.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
//! A demonstration of embedding a winit window in an existing X11 application.
|
||||||
|
|
||||||
|
#[cfg(x11_platform)]
|
||||||
|
#[path = "util/fill.rs"]
|
||||||
|
mod fill;
|
||||||
|
|
||||||
|
#[cfg(x11_platform)]
|
||||||
|
mod imple {
|
||||||
|
use super::fill;
|
||||||
|
use simple_logger::SimpleLogger;
|
||||||
|
use winit::{
|
||||||
|
event::{Event, WindowEvent},
|
||||||
|
event_loop::EventLoop,
|
||||||
|
platform::x11::WindowBuilderExtX11,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// First argument should be a 32-bit X11 window ID.
|
||||||
|
let parent_window_id = std::env::args()
|
||||||
|
.nth(1)
|
||||||
|
.ok_or("Expected a 32-bit X11 window ID as the first argument.")?
|
||||||
|
.parse::<u32>()?;
|
||||||
|
|
||||||
|
SimpleLogger::new().init().unwrap();
|
||||||
|
let event_loop = EventLoop::new()?;
|
||||||
|
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("An embedded window!")
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||||
|
.with_embed_parent_window(parent_window_id)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
event_loop.run(move |event, elwt| {
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::CloseRequested,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => elwt.exit(),
|
||||||
|
Event::AboutToWait => {
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
Event::WindowEvent {
|
||||||
|
event: WindowEvent::RedrawRequested,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
// Notify the windowing system that we'll be presenting to the window.
|
||||||
|
window.pre_present_notify();
|
||||||
|
fill::fill_window(&window);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(x11_platform))]
|
||||||
|
mod imple {
|
||||||
|
pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("This example is only supported on X11 platforms.");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
imple::entry()
|
||||||
|
}
|
||||||
9
run-wasm/Cargo.toml
Normal file
9
run-wasm/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "run-wasm"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
cargo-run-wasm = "0.2.0"
|
||||||
3
run-wasm/src/main.rs
Normal file
3
run-wasm/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
cargo_run_wasm::run_wasm_with_css("body { margin: 0px; }");
|
||||||
|
}
|
||||||
23
rustfmt.toml
23
rustfmt.toml
@@ -1,20 +1,3 @@
|
|||||||
comment_width = 100
|
force_explicit_abi=true
|
||||||
condense_wildcard_suffixes = true
|
use_field_init_shorthand=true
|
||||||
error_on_unformatted = true
|
# merge_imports=true
|
||||||
format_code_in_doc_comments = true
|
|
||||||
format_macro_bodies = true
|
|
||||||
format_macro_matchers = true
|
|
||||||
format_strings = true
|
|
||||||
group_imports = "StdExternalCrate"
|
|
||||||
hex_literal_case = "Lower"
|
|
||||||
imports_granularity = "Module"
|
|
||||||
match_block_trailing_comma = true
|
|
||||||
newline_style = "Unix"
|
|
||||||
normalize_comments = true
|
|
||||||
normalize_doc_attributes = true
|
|
||||||
overflow_delimited_expr = true
|
|
||||||
# Some macros break with this.
|
|
||||||
reorder_impl_items = false
|
|
||||||
use_field_init_shorthand = true
|
|
||||||
use_small_heuristics = "Max"
|
|
||||||
wrap_comments = true
|
|
||||||
|
|||||||
1001
src/dpi.rs
Normal file
1001
src/dpi.rs
Normal file
File diff suppressed because it is too large
Load Diff
142
src/error.rs
Normal file
142
src/error.rs
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
use std::{error, fmt};
|
||||||
|
|
||||||
|
use crate::platform_impl;
|
||||||
|
|
||||||
|
// TODO: Rename
|
||||||
|
/// An error that may be generated when requesting Winit state
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ExternalError {
|
||||||
|
/// The operation is not supported by the backend.
|
||||||
|
NotSupported(NotSupportedError),
|
||||||
|
/// The operation was ignored.
|
||||||
|
Ignored,
|
||||||
|
/// The OS cannot perform the operation.
|
||||||
|
Os(OsError),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The error type for when the requested operation is not supported by the backend.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct NotSupportedError {
|
||||||
|
_marker: (),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The error type for when the OS cannot perform the requested operation.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct OsError {
|
||||||
|
line: u32,
|
||||||
|
file: &'static str,
|
||||||
|
error: platform_impl::OsError,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A general error that may occur while running the Winit event loop
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum EventLoopError {
|
||||||
|
/// The operation is not supported by the backend.
|
||||||
|
NotSupported(NotSupportedError),
|
||||||
|
/// The OS cannot perform the operation.
|
||||||
|
Os(OsError),
|
||||||
|
/// The event loop can't be re-run while it's already running
|
||||||
|
AlreadyRunning,
|
||||||
|
/// The event loop can't be re-created.
|
||||||
|
RecreationAttempt,
|
||||||
|
/// Application has exit with an error status.
|
||||||
|
ExitFailure(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<OsError> for EventLoopError {
|
||||||
|
fn from(value: OsError) -> Self {
|
||||||
|
Self::Os(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NotSupportedError {
|
||||||
|
#[inline]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) fn new() -> NotSupportedError {
|
||||||
|
NotSupportedError { _marker: () }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OsError {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) fn new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError {
|
||||||
|
OsError { line, file, error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_macros)]
|
||||||
|
macro_rules! os_error {
|
||||||
|
($error:expr) => {{
|
||||||
|
crate::error::OsError::new(line!(), file!(), $error)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for OsError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
f.pad(&format!(
|
||||||
|
"os error at {}:{}: {}",
|
||||||
|
self.file, self.line, self.error
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ExternalError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
match self {
|
||||||
|
ExternalError::NotSupported(e) => e.fmt(f),
|
||||||
|
ExternalError::Ignored => write!(f, "Operation was ignored"),
|
||||||
|
ExternalError::Os(e) => e.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for NotSupportedError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
f.debug_struct("NotSupportedError").finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for NotSupportedError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
f.pad("the requested operation is not supported by Winit")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for EventLoopError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
match self {
|
||||||
|
EventLoopError::AlreadyRunning => write!(f, "EventLoop is already running"),
|
||||||
|
EventLoopError::RecreationAttempt => write!(f, "EventLoop can't be recreated"),
|
||||||
|
EventLoopError::NotSupported(e) => e.fmt(f),
|
||||||
|
EventLoopError::Os(e) => e.fmt(f),
|
||||||
|
EventLoopError::ExitFailure(status) => write!(f, "Exit Failure: {status}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl error::Error for OsError {}
|
||||||
|
impl error::Error for ExternalError {}
|
||||||
|
impl error::Error for NotSupportedError {}
|
||||||
|
impl error::Error for EventLoopError {}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#![allow(clippy::redundant_clone)]
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
// Eat attributes for testing
|
||||||
|
#[test]
|
||||||
|
fn ensure_fmt_does_not_panic() {
|
||||||
|
let _ = format!(
|
||||||
|
"{:?}, {}",
|
||||||
|
NotSupportedError::new(),
|
||||||
|
NotSupportedError::new().clone()
|
||||||
|
);
|
||||||
|
let _ = format!(
|
||||||
|
"{:?}, {}",
|
||||||
|
ExternalError::NotSupported(NotSupportedError::new()),
|
||||||
|
ExternalError::NotSupported(NotSupportedError::new())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
1342
src/event.rs
Normal file
1342
src/event.rs
Normal file
File diff suppressed because it is too large
Load Diff
475
src/event_loop.rs
Normal file
475
src/event_loop.rs
Normal file
@@ -0,0 +1,475 @@
|
|||||||
|
//! 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
|
||||||
|
//! [`send_event`](`EventLoopProxy::send_event`) method.
|
||||||
|
//!
|
||||||
|
//! See the root-level documentation for information on how to create and use an event loop to
|
||||||
|
//! handle events.
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::ops::Deref;
|
||||||
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
|
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||||
|
use std::{error, fmt};
|
||||||
|
|
||||||
|
#[cfg(not(wasm_platform))]
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
#[cfg(wasm_platform)]
|
||||||
|
use web_time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::error::EventLoopError;
|
||||||
|
use crate::{event::Event, monitor::MonitorHandle, platform_impl};
|
||||||
|
|
||||||
|
/// Provides a way to retrieve events from the system and from the windows that were registered to
|
||||||
|
/// 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
|
||||||
|
pub struct EventLoop<T: 'static> {
|
||||||
|
pub(crate) event_loop: platform_impl::EventLoop<T>,
|
||||||
|
pub(crate) _marker: PhantomData<*mut ()>, // Not Send nor Sync
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Target that associates windows with an [`EventLoop`].
|
||||||
|
///
|
||||||
|
/// This type exists to allow you to create new windows while Winit executes
|
||||||
|
/// your callback. [`EventLoop`] will coerce into this type (`impl<T> Deref for
|
||||||
|
/// EventLoop<T>`), so functions that take this as a parameter can also take
|
||||||
|
/// `&EventLoop`.
|
||||||
|
pub struct EventLoopWindowTarget<T: 'static> {
|
||||||
|
pub(crate) p: platform_impl::EventLoopWindowTarget<T>,
|
||||||
|
pub(crate) _marker: PhantomData<*mut ()>, // Not Send nor Sync
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct EventLoopBuilder<T: 'static> {
|
||||||
|
pub(crate) platform_specific: platform_impl::PlatformSpecificEventLoopAttributes,
|
||||||
|
_p: PhantomData<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventLoopBuilder<()> {
|
||||||
|
/// Start building a new event loop.
|
||||||
|
#[inline]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::with_user_event()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
|
impl<T> EventLoopBuilder<T> {
|
||||||
|
/// Start building a new event loop, with the given type as the user event
|
||||||
|
/// type.
|
||||||
|
#[inline]
|
||||||
|
pub fn with_user_event() -> Self {
|
||||||
|
Self {
|
||||||
|
platform_specific: Default::default(),
|
||||||
|
_p: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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::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,
|
||||||
|
doc = "[`.with_android_app(app)`]: crate::platform::android::EventLoopBuilderExtAndroid::with_android_app"
|
||||||
|
)]
|
||||||
|
#[cfg_attr(
|
||||||
|
not(android),
|
||||||
|
doc = "[`.with_android_app(app)`]: #only-available-on-android"
|
||||||
|
)]
|
||||||
|
#[inline]
|
||||||
|
pub fn build(&mut self) -> Result<EventLoop<T>, EventLoopError> {
|
||||||
|
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(wasm_platform)]
|
||||||
|
pub(crate) fn allow_event_loop_recreation() {
|
||||||
|
EVENT_LOOP_CREATED.store(false, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Debug for EventLoop<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.pad("EventLoop { .. }")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Debug for EventLoopWindowTarget<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.pad("EventLoopWindowTarget { .. }")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set through [`EventLoopWindowTarget::set_control_flow()`].
|
||||||
|
///
|
||||||
|
/// Indicates the desired behavior of the event loop after [`Event::AboutToWait`] is emitted.
|
||||||
|
///
|
||||||
|
/// Defaults to [`Wait`].
|
||||||
|
///
|
||||||
|
/// [`Wait`]: Self::Wait
|
||||||
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
||||||
|
pub enum ControlFlow {
|
||||||
|
/// When the current loop iteration finishes, immediately begin a new iteration regardless of
|
||||||
|
/// whether or not new events are available to process.
|
||||||
|
Poll,
|
||||||
|
|
||||||
|
/// When the current loop iteration finishes, suspend the thread until another event arrives.
|
||||||
|
#[default]
|
||||||
|
Wait,
|
||||||
|
|
||||||
|
/// When the current loop iteration finishes, suspend the thread until either another event
|
||||||
|
/// arrives or the given time is reached.
|
||||||
|
///
|
||||||
|
/// Useful for implementing efficient timers. Applications which want to render at the display's
|
||||||
|
/// native refresh rate should instead use [`Poll`] and the VSync functionality of a graphics API
|
||||||
|
/// to reduce odds of missed frames.
|
||||||
|
///
|
||||||
|
/// [`Poll`]: Self::Poll
|
||||||
|
WaitUntil(Instant),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControlFlow {
|
||||||
|
/// Creates a [`ControlFlow`] that waits until a timeout has expired.
|
||||||
|
///
|
||||||
|
/// In most cases, this is set to [`WaitUntil`]. However, if the timeout overflows, it is
|
||||||
|
/// instead set to [`Wait`].
|
||||||
|
///
|
||||||
|
/// [`WaitUntil`]: Self::WaitUntil
|
||||||
|
/// [`Wait`]: Self::Wait
|
||||||
|
pub fn wait_duration(timeout: Duration) -> Self {
|
||||||
|
match Instant::now().checked_add(timeout) {
|
||||||
|
Some(instant) => Self::WaitUntil(instant),
|
||||||
|
None => Self::Wait,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventLoop<()> {
|
||||||
|
/// Alias for [`EventLoopBuilder::new().build()`].
|
||||||
|
///
|
||||||
|
/// [`EventLoopBuilder::new().build()`]: EventLoopBuilder::build
|
||||||
|
#[inline]
|
||||||
|
pub fn new() -> Result<EventLoop<()>, EventLoopError> {
|
||||||
|
EventLoopBuilder::new().build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoop<T> {
|
||||||
|
#[deprecated = "Use `EventLoopBuilder::<T>::with_user_event().build()` instead."]
|
||||||
|
pub fn with_user_event() -> Result<EventLoop<T>, EventLoopError> {
|
||||||
|
EventLoopBuilder::<T>::with_user_event().build()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the event loop in the calling thread and calls the given `event_handler` closure
|
||||||
|
/// to dispatch any pending events.
|
||||||
|
///
|
||||||
|
/// Since the closure is `'static`, it must be a `move` closure if it needs to
|
||||||
|
/// access any data from the calling context.
|
||||||
|
///
|
||||||
|
/// See the [`set_control_flow()`] docs on how to change the event loop's behavior.
|
||||||
|
///
|
||||||
|
/// ## 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(
|
||||||
|
wasm_platform,
|
||||||
|
doc = "[`EventLoopExtWebSys::spawn()`][crate::platform::web::EventLoopExtWebSys::spawn()]"
|
||||||
|
)]
|
||||||
|
#[cfg_attr(not(wasm_platform), doc = "`EventLoopExtWebSys::spawn()`")]
|
||||||
|
/// [^1] instead of [`run()`] 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"`.
|
||||||
|
///
|
||||||
|
/// [`set_control_flow()`]: EventLoopWindowTarget::set_control_flow()
|
||||||
|
/// [`run()`]: Self::run()
|
||||||
|
/// [^1]: `EventLoopExtWebSys::spawn()` is only available on WASM.
|
||||||
|
#[inline]
|
||||||
|
#[cfg(not(all(wasm_platform, target_feature = "exception-handling")))]
|
||||||
|
pub fn run<F>(self, event_handler: F) -> Result<(), EventLoopError>
|
||||||
|
where
|
||||||
|
F: FnMut(Event<T>, &EventLoopWindowTarget<T>),
|
||||||
|
{
|
||||||
|
self.event_loop.run(event_handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an [`EventLoopProxy`] that can be used to dispatch user events to the main event loop.
|
||||||
|
pub fn create_proxy(&self) -> EventLoopProxy<T> {
|
||||||
|
EventLoopProxy {
|
||||||
|
event_loop_proxy: self.event_loop.create_proxy(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "rwh_06")]
|
||||||
|
impl<T> rwh_06::HasDisplayHandle for EventLoop<T> {
|
||||||
|
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
|
||||||
|
rwh_06::HasDisplayHandle::display_handle(&**self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "rwh_05")]
|
||||||
|
unsafe impl<T> rwh_05::HasRawDisplayHandle for EventLoop<T> {
|
||||||
|
/// Returns a [`rwh_05::RawDisplayHandle`] for the event loop.
|
||||||
|
fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle {
|
||||||
|
rwh_05::HasRawDisplayHandle::raw_display_handle(&**self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
|
impl<T> AsFd for EventLoop<T> {
|
||||||
|
/// 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_events`] API.
|
||||||
|
///
|
||||||
|
/// [`calloop`]: https://crates.io/crates/calloop
|
||||||
|
/// [`mio`]: https://crates.io/crates/mio
|
||||||
|
/// [`pump_events`]: crate::platform::pump_events::EventLoopExtPumpEvents::pump_events
|
||||||
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||||
|
self.event_loop.as_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
|
impl<T> AsRawFd for EventLoop<T> {
|
||||||
|
/// 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_events`] API.
|
||||||
|
///
|
||||||
|
/// [`calloop`]: https://crates.io/crates/calloop
|
||||||
|
/// [`mio`]: https://crates.io/crates/mio
|
||||||
|
/// [`pump_events`]: crate::platform::pump_events::EventLoopExtPumpEvents::pump_events
|
||||||
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
|
self.event_loop.as_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Deref for EventLoop<T> {
|
||||||
|
type Target = EventLoopWindowTarget<T>;
|
||||||
|
fn deref(&self) -> &EventLoopWindowTarget<T> {
|
||||||
|
self.event_loop.window_target()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopWindowTarget<T> {
|
||||||
|
/// Returns the list of all the monitors available on the system.
|
||||||
|
#[inline]
|
||||||
|
pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
|
||||||
|
#[allow(clippy::useless_conversion)] // false positive on some platforms
|
||||||
|
self.p
|
||||||
|
.available_monitors()
|
||||||
|
.into_iter()
|
||||||
|
.map(|inner| MonitorHandle { inner })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the primary monitor of the system.
|
||||||
|
///
|
||||||
|
/// Returns `None` if it can't identify any monitor as a primary one.
|
||||||
|
///
|
||||||
|
/// ## Platform-specific
|
||||||
|
///
|
||||||
|
/// **Wayland / Web:** Always returns `None`.
|
||||||
|
#[inline]
|
||||||
|
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
|
||||||
|
self.p
|
||||||
|
.primary_monitor()
|
||||||
|
.map(|inner| MonitorHandle { inner })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
pub fn listen_device_events(&self, allowed: DeviceEvents) {
|
||||||
|
self.p.listen_device_events(allowed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the [`ControlFlow`].
|
||||||
|
pub fn set_control_flow(&self, control_flow: ControlFlow) {
|
||||||
|
self.p.set_control_flow(control_flow)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the current [`ControlFlow`].
|
||||||
|
pub fn control_flow(&self) -> ControlFlow {
|
||||||
|
self.p.control_flow()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This exits the event loop.
|
||||||
|
///
|
||||||
|
/// See [`LoopExiting`](Event::LoopExiting).
|
||||||
|
pub fn exit(&self) {
|
||||||
|
self.p.exit()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns if the [`EventLoop`] is about to stop.
|
||||||
|
///
|
||||||
|
/// See [`exit()`](Self::exit).
|
||||||
|
pub fn exiting(&self) -> bool {
|
||||||
|
self.p.exiting()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "rwh_06")]
|
||||||
|
impl<T> rwh_06::HasDisplayHandle for EventLoopWindowTarget<T> {
|
||||||
|
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
|
||||||
|
let raw = self.p.raw_display_handle_rwh_06()?;
|
||||||
|
// SAFETY: The display will never be deallocated while the event loop is alive.
|
||||||
|
Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "rwh_05")]
|
||||||
|
unsafe impl<T> rwh_05::HasRawDisplayHandle for EventLoopWindowTarget<T> {
|
||||||
|
/// Returns a [`rwh_05::RawDisplayHandle`] for the event loop.
|
||||||
|
fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle {
|
||||||
|
self.p.raw_display_handle_rwh_05()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used to send custom events to [`EventLoop`].
|
||||||
|
pub struct EventLoopProxy<T: 'static> {
|
||||||
|
event_loop_proxy: platform_impl::EventLoopProxy<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> Clone for EventLoopProxy<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
event_loop_proxy: self.event_loop_proxy.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> EventLoopProxy<T> {
|
||||||
|
/// Send an event to the [`EventLoop`] from which this proxy was created. This emits a
|
||||||
|
/// `UserEvent(event)` event in the event loop, where `event` is the value passed to this
|
||||||
|
/// function.
|
||||||
|
///
|
||||||
|
/// Returns an `Err` if the associated [`EventLoop`] no longer exists.
|
||||||
|
///
|
||||||
|
/// [`UserEvent(event)`]: Event::UserEvent
|
||||||
|
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
|
||||||
|
self.event_loop_proxy.send_event(event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> fmt::Debug for EventLoopProxy<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.pad("EventLoopProxy { .. }")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] that
|
||||||
|
/// no longer exists.
|
||||||
|
///
|
||||||
|
/// Contains the original event given to [`EventLoopProxy::send_event`].
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub struct EventLoopClosed<T>(pub T);
|
||||||
|
|
||||||
|
impl<T> fmt::Display for EventLoopClosed<T> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str("Tried to wake up a closed `EventLoop`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Debug> error::Error for EventLoopClosed<T> {}
|
||||||
|
|
||||||
|
/// Control when device events are captured.
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
|
||||||
|
pub enum DeviceEvents {
|
||||||
|
/// Report device events regardless of window focus.
|
||||||
|
Always,
|
||||||
|
/// Only capture device events while the window is focused.
|
||||||
|
#[default]
|
||||||
|
WhenFocused,
|
||||||
|
/// Never capture device events.
|
||||||
|
Never,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A unique identifier of the winit's async request.
|
||||||
|
///
|
||||||
|
/// This could be used to identify the async request once it's done
|
||||||
|
/// and a specific action must be taken.
|
||||||
|
///
|
||||||
|
/// One of the handling scenarious could be to maintain a working list
|
||||||
|
/// containing [`AsyncRequestSerial`] and some closure associated with it.
|
||||||
|
/// Then once event is arriving the working list is being traversed and a job
|
||||||
|
/// executed and removed from the list.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct AsyncRequestSerial {
|
||||||
|
serial: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsyncRequestSerial {
|
||||||
|
// TODO(kchibisov) remove `cfg` when the clipboard will be added.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub(crate) fn get() -> Self {
|
||||||
|
static CURRENT_SERIAL: AtomicU64 = AtomicU64::new(0);
|
||||||
|
// NOTE: we rely on wrap around here, while the user may just request
|
||||||
|
// in the loop u64::MAX times that's issue is considered on them.
|
||||||
|
let serial = CURRENT_SERIAL.fetch_add(1, Ordering::Relaxed);
|
||||||
|
Self { serial }
|
||||||
|
}
|
||||||
|
}
|
||||||
125
src/icon.rs
Normal file
125
src/icon.rs
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
use crate::platform_impl::PlatformIcon;
|
||||||
|
use std::{error::Error, fmt, io, mem};
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct Pixel {
|
||||||
|
pub(crate) r: u8,
|
||||||
|
pub(crate) g: u8,
|
||||||
|
pub(crate) b: u8,
|
||||||
|
pub(crate) a: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) const PIXEL_SIZE: usize = mem::size_of::<Pixel>();
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
/// An error produced when using [`Icon::from_rgba`] with invalid arguments.
|
||||||
|
pub enum BadIcon {
|
||||||
|
/// Produced when the length of the `rgba` argument isn't divisible by 4, thus `rgba` can't be
|
||||||
|
/// safely interpreted as 32bpp RGBA pixels.
|
||||||
|
ByteCountNotDivisibleBy4 { byte_count: usize },
|
||||||
|
/// Produced when the number of pixels (`rgba.len() / 4`) isn't equal to `width * height`.
|
||||||
|
/// At least one of your arguments is incorrect.
|
||||||
|
DimensionsVsPixelCount {
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
width_x_height: usize,
|
||||||
|
pixel_count: usize,
|
||||||
|
},
|
||||||
|
/// Produced when underlying OS functionality failed to create the icon
|
||||||
|
OsError(io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for BadIcon {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
BadIcon::ByteCountNotDivisibleBy4 { byte_count } => write!(f,
|
||||||
|
"The length of the `rgba` argument ({byte_count:?}) isn't divisible by 4, making it impossible to interpret as 32bpp RGBA pixels.",
|
||||||
|
),
|
||||||
|
BadIcon::DimensionsVsPixelCount {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
width_x_height,
|
||||||
|
pixel_count,
|
||||||
|
} => write!(f,
|
||||||
|
"The specified dimensions ({width:?}x{height:?}) don't match the number of pixels supplied by the `rgba` argument ({pixel_count:?}). For those dimensions, the expected pixel count is {width_x_height:?}.",
|
||||||
|
),
|
||||||
|
BadIcon::OsError(e) => write!(f, "OS error when instantiating the icon: {e:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for BadIcon {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub(crate) struct RgbaIcon {
|
||||||
|
pub(crate) rgba: Vec<u8>,
|
||||||
|
pub(crate) width: u32,
|
||||||
|
pub(crate) height: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// For platforms which don't have window icons (e.g. web)
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub(crate) struct NoIcon;
|
||||||
|
|
||||||
|
#[allow(dead_code)] // These are not used on every platform
|
||||||
|
mod constructors {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl RgbaIcon {
|
||||||
|
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
|
||||||
|
if rgba.len() % PIXEL_SIZE != 0 {
|
||||||
|
return Err(BadIcon::ByteCountNotDivisibleBy4 {
|
||||||
|
byte_count: rgba.len(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let pixel_count = rgba.len() / PIXEL_SIZE;
|
||||||
|
if pixel_count != (width * height) as usize {
|
||||||
|
Err(BadIcon::DimensionsVsPixelCount {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
width_x_height: (width * height) as usize,
|
||||||
|
pixel_count,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(RgbaIcon {
|
||||||
|
rgba,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NoIcon {
|
||||||
|
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
|
||||||
|
// Create the rgba icon anyway to validate the input
|
||||||
|
let _ = RgbaIcon::from_rgba(rgba, width, height)?;
|
||||||
|
Ok(NoIcon)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An icon used for the window titlebar, taskbar, etc.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Icon {
|
||||||
|
pub(crate) inner: PlatformIcon,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Icon {
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
|
fmt::Debug::fmt(&self.inner, formatter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Icon {
|
||||||
|
/// Creates an icon from 32bpp RGBA data.
|
||||||
|
///
|
||||||
|
/// The length of `rgba` must be divisible by 4, and `width * height` must equal
|
||||||
|
/// `rgba.len() / 4`. Otherwise, this will return a `BadIcon` error.
|
||||||
|
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
|
||||||
|
Ok(Icon {
|
||||||
|
inner: PlatformIcon::from_rgba(rgba, width, height)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
1798
src/keyboard.rs
Normal file
1798
src/keyboard.rs
Normal file
File diff suppressed because it is too large
Load Diff
198
src/lib.rs
Normal file
198
src/lib.rs
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
//! Winit is a cross-platform window creation and event loop management library.
|
||||||
|
//!
|
||||||
|
//! # Building windows
|
||||||
|
//!
|
||||||
|
//! Before you can build a [`Window`], you first need to build an [`EventLoop`]. This is done with the
|
||||||
|
//! [`EventLoop::new()`] function.
|
||||||
|
//!
|
||||||
|
//! ```no_run
|
||||||
|
//! use winit::event_loop::EventLoop;
|
||||||
|
//! let event_loop = EventLoop::new().unwrap();
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! Once this is done, there are two ways to create a [`Window`]:
|
||||||
|
//!
|
||||||
|
//! - Calling [`Window::new(&event_loop)`][window_new].
|
||||||
|
//! - Calling [`let builder = WindowBuilder::new()`][window_builder_new] then [`builder.build(&event_loop)`][window_builder_build].
|
||||||
|
//!
|
||||||
|
//! The first method is the simplest and will give you default values for everything. The second
|
||||||
|
//! method allows you to customize the way your [`Window`] will look and behave by modifying the
|
||||||
|
//! fields of the [`WindowBuilder`] object before you create the [`Window`].
|
||||||
|
//!
|
||||||
|
//! # Event handling
|
||||||
|
//!
|
||||||
|
//! Once a [`Window`] has been created, it will generate different *events*. A [`Window`] object can
|
||||||
|
//! generate [`WindowEvent`]s when certain input events occur, such as a cursor moving over the
|
||||||
|
//! window or a key getting pressed while the window is focused. Devices can generate
|
||||||
|
//! [`DeviceEvent`]s, which contain unfiltered event data that isn't specific to a certain window.
|
||||||
|
//! Some user activity, like mouse movement, can generate both a [`WindowEvent`] *and* a
|
||||||
|
//! [`DeviceEvent`]. You can also create and handle your own custom [`Event::UserEvent`]s, if desired.
|
||||||
|
//!
|
||||||
|
//! You can retrieve events by calling [`EventLoop::run()`]. This function will
|
||||||
|
//! dispatch events for every [`Window`] that was created with that particular [`EventLoop`], and
|
||||||
|
//! will run until [`exit()`] is used, at which point [`Event::LoopExiting`].
|
||||||
|
//!
|
||||||
|
//! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop
|
||||||
|
//! 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
|
||||||
|
#![cfg_attr(
|
||||||
|
any(
|
||||||
|
windows_platform,
|
||||||
|
macos_platform,
|
||||||
|
android_platform,
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform
|
||||||
|
),
|
||||||
|
doc = "[`EventLoopExtPumpEvents::pump_events()`][platform::pump_events::EventLoopExtPumpEvents::pump_events()]"
|
||||||
|
)]
|
||||||
|
#![cfg_attr(
|
||||||
|
not(any(
|
||||||
|
windows_platform,
|
||||||
|
macos_platform,
|
||||||
|
android_platform,
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform
|
||||||
|
)),
|
||||||
|
doc = "`EventLoopExtPumpEvents::pump_events()`"
|
||||||
|
)]
|
||||||
|
//! [^1]. See that method's documentation for more reasons about why
|
||||||
|
//! it's discouraged beyond compatibility reasons.
|
||||||
|
//!
|
||||||
|
//!
|
||||||
|
//! ```no_run
|
||||||
|
//! use winit::{
|
||||||
|
//! event::{Event, WindowEvent},
|
||||||
|
//! event_loop::{ControlFlow, EventLoop},
|
||||||
|
//! window::WindowBuilder,
|
||||||
|
//! };
|
||||||
|
//!
|
||||||
|
//! let event_loop = EventLoop::new().unwrap();
|
||||||
|
//! let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
//!
|
||||||
|
//! // ControlFlow::Poll continuously runs the event loop, even if the OS hasn't
|
||||||
|
//! // dispatched any events. This is ideal for games and similar applications.
|
||||||
|
//! event_loop.set_control_flow(ControlFlow::Poll);
|
||||||
|
//!
|
||||||
|
//! // ControlFlow::Wait pauses the event loop if no events are available to process.
|
||||||
|
//! // This is ideal for non-game applications that only update in response to user
|
||||||
|
//! // input, and uses significantly less power/CPU time than ControlFlow::Poll.
|
||||||
|
//! event_loop.set_control_flow(ControlFlow::Wait);
|
||||||
|
//!
|
||||||
|
//! event_loop.run(move |event, elwt| {
|
||||||
|
//! match event {
|
||||||
|
//! Event::WindowEvent {
|
||||||
|
//! event: WindowEvent::CloseRequested,
|
||||||
|
//! ..
|
||||||
|
//! } => {
|
||||||
|
//! println!("The close button was pressed; stopping");
|
||||||
|
//! elwt.exit();
|
||||||
|
//! },
|
||||||
|
//! Event::AboutToWait => {
|
||||||
|
//! // Application update code.
|
||||||
|
//!
|
||||||
|
//! // Queue a RedrawRequested event.
|
||||||
|
//! //
|
||||||
|
//! // You only need to call this if you've determined that you need to redraw in
|
||||||
|
//! // applications which do not always need to. Applications that redraw continuously
|
||||||
|
//! // can render here instead.
|
||||||
|
//! window.request_redraw();
|
||||||
|
//! },
|
||||||
|
//! Event::WindowEvent {
|
||||||
|
//! event: WindowEvent::RedrawRequested,
|
||||||
|
//! ..
|
||||||
|
//! } => {
|
||||||
|
//! // Redraw the application.
|
||||||
|
//! //
|
||||||
|
//! // It's preferable for applications that do not render continuously to render in
|
||||||
|
//! // this event rather than in AboutToWait, since rendering in here allows
|
||||||
|
//! // the program to gracefully handle redraws requested by the OS.
|
||||||
|
//! },
|
||||||
|
//! _ => ()
|
||||||
|
//! }
|
||||||
|
//! });
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! [`WindowEvent`] has a [`WindowId`] member. In multi-window environments, it should be
|
||||||
|
//! compared to the value returned by [`Window::id()`] to determine which [`Window`]
|
||||||
|
//! dispatched the event.
|
||||||
|
//!
|
||||||
|
//! # Drawing on the window
|
||||||
|
//!
|
||||||
|
//! Winit doesn't directly provide any methods for drawing on a [`Window`]. However, it allows you to
|
||||||
|
//! retrieve the raw handle of the window and display (see the [`platform`] module and/or the
|
||||||
|
//! [`raw_window_handle`] and [`raw_display_handle`] methods), which in turn allows
|
||||||
|
//! you to create an OpenGL/Vulkan/DirectX/Metal/etc. context that can be used to render graphics.
|
||||||
|
//!
|
||||||
|
//! Note that many platforms will display garbage data in the window's client area if the
|
||||||
|
//! application doesn't render anything to the window by the time the desktop compositor is ready to
|
||||||
|
//! display the window to the user. If you notice this happening, you should create the window with
|
||||||
|
//! [`visible` set to `false`](crate::window::WindowBuilder::with_visible) and explicitly make the
|
||||||
|
//! window visible only once you're ready to render into it.
|
||||||
|
//!
|
||||||
|
//! [`EventLoop`]: event_loop::EventLoop
|
||||||
|
//! [`EventLoop::new()`]: event_loop::EventLoop::new
|
||||||
|
//! [`EventLoop::run()`]: event_loop::EventLoop::run
|
||||||
|
//! [`exit()`]: event_loop::EventLoopWindowTarget::exit
|
||||||
|
//! [`Window`]: window::Window
|
||||||
|
//! [`WindowId`]: window::WindowId
|
||||||
|
//! [`WindowBuilder`]: window::WindowBuilder
|
||||||
|
//! [window_new]: window::Window::new
|
||||||
|
//! [window_builder_new]: window::WindowBuilder::new
|
||||||
|
//! [window_builder_build]: window::WindowBuilder::build
|
||||||
|
//! [`Window::id()`]: window::Window::id
|
||||||
|
//! [`WindowEvent`]: event::WindowEvent
|
||||||
|
//! [`DeviceEvent`]: event::DeviceEvent
|
||||||
|
//! [`Event::UserEvent`]: event::Event::UserEvent
|
||||||
|
//! [`Event::LoopExiting`]: event::Event::LoopExiting
|
||||||
|
//! [`raw_window_handle`]: ./window/struct.Window.html#method.raw_window_handle
|
||||||
|
//! [`raw_display_handle`]: ./window/struct.Window.html#method.raw_display_handle
|
||||||
|
//! [^1]: `EventLoopExtPumpEvents::pump_events()` is only available on Windows, macOS, Android, X11 and Wayland.
|
||||||
|
|
||||||
|
#![deny(rust_2018_idioms)]
|
||||||
|
#![deny(rustdoc::broken_intra_doc_links)]
|
||||||
|
#![deny(clippy::all)]
|
||||||
|
#![deny(unsafe_op_in_unsafe_fn)]
|
||||||
|
#![cfg_attr(feature = "cargo-clippy", deny(warnings))]
|
||||||
|
// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc
|
||||||
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||||
|
#![allow(clippy::missing_safety_doc)]
|
||||||
|
|
||||||
|
#[cfg(feature = "rwh_06")]
|
||||||
|
pub use rwh_06 as raw_window_handle;
|
||||||
|
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate bitflags;
|
||||||
|
|
||||||
|
pub mod dpi;
|
||||||
|
#[macro_use]
|
||||||
|
pub mod error;
|
||||||
|
pub mod event;
|
||||||
|
pub mod event_loop;
|
||||||
|
mod icon;
|
||||||
|
pub mod keyboard;
|
||||||
|
pub mod monitor;
|
||||||
|
mod platform_impl;
|
||||||
|
pub mod window;
|
||||||
|
|
||||||
|
pub mod platform;
|
||||||
|
|
||||||
|
/// Wrapper for objects which winit will access on the main thread so they are effectively `Send`
|
||||||
|
/// and `Sync`, since they always execute on a single thread.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// Winit can run only one event loop at a time, and the event loop itself is tied to some thread.
|
||||||
|
/// The objects could be sent across the threads, but once passed to winit, they execute on the
|
||||||
|
/// main thread if the platform demands it. Thus, marking such objects as `Send + Sync` is safe.
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct SendSyncWrapper<T>(pub(crate) T);
|
||||||
|
|
||||||
|
unsafe impl<T> Send for SendSyncWrapper<T> {}
|
||||||
|
unsafe impl<T> Sync for SendSyncWrapper<T> {}
|
||||||
169
src/monitor.rs
Normal file
169
src/monitor.rs
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
//! Types useful for interacting with a user's monitors.
|
||||||
|
//!
|
||||||
|
//! If you want to get basic information about a monitor, you can use the
|
||||||
|
//! [`MonitorHandle`] type. This is retrieved from one of the following
|
||||||
|
//! methods, which return an iterator of [`MonitorHandle`]:
|
||||||
|
//! - [`EventLoopWindowTarget::available_monitors`](crate::event_loop::EventLoopWindowTarget::available_monitors).
|
||||||
|
//! - [`Window::available_monitors`](crate::window::Window::available_monitors).
|
||||||
|
use crate::{
|
||||||
|
dpi::{PhysicalPosition, PhysicalSize},
|
||||||
|
platform_impl,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Describes a fullscreen video mode of a monitor.
|
||||||
|
///
|
||||||
|
/// Can be acquired with [`MonitorHandle::video_modes`].
|
||||||
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct VideoMode {
|
||||||
|
pub(crate) video_mode: platform_impl::VideoMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for VideoMode {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.video_mode.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for VideoMode {
|
||||||
|
fn partial_cmp(&self, other: &VideoMode) -> Option<std::cmp::Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for VideoMode {
|
||||||
|
fn cmp(&self, other: &VideoMode) -> std::cmp::Ordering {
|
||||||
|
self.monitor().cmp(&other.monitor()).then(
|
||||||
|
self.size()
|
||||||
|
.cmp(&other.size())
|
||||||
|
.then(
|
||||||
|
self.refresh_rate_millihertz()
|
||||||
|
.cmp(&other.refresh_rate_millihertz())
|
||||||
|
.then(self.bit_depth().cmp(&other.bit_depth())),
|
||||||
|
)
|
||||||
|
.reverse(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VideoMode {
|
||||||
|
/// Returns the resolution of this video mode.
|
||||||
|
#[inline]
|
||||||
|
pub fn size(&self) -> PhysicalSize<u32> {
|
||||||
|
self.video_mode.size()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
///
|
||||||
|
/// - **Wayland / Orbital:** Always returns 32.
|
||||||
|
/// - **iOS:** Always returns 32.
|
||||||
|
#[inline]
|
||||||
|
pub fn bit_depth(&self) -> u16 {
|
||||||
|
self.video_mode.bit_depth()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the refresh rate of this video mode in mHz.
|
||||||
|
#[inline]
|
||||||
|
pub fn refresh_rate_millihertz(&self) -> u32 {
|
||||||
|
self.video_mode.refresh_rate_millihertz()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the monitor that this video mode is valid for. Each monitor has
|
||||||
|
/// a separate set of valid video modes.
|
||||||
|
#[inline]
|
||||||
|
pub fn monitor(&self) -> MonitorHandle {
|
||||||
|
MonitorHandle {
|
||||||
|
inner: self.video_mode.monitor(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for VideoMode {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}x{} @ {} mHz ({} bpp)",
|
||||||
|
self.size().width,
|
||||||
|
self.size().height,
|
||||||
|
self.refresh_rate_millihertz(),
|
||||||
|
self.bit_depth()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handle to a monitor.
|
||||||
|
///
|
||||||
|
/// Allows you to retrieve information about a given monitor and can be used in [`Window`] creation.
|
||||||
|
///
|
||||||
|
/// [`Window`]: crate::window::Window
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct MonitorHandle {
|
||||||
|
pub(crate) inner: platform_impl::MonitorHandle,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MonitorHandle {
|
||||||
|
/// Returns a human-readable name of the monitor.
|
||||||
|
///
|
||||||
|
/// Returns `None` if the monitor doesn't exist anymore.
|
||||||
|
#[inline]
|
||||||
|
pub fn name(&self) -> Option<String> {
|
||||||
|
self.inner.name()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the monitor's resolution.
|
||||||
|
#[inline]
|
||||||
|
pub fn size(&self) -> PhysicalSize<u32> {
|
||||||
|
self.inner.size()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the top-left corner position of the monitor relative to the larger full
|
||||||
|
/// screen area.
|
||||||
|
#[inline]
|
||||||
|
pub fn position(&self) -> PhysicalPosition<i32> {
|
||||||
|
self.inner.position()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The monitor refresh rate used by the system.
|
||||||
|
///
|
||||||
|
/// Return `Some` if succeed, or `None` if failed, which usually happens when the monitor
|
||||||
|
/// the window is on is removed.
|
||||||
|
///
|
||||||
|
/// When using exclusive fullscreen, the refresh rate of the [`VideoMode`] that was used to
|
||||||
|
/// enter fullscreen should be used instead.
|
||||||
|
#[inline]
|
||||||
|
pub fn refresh_rate_millihertz(&self) -> Option<u32> {
|
||||||
|
self.inner.refresh_rate_millihertz()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the scale factor of the underlying monitor. To map logical pixels to physical
|
||||||
|
/// pixels and vice versa, use [`Window::scale_factor`].
|
||||||
|
///
|
||||||
|
/// See the [`dpi`](crate::dpi) module for more information.
|
||||||
|
///
|
||||||
|
/// ## 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.
|
||||||
|
///
|
||||||
|
/// [`Window::scale_factor`]: crate::window::Window::scale_factor
|
||||||
|
#[inline]
|
||||||
|
pub fn scale_factor(&self) -> f64 {
|
||||||
|
self.inner.scale_factor()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns all fullscreen video modes supported by this monitor.
|
||||||
|
///
|
||||||
|
/// ## Platform-specific
|
||||||
|
///
|
||||||
|
/// - **Web:** Always returns an empty iterator
|
||||||
|
#[inline]
|
||||||
|
pub fn video_modes(&self) -> impl Iterator<Item = VideoMode> {
|
||||||
|
self.inner
|
||||||
|
.video_modes()
|
||||||
|
.map(|video_mode| VideoMode { video_mode })
|
||||||
|
}
|
||||||
|
}
|
||||||
93
src/platform/android.rs
Normal file
93
src/platform/android.rs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
use crate::{
|
||||||
|
event_loop::{EventLoop, EventLoopBuilder, EventLoopWindowTarget},
|
||||||
|
window::{Window, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
use android_activity::{AndroidApp, ConfigurationRef, Rect};
|
||||||
|
|
||||||
|
/// Additional methods on [`EventLoop`] that are specific to Android.
|
||||||
|
pub trait EventLoopExtAndroid {}
|
||||||
|
|
||||||
|
impl<T> EventLoopExtAndroid for EventLoop<T> {}
|
||||||
|
|
||||||
|
/// Additional methods on [`EventLoopWindowTarget`] that are specific to Android.
|
||||||
|
pub trait EventLoopWindowTargetExtAndroid {}
|
||||||
|
|
||||||
|
/// Additional methods on [`Window`] that are specific to Android.
|
||||||
|
pub trait WindowExtAndroid {
|
||||||
|
fn content_rect(&self) -> Rect;
|
||||||
|
|
||||||
|
fn config(&self) -> ConfigurationRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WindowExtAndroid for Window {
|
||||||
|
fn content_rect(&self) -> Rect {
|
||||||
|
self.window.content_rect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn config(&self) -> ConfigurationRef {
|
||||||
|
self.window.config()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopWindowTargetExtAndroid for EventLoopWindowTarget<T> {}
|
||||||
|
|
||||||
|
/// Additional methods on [`WindowBuilder`] that are specific to Android.
|
||||||
|
pub trait WindowBuilderExtAndroid {}
|
||||||
|
|
||||||
|
impl WindowBuilderExtAndroid for WindowBuilder {}
|
||||||
|
|
||||||
|
pub trait EventLoopBuilderExtAndroid {
|
||||||
|
/// Associates the `AndroidApp` that was passed to `android_main()` with the event loop
|
||||||
|
///
|
||||||
|
/// This must be called on Android since the `AndroidApp` is not global state.
|
||||||
|
fn with_android_app(&mut self, app: AndroidApp) -> &mut Self;
|
||||||
|
|
||||||
|
/// Calling this will mark the volume keys to be manually handled by the application
|
||||||
|
///
|
||||||
|
/// Default is to let the operating system handle the volume keys
|
||||||
|
fn handle_volume_keys(&mut self) -> &mut Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopBuilderExtAndroid for EventLoopBuilder<T> {
|
||||||
|
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
|
||||||
|
/// applications can rely on the Winit crate to resolve the required version of
|
||||||
|
/// `android_activity` and avoid any chance of a conflict between Winit and the
|
||||||
|
/// application crate.
|
||||||
|
///
|
||||||
|
/// Unlike most libraries there can only be a single implementation
|
||||||
|
/// of the `android_activity` glue crate linked with an application because
|
||||||
|
/// it is responsible for the application's `android_main()` entry point.
|
||||||
|
///
|
||||||
|
/// Since Winit depends on a specific version of `android_activity` the simplest
|
||||||
|
/// way to avoid creating a conflict is for applications to avoid explicitly
|
||||||
|
/// depending on the `android_activity` crate, and instead consume the API that
|
||||||
|
/// is re-exported by Winit.
|
||||||
|
///
|
||||||
|
/// For compatibility applications should then import the `AndroidApp` type for
|
||||||
|
/// their `android_main(app: AndroidApp)` function like:
|
||||||
|
/// ```rust
|
||||||
|
/// #[cfg(target_os="android")]
|
||||||
|
/// use winit::platform::android::activity::AndroidApp;
|
||||||
|
/// ```
|
||||||
|
pub mod activity {
|
||||||
|
// We enable the `"native-activity"` feature just so that we can build the
|
||||||
|
// docs, but it'll be very confusing for users to see the docs with that
|
||||||
|
// 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)]
|
||||||
|
pub use android_activity::*;
|
||||||
|
}
|
||||||
308
src/platform/ios.rs
Normal file
308
src/platform/ios.rs
Normal file
@@ -0,0 +1,308 @@
|
|||||||
|
use std::os::raw::c_void;
|
||||||
|
|
||||||
|
use icrate::Foundation::MainThreadMarker;
|
||||||
|
use objc2::rc::Id;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
event_loop::EventLoop,
|
||||||
|
monitor::{MonitorHandle, VideoMode},
|
||||||
|
window::{Window, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Additional methods on [`EventLoop`] that are specific to iOS.
|
||||||
|
pub trait EventLoopExtIOS {
|
||||||
|
/// Returns the [`Idiom`] (phone/tablet/tv/etc) for the current device.
|
||||||
|
fn idiom(&self) -> Idiom;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: 'static> EventLoopExtIOS for EventLoop<T> {
|
||||||
|
fn idiom(&self) -> Idiom {
|
||||||
|
self.event_loop.idiom()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional methods on [`Window`] that are specific to iOS.
|
||||||
|
pub trait WindowExtIOS {
|
||||||
|
/// 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
|
||||||
|
/// this to [`MonitorHandle::scale_factor()`].
|
||||||
|
///
|
||||||
|
/// [`UIWindow`]: https://developer.apple.com/documentation/uikit/uiwindow?language=objc
|
||||||
|
/// [`contentScaleFactor`]: https://developer.apple.com/documentation/uikit/uiview/1622657-contentscalefactor?language=objc
|
||||||
|
fn set_scale_factor(&self, scale_factor: f64);
|
||||||
|
|
||||||
|
/// Sets the valid orientations for the [`Window`].
|
||||||
|
///
|
||||||
|
/// The default value is [`ValidOrientations::LandscapeAndPortrait`].
|
||||||
|
///
|
||||||
|
/// This changes the value returned by
|
||||||
|
/// [`-[UIViewController supportedInterfaceOrientations]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations?language=objc),
|
||||||
|
/// and then calls
|
||||||
|
/// [`-[UIViewController attemptRotationToDeviceOrientation]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621400-attemptrotationtodeviceorientati?language=objc).
|
||||||
|
fn set_valid_orientations(&self, valid_orientations: ValidOrientations);
|
||||||
|
|
||||||
|
/// Sets whether the [`Window`] prefers the home indicator hidden.
|
||||||
|
///
|
||||||
|
/// The default is to prefer showing the home indicator.
|
||||||
|
///
|
||||||
|
/// This changes the value returned by
|
||||||
|
/// [`-[UIViewController prefersHomeIndicatorAutoHidden]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887510-prefershomeindicatorautohidden?language=objc),
|
||||||
|
/// and then calls
|
||||||
|
/// [`-[UIViewController setNeedsUpdateOfHomeIndicatorAutoHidden]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887509-setneedsupdateofhomeindicatoraut?language=objc).
|
||||||
|
///
|
||||||
|
/// This only has an effect on iOS 11.0+.
|
||||||
|
fn set_prefers_home_indicator_hidden(&self, hidden: bool);
|
||||||
|
|
||||||
|
/// Sets the screen edges for which the system gestures will take a lower priority than the
|
||||||
|
/// application's touch handling.
|
||||||
|
///
|
||||||
|
/// This changes the value returned by
|
||||||
|
/// [`-[UIViewController preferredScreenEdgesDeferringSystemGestures]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887512-preferredscreenedgesdeferringsys?language=objc),
|
||||||
|
/// and then calls
|
||||||
|
/// [`-[UIViewController setNeedsUpdateOfScreenEdgesDeferringSystemGestures]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887507-setneedsupdateofscreenedgesdefer?language=objc).
|
||||||
|
///
|
||||||
|
/// This only has an effect on iOS 11.0+.
|
||||||
|
fn set_preferred_screen_edges_deferring_system_gestures(&self, edges: ScreenEdge);
|
||||||
|
|
||||||
|
/// Sets whether the [`Window`] prefers the status bar hidden.
|
||||||
|
///
|
||||||
|
/// The default is to prefer showing the status bar.
|
||||||
|
///
|
||||||
|
/// This sets the value of the
|
||||||
|
/// [`prefersStatusBarHidden`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621440-prefersstatusbarhidden?language=objc)
|
||||||
|
/// property.
|
||||||
|
///
|
||||||
|
/// [`setNeedsStatusBarAppearanceUpdate()`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621354-setneedsstatusbarappearanceupdat?language=objc)
|
||||||
|
/// is also called for you.
|
||||||
|
fn set_prefers_status_bar_hidden(&self, hidden: bool);
|
||||||
|
|
||||||
|
/// Sets the preferred status bar style for the [`Window`].
|
||||||
|
///
|
||||||
|
/// The default is system-defined.
|
||||||
|
///
|
||||||
|
/// This sets the value of the
|
||||||
|
/// [`preferredStatusBarStyle`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621416-preferredstatusbarstyle?language=objc)
|
||||||
|
/// property.
|
||||||
|
///
|
||||||
|
/// [`setNeedsStatusBarAppearanceUpdate()`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621354-setneedsstatusbarappearanceupdat?language=objc)
|
||||||
|
/// is also called for you.
|
||||||
|
fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WindowExtIOS for Window {
|
||||||
|
#[inline]
|
||||||
|
fn set_scale_factor(&self, scale_factor: f64) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_scale_factor(scale_factor))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_valid_orientations(&self, valid_orientations: ValidOrientations) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_valid_orientations(valid_orientations))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_prefers_home_indicator_hidden(&self, hidden: bool) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_prefers_home_indicator_hidden(hidden))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_preferred_screen_edges_deferring_system_gestures(&self, edges: ScreenEdge) {
|
||||||
|
self.window.maybe_queue_on_main(move |w| {
|
||||||
|
w.set_preferred_screen_edges_deferring_system_gestures(edges)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_prefers_status_bar_hidden(&self, hidden: bool) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_prefers_status_bar_hidden(hidden))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_preferred_status_bar_style(status_bar_style))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional methods on [`WindowBuilder`] that are specific to iOS.
|
||||||
|
pub trait WindowBuilderExtIOS {
|
||||||
|
/// 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
|
||||||
|
/// this to [`MonitorHandle::scale_factor()`].
|
||||||
|
///
|
||||||
|
/// [`UIWindow`]: https://developer.apple.com/documentation/uikit/uiwindow?language=objc
|
||||||
|
/// [`contentScaleFactor`]: https://developer.apple.com/documentation/uikit/uiview/1622657-contentscalefactor?language=objc
|
||||||
|
fn with_scale_factor(self, scale_factor: f64) -> Self;
|
||||||
|
|
||||||
|
/// Sets the valid orientations for the [`Window`].
|
||||||
|
///
|
||||||
|
/// The default value is [`ValidOrientations::LandscapeAndPortrait`].
|
||||||
|
///
|
||||||
|
/// This sets the initial value returned by
|
||||||
|
/// [`-[UIViewController supportedInterfaceOrientations]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations?language=objc).
|
||||||
|
fn with_valid_orientations(self, valid_orientations: ValidOrientations) -> Self;
|
||||||
|
|
||||||
|
/// Sets whether the [`Window`] prefers the home indicator hidden.
|
||||||
|
///
|
||||||
|
/// The default is to prefer showing the home indicator.
|
||||||
|
///
|
||||||
|
/// This sets the initial value returned by
|
||||||
|
/// [`-[UIViewController prefersHomeIndicatorAutoHidden]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887510-prefershomeindicatorautohidden?language=objc).
|
||||||
|
///
|
||||||
|
/// This only has an effect on iOS 11.0+.
|
||||||
|
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.
|
||||||
|
///
|
||||||
|
/// This sets the initial value returned by
|
||||||
|
/// [`-[UIViewController preferredScreenEdgesDeferringSystemGestures]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2887512-preferredscreenedgesdeferringsys?language=objc).
|
||||||
|
///
|
||||||
|
/// This only has an effect on iOS 11.0+.
|
||||||
|
fn with_preferred_screen_edges_deferring_system_gestures(self, edges: ScreenEdge) -> Self;
|
||||||
|
|
||||||
|
/// Sets whether the [`Window`] prefers the status bar hidden.
|
||||||
|
///
|
||||||
|
/// The default is to prefer showing the status bar.
|
||||||
|
///
|
||||||
|
/// This sets the initial value returned by
|
||||||
|
/// [`-[UIViewController prefersStatusBarHidden]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621440-prefersstatusbarhidden?language=objc).
|
||||||
|
fn with_prefers_status_bar_hidden(self, hidden: bool) -> Self;
|
||||||
|
|
||||||
|
/// Sets the style of the [`Window`]'s status bar.
|
||||||
|
///
|
||||||
|
/// The default is system-defined.
|
||||||
|
///
|
||||||
|
/// This sets the initial value returned by
|
||||||
|
/// [`-[UIViewController preferredStatusBarStyle]`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621416-preferredstatusbarstyle?language=objc),
|
||||||
|
fn with_preferred_status_bar_style(self, status_bar_style: StatusBarStyle) -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WindowBuilderExtIOS for WindowBuilder {
|
||||||
|
#[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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional methods on [`MonitorHandle`] that are specific to iOS.
|
||||||
|
pub trait MonitorHandleExtIOS {
|
||||||
|
/// Returns a pointer to the [`UIScreen`] that is used by this monitor.
|
||||||
|
///
|
||||||
|
/// [`UIScreen`]: https://developer.apple.com/documentation/uikit/uiscreen?language=objc
|
||||||
|
fn ui_screen(&self) -> *mut c_void;
|
||||||
|
|
||||||
|
/// Returns the preferred [`VideoMode`] for this monitor.
|
||||||
|
///
|
||||||
|
/// This translates to a call to [`-[UIScreen preferredMode]`](https://developer.apple.com/documentation/uikit/uiscreen/1617823-preferredmode?language=objc).
|
||||||
|
fn preferred_video_mode(&self) -> VideoMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MonitorHandleExtIOS for MonitorHandle {
|
||||||
|
#[inline]
|
||||||
|
fn ui_screen(&self) -> *mut c_void {
|
||||||
|
// SAFETY: The marker is only used to get the pointer of the screen
|
||||||
|
let mtm = unsafe { MainThreadMarker::new_unchecked() };
|
||||||
|
Id::as_ptr(self.inner.ui_screen(mtm)) as *mut c_void
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn preferred_video_mode(&self) -> VideoMode {
|
||||||
|
VideoMode {
|
||||||
|
video_mode: self.inner.preferred_video_mode(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Valid orientations for a particular [`Window`].
|
||||||
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum ValidOrientations {
|
||||||
|
/// Excludes `PortraitUpsideDown` on iphone
|
||||||
|
#[default]
|
||||||
|
LandscapeAndPortrait,
|
||||||
|
|
||||||
|
Landscape,
|
||||||
|
|
||||||
|
/// Excludes `PortraitUpsideDown` on iphone
|
||||||
|
Portrait,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The device [idiom].
|
||||||
|
///
|
||||||
|
/// [idiom]: https://developer.apple.com/documentation/uikit/uidevice/1620037-userinterfaceidiom?language=objc
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum Idiom {
|
||||||
|
Unspecified,
|
||||||
|
|
||||||
|
/// iPhone and iPod touch.
|
||||||
|
Phone,
|
||||||
|
|
||||||
|
/// iPad.
|
||||||
|
Pad,
|
||||||
|
|
||||||
|
/// tvOS and Apple TV.
|
||||||
|
TV,
|
||||||
|
CarPlay,
|
||||||
|
}
|
||||||
|
|
||||||
|
bitflags! {
|
||||||
|
/// The [edges] of a screen.
|
||||||
|
///
|
||||||
|
/// [edges]: https://developer.apple.com/documentation/uikit/uirectedge?language=objc
|
||||||
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct ScreenEdge: u8 {
|
||||||
|
const NONE = 0;
|
||||||
|
const TOP = 1 << 0;
|
||||||
|
const LEFT = 1 << 1;
|
||||||
|
const BOTTOM = 1 << 2;
|
||||||
|
const RIGHT = 1 << 3;
|
||||||
|
const ALL = ScreenEdge::TOP.bits() | ScreenEdge::LEFT.bits()
|
||||||
|
| ScreenEdge::BOTTOM.bits() | ScreenEdge::RIGHT.bits();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
|
pub enum StatusBarStyle {
|
||||||
|
#[default]
|
||||||
|
Default,
|
||||||
|
LightContent,
|
||||||
|
DarkContent,
|
||||||
|
}
|
||||||
422
src/platform/macos.rs
Normal file
422
src/platform/macos.rs
Normal file
@@ -0,0 +1,422 @@
|
|||||||
|
use std::os::raw::c_void;
|
||||||
|
|
||||||
|
use objc2::rc::Id;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
event_loop::{EventLoopBuilder, EventLoopWindowTarget},
|
||||||
|
monitor::MonitorHandle,
|
||||||
|
window::{Window, WindowBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Additional methods on [`Window`] that are specific to MacOS.
|
||||||
|
pub trait WindowExtMacOS {
|
||||||
|
/// Returns whether or not the window is in simple fullscreen mode.
|
||||||
|
fn simple_fullscreen(&self) -> bool;
|
||||||
|
|
||||||
|
/// Toggles a fullscreen mode that doesn't require a new macOS space.
|
||||||
|
/// Returns a boolean indicating whether the transition was successful (this
|
||||||
|
/// won't work if the window was already in the native fullscreen).
|
||||||
|
///
|
||||||
|
/// This is how fullscreen used to work on macOS in versions before Lion.
|
||||||
|
/// And allows the user to have a fullscreen window without using another
|
||||||
|
/// space or taking control over the entire monitor.
|
||||||
|
fn set_simple_fullscreen(&self, fullscreen: bool) -> bool;
|
||||||
|
|
||||||
|
/// Returns whether or not the window has shadow.
|
||||||
|
fn has_shadow(&self) -> bool;
|
||||||
|
|
||||||
|
/// Sets whether or not the window has shadow.
|
||||||
|
fn set_has_shadow(&self, has_shadow: bool);
|
||||||
|
|
||||||
|
/// Group windows together by using the same tabbing identifier.
|
||||||
|
///
|
||||||
|
/// <https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier>
|
||||||
|
fn set_tabbing_identifier(&self, identifier: &str);
|
||||||
|
|
||||||
|
/// Returns the window's tabbing identifier.
|
||||||
|
fn tabbing_identifier(&self) -> String;
|
||||||
|
|
||||||
|
/// Select next tab.
|
||||||
|
fn select_next_tab(&self);
|
||||||
|
|
||||||
|
/// Select previous tab.
|
||||||
|
fn select_previous_tab(&self);
|
||||||
|
|
||||||
|
/// Select the tab with the given index.
|
||||||
|
///
|
||||||
|
/// Will no-op when the index is out of bounds.
|
||||||
|
fn select_tab_at_index(&self, index: usize);
|
||||||
|
|
||||||
|
/// Get the number of tabs in the window tab group.
|
||||||
|
fn num_tabs(&self) -> usize;
|
||||||
|
|
||||||
|
/// Get the window's edit state.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```ignore
|
||||||
|
/// WindowEvent::CloseRequested => {
|
||||||
|
/// if window.is_document_edited() {
|
||||||
|
/// // Show the user a save pop-up or similar
|
||||||
|
/// } else {
|
||||||
|
/// // Close the window
|
||||||
|
/// drop(window);
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
fn is_document_edited(&self) -> bool;
|
||||||
|
|
||||||
|
/// Put the window in a state which indicates a file save is required.
|
||||||
|
fn set_document_edited(&self, edited: bool);
|
||||||
|
|
||||||
|
/// Set option as alt behavior as described in [`OptionAsAlt`].
|
||||||
|
///
|
||||||
|
/// This will ignore diacritical marks and accent characters from
|
||||||
|
/// being processed as received characters. Instead, the input
|
||||||
|
/// device's raw character will be placed in event queues with the
|
||||||
|
/// Alt modifier set.
|
||||||
|
fn set_option_as_alt(&self, option_as_alt: OptionAsAlt);
|
||||||
|
|
||||||
|
/// Getter for the [`WindowExtMacOS::set_option_as_alt`].
|
||||||
|
fn option_as_alt(&self) -> OptionAsAlt;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WindowExtMacOS for Window {
|
||||||
|
#[inline]
|
||||||
|
fn simple_fullscreen(&self) -> bool {
|
||||||
|
self.window.maybe_wait_on_main(|w| w.simple_fullscreen())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
|
||||||
|
self.window
|
||||||
|
.maybe_wait_on_main(move |w| w.set_simple_fullscreen(fullscreen))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn has_shadow(&self) -> bool {
|
||||||
|
self.window.maybe_wait_on_main(|w| w.has_shadow())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_has_shadow(&self, has_shadow: bool) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_has_shadow(has_shadow))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_tabbing_identifier(&self, identifier: &str) {
|
||||||
|
self.window
|
||||||
|
.maybe_wait_on_main(|w| w.set_tabbing_identifier(identifier))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn tabbing_identifier(&self) -> String {
|
||||||
|
self.window.maybe_wait_on_main(|w| w.tabbing_identifier())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn select_next_tab(&self) {
|
||||||
|
self.window.maybe_queue_on_main(|w| w.select_next_tab())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn select_previous_tab(&self) {
|
||||||
|
self.window.maybe_queue_on_main(|w| w.select_previous_tab())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn select_tab_at_index(&self, index: usize) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.select_tab_at_index(index))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn num_tabs(&self) -> usize {
|
||||||
|
self.window.maybe_wait_on_main(|w| w.num_tabs())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn is_document_edited(&self) -> bool {
|
||||||
|
self.window.maybe_wait_on_main(|w| w.is_document_edited())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_document_edited(&self, edited: bool) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_document_edited(edited))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn set_option_as_alt(&self, option_as_alt: OptionAsAlt) {
|
||||||
|
self.window
|
||||||
|
.maybe_queue_on_main(move |w| w.set_option_as_alt(option_as_alt))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn option_as_alt(&self) -> OptionAsAlt {
|
||||||
|
self.window.maybe_wait_on_main(|w| w.option_as_alt())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Corresponds to `NSApplicationActivationPolicy`.
|
||||||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum ActivationPolicy {
|
||||||
|
/// Corresponds to `NSApplicationActivationPolicyRegular`.
|
||||||
|
#[default]
|
||||||
|
Regular,
|
||||||
|
|
||||||
|
/// Corresponds to `NSApplicationActivationPolicyAccessory`.
|
||||||
|
Accessory,
|
||||||
|
|
||||||
|
/// Corresponds to `NSApplicationActivationPolicyProhibited`.
|
||||||
|
Prohibited,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional methods on [`WindowBuilder`] that are specific to MacOS.
|
||||||
|
///
|
||||||
|
/// **Note:** Properties dealing with the titlebar will be overwritten by the [`WindowBuilder::with_decorations`] method:
|
||||||
|
/// - `with_titlebar_transparent`
|
||||||
|
/// - `with_title_hidden`
|
||||||
|
/// - `with_titlebar_hidden`
|
||||||
|
/// - `with_titlebar_buttons_hidden`
|
||||||
|
/// - `with_fullsize_content_view`
|
||||||
|
pub trait WindowBuilderExtMacOS {
|
||||||
|
/// Enables click-and-drag behavior for the entire window, not just the titlebar.
|
||||||
|
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.
|
||||||
|
fn with_titlebar_transparent(self, titlebar_transparent: bool) -> Self;
|
||||||
|
/// Hides the window title.
|
||||||
|
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.
|
||||||
|
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.
|
||||||
|
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>
|
||||||
|
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.
|
||||||
|
fn with_option_as_alt(self, option_as_alt: OptionAsAlt) -> Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WindowBuilderExtMacOS for WindowBuilder {
|
||||||
|
#[inline]
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> Self {
|
||||||
|
self.platform_specific.titlebar_transparent = titlebar_transparent;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait EventLoopBuilderExtMacOS {
|
||||||
|
/// Sets the activation policy for the application.
|
||||||
|
///
|
||||||
|
/// It is set to [`ActivationPolicy::Regular`] by default.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// Set the activation policy to "accessory".
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use winit::event_loop::EventLoopBuilder;
|
||||||
|
/// #[cfg(target_os = "macos")]
|
||||||
|
/// use winit::platform::macos::{EventLoopBuilderExtMacOS, ActivationPolicy};
|
||||||
|
///
|
||||||
|
/// let mut builder = EventLoopBuilder::new();
|
||||||
|
/// #[cfg(target_os = "macos")]
|
||||||
|
/// builder.with_activation_policy(ActivationPolicy::Accessory);
|
||||||
|
/// # if false { // We can't test this part
|
||||||
|
/// let event_loop = builder.build();
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
fn with_activation_policy(&mut self, activation_policy: ActivationPolicy) -> &mut Self;
|
||||||
|
|
||||||
|
/// Used to control whether a default menubar menu is created.
|
||||||
|
///
|
||||||
|
/// Menu creation is enabled by default.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// Disable creating a default menubar.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use winit::event_loop::EventLoopBuilder;
|
||||||
|
/// #[cfg(target_os = "macos")]
|
||||||
|
/// use winit::platform::macos::EventLoopBuilderExtMacOS;
|
||||||
|
///
|
||||||
|
/// let mut builder = EventLoopBuilder::new();
|
||||||
|
/// #[cfg(target_os = "macos")]
|
||||||
|
/// builder.with_default_menu(false);
|
||||||
|
/// # if false { // We can't test this part
|
||||||
|
/// let event_loop = builder.build();
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
fn with_default_menu(&mut self, enable: bool) -> &mut Self;
|
||||||
|
|
||||||
|
/// Used to prevent the application from automatically activating when launched if
|
||||||
|
/// another application is already active.
|
||||||
|
///
|
||||||
|
/// The default behavior is to ignore other applications and activate when launched.
|
||||||
|
fn with_activate_ignoring_other_apps(&mut self, ignore: bool) -> &mut Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopBuilderExtMacOS for EventLoopBuilder<T> {
|
||||||
|
#[inline]
|
||||||
|
fn with_activation_policy(&mut self, activation_policy: ActivationPolicy) -> &mut Self {
|
||||||
|
self.platform_specific.activation_policy = 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 the identifier of the monitor for Cocoa.
|
||||||
|
fn native_id(&self) -> u32;
|
||||||
|
/// Returns a pointer to the NSScreen representing this monitor.
|
||||||
|
fn ns_screen(&self) -> Option<*mut c_void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MonitorHandleExtMacOS for MonitorHandle {
|
||||||
|
#[inline]
|
||||||
|
fn native_id(&self) -> u32 {
|
||||||
|
self.inner.native_identifier()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ns_screen(&self) -> Option<*mut c_void> {
|
||||||
|
self.inner.ns_screen().map(|s| Id::as_ptr(&s) as _)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional methods on [`EventLoopWindowTarget`] that are specific to macOS.
|
||||||
|
pub trait EventLoopWindowTargetExtMacOS {
|
||||||
|
/// Hide the entire application. In most applications this is typically triggered with Command-H.
|
||||||
|
fn hide_application(&self);
|
||||||
|
/// Hide the other applications. In most applications this is typically triggered with Command+Option-H.
|
||||||
|
fn hide_other_applications(&self);
|
||||||
|
/// Set whether the system can automatically organize windows into tabs.
|
||||||
|
///
|
||||||
|
/// <https://developer.apple.com/documentation/appkit/nswindow/1646657-allowsautomaticwindowtabbing>
|
||||||
|
fn set_allows_automatic_window_tabbing(&self, enabled: bool);
|
||||||
|
/// Returns whether the system can automatically organize windows into tabs.
|
||||||
|
fn allows_automatic_window_tabbing(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopWindowTargetExtMacOS for EventLoopWindowTarget<T> {
|
||||||
|
fn hide_application(&self) {
|
||||||
|
self.p.hide_application()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hide_other_applications(&self) {
|
||||||
|
self.p.hide_other_applications()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_allows_automatic_window_tabbing(&self, enabled: bool) {
|
||||||
|
self.p.set_allows_automatic_window_tabbing(enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn allows_automatic_window_tabbing(&self) -> bool {
|
||||||
|
self.p.allows_automatic_window_tabbing()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Option as alt behavior.
|
||||||
|
///
|
||||||
|
/// The default is `None`.
|
||||||
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
pub enum OptionAsAlt {
|
||||||
|
/// The left `Option` key is treated as `Alt`.
|
||||||
|
OnlyLeft,
|
||||||
|
|
||||||
|
/// The right `Option` key is treated as `Alt`.
|
||||||
|
OnlyRight,
|
||||||
|
|
||||||
|
/// Both `Option` keys are treated as `Alt`.
|
||||||
|
Both,
|
||||||
|
|
||||||
|
/// No special handling is applied for `Option` key.
|
||||||
|
#[default]
|
||||||
|
None,
|
||||||
|
}
|
||||||
57
src/platform/mod.rs
Normal file
57
src/platform/mod.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
//! Contains traits with platform-specific methods in them.
|
||||||
|
//!
|
||||||
|
//! Contains the follow OS-specific modules:
|
||||||
|
//!
|
||||||
|
//! - `android`
|
||||||
|
//! - `ios`
|
||||||
|
//! - `macos`
|
||||||
|
//! - `unix`
|
||||||
|
//! - `windows`
|
||||||
|
//! - `web`
|
||||||
|
//!
|
||||||
|
//! And the following platform-specific modules:
|
||||||
|
//!
|
||||||
|
//! - `run_on_demand` (available on `windows`, `unix`, `macos`, `android`)
|
||||||
|
//! - `pump_events` (available on `windows`, `unix`, `macos`, `android`)
|
||||||
|
//!
|
||||||
|
//! However only the module 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(wasm_platform)]
|
||||||
|
pub mod web;
|
||||||
|
#[cfg(windows_platform)]
|
||||||
|
pub mod windows;
|
||||||
|
#[cfg(x11_platform)]
|
||||||
|
pub mod x11;
|
||||||
|
|
||||||
|
#[cfg(any(
|
||||||
|
windows_platform,
|
||||||
|
macos_platform,
|
||||||
|
android_platform,
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform
|
||||||
|
))]
|
||||||
|
pub mod run_on_demand;
|
||||||
|
|
||||||
|
#[cfg(any(
|
||||||
|
windows_platform,
|
||||||
|
macos_platform,
|
||||||
|
android_platform,
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform
|
||||||
|
))]
|
||||||
|
pub mod pump_events;
|
||||||
|
|
||||||
|
pub mod modifier_supplement;
|
||||||
|
pub mod scancode;
|
||||||
24
src/platform/modifier_supplement.rs
Normal file
24
src/platform/modifier_supplement.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#![cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform))]
|
||||||
|
|
||||||
|
use crate::keyboard::Key;
|
||||||
|
|
||||||
|
/// Additional methods for the `KeyEvent` which cannot be implemented on all
|
||||||
|
/// platforms.
|
||||||
|
pub trait KeyEventExtModifierSupplement {
|
||||||
|
/// Identical to `KeyEvent::text` but this is affected by <kbd>Ctrl</kbd>.
|
||||||
|
///
|
||||||
|
/// For example, pressing <kbd>Ctrl</kbd>+<kbd>a</kbd> produces `Some("\x01")`.
|
||||||
|
fn text_with_all_modifiers(&self) -> Option<&str>;
|
||||||
|
|
||||||
|
/// This value ignores all modifiers including,
|
||||||
|
/// but not limited to <kbd>Shift</kbd>, <kbd>Caps Lock</kbd>,
|
||||||
|
/// and <kbd>Ctrl</kbd>. In most cases this means that the
|
||||||
|
/// unicode character in the resulting string is lowercase.
|
||||||
|
///
|
||||||
|
/// This is useful for key-bindings / shortcut key combinations.
|
||||||
|
///
|
||||||
|
/// In case `logical_key` reports `Dead`, this will still report the
|
||||||
|
/// key as `Character` according to the current keyboard layout. This value
|
||||||
|
/// cannot be `Dead`.
|
||||||
|
fn key_without_modifiers(&self) -> Key;
|
||||||
|
}
|
||||||
1
src/platform/orbital.rs
Normal file
1
src/platform/orbital.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// There are no Orbital specific traits yet.
|
||||||
189
src/platform/pump_events.rs
Normal file
189
src/platform/pump_events.rs
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
event::Event,
|
||||||
|
event_loop::{EventLoop, EventLoopWindowTarget},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The return status for `pump_events`
|
||||||
|
pub enum PumpStatus {
|
||||||
|
/// Continue running external loop.
|
||||||
|
Continue,
|
||||||
|
/// Exit external loop.
|
||||||
|
Exit(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Additional methods on [`EventLoop`] for pumping events within an external event loop
|
||||||
|
pub trait EventLoopExtPumpEvents {
|
||||||
|
/// A type provided by the user that can be passed through [`Event::UserEvent`].
|
||||||
|
type UserEvent;
|
||||||
|
|
||||||
|
/// Pump the `EventLoop` to check for and dispatch pending events.
|
||||||
|
///
|
||||||
|
/// This API is designed to enable applications to integrate Winit into an
|
||||||
|
/// external event loop, for platforms that can support this.
|
||||||
|
///
|
||||||
|
/// The given `timeout` limits how long it may block waiting for new events.
|
||||||
|
///
|
||||||
|
/// Passing a `timeout` of `Some(Duration::ZERO)` would ensure your external
|
||||||
|
/// event loop is never blocked but you would likely need to consider how
|
||||||
|
/// to throttle your own external loop.
|
||||||
|
///
|
||||||
|
/// Passing a `timeout` of `None` means that it may wait indefinitely for new
|
||||||
|
/// events before returning control back to the external loop.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
///
|
||||||
|
/// ```rust,no_run
|
||||||
|
/// # // Copied from examples/window_pump_events.rs
|
||||||
|
/// # #[cfg(any(
|
||||||
|
/// # windows_platform,
|
||||||
|
/// # macos_platform,
|
||||||
|
/// # x11_platform,
|
||||||
|
/// # wayland_platform,
|
||||||
|
/// # android_platform,
|
||||||
|
/// # ))]
|
||||||
|
/// fn main() -> std::process::ExitCode {
|
||||||
|
/// # use std::{process::ExitCode, thread::sleep, time::Duration};
|
||||||
|
/// #
|
||||||
|
/// # use simple_logger::SimpleLogger;
|
||||||
|
/// # use winit::{
|
||||||
|
/// # event::{Event, WindowEvent},
|
||||||
|
/// # event_loop::EventLoop,
|
||||||
|
/// # platform::pump_events::{EventLoopExtPumpEvents, PumpStatus},
|
||||||
|
/// # window::WindowBuilder,
|
||||||
|
/// # };
|
||||||
|
/// let mut event_loop = EventLoop::new().unwrap();
|
||||||
|
/// #
|
||||||
|
/// # SimpleLogger::new().init().unwrap();
|
||||||
|
/// let window = WindowBuilder::new()
|
||||||
|
/// .with_title("A fantastic window!")
|
||||||
|
/// .build(&event_loop)
|
||||||
|
/// .unwrap();
|
||||||
|
///
|
||||||
|
/// 'main: loop {
|
||||||
|
/// let timeout = Some(Duration::ZERO);
|
||||||
|
/// let status = event_loop.pump_events(timeout, |event, elwt| {
|
||||||
|
/// # if let Event::WindowEvent { event, .. } = &event {
|
||||||
|
/// # // Print only Window events to reduce noise
|
||||||
|
/// # println!("{event:?}");
|
||||||
|
/// # }
|
||||||
|
/// #
|
||||||
|
/// match event {
|
||||||
|
/// Event::WindowEvent {
|
||||||
|
/// event: WindowEvent::CloseRequested,
|
||||||
|
/// window_id,
|
||||||
|
/// } if window_id == window.id() => elwt.exit(),
|
||||||
|
/// Event::AboutToWait => {
|
||||||
|
/// window.request_redraw();
|
||||||
|
/// }
|
||||||
|
/// _ => (),
|
||||||
|
/// }
|
||||||
|
/// });
|
||||||
|
/// if let PumpStatus::Exit(exit_code) = status {
|
||||||
|
/// break 'main ExitCode::from(exit_code as u8);
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // Sleep for 1/60 second to simulate application work
|
||||||
|
/// //
|
||||||
|
/// // Since `pump_events` doesn't block it will be important to
|
||||||
|
/// // throttle the loop in the app somehow.
|
||||||
|
/// println!("Update()");
|
||||||
|
/// sleep(Duration::from_millis(16));
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// **Note:** This is not a portable API, and its usage involves a number of
|
||||||
|
/// caveats and trade offs that should be considered before using this API!
|
||||||
|
///
|
||||||
|
/// You almost certainly shouldn't use this API, unless you absolutely know it's
|
||||||
|
/// the only practical option you have.
|
||||||
|
///
|
||||||
|
/// ## Synchronous events
|
||||||
|
///
|
||||||
|
/// Some events _must_ only be handled synchronously via the closure that
|
||||||
|
/// is passed to Winit so that the handler will also be synchronized with
|
||||||
|
/// the window system and operating system.
|
||||||
|
///
|
||||||
|
/// This is because some events are driven by a window system callback
|
||||||
|
/// where the window systems expects the application to have handled the
|
||||||
|
/// event before returning.
|
||||||
|
///
|
||||||
|
/// **These events can not be buffered and handled outside of the closure
|
||||||
|
/// passed to Winit.**
|
||||||
|
///
|
||||||
|
/// As a general rule it is not recommended to ever buffer events to handle
|
||||||
|
/// them outside of the closure passed to Winit since it's difficult to
|
||||||
|
/// provide guarantees about which events are safe to buffer across all
|
||||||
|
/// operating systems.
|
||||||
|
///
|
||||||
|
/// Notable events that will certainly create portability problems if
|
||||||
|
/// buffered and handled outside of Winit include:
|
||||||
|
/// - `RedrawRequested` events, used to schedule rendering.
|
||||||
|
///
|
||||||
|
/// macOS for example uses a `drawRect` callback to drive rendering
|
||||||
|
/// within applications and expects rendering to be finished before
|
||||||
|
/// the `drawRect` callback returns.
|
||||||
|
///
|
||||||
|
/// For portability it's strongly recommended that applications should
|
||||||
|
/// keep their rendering inside the closure provided to Winit.
|
||||||
|
/// - Any lifecycle events, such as `Suspended` / `Resumed`.
|
||||||
|
///
|
||||||
|
/// The handling of these events needs to be synchronized with the
|
||||||
|
/// operating system and it would never be appropriate to buffer a
|
||||||
|
/// notification that your application has been suspended or resumed and
|
||||||
|
/// then handled that later since there would always be a chance that
|
||||||
|
/// other lifecycle events occur while the event is buffered.
|
||||||
|
///
|
||||||
|
/// ## Supported Platforms
|
||||||
|
/// - Windows
|
||||||
|
/// - Linux
|
||||||
|
/// - MacOS
|
||||||
|
/// - Android
|
||||||
|
///
|
||||||
|
/// ## Unsupported Platforms
|
||||||
|
/// - **Web:** This API is fundamentally incompatible with the event-based way in which
|
||||||
|
/// Web browsers work because it's not possible to have a long-running external
|
||||||
|
/// loop that would block the browser and there is nothing that can be
|
||||||
|
/// polled to ask for new new events. Events are delivered via callbacks based
|
||||||
|
/// on an event loop that is internal to the browser itself.
|
||||||
|
/// - **iOS:** It's not possible to stop and start an `NSApplication` repeatedly on iOS so
|
||||||
|
/// there's no way to support the same approach to polling as on MacOS.
|
||||||
|
///
|
||||||
|
/// ## Platform-specific
|
||||||
|
/// - **Windows**: The implementation will use `PeekMessage` when checking for
|
||||||
|
/// window messages to avoid blocking your external event loop.
|
||||||
|
///
|
||||||
|
/// - **MacOS**: The implementation works in terms of stopping the global `NSApp`
|
||||||
|
/// whenever the application `RunLoop` indicates that it is preparing to block
|
||||||
|
/// and wait for new events.
|
||||||
|
///
|
||||||
|
/// This is very different to the polling APIs that are available on other
|
||||||
|
/// platforms (the lower level polling primitives on MacOS are private
|
||||||
|
/// implementation details for `NSApp` which aren't accessible to application
|
||||||
|
/// developers)
|
||||||
|
///
|
||||||
|
/// It's likely this will be less efficient than polling on other OSs and
|
||||||
|
/// it also means the `NSApp` is stopped while outside of the Winit
|
||||||
|
/// event loop - and that's observable (for example to crates like `rfd`)
|
||||||
|
/// because the `NSApp` is global state.
|
||||||
|
///
|
||||||
|
/// 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_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
|
||||||
|
where
|
||||||
|
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopExtPumpEvents for EventLoop<T> {
|
||||||
|
type UserEvent = T;
|
||||||
|
|
||||||
|
fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
|
||||||
|
where
|
||||||
|
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>),
|
||||||
|
{
|
||||||
|
self.event_loop.pump_events(timeout, event_handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
89
src/platform/run_on_demand.rs
Normal file
89
src/platform/run_on_demand.rs
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
use crate::{
|
||||||
|
error::EventLoopError,
|
||||||
|
event::Event,
|
||||||
|
event_loop::{EventLoop, EventLoopWindowTarget},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(doc)]
|
||||||
|
use crate::{platform::pump_events::EventLoopExtPumpEvents, window::Window};
|
||||||
|
|
||||||
|
/// Additional methods on [`EventLoop`] to return control flow to the caller.
|
||||||
|
pub trait EventLoopExtRunOnDemand {
|
||||||
|
/// A type provided by the user that can be passed through [`Event::UserEvent`].
|
||||||
|
type UserEvent;
|
||||||
|
|
||||||
|
/// Runs the event loop in the calling thread and calls the given `event_handler` closure
|
||||||
|
/// to dispatch any window system events.
|
||||||
|
///
|
||||||
|
/// Unlike [`EventLoop::run`], 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
|
||||||
|
/// system resources, such as a display server connection.
|
||||||
|
///
|
||||||
|
/// This API is not designed to run an event loop in bursts that you can exit from and return
|
||||||
|
/// to while maintaining the full state of your application. (If you need something like this
|
||||||
|
/// you can look at the [`EventLoopExtPumpEvents::pump_events()`] API)
|
||||||
|
///
|
||||||
|
/// Each time `run_on_demand` is called the `event_handler` can expect to receive a
|
||||||
|
/// `NewEvents(Init)` and `Resumed` event (even on platforms that have no suspend/resume
|
||||||
|
/// lifecycle) - which can be used to consistently initialize application state.
|
||||||
|
///
|
||||||
|
/// See the [`set_control_flow()`] docs on how to change the event loop's behavior.
|
||||||
|
///
|
||||||
|
/// # 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 - though with the Web
|
||||||
|
/// backend it is possible to use `EventLoopExtWebSys::spawn()`[^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()`] for portability, unless you specifically need
|
||||||
|
/// the ability to re-run a single event loop more than once
|
||||||
|
///
|
||||||
|
/// # Supported Platforms
|
||||||
|
/// - Windows
|
||||||
|
/// - Linux
|
||||||
|
/// - macOS
|
||||||
|
/// - Android
|
||||||
|
///
|
||||||
|
/// # Unsupported Platforms
|
||||||
|
/// - **Web:** This API is fundamentally incompatible with the event-based way in which
|
||||||
|
/// Web browsers work because it's not possible to have a long-running external
|
||||||
|
/// loop that would block the browser and there is nothing that can be
|
||||||
|
/// polled to ask for new events. Events are delivered via callbacks based
|
||||||
|
/// on an event loop that is internal to the browser itself.
|
||||||
|
/// - **iOS:** It's not possible to stop and start an `NSApplication` repeatedly on iOS.
|
||||||
|
///
|
||||||
|
#[cfg_attr(
|
||||||
|
not(wasm_platform),
|
||||||
|
doc = "[^1]: `spawn()` is only available on `wasm` platforms."
|
||||||
|
)]
|
||||||
|
///
|
||||||
|
/// [`exit()`]: EventLoopWindowTarget::exit()
|
||||||
|
/// [`set_control_flow()`]: EventLoopWindowTarget::set_control_flow()
|
||||||
|
fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
|
||||||
|
where
|
||||||
|
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopExtRunOnDemand for EventLoop<T> {
|
||||||
|
type UserEvent = T;
|
||||||
|
|
||||||
|
fn run_on_demand<F>(&mut self, event_handler: F) -> Result<(), EventLoopError>
|
||||||
|
where
|
||||||
|
F: FnMut(Event<Self::UserEvent>, &EventLoopWindowTarget<Self::UserEvent>),
|
||||||
|
{
|
||||||
|
self.event_loop.window_target().clear_exit();
|
||||||
|
self.event_loop.run_on_demand(event_handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EventLoopWindowTarget<T> {
|
||||||
|
/// Clear exit status.
|
||||||
|
pub(crate) fn clear_exit(&self) {
|
||||||
|
self.p.clear_exit()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
|
#![cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform))]
|
||||||
|
|
||||||
use crate::keyboard::{KeyCode, PhysicalKey};
|
use crate::keyboard::{KeyCode, PhysicalKey};
|
||||||
|
|
||||||
// TODO: Describe what this value contains for each platform
|
// TODO: Describe what this value contains for each platform
|
||||||
|
|
||||||
/// Additional methods for the [`PhysicalKey`] type that allow the user to access the
|
/// Additional methods for the [`PhysicalKey`] type that allow the user to access the platform-specific
|
||||||
/// platform-specific scancode.
|
/// scancode.
|
||||||
///
|
///
|
||||||
/// [`PhysicalKey`]: crate::keyboard::PhysicalKey
|
/// [`PhysicalKey`]: crate::keyboard::PhysicalKey
|
||||||
pub trait PhysicalKeyExtScancode {
|
pub trait PhysicalKeyExtScancode {
|
||||||
/// The raw value of the platform-specific physical key identifier.
|
/// The raw value of the platform-specific physical key identifier.
|
||||||
///
|
///
|
||||||
/// Returns `Some(key_id)` if the conversion was successful; returns `None` otherwise.
|
/// Returns `Some(key_id)` if the conversion was succesful; returns `None` otherwise.
|
||||||
///
|
///
|
||||||
/// ## Platform-specific
|
/// ## Platform-specific
|
||||||
/// - **Windows:** A 16bit extended scancode
|
/// - **Windows:** A 16bit extended scancode
|
||||||
@@ -23,28 +25,21 @@ pub trait PhysicalKeyExtScancode {
|
|||||||
///
|
///
|
||||||
/// ## Platform-specific
|
/// ## Platform-specific
|
||||||
/// - **Wayland/X11**: A 32-bit linux scancode. When building from X11/Wayland keycode subtract
|
/// - **Wayland/X11**: A 32-bit linux scancode. When building from X11/Wayland keycode subtract
|
||||||
/// `8` to get the value you wanted.
|
/// `8` to get the value you wanted.
|
||||||
fn from_scancode(scancode: u32) -> PhysicalKey;
|
fn from_scancode(scancode: u32) -> PhysicalKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PhysicalKeyExtScancode for PhysicalKey {
|
impl PhysicalKeyExtScancode for KeyCode
|
||||||
fn to_scancode(self) -> Option<u32> {
|
where
|
||||||
crate::platform_impl::physicalkey_to_scancode(self)
|
PhysicalKey: PhysicalKeyExtScancode,
|
||||||
}
|
{
|
||||||
|
|
||||||
fn from_scancode(scancode: u32) -> PhysicalKey {
|
|
||||||
crate::platform_impl::scancode_to_physicalkey(scancode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PhysicalKeyExtScancode for KeyCode {
|
|
||||||
#[inline]
|
|
||||||
fn to_scancode(self) -> Option<u32> {
|
|
||||||
<PhysicalKey as PhysicalKeyExtScancode>::to_scancode(PhysicalKey::Code(self))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_scancode(scancode: u32) -> PhysicalKey {
|
fn from_scancode(scancode: u32) -> PhysicalKey {
|
||||||
<PhysicalKey as PhysicalKeyExtScancode>::from_scancode(scancode)
|
<PhysicalKey as PhysicalKeyExtScancode>::from_scancode(scancode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn to_scancode(self) -> Option<u32> {
|
||||||
|
<PhysicalKey as PhysicalKeyExtScancode>::to_scancode(PhysicalKey::Code(self))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -23,11 +23,9 @@
|
|||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use crate::error::{NotSupportedError, RequestError};
|
use crate::error::NotSupportedError;
|
||||||
use crate::event_loop::{ActiveEventLoop, AsyncRequestSerial};
|
use crate::event_loop::{AsyncRequestSerial, EventLoopWindowTarget};
|
||||||
#[cfg(wayland_platform)]
|
use crate::window::{ActivationToken, Window, WindowBuilder};
|
||||||
use crate::platform::wayland::ActiveEventLoopExtWayland;
|
|
||||||
use crate::window::{ActivationToken, Window};
|
|
||||||
|
|
||||||
/// The variable which is used mostly on X11.
|
/// The variable which is used mostly on X11.
|
||||||
const X11_VAR: &str = "DESKTOP_STARTUP_ID";
|
const X11_VAR: &str = "DESKTOP_STARTUP_ID";
|
||||||
@@ -46,10 +44,10 @@ pub trait WindowExtStartupNotify {
|
|||||||
/// Request a new activation token.
|
/// Request a new activation token.
|
||||||
///
|
///
|
||||||
/// The token will be delivered inside
|
/// The token will be delivered inside
|
||||||
fn request_activation_token(&self) -> Result<AsyncRequestSerial, RequestError>;
|
fn request_activation_token(&self) -> Result<AsyncRequestSerial, NotSupportedError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WindowAttributesExtStartupNotify {
|
pub trait WindowBuilderExtStartupNotify {
|
||||||
/// Use this [`ActivationToken`] during window creation.
|
/// Use this [`ActivationToken`] during window creation.
|
||||||
///
|
///
|
||||||
/// Not using such a token upon a window could make your window not gaining
|
/// Not using such a token upon a window could make your window not gaining
|
||||||
@@ -57,34 +55,29 @@ pub trait WindowAttributesExtStartupNotify {
|
|||||||
fn with_activation_token(self, token: ActivationToken) -> Self;
|
fn with_activation_token(self, token: ActivationToken) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventLoopExtStartupNotify for dyn ActiveEventLoop + '_ {
|
impl<T> EventLoopExtStartupNotify for EventLoopWindowTarget<T> {
|
||||||
fn read_token_from_env(&self) -> Option<ActivationToken> {
|
fn read_token_from_env(&self) -> Option<ActivationToken> {
|
||||||
#[cfg(x11_platform)]
|
match self.p {
|
||||||
let _is_wayland = false;
|
#[cfg(wayland_platform)]
|
||||||
#[cfg(wayland_platform)]
|
crate::platform_impl::EventLoopWindowTarget::Wayland(_) => env::var(WAYLAND_VAR),
|
||||||
let _is_wayland = self.is_wayland();
|
#[cfg(x11_platform)]
|
||||||
|
crate::platform_impl::EventLoopWindowTarget::X(_) => env::var(X11_VAR),
|
||||||
if _is_wayland {
|
|
||||||
env::var(WAYLAND_VAR).ok().map(ActivationToken::from_raw)
|
|
||||||
} else {
|
|
||||||
env::var(X11_VAR).ok().map(ActivationToken::from_raw)
|
|
||||||
}
|
}
|
||||||
|
.ok()
|
||||||
|
.map(ActivationToken::_new)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowExtStartupNotify for dyn Window + '_ {
|
impl WindowExtStartupNotify for Window {
|
||||||
fn request_activation_token(&self) -> Result<AsyncRequestSerial, RequestError> {
|
fn request_activation_token(&self) -> Result<AsyncRequestSerial, NotSupportedError> {
|
||||||
#[cfg(wayland_platform)]
|
self.window.request_activation_token()
|
||||||
if let Some(window) = self.cast_ref::<crate::platform_impl::wayland::Window>() {
|
}
|
||||||
return window.request_activation_token();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(x11_platform)]
|
impl WindowBuilderExtStartupNotify for WindowBuilder {
|
||||||
if let Some(window) = self.cast_ref::<crate::platform_impl::x11::Window>() {
|
fn with_activation_token(mut self, token: ActivationToken) -> Self {
|
||||||
return window.request_activation_token();
|
self.platform_specific.activation_token = Some(token);
|
||||||
}
|
self
|
||||||
|
|
||||||
Err(NotSupportedError::new("startup notify is not supported").into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,19 +86,14 @@ impl WindowExtStartupNotify for dyn Window + '_ {
|
|||||||
/// This is wise to do before running child processes,
|
/// This is wise to do before running child processes,
|
||||||
/// which may not to support the activation token.
|
/// which may not to support the activation token.
|
||||||
pub fn reset_activation_token_env() {
|
pub fn reset_activation_token_env() {
|
||||||
unsafe {
|
env::remove_var(X11_VAR);
|
||||||
env::remove_var(X11_VAR);
|
env::remove_var(WAYLAND_VAR);
|
||||||
env::remove_var(WAYLAND_VAR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set environment variables responsible for activation token.
|
/// Set environment variables responsible for activation token.
|
||||||
///
|
///
|
||||||
/// This could be used before running daemon processes.
|
/// This could be used before running daemon processes.
|
||||||
pub fn set_activation_token_env(token: ActivationToken) {
|
pub fn set_activation_token_env(token: ActivationToken) {
|
||||||
let token = token.into_raw();
|
env::set_var(X11_VAR, &token._token);
|
||||||
unsafe {
|
env::set_var(WAYLAND_VAR, token._token);
|
||||||
env::set_var(X11_VAR, &token);
|
|
||||||
env::set_var(WAYLAND_VAR, token);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user