From 992d17bc7f3a362cd7521b096fe969939e5bbe99 Mon Sep 17 00:00:00 2001 From: aecsocket <43144841+aecsocket@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:11:21 +0100 Subject: [PATCH] Add `bevy_window::Window` options for MacOS (#15820) # Objective MacOS has some nice options for controlling the window and titlebar to make the content appear much more "immersively" in the window. This PR exposes options for controlling this. ## Solution Adds new fields to `Window` to control these, with doc comments to explain what they do and that they're MacOS only. ## Testing Tested on a MacOS machine (not my own, I don't have one). That's where the below screenshots were taken. --- ## Showcase On MacOS, you now have more options for configuring the window titlebar. You can, for example, make the title bar transparent and only show the window controls. This provides a more "immersive" experience for your rendered content. Before, only this was possible: image Now, you can create windows like this: image2 This uses the following `bevy_window::Window` settings: ```rs fullsize_content_view: true, titlebar_transparent: true, titlebar_show_title: false, ``` ## Migration Guide `bevy_window::Window` now has extra fields for configuring MacOS window settings: ```rs pub movable_by_window_background: bool, pub fullsize_content_view: bool, pub has_shadow: bool, pub titlebar_shown: bool, pub titlebar_transparent: bool, pub titlebar_show_title: bool, pub titlebar_show_buttons: bool, ``` Using `Window::default` keeps the same behaviour as before. --- crates/bevy_window/src/window.rs | 81 ++++++++++++++++++++++++++ crates/bevy_winit/src/winit_windows.rs | 13 +++++ 2 files changed, 94 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 34781501e6..cc21e6ab8c 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -314,6 +314,80 @@ pub struct Window { /// /// - Only used on iOS. pub recognize_pan_gesture: Option<(u8, u8)>, + /// Enables click-and-drag behavior for the entire window, not just the titlebar. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_movable_by_window_background`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_movable_by_window_background`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_movable_by_window_background + pub movable_by_window_background: bool, + /// Makes the window content appear behind the titlebar. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_fullsize_content_view`]. + /// + /// For apps which want to render the window buttons on top of the apps + /// itself, this should be enabled along with [`titlebar_transparent`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_fullsize_content_view`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_fullsize_content_view + /// [`titlebar_transparent`]: Self::titlebar_transparent + pub fullsize_content_view: bool, + /// Toggles drawing the drop shadow behind the window. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_has_shadow`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_has_shadow`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_has_shadow + pub has_shadow: bool, + /// Toggles drawing the titlebar. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_titlebar_hidden`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_titlebar_hidden`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_titlebar_hidden + pub titlebar_shown: bool, + /// Makes the titlebar transparent, allowing the app content to appear behind it. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_titlebar_transparent`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_titlebar_transparent`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_titlebar_transparent + pub titlebar_transparent: bool, + /// Toggles showing the window title. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_title_hidden`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_title_hidden`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_title_hidden + pub titlebar_show_title: bool, + /// Toggles showing the traffic light window buttons. + /// + /// Corresponds to [`WindowAttributesExtMacOS::with_titlebar_buttons_hidden`]. + /// + /// # Platform-specific + /// + /// - Only used on macOS. + /// + /// [`WindowAttributesExtMacOS::with_titlebar_buttons_hidden`]: https://docs.rs/winit/latest/x86_64-apple-darwin/winit/platform/macos/trait.WindowAttributesExtMacOS.html#tymethod.with_titlebar_buttons_hidden + pub titlebar_show_buttons: bool, } impl Default for Window { @@ -348,6 +422,13 @@ impl Default for Window { recognize_rotation_gesture: false, recognize_doubletap_gesture: false, recognize_pan_gesture: None, + movable_by_window_background: false, + fullsize_content_view: false, + has_shadow: true, + titlebar_shown: true, + titlebar_transparent: false, + titlebar_show_title: true, + titlebar_show_buttons: true, } } } diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index bfefae0c87..7023fb211a 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -127,6 +127,19 @@ impl WinitWindows { winit_window_attributes.with_skip_taskbar(window.skip_taskbar); } + #[cfg(target_os = "macos")] + { + use winit::platform::macos::WindowAttributesExtMacOS; + winit_window_attributes = winit_window_attributes + .with_movable_by_window_background(window.movable_by_window_background) + .with_fullsize_content_view(window.fullsize_content_view) + .with_has_shadow(window.has_shadow) + .with_titlebar_hidden(!window.titlebar_shown) + .with_titlebar_transparent(window.titlebar_transparent) + .with_title_hidden(!window.titlebar_show_title) + .with_titlebar_buttons_hidden(!window.titlebar_show_buttons); + } + let display_info = DisplayInfo { window_physical_resolution: ( window.resolution.physical_width(),