From 87ec7c882c2feabdc7d9038f79b7ab6826811f63 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 27 Jun 2024 16:58:26 +0200 Subject: [PATCH] Fix focus tracking --- crates/eframe/src/web/app_runner.rs | 4 ++++ crates/eframe/src/web/backend.rs | 8 ++++++-- crates/eframe/src/web/events.rs | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/eframe/src/web/app_runner.rs b/crates/eframe/src/web/app_runner.rs index a9de9e1ff731..5b4ace8f19d1 100644 --- a/crates/eframe/src/web/app_runner.rs +++ b/crates/eframe/src/web/app_runner.rs @@ -187,6 +187,10 @@ impl AppRunner { /// /// The result can be painted later with a call to [`Self::run_and_paint`] or [`Self::paint`]. pub fn logic(&mut self) { + // We sometimes miss blur/focus events due to the text agent, so let's just poll each frame: + let has_focus = super::has_focus(self.canvas()) || self.text_agent.has_focus(); + self.input.set_focus(has_focus); + let canvas_size = super::canvas_size_in_points(self.canvas(), self.egui_ctx()); let mut raw_input = self.input.new_frame(canvas_size); diff --git a/crates/eframe/src/web/backend.rs b/crates/eframe/src/web/backend.rs index 74853abe9fb1..fa87bf3b3242 100644 --- a/crates/eframe/src/web/backend.rs +++ b/crates/eframe/src/web/backend.rs @@ -36,8 +36,12 @@ impl WebInput { raw_input } - /// On alt-tab and similar. - pub fn on_web_page_focus_change(&mut self, focused: bool) { + /// On alt-tab, or user clicking another HTML element. + pub fn set_focus(&mut self, focused: bool) { + if self.raw.focused == focused { + return; + } + // log::debug!("on_web_page_focus_change: {focused}"); self.raw.modifiers = egui::Modifiers::default(); // Avoid sticky modifier keys on alt-tab: self.raw.focused = focused; diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index fca7595612dd..031a4c63fd9f 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -105,7 +105,7 @@ fn install_blur_focus(runner_ref: &WebRunner, target: &EventTarget) -> Result<() runner.save(); } - runner.input.on_web_page_focus_change(has_focus); + runner.input.set_focus(has_focus); runner.egui_ctx().request_repaint(); };