From 818a8fe154697a22bf1abd9a9d45990984229860 Mon Sep 17 00:00:00 2001 From: Benjamin Brienen Date: Mon, 26 May 2025 22:05:33 +0200 Subject: [PATCH] Allow unfocused window creation (#19237) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective Allow creating a new window without it being focused, when `Window`'s `focused` is `false. ## Solution Use `winit`'s `WindowBuilder`'s `with_active` method ## Notes - `winit`'s doc lists [redox's `Orbital`](https://gitlab.redox-os.org/redox-os/orbital) as an unsupported platform, but since Bevy doesn't officially support this platform, I didn't put it in the documentation. - I only tested on Linux, which is an unsupported platform. I can give you a test code if you want to test on another platform. - I initially put a line [here](https://github.com/bevyengine/bevy/blob/v0.11.0/crates/bevy_winit/src/system.rs#L72) to set the Bevy `Window`'s `focused` to `winit_window.has_focus()` after window creation to avoid the case where `with_active` is not supported, the window is spawned focused, no `WindowFocused` event is triggered, and Bevy `Window` would be desynced from winit's window. But after testing on Linux (which doesn't support `with_active`) it seems like at that point `has_focus` returns `false` and the event is triggered, so I removed it. Do you think I should add it back to be safe? ## Changelog - A new unfocused `Window` can be created by setting `focused` to `false`. ## Migration Guide - If a `Window` is spawned with `focused` set to `false`, it will now start not focused on supported platforms. Adopted from #9208 --------- Co-authored-by: Sélène Amanita Co-authored-by: Sélène Amanita <134181069+Selene-Amanita@users.noreply.github.com> Co-authored-by: atlv Co-authored-by: Alice Cecile --- crates/bevy_window/src/window.rs | 9 +++++++++ crates/bevy_winit/src/winit_windows.rs | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 31ff212ebe..4f7d7b2f7b 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -225,6 +225,15 @@ pub struct Window { /// You should also set the window `composite_alpha_mode` to `CompositeAlphaMode::PostMultiplied`. pub transparent: bool, /// Get/set whether the window is focused. + /// + /// It cannot be set unfocused after creation. + /// + /// ## Platform-specific + /// + /// - iOS / Android / X11 / Wayland: Spawning unfocused is + /// [not supported](https://docs.rs/winit/latest/winit/window/struct.WindowAttributes.html#method.with_active). + /// - iOS / Android / Web / Wayland: Setting focused after creation is + /// [not supported](https://docs.rs/winit/latest/winit/window/struct.Window.html#method.focus_window). pub focused: bool, /// Where should the window appear relative to other overlapping window. /// diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index d666491311..8bf326f453 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -129,7 +129,8 @@ impl WinitWindows { .with_resizable(window.resizable) .with_enabled_buttons(convert_enabled_buttons(window.enabled_buttons)) .with_decorations(window.decorations) - .with_transparent(window.transparent); + .with_transparent(window.transparent) + .with_active(window.focused); #[cfg(target_os = "windows")] {