Remove duplication in dispatch_pending_point_input_events (#37426)

Follow up to:
https://github.com/servo/servo/pull/37285#pullrequestreview-2916829212,
this PR removes duplicate code.
cc: @xiaochengh

---------

Signed-off-by: batu_hoang <longvatrong111@gmail.com>
This commit is contained in:
batu_hoang 2025-06-15 13:32:13 +08:00 committed by GitHub
parent 5d42ab05fb
commit 810c0e6891
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 50 deletions

View file

@ -658,6 +658,7 @@ impl IOCompositor {
webview_renderer.dispatch_point_input_event(
InputEvent::MouseButton(MouseButtonEvent::new(action, button, point))
.with_webdriver_message_id(message_id),
false,
);
},
@ -671,6 +672,7 @@ impl IOCompositor {
webview_renderer.dispatch_point_input_event(
InputEvent::MouseMove(MouseMoveEvent::new(point))
.with_webdriver_message_id(message_id),
false,
);
},
@ -699,6 +701,7 @@ impl IOCompositor {
webview_renderer.dispatch_point_input_event(
InputEvent::Wheel(WheelEvent::new(delta, point))
.with_webdriver_message_id(message_id),
false,
);
webview_renderer.on_webdriver_wheel_action(scroll_delta, point);
},

View file

@ -315,7 +315,14 @@ impl WebViewRenderer {
}
}
pub(crate) fn dispatch_point_input_event(&mut self, mut event: InputEvent) -> bool {
/// This function performs hit testing for point input events
/// before dispatching them to the constellation.
/// Return `true` if the event was successfully dispatched.
///
/// We may want to retry the event if the hit test failed due to
/// epoch mismatch. To avoid infinite loops, we only retry once.
/// The `retried` parameter indicates whether the event has already been retried.
pub(crate) fn dispatch_point_input_event(&self, mut event: InputEvent, retried: bool) -> bool {
// Events that do not need to do hit testing are sent directly to the
// constellation to filter down.
let Some(point) = event.point() else {
@ -324,7 +331,9 @@ impl WebViewRenderer {
// Delay the event if the epoch is not synchronized yet (new frame is not ready),
// or hit test result would fail and the event is rejected anyway.
if !self.webrender_frame_ready.get() || !self.pending_point_input_events.borrow().is_empty()
if !(self.webrender_frame_ready.get() &&
self.pending_point_input_events.borrow().is_empty() &&
retried)
{
self.pending_point_input_events
.borrow_mut()
@ -341,9 +350,12 @@ impl WebViewRenderer {
{
Ok(hit_test_results) => hit_test_results,
Err(HitTestError::EpochMismatch) => {
self.pending_point_input_events
.borrow_mut()
.push_back(event);
if !retried {
self.pending_point_input_events
.borrow_mut()
.push_back(event);
}
return false;
},
_ => {
@ -371,42 +383,21 @@ impl WebViewRenderer {
}
}
// TODO: This function duplicates a lot of `dispatch_point_input_event.
// Perhaps it should just be called here instead.
pub(crate) fn dispatch_pending_point_input_events(&self) {
while let Some(mut event) = self.pending_point_input_events.borrow_mut().pop_front() {
// Events that do not need to do hit testing are sent directly to the
// constellation to filter down.
let Some(point) = event.point() else {
continue;
loop {
let event = {
let mut pending = self.pending_point_input_events.borrow_mut();
pending.pop_front()
};
// If we can't find a pipeline to send this event to, we cannot continue.
let get_pipeline_details = |pipeline_id| self.pipelines.get(&pipeline_id);
let Ok(result) = self
.global
.borrow()
.hit_test_at_point(point, get_pipeline_details)
else {
// Don't need to process pending input events in this frame any more.
// TODO: Add multiple retry later if needed.
let event = match event {
Some(ev) => ev,
None => break,
};
// We don't need to process more if 1 event failed to dispatch.
// All events are point events
if !self.dispatch_point_input_event(event, true) {
return;
};
match event {
InputEvent::Touch(ref mut touch_event) => {
touch_event.init_sequence_id(self.touch_handler.current_sequence_id);
},
InputEvent::MouseButton(_) | InputEvent::MouseMove(_) | InputEvent::Wheel(_) => {
self.global.borrow_mut().update_cursor(point, &result);
},
_ => unreachable!("Unexpected input event type: {event:?}"),
}
if let Err(error) = self.global.borrow().constellation_sender.send(
EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, Some(result)),
) {
warn!("Sending event to constellation failed ({error:?}).");
}
}
}
@ -477,11 +468,11 @@ impl WebViewRenderer {
}
}
self.dispatch_point_input_event(event);
self.dispatch_point_input_event(event, false);
}
fn send_touch_event(&mut self, event: TouchEvent) -> bool {
self.dispatch_point_input_event(InputEvent::Touch(event))
self.dispatch_point_input_event(InputEvent::Touch(event), false)
}
pub(crate) fn on_touch_event(&mut self, event: TouchEvent) {
@ -745,17 +736,19 @@ impl WebViewRenderer {
/// <http://w3c.github.io/touch-events/#mouse-events>
fn simulate_mouse_click(&mut self, point: DevicePoint) {
let button = MouseButton::Left;
self.dispatch_point_input_event(InputEvent::MouseMove(MouseMoveEvent::new(point)));
self.dispatch_point_input_event(InputEvent::MouseButton(MouseButtonEvent::new(
MouseButtonAction::Down,
button,
point,
)));
self.dispatch_point_input_event(InputEvent::MouseButton(MouseButtonEvent::new(
MouseButtonAction::Up,
button,
point,
)));
self.dispatch_point_input_event(InputEvent::MouseMove(MouseMoveEvent::new(point)), false);
self.dispatch_point_input_event(
InputEvent::MouseButton(MouseButtonEvent::new(
MouseButtonAction::Down,
button,
point,
)),
false,
);
self.dispatch_point_input_event(
InputEvent::MouseButton(MouseButtonEvent::new(MouseButtonAction::Up, button, point)),
false,
);
}
pub(crate) fn notify_scroll_event(