1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 14:20:04 -04:00

egui-winit: Automatically detect and apply dark or light mode (#1045)

This commit is contained in:
Emil Ernerfeldt
2022-02-02 17:09:36 +01:00
committed by GitHub
parent 270c08a030
commit c3be566574
8 changed files with 601 additions and 15 deletions

View File

@@ -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)).

View File

@@ -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.2.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.

View File

@@ -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<bool> {
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<bool> {
None
}