From 9e2743472d08a7709063d11aabe7d80a689fb3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tau=20G=C3=A4rtli?= Date: Wed, 17 Jul 2024 17:16:10 +0200 Subject: [PATCH] WIP --- crates/egui/src/memory.rs | 8 +++-- crates/egui/src/memory/theme.rs | 61 +++++++++++++++++++++++++++++++++ crates/egui/src/style.rs | 2 -- crates/egui/src/widgets/mod.rs | 14 ++++---- scripts/build_demo_web.sh | 1 - 5 files changed, 73 insertions(+), 13 deletions(-) diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index 39c5b9110b39..91ab0e12ed25 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -304,7 +304,7 @@ impl Options { let Self { dark_style, // covered above light_style, - theme_preference: _, + theme_preference, fallback_theme: _, zoom_factor: _, // TODO(emilk) zoom_with_keyboard, @@ -343,10 +343,12 @@ impl Options { CollapsingHeader::new("πŸŽ‘ Style") .default_open(true) .show(ui, |ui| { - CollapsingHeader::new("πŸŒ™ Dark") + theme_preference.radio_buttons(ui); + + CollapsingHeader::new("πŸŒ™ Dark Style") .default_open(ui.ctx().theme() == Theme::Dark) .show(ui, |ui| std::sync::Arc::make_mut(dark_style).ui(ui)); - CollapsingHeader::new("β˜€ Light") + CollapsingHeader::new("β˜€ Light Style") .default_open(ui.ctx().theme() == Theme::Light) .show(ui, |ui| std::sync::Arc::make_mut(light_style).ui(ui)); }); diff --git a/crates/egui/src/memory/theme.rs b/crates/egui/src/memory/theme.rs index 640979e99daa..22d5ef14e3fb 100644 --- a/crates/egui/src/memory/theme.rs +++ b/crates/egui/src/memory/theme.rs @@ -1,3 +1,5 @@ +use crate::{Button, ComboBox, InnerResponse, Response}; + /// Dark or Light theme. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] @@ -44,3 +46,62 @@ impl From for ThemePreference { } } } + +impl ThemePreference { + /// Shows a combo box to switch between dark, light and follow system. + pub fn small_combo_box(&mut self, ui: &mut crate::Ui) -> Response { + let InnerResponse { + inner: changed, + mut response, + } = ComboBox::from_id_source("Theme Preference") + .selected_text(icon(*self)) + .width(0.0) + .show_ui(ui, |ui| self.radio_buttons_impl(ui)); + if changed.unwrap_or_default() { + response.mark_changed(); + } + response + } + + /// Shows radio buttons to switch between dark, light and follow system. + pub fn radio_buttons(&mut self, ui: &mut crate::Ui) -> Response { + let InnerResponse { + inner: changed, + mut response, + } = ui.horizontal(|ui| self.radio_buttons_impl(ui)); + if changed { + response.mark_changed(); + } + response + } + + fn radio_buttons_impl(&mut self, ui: &mut crate::Ui) -> bool { + let mut changed = false; + changed |= ui + .selectable_value(self, Self::System, label(Self::System)) + .changed(); + changed |= ui + .selectable_value(self, Self::Dark, label(Self::Dark)) + .changed(); + changed |= ui + .selectable_value(self, Self::Light, label(Self::Light)) + .changed(); + changed + } +} + +fn icon(preference: ThemePreference) -> &'static str { + match preference { + ThemePreference::Dark => "πŸŒ™", + ThemePreference::Light => "β˜€", + ThemePreference::System => "✨", + } +} + +fn label(preference: ThemePreference) -> &'static str { + match preference { + ThemePreference::Dark => "πŸŒ™ Dark", + ThemePreference::Light => "β˜€ Light", + ThemePreference::System => "✨ Follow System", + } +} diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 41897cbf4ae1..38321d6a1609 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -1436,8 +1436,6 @@ impl Style { always_scroll_the_only_direction, } = self; - visuals.light_dark_radio_buttons(ui); - crate::Grid::new("_options").show(ui, |ui| { ui.label("Override font id"); ui.vertical(|ui| { diff --git a/crates/egui/src/widgets/mod.rs b/crates/egui/src/widgets/mod.rs index 9900117062e9..365782f7c809 100644 --- a/crates/egui/src/widgets/mod.rs +++ b/crates/egui/src/widgets/mod.rs @@ -134,16 +134,16 @@ pub fn stroke_ui(ui: &mut crate::Ui, stroke: &mut epaint::Stroke, text: &str) { /// Show a small button to switch to/from dark/light mode (globally). pub fn global_dark_light_mode_switch(ui: &mut Ui) { - let style: crate::Style = (*ui.ctx().style()).clone(); - let new_visuals = style.visuals.light_dark_small_toggle_button(ui); - if let Some(visuals) = new_visuals { - ui.ctx().set_visuals(visuals); + let mut theme_preference = ui.ctx().options(|opt| opt.theme_preference); + if theme_preference.small_combo_box(ui).changed() { + ui.ctx().set_theme(theme_preference); } } /// Show larger buttons for switching between light and dark mode (globally). pub fn global_dark_light_mode_buttons(ui: &mut Ui) { - let mut visuals = ui.ctx().style().visuals.clone(); - visuals.light_dark_radio_buttons(ui); - ui.ctx().set_visuals(visuals); + let mut theme_preference = ui.ctx().options(|opt| opt.theme_preference); + if theme_preference.radio_buttons(ui).changed() { + ui.ctx().set_theme(theme_preference); + } } diff --git a/scripts/build_demo_web.sh b/scripts/build_demo_web.sh index 3bc33d225c77..13bc4914a973 100755 --- a/scripts/build_demo_web.sh +++ b/scripts/build_demo_web.sh @@ -87,7 +87,6 @@ echo "Building rust…" (cd crates/$CRATE_NAME && cargo build \ ${BUILD_FLAGS} \ - --quiet \ --lib \ --target wasm32-unknown-unknown \ --no-default-features \