mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 22:30:03 -04:00
egui-winit: Automatically detect and apply dark or light mode (#1045)
This commit is contained in:
@@ -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)).
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user