1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30: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:
Ruben
2023-03-29 16:39:30 +02:00
committed by GitHub
parent 870264b005
commit 977749b0e0
7 changed files with 87 additions and 10 deletions

View File

@@ -530,15 +530,16 @@ pub async fn start(
tracing::warn!(
"eframe compiled without RUSTFLAGS='--cfg=web_sys_unstable_apis'. Copying text won't work."
);
let follow_system_theme = web_options.follow_system_theme;
let mut runner = AppRunner::new(canvas_id, web_options, app_creator).await?;
runner.warm_up()?;
start_runner(runner)
start_runner(runner, follow_system_theme)
}
/// Install event listeners to register different input events
/// and starts running the given [`AppRunner`].
fn start_runner(app_runner: AppRunner) -> Result<AppRunnerRef, JsValue> {
fn start_runner(app_runner: AppRunner, follow_system_theme: bool) -> Result<AppRunnerRef, JsValue> {
let mut runner_container = AppRunnerContainer {
runner: Arc::new(Mutex::new(app_runner)),
panicked: Arc::new(AtomicBool::new(false)),
@@ -549,6 +550,10 @@ fn start_runner(app_runner: AppRunner) -> Result<AppRunnerRef, JsValue> {
super::events::install_document_events(&mut runner_container)?;
text_agent::install_text_agent(&mut runner_container)?;
if follow_system_theme {
super::events::install_color_scheme_change_event(&mut runner_container)?;
}
super::events::paint_and_schedule(&runner_container.runner, runner_container.panicked.clone())?;
// Disable all event handlers on panic