//! Illustrates how to change window settings and shows how to affect //! the mouse pointer in various ways. #[cfg(feature = "custom_cursor")] use bevy::winit::cursor::{CustomCursor, CustomCursorImage}; use bevy::{ diagnostic::{FrameCount, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, window::{ CursorGrabMode, CursorOptions, PresentMode, PrimaryWindow, SystemCursorIcon, WindowLevel, WindowTheme, }, winit::cursor::CursorIcon, }; fn main() { App::new() .add_plugins(( DefaultPlugins, LogDiagnosticsPlugin::default(), FrameTimeDiagnosticsPlugin::default(), )) .add_observer(configure_window) .add_systems(Startup, init_cursor_icons) .add_systems( Update, ( change_title, toggle_theme, toggle_cursor, toggle_vsync, toggle_window_controls, cycle_cursor_icon, switch_level, make_visible, ), ) .run(); } fn configure_window( trigger: On, mut window: Query<(&mut Window, &mut PresentMode)>, ) { let (mut window, mut present_mode) = window.get_mut(trigger.target()).unwrap(); window.title = "I am a window!".into(); window.name = Some("bevy.app".into()); window.resolution = (500., 300.).into(); // Tells Wasm to resize the window according to the available canvas window.fit_canvas_to_parent = true; // Tells Wasm not to override default event handling, like F5, Ctrl+R etc. window.prevent_default_event_handling = false; window.window_theme = Some(WindowTheme::Dark); window.enabled_buttons.maximize = false; // This will spawn an invisible window // The window will be made visible in the make_visible() system after 3 frames. // This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app. window.visible = false; *present_mode = PresentMode::AutoNoVsync; } fn make_visible(mut window: Single<&mut Window>, frames: Res) { // The delay may be different for your app or system. if frames.0 == 3 { // At this point the gpu is ready to show the app so we can make the window visible. // Alternatively, you could toggle the visibility in Startup. // It will work, but it will have one white frame before it starts rendering window.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>, mut present_mode: Single<&mut PresentMode>) { if input.just_pressed(KeyCode::KeyV) { **present_mode = if matches!(**present_mode, PresentMode::AutoVsync) { PresentMode::AutoNoVsync } else { PresentMode::AutoVsync }; info!("PRESENT_MODE: {:?}", *present_mode); } } /// This system switches the window level when pressing the T button /// You'll notice it won't be covered by other windows, or will be covered by all the other /// windows depending on the level. /// /// This feature only works on some platforms. Please check the /// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.window_level) /// for more details. fn switch_level(input: Res>, mut window: Single<&mut Window>) { if input.just_pressed(KeyCode::KeyT) { window.window_level = match window.window_level { WindowLevel::AlwaysOnBottom => WindowLevel::Normal, WindowLevel::Normal => WindowLevel::AlwaysOnTop, WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnBottom, }; info!("WINDOW_LEVEL: {:?}", window.window_level); } } /// This system toggles the window controls when pressing buttons 1, 2 and 3 /// /// This feature only works on some platforms. Please check the /// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.enabled_buttons) /// for more details. fn toggle_window_controls(input: Res>, mut window: Single<&mut Window>) { let toggle_minimize = input.just_pressed(KeyCode::Digit1); let toggle_maximize = input.just_pressed(KeyCode::Digit2); let toggle_close = input.just_pressed(KeyCode::Digit3); if toggle_minimize || toggle_maximize || toggle_close { if toggle_minimize { window.enabled_buttons.minimize = !window.enabled_buttons.minimize; } if toggle_maximize { window.enabled_buttons.maximize = !window.enabled_buttons.maximize; } if toggle_close { window.enabled_buttons.close = !window.enabled_buttons.close; } } } /// This system will then change the title during execution fn change_title(mut window: Single<&mut Window>, time: Res