- 1 year ago
Bevy / Winit - Open GUI Without Focus
To open a GUI window without giving it initial focus using Bevy and Winit, you can make use of the WindowBuilder
from the Winit library and set the visible
property to false
initially. Here's an example:
rustuse bevy::prelude::*;
use winit::window::WindowBuilder;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.run();
}
fn setup(mut windows: ResMut<Windows>) {
let window = WindowBuilder::new()
.with_title("My Window")
.with_visible(false) // Set visible to false initially
.build(&windows);
// Show the window after a delay
let mut timer = Timer::from_seconds(2.0, true);
timer.set_elapsed(2.0);
windows.get_primary_mut().unwrap().set_visible(true);
App::build()
.add_plugins(DefaultPlugins)
.add_plugin(winit::WinitPlugin::default())
.add_startup_system(setup.system())
.add_system(timer_system.system())
.add_system(show_window_system.system())
.run();
}
fn timer_system(time: Res<Time>, mut timer: ResMut<Timer>) {
timer.tick(time.delta());
}
fn show_window_system(time: Res<Time>, mut windows: ResMut<Windows>) {
if let Some(window) = windows.get_primary_mut() {
if time.seconds_since_startup() >= 2.0 && !window.visible {
window.set_visible(true);
}
}
}
In this example, we create a window using WindowBuilder
and set the visible
property to false
initially. Then, we use a Timer
to delay the visibility change of the window. After 2 seconds, we update the window's visibility to true
using the show_window_system
.
Note that this is just a basic example to demonstrate the concept. You can further customize the window settings, add more systems, and handle other events as per your requirements.
Make sure to include the necessary dependencies in your Cargo.toml
file:
toml[dependencies]
bevy = "0.5"
winit = "0.26"
You may need to adjust the versions according to the latest available versions of the Bevy and Winit crates.
By following this approach, you should be able to open a GUI window without giving it initial focus using Bevy and Winit in Rust.