Skip to content

Ignore mouse events without any button state changes #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions editor/src/input/input_preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ impl MessageHandler<InputPreprocessorMessage, ()> for InputPreprocessor {
}
InputPreprocessorMessage::MouseDown(state, modifier_keys) => {
self.handle_modifier_keys(modifier_keys, responses);
responses.push_back(self.translate_mouse_event(state, KeyPosition::Pressed));
if let Some(message) = self.translate_mouse_event(state, KeyPosition::Pressed) {
responses.push_back(message);
}
}
InputPreprocessorMessage::MouseUp(state, modifier_keys) => {
self.handle_modifier_keys(modifier_keys, responses);
responses.push_back(self.translate_mouse_event(state, KeyPosition::Released));
if let Some(message) = self.translate_mouse_event(state, KeyPosition::Released) {
responses.push_back(message);
}
}
InputPreprocessorMessage::KeyDown(key, modifier_keys) => {
self.handle_modifier_keys(modifier_keys, responses);
Expand Down Expand Up @@ -92,23 +96,24 @@ impl MessageHandler<InputPreprocessorMessage, ()> for InputPreprocessor {
}

impl InputPreprocessor {
fn translate_mouse_event(&mut self, new_state: MouseState, position: KeyPosition) -> Message {
fn translate_mouse_event(&mut self, new_state: MouseState, position: KeyPosition) -> Option<Message> {
// Calculate the difference between the two key states (binary xor)
let diff = self.mouse.mouse_keys ^ new_state.mouse_keys;
self.mouse = new_state;
let key = match diff {
MouseKeys::LEFT => Key::Lmb,
MouseKeys::RIGHT => Key::Rmb,
MouseKeys::MIDDLE => Key::Mmb,
MouseKeys::NONE => return None, // self.mouse.mouse_keys was invalid, e.g. when a drag began outside the client
_ => {
log::warn!("The number of buttons modified at the same time was not equal to 1. Modification: {:#010b}", diff);
log::warn!("The number of buttons modified at the same time was greater than 1. Modification: {:#010b}", diff);
Key::UnknownKey
}
};
match position {
Some(match position {
KeyPosition::Pressed => InputMapperMessage::KeyDown(key).into(),
KeyPosition::Released => InputMapperMessage::KeyUp(key).into(),
}
})
}

fn handle_modifier_keys(&mut self, modifier_keys: ModifierKeys, responses: &mut VecDeque<Message>) {
Expand Down
1 change: 1 addition & 0 deletions editor/src/input/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ bitflags! {
const LEFT = 0b0000_0001;
const RIGHT = 0b0000_0010;
const MIDDLE = 0b0000_0100;
const NONE = 0b0000_0000;
}
}