diff --git a/Cargo.lock b/Cargo.lock index 6158dedff59..fef421abaf1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -659,6 +659,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" @@ -806,6 +815,7 @@ name = "egui-winit" version = "0.16.0" dependencies = [ "copypasta", + "dark-light", "egui", "epi", "instant", @@ -3018,6 +3028,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 42e78ccad3b..ebe4c607f75 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -8,6 +8,7 @@ NOTE: [`egui_web`](../egui_web/CHANGELOG.md), [`egui-winit`](../egui-winit/CHANG * Removed `Frame::alloc_texture`. Use `egui::Context::load_texture` instead ([#1110](https://github.com/emilk/egui/pull/1110)). * 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)). * Fix horizontal scrolling direction on Linux. * Added `App::on_exit_event` ([#1038](https://github.com/emilk/egui/pull/1038)) * Shift-scroll will now result in horizontal scrolling on all platforms ([#1136](https://github.com/emilk/egui/pull/1136)). diff --git a/egui-winit/CHANGELOG.md b/egui-winit/CHANGELOG.md index 426fbc6a206..80de1eaa77b 100644 --- a/egui-winit/CHANGELOG.md +++ b/egui-winit/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to the `egui-winit` integration will be noted in this file. ## Unreleased * Fixed horizontal scrolling direction on Linux. +* 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)) * Shift-scroll will now result in horizontal scrolling on all platforms ([#1136](https://github.com/emilk/egui/pull/1136)). * Require knowledge about max texture side (e.g. `GL_MAX_TEXTURE_SIZE`)) ([#1154](https://github.com/emilk/egui/pull/1154)). diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index 8e6c88824ac..6140ab66183 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -30,6 +30,7 @@ winit = "0.26.1" 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 } @@ -37,7 +38,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 995d8f8b4ff..b4762be5d6a 100644 --- a/egui-winit/src/epi.rs +++ b/egui-winit/src/epi.rs @@ -223,11 +223,13 @@ impl EpiIntegration { *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)), }, @@ -235,6 +237,12 @@ impl EpiIntegration { repaint_signal, }); + 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, @@ -340,3 +348,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 45cb11391dd..3de396f5412 100644 --- a/egui_glium/CHANGELOG.md +++ b/egui_glium/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to the `egui_glium` integration will be noted in this file. ## Unreleased * `EguiGlium::run` no longer returns the shapes to paint, but stores them internally until you call `EguiGlium::paint` ([#1110](https://github.com/emilk/egui/pull/1110)). * Optimize the painter and texture uploading ([#1110](https://github.com/emilk/egui/pull/1110)). +* 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/CHANGELOG.md b/egui_glow/CHANGELOG.md index a5eaa4b2182..b5bcdb6de09 100644 --- a/egui_glow/CHANGELOG.md +++ b/egui_glow/CHANGELOG.md @@ -7,7 +7,9 @@ All notable changes to the `egui_glow` integration will be noted in this file. * Added `set_texture_filter` method to `Painter` ([#1041](https://github.com/emilk/egui/pull/1041)). * Fix failure to run in Chrome ([#1092](https://github.com/emilk/egui/pull/1092)). * `EguiGlow::new` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)). -* `EguiGlow::paint` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)). +* `EguiGlow::paint` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)). +* Automatically detect and apply dark or light mode from system ([#1045](https://github.com/emilk/egui/pull/1045)). + ## 0.16.0 - 2021-12-29 * Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)). diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index 9b307f1c55f..cd0add6e00e 100644 --- a/egui_glow/Cargo.toml +++ b/egui_glow/Cargo.toml @@ -35,7 +35,7 @@ memoffset = "0.6" tracing = "0.1" [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]