Skip to content

Commit 3900b48

Browse files
committed
update winit to 0.28 (#7480)
# Objective - Update winit to 0.28 ## Solution - Small API change - A security advisory has been added for a unmaintained crate used by a dependency of winit build script for wayland I didn't do anything for Android support in this PR though it should be fixable, it should be done in a separate one, maybe #6830 --- ## Changelog - `window.always_on_top` has been removed, you can now use `window.window_level` ## Migration Guide before: ```rust app.new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { always_on_top: true, ..default() }), ..default() })); ``` after: ```rust app.new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { window_level: bevy::window::WindowLevel::AlwaysOnTop, ..default() }), ..default() })); ```
1 parent 44a572e commit 3900b48

File tree

14 files changed

+101
-47
lines changed

14 files changed

+101
-47
lines changed

crates/bevy_asset/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ fastrand = "1.7.0"
3333
notify = { version = "5.0.0", optional = true }
3434
parking_lot = "0.12.1"
3535

36+
[target.'cfg(target_os = "android")'.dependencies]
37+
bevy_winit = { path = "../bevy_winit", version = "0.9.0" }
38+
3639
[target.'cfg(target_arch = "wasm32")'.dependencies]
3740
wasm-bindgen = { version = "0.2" }
3841
web-sys = { version = "0.3", features = ["Request", "Window", "Response"] }
3942
wasm-bindgen-futures = "0.4"
4043
js-sys = "0.3"
4144

42-
[target.'cfg(target_os = "android")'.dependencies]
43-
ndk-glue = { version = "0.7" }
44-
4545
[dev-dependencies]
4646
futures-lite = "1.4.0"
4747
tempfile = "3.2.0"

crates/bevy_asset/src/io/android_asset_io.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ impl AndroidAssetIo {
3232
impl AssetIo for AndroidAssetIo {
3333
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
3434
Box::pin(async move {
35-
let asset_manager = ndk_glue::native_activity().asset_manager();
35+
let asset_manager = bevy_winit::ANDROID_APP
36+
.get()
37+
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
38+
.asset_manager();
3639
let mut opened_asset = asset_manager
3740
.open(&CString::new(path.to_str().unwrap()).unwrap())
3841
.ok_or(AssetIoError::NotFound(path.to_path_buf()))?;

crates/bevy_derive/src/bevy_main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
1010
);
1111

1212
TokenStream::from(quote! {
13-
// use ndk-glue macro to create an activity: https://github.com/rust-mobile/ndk-glue/tree/main/ndk-macro
13+
#[no_mangle]
1414
#[cfg(target_os = "android")]
15-
#[cfg_attr(target_os = "android", bevy::ndk_glue::main(backtrace = "on", ndk_glue = "bevy::ndk_glue"))]
16-
fn android_main() {
17-
main()
15+
fn android_main(android_app: bevy::winit::AndroidApp) {
16+
let _ = bevy::winit::ANDROID_APP.set(android_app);
17+
main();
1818
}
1919

2020
#[no_mangle]

crates/bevy_internal/Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,3 @@ bevy_text = { path = "../bevy_text", optional = true, version = "0.9.0" }
107107
bevy_ui = { path = "../bevy_ui", optional = true, version = "0.9.0" }
108108
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.9.0" }
109109
bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.9.0" }
110-
111-
[target.'cfg(target_os = "android")'.dependencies]
112-
# This version *must* be the same as the version used by winit,
113-
# or Android will break: https://github.com/rust-windowing/winit#android
114-
ndk-glue = {version = "0.7", features = ["logger"]}

crates/bevy_internal/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,3 @@ pub mod dynamic_plugin {
172172
//! Dynamic linking of plugins
173173
pub use bevy_dynamic_plugin::*;
174174
}
175-
176-
#[cfg(target_os = "android")]
177-
pub use ndk_glue;

crates/bevy_window/src/window.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub struct Window {
141141
/// ## Platform-specific
142142
///
143143
/// - iOS / Android / Web / Wayland: Unsupported.
144-
pub always_on_top: bool,
144+
pub window_level: WindowLevel,
145145
/// The "html canvas" element selector.
146146
///
147147
/// If set, this selector will be used to find a matching html canvas element,
@@ -205,7 +205,7 @@ impl Default for Window {
205205
decorations: true,
206206
transparent: false,
207207
focused: true,
208-
always_on_top: false,
208+
window_level: Default::default(),
209209
fit_canvas_to_parent: false,
210210
prevent_default_event_handling: true,
211211
canvas: None,
@@ -800,3 +800,30 @@ pub enum WindowMode {
800800
/// Creates a fullscreen window that uses the maximum supported size.
801801
Fullscreen,
802802
}
803+
804+
/// A window level groups windows with respect to their z-position.
805+
///
806+
/// The relative ordering between windows in different window levels is fixed.
807+
/// The z-order of a window within the same window level may change dynamically on user interaction.
808+
///
809+
/// ## Platform-specific
810+
///
811+
/// - **iOS / Android / Web / Wayland:** Unsupported.
812+
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)]
813+
#[cfg_attr(
814+
feature = "serialize",
815+
derive(serde::Serialize, serde::Deserialize),
816+
reflect(Serialize, Deserialize)
817+
)]
818+
#[reflect(Debug, PartialEq)]
819+
pub enum WindowLevel {
820+
/// The window will always be below normal windows.
821+
///
822+
/// This is useful for a widget-based app.
823+
AlwaysOnBottom,
824+
/// The default.
825+
#[default]
826+
Normal,
827+
/// The window will always be on top of normal windows.
828+
AlwaysOnTop,
829+
}

crates/bevy_winit/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" }
2323
bevy_utils = { path = "../bevy_utils", version = "0.9.0" }
2424

2525
# other
26-
winit = { version = "0.27", default-features = false }
26+
winit = { version = "0.28", default-features = false }
2727
approx = { version = "0.5", default-features = false }
2828
raw-window-handle = "0.5"
2929

30+
[target.'cfg(target_os = "android")'.dependencies]
31+
winit = { version = "0.28", default-features = false, features = ["android-native-activity"] }
32+
once_cell = "1.11"
33+
3034
[target.'cfg(target_arch = "wasm32")'.dependencies]
3135
wasm-bindgen = { version = "0.2" }
3236
web-sys = "0.3"

crates/bevy_winit/src/converters.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy_input::{
55
ButtonState,
66
};
77
use bevy_math::Vec2;
8-
use bevy_window::CursorIcon;
8+
use bevy_window::{CursorIcon, WindowLevel};
99

1010
pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> KeyboardInput {
1111
KeyboardInput {
@@ -266,3 +266,11 @@ pub fn convert_cursor_icon(cursor_icon: CursorIcon) -> winit::window::CursorIcon
266266
CursorIcon::RowResize => winit::window::CursorIcon::RowResize,
267267
}
268268
}
269+
270+
pub fn convert_window_level(window_level: WindowLevel) -> winit::window::WindowLevel {
271+
match window_level {
272+
WindowLevel::AlwaysOnBottom => winit::window::WindowLevel::AlwaysOnBottom,
273+
WindowLevel::Normal => winit::window::WindowLevel::Normal,
274+
WindowLevel::AlwaysOnTop => winit::window::WindowLevel::AlwaysOnTop,
275+
}
276+
}

crates/bevy_winit/src/lib.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,40 @@ use bevy_window::{
3131
WindowScaleFactorChanged,
3232
};
3333

34+
#[cfg(target_os = "android")]
35+
pub use winit::platform::android::activity::AndroidApp;
36+
3437
use winit::{
3538
event::{self, DeviceEvent, Event, StartCause, WindowEvent},
36-
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
39+
event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopWindowTarget},
3740
};
3841

3942
use crate::system::WinitWindowInfo;
4043
#[cfg(target_arch = "wasm32")]
4144
use crate::web_resize::{CanvasParentResizeEventChannel, CanvasParentResizePlugin};
4245

46+
#[cfg(target_os = "android")]
47+
pub static ANDROID_APP: once_cell::sync::OnceCell<AndroidApp> = once_cell::sync::OnceCell::new();
48+
4349
#[derive(Default)]
4450
pub struct WinitPlugin;
4551

4652
impl Plugin for WinitPlugin {
4753
fn build(&self, app: &mut App) {
48-
let event_loop = EventLoop::new();
54+
let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event();
55+
56+
#[cfg(target_os = "android")]
57+
{
58+
use winit::platform::android::EventLoopBuilderExtAndroid;
59+
event_loop_builder.with_android_app(
60+
ANDROID_APP
61+
.get()
62+
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
63+
.clone(),
64+
);
65+
}
66+
67+
let event_loop = event_loop_builder.build();
4968
app.insert_non_send_resource(event_loop);
5069

5170
app.init_non_send_resource::<WinitWindows>()

crates/bevy_winit/src/system.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ use winit::{
1919

2020
#[cfg(target_arch = "wasm32")]
2121
use crate::web_resize::{CanvasParentResizeEventChannel, WINIT_CANVAS_SELECTOR};
22-
use crate::{converters, get_best_videomode, get_fitting_videomode, WinitWindows};
22+
use crate::{
23+
converters::{self, convert_window_level},
24+
get_best_videomode, get_fitting_videomode, WinitWindows,
25+
};
2326
#[cfg(target_arch = "wasm32")]
2427
use bevy_ecs::system::ResMut;
2528

@@ -262,8 +265,8 @@ pub(crate) fn changed_window(
262265
winit_window.focus_window();
263266
}
264267

265-
if window.always_on_top != previous.always_on_top {
266-
winit_window.set_always_on_top(window.always_on_top);
268+
if window.window_level != previous.window_level {
269+
winit_window.set_window_level(convert_window_level(window.window_level));
267270
}
268271

269272
// Currently unsupported changes

crates/bevy_winit/src/winit_windows.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use winit::{
88
monitor::MonitorHandle,
99
};
1010

11+
use crate::converters::convert_window_level;
12+
1113
#[derive(Debug, Default)]
1214
pub struct WinitWindows {
1315
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
@@ -65,7 +67,7 @@ impl WinitWindows {
6567
};
6668

6769
winit_window_builder = winit_window_builder
68-
.with_always_on_top(window.always_on_top)
70+
.with_window_level(convert_window_level(window.window_level))
6971
.with_resizable(window.resizable)
7072
.with_decorations(window.decorations)
7173
.with_transparent(window.transparent);

deny.toml

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ yanked = "deny"
77
notice = "deny"
88
ignore = [
99
"RUSTSEC-2020-0056", # from cpal v0.14.1 - unmaintained - https://github.com/koute/stdweb/issues/403
10+
"RUSTSEC-2022-0048", # from xml-rs 0.8.4 - unmaintained - it's used in a build script of winit
1011
]
1112

1213
[licenses]
@@ -36,16 +37,11 @@ highlight = "all"
3637
skip = [
3738
{ name = "ndk-sys", version = "0.3" }, # from rodio v0.16.0
3839
{ name = "ndk", version = "0.6" }, # from rodio v0.16.0
39-
{ name = "raw-window-handle", version = "0.4" }, # from winit v0.27.4
4040
{ name = "nix", version = "0.23" }, # from cpal v0.14.1
41+
{ name = "redox_syscall", version = "0.2" }, # from notify v5.1.0
4142
{ name = "rustc_version", version = "0.2" }, # from postcard v1.0.2
4243
{ name = "semver", version = "0.9" }, # from postcard v1.0.2
43-
{ name = "windows_aarch64_msvc", version = "0.36" }, # from notify v5.0.0
44-
{ name = "windows_i686_gnu", version = "0.36" }, # from notify v5.0.0
45-
{ name = "windows_i686_msvc", version = "0.36" }, # from notify v5.0.0
46-
{ name = "windows_x86_64_gnu", version = "0.36" }, # from notify v5.0.0
47-
{ name = "windows_x86_64_msvc", version = "0.36" }, # from notify v5.0.0
48-
{ name = "windows-sys", version = "0.36" }, # from notify v5.0.0
44+
{ name = "windows-sys", version = "0.42" }, # from notify v5.1.0
4945
{ name = "windows", version = "0.37" }, # from rodio v0.16.0
5046
{ name = "windows_aarch64_msvc", version = "0.37" }, # from rodio v0.16.0
5147
{ name = "windows_i686_gnu", version = "0.37" }, # from rodio v0.16.0

examples/ui/window_fallthrough.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
// Set the window's parameters, note we're setting the window to always be on top.
1212
transparent: true,
1313
decorations: true,
14-
always_on_top: true,
14+
window_level: bevy::window::WindowLevel::AlwaysOnTop,
1515
..default()
1616
}),
1717
..default()

examples/window/window_settings.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use bevy::{
55
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
66
prelude::*,
7-
window::{CursorGrabMode, PresentMode},
7+
window::{CursorGrabMode, PresentMode, WindowLevel},
88
};
99

1010
fn main() {
@@ -28,7 +28,7 @@ fn main() {
2828
.add_system(toggle_cursor)
2929
.add_system(toggle_vsync)
3030
.add_system(cycle_cursor_icon)
31-
.add_system(toggle_always_on_top)
31+
.add_system(switch_level)
3232
.run();
3333
}
3434

@@ -47,23 +47,23 @@ fn toggle_vsync(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
4747
}
4848
}
4949

50-
/// This system toggles whether the window is always on top when pressing the T button
51-
/// You'll notice it won't be covered by other windows.
50+
/// This system switches the window level when pressing the T button
51+
/// You'll notice it won't be covered by other windows, or will be covered by all the other
52+
/// windows depending on the level.
5253
///
5354
/// This feature only works on some platforms. Please check the
54-
/// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.WindowDescriptor.html#structfield.always_on_top)
55+
/// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.window_level)
5556
/// for more details.
56-
fn toggle_always_on_top(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
57+
fn switch_level(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
5758
if input.just_pressed(KeyCode::T) {
5859
let mut window = windows.single_mut();
5960

60-
window.always_on_top = !window.always_on_top;
61-
62-
if window.always_on_top {
63-
info!("LOCKING WINDOW ON TOP");
64-
} else {
65-
info!("UNLOCKING WINDOW");
66-
}
61+
window.window_level = match window.window_level {
62+
WindowLevel::AlwaysOnBottom => WindowLevel::Normal,
63+
WindowLevel::Normal => WindowLevel::AlwaysOnTop,
64+
WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnBottom,
65+
};
66+
info!("WINDOW_LEVEL: {:?}", window.window_level);
6767
}
6868
}
6969

0 commit comments

Comments
 (0)