Skip to content

Commit

Permalink
Move Theme enum to egui
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jun 30, 2024
1 parent d4e8966 commit 7c6c0e1
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 39 deletions.
29 changes: 3 additions & 26 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#![warn(missing_docs)] // Let's keep `epi` well-documented.

use egui::Theme;

#[cfg(target_arch = "wasm32")]
use std::any::Any;

Expand Down Expand Up @@ -487,31 +489,6 @@ impl Default for WebOptions {

// ----------------------------------------------------------------------------

/// Dark or Light theme.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Theme {
/// Dark mode: light text on a dark background.
Dark,

/// Light mode: dark text on a light background.
Light,
}

impl Theme {
/// Get the egui visuals corresponding to this theme.
///
/// Use with [`egui::Context::set_visuals`].
pub fn egui_visuals(self) -> egui::Visuals {
match self {
Self::Dark => egui::Visuals::dark(),
Self::Light => egui::Visuals::light(),
}
}
}

// ----------------------------------------------------------------------------

/// WebGL Context options
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
Expand Down Expand Up @@ -795,7 +772,7 @@ pub struct IntegrationInfo {
/// Does the OS use dark or light mode?
///
/// `None` means "don't know".
pub system_theme: Option<Theme>,
pub system_theme: Option<egui::Theme>,

/// Seconds of cpu usage (in seconds) on the previous frame.
///
Expand Down
6 changes: 3 additions & 3 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use winit::event_loop::EventLoopWindowTarget;

use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};

use egui::{DeferredViewportUiCallback, NumExt as _, ViewportBuilder, ViewportId};
use egui::{DeferredViewportUiCallback, NumExt as _, Theme, ViewportBuilder, ViewportId, Visuals};
use egui_winit::{EventResponse, WindowSettings};

use crate::{epi, Theme};
use crate::epi;

pub fn viewport_builder<E>(
egui_zoom_factor: f32,
Expand Down Expand Up @@ -280,7 +280,7 @@ impl EpiIntegration {
WindowEvent::ThemeChanged(winit_theme) if self.follow_system_theme => {
let theme = theme_from_winit_theme(*winit_theme);
self.frame.info.system_theme = Some(theme);
self.egui_ctx.set_visuals(theme.egui_visuals());
self.egui_ctx.set_visuals(Visuals::theme(theme));
}
_ => {}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use winit::{
use ahash::{HashMap, HashSet};
use egui::{
DeferredViewportUiCallback, ImmediateViewport, ViewportBuilder, ViewportClass, ViewportId,
ViewportIdMap, ViewportIdPair, ViewportInfo, ViewportOutput,
ViewportIdMap, ViewportIdPair, ViewportInfo, ViewportOutput, Visuals,
};
#[cfg(feature = "accesskit")]
use egui_winit::accesskit_winit;
Expand Down Expand Up @@ -279,7 +279,7 @@ impl GlowWinitApp {
}

let theme = system_theme.unwrap_or(self.native_options.default_theme);
integration.egui_ctx.set_visuals(theme.egui_visuals());
integration.egui_ctx.set_visuals(Visuals::theme(theme));

if self
.native_options
Expand Down
3 changes: 2 additions & 1 deletion crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ahash::{HashMap, HashSet, HashSetExt};
use egui::{
DeferredViewportUiCallback, FullOutput, ImmediateViewport, ViewportBuilder, ViewportClass,
ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet, ViewportInfo, ViewportOutput,
Visuals,
};
#[cfg(feature = "accesskit")]
use egui_winit::accesskit_winit;
Expand Down Expand Up @@ -254,7 +255,7 @@ impl WgpuWinitApp {
integration.init_accesskit(&mut egui_winit, &window, event_loop_proxy);
}
let theme = system_theme.unwrap_or(self.native_options.default_theme);
egui_ctx.set_visuals(theme.egui_visuals());
egui_ctx.set_visuals(Visuals::theme(theme));

let app_creator = std::mem::take(&mut self.app_creator)
.expect("Single-use AppCreator has unexpectedly already been taken");
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/native/winit_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winit::{
window::{Window, WindowId},
};

use egui::ViewportId;
use egui::{Theme, ViewportId};
#[cfg(feature = "accesskit")]
use egui_winit::accesskit_winit;

Expand Down Expand Up @@ -103,7 +103,7 @@ pub enum EventResult {
Exit,
}

pub fn system_theme(window: &Window, options: &crate::NativeOptions) -> Option<crate::Theme> {
pub fn system_theme(window: &Window, options: &crate::NativeOptions) -> Option<Theme> {
if options.follow_system_theme {
window
.theme()
Expand Down
4 changes: 2 additions & 2 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::TexturesDelta;
use egui::{TexturesDelta, Visuals};

use crate::{epi, App};

Expand Down Expand Up @@ -69,7 +69,7 @@ impl AppRunner {
});

let theme = system_theme.unwrap_or(web_options.default_theme);
egui_ctx.set_visuals(theme.egui_visuals());
egui_ctx.set_visuals(Visuals::theme(theme));

let cc = epi::CreationContext {
egui_ctx: egui_ctx.clone(),
Expand Down
3 changes: 2 additions & 1 deletion crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use egui::Visuals;
use web_sys::EventTarget;

use super::*;
Expand Down Expand Up @@ -368,7 +369,7 @@ pub(crate) fn install_color_scheme_change_event(runner_ref: &WebRunner) -> Resul
|event, runner| {
let theme = theme_from_dark_mode(event.matches());
runner.frame.info.system_theme = Some(theme);
runner.egui_ctx().set_visuals(theme.egui_visuals());
runner.egui_ctx().set_visuals(Visuals::theme(theme));
runner.needs_repaint.repaint_asap();
},
)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use web_sys::MediaQueryList;

use input::*;

use crate::Theme;
use egui::Theme;

// ----------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ pub use {
painter::Painter,
response::{InnerResponse, Response},
sense::Sense,
style::{FontSelection, Style, TextStyle, Visuals},
style::{FontSelection, Style, TextStyle, Theme, Visuals},
text::{Galley, TextFormat},
ui::Ui,
ui_stack::*,
Expand Down
19 changes: 19 additions & 0 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,17 @@ impl Visuals {
}
}

/// Dark or Light theme.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Theme {
/// Dark mode: light text on a dark background.
Dark,

/// Light mode: dark text on a light background.
Light,
}

/// Selected text, selected elements etc
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
Expand Down Expand Up @@ -1118,6 +1129,14 @@ impl Default for Interaction {
}

impl Visuals {
/// Default dark or light theme.
pub fn theme(theme: Theme) -> Self {
match theme {
Theme::Dark => Self::dark(),
Theme::Light => Self::light(),
}
}

/// Default dark theme.
pub fn dark() -> Self {
Self {
Expand Down

0 comments on commit 7c6c0e1

Please sign in to comment.