Skip to content

Truly independent web browser

Compositor: Move elastic overscroll state out of ContextState

Aliaksandr Kalenik9081cc9e51d9
diff --git a/Services/Compositor/CMakeLists.txt b/Services/Compositor/CMakeLists.txt
index 0e2db5da018..05896f921f4 100644
--- a/Services/Compositor/CMakeLists.txt
+++ b/Services/Compositor/CMakeLists.txt
@@ -9,6 +9,7 @@ set(SOURCES
DisplayListRasterCache.cpp
ConnectionFromClient.cpp
ConnectionFromWebContent.cpp
+ ElasticOverscrollController.cpp
FramePacer.cpp
HostWebGLContext.cpp
OpenGLContext.cpp
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 9855c17bc93..2aeca191be6 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -150,7 +150,7 @@ void ContextState::dispatch_key_event_to_web_content(Web::KeyEvent const& event)
void ContextState::stop_presenting_to_client()
{
end_keyboard_scroll_gesture();
- forget_elastic_overscrolls();
+ m_elastic_overscroll_controller.forget();
m_presents_to_client = false;
}
@@ -202,15 +202,7 @@ void ContextState::install_display_list_update(
m_scrollbar_controller.set_scrollbars(async_scrolling_state.scrollbars);
note_user_scroll_gesture_end_if_drag_ended(was_dragging_scrollbar);
m_async_scroll_tree.set_state(move(async_scrolling_state));
- m_elastic_overscrolls.remove_all_matching([&](auto const& active) {
- auto node_id = m_async_scroll_tree.scroll_node_id_for_stable_id(active.stable_node_id);
- auto const* node = node_id.has_value() ? m_async_scroll_tree.scroll_node_for_id(*node_id) : nullptr;
- if (!node)
- return true;
- // A scroller that has been told not to stretch along an axis since it was pulled lets go at once.
- auto stretch = active.overscroll.stretch();
- return (stretch.x() != 0 && !node->shows_overscroll_effect_horizontally) || (stretch.y() != 0 && !node->shows_overscroll_effect_vertically);
- });
+ m_elastic_overscroll_controller.did_install_scrolling_state(m_async_scroll_tree);
m_scroll_snap_controller.did_install_scrolling_state(m_async_scroll_tree);
if (auto viewport_scroll_offset = reapply_unreconciled_async_scroll_offsets(); viewport_scroll_offset.has_value())
async_scrolling_viewport_rect.set_location(viewport_scroll_offset->to_type<int>());
@@ -749,15 +741,14 @@ ContextState::WheelScrollOutcome ContextState::perform_wheel_scroll_of_node(Comp
auto presented_stretch_of_latched_scroller = [&]() -> Gfx::FloatPoint {
if (!latched_node)
return {};
- auto const* active = elastic_overscroll_of(latched_node->stable_node_id);
- return active ? active->overscroll.stretch() : Gfx::FloatPoint {};
+ return m_elastic_overscroll_controller.stretch_of(latched_node->stable_node_id);
};
auto stretch_before_step = presented_stretch_of_latched_scroller();
// 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)) {
+ m_elastic_overscroll_controller.release_held_by_fingers(now);
+ if (momentum_can_bounce_latched_scroller && m_elastic_overscroll_controller.has_active_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;
momentum_can_bounce_latched_scroller = false;
@@ -769,7 +760,7 @@ ContextState::WheelScrollOutcome ContextState::perform_wheel_scroll_of_node(Comp
if (fingers_can_stretch_latched_scroller) {
if (m_wheel_scroll_latch.has_value())
m_wheel_scroll_latch->momentum_has_sprung_the_scroller_back = false;
- auto step_delta_left = relax_elastic_overscroll_by(*latched_node, step_delta);
+ auto step_delta_left = m_elastic_overscroll_controller.relax_by(latched_node->stable_node_id, step_delta);
if (step_delta_left != step_delta)
delta_left = step_delta_left.scaled(magnification);
}
@@ -800,7 +791,7 @@ ContextState::WheelScrollOutcome ContextState::perform_wheel_scroll_of_node(Comp
} else if (momentum_can_bounce_latched_scroller && pull_elastic_overscroll_by(*latched_node, step_delta, *delta_the_scroller_did_not_scroll_by)) {
// Momentum that carries a scroller into its edge bounces it: the velocity it arrives with takes the
// rebound out past the edge before the rebound brings it back.
- release_elastic_overscrolls_held_by_fingers(now);
+ m_elastic_overscroll_controller.release_held_by_fingers(now);
m_wheel_scroll_latch->momentum_has_sprung_the_scroller_back = true;
}
}
@@ -919,7 +910,7 @@ ContextState::AsyncScrollResult ContextState::async_scroll_by(
// The end of a gesture lets go of the scroller it stretched even if the context has stopped taking wheel events.
if (scroll_gesture_phase == Web::ScrollGesturePhase::Ended)
- end_gesture_that_stretches_elastically(now);
+ m_elastic_overscroll_controller.end_gesture(now);
// Blocking wheel event listeners have nothing left to hold back once the page has dispatched a step and did not
// cancel it. The compositor then scrolls the step of a touchpad gesture, so that the gesture stretches the edges
@@ -935,7 +926,7 @@ ContextState::AsyncScrollResult ContextState::async_scroll_by(
// The end of the gesture before this one may never have arrived. Whether a step of the UI starts another gesture
// was decided when the UI sent it.
if (!latched_node_id.has_value() && !step_came_from_ui)
- end_gesture_that_stretches_elastically(now);
+ m_elastic_overscroll_controller.end_gesture(now);
if (!admits_async_wheel(m_wheel_routing_admission)
&& !step_continues_touchpad_gesture_the_page_let_through(latched_node_id, wheel_delta_precision, scroll_gesture_phase)
@@ -1255,13 +1246,13 @@ void ContextState::note_wheel_event_from_ui(Web::MouseEvent const& event, Option
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_elastic_overscroll_controller.end_gesture(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);
+ m_elastic_overscroll_controller.note_step_from_ui(wheel_event_delta_in_device_pixels(event).scaled(1.0f / visual_viewport_magnification()), now);
}
static bool is_touchpad_step(Web::MouseEvent const& event)
@@ -1378,7 +1369,7 @@ ContextState::ContextUpdateResult ContextState::scroll_for_wheel_event(Web::Mous
auto delta = wheel_event_delta_in_device_pixels(event);
// The end of a gesture lets go of the scroller it stretched even if the context has stopped taking wheel events.
if (event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended)
- end_gesture_that_stretches_elastically(now);
+ m_elastic_overscroll_controller.end_gesture(now);
auto latched_node_id = resolve_wheel_scroll_latch(position, event.scroll_gesture_phase, event.modifiers, now);
@@ -1432,32 +1423,7 @@ ContextState::ContextUpdateResult ContextState::scroll_for_wheel_event(Web::Mous
bool ContextState::wheel_step_can_stretch_elastically(Web::WheelDeltaPrecision wheel_delta_precision, Web::ScrollGesturePhase scroll_gesture_phase) const
{
- if (!m_scrolls_elastically || !presents_to_client())
- return false;
- // Only a touchpad gesture says when the fingers lift, which is when a stretch has to spring back.
- if (wheel_delta_precision != Web::WheelDeltaPrecision::Precise)
- return false;
- return scroll_gesture_phase == Web::ScrollGesturePhase::Ongoing || scroll_gesture_phase == Web::ScrollGesturePhase::Momentum;
-}
-
-ContextState::ActiveElasticOverscroll* ContextState::elastic_overscroll_of(Web::AsyncScrollNodeStableID stable_node_id)
-{
- for (auto& active : m_elastic_overscrolls) {
- if (active.stable_node_id == stable_node_id)
- return &active;
- }
- return nullptr;
-}
-
-Gfx::FloatPoint ContextState::relax_elastic_overscroll_by(Compositing::AsyncScrollNode const& node, Gfx::FloatPoint step_delta)
-{
- auto* active = elastic_overscroll_of(node.stable_node_id);
- if (!active)
- return step_delta;
- auto delta_left_to_scroll_with = active->overscroll.relax_by(step_delta);
- if (active->overscroll.is_at_rest())
- m_elastic_overscrolls.remove_all_matching([&](auto const& candidate) { return candidate.stable_node_id == node.stable_node_id; });
- return delta_left_to_scroll_with;
+ return presents_to_client() && m_elastic_overscroll_controller.step_can_stretch(wheel_delta_precision, scroll_gesture_phase);
}
bool ContextState::pull_elastic_overscroll_by(Compositing::AsyncScrollNode const& node, Gfx::FloatPoint step_delta, Gfx::FloatPoint delta_the_scroller_did_not_scroll_by)
@@ -1465,117 +1431,29 @@ bool ContextState::pull_elastic_overscroll_by(Compositing::AsyncScrollNode const
auto scroll_offset = m_async_scroll_tree.scroll_offset_for_node(node.node_id, m_scroll_state_snapshot);
if (!scroll_offset.has_value())
return false;
-
- // A touchpad step is rarely along one axis exactly. Only its main direction pulls, so that a vertical pan next to
- // a side edge does not creep past that edge.
- Gfx::FloatPoint overscroll_delta;
- bool step_is_mainly_vertical = AK::abs(step_delta.y()) >= AK::abs(step_delta.x());
- if (step_is_mainly_vertical) {
- auto delta_y = delta_the_scroller_did_not_scroll_by.y();
- bool is_at_the_edge_the_delta_points_past = (delta_y < 0 && scroll_offset->y() <= node.min_scroll_offset.y())
- || (delta_y > 0 && scroll_offset->y() >= node.max_scroll_offset.y());
- if (node.can_be_wheel_scrolled_vertically && node.shows_overscroll_effect_vertically && is_at_the_edge_the_delta_points_past)
- overscroll_delta.set_y(delta_y);
- } else {
- auto delta_x = delta_the_scroller_did_not_scroll_by.x();
- bool is_at_the_edge_the_delta_points_past = (delta_x < 0 && scroll_offset->x() <= node.min_scroll_offset.x())
- || (delta_x > 0 && scroll_offset->x() >= node.max_scroll_offset.x());
- if (node.can_be_wheel_scrolled_horizontally && node.shows_overscroll_effect_horizontally && is_at_the_edge_the_delta_points_past)
- overscroll_delta.set_x(delta_x);
- }
- if (overscroll_delta.is_zero())
- return false;
-
- auto* active = elastic_overscroll_of(node.stable_node_id);
- if (!active) {
- // A zoomed page shows only the part of the viewport's scrollport that its visual viewport is on, and that part
- // is what bounds how far the viewport stretches.
- auto magnification = visual_viewport_magnification();
- auto scrollport_size = node.scrollport_rect.size().to_type<float>();
- if (node.is_viewport)
- scrollport_size.scale_by(1.0f / magnification);
- m_elastic_overscrolls.append({ node.stable_node_id, Compositing::ElasticOverscroll { scrollport_size, magnification } });
- active = &m_elastic_overscrolls.last();
- }
- active->overscroll.pull_by(overscroll_delta);
- return true;
-}
-
-void ContextState::release_elastic_overscrolls_held_by_fingers(MonotonicTime now)
-{
- auto velocity = m_elastic_overscroll_velocity_tracker.velocity_at_lift(now).value_or({});
- for (auto& active : m_elastic_overscrolls)
- active.overscroll.release(velocity, now);
-}
-
-void ContextState::end_gesture_that_stretches_elastically(MonotonicTime now)
-{
- release_elastic_overscrolls_held_by_fingers(now);
- m_elastic_overscroll_velocity_tracker.reset();
-}
-
-void ContextState::forget_elastic_overscrolls()
-{
- m_elastic_overscrolls.clear();
- m_elastic_overscroll_velocity_tracker.reset();
+ return m_elastic_overscroll_controller.pull_by(node, *scroll_offset, step_delta, delta_the_scroller_did_not_scroll_by, visual_viewport_magnification());
}
void ContextState::set_scrolls_elastically(bool scrolls_elastically)
{
- m_scrolls_elastically = scrolls_elastically;
- if (!scrolls_elastically)
- forget_elastic_overscrolls();
-}
-
-bool ContextState::has_rebounding_elastic_overscroll() const
-{
- return any_of(m_elastic_overscrolls, [](auto const& active) { return active.overscroll.is_rebounding(); });
+ m_elastic_overscroll_controller.set_enabled(scrolls_elastically);
}
Optional<Gfx::IntRect> ContextState::advance_elastic_overscroll_rebounds(MonotonicTime frame_time)
{
- bool a_presented_stretch_changed = false;
- m_elastic_overscrolls.remove_all_matching([&](auto& active) {
- if (!active.overscroll.is_rebounding())
- return false;
- auto stretch_before = active.overscroll.stretch();
- bool is_still_rebounding = active.overscroll.advance_rebound_to(frame_time);
- if (active.overscroll.stretch() != stretch_before)
- a_presented_stretch_changed = true;
- return !is_still_rebounding;
- });
- if (!a_presented_stretch_changed)
+ if (!m_elastic_overscroll_controller.advance_rebounds(frame_time))
return {};
return m_async_scrolling_viewport_rect;
}
Gfx::FloatPoint ContextState::elastic_overscroll_stretch_for_testing(Web::AsyncScrollNodeStableID stable_node_id) const
{
- for (auto const& active : m_elastic_overscrolls) {
- if (active.stable_node_id == stable_node_id)
- return active.overscroll.stretch();
- }
- return {};
+ return m_elastic_overscroll_controller.stretch_of(stable_node_id);
}
Optional<Compositing::ScrollStateSnapshot> ContextState::scroll_state_snapshot_stretched_by_elastic_overscrolls() const
{
- Optional<Compositing::ScrollStateSnapshot> stretched_scroll_state_snapshot;
- for (auto const& active : m_elastic_overscrolls) {
- auto stretch = active.overscroll.stretch();
- if (stretch.is_zero())
- continue;
- auto node_id = m_async_scroll_tree.scroll_node_id_for_stable_id(active.stable_node_id);
- if (!node_id.has_value())
- continue;
- if (!stretched_scroll_state_snapshot.has_value())
- stretched_scroll_state_snapshot = m_scroll_state_snapshot;
- // Only the scroller's own entry moves. The sticky boxes in it keep the shifts resolved for the scroll offset
- // it really has, so they move with its content.
- auto index = node_id->scroll_node_index;
- stretched_scroll_state_snapshot->set_device_offset_for_index(index, stretched_scroll_state_snapshot->device_offset_for_index(index) - stretch);
- }
- return stretched_scroll_state_snapshot;
+ return m_elastic_overscroll_controller.stretched_scroll_state_snapshot(m_scroll_state_snapshot, m_async_scroll_tree);
}
Compositing::PendingAsyncScrollUpdates ContextState::take_pending_async_scroll_updates()
@@ -1801,7 +1679,7 @@ bool ContextState::set_visibility(Compositing::ContextVisibility visibility)
m_visibility = visibility;
if (visibility == Compositing::ContextVisibility::Hidden) {
end_keyboard_scroll_gesture();
- forget_elastic_overscrolls();
+ m_elastic_overscroll_controller.forget();
}
return true;
}
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index ffa9012edeb..77108be7b7d 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -17,6 +17,7 @@
#include <Compositor/AsyncScrollUpdateOutbox.h>
#include <Compositor/BackingStoreManager.h>
#include <Compositor/DisplayListRasterCache.h>
+#include <Compositor/ElasticOverscrollController.h>
#include <Compositor/Forward.h>
#include <Compositor/FramePacer.h>
#include <Compositor/ScrollSnapController.h>
@@ -28,7 +29,6 @@
#include <LibCompositing/Forward.h>
#include <LibCompositing/Scrolling/AsyncScrollTree.h>
#include <LibCompositing/Scrolling/AsyncScrollingState.h>
-#include <LibCompositing/Scrolling/ElasticOverscroll.h>
#include <LibCompositing/Scrolling/ScrollFling.h>
#include <LibCompositing/Scrolling/ScrollState.h>
#include <LibCompositing/Scrolling/SmoothScrollAnimation.h>
@@ -144,7 +144,7 @@ public:
AsyncScrollResult smooth_scroll_to(Web::AsyncScrollNodeStableID, Gfx::FloatPoint offset, Gfx::FloatPoint main_thread_offset, Gfx::IntRect viewport_rect, Compositing::ScrollAnimationKind, Compositing::SmoothScrollInitiator, Optional<Compositing::AsyncScrollOperationID> operation_to_continue);
void cancel_smooth_scroll(Web::AsyncScrollNodeStableID);
Optional<Gfx::IntRect> advance_smooth_scroll_animations(MonotonicTime now);
- bool needs_animation_frames() { return has_active_smooth_scroll_animations() || has_active_scroll_fling() || has_rebounding_elastic_overscroll() || visual_animations_need_frame(); }
+ bool needs_animation_frames() { return has_active_smooth_scroll_animations() || has_active_scroll_fling() || m_elastic_overscroll_controller.has_rebound() || visual_animations_need_frame(); }
bool has_active_scroll_fling() const { return m_scroll_fling.has_value(); }
struct ScrollFlingStep {
@@ -159,7 +159,6 @@ public:
// Ends the fling before a wheel event from a new gesture or other input.
Optional<ScrollFlingStep> interrupt_scroll_fling(Web::MouseEvent const&, Optional<MonotonicTime> now_for_testing = {});
void set_scrolls_elastically(bool);
- bool has_rebounding_elastic_overscroll() const;
Optional<Gfx::IntRect> advance_elastic_overscroll_rebounds(MonotonicTime frame_time);
Gfx::FloatPoint elastic_overscroll_stretch_for_testing(Web::AsyncScrollNodeStableID) const;
bool advance_visual_animations(MonotonicTime now);
@@ -260,13 +259,6 @@ private:
Optional<u64> wheel_event_listener_state_generation_the_page_let_the_gesture_through_at {};
};
- // A scroller that a touchpad gesture holds past an edge of its scroll range, or that is springing back from there.
- // The stretch is only presented: it is in no scroll offset that the scroll tree or WebContent sees.
- struct ActiveElasticOverscroll {
- Web::AsyncScrollNodeStableID stable_node_id;
- Compositing::ElasticOverscroll overscroll;
- };
-
struct RasterizedFrame {
NonnullRefPtr<Compositing::DisplayList const> display_list;
Compositing::AccumulatedVisualContextTree visual_context_tree;
@@ -329,16 +321,7 @@ private:
void cancel_smooth_scroll_taken_over_by_user_input(Compositing::AsyncScrollNodeID);
void track_wheel_event_for_scroll_fling(Web::MouseEvent const&, MonotonicTime now);
bool wheel_step_can_stretch_elastically(Web::WheelDeltaPrecision, Web::ScrollGesturePhase) const;
- ActiveElasticOverscroll* elastic_overscroll_of(Web::AsyncScrollNodeStableID);
- // Uses the part of a finger step that points back to the scroll range to relax the stretch of the scroller, and
- // returns the rest of the step.
- Gfx::FloatPoint relax_elastic_overscroll_by(Compositing::AsyncScrollNode const&, Gfx::FloatPoint step_delta);
- // Returns whether the scroller is at an edge that the step points past, which is what lets the step pull it.
bool pull_elastic_overscroll_by(Compositing::AsyncScrollNode const&, Gfx::FloatPoint step_delta, Gfx::FloatPoint delta_the_scroller_did_not_scroll_by);
- void release_elastic_overscrolls_held_by_fingers(MonotonicTime now);
- void end_gesture_that_stretches_elastically(MonotonicTime now);
- void forget_elastic_overscrolls();
- // 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;
@@ -414,7 +397,7 @@ private:
Compositing::ScrollVelocityTracker m_scroll_velocity_tracker;
Compositing::ScrollFlingBooster m_scroll_fling_booster;
Optional<ActiveScrollFling> m_scroll_fling;
- Vector<ActiveElasticOverscroll> m_elastic_overscrolls;
+ ElasticOverscrollController m_elastic_overscroll_controller;
// 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;
@@ -424,8 +407,6 @@ private:
u64 ended { 0 };
};
NewestWheelEventIdsFromUI m_newest_wheel_event_ids_from_ui;
- Compositing::ScrollVelocityTracker m_elastic_overscroll_velocity_tracker;
- bool m_scrolls_elastically { false };
Gfx::IntRect m_async_scrolling_viewport_rect;
bool m_has_async_scrolling_state { false };
bool m_has_blocking_wheel_event_listeners { false };
diff --git a/Services/Compositor/ElasticOverscrollController.cpp b/Services/Compositor/ElasticOverscrollController.cpp
new file mode 100644
index 00000000000..5b183072fd8
--- /dev/null
+++ b/Services/Compositor/ElasticOverscrollController.cpp
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Math.h>
+#include <Compositor/ElasticOverscrollController.h>
+#include <LibCompositing/Scrolling/AsyncScrollTree.h>
+
+namespace Compositor {
+
+void ElasticOverscrollController::set_enabled(bool enabled)
+{
+ m_enabled = enabled;
+ if (!enabled)
+ forget();
+}
+
+bool ElasticOverscrollController::step_can_stretch(Web::WheelDeltaPrecision wheel_delta_precision, Web::ScrollGesturePhase scroll_gesture_phase) const
+{
+ if (!m_enabled)
+ return false;
+ // Only a touchpad gesture says when the fingers lift, which is when a stretch has to spring back.
+ if (wheel_delta_precision != Web::WheelDeltaPrecision::Precise)
+ return false;
+ return scroll_gesture_phase == Web::ScrollGesturePhase::Ongoing || scroll_gesture_phase == Web::ScrollGesturePhase::Momentum;
+}
+
+void ElasticOverscrollController::note_step_from_ui(Gfx::FloatPoint delta, MonotonicTime now)
+{
+ m_step_velocity_tracker.add_step(delta, now);
+}
+
+ElasticOverscrollController::ActiveOverscroll* ElasticOverscrollController::active_overscroll_of(Web::AsyncScrollNodeStableID stable_node_id)
+{
+ for (auto& active : m_active_overscrolls) {
+ if (active.stable_node_id == stable_node_id)
+ return &active;
+ }
+ return nullptr;
+}
+
+bool ElasticOverscrollController::has_active_overscroll_of(Web::AsyncScrollNodeStableID stable_node_id) const
+{
+ return any_of(m_active_overscrolls, [&](auto const& active) { return active.stable_node_id == stable_node_id; });
+}
+
+Gfx::FloatPoint ElasticOverscrollController::stretch_of(Web::AsyncScrollNodeStableID stable_node_id) const
+{
+ for (auto const& active : m_active_overscrolls) {
+ if (active.stable_node_id == stable_node_id)
+ return active.overscroll.stretch();
+ }
+ return {};
+}
+
+Gfx::FloatPoint ElasticOverscrollController::relax_by(Web::AsyncScrollNodeStableID stable_node_id, Gfx::FloatPoint step_delta)
+{
+ auto* active = active_overscroll_of(stable_node_id);
+ if (!active)
+ return step_delta;
+ auto delta_left_to_scroll_with = active->overscroll.relax_by(step_delta);
+ if (active->overscroll.is_at_rest())
+ m_active_overscrolls.remove_all_matching([&](auto const& candidate) { return candidate.stable_node_id == stable_node_id; });
+ return delta_left_to_scroll_with;
+}
+
+bool ElasticOverscrollController::pull_by(Compositing::AsyncScrollNode const& node, Gfx::FloatPoint scroll_offset, Gfx::FloatPoint step_delta, Gfx::FloatPoint delta_the_scroller_did_not_scroll_by, float visual_viewport_magnification)
+{
+ // A touchpad step is rarely along one axis exactly. Only its main direction pulls, so that a vertical pan next to
+ // a side edge does not creep past that edge.
+ Gfx::FloatPoint overscroll_delta;
+ bool step_is_mainly_vertical = AK::abs(step_delta.y()) >= AK::abs(step_delta.x());
+ if (step_is_mainly_vertical) {
+ auto delta_y = delta_the_scroller_did_not_scroll_by.y();
+ bool is_at_the_edge_the_delta_points_past = (delta_y < 0 && scroll_offset.y() <= node.min_scroll_offset.y())
+ || (delta_y > 0 && scroll_offset.y() >= node.max_scroll_offset.y());
+ if (node.can_be_wheel_scrolled_vertically && node.shows_overscroll_effect_vertically && is_at_the_edge_the_delta_points_past)
+ overscroll_delta.set_y(delta_y);
+ } else {
+ auto delta_x = delta_the_scroller_did_not_scroll_by.x();
+ bool is_at_the_edge_the_delta_points_past = (delta_x < 0 && scroll_offset.x() <= node.min_scroll_offset.x())
+ || (delta_x > 0 && scroll_offset.x() >= node.max_scroll_offset.x());
+ if (node.can_be_wheel_scrolled_horizontally && node.shows_overscroll_effect_horizontally && is_at_the_edge_the_delta_points_past)
+ overscroll_delta.set_x(delta_x);
+ }
+ if (overscroll_delta.is_zero())
+ return false;
+
+ auto* active = active_overscroll_of(node.stable_node_id);
+ if (!active) {
+ // A zoomed page shows only the part of the viewport's scrollport that its visual viewport is on, and that part
+ // is what bounds how far the viewport stretches.
+ auto scrollport_size = node.scrollport_rect.size().to_type<float>();
+ if (node.is_viewport)
+ scrollport_size.scale_by(1.0f / visual_viewport_magnification);
+ m_active_overscrolls.append({ node.stable_node_id, Compositing::ElasticOverscroll { scrollport_size, visual_viewport_magnification } });
+ active = &m_active_overscrolls.last();
+ }
+ active->overscroll.pull_by(overscroll_delta);
+ return true;
+}
+
+void ElasticOverscrollController::release_held_by_fingers(MonotonicTime now)
+{
+ auto velocity = m_step_velocity_tracker.velocity_at_lift(now).value_or({});
+ for (auto& active : m_active_overscrolls)
+ active.overscroll.release(velocity, now);
+}
+
+void ElasticOverscrollController::end_gesture(MonotonicTime now)
+{
+ release_held_by_fingers(now);
+ m_step_velocity_tracker.reset();
+}
+
+void ElasticOverscrollController::forget()
+{
+ m_active_overscrolls.clear();
+ m_step_velocity_tracker.reset();
+}
+
+void ElasticOverscrollController::did_install_scrolling_state(Compositing::AsyncScrollTree const& async_scroll_tree)
+{
+ m_active_overscrolls.remove_all_matching([&](auto const& active) {
+ auto node_id = async_scroll_tree.scroll_node_id_for_stable_id(active.stable_node_id);
+ auto const* node = node_id.has_value() ? async_scroll_tree.scroll_node_for_id(*node_id) : nullptr;
+ if (!node)
+ return true;
+ // A scroller that has been told not to stretch along an axis since it was pulled lets go at once.
+ auto stretch = active.overscroll.stretch();
+ return (stretch.x() != 0 && !node->shows_overscroll_effect_horizontally) || (stretch.y() != 0 && !node->shows_overscroll_effect_vertically);
+ });
+}
+
+bool ElasticOverscrollController::has_rebound() const
+{
+ return any_of(m_active_overscrolls, [](auto const& active) { return active.overscroll.is_rebounding(); });
+}
+
+bool ElasticOverscrollController::advance_rebounds(MonotonicTime frame_time)
+{
+ bool a_presented_stretch_changed = false;
+ m_active_overscrolls.remove_all_matching([&](auto& active) {
+ if (!active.overscroll.is_rebounding())
+ return false;
+ auto stretch_before = active.overscroll.stretch();
+ bool is_still_rebounding = active.overscroll.advance_rebound_to(frame_time);
+ if (active.overscroll.stretch() != stretch_before)
+ a_presented_stretch_changed = true;
+ return !is_still_rebounding;
+ });
+ return a_presented_stretch_changed;
+}
+
+Optional<Compositing::ScrollStateSnapshot> ElasticOverscrollController::stretched_scroll_state_snapshot(Compositing::ScrollStateSnapshot const& scroll_state_snapshot, Compositing::AsyncScrollTree const& async_scroll_tree) const
+{
+ Optional<Compositing::ScrollStateSnapshot> stretched_scroll_state_snapshot;
+ for (auto const& active : m_active_overscrolls) {
+ auto stretch = active.overscroll.stretch();
+ if (stretch.is_zero())
+ continue;
+ auto node_id = async_scroll_tree.scroll_node_id_for_stable_id(active.stable_node_id);
+ if (!node_id.has_value())
+ continue;
+ if (!stretched_scroll_state_snapshot.has_value())
+ stretched_scroll_state_snapshot = scroll_state_snapshot;
+ // Only the scroller's own entry moves. The sticky boxes in it keep the shifts resolved for the scroll offset
+ // it really has, so they move with its content.
+ auto index = node_id->scroll_node_index;
+ stretched_scroll_state_snapshot->set_device_offset_for_index(index, stretched_scroll_state_snapshot->device_offset_for_index(index) - stretch);
+ }
+ return stretched_scroll_state_snapshot;
+}
+
+}
diff --git a/Services/Compositor/ElasticOverscrollController.h b/Services/Compositor/ElasticOverscrollController.h
new file mode 100644
index 00000000000..fd7720ac4d9
--- /dev/null
+++ b/Services/Compositor/ElasticOverscrollController.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/Time.h>
+#include <AK/Vector.h>
+#include <LibCompositing/Scrolling/AsyncScrollingState.h>
+#include <LibCompositing/Scrolling/ElasticOverscroll.h>
+#include <LibCompositing/Scrolling/ScrollFling.h>
+#include <LibCompositing/Scrolling/ScrollState.h>
+#include <LibGfx/Point.h>
+#include <LibWebCommon/Page/InputEvent.h>
+
+namespace Compositing {
+
+class AsyncScrollTree;
+
+}
+
+namespace Compositor {
+
+class ElasticOverscrollController {
+public:
+ void set_enabled(bool);
+ bool step_can_stretch(Web::WheelDeltaPrecision, Web::ScrollGesturePhase) const;
+ void note_step_from_ui(Gfx::FloatPoint delta, MonotonicTime now);
+
+ bool has_active_overscroll_of(Web::AsyncScrollNodeStableID) const;
+ Gfx::FloatPoint stretch_of(Web::AsyncScrollNodeStableID) const;
+ // Uses the part of a finger step that points back to the scroll range to relax the stretch of the scroller, and
+ // returns the rest of the step.
+ Gfx::FloatPoint relax_by(Web::AsyncScrollNodeStableID, Gfx::FloatPoint step_delta);
+ // Returns whether the scroller is at an edge that the step points past, which is what lets the step pull it.
+ bool pull_by(Compositing::AsyncScrollNode const&, Gfx::FloatPoint scroll_offset, Gfx::FloatPoint step_delta, Gfx::FloatPoint delta_the_scroller_did_not_scroll_by, float visual_viewport_magnification);
+ void release_held_by_fingers(MonotonicTime now);
+ void end_gesture(MonotonicTime now);
+ void forget();
+ void did_install_scrolling_state(Compositing::AsyncScrollTree const&);
+
+ bool has_rebound() const;
+ bool advance_rebounds(MonotonicTime frame_time);
+
+ // The scroll offsets to present when a scroller is stretched, which are the only ones that leave a scroll range.
+ Optional<Compositing::ScrollStateSnapshot> stretched_scroll_state_snapshot(Compositing::ScrollStateSnapshot const&, Compositing::AsyncScrollTree const&) const;
+
+private:
+ // A scroller that a touchpad gesture holds past an edge of its scroll range, or that is springing back from there.
+ // The stretch is only presented: it is in no scroll offset that the scroll tree or WebContent sees.
+ struct ActiveOverscroll {
+ Web::AsyncScrollNodeStableID stable_node_id;
+ Compositing::ElasticOverscroll overscroll;
+ };
+
+ ActiveOverscroll* active_overscroll_of(Web::AsyncScrollNodeStableID);
+
+ bool m_enabled { false };
+ Vector<ActiveOverscroll> m_active_overscrolls;
+ Compositing::ScrollVelocityTracker m_step_velocity_tracker;
+};
+
+}