Skip to content

Truly independent web browser

Compositor: Move scroll fling state out of ContextState

Aliaksandr Kalenikbaa510c67e5a
diff --git a/Services/Compositor/CMakeLists.txt b/Services/Compositor/CMakeLists.txt
index 05896f921f4..6da573e0926 100644
--- a/Services/Compositor/CMakeLists.txt
+++ b/Services/Compositor/CMakeLists.txt
@@ -16,6 +16,7 @@ set(SOURCES
PausedDebuggerOverlay.cpp
RasterizeDisplayList.cpp
RenderClockConnection.cpp
+ ScrollFlingController.cpp
ScrollSnapController.cpp
ScrollbarController.cpp
VideoFrameSkia.cpp
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 2aeca191be6..d2a655cb624 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -1175,7 +1175,7 @@ ContextState::ContextUpdateResult ContextState::handle_wheel_event(Web::MouseEve
{
VERIFY(event.type == Web::MouseEvent::Type::MouseWheel);
auto now = now_for_testing.value_or(MonotonicTime::now());
- track_wheel_event_for_scroll_fling(event, now);
+ m_scroll_fling_controller.note_wheel_event(event, now);
return scroll_for_wheel_event(event, now);
}
@@ -1255,107 +1255,44 @@ void ContextState::note_wheel_event_from_ui(Web::MouseEvent const& event, Option
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)
-{
- return event.wheel_delta_precision == Web::WheelDeltaPrecision::Precise && event.scroll_gesture_phase == Web::ScrollGesturePhase::Ongoing;
-}
-
-void ContextState::track_wheel_event_for_scroll_fling(Web::MouseEvent const& event, MonotonicTime now)
-{
- if (is_touchpad_step(event)) {
- m_scroll_velocity_tracker.add_step({ static_cast<float>(event.wheel_delta_x), static_cast<float>(event.wheel_delta_y) }, now);
- return;
- }
- m_scroll_velocity_tracker.reset();
- // Other wheel input cancels the boost of the next fling.
- if (event.scroll_gesture_phase != Web::ScrollGesturePhase::Ended)
- m_scroll_fling_booster.reset();
-}
-
bool ContextState::start_scroll_fling_if_flicked(Web::MouseEvent const& gesture_end_event, Optional<MonotonicTime> now_for_testing)
{
- VERIFY(gesture_end_event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended);
- auto now = now_for_testing.value_or(MonotonicTime::now());
- auto velocity = m_scroll_velocity_tracker.velocity_at_lift(now);
- m_scroll_velocity_tracker.reset();
- if (!velocity.has_value())
- return false;
-
- auto fling = Compositing::ScrollFling::start(m_scroll_fling_booster.boosted_velocity(*velocity, now), now);
- if (!fling.has_value())
- return false;
- m_scroll_fling = ActiveScrollFling {
- .fling = fling.release_value(),
- .position = gesture_end_event.position,
- .screen_position = gesture_end_event.screen_position,
- .modifiers = gesture_end_event.modifiers,
- };
- return true;
+ return m_scroll_fling_controller.start_if_flicked(gesture_end_event, now_for_testing.value_or(MonotonicTime::now()));
}
Optional<ContextState::ScrollFlingStep> ContextState::interrupt_scroll_fling(Web::MouseEvent const& event, Optional<MonotonicTime> now_for_testing)
{
- if (!m_scroll_fling.has_value())
- return {};
auto now = now_for_testing.value_or(MonotonicTime::now());
-
- // A new touchpad step stops the fling. The next flick can add the velocity of this fling.
- if (is_touchpad_step(event))
- m_scroll_fling_booster.fling_was_interrupted(m_scroll_fling->fling.velocity_at(now), now);
- return end_scroll_fling(now);
-}
-
-Web::MouseEvent ContextState::scroll_fling_event(Gfx::FloatPoint delta, Web::ScrollGesturePhase phase) const
-{
- VERIFY(m_scroll_fling.has_value());
- return {
- .type = Web::MouseEvent::Type::MouseWheel,
- .position = m_scroll_fling->position,
- .screen_position = m_scroll_fling->screen_position,
- .modifiers = m_scroll_fling->modifiers,
- .wheel_delta_x = delta.x(),
- .wheel_delta_y = delta.y(),
- .wheel_delta_precision = Web::WheelDeltaPrecision::Precise,
- .scroll_gesture_phase = phase,
- .click_count = 0,
- .browser_data = nullptr,
- .async_scroll_performed_default_action = false,
- // The IDs of UI input events start at 1. Thus, ID 0 does not match a UI event.
- .id = 0,
- };
+ auto end_event = m_scroll_fling_controller.interrupt(event, now);
+ if (!end_event.has_value())
+ return {};
+ return scroll_for_fling_event(end_event.release_value(), now);
}
Optional<ContextState::ScrollFlingStep> ContextState::take_scroll_fling_step(MonotonicTime frame_time)
{
- if (!m_scroll_fling.has_value())
- return {};
- if (m_scroll_fling->stopped)
- return end_scroll_fling(frame_time);
-
- auto delta = m_scroll_fling->fling.step(frame_time);
- if (!delta.has_value())
- return end_scroll_fling(frame_time);
- if (delta->is_zero())
+ auto event = m_scroll_fling_controller.take_step(frame_time);
+ if (!event.has_value())
return {};
-
- auto event = scroll_fling_event(*delta, Web::ScrollGesturePhase::Momentum);
- auto result = scroll_for_wheel_event(event, frame_time);
+ auto step = scroll_for_fling_event(event.release_value(), frame_time);
// If the compositor accepts the step but does not scroll, the scroller is at its edge.
// Alternatively, a snap scroll uses the step. The next display tick ends the fling.
- if (result.accepted && !result.frame_to_present.has_value())
- m_scroll_fling->stopped = true;
- return ScrollFlingStep { move(event), move(result) };
+ if (step.event.scroll_gesture_phase == Web::ScrollGesturePhase::Momentum && step.result.accepted && !step.result.frame_to_present.has_value())
+ m_scroll_fling_controller.end_on_next_step();
+ return step;
}
Optional<ContextState::ScrollFlingStep> ContextState::end_scroll_fling(MonotonicTime now)
{
- if (!m_scroll_fling.has_value())
+ auto end_event = m_scroll_fling_controller.end();
+ if (!end_event.has_value())
return {};
+ return scroll_for_fling_event(end_event.release_value(), now);
+}
- // WebContent holds the gesture open while momentum events arrive. The end of the gesture releases it.
- auto event = scroll_fling_event({}, Web::ScrollGesturePhase::Ended);
- m_scroll_fling.clear();
+ContextState::ScrollFlingStep ContextState::scroll_for_fling_event(Web::MouseEvent event, MonotonicTime now)
+{
auto result = scroll_for_wheel_event(event, now);
return ScrollFlingStep { move(event), move(result) };
}
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 77108be7b7d..3168bdcc29c 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -20,6 +20,7 @@
#include <Compositor/ElasticOverscrollController.h>
#include <Compositor/Forward.h>
#include <Compositor/FramePacer.h>
+#include <Compositor/ScrollFlingController.h>
#include <Compositor/ScrollSnapController.h>
#include <Compositor/ScrollbarController.h>
#include <LibCompositing/DisplayList/AccumulatedVisualContext.h>
@@ -29,7 +30,6 @@
#include <LibCompositing/Forward.h>
#include <LibCompositing/Scrolling/AsyncScrollTree.h>
#include <LibCompositing/Scrolling/AsyncScrollingState.h>
-#include <LibCompositing/Scrolling/ScrollFling.h>
#include <LibCompositing/Scrolling/ScrollState.h>
#include <LibCompositing/Scrolling/SmoothScrollAnimation.h>
#include <LibCompositing/Scrolling/WheelGestureIdentity.h>
@@ -146,7 +146,7 @@ public:
Optional<Gfx::IntRect> advance_smooth_scroll_animations(MonotonicTime now);
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(); }
+ bool has_active_scroll_fling() const { return m_scroll_fling_controller.is_active(); }
struct ScrollFlingStep {
Web::MouseEvent event;
ContextUpdateResult result;
@@ -231,15 +231,6 @@ private:
bool is_user_scroll { false };
};
- struct ActiveScrollFling {
- Compositing::ScrollFling fling;
- // The fling steps use the position and modifiers of the gesture end event.
- Web::DevicePixelPoint position;
- Web::DevicePixelPoint screen_position;
- Web::UIEvents::KeyModifier modifiers { Web::UIEvents::KeyModifier::Mod_None };
- bool stopped { false };
- };
-
struct VisualViewportScrollDelta {
Compositing::AsyncScrollOffset scroll_offset;
Gfx::FloatPoint consumed_delta;
@@ -319,7 +310,6 @@ private:
bool has_active_smooth_scroll_animations() const { return !m_smooth_scroll_animations.is_empty(); }
void retire_smooth_scroll_animation(Web::AsyncScrollNodeStableID);
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;
bool pull_elastic_overscroll_by(Compositing::AsyncScrollNode const&, Gfx::FloatPoint step_delta, Gfx::FloatPoint delta_the_scroller_did_not_scroll_by);
Optional<Compositing::ScrollStateSnapshot> scroll_state_snapshot_stretched_by_elastic_overscrolls() const;
@@ -329,7 +319,7 @@ private:
// of a gesture this context has already ended or moved past.
bool wheel_step_was_overtaken_by_ui(Web::ScrollGesturePhase, u64 input_event_id) const;
bool step_continues_touchpad_gesture_the_page_let_through(Optional<Compositing::AsyncScrollNodeID> const& latched_node_id, Web::WheelDeltaPrecision, Web::ScrollGesturePhase) const;
- Web::MouseEvent scroll_fling_event(Gfx::FloatPoint delta, Web::ScrollGesturePhase) const;
+ ScrollFlingStep scroll_for_fling_event(Web::MouseEvent, MonotonicTime now);
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);
void retarget_user_scroll(ActiveSmoothScrollAnimation&, Gfx::FloatPoint destination_offset, MonotonicTime now);
@@ -394,9 +384,7 @@ private:
RefPtr<Core::Timer> m_scroll_step_gesture_input_timer;
Optional<WheelScrollLatch> m_wheel_scroll_latch;
Vector<ActiveSmoothScrollAnimation> m_smooth_scroll_animations;
- Compositing::ScrollVelocityTracker m_scroll_velocity_tracker;
- Compositing::ScrollFlingBooster m_scroll_fling_booster;
- Optional<ActiveScrollFling> m_scroll_fling;
+ ScrollFlingController m_scroll_fling_controller;
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.
diff --git a/Services/Compositor/ScrollFlingController.cpp b/Services/Compositor/ScrollFlingController.cpp
new file mode 100644
index 00000000000..ac78ba8e218
--- /dev/null
+++ b/Services/Compositor/ScrollFlingController.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Compositor/ScrollFlingController.h>
+
+namespace Compositor {
+
+static bool is_touchpad_step(Web::MouseEvent const& event)
+{
+ return event.wheel_delta_precision == Web::WheelDeltaPrecision::Precise && event.scroll_gesture_phase == Web::ScrollGesturePhase::Ongoing;
+}
+
+void ScrollFlingController::note_wheel_event(Web::MouseEvent const& event, MonotonicTime now)
+{
+ if (is_touchpad_step(event)) {
+ m_velocity_tracker.add_step({ static_cast<float>(event.wheel_delta_x), static_cast<float>(event.wheel_delta_y) }, now);
+ return;
+ }
+ m_velocity_tracker.reset();
+ // Other wheel input cancels the boost of the next fling.
+ if (event.scroll_gesture_phase != Web::ScrollGesturePhase::Ended)
+ m_booster.reset();
+}
+
+bool ScrollFlingController::start_if_flicked(Web::MouseEvent const& gesture_end_event, MonotonicTime now)
+{
+ VERIFY(gesture_end_event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended);
+ auto velocity = m_velocity_tracker.velocity_at_lift(now);
+ m_velocity_tracker.reset();
+ if (!velocity.has_value())
+ return false;
+
+ auto fling = Compositing::ScrollFling::start(m_booster.boosted_velocity(*velocity, now), now);
+ if (!fling.has_value())
+ return false;
+ m_active_fling = ActiveFling {
+ .fling = fling.release_value(),
+ .position = gesture_end_event.position,
+ .screen_position = gesture_end_event.screen_position,
+ .modifiers = gesture_end_event.modifiers,
+ };
+ return true;
+}
+
+Optional<Web::MouseEvent> ScrollFlingController::take_step(MonotonicTime frame_time)
+{
+ if (!m_active_fling.has_value())
+ return {};
+ if (m_active_fling->ends_on_next_step)
+ return end();
+
+ auto delta = m_active_fling->fling.step(frame_time);
+ if (!delta.has_value())
+ return end();
+ if (delta->is_zero())
+ return {};
+ return event_of_active_fling(*delta, Web::ScrollGesturePhase::Momentum);
+}
+
+void ScrollFlingController::end_on_next_step()
+{
+ VERIFY(m_active_fling.has_value());
+ m_active_fling->ends_on_next_step = true;
+}
+
+Optional<Web::MouseEvent> ScrollFlingController::end()
+{
+ if (!m_active_fling.has_value())
+ return {};
+
+ // WebContent holds the gesture open while momentum events arrive. The end of the gesture releases it.
+ auto event = event_of_active_fling({}, Web::ScrollGesturePhase::Ended);
+ m_active_fling.clear();
+ return event;
+}
+
+Optional<Web::MouseEvent> ScrollFlingController::interrupt(Web::MouseEvent const& event, MonotonicTime now)
+{
+ if (!m_active_fling.has_value())
+ return {};
+
+ // A new touchpad step stops the fling. The next flick can add the velocity of this fling.
+ if (is_touchpad_step(event))
+ m_booster.fling_was_interrupted(m_active_fling->fling.velocity_at(now), now);
+ return end();
+}
+
+Web::MouseEvent ScrollFlingController::event_of_active_fling(Gfx::FloatPoint delta, Web::ScrollGesturePhase phase) const
+{
+ VERIFY(m_active_fling.has_value());
+ return {
+ .type = Web::MouseEvent::Type::MouseWheel,
+ .position = m_active_fling->position,
+ .screen_position = m_active_fling->screen_position,
+ .modifiers = m_active_fling->modifiers,
+ .wheel_delta_x = delta.x(),
+ .wheel_delta_y = delta.y(),
+ .wheel_delta_precision = Web::WheelDeltaPrecision::Precise,
+ .scroll_gesture_phase = phase,
+ .click_count = 0,
+ .browser_data = nullptr,
+ .async_scroll_performed_default_action = false,
+ // The IDs of UI input events start at 1. Thus, ID 0 does not match a UI event.
+ .id = 0,
+ };
+}
+
+}
diff --git a/Services/Compositor/ScrollFlingController.h b/Services/Compositor/ScrollFlingController.h
new file mode 100644
index 00000000000..a94b99d0047
--- /dev/null
+++ b/Services/Compositor/ScrollFlingController.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/Time.h>
+#include <LibCompositing/Scrolling/ScrollFling.h>
+#include <LibGfx/Point.h>
+#include <LibWebCommon/Page/InputEvent.h>
+
+namespace Compositor {
+
+// Synthesizes the momentum of a touchpad flick as wheel events, for platforms that send no momentum scroll events.
+class ScrollFlingController {
+public:
+ void note_wheel_event(Web::MouseEvent const&, MonotonicTime now);
+ // Starts a fling if the gesture that the event ends was a flick.
+ bool start_if_flicked(Web::MouseEvent const& gesture_end_event, MonotonicTime now);
+ bool is_active() const { return m_active_fling.has_value(); }
+
+ // The next momentum step of the fling, or the end of its gesture once the fling has run out or was told to end.
+ Optional<Web::MouseEvent> take_step(MonotonicTime frame_time);
+ void end_on_next_step();
+ // The end of the gesture of the fling, if one is active.
+ Optional<Web::MouseEvent> end();
+ // Ends the fling before a wheel event from a new gesture or other input.
+ Optional<Web::MouseEvent> interrupt(Web::MouseEvent const&, MonotonicTime now);
+
+private:
+ struct ActiveFling {
+ Compositing::ScrollFling fling;
+ // The fling steps use the position and modifiers of the gesture end event.
+ Web::DevicePixelPoint position;
+ Web::DevicePixelPoint screen_position;
+ Web::UIEvents::KeyModifier modifiers { Web::UIEvents::KeyModifier::Mod_None };
+ bool ends_on_next_step { false };
+ };
+
+ Web::MouseEvent event_of_active_fling(Gfx::FloatPoint delta, Web::ScrollGesturePhase) const;
+
+ Compositing::ScrollVelocityTracker m_velocity_tracker;
+ Compositing::ScrollFlingBooster m_booster;
+ Optional<ActiveFling> m_active_fling;
+};
+
+}