From 7745bba10eb02bced17692036d3e95b6c43d1f39 Mon Sep 17 00:00:00 2001 From: futile Date: Sat, 10 May 2025 06:44:58 +0100 Subject: [PATCH] improve styling in relation to windows theme --- src/gui/app.rs | 13 ++++++++++++- src/gui/run.rs | 6 ++++-- src/gui/settings.rs | 12 +++++++++++- src/util/win.rs | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/gui/app.rs b/src/gui/app.rs index dff2ce1..8cd54fb 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -1,6 +1,10 @@ use crate::{ features, - gui::{settings::Settings, tools, ui_ext::UiExt}, + gui::{ + settings::{Settings, Theme}, + tools, + ui_ext::UiExt, + }, util::{consts::game::WINDOW_TITLE, meta::Meta, persistent_state::PersistentState, win}, }; use eframe::egui; @@ -201,6 +205,13 @@ impl App { if selection != self.settings.theme { catppuccin_egui::set_theme(ctx, self.settings.theme.into()); } + if ui + .add_visible(self.settings.theme == Theme::Auto, egui::Button::new("⟲")) + .on_hover_text("Refresh theme") + .clicked() + { + catppuccin_egui::set_theme(ctx, self.settings.theme.into()); + } }); ui.checkbox(&mut self.settings.start_elevated, "Always start elevated"); } diff --git a/src/gui/run.rs b/src/gui/run.rs index c1e5a75..52deab8 100644 --- a/src/gui/run.rs +++ b/src/gui/run.rs @@ -50,10 +50,12 @@ fn app_creator( app.sysinfo.refresh_all(); // enable image loading support in egui egui_extras::install_image_loaders(&cc.egui_ctx); - // set our initial theme, from earlier loaded settings + // set our initial theme, from earlier loaded settings. we set the egui theme + // to dark here to work around system theme based switching of the egui style + cc.egui_ctx.set_theme(egui::Theme::Dark); catppuccin_egui::set_theme(&cc.egui_ctx, app.settings.theme.into()); // apply some global styling that we like - cc.egui_ctx.style_mut(|style| { + cc.egui_ctx.all_styles_mut(|style| { style.spacing.item_spacing = egui::vec2(4.0, 4.0); style.interaction.selectable_labels = false; }); diff --git a/src/gui/settings.rs b/src/gui/settings.rs index c4f3fe2..2123f30 100644 --- a/src/gui/settings.rs +++ b/src/gui/settings.rs @@ -1,8 +1,11 @@ +use crate::util::win; use serde::{Deserialize, Serialize}; use strum::{Display, EnumIter}; #[derive(Clone, Copy, Debug, Display, PartialEq, Eq, Serialize, Deserialize, EnumIter)] pub enum Theme { + #[strum(to_string = "Auto")] + Auto, #[strum(to_string = "Catppuccin Latte")] CatppuccinLatte, #[strum(to_string = "Catppuccin Frappe")] @@ -16,6 +19,13 @@ pub enum Theme { impl From for catppuccin_egui::Theme { fn from(val: Theme) -> Self { match val { + Theme::Auto => { + if win::is_system_theme_dark() { + catppuccin_egui::MOCHA + } else { + catppuccin_egui::LATTE + } + } Theme::CatppuccinLatte => catppuccin_egui::LATTE, Theme::CatppuccinFrappe => catppuccin_egui::FRAPPE, Theme::CatppuccinMacchiato => catppuccin_egui::MACCHIATO, @@ -33,7 +43,7 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { - theme: Theme::CatppuccinMocha, + theme: Theme::Auto, start_elevated: false, } } diff --git a/src/util/win.rs b/src/util/win.rs index 3e907d6..67e4f6a 100644 --- a/src/util/win.rs +++ b/src/util/win.rs @@ -83,3 +83,17 @@ pub fn is_elevated() -> bool { result.is_ok() && elevation.TokenIsElevated != 0 } } + +pub fn is_system_theme_dark() -> bool { + use winreg::RegKey; + let hkcu = RegKey::predef(winreg::enums::HKEY_CURRENT_USER); + let Ok(subkey) = + hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize") + else { + return true; + }; + let Ok(dword): Result = subkey.get_value("AppsUseLightTheme") else { + return true; + }; + dword != 1 +}