Skip to content

Commit

Permalink
update winit to 0.28 (#7480)
Browse files Browse the repository at this point in the history
# 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()
        }));
```
  • Loading branch information
mockersf committed Feb 3, 2023
1 parent 2931761 commit b4fb4d3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" }
bevy_utils = { path = "../bevy_utils", version = "0.9.0" }

# other
winit = { version = "0.27", default-features = false }
winit = { version = "0.28", default-features = false }
approx = { version = "0.5", default-features = false }
raw-window-handle = "0.5"

[target.'cfg(target_os = "android")'.dependencies]
winit = { version = "0.28", default-features = false, features = ["android-native-activity"] }
once_cell = "1.11"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = "0.3"
Expand Down
10 changes: 9 additions & 1 deletion src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_input::{
ButtonState,
};
use bevy_math::Vec2;
use bevy_window::CursorIcon;
use bevy_window::{CursorIcon, WindowLevel};

pub fn convert_keyboard_input(keyboard_input: &winit::event::KeyboardInput) -> KeyboardInput {
KeyboardInput {
Expand Down Expand Up @@ -266,3 +266,11 @@ pub fn convert_cursor_icon(cursor_icon: CursorIcon) -> winit::window::CursorIcon
CursorIcon::RowResize => winit::window::CursorIcon::RowResize,
}
}

pub fn convert_window_level(window_level: WindowLevel) -> winit::window::WindowLevel {
match window_level {
WindowLevel::AlwaysOnBottom => winit::window::WindowLevel::AlwaysOnBottom,
WindowLevel::Normal => winit::window::WindowLevel::Normal,
WindowLevel::AlwaysOnTop => winit::window::WindowLevel::AlwaysOnTop,
}
}
23 changes: 21 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,40 @@ use bevy_window::{
WindowScaleFactorChanged,
};

#[cfg(target_os = "android")]
pub use winit::platform::android::activity::AndroidApp;

use winit::{
event::{self, DeviceEvent, Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopWindowTarget},
};

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

#[cfg(target_os = "android")]
pub static ANDROID_APP: once_cell::sync::OnceCell<AndroidApp> = once_cell::sync::OnceCell::new();

#[derive(Default)]
pub struct WinitPlugin;

impl Plugin for WinitPlugin {
fn build(&self, app: &mut App) {
let event_loop = EventLoop::new();
let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event();

#[cfg(target_os = "android")]
{
use winit::platform::android::EventLoopBuilderExtAndroid;
event_loop_builder.with_android_app(
ANDROID_APP
.get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android")
.clone(),
);
}

let event_loop = event_loop_builder.build();
app.insert_non_send_resource(event_loop);

app.init_non_send_resource::<WinitWindows>()
Expand Down
9 changes: 6 additions & 3 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use winit::{

#[cfg(target_arch = "wasm32")]
use crate::web_resize::{CanvasParentResizeEventChannel, WINIT_CANVAS_SELECTOR};
use crate::{converters, get_best_videomode, get_fitting_videomode, WinitWindows};
use crate::{
converters::{self, convert_window_level},
get_best_videomode, get_fitting_videomode, WinitWindows,
};
#[cfg(target_arch = "wasm32")]
use bevy_ecs::system::ResMut;

Expand Down Expand Up @@ -262,8 +265,8 @@ pub(crate) fn changed_window(
winit_window.focus_window();
}

if window.always_on_top != previous.always_on_top {
winit_window.set_always_on_top(window.always_on_top);
if window.window_level != previous.window_level {
winit_window.set_window_level(convert_window_level(window.window_level));
}

// Currently unsupported changes
Expand Down
4 changes: 3 additions & 1 deletion src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use winit::{
monitor::MonitorHandle,
};

use crate::converters::convert_window_level;

#[derive(Debug, Default)]
pub struct WinitWindows {
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
Expand Down Expand Up @@ -65,7 +67,7 @@ impl WinitWindows {
};

winit_window_builder = winit_window_builder
.with_always_on_top(window.always_on_top)
.with_window_level(convert_window_level(window.window_level))
.with_resizable(window.resizable)
.with_decorations(window.decorations)
.with_transparent(window.transparent);
Expand Down

0 comments on commit b4fb4d3

Please sign in to comment.