Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 20, 2024
1 parent 7573af0 commit 9e27434
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
8 changes: 5 additions & 3 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
});
Expand Down
61 changes: 61 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, InnerResponse, Response};

/// 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,62 @@ impl From<Theme> 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",
}
}
2 changes: 0 additions & 2 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
14 changes: 7 additions & 7 deletions crates/egui/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
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 9e27434

Please sign in to comment.