Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 17, 2024
1 parent 7573af0 commit 7493604
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
20 changes: 20 additions & 0 deletions crates/egui/src/memory/theme.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::{Button, ComboBox};

/// Dark or Light theme.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
Expand Down Expand Up @@ -44,3 +46,21 @@ impl From<Theme> for ThemePreference {
}
}
}

impl ThemePreference {
pub fn small_combobox(self, ui: &mut crate::Ui) -> Option<Self> {
let mut new = self;
ui.selectable_value(&mut new, Self::System, icon(Self::System));
ui.selectable_value(&mut new, Self::Dark, icon(Self::Dark));
ui.selectable_value(&mut new, Self::Light, icon(Self::Light));
(new != self).then_some(new)
}
}

fn icon(preference: ThemePreference) -> &'static str {
match preference {
ThemePreference::Dark => "🌙",
ThemePreference::Light => "☀",
ThemePreference::System => "⚙",
}
}
7 changes: 3 additions & 4 deletions crates/egui/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ 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 theme_preference = ui.ctx().options(|opt| opt.theme_preference);
if let Some(theme_preference) = theme_preference.small_combobox(ui) {
ui.ctx().set_theme(theme_preference);
}
}

Expand Down
1 change: 0 additions & 1 deletion scripts/build_demo_web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ echo "Building rust…"
(cd crates/$CRATE_NAME &&
cargo build \
${BUILD_FLAGS} \
--quiet \
--lib \
--target wasm32-unknown-unknown \
--no-default-features \
Expand Down

0 comments on commit 7493604

Please sign in to comment.