Skip to content

Commit

Permalink
user controlled window visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Aug 4, 2023
1 parent 4209819 commit b475ad8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Expand Up @@ -2017,6 +2017,16 @@ description = "Creates a solid color window"
category = "Window"
wasm = true

[[example]]
name = "invisible_window"
path = "examples/window/invisible_window.rs"

[package.metadata.example.invisible_window]
name = "Invisible Window"
description = "Demonstrates how to spawn an invisible window"
category = "Window"
wasm = false

[[example]]
name = "low_power"
path = "examples/window/low_power.rs"
Expand Down
11 changes: 10 additions & 1 deletion crates/bevy_window/src/window.rs
Expand Up @@ -213,6 +213,14 @@ pub struct Window {
///
/// - iOS / Android / Web: Unsupported.
pub window_theme: Option<WindowTheme>,
/// Sets the window's visibility.
///
/// If `false`, this will hide the window. If `true`, this will show the window.
///
/// ## Platform-specific
///
/// - **Android / Wayland / Web:** Unsupported.
pub visible: bool,
}

impl Default for Window {
Expand All @@ -239,6 +247,7 @@ impl Default for Window {
prevent_default_event_handling: true,
canvas: None,
window_theme: None,
visible: true,
}
}
}
Expand Down Expand Up @@ -1016,7 +1025,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
Expand Up @@ -314,6 +314,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
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/winit_windows.rs
Expand Up @@ -167,7 +167,7 @@ impl WinitWindows {
);
adapters.insert(entity, adapter);
handlers.insert(entity, handler);
winit_window.set_visible(true);
winit_window.set_visible(window.visible);

// 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
1 change: 1 addition & 0 deletions examples/README.md
Expand Up @@ -365,6 +365,7 @@ Example | Description
Example | Description
--- | ---
[Clear Color](../examples/window/clear_color.rs) | Creates a solid color window
[Invisible Window](../examples/window/invisible_window.rs) | Demonstrates how to spawn an invisible window
[Low Power](../examples/window/low_power.rs) | Demonstrates settings to reduce power use for bevy applications
[Multiple Windows](../examples/window/multiple_windows.rs) | Demonstrates creating multiple windows, and rendering to them
[Scale Factor Override](../examples/window/scale_factor_override.rs) | Illustrates how to customize the default window settings
Expand Down
26 changes: 26 additions & 0 deletions examples/window/invisible_window.rs
@@ -0,0 +1,26 @@
//! This shows how to spawn an invisible window and turn it visible once it's ready to display.
//! This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app.

use bevy::prelude::*;
use bevy_internal::window::PrimaryWindow;

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
// This will spawn an invisible window
visible: false,
..default()
}),
..default()
}))
.add_systems(Startup, setup)
.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;
}

0 comments on commit b475ad8

Please sign in to comment.