From d7e0f2514173e8746bd2e72b251d500501448db9 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 6 Jan 2022 11:52:14 +0100 Subject: [PATCH] egui-winit: Automatically detect and apply dark or light mode Closes https://github.com/emilk/egui/issues/1001 Uses the `dark-light` crate: https://crates.io/crates/dark-light --- Cargo.lock | 19 +++++++++++++++++++ eframe/CHANGELOG.md | 1 + egui-winit/CHANGELOG.md | 1 + egui-winit/Cargo.toml | 3 ++- egui-winit/src/epi.rs | 28 ++++++++++++++++++++++++---- egui_glium/CHANGELOG.md | 1 + egui_glium/Cargo.toml | 2 +- egui_glow/CHANGELOG.md | 1 + egui_glow/Cargo.toml | 2 +- 9 files changed, 51 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c376760403ec..cfdda171d7a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -649,6 +649,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" +[[package]] +name = "dark-light" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7120c3a2b4cbef379407b56cc808304a728cadc756605ab5f0b70619b4d44b9" +dependencies = [ + "winreg", +] + [[package]] name = "darling" version = "0.13.1" @@ -804,6 +813,7 @@ name = "egui-winit" version = "0.16.0" dependencies = [ "copypasta", + "dark-light", "egui", "epi", "instant", @@ -2899,6 +2909,15 @@ dependencies = [ "x11-dl", ] +[[package]] +name = "winreg" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d107f8c6e916235c4c01cabb3e8acf7bea8ef6a63ca2e7fa0527c049badfc48c" +dependencies = [ + "winapi", +] + [[package]] name = "x11-clipboard" version = "0.5.3" diff --git a/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index a648096558cd..206a3bc14156 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -7,6 +7,7 @@ NOTE: [`egui_web`](egui_web/CHANGELOG.md), [`egui-winit`](egui-winit/CHANGELOG.m ## Unreleased * The default native backend is now `egui_glow` (instead of `egui_glium`) ([#1020](https://github.com/emilk/egui/pull/1020)). * The default web painter is now `egui_glow` (instead of WebGL) ([#1020](https://github.com/emilk/egui/pull/1020)). +* Automatically detect and apply dark or light mode from system ([#1045](https://github.com/emilk/egui/pull/1045)). ## 0.16.0 - 2021-12-29 diff --git a/egui-winit/CHANGELOG.md b/egui-winit/CHANGELOG.md index e2eb6621b206..0db9c3305a5d 100644 --- a/egui-winit/CHANGELOG.md +++ b/egui-winit/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to the `egui-winit` integration will be noted in this file. ## Unreleased +* Automatically detect and apply dark or light mode from system ([#1045](https://github.com/emilk/egui/pull/1045)). * Replaced `std::time::Instant` with `instant::Instant` for WebAssembly compatability ([#1023](https://github.com/emilk/egui/pull/1023)) diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index 917a3ae5e35e..27a6d1a3a184 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -29,6 +29,7 @@ winit = "0.26" epi = { version = "0.16.0", path = "../epi", optional = true } copypasta = { version = "0.7", optional = true } +dark-light = { version = "0.1.1", optional = true } # detect dark mode system preference serde = { version = "1.0", optional = true, features = ["derive"] } webbrowser = { version = "0.5", optional = true } @@ -36,7 +37,7 @@ webbrowser = { version = "0.5", optional = true } tts = { version = "0.19", optional = true } [features] -default = ["clipboard", "links"] +default = ["clipboard", "dark-light", "links"] # enable cut/copy/paste to OS clipboard. # if disabled a clipboard will be simulated so you can still copy/paste within the egui app. diff --git a/egui-winit/src/epi.rs b/egui-winit/src/epi.rs index badea97aaeb6..e07fd18f6149 100644 --- a/egui-winit/src/epi.rs +++ b/egui-winit/src/epi.rs @@ -206,15 +206,13 @@ impl EpiIntegration { persistence: crate::epi::Persistence, app: Box, ) -> Self { - let egui_ctx = egui::CtxRef::default(); - - *egui_ctx.memory() = persistence.load_memory().unwrap_or_default(); + let prefer_dark_mode = prefer_dark_mode(); let frame = epi::Frame::new(epi::backend::FrameData { info: epi::IntegrationInfo { name: integration_name, web_info: None, - prefer_dark_mode: None, // TODO: figure out system default + prefer_dark_mode, cpu_usage: None, native_pixels_per_point: Some(crate::native_pixels_per_point(window)), }, @@ -222,6 +220,15 @@ impl EpiIntegration { repaint_signal, }); + let egui_ctx = egui::CtxRef::default(); + *egui_ctx.memory() = persistence.load_memory().unwrap_or_default(); + + if prefer_dark_mode == Some(true) { + egui_ctx.set_visuals(egui::Visuals::dark()); + } else { + egui_ctx.set_visuals(egui::Visuals::light()); + } + let mut slf = Self { frame, persistence, @@ -311,3 +318,16 @@ impl EpiIntegration { .save(&mut *self.app, &self.egui_ctx, window); } } + +#[cfg(feature = "dark-light")] +fn prefer_dark_mode() -> Option { + match dark_light::detect() { + dark_light::Mode::Dark => Some(true), + dark_light::Mode::Light => Some(false), + } +} + +#[cfg(not(feature = "dark-light"))] +fn prefer_dark_mode() -> Option { + None +} diff --git a/egui_glium/CHANGELOG.md b/egui_glium/CHANGELOG.md index c01cd4ae4f80..662fb6782bca 100644 --- a/egui_glium/CHANGELOG.md +++ b/egui_glium/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to the `egui_glium` integration will be noted in this file. ## Unreleased +* Automatically detect and apply dark or light mode from system ([#1045](https://github.com/emilk/egui/pull/1045)). ## 0.16.0 - 2021-12-29 diff --git a/egui_glium/Cargo.toml b/egui_glium/Cargo.toml index 8f6634bbab24..57c90762e991 100644 --- a/egui_glium/Cargo.toml +++ b/egui_glium/Cargo.toml @@ -24,7 +24,7 @@ all-features = true [dependencies] egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded"] } -egui-winit = { version = "0.16.0", path = "../egui-winit", default-features = false, features = ["epi"] } +egui-winit = { version = "0.16.0", path = "../egui-winit", default-features = false, features = ["dark-light", "epi"] } epi = { version = "0.16.0", path = "../epi", optional = true } glium = "0.31" diff --git a/egui_glow/CHANGELOG.md b/egui_glow/CHANGELOG.md index 5fa3505f9590..db4b704d2e06 100644 --- a/egui_glow/CHANGELOG.md +++ b/egui_glow/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to the `egui_glow` integration will be noted in this file. ## Unreleased +* Automatically detect and apply dark or light mode from system ([#1045](https://github.com/emilk/egui/pull/1045)). ## 0.16.0 - 2021-12-29 diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index 8945b5f0e3dc..ad3040f6690e 100644 --- a/egui_glow/Cargo.toml +++ b/egui_glow/Cargo.toml @@ -30,7 +30,7 @@ glow = "0.11" memoffset = "0.6" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -egui-winit = { version = "0.16.0", path = "../egui-winit", default-features = false, features = ["epi"], optional = true } +egui-winit = { version = "0.16.0", path = "../egui-winit", default-features = false, features = ["dark-light", "epi"], optional = true } glutin = { version = "0.28.0", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies]