diff --git a/crates/eframe/src/web/backend.rs b/crates/eframe/src/web/backend.rs index 041fe42028a..74853abe9fb 100644 --- a/crates/eframe/src/web/backend.rs +++ b/crates/eframe/src/web/backend.rs @@ -36,8 +36,10 @@ impl WebInput { raw_input } + /// On alt-tab and similar. pub fn on_web_page_focus_change(&mut self, focused: bool) { - self.raw.modifiers = egui::Modifiers::default(); + // 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; self.raw.events.push(egui::Event::WindowFocused(focused)); self.latest_touch_pos = None; diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index 224ae3ac0db..f7234d76fad 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -50,24 +50,21 @@ fn paint_if_needed(runner: &mut AppRunner) { pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsValue> { let document = web_sys::window().unwrap().document().unwrap(); - { - // Avoid sticky modifier keys on alt-tab: - for event_name in ["blur", "focus"] { - let closure = move |_event: web_sys::MouseEvent, runner: &mut AppRunner| { - let has_focus = event_name == "focus"; - - if !has_focus { - // We lost focus - good idea to save - runner.save(); - } + for event_name in ["blur", "focus"] { + let closure = move |_event: web_sys::MouseEvent, runner: &mut AppRunner| { + // log::debug!("{event_name:?}"); + let has_focus = event_name == "focus"; + + if !has_focus { + // We lost focus - good idea to save + runner.save(); + } - runner.input.on_web_page_focus_change(has_focus); - runner.egui_ctx().request_repaint(); - // log::debug!("{event_name:?}"); - }; + runner.input.on_web_page_focus_change(has_focus); + runner.egui_ctx().request_repaint(); + }; - runner_ref.add_event_listener(&document, event_name, closure)?; - } + runner_ref.add_event_listener(&document, event_name, closure)?; } runner_ref.add_event_listener( @@ -228,13 +225,31 @@ pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsVa pub(crate) fn install_window_events(runner_ref: &WebRunner) -> Result<(), JsValue> { let window = web_sys::window().unwrap(); + for event_name in ["blur", "focus"] { + let closure = move |_event: web_sys::MouseEvent, runner: &mut AppRunner| { + // log::debug!("{event_name:?}"); + let has_focus = event_name == "focus"; + + if !has_focus { + // We lost focus - good idea to save + runner.save(); + } + + runner.input.on_web_page_focus_change(has_focus); + runner.egui_ctx().request_repaint(); + }; + + runner_ref.add_event_listener(&window, event_name, closure)?; + } + // Save-on-close runner_ref.add_event_listener(&window, "onbeforeunload", |_: web_sys::Event, runner| { runner.save(); })?; for event_name in &["load", "pagehide", "pageshow", "resize"] { - runner_ref.add_event_listener(&window, event_name, |_: web_sys::Event, runner| { + runner_ref.add_event_listener(&window, event_name, move |_: web_sys::Event, runner| { + // log::debug!("{event_name:?}"); runner.needs_repaint.repaint_asap(); })?; }