Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Added transparency to window builder #3105

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ path = "examples/window/multiple_windows.rs"
name = "scale_factor_override"
path = "examples/window/scale_factor_override.rs"

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

[[example]]
name = "window_settings"
path = "examples/window/window_settings.rs"
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,13 @@ pub struct WindowDescriptor {
pub cursor_visible: bool,
pub cursor_locked: bool,
pub mode: WindowMode,
/// Sets whether the background of the window should be transparent.
/// # Platform-specific
/// - iOS / Android / Web: Unsupported.
/// - OSX / Linux : Not working as expected.
/// OSX transparent works with winit out of the box, so this issue might be related to: https://github.com/gfx-rs/wgpu/issues/687
/// Linux now works with this pr merged in, which should work with the next release of winit : https://github.com/rust-windowing/winit/pull/2006
pub transparent: bool,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
}
Expand All @@ -554,6 +561,7 @@ impl Default for WindowDescriptor {
cursor_locked: false,
cursor_visible: true,
mode: WindowMode::Windowed,
transparent: false,
#[cfg(target_arch = "wasm32")]
canvas: None,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ impl WinitWindows {
}
}
.with_resizable(window_descriptor.resizable)
.with_decorations(window_descriptor.decorations),
.with_decorations(window_descriptor.decorations)
.with_transparent(window_descriptor.transparent),
};

let constraints = window_descriptor.resize_constraints.check_constraints();
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ Example | File | Description
`clear_color` | [`window/clear_color.rs`](./window/clear_color.rs) | Creates a solid color window
`multiple_windows` | [`window/multiple_windows.rs`](./window/multiple_windows.rs) | Creates two windows and cameras viewing the same mesh
`scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings
`transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration
`window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings

# Platform-Specific Examples
Expand Down
32 changes: 32 additions & 0 deletions examples/window/transparent_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// An example of how to display a window in transparent mode
/// [Documentation & Platform support.](https://docs.rs/bevy/latest/bevy/prelude/struct.WindowDescriptor.html#structfield.transparent)
use bevy::{prelude::*, render::pass::ClearColor, window::WindowDescriptor};

fn main() {
App::new()
// ClearColor must have 0 alpha, otherwise some color will bleed through
.insert_resource(ClearColor(Color::NONE))
.insert_resource(WindowDescriptor {
// Setting `transparent` allows the `ClearColor`'s alpha value to take effect
transparent: true,
// Disabling window decorations to make it feel more like a widget than a window
decorations: false,
..Default::default()
})
.add_startup_system(setup)
.add_plugins(DefaultPlugins)
.run();
}

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let texture_handle = asset_server.load("branding/icon.png");
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(SpriteBundle {
material: materials.add(texture_handle.into()),
..Default::default()
});
}