diff --git a/components/compositing/webview_renderer.rs b/components/compositing/webview_renderer.rs index d810e3286fb..108ab13c95c 100644 --- a/components/compositing/webview_renderer.rs +++ b/components/compositing/webview_renderer.rs @@ -345,7 +345,22 @@ impl WebViewRenderer { } } - pub(crate) fn dispatch_point_input_event(&mut self, mut event: InputEvent) -> bool { + pub(crate) fn dispatch_point_input_event(&self, event: InputEvent) -> bool { + self.dispatch_point_input_event_internal(event, true) + } + + pub(crate) fn dispatch_pending_point_input_events(&self) { + while let Some(event) = self.pending_point_input_events.borrow_mut().pop_front() { + // TODO: Add multiple retry later if needed. + self.dispatch_point_input_event_internal(event, false); + } + } + + pub(crate) fn dispatch_point_input_event_internal( + &self, + mut event: InputEvent, + retry_on_error: 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 { @@ -354,7 +369,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 retry_on_error && + (!self.webrender_frame_ready.get() || + !self.pending_point_input_events.borrow().is_empty()) { self.pending_point_input_events .borrow_mut() @@ -369,16 +386,14 @@ impl WebViewRenderer { .borrow() .hit_test_at_point(point, get_pipeline_details) { - Ok(hit_test_results) => hit_test_results, - Err(HitTestError::EpochMismatch) => { + Ok(hit_test_results) => Some(hit_test_results), + Err(HitTestError::EpochMismatch) if retry_on_error => { self.pending_point_input_events .borrow_mut() - .push_back(event); - return false; - }, - _ => { + .push_back(event.clone()); return false; }, + _ => None, }; match event { @@ -389,15 +404,19 @@ impl WebViewRenderer { InputEvent::MouseLeave(_) | InputEvent::MouseMove(_) | InputEvent::Wheel(_) => { - self.global - .borrow_mut() - .update_cursor_from_hittest(point, &result); + if let Some(ref result) = result { + self.global + .borrow_mut() + .update_cursor_from_hittest(point, result); + } else { + warn!("Not hit test result."); + } }, _ => unreachable!("Unexpected input event type: {event:?}"), } if let Err(error) = self.global.borrow().constellation_sender.send( - EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, Some(result)), + EmbedderToConstellationMessage::ForwardInputEvent(self.id, event, result), ) { warn!("Sending event to constellation failed ({error:?})."); false @@ -406,51 +425,6 @@ 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; - }; - - // 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. - return; - }; - - match event { - InputEvent::Touch(ref mut touch_event) => { - touch_event.init_sequence_id(self.touch_handler.current_sequence_id); - }, - InputEvent::MouseButton(_) | - InputEvent::MouseLeave(_) | - InputEvent::MouseMove(_) | - InputEvent::Wheel(_) => { - self.global - .borrow_mut() - .update_cursor_from_hittest(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:?})."); - } - } - } - pub(crate) fn notify_input_event(&mut self, event: InputEvent) { if self.global.borrow().shutdown_state() != ShutdownState::NotShuttingDown { return; diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 2b2613e0b6e..2c5e5faa5b8 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -2246,6 +2246,7 @@ impl Document { ) -> TouchEventResult { // Ignore all incoming events without a hit test. let Some(hit_test_result) = hit_test_result else { + self.update_active_touch_points_when_early_return(event); return TouchEventResult::Forwarded; }; @@ -2263,6 +2264,7 @@ impl Document { .filter_map(DomRoot::downcast::) .next() else { + self.update_active_touch_points_when_early_return(event); return TouchEventResult::Forwarded; }; @@ -2348,6 +2350,34 @@ impl Document { } } + // If hittest fails, we still need to update the active point information. + fn update_active_touch_points_when_early_return(&self, event: TouchEvent) { + match event.event_type { + TouchEventType::Down => { + // If the touchdown fails, we don't need to do anything. + // When a touchmove or touchdown occurs at that touch point, + // a warning is triggered: Got a touchmove/touchend event for a non-active touch point + }, + TouchEventType::Move => { + // The failure of touchmove does not affect the number of active points. + // Since there is no position information when it fails, we do not need to update. + }, + TouchEventType::Up | TouchEventType::Cancel => { + // Remove an existing touch point + let mut active_touch_points = self.active_touch_points.borrow_mut(); + match active_touch_points + .iter() + .position(|t| t.Identifier() == event.id.0) + { + Some(i) => { + active_touch_points.swap_remove(i); + }, + None => warn!("Got a touchend event for a non-active touch point"), + } + }, + } + } + #[allow(unsafe_code)] pub(crate) fn handle_scroll_event(&self, event: ScrollEvent, can_gc: CanGc) { //