Skip to content

Commit

Permalink
User controlled window visibility (#9355)
Browse files Browse the repository at this point in the history
# Objective

- When spawning a window, it will be white until the GPU is ready to
draw the app. To avoid this, we can make the window invisible and then
make it visible once the gpu is ready. Unfortunately, the visible flag
is not available to users.

## Solution

- Let users change the visible flag

## Notes

This is only user controlled. It would be nice if it was done
automatically by bevy instead but I want to keep this PR simple.
  • Loading branch information
IceSentry committed Aug 20, 2023
1 parent 02ac5c4 commit 49bbbe0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
13 changes: 12 additions & 1 deletion crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ pub struct Window {
///
/// - iOS / Android / Web: Unsupported.
pub window_theme: Option<WindowTheme>,
/// Sets the window's visibility.
///
/// If `false`, this will hide the window the window completely, it won't appear on the screen or in the task bar.
/// If `true`, this will show the window.
/// Note that this doesn't change its focused or minimized state.
///
/// ## Platform-specific
///
/// - **Android / Wayland / Web:** Unsupported.
pub visible: bool,
}

impl Default for Window {
Expand All @@ -239,6 +249,7 @@ impl Default for Window {
prevent_default_event_handling: true,
canvas: None,
window_theme: None,
visible: true,
}
}
}
Expand Down Expand Up @@ -1016,7 +1027,7 @@ pub enum WindowTheme {
///
/// ## Platform-specific
///
/// **`iOS`**, **`Android`**, and the **`Web`** do not have window control buttons.
/// **`iOS`**, **`Android`**, and the **`Web`** do not have window control buttons.
///
/// On some **`Linux`** environments these values have no effect.
#[derive(Debug, Copy, Clone, PartialEq, Reflect)]
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ pub(crate) fn changed_windows(
winit_window.set_theme(window.window_theme.map(convert_window_theme));
}

if window.visible != cache.window.visible {
winit_window.set_visible(window.visible);
}

cache.window = window.clone();
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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_visible(window.visible);

let constraints = window.resize_constraints.check_constraints();
let min_inner_size = LogicalSize {
Expand Down Expand Up @@ -167,7 +168,6 @@ impl WinitWindows {
);
adapters.insert(entity, adapter);
handlers.insert(entity, handler);
winit_window.set_visible(true);

// Do not set the grab mode on window creation if it's none. It can fail on mobile.
if window.cursor.grab_mode != CursorGrabMode::None {
Expand Down
13 changes: 13 additions & 0 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::{
prelude::*,
window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme},
};
use bevy_internal::window::PrimaryWindow;

fn main() {
App::new()
Expand All @@ -24,13 +25,18 @@ fn main() {
maximize: false,
..Default::default()
},
// This will spawn an invisible window
// The window will be made visible in the setup() function
// This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app.
visible: false,
..default()
}),
..default()
}),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
))
.add_systems(Startup, setup)
.add_systems(
Update,
(
Expand All @@ -46,6 +52,13 @@ fn main() {
.run();
}

fn setup(mut primary_window: Query<&mut Window, With<PrimaryWindow>>) {
// At this point the gpu is ready to show the app so we can make the window visible
// There might still be a white frame when doing it in startup.
// Alternatively, you could have a system that waits a few seconds before making the window visible.
primary_window.single_mut().visible = true;
}

/// This system toggles the vsync mode when pressing the button V.
/// You'll see fps increase displayed in the console.
fn toggle_vsync(input: Res<Input<KeyCode>>, mut windows: Query<&mut Window>) {
Expand Down

0 comments on commit 49bbbe0

Please sign in to comment.