Skip to content

Commit

Permalink
Use the new baseview mouse modifiers
Browse files Browse the repository at this point in the history
Which should fix RustAudio/baseview#116 for
the use case of Shift+dragging a slider.
  • Loading branch information
robbert-vdh committed Feb 9, 2022
1 parent 240be38 commit 3893ef0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
30 changes: 26 additions & 4 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<State, U> WindowHandler for EguiWindow<State, U>
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 3893ef0

Please sign in to comment.