mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 13:50:04 -04:00
Follow the System Theme in egui (#4860)
* Some initial progress towards #4490 This PR just moves `Theme` and the "follow system theme" settings to egui and adds `RawInput.system_theme`. A follow-up PR can then introduce the two separate `dark_mode_style` and `light_mode_style` fields on `Options`. <!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * [x] I have followed the instructions in the PR template ### Breaking changes The options `follow_system_theme` and `default_theme` has been moved from `eframe` into `egui::Options`, settable with `ctx.options_mut` --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -297,21 +297,6 @@ pub struct NativeOptions {
|
||||
#[cfg(any(feature = "glow", feature = "wgpu"))]
|
||||
pub renderer: Renderer,
|
||||
|
||||
/// Try to detect and follow the system preferred setting for dark vs light mode.
|
||||
///
|
||||
/// The theme will automatically change when the dark vs light mode preference is changed.
|
||||
///
|
||||
/// Does not work on Linux (see <https://github.com/rust-windowing/winit/issues/1549>).
|
||||
///
|
||||
/// See also [`Self::default_theme`].
|
||||
pub follow_system_theme: bool,
|
||||
|
||||
/// Which theme to use in case [`Self::follow_system_theme`] is `false`
|
||||
/// or eframe fails to detect the system theme.
|
||||
///
|
||||
/// Default: [`Theme::Dark`].
|
||||
pub default_theme: Theme,
|
||||
|
||||
/// This controls what happens when you close the main eframe window.
|
||||
///
|
||||
/// If `true`, execution will continue after the eframe window is closed.
|
||||
@@ -417,8 +402,6 @@ impl Default for NativeOptions {
|
||||
#[cfg(any(feature = "glow", feature = "wgpu"))]
|
||||
renderer: Renderer::default(),
|
||||
|
||||
follow_system_theme: cfg!(target_os = "macos") || cfg!(target_os = "windows"),
|
||||
default_theme: Theme::Dark,
|
||||
run_and_return: true,
|
||||
|
||||
#[cfg(any(feature = "glow", feature = "wgpu"))]
|
||||
@@ -449,19 +432,6 @@ impl Default for NativeOptions {
|
||||
/// Options when using `eframe` in a web page.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub struct WebOptions {
|
||||
/// Try to detect and follow the system preferred setting for dark vs light mode.
|
||||
///
|
||||
/// See also [`Self::default_theme`].
|
||||
///
|
||||
/// Default: `true`.
|
||||
pub follow_system_theme: bool,
|
||||
|
||||
/// Which theme to use in case [`Self::follow_system_theme`] is `false`
|
||||
/// or system theme detection fails.
|
||||
///
|
||||
/// Default: `Theme::Dark`.
|
||||
pub default_theme: Theme,
|
||||
|
||||
/// Sets the number of bits in the depth buffer.
|
||||
///
|
||||
/// `egui` doesn't need the depth buffer, so the default value is 0.
|
||||
@@ -492,8 +462,6 @@ pub struct WebOptions {
|
||||
impl Default for WebOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
follow_system_theme: true,
|
||||
default_theme: Theme::Dark,
|
||||
depth_buffer: 0,
|
||||
|
||||
#[cfg(feature = "glow")]
|
||||
@@ -509,31 +477,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))]
|
||||
@@ -814,11 +757,6 @@ pub struct IntegrationInfo {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub web_info: WebInfo,
|
||||
|
||||
/// Does the OS use dark or light mode?
|
||||
///
|
||||
/// `None` means "don't know".
|
||||
pub system_theme: Option<Theme>,
|
||||
|
||||
/// Seconds of cpu usage (in seconds) on the previous frame.
|
||||
///
|
||||
/// This includes [`App::update`] as well as rendering (except for vsync waiting).
|
||||
|
||||
Reference in New Issue
Block a user