Skip to content

Toggle the window's theme in the example #1

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
merged 2 commits into from
May 12, 2023
Merged
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
24 changes: 24 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ impl Window {
pub fn set_physical_cursor_position(&mut self, position: Option<DVec2>) {
self.internal.physical_cursor_position = position;
}

/// The window's current theme
///
/// ## Platform-specific
///
/// - iOS / Android / Web / x11: Unsupported.
pub fn window_theme(&self) -> Option<WindowTheme> {
self.internal.window_theme
}

/// Set the window's theme
///
/// ## Platform-specific
///
/// - iOS / Android / Web / x11: Unsupported.
pub fn set_window_theme(&mut self, theme: Option<WindowTheme>) {
self.internal.window_theme = theme;
}
}

/// The size limits on a window.
Expand Down Expand Up @@ -667,6 +685,12 @@ pub struct InternalWindowState {
maximize_request: Option<bool>,
/// Unscaled cursor position.
physical_cursor_position: Option<DVec2>,
/// The window's current theme
///
/// ## Platform-specific
///
/// - iOS / Android / Web / x11: Unsupported.
window_theme: Option<WindowTheme>,
}

impl InternalWindowState {
Expand Down
13 changes: 12 additions & 1 deletion crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use winit::{
use crate::web_resize::{CanvasParentResizeEventChannel, WINIT_CANVAS_SELECTOR};
use crate::{
accessibility::{AccessKitAdapters, WinitActionHandlers},
converters::{self, convert_window_level},
converters::{self, convert_window_level, convert_winit_theme, convert_window_theme},
get_best_videomode, get_fitting_videomode, WinitWindows,
};

Expand Down Expand Up @@ -62,6 +62,11 @@ pub(crate) fn create_window<'a>(
&mut handlers,
&mut accessibility_requested,
);

if let Some(theme) = winit_window.theme() {
window.set_window_theme(Some(convert_winit_theme(theme)));
}

window
.resolution
.set_scale_factor(winit_window.scale_factor());
Expand Down Expand Up @@ -296,6 +301,12 @@ pub(crate) fn changed_window(
));
}

if window.window_theme() != cache.window.window_theme() {
if let Some(theme) = window.window_theme() {
winit_window.set_theme(Some(convert_window_theme(theme)));
}
}

cache.window = window.clone();
}
}
Expand Down
17 changes: 16 additions & 1 deletion examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::{CursorGrabMode, PresentMode, WindowLevel},
window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme},
};

fn main() {
Expand All @@ -28,6 +28,7 @@ fn main() {
Update,
(
change_title,
toggle_theme,
toggle_cursor,
toggle_vsync,
cycle_cursor_icon,
Expand Down Expand Up @@ -93,6 +94,20 @@ fn toggle_cursor(mut windows: Query<&mut Window>, input: Res<Input<KeyCode>>) {
}
}

// This system will toggle the color theme used by the window
fn toggle_theme(mut windows: Query<&mut Window>, input: Res<Input<KeyCode>>) {
if input.just_pressed(KeyCode::F) {
let mut window = windows.single_mut();

if let Some(current_theme) = window.window_theme() {
window.set_window_theme(match current_theme {
WindowTheme::Light => Some(WindowTheme::Dark),
WindowTheme::Dark => Some(WindowTheme::Light),
});
}
}
}

/// This system cycles the cursor's icon through a small set of icons when clicking
fn cycle_cursor_icon(
mut windows: Query<&mut Window>,
Expand Down