diff --git a/crates/egui/src/lib.rs b/crates/egui/src/lib.rs index 9675fd69790..ae8e171daa8 100644 --- a/crates/egui/src/lib.rs +++ b/crates/egui/src/lib.rs @@ -460,7 +460,7 @@ pub use { layers::{LayerId, Order}, layout::*, load::SizeHint, - memory::{Memory, Options, ThemePreference}, + memory::{Memory, Options}, painter::Painter, response::{InnerResponse, Response}, sense::Sense, diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index f37e422b1b1..7fad4a5baab 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -5,7 +5,7 @@ use epaint::emath::TSTransform; use crate::{ area, vec2, EventFilter, Id, IdMap, LayerId, Order, Pos2, Rangef, RawInput, Rect, Style, Vec2, - ViewportId, ViewportIdMap, ViewportIdSet, + ViewportId, ViewportIdMap, ViewportIdSet, Visuals, }; // ---------------------------------------------------------------------------- @@ -1106,7 +1106,7 @@ impl Areas { /// Which theme should egui use? #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub enum ThemePreference { +pub(crate) enum ThemePreference { /// Dark mode: light text on a dark background. Dark, @@ -1117,6 +1117,23 @@ pub enum ThemePreference { System, } +impl ThemePreference { + pub(crate) fn from_dark_mode(dark_mode: bool) -> Self { + if dark_mode { + Self::Dark + } else { + Self::Light + } + } + + pub(crate) fn into_visuals(self) -> Visuals { + match self { + Self::Dark | Self::System => Visuals::dark(), + Self::Light => Visuals::light(), + } + } +} + // ---------------------------------------------------------------------------- #[test] diff --git a/crates/egui/src/widgets/mod.rs b/crates/egui/src/widgets/mod.rs index 8dbc8454f1d..d4cc2d702b7 100644 --- a/crates/egui/src/widgets/mod.rs +++ b/crates/egui/src/widgets/mod.rs @@ -41,7 +41,6 @@ pub use self::{ slider::{Slider, SliderOrientation}, spinner::Spinner, text_edit::{TextBuffer, TextEdit}, - theme_switch::ThemeSwitch, }; // ---------------------------------------------------------------------------- @@ -136,18 +135,10 @@ 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 mut theme = if ui.ctx().style().visuals.dark_mode { - ThemePreference::Dark - } else { - ThemePreference::Light - }; - let response = ui.add(ThemeSwitch::new(&mut theme).show_follow_system(false)); + let mut theme = memory::ThemePreference::from_dark_mode(ui.ctx().style().visuals.dark_mode); + let response = ui.add(theme_switch::ThemeSwitch::new(&mut theme).show_follow_system(false)); if response.changed { - let visuals = match theme { - ThemePreference::Dark | ThemePreference::System => Visuals::dark(), - ThemePreference::Light => Visuals::light(), - }; - ui.ctx().set_visuals(visuals); + ui.ctx().set_visuals(theme.into_visuals()); } } diff --git a/crates/egui/src/widgets/theme_switch/mod.rs b/crates/egui/src/widgets/theme_switch/mod.rs index c24bd9fb48a..02997473c97 100644 --- a/crates/egui/src/widgets/theme_switch/mod.rs +++ b/crates/egui/src/widgets/theme_switch/mod.rs @@ -1,4 +1,5 @@ -use crate::{Painter, Response, ThemePreference, Ui, Widget}; +use crate::memory::ThemePreference; +use crate::{Painter, Response, Ui, Widget}; use emath::{Pos2, Rect}; use epaint::Color32; @@ -12,13 +13,13 @@ mod sun; /// preference (dark, light or follow system). #[must_use = "You should put this widget in an ui with `ui.add(widget);`"] #[derive(Debug)] -pub struct ThemeSwitch<'a> { +pub(crate) struct ThemeSwitch<'a> { value: &'a mut ThemePreference, show_follow_system: bool, } impl<'a> ThemeSwitch<'a> { - pub fn new(value: &'a mut ThemePreference) -> Self { + pub(crate) fn new(value: &'a mut ThemePreference) -> Self { Self { value, show_follow_system: true, diff --git a/crates/egui_demo_lib/src/demo/widget_gallery.rs b/crates/egui_demo_lib/src/demo/widget_gallery.rs index 1626488ad9b..2f59c7caee0 100644 --- a/crates/egui_demo_lib/src/demo/widget_gallery.rs +++ b/crates/egui_demo_lib/src/demo/widget_gallery.rs @@ -1,5 +1,3 @@ -use egui::{ThemePreference, ThemeSwitch}; - #[derive(Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] enum Enum { @@ -20,7 +18,6 @@ pub struct WidgetGallery { string: String, color: egui::Color32, animate_progress_bar: bool, - theme: ThemePreference, #[cfg(feature = "chrono")] #[cfg_attr(feature = "serde", serde(skip))] @@ -39,7 +36,6 @@ impl Default for WidgetGallery { string: Default::default(), color: egui::Color32::LIGHT_BLUE.linear_multiply(0.5), animate_progress_bar: false, - theme: ThemePreference::System, #[cfg(feature = "chrono")] date: None, } @@ -123,7 +119,6 @@ impl WidgetGallery { animate_progress_bar, #[cfg(feature = "chrono")] date, - theme, } = self; ui.add(doc_link_label("Label", "label")); @@ -253,10 +248,6 @@ impl WidgetGallery { }); ui.end_row(); - ui.add(doc_link_label("ThemeSwitch", "ThemeSwitch")); - ui.add(ThemeSwitch::new(theme)); - ui.end_row(); - ui.add(doc_link_label_with_crate("egui_plot", "Plot", "plot")); example_plot(ui); ui.end_row();