Ignore mouse events without any button state changes (#343)

This commit is contained in:
Henry Sloan 2021-08-12 01:26:46 -04:00 committed by GitHub
parent 02fd00da10
commit da77820c82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View file

@ -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);
@ -92,7 +96,7 @@ 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;
@ -100,15 +104,16 @@ impl InputPreprocessor {
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>) {

View file

@ -58,5 +58,6 @@ bitflags! {
const LEFT = 0b0000_0001;
const RIGHT = 0b0000_0010;
const MIDDLE = 0b0000_0100;
const NONE = 0b0000_0000;
}
}