Skip to content

Commit

Permalink
egui-winit: Automatically detect and apply dark or light mode
Browse files Browse the repository at this point in the history
Closes #1001

Uses the `dark-light` crate: https://crates.io/crates/dark-light
  • Loading branch information
emilk committed Jan 6, 2022
1 parent 7863f44 commit d7e0f25
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 7 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions egui-winit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
3 changes: 2 additions & 1 deletion egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ 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 }

# feature screen_reader
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.
Expand Down
28 changes: 24 additions & 4 deletions egui-winit/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,29 @@ impl EpiIntegration {
persistence: crate::epi::Persistence,
app: Box<dyn epi::App>,
) -> 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)),
},
output: Default::default(),
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,
Expand Down Expand Up @@ -311,3 +318,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
}
1 change: 1 addition & 0 deletions egui_glium/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion egui_glium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions egui_glow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion egui_glow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit d7e0f25

Please sign in to comment.