Skip to content

Commit

Permalink
Set modifiers on baseview mouse events
Browse files Browse the repository at this point in the history
Using the changes from RustAudio/baseview#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.
  • Loading branch information
robbert-vdh committed Apr 26, 2022
1 parent 80fc054 commit 82d2034
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion baseview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
33 changes: 28 additions & 5 deletions baseview/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,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 {
Expand Down Expand Up @@ -488,6 +496,21 @@ impl ApplicationRunner {
(idle_callback)(&mut self.context);
}
}

fn update_modifiers(&mut self, modifiers: keyboard_types::Modifiers) {
self.context
.modifiers
.set(Modifiers::SHIFT, modifiers.contains(keyboard_types::Modifiers::SHIFT));
self.context
.modifiers
.set(Modifiers::CTRL, modifiers.contains(keyboard_types::Modifiers::CONTROL));
self.context
.modifiers
.set(Modifiers::LOGO, modifiers.contains(keyboard_types::Modifiers::META));
self.context
.modifiers
.set(Modifiers::ALT, modifiers.contains(keyboard_types::Modifiers::ALT));
}
}

/// Returns true if the provided event should cause an [`Application`] to
Expand Down

0 comments on commit 82d2034

Please sign in to comment.