Truly independent web browser
Compositor: Time touchpad steps as they arrive from the UI
Aliaksandr Kalenik8460acb4f2aa
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex bb63f490d0b..f608f9658f8 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -543,6 +543,8 @@ void CompositorState::handle_and_dispatch_mouse_event(Web::CompositorContextId cauto is_wheel_event = event.type == Web::MouseEvent::Type::MouseWheel;if (is_wheel_event) {+ context->note_wheel_event_from_ui(event);+// The UI can send the end of a gesture more than once. An end that arrives during a fling belongs to the// flicked gesture, so it does not stop the fling.if (event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended && context->has_active_scroll_fling()) {diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cppindex 1f77cfa8692..3d703289f86 100644--- a/Services/Compositor/ContextState.cpp+++ b/Services/Compositor/ContextState.cpp@@ -772,12 +772,9 @@ ContextState::WheelScrollOutcome ContextState::perform_wheel_scroll_of_node(Comp};auto stretch_before_step = presented_stretch_of_latched_scroller();- if (step_can_stretch_elastically) {- m_elastic_overscroll_velocity_tracker.add_step(step_delta, now);- // Momentum only arrives once the fingers have lifted, and its first step can be the first sign that they did.- if (scroll_gesture_phase == Web::ScrollGesturePhase::Momentum)- release_elastic_overscrolls_held_by_fingers(now);- }+ // Momentum only arrives once the fingers have lifted, and its first step can be the first sign that they did.+ if (step_can_stretch_elastically && scroll_gesture_phase == Web::ScrollGesturePhase::Momentum)+ release_elastic_overscrolls_held_by_fingers(now);if (momentum_can_bounce_latched_scroller && elastic_overscroll_of(latched_node->stable_node_id)) {// The fingers lifted from a stretched scroller, which this momentum already springs back with.m_wheel_scroll_latch->momentum_has_sprung_the_scroller_back = true;@@ -1182,20 +1179,47 @@ ContextState::ContextUpdateResult ContextState::handle_wheel_event(Web::MouseEvereturn scroll_for_wheel_event(event, now);}-ContextState::ContextUpdateResult ContextState::scroll_for_wheel_event(Web::MouseEvent const& event, MonotonicTime now)+static Gfx::FloatPoint wheel_event_position(Web::MouseEvent const& event)+{+ return {+ static_cast<float>(event.position.x().value()),+ static_cast<float>(event.position.y().value()),+ };+}++Gfx::FloatPoint ContextState::wheel_event_delta_in_device_pixels(Web::MouseEvent const& event) const{auto wheel_delta_x = event.wheel_delta_x;auto wheel_delta_y = event.wheel_delta_y;if (event.modifiers & Web::UIEvents::KeyModifier::Mod_Shift)swap(wheel_delta_x, wheel_delta_y);+ return Gfx::FloatPoint { static_cast<float>(wheel_delta_x), static_cast<float>(wheel_delta_y) }+ .scaled(static_cast<float>(m_async_scroll_tree.device_pixels_per_css_pixel()));+}- auto position = Gfx::FloatPoint {- static_cast<float>(event.position.x().value()),- static_cast<float>(event.position.y().value()),- };- auto delta_in_device_pixels = Gfx::FloatPoint { static_cast<float>(wheel_delta_x), static_cast<float>(wheel_delta_y) }- .scaled(static_cast<float>(m_async_scroll_tree.device_pixels_per_css_pixel()));- return async_scroll_by(position, delta_in_device_pixels, event.wheel_delta_precision, event.scroll_gesture_phase, event.modifiers, now);+ContextState::ContextUpdateResult ContextState::scroll_for_wheel_event(Web::MouseEvent const& event, MonotonicTime now)+{+ return async_scroll_by(wheel_event_position(event), wheel_event_delta_in_device_pixels(event), event.wheel_delta_precision, event.scroll_gesture_phase, event.modifiers, now);+}++void ContextState::note_wheel_event_from_ui(Web::MouseEvent const& event, Optional<MonotonicTime> now_for_testing)+{+ VERIFY(event.type == Web::MouseEvent::Type::MouseWheel);+ auto now = now_for_testing.value_or(MonotonicTime::now());+ auto position = wheel_event_position(event);+ auto phase = event.scroll_gesture_phase;+ auto position_slop_in_device_pixels = Compositing::wheel_gesture_position_slop_in_css_pixels * static_cast<float>(m_async_scroll_tree.device_pixels_per_css_pixel());+ if (m_wheel_gesture_from_ui.has_value() && m_wheel_gesture_from_ui->is_continued_by(position, phase, event.modifiers, now, position_slop_in_device_pixels)) {+ m_wheel_gesture_from_ui->advance_to(phase, now);+ } else {+ // The end of the gesture before this one may never have arrived.+ end_gesture_that_stretches_elastically(now);+ m_wheel_gesture_from_ui = Compositing::WheelGestureIdentity::started_by(position, phase, event.modifiers, now);+ }++ // Fling steps are not timed here, which leaves them out of the velocity; no platform both flings and stretches.+ if (wheel_step_can_stretch_elastically(event.wheel_delta_precision, phase))+ m_elastic_overscroll_velocity_tracker.add_step(wheel_event_delta_in_device_pixels(event).scaled(1.0f / visual_viewport_magnification()), now);}static bool is_touchpad_step(Web::MouseEvent const& event)@@ -1321,9 +1345,6 @@ ContextState::ContextUpdateResult ContextState::async_scroll_by(Gfx::FloatPointm_scroll_snap_controller.note_gesture_phase(scroll_gesture_phase);auto latched_node_id = resolve_wheel_scroll_latch(position, scroll_gesture_phase, modifiers, now);- // The end of the gesture before this one may never have arrived.- if (!latched_node_id.has_value())- end_gesture_that_stretches_elastically(now);if (scroll_gesture_phase == Web::ScrollGesturePhase::Ended && delta.is_zero()) {if (!snap_at_gesture_end(now).has_value())diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.hindex 51fb94f174e..da403e0bf2e 100644--- a/Services/Compositor/ContextState.h+++ b/Services/Compositor/ContextState.h@@ -124,6 +124,9 @@ public:ContextUpdateResult handle_mouse_event(Web::MouseEvent const&);// The UI's wheel deltas are in CSS pixels; this scales them for the scroll tree, which works in device pixels.ContextUpdateResult handle_wheel_event(Web::MouseEvent const&, Optional<MonotonicTime> now_for_testing = {});+ // Takes note of a wheel event from the UI as it arrives, ahead of anything that may turn it away or leave it to+ // the main thread.+ void note_wheel_event_from_ui(Web::MouseEvent const&, Optional<MonotonicTime> now_for_testing = {});ContextUpdateResult handle_pinch_event(Web::PinchEvent const&);AsyncScrollResult async_scroll_by(Web::UniqueNodeID document_id,@@ -338,6 +341,7 @@ private:// The scroll offsets to present when a scroller is stretched, which are the only ones that leave a scroll range.Optional<Compositing::ScrollStateSnapshot> scroll_state_snapshot_stretched_by_elastic_overscrolls() const;ContextUpdateResult scroll_for_wheel_event(Web::MouseEvent const&, MonotonicTime now);+ Gfx::FloatPoint wheel_event_delta_in_device_pixels(Web::MouseEvent const&) const;Web::MouseEvent scroll_fling_event(Gfx::FloatPoint delta, Web::ScrollGesturePhase) const;Optional<Gfx::FloatPoint> in_flight_wheel_destination(Web::AsyncScrollNodeStableID) const;AsyncScrollResult smooth_wheel_scroll_by(Optional<Compositing::WheelScrollTarget> const&, Gfx::IntRect viewport_rect, MonotonicTime now);@@ -420,6 +424,9 @@ private:Compositing::ScrollFlingBooster m_scroll_fling_booster;Optional<ActiveScrollFling> m_scroll_fling;Vector<ActiveElasticOverscroll> m_elastic_overscrolls;+ // The touchpad gesture the UI is sending, whether this context or the main thread scrolls its steps. A stretch is+ // released with the velocity of the steps as they arrived from the UI, not as they were scrolled.+ Optional<Compositing::WheelGestureIdentity> m_wheel_gesture_from_ui;Compositing::ScrollVelocityTracker m_elastic_overscroll_velocity_tracker;bool m_scrolls_elastically { false };Compositing::AsyncScrollOperationID m_next_async_scroll_operation_id { 0 };