Skip to content

Commit

Permalink
Fix focus tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jun 27, 2024
1 parent 5154a9d commit c6980af
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/eframe/src/web/app_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
self.input
.set_focus(super::has_focus(self.canvas()) || self.text_agent.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);

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

Expand Down

0 comments on commit c6980af

Please sign in to comment.