Skip to content

Commit

Permalink
Remove from public API for now
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 8, 2024
1 parent b684322 commit 671a834
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 19 additions & 2 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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,

Expand All @@ -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]
Expand Down
15 changes: 3 additions & 12 deletions crates/egui/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub use self::{
slider::{Slider, SliderOrientation},
spinner::Spinner,
text_edit::{TextBuffer, TextEdit},
theme_switch::ThemeSwitch,
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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());
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/egui/src/widgets/theme_switch/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
Expand Down
9 changes: 0 additions & 9 deletions crates/egui_demo_lib/src/demo/widget_gallery.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use egui::{ThemePreference, ThemeSwitch};

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum Enum {
Expand All @@ -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))]
Expand All @@ -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,
}
Expand Down Expand Up @@ -123,7 +119,6 @@ impl WidgetGallery {
animate_progress_bar,
#[cfg(feature = "chrono")]
date,
theme,
} = self;

ui.add(doc_link_label("Label", "label"));
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 671a834

Please sign in to comment.