mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
eframe: Automatically change theme when system dark/light mode changes (#2750)
* React to ThemeChanged event from winit * React to theme change using media query change event in WASM * Share conversion from bool -> Theme * Suppress too_many_arguments warning * Document limitations of automatically following the dark vs light mode preference * Simplify expression * Conditionally compile code to prevent unused item warnings * Remove needless borrow * Remove another needless borrow * Make associated functions to standalone * Request repaint after theme has changed * Only install event listener when `follow_system_theme` is enabled * Remove dark-light feature gate * Detect system theme using winit * Update documentation * Fix typos * fix warning about unused argument --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -36,7 +36,7 @@ use std::sync::{
|
||||
|
||||
use egui::Vec2;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::EventTarget;
|
||||
use web_sys::{EventTarget, MediaQueryList};
|
||||
|
||||
use input::*;
|
||||
|
||||
@@ -75,11 +75,22 @@ pub fn native_pixels_per_point() -> f32 {
|
||||
}
|
||||
|
||||
pub fn system_theme() -> Option<Theme> {
|
||||
let dark_mode = web_sys::window()?
|
||||
.match_media("(prefers-color-scheme: dark)")
|
||||
let dark_mode = prefers_color_scheme_dark(&web_sys::window()?)
|
||||
.ok()??
|
||||
.matches();
|
||||
Some(if dark_mode { Theme::Dark } else { Theme::Light })
|
||||
Some(theme_from_dark_mode(dark_mode))
|
||||
}
|
||||
|
||||
fn prefers_color_scheme_dark(window: &web_sys::Window) -> Result<Option<MediaQueryList>, JsValue> {
|
||||
window.match_media("(prefers-color-scheme: dark)")
|
||||
}
|
||||
|
||||
fn theme_from_dark_mode(dark_mode: bool) -> Theme {
|
||||
if dark_mode {
|
||||
Theme::Dark
|
||||
} else {
|
||||
Theme::Light
|
||||
}
|
||||
}
|
||||
|
||||
pub fn canvas_element(canvas_id: &str) -> Option<web_sys::HtmlCanvasElement> {
|
||||
|
||||
Reference in New Issue
Block a user