From f3a0ac1d1b45958d1d4dafab22d7ace4259a9ba1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 1 Jul 2024 16:16:56 +0200 Subject: [PATCH] eframe web fix: `save()` on "blur" event --- crates/eframe/src/web/app_runner.rs | 2 +- crates/eframe/src/web/events.rs | 7 ++++++- crates/eframe/src/web/text_agent.rs | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/eframe/src/web/app_runner.rs b/crates/eframe/src/web/app_runner.rs index e8af27d76be..f683b991aff 100644 --- a/crates/eframe/src/web/app_runner.rs +++ b/crates/eframe/src/web/app_runner.rs @@ -191,7 +191,7 @@ impl AppRunner { pub fn update_focus(&mut self) { let has_focus = self.has_focus(); if self.input.raw.focused != has_focus { - // log::debug!("{} Focus changed to {has_focus}", self.canvas().id()); + log::trace!("{} Focus changed to {has_focus}", self.canvas().id()); self.input.set_focus(has_focus); if !has_focus { diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index befe6e3d850..0ed759d952a 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -103,8 +103,13 @@ fn install_blur_focus(runner_ref: &WebRunner, target: &EventTarget) -> Result<() // so we also poll the focus state each frame in `AppRunner::logic`. for event_name in ["blur", "focus"] { let closure = move |_event: web_sys::MouseEvent, runner: &mut AppRunner| { - // log::debug!("{} {event_name:?}", runner.canvas().id()); + 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)?; diff --git a/crates/eframe/src/web/text_agent.rs b/crates/eframe/src/web/text_agent.rs index 79f63ee0662..234dbb8ff3f 100644 --- a/crates/eframe/src/web/text_agent.rs +++ b/crates/eframe/src/web/text_agent.rs @@ -148,7 +148,7 @@ impl TextAgent { return; } - // log::debug!("Focusing text agent"); + log::trace!("Focusing text agent"); if let Err(err) = self.input.focus() { log::error!("failed to set focus: {}", super::string_from_js_value(&err)); @@ -160,7 +160,7 @@ impl TextAgent { return; } - // log::debug!("Blurring text agent"); + log::trace!("Blurring text agent"); if let Err(err) = self.input.blur() { log::error!("failed to set focus: {}", super::string_from_js_value(&err));