Skip to content

Commit

Permalink
Web: account for canvas being focused already (#3369)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and kchibisov committed Jan 15, 2024
1 parent da82971 commit 87f44ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Unreleased` header.

# Unreleased

- On Web, account for canvas being focused already before event loop starts.

# 0.29.9

- On X11, fix `NotSupported` error not propagated when creating event loop.
Expand Down
21 changes: 21 additions & 0 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::iter;
use std::marker::PhantomData;
use std::rc::{Rc, Weak};

use web_sys::Element;

use super::runner::{EventWrapper, Execution};
use super::{
super::{monitor::MonitorHandle, KeyEventExtra},
Expand Down Expand Up @@ -122,6 +124,25 @@ impl<T> EventLoopWindowTarget<T> {
}
});

// It is possible that at this point the canvas has
// been focused before the callback can be called.
let focused = canvas
.document()
.active_element()
.filter(|element| {
let canvas: &Element = canvas.raw();
element == canvas
})
.is_some();

if focused {
canvas.has_focus.set(true);
self.runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
})
}

let runner = self.runner.clone();
let modifiers = self.modifiers.clone();
canvas.on_keyboard_press(
Expand Down

0 comments on commit 87f44ec

Please sign in to comment.