touch: Fix panic with -Zconvert-mouse-to-touch (#36531)

- We previously converted all mouse move events to touch events, but we
should only be doing that while a mouse button is pressed (a finger
always does touch-down -> move -> up / cancel)
- Only consider Left mouse button for mouse-to-touch. We currently
already hardcode TouchId 0, which we would need to change in order to
properly support Multi-touch. Since we don't have any Multi-touch
gestures at the moment, we leave this unimplemented and simply only
evaluate the left mouse button.

Testing:  Manual testing. We don't have servodriver support yet.
Fixes: #36526

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
Jonathan Schwender 2025-04-15 11:59:30 +02:00 committed by GitHub
parent fe4306fc30
commit 1ea80c4335
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 17 deletions

View file

@ -282,6 +282,10 @@ impl TouchHandler {
debug_assert!(old.is_some(), "Sequence already removed?"); debug_assert!(old.is_some(), "Sequence already removed?");
} }
pub fn try_get_current_touch_sequence(&self) -> Option<&TouchSequenceInfo> {
self.touch_sequence_map.get(&self.current_sequence_id)
}
pub fn get_current_touch_sequence_mut(&mut self) -> &mut TouchSequenceInfo { pub fn get_current_touch_sequence_mut(&mut self) -> &mut TouchSequenceInfo {
self.touch_sequence_map self.touch_sequence_map
.get_mut(&self.current_sequence_id) .get_mut(&self.current_sequence_id)

View file

@ -338,27 +338,53 @@ impl WebView {
if self.global.borrow().convert_mouse_to_touch { if self.global.borrow().convert_mouse_to_touch {
match event { match event {
InputEvent::MouseButton(event) => { InputEvent::MouseButton(event) => {
match event.action { match (event.button, event.action) {
MouseButtonAction::Click => {}, (MouseButton::Left, MouseButtonAction::Down) => self.on_touch_down(
MouseButtonAction::Down => self.on_touch_down(TouchEvent::new( TouchEvent::new(TouchEventType::Down, TouchId(0), event.point),
TouchEventType::Down, ),
TouchId(0), (MouseButton::Left, MouseButtonAction::Up) => self.on_touch_up(
event.point, TouchEvent::new(TouchEventType::Up, TouchId(0), event.point),
)), ),
MouseButtonAction::Up => self.on_touch_up(TouchEvent::new( _ => {},
TouchEventType::Up,
TouchId(0),
event.point,
)),
} }
return; return;
}, },
InputEvent::MouseMove(event) => { InputEvent::MouseMove(event) => {
self.on_touch_move(TouchEvent::new( if let Some(state) = self.touch_handler.try_get_current_touch_sequence() {
TouchEventType::Move, // We assume that the debug option `-Z convert-mouse-to-touch` will only
TouchId(0), // be used on devices without native touch input, so we can directly
event.point, // reuse the touch handler for tracking the state of pressed buttons.
)); match state.state {
TouchSequenceState::Touching | TouchSequenceState::Panning { .. } => {
self.on_touch_move(TouchEvent::new(
TouchEventType::Move,
TouchId(0),
event.point,
));
},
TouchSequenceState::MultiTouch => {
// Multitouch simulation currently is not implemented.
// Since we only get one mouse move event, we would need to
// dispatch one mouse move event per currently pressed mouse button.
},
TouchSequenceState::Pinching => {
// We only have one mouse button, so Pinching should be impossible.
#[cfg(debug_assertions)]
log::error!(
"Touch handler is in Pinching state, which should be unreachable with \
-Z convert-mouse-to-touch debug option."
);
},
TouchSequenceState::PendingFling { .. } |
TouchSequenceState::Flinging { .. } |
TouchSequenceState::PendingClick(_) |
TouchSequenceState::Finished => {
// Mouse movement without a button being pressed is not
// translated to touch events.
},
}
}
// We don't want to (directly) dispatch mouse events when simulating touch input.
return; return;
}, },
_ => {}, _ => {},