Skip to content

Commit b4fb4d3

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 bevyengine/bevy#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 2931761 commit b4fb4d3

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
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"

src/converters.rs

Lines changed: 9 additions & 1 deletion
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+
}

src/lib.rs

Lines changed: 21 additions & 2 deletions
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>()

src/system.rs

Lines changed: 6 additions & 3 deletions
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

src/winit_windows.rs

Lines changed: 3 additions & 1 deletion
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);

0 commit comments

Comments
 (0)