diff options
author | Emil Ernerfeldt <emil.ernerfeldt@gmail.com> | 2024-09-16 16:28:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-16 16:28:54 +0200 |
commit | 89da356b79f7c7e11ac225e0f7c81ff953169db6 (patch) | |
tree | 3737de2767a9d5da28283e6eae68a5c8552b0403 | |
parent | 1488ffa35a407653c81d1400483a67e559768878 (diff) |
Fix: call `save` when hiding tab, and `update` when focusing it (#5114)
Fix for a regression in 0.28
* `App::save` will now be called when the web app is hidden (e.g. goes
to a background tab)
* `App::update` will now be called when the web app is un-hidden (e.g.
becomes the foreground tab)
-rw-r--r-- | crates/eframe/src/web/app_runner.rs | 6 | ||||
-rw-r--r-- | crates/eframe/src/web/events.rs | 8 |
2 files changed, 8 insertions, 6 deletions
diff --git a/crates/eframe/src/web/app_runner.rs b/crates/eframe/src/web/app_runner.rs index 3fc3f95c..4b7afebf 100644 --- a/crates/eframe/src/web/app_runner.rs +++ b/crates/eframe/src/web/app_runner.rs @@ -176,6 +176,12 @@ impl AppRunner { /// /// Technically: does either the canvas or the [`TextAgent`] have focus? pub fn has_focus(&self) -> bool { + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + if document.hidden() { + return false; + } + super::has_focus(self.canvas()) || self.text_agent.has_focus() } diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index ebb1bda2..cdecf3b7 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -61,6 +61,7 @@ pub(crate) fn install_event_handlers(runner_ref: &WebRunner) -> Result<(), JsVal let document = window.document().unwrap(); let canvas = runner_ref.try_lock().unwrap().canvas().clone(); + install_blur_focus(runner_ref, &document)?; install_blur_focus(runner_ref, &canvas)?; prevent_default_and_stop_propagation( @@ -106,15 +107,10 @@ pub(crate) fn install_event_handlers(runner_ref: &WebRunner) -> Result<(), JsVal fn install_blur_focus(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), JsValue> { // NOTE: because of the text agent we sometime miss 'blur' events, // so we also poll the focus state each frame in `AppRunner::logic`. - for event_name in ["blur", "focus"] { + for event_name in ["blur", "focus", "visibilitychange"] { let closure = move |_event: web_sys::MouseEvent, runner: &mut AppRunner| { log::trace!("{} {event_name:?}", runner.canvas().id()); runner.update_focus(); - - if event_name == "blur" { - // This might be a good time to save the state - runner.save(); - } }; runner_ref.add_event_listener(target, event_name, closure)?; |