Skip to content

Commit 82d9974

Browse files
ming08108hacknus
authored andcommitted
Update kb modifiers from web mouse events (emilk#4156)
Update modifier state from web mouse events. This allows modifiers to be correctly updated when the window is not in focus but the mouse is still moving over the window.
1 parent 0f0afb4 commit 82d9974

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Please keep pull requests small and focused. The smaller it is, the more likely
4444

4545
Most PR reviews are done by me, Emil, but I very much appreciate any help I can get reviewing PRs!
4646

47-
It is very easy to add complexity to a project, but remember that each line of code added is code that needs to be maintained in perpituity, so we have a high bar on what get merged!
47+
It is very easy to add complexity to a project, but remember that each line of code added is code that needs to be maintained in perpetuity, so we have a high bar on what get merged!
4848

4949
When reviewing, we look for:
5050
* The PR title and description should be helpful
@@ -123,7 +123,7 @@ with `Vec2::X` increasing to the right and `Vec2::Y` increasing downwards.
123123

124124
`egui` uses logical _points_ as its coordinate system.
125125
Those related to physical _pixels_ by the `pixels_per_point` scale factor.
126-
For example, a high-dpi screeen can have `pixels_per_point = 2.0`,
126+
For example, a high-dpi screen can have `pixels_per_point = 2.0`,
127127
meaning there are two physical screen pixels for each logical point.
128128

129129
Angles are in radians, and are measured clockwise from the X-axis, which has angle=0.

crates/eframe/src/web/events.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsVa
8787
return;
8888
}
8989

90-
let modifiers = modifiers_from_event(&event);
90+
let modifiers = modifiers_from_kb_event(&event);
9191
runner.input.raw.modifiers = modifiers;
9292

9393
let key = event.key();
@@ -158,7 +158,7 @@ pub(crate) fn install_document_events(runner_ref: &WebRunner) -> Result<(), JsVa
158158
&document,
159159
"keyup",
160160
|event: web_sys::KeyboardEvent, runner| {
161-
let modifiers = modifiers_from_event(&event);
161+
let modifiers = modifiers_from_kb_event(&event);
162162
runner.input.raw.modifiers = modifiers;
163163
if let Some(key) = translate_key(&event.key()) {
164164
runner.input.raw.events.push(egui::Event::Key {
@@ -301,6 +301,8 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
301301
&canvas,
302302
"mousedown",
303303
|event: web_sys::MouseEvent, runner: &mut AppRunner| {
304+
let modifiers = modifiers_from_mouse_event(&event);
305+
runner.input.raw.modifiers = modifiers;
304306
if let Some(button) = button_from_mouse_event(&event) {
305307
let pos = pos_from_mouse_event(runner.canvas_id(), &event);
306308
let modifiers = runner.input.raw.modifiers;
@@ -327,6 +329,8 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
327329
&canvas,
328330
"mousemove",
329331
|event: web_sys::MouseEvent, runner| {
332+
let modifiers = modifiers_from_mouse_event(&event);
333+
runner.input.raw.modifiers = modifiers;
330334
let pos = pos_from_mouse_event(runner.canvas_id(), &event);
331335
runner.input.raw.events.push(egui::Event::PointerMoved(pos));
332336
runner.needs_repaint.repaint_asap();
@@ -336,6 +340,8 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
336340
)?;
337341

338342
runner_ref.add_event_listener(&canvas, "mouseup", |event: web_sys::MouseEvent, runner| {
343+
let modifiers = modifiers_from_mouse_event(&event);
344+
runner.input.raw.modifiers = modifiers;
339345
if let Some(button) = button_from_mouse_event(&event) {
340346
let pos = pos_from_mouse_event(runner.canvas_id(), &event);
341347
let modifiers = runner.input.raw.modifiers;
@@ -474,7 +480,7 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
474480

475481
// Report a zoom event in case CTRL (on Windows or Linux) or CMD (on Mac) is pressed.
476482
// This if-statement is equivalent to how `Modifiers.command` is determined in
477-
// `modifiers_from_event()`, but we cannot directly use that fn for a [`WheelEvent`].
483+
// `modifiers_from_kb_event()`, but we cannot directly use that fn for a [`WheelEvent`].
478484
if event.ctrl_key() || event.meta_key() {
479485
let factor = (delta.y / 200.0).exp();
480486
runner.input.raw.events.push(egui::Event::Zoom(factor));

crates/eframe/src/web/input.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,23 @@ pub fn translate_key(key: &str) -> Option<egui::Key> {
115115
egui::Key::from_name(key)
116116
}
117117

118-
pub fn modifiers_from_event(event: &web_sys::KeyboardEvent) -> egui::Modifiers {
118+
pub fn modifiers_from_kb_event(event: &web_sys::KeyboardEvent) -> egui::Modifiers {
119+
egui::Modifiers {
120+
alt: event.alt_key(),
121+
ctrl: event.ctrl_key(),
122+
shift: event.shift_key(),
123+
124+
// Ideally we should know if we are running or mac or not,
125+
// but this works good enough for now.
126+
mac_cmd: event.meta_key(),
127+
128+
// Ideally we should know if we are running or mac or not,
129+
// but this works good enough for now.
130+
command: event.ctrl_key() || event.meta_key(),
131+
}
132+
}
133+
134+
pub fn modifiers_from_mouse_event(event: &web_sys::MouseEvent) -> egui::Modifiers {
119135
egui::Modifiers {
120136
alt: event.alt_key(),
121137
ctrl: event.ctrl_key(),

0 commit comments

Comments
 (0)