From f495ad32f84684637a64cf0414f46cbfd1406bd9 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 5 Jun 2024 18:05:48 +0200 Subject: [PATCH] Fix pinch-to-zoom on web by using the "artificial" modifier keys (#4619) * Introduced in https://github.com/emilk/egui/pull/4524 * Closes https://github.com/emilk/egui/issues/4615 --- crates/eframe/src/web/events.rs | 5 ++++- crates/eframe/src/web/input.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index b2f371bde3ee..0fa3f69a6f2a 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -500,7 +500,10 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu }; // delta sign is flipped to match native (winit) convention. let delta = -egui::vec2(event.delta_x() as f32, event.delta_y() as f32); - let modifiers = runner.input.raw.modifiers; + + // NOTE: pinch-to-zoom on a trackpad will set the `ctrl` modifier on the event, + // even though the user is not holding down ctrl! + let modifiers = modifiers_from_wheel_event(&event); runner.input.raw.events.push(egui::Event::MouseWheel { unit, diff --git a/crates/eframe/src/web/input.rs b/crates/eframe/src/web/input.rs index 361e8031d407..a1b6a802dadd 100644 --- a/crates/eframe/src/web/input.rs +++ b/crates/eframe/src/web/input.rs @@ -156,3 +156,19 @@ pub fn modifiers_from_mouse_event(event: &web_sys::MouseEvent) -> egui::Modifier command: event.ctrl_key() || event.meta_key(), } } + +pub fn modifiers_from_wheel_event(event: &web_sys::WheelEvent) -> egui::Modifiers { + egui::Modifiers { + alt: event.alt_key(), + ctrl: event.ctrl_key(), + shift: event.shift_key(), + + // Ideally we should know if we are running or mac or not, + // but this works good enough for now. + mac_cmd: event.meta_key(), + + // Ideally we should know if we are running or mac or not, + // but this works good enough for now. + command: event.ctrl_key() || event.meta_key(), + } +}