diff --git a/Cargo.toml b/Cargo.toml index fa0952a..3d6bf7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,6 @@ egui = "0.16" gl = { version = "0.14" } keyboard-types = { version = "0.6.1", default-features = false } # TODO: Move this back to RustAudio/baseview once the PR gets merged -baseview = { git = "https://github.com/robbert-vdh/baseview.git", branch = "feature/merge-raw-gl-context", features = ["opengl"] } +baseview = { git = "https://github.com/robbert-vdh/baseview.git", branch = "feature/mouse-event-modifiers", features = ["opengl"] } raw-window-handle = "0.4" copypasta = "0.7.1" diff --git a/src/window.rs b/src/window.rs index 6a6fcfa..2ed4d01 100644 --- a/src/window.rs +++ b/src/window.rs @@ -3,6 +3,7 @@ use baseview::{ Event, EventStatus, Window, WindowHandle, WindowHandler, WindowOpenOptions, WindowScalePolicy, }; use copypasta::ClipboardProvider; +use keyboard_types::Modifiers; use raw_window_handle::HasRawWindowHandle; use std::time::Instant; @@ -306,6 +307,13 @@ where ) } } + + /// Update the pressed key modifiers when a mouse event has sent a new set of modifiers. + fn update_modifiers(&mut self, modifiers: &Modifiers) { + self.raw_input.modifiers.alt = !(*modifiers & Modifiers::ALT).is_empty(); + self.raw_input.modifiers.shift = !(*modifiers & Modifiers::SHIFT).is_empty(); + self.raw_input.modifiers.command = !(*modifiers & Modifiers::CONTROL).is_empty(); + } } impl WindowHandler for EguiWindow @@ -365,12 +373,19 @@ where fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus { match &event { baseview::Event::Mouse(event) => match event { - baseview::MouseEvent::CursorMoved { position } => { + baseview::MouseEvent::CursorMoved { + position, + modifiers, + } => { + self.update_modifiers(modifiers); + let pos = pos2(position.x as f32, position.y as f32); self.mouse_pos = Some(pos); self.raw_input.events.push(egui::Event::PointerMoved(pos)); } - baseview::MouseEvent::ButtonPressed(button) => { + baseview::MouseEvent::ButtonPressed { button, modifiers } => { + self.update_modifiers(modifiers); + if let Some(pos) = self.mouse_pos { if let Some(button) = translate_mouse_button(*button) { self.raw_input.events.push(egui::Event::PointerButton { @@ -382,7 +397,9 @@ where } } } - baseview::MouseEvent::ButtonReleased(button) => { + baseview::MouseEvent::ButtonReleased { button, modifiers } => { + self.update_modifiers(modifiers); + if let Some(pos) = self.mouse_pos { if let Some(button) = translate_mouse_button(*button) { self.raw_input.events.push(egui::Event::PointerButton { @@ -394,7 +411,12 @@ where } } } - baseview::MouseEvent::WheelScrolled(scroll_delta) => { + baseview::MouseEvent::WheelScrolled { + delta: scroll_delta, + modifiers, + } => { + self.update_modifiers(modifiers); + let mut delta = match scroll_delta { baseview::ScrollDelta::Lines { x, y } => { let points_per_scroll_line = 50.0; // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461