From 351930a20054e5399a9d5294798058a7ff718353 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 17 Mar 2022 18:16:23 +0100 Subject: [PATCH] Set modifiers on baseview mouse events Using the changes from https://github.com/RustAudio/baseview/pull/117. Until baseview implements proper keyboard focus handling this helps when dragging a knob or slider outside of the window. Otherwise the modifier state may get stuck on an incorrect value. --- baseview/Cargo.toml | 2 +- baseview/src/application.rs | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/baseview/Cargo.toml b/baseview/Cargo.toml index 225f4c1e8..141b45926 100644 --- a/baseview/Cargo.toml +++ b/baseview/Cargo.toml @@ -10,7 +10,7 @@ description = "Baseview backend for vizia" [dependencies] vizia_core = { path = "../core" } -baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "b3712638bacb3fdf2883cb5aa3f6caed0e91ac8c", features = ["opengl"] } +baseview = { git = "https://github.com/robbert-vdh/baseview.git", branch = "feature/mouse-event-modifiers", features = ["opengl"] } keyboard-types = { version = "0.6.2", default-features = false } raw-window-handle = "0.4" femtovg = { git = "https://github.com/femtovg/femtovg", rev = "87fe627794f3f793d8e3a338c838a2f8e8a8aa9d", default-features = false } diff --git a/baseview/src/application.rs b/baseview/src/application.rs index 8b8b96e60..8fe50b269 100644 --- a/baseview/src/application.rs +++ b/baseview/src/application.rs @@ -311,23 +311,31 @@ impl ApplicationRunner { match event { baseview::Event::Mouse(event) => match event { - baseview::MouseEvent::CursorMoved { position } => { + baseview::MouseEvent::CursorMoved { position, modifiers } => { + self.update_modifiers(modifiers); + let physical_posx = position.x * self.context.style().dpi_factor; let physical_posy = position.y * self.context.style().dpi_factor; let cursorx = (physical_posx) as f32; let cursory = (physical_posy) as f32; self.context.dispatch_system_event(WindowEvent::MouseMove(cursorx, cursory)); } - baseview::MouseEvent::ButtonPressed(button) => { + baseview::MouseEvent::ButtonPressed { button, modifiers } => { + self.update_modifiers(modifiers); + let b = translate_mouse_button(button); self.context.dispatch_system_event(WindowEvent::MouseDown(b)); } - baseview::MouseEvent::ButtonReleased(button) => { + baseview::MouseEvent::ButtonReleased { button, modifiers } => { + self.update_modifiers(modifiers); + let b = translate_mouse_button(button); self.context.dispatch_system_event(WindowEvent::MouseUp(b)); } - baseview::MouseEvent::WheelScrolled(scroll_delta) => { - let (lines_x, lines_y) = match scroll_delta { + baseview::MouseEvent::WheelScrolled { delta, modifiers } => { + self.update_modifiers(modifiers); + + let (lines_x, lines_y) = match delta { baseview::ScrollDelta::Lines { x, y } => (x, y), baseview::ScrollDelta::Pixels { x, y } => ( if x < 0.0 { @@ -464,6 +472,21 @@ impl ApplicationRunner { (idle_callback)(&mut self.context); } } + + fn update_modifiers(&mut self, modifiers: keyboard_types::Modifiers) { + self.context + .modifiers_mut() + .set(Modifiers::SHIFT, modifiers.contains(keyboard_types::Modifiers::SHIFT)); + self.context + .modifiers_mut() + .set(Modifiers::CTRL, modifiers.contains(keyboard_types::Modifiers::CONTROL)); + self.context + .modifiers_mut() + .set(Modifiers::LOGO, modifiers.contains(keyboard_types::Modifiers::META)); + self.context + .modifiers_mut() + .set(Modifiers::ALT, modifiers.contains(keyboard_types::Modifiers::ALT)); + } } /// Returns true if the provided event should cause an [`Application`] to