Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: call save when hiding web tab, and update when focusing it #5114

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 2 additions & 6 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)?;
Expand Down
Loading