Skip to content

Truly independent web browser

Everywhere: Let the compositor resolve scrolls by a distance

Zaggy1024c0c7dca33996
diff --git a/Libraries/LibCompositing/Types.cpp b/Libraries/LibCompositing/Types.cpp
index 4c5ae77f06f..94dd605c92b 100644
--- a/Libraries/LibCompositing/Types.cpp
+++ b/Libraries/LibCompositing/Types.cpp
@@ -167,11 +167,31 @@ ErrorOr<Compositing::PendingAsyncScrollUpdates> decode(Decoder& decoder)
};
}
+template<>
+ErrorOr<void> encode(Encoder& encoder, Compositing::RequestedRelativeScroll const& relative_scroll)
+{
+ TRY(encoder.encode(relative_scroll.delta));
+ TRY(encoder.encode(relative_scroll.lower_bound));
+ TRY(encoder.encode(relative_scroll.upper_bound));
+ return {};
+}
+
+template<>
+ErrorOr<Compositing::RequestedRelativeScroll> decode(Decoder& decoder)
+{
+ return Compositing::RequestedRelativeScroll {
+ .delta = TRY(decoder.decode<Gfx::FloatPoint>()),
+ .lower_bound = TRY(decoder.decode<Gfx::FloatPoint>()),
+ .upper_bound = TRY(decoder.decode<Gfx::FloatPoint>()),
+ };
+}
+
template<>
ErrorOr<void> encode(Encoder& encoder, Compositing::RequestedSmoothScroll const& smooth_scroll)
{
TRY(encoder.encode(smooth_scroll.operation_id));
TRY(encoder.encode(smooth_scroll.destination_offset));
+ TRY(encoder.encode(smooth_scroll.destination_is_relative));
TRY(encoder.encode(smooth_scroll.animation_kind));
TRY(encoder.encode(smooth_scroll.initiator));
TRY(encoder.encode(smooth_scroll.retargets_operation));
@@ -185,6 +205,7 @@ ErrorOr<Compositing::RequestedSmoothScroll> decode(Decoder& decoder)
return Compositing::RequestedSmoothScroll {
.operation_id = TRY(decoder.decode<Compositing::AsyncScrollOperationID>()),
.destination_offset = TRY(decoder.decode<Gfx::FloatPoint>()),
+ .destination_is_relative = TRY(decoder.decode<bool>()),
.animation_kind = TRY(decoder.decode<Compositing::ScrollAnimationKind>()),
.initiator = TRY(decoder.decode<Compositing::SmoothScrollInitiator>()),
.retargets_operation = TRY(decoder.decode<bool>()),
@@ -199,6 +220,7 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::ScrollRequest const& request
TRY(encoder.encode(request.request_id));
TRY(encoder.encode(request.jump_offset));
TRY(encoder.encode(request.visual_viewport_scale));
+ TRY(encoder.encode(request.relative_scroll));
TRY(encoder.encode(request.smooth_scroll));
return {};
}
@@ -211,6 +233,7 @@ ErrorOr<Compositing::ScrollRequest> decode(Decoder& decoder)
.request_id = TRY(decoder.decode<u64>()),
.jump_offset = TRY(decoder.decode<Optional<Gfx::FloatPoint>>()),
.visual_viewport_scale = TRY(decoder.decode<Optional<float>>()),
+ .relative_scroll = TRY(decoder.decode<Optional<Compositing::RequestedRelativeScroll>>()),
.smooth_scroll = TRY(decoder.decode<Optional<Compositing::RequestedSmoothScroll>>()),
};
}
diff --git a/Libraries/LibCompositing/Types.h b/Libraries/LibCompositing/Types.h
index f4202728199..65c39b4e25d 100644
--- a/Libraries/LibCompositing/Types.h
+++ b/Libraries/LibCompositing/Types.h
@@ -9,6 +9,7 @@
#include <AK/Assertions.h>
#include <AK/DistinctNumeric.h>
#include <AK/EnumBits.h>
+#include <AK/Math.h>
#include <AK/Optional.h>
#include <AK/Time.h>
#include <AK/Types.h>
@@ -120,10 +121,41 @@ enum class SmoothScrollInitiator : u8 {
// compositor allocates for scrolls of its own.
inline constexpr AsyncScrollOperationID first_operation_id_the_main_thread_requests = AsyncScrollOperationID { 1 } << 63;
+// An instant scroll by a distance from wherever the box is. Each scroll folded into it clamps the box to the scroll range
+// it had, which leaves the box at clamp(offset + delta, lower_bound, upper_bound) in each axis.
+struct RequestedRelativeScroll {
+ Gfx::FloatPoint delta;
+ Gfx::FloatPoint lower_bound { -AK::Infinity<float>, -AK::Infinity<float> };
+ Gfx::FloatPoint upper_bound { AK::Infinity<float>, AK::Infinity<float> };
+
+ void then(RequestedRelativeScroll const& later)
+ {
+ lower_bound = {
+ AK::clamp(lower_bound.x() + later.delta.x(), later.lower_bound.x(), later.upper_bound.x()),
+ AK::clamp(lower_bound.y() + later.delta.y(), later.lower_bound.y(), later.upper_bound.y()),
+ };
+ upper_bound = {
+ AK::clamp(upper_bound.x() + later.delta.x(), later.lower_bound.x(), later.upper_bound.x()),
+ AK::clamp(upper_bound.y() + later.delta.y(), later.lower_bound.y(), later.upper_bound.y()),
+ };
+ delta.translate_by(later.delta);
+ }
+
+ Gfx::FloatPoint applied_to(Gfx::FloatPoint offset) const
+ {
+ return {
+ AK::clamp(offset.x() + delta.x(), lower_bound.x(), upper_bound.x()),
+ AK::clamp(offset.y() + delta.y(), lower_bound.y(), upper_bound.y()),
+ };
+ }
+};
+
// A smooth scroll that the main thread requests, from wherever the box is once the request's instant scroll is done.
struct RequestedSmoothScroll {
AsyncScrollOperationID operation_id { 0 };
+ // Where the scroll goes, or how far it goes from wherever the box is.
Gfx::FloatPoint destination_offset;
+ bool destination_is_relative { false };
ScrollAnimationKind animation_kind { ScrollAnimationKind::SmoothScroll };
SmoothScrollInitiator initiator { SmoothScrollInitiator::Programmatic };
// User input that heads the user input scroll of the same operation somewhere else, rather than starting another.
@@ -131,6 +163,21 @@ struct RequestedSmoothScroll {
// For the scroll that settles a user's gesture, the count of the box's scrolls for the compositor's own user input
// that the main thread had heard of. User input the compositor took since carries the gesture on instead.
Optional<u64> settled_user_input_scroll_count;
+
+ // A later scroll of the same operation goes on from where this one was headed.
+ void fold_later(RequestedSmoothScroll const& later)
+ {
+ VERIFY(later.operation_id == operation_id);
+ auto earlier_destination_offset = destination_offset;
+ auto earlier_destination_is_relative = destination_is_relative;
+ auto earlier_retargets_operation = retargets_operation;
+ *this = later;
+ retargets_operation |= earlier_retargets_operation;
+ if (later.destination_is_relative) {
+ destination_offset.translate_by(earlier_destination_offset);
+ destination_is_relative = earlier_destination_is_relative;
+ }
+ }
};
// What the main thread asks of the compositor for one scrolling box, carried with the frame it changed the box in. A
@@ -142,6 +189,8 @@ struct ScrollRequest {
Optional<Gfx::FloatPoint> jump_offset;
// The scale the visual viewport jumps to along with its offset.
Optional<float> visual_viewport_scale;
+ // A relative scroll after the jump.
+ Optional<RequestedRelativeScroll> relative_scroll;
Optional<RequestedSmoothScroll> smooth_scroll;
// A request made later in the same frame aborts the smooth scroll of this one, which never got under way.
@@ -151,8 +200,17 @@ struct ScrollRequest {
if (later.jump_offset.has_value()) {
jump_offset = later.jump_offset;
visual_viewport_scale = later.visual_viewport_scale;
+ relative_scroll = later.relative_scroll;
+ } else if (later.relative_scroll.has_value()) {
+ if (relative_scroll.has_value())
+ relative_scroll->then(later.relative_scroll.value());
+ else
+ relative_scroll = later.relative_scroll;
}
- smooth_scroll = later.smooth_scroll;
+ if (smooth_scroll.has_value() && later.smooth_scroll.has_value() && smooth_scroll->operation_id == later.smooth_scroll->operation_id)
+ smooth_scroll->fold_later(later.smooth_scroll.value());
+ else
+ smooth_scroll = later.smooth_scroll;
}
};
@@ -196,6 +254,11 @@ COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::PendingAsyncScrollUp
template<>
COMPOSITING_API ErrorOr<Compositing::PendingAsyncScrollUpdates> decode(Decoder&);
+template<>
+COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::RequestedRelativeScroll const&);
+template<>
+COMPOSITING_API ErrorOr<Compositing::RequestedRelativeScroll> decode(Decoder&);
+
template<>
COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::RequestedSmoothScroll const&);
template<>
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 9a4eab1fee6..5ec66c38e3d 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -5783,7 +5783,7 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
// what that scroll replaced. Once it applied it, the compositor has the box's offset.
if (async_scroll_offset.applied_scroll_request_id < last_scroll_request_id_for(async_scroll_offset.stable_node_id))
continue;
- TemporaryChange adopting_compositor_scroll_offsets { m_is_adopting_compositor_scroll_offsets, true };
+ TemporaryChange adopting_compositor_scroll_offsets { m_scroll_offset_changes_need_no_request, true };
auto css_scroll_offset = async_scroll_offset_to_css_pixels(async_scroll_offset.compositor_scroll_offset, device_pixels_per_css_pixel);
bool has_in_flight_smooth_scroll = false;
@@ -7078,7 +7078,7 @@ Compositing::ScrollRequest& LocalNavigable::unsent_scroll_request_for(Web::Async
auto& state = m_scroll_request_states.ensure(stable_node_id);
if (!state.unsent_request.has_value()) {
state.last_request_id = ++m_last_scroll_request_id;
- state.unsent_request = Compositing::ScrollRequest { .stable_node_id = stable_node_id, .request_id = state.last_request_id, .jump_offset = {}, .visual_viewport_scale = {}, .smooth_scroll = {} };
+ state.unsent_request = Compositing::ScrollRequest { .stable_node_id = stable_node_id, .request_id = state.last_request_id, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = {}, .smooth_scroll = {} };
// The request goes with the next frame, which it takes one being committed for.
if (auto document = active_document())
document->set_needs_repaint(Badge<HTML::LocalNavigable> {}, InvalidateDisplayList::No);
@@ -7119,6 +7119,7 @@ void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_nod
auto& scroll_request = unsent_scroll_request_for(stable_node_id);
drop_unsent_smooth_scroll(scroll_request);
scroll_request.jump_offset = device_scroll_offset(scroll_offset.value());
+ scroll_request.relative_scroll.clear();
if (stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport)
scroll_request.visual_viewport_scale = active_document()->visual_viewport()->scale();
}
@@ -7126,11 +7127,12 @@ void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_nod
void LocalNavigable::request_smooth_scroll(Web::AsyncScrollNodeStableID stable_node_id, Compositing::RequestedSmoothScroll smooth_scroll)
{
auto& scroll_request = unsent_scroll_request_for(stable_node_id);
- // A scroll that continues the same operation replaces the request for it, whose promises carry over.
- if (scroll_request.smooth_scroll.has_value() && scroll_request.smooth_scroll->operation_id == smooth_scroll.operation_id)
- smooth_scroll.retargets_operation |= scroll_request.smooth_scroll->retargets_operation;
- else
- drop_unsent_smooth_scroll(scroll_request);
+ // A scroll that continues the same operation folds into the request for it, whose promises carry over.
+ if (scroll_request.smooth_scroll.has_value() && scroll_request.smooth_scroll->operation_id == smooth_scroll.operation_id) {
+ scroll_request.smooth_scroll->fold_later(smooth_scroll);
+ return;
+ }
+ drop_unsent_smooth_scroll(scroll_request);
scroll_request.smooth_scroll = smooth_scroll;
}
@@ -7139,9 +7141,31 @@ void LocalNavigable::request_abort_of_smooth_scroll(Web::AsyncScrollNodeStableID
drop_unsent_smooth_scroll(unsent_scroll_request_for(stable_node_id));
}
+void LocalNavigable::request_relative_scroll(Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint displacement)
+{
+ auto document = active_document();
+ Layout::ForcedReadScope read { *document };
+ auto const* scrolling_box = layout_node_for_async_scroll_node(read, *document, stable_node_id);
+ if (!scrolling_box || !Painting::has_committed_box(*scrolling_box)) {
+ request_scroll_jump(stable_node_id);
+ return;
+ }
+ auto& scroll_request = unsent_scroll_request_for(stable_node_id);
+ drop_unsent_smooth_scroll(scroll_request);
+ Compositing::RequestedRelativeScroll relative_scroll {
+ .delta = device_scroll_offset(displacement),
+ .lower_bound = device_scroll_offset(Painting::minimum_scroll_offset(*scrolling_box)),
+ .upper_bound = device_scroll_offset(Painting::maximum_scroll_offset(*scrolling_box)),
+ };
+ if (scroll_request.relative_scroll.has_value())
+ scroll_request.relative_scroll->then(relative_scroll);
+ else
+ scroll_request.relative_scroll = relative_scroll;
+}
+
void LocalNavigable::did_change_scroll_offset(Web::AsyncScrollNodeStableID stable_node_id)
{
- if (m_is_adopting_compositor_scroll_offsets || !has_compositor_context())
+ if (m_scroll_offset_changes_need_no_request || !has_compositor_context())
return;
request_scroll_jump(stable_node_id);
}
@@ -7212,6 +7236,10 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
if (!initial_scroll_offset.has_value())
return WebIDL::create_resolved_promise_for(*document, JS::js_undefined());
+ // A scroll by a distance goes from wherever the compositor has the box, unless it came to rest at a snap position:
+ // that one was selected from where the box is here.
+ bool goes_to_a_snap_position = destination_snapping == DestinationSnapping::DestinationIsSnapPosition;
+
// https://drafts.csswg.org/css-scroll-snap-1/#snap-strictness
// If a valid snap position exists then the scroll container must snap at the termination of a scroll (if none
// exist then no snapping occurs).
@@ -7225,9 +7253,13 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
strategy = { Compositing::SnapSelectionStrategy::Type::EndPositionAndDirection, *initial_scroll_offset, *relative_displacement };
auto snap_destination = Painting::adjust_scroll_destination_for_snapping(*snap_container, position, strategy);
position = snap_destination.position;
+ goes_to_a_snap_position = snap_destination.snapped_x || snap_destination.snapped_y;
record_snapped_areas_of_scroll_container(*document, stable_node_id, snap_destination);
}
}
+ Optional<CSSPixelPoint> relative_request_displacement;
+ if (relative_displacement.has_value() && !goes_to_a_snap_position && stable_node_id.kind != Web::AsyncScrollNodeKind::VisualViewport)
+ relative_request_displacement = relative_displacement;
if (scroll_kind == Painting::ScrollKind::Relative || relative_displacement.has_value()) {
// User input that continues a smooth scroll in flight scrolls on from where that scroll is headed.
@@ -7259,7 +7291,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
// AD-HOC: User input that arrives while the box is already scrolling smoothly under user input retargets that
// scroll instead of restarting it.
if (should_scroll_smoothly && trigger == ScrollTrigger::UserInput) {
- if (auto scroll_promise = continue_in_flight_user_smooth_scroll(stable_node_id, position, animation_kind))
+ if (auto scroll_promise = continue_in_flight_user_smooth_scroll(stable_node_id, position, relative_request_displacement, animation_kind))
return *scroll_promise;
}
@@ -7287,11 +7319,19 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
// requested behavior or the associated element's computed behavior is
// smooth, perform a smooth scroll. Otherwise, perform an instant scroll.
if (!should_scroll_smoothly) {
- auto did_scroll = set_scroll_offset_for(stable_node_id, position);
+ bool did_scroll = false;
+ {
+ TemporaryChange requesting_scroll { m_scroll_offset_changes_need_no_request, true };
+ did_scroll = set_scroll_offset_for(stable_node_id, position);
+ }
// The compositor may have scrolled the box elsewhere since the main thread last heard, so the box jumps to the
// position even where its offset here is unchanged.
- if (has_compositor_context())
- request_scroll_jump(stable_node_id);
+ if (has_compositor_context()) {
+ if (relative_request_displacement.has_value())
+ request_relative_scroll(stable_node_id, relative_request_displacement.value());
+ else
+ request_scroll_jump(stable_node_id);
+ }
if (did_scroll)
queue_scrollend_event(stable_node_id, trigger, initial_scroll_offset);
WebIDL::resolve_promise(scroll_promise);
@@ -7306,7 +7346,8 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
settled_user_input_scroll_count = m_compositor_user_input_scroll_counts.get(stable_node_id).value_or(0);
request_smooth_scroll(stable_node_id, {
.operation_id = operation_id,
- .destination_offset = device_scroll_offset(position),
+ .destination_offset = device_scroll_offset(relative_request_displacement.value_or(position)),
+ .destination_is_relative = relative_request_displacement.has_value(),
.animation_kind = animation_kind,
.initiator = trigger == ScrollTrigger::UserInput ? Compositing::SmoothScrollInitiator::UserInput : Compositing::SmoothScrollInitiator::Programmatic,
.retargets_operation = false,
@@ -7351,7 +7392,7 @@ Gfx::FloatPoint LocalNavigable::device_scroll_offset(CSSPixelPoint offset) const
};
}
-GC::Ptr<WebIDL::Promise> LocalNavigable::continue_in_flight_user_smooth_scroll(Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint position, Compositing::ScrollAnimationKind animation_kind)
+GC::Ptr<WebIDL::Promise> LocalNavigable::continue_in_flight_user_smooth_scroll(Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint position, Optional<CSSPixelPoint> relative_displacement, Compositing::ScrollAnimationKind animation_kind)
{
auto document = active_document();
if (!document)
@@ -7365,7 +7406,8 @@ GC::Ptr<WebIDL::Promise> LocalNavigable::continue_in_flight_user_smooth_scroll(W
request_smooth_scroll(stable_node_id, {
.operation_id = pending.operation_id,
- .destination_offset = device_scroll_offset(position),
+ .destination_offset = device_scroll_offset(relative_displacement.value_or(position)),
+ .destination_is_relative = relative_displacement.has_value(),
.animation_kind = animation_kind,
.initiator = Compositing::SmoothScrollInitiator::UserInput,
.retargets_operation = true,
@@ -7434,7 +7476,7 @@ bool LocalNavigable::perform_a_scroll_step_for_key_input(Layout::Node& scroll_co
return true;
}
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*document) };
- perform_a_scroll_of_a_scrolling_box(*stable_node_id, destination, Bindings::ScrollBehavior::Smooth, nullptr, ScrollTrigger::UserInput, {}, DestinationSnapping::SelectSnapPosition, Compositing::ScrollAnimationKind::SmoothScroll, Painting::ScrollKind::Relative);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, destination, Bindings::ScrollBehavior::Smooth, nullptr, ScrollTrigger::UserInput, delta, DestinationSnapping::SelectSnapPosition, Compositing::ScrollAnimationKind::SmoothScroll, Painting::ScrollKind::Relative);
return true;
}
@@ -7595,8 +7637,11 @@ bool LocalNavigable::scroll_scrolling_box_by_delta(Layout::Node& scrolling_box,
return false;
}
+ Optional<CSSPixelPoint> relative_displacement;
+ if (scroll_kind == Painting::ScrollKind::Relative)
+ relative_displacement = delta;
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*document) };
- perform_a_scroll_of_a_scrolling_box(*stable_node_id, position, Bindings::ScrollBehavior::Smooth, {}, ScrollTrigger::UserInput, {}, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, position, Bindings::ScrollBehavior::Smooth, {}, ScrollTrigger::UserInput, relative_displacement, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
return true;
}
@@ -7619,7 +7664,10 @@ bool LocalNavigable::scroll_viewport_by_delta(CSSPixelPoint delta, Bindings::Scr
};
auto page_position = in_flight_aware_page_position();
- perform_a_scroll_of_the_viewport(page_position + delta, behavior, ScrollTrigger::UserInput, {}, scroll_kind, animation_kind);
+ Optional<CSSPixelPoint> relative_displacement;
+ if (scroll_kind == Painting::ScrollKind::Relative)
+ relative_displacement = delta;
+ perform_a_scroll_of_the_viewport(page_position + delta, behavior, ScrollTrigger::UserInput, relative_displacement, scroll_kind, animation_kind);
return in_flight_aware_page_position() != page_position;
}
@@ -7720,6 +7768,17 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_layout_viewport
new_viewport_scroll_offset.set_x(clamp(new_viewport_scroll_offset.x(), minimum_scroll_offset.x(), maximum_scroll_offset.x()));
new_viewport_scroll_offset.set_y(clamp(new_viewport_scroll_offset.y(), minimum_scroll_offset.y(), maximum_scroll_offset.y()));
+ // A scroll of the viewport by a distance moves the layout viewport by the part the visual viewport does not take.
+ if (relative_displacement.has_value()) {
+ relative_displacement = delta;
+ // A step of the user's goes on from where the user's scroll of the layout viewport is headed.
+ Web::AsyncScrollNodeStableID viewport_stable_node_id { .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport };
+ if (trigger == ScrollTrigger::UserInput && behavior == Bindings::ScrollBehavior::Smooth) {
+ if (auto destination = in_flight_user_scroll_destination(viewport_stable_node_id, animation_kind); destination.has_value())
+ relative_displacement = m_viewport_scroll_offset + delta - destination.value();
+ }
+ }
+
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*doc) };
return perform_a_scroll_of_a_scrolling_box({
.node_id = doc->unique_id(),
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 8ce486d7115..fb9dffdd156 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -502,7 +502,7 @@ private:
bool adopt_async_viewport_scroll_offset(CSSPixelPoint);
bool adopt_async_visual_viewport_scroll(Optional<float> scale, CSSPixelPoint scroll_offset);
Gfx::FloatPoint device_scroll_offset(CSSPixelPoint) const;
- GC::Ptr<WebIDL::Promise> continue_in_flight_user_smooth_scroll(Web::AsyncScrollNodeStableID, CSSPixelPoint position, Compositing::ScrollAnimationKind);
+ GC::Ptr<WebIDL::Promise> continue_in_flight_user_smooth_scroll(Web::AsyncScrollNodeStableID, CSSPixelPoint position, Optional<CSSPixelPoint> relative_displacement, Compositing::ScrollAnimationKind);
Optional<CSSPixelPoint> scroll_offset_for(Web::AsyncScrollNodeStableID) const;
bool set_scroll_offset_for(Web::AsyncScrollNodeStableID, CSSPixelPoint);
void queue_scrollend_event(Web::AsyncScrollNodeStableID, ScrollTrigger, Optional<CSSPixelPoint> scroll_offset_before_scroll = {});
@@ -676,11 +676,13 @@ private:
Compositing::AsyncScrollOperationID m_last_requested_operation_id { Compositing::first_operation_id_the_main_thread_requests };
// How many times the compositor reported having scrolled each box for its own user input.
HashMap<Web::AsyncScrollNodeStableID, u64> m_compositor_user_input_scroll_counts;
- // Set while this navigable takes the compositor's offsets, which it asks for nothing about.
- bool m_is_adopting_compositor_scroll_offsets { false };
+ // Set while this navigable changes an offset it asks for in a request of its own making, or takes the compositor's,
+ // which it asks for nothing about.
+ bool m_scroll_offset_changes_need_no_request { false };
Compositing::ScrollRequest& unsent_scroll_request_for(Web::AsyncScrollNodeStableID);
u64 last_scroll_request_id_for(Web::AsyncScrollNodeStableID) const;
void request_scroll_jump(Web::AsyncScrollNodeStableID);
+ void request_relative_scroll(Web::AsyncScrollNodeStableID, CSSPixelPoint displacement);
void request_smooth_scroll(Web::AsyncScrollNodeStableID, Compositing::RequestedSmoothScroll);
void request_abort_of_smooth_scroll(Web::AsyncScrollNodeStableID);
void drop_unsent_smooth_scroll(Compositing::ScrollRequest&);
diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cpp
index 4b15226440c..949843c15e8 100644
--- a/Services/Compositor/ConnectionFromWebContent.cpp
+++ b/Services/Compositor/ConnectionFromWebContent.cpp
@@ -498,6 +498,12 @@ static bool is_valid_scroll_request(Compositing::ScrollRequest const& scroll_req
auto is_finite = [](Gfx::FloatPoint point) { return isfinite(point.x()) && isfinite(point.y()); };
if (scroll_request.jump_offset.has_value() && !is_finite(scroll_request.jump_offset.value()))
return false;
+ if (auto const& relative_scroll = scroll_request.relative_scroll; relative_scroll.has_value()) {
+ if (!is_finite(relative_scroll->delta) || !is_finite(relative_scroll->lower_bound) || !is_finite(relative_scroll->upper_bound))
+ return false;
+ if (relative_scroll->lower_bound.x() > relative_scroll->upper_bound.x() || relative_scroll->lower_bound.y() > relative_scroll->upper_bound.y())
+ return false;
+ }
if (scroll_request.visual_viewport_scale.has_value()) {
auto scale = scroll_request.visual_viewport_scale.value();
if (!isfinite(scale) || scale <= 0)
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 95975141c00..82ad9cd2d67 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -1020,6 +1020,13 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
retargeted_operation_id = scroll_request.smooth_scroll->operation_id;
auto running_animation = m_smooth_scroll_animations.find_if([&](auto const& animation) { return animation.stable_node_id == scroll_request.stable_node_id; });
bool retargets_running_animation = running_animation != m_smooth_scroll_animations.end() && running_animation->operation_id == retargeted_operation_id;
+ // A step of the user's by a distance goes on from where the user's scroll of the same kind is headed, unless the
+ // box jumped or scrolled elsewhere first.
+ Optional<Gfx::FloatPoint> destination_of_running_user_scroll;
+ if (running_animation != m_smooth_scroll_animations.end() && running_animation->is_user_scroll && scroll_request.smooth_scroll.has_value()
+ && running_animation->animation.kind() == scroll_request.smooth_scroll->animation_kind
+ && !scroll_request.jump_offset.has_value() && !scroll_request.relative_scroll.has_value())
+ destination_of_running_user_scroll = running_animation->animation.destination_offset();
// The main thread settled the user's gesture once it heard of no more input for the box. User input this
// context took since then carries the gesture on, and the box goes where that input takes it instead.
@@ -1029,7 +1036,7 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
m_async_scroll_update_outbox.note_operation_taken_over_by_user_input(smooth_scroll->operation_id);
m_async_scroll_update_outbox.complete_operation(smooth_scroll->operation_id);
smooth_scroll.clear();
- if (!scroll_request.jump_offset.has_value()) {
+ if (!scroll_request.jump_offset.has_value() && !scroll_request.relative_scroll.has_value()) {
answer_with_the_offset_of_the_box();
continue;
}
@@ -1042,9 +1049,22 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
if (scroll_request.jump_offset.has_value())
(void)m_async_scroll_tree.set_scroll_offset(node_id.value(), scroll_request.jump_offset.value(), m_scene.current_visual_context_tree(), m_scroll_state_snapshot);
+ // A box new to this context took its offset from the frame's snapshot, which has the frame's scrolls in it.
+ bool offset_has_the_request_in_it = m_scroll_nodes_seeded_by_last_frame.contains(scroll_request.stable_node_id);
+ if (auto const& relative_scroll = scroll_request.relative_scroll; relative_scroll.has_value() && !offset_has_the_request_in_it) {
+ auto offset = m_async_scroll_tree.scroll_offset_for_node(node_id.value(), m_scroll_state_snapshot).value();
+ (void)m_async_scroll_tree.set_scroll_offset(node_id.value(), relative_scroll->applied_to(offset), m_scene.current_visual_context_tree(), m_scroll_state_snapshot);
+ }
if (smooth_scroll.has_value()) {
- auto destination_offset = m_async_scroll_tree.clamped_scroll_offset_for_node(node_id.value(), smooth_scroll->destination_offset);
+ auto destination_offset = smooth_scroll->destination_offset;
+ if (smooth_scroll->destination_is_relative) {
+ auto relative_scroll_start = m_async_scroll_tree.scroll_offset_for_node(node_id.value(), m_scroll_state_snapshot).value();
+ if (smooth_scroll->initiator == Compositing::SmoothScrollInitiator::UserInput && destination_of_running_user_scroll.has_value())
+ relative_scroll_start = destination_of_running_user_scroll.value();
+ destination_offset.translate_by(relative_scroll_start);
+ }
+ destination_offset = m_async_scroll_tree.clamped_scroll_offset_for_node(node_id.value(), destination_offset);
if (retargets_running_animation) {
retarget_user_scroll(*running_animation, destination_offset, MonotonicTime::now());
} else {
@@ -1884,10 +1904,13 @@ Optional<Gfx::FloatPoint> ContextState::keep_scroll_offsets_of_known_nodes(HashM
Optional<Gfx::FloatPoint> viewport_scroll_offset;
Vector<Compositing::AsyncScrollOffset> clamped_scroll_offsets;
m_scroll_offsets_differ_from_last_frame = false;
+ m_scroll_nodes_seeded_by_last_frame.clear();
for (auto const& node : m_async_scroll_tree.scroll_nodes()) {
auto known_scroll_offset = scroll_offsets_of_known_nodes.get(node.stable_node_id);
- if (!known_scroll_offset.has_value())
+ if (!known_scroll_offset.has_value()) {
+ m_scroll_nodes_seeded_by_last_frame.set(node.stable_node_id);
continue;
+ }
if (m_async_scroll_tree.scroll_offset_for_node(node.node_id, m_scroll_state_snapshot) != known_scroll_offset)
m_scroll_offsets_differ_from_last_frame = true;
auto kept_scroll_offset = m_async_scroll_tree.set_scroll_offset(node.node_id, known_scroll_offset.value(), m_scene.current_visual_context_tree(), m_scroll_state_snapshot);
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index c9f323a0729..fb975d2a194 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -364,6 +364,8 @@ private:
HashMap<Web::AsyncScrollNodeStableID, u64> m_applied_scroll_request_ids;
// How many times this context scrolled each box for user input of its own, which the reports are marked with too.
HashMap<Web::AsyncScrollNodeStableID, u64> m_user_input_scroll_counts;
+ // The boxes the last frame brought, whose offsets came from that frame's snapshot.
+ HashTable<Web::AsyncScrollNodeStableID> m_scroll_nodes_seeded_by_last_frame;
// Whether this context has scrolled boxes to offsets that WebContent's last frame does not have them at.
bool m_scroll_offsets_differ_from_last_frame { false };
// The document whose visual viewport this context zoomed or panned, which it presents as it left it from then on.
diff --git a/Tests/Compositor/TestContextState.cpp b/Tests/Compositor/TestContextState.cpp
index db95a6fb327..43bd372d72b 100644
--- a/Tests/Compositor/TestContextState.cpp
+++ b/Tests/Compositor/TestContextState.cpp
@@ -111,6 +111,7 @@ TEST_CASE(pinch_zoom_is_presented_until_the_main_thread_asks_for_another)
.request_id = 1,
.jump_offset = Gfx::FloatPoint {},
.visual_viewport_scale = 1.0f,
+ .relative_scroll = {},
.smooth_scroll = {},
};
(void)context.apply_scroll_requests({ &reset_zoom, 1 });
diff --git a/Tests/LibCompositing/CMakeLists.txt b/Tests/LibCompositing/CMakeLists.txt
index dd720c9b0f6..21d939e3c34 100644
--- a/Tests/LibCompositing/CMakeLists.txt
+++ b/Tests/LibCompositing/CMakeLists.txt
@@ -4,6 +4,7 @@ set(TEST_SOURCES
TestOverlayGeometry.cpp
TestPausedDebuggerOverlay.cpp
TestPictureInPictureControls.cpp
+ TestScrollRequest.cpp
TestScrollState.cpp
TestSmoothScrollAnimation.cpp
TestTextureUpload.cpp
diff --git a/Tests/LibCompositing/TestScrollRequest.cpp b/Tests/LibCompositing/TestScrollRequest.cpp
new file mode 100644
index 00000000000..1120c62ca48
--- /dev/null
+++ b/Tests/LibCompositing/TestScrollRequest.cpp
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCompositing/Types.h>
+#include <LibTest/TestCase.h>
+
+using namespace Compositing;
+
+static RequestedRelativeScroll scroll_by(float delta, float minimum, float maximum)
+{
+ return {
+ .delta = { 0, delta },
+ .lower_bound = { 0, minimum },
+ .upper_bound = { 0, maximum },
+ };
+}
+
+// Applying each scroll in turn, clamped to the range it had.
+static float scrolled_in_turn(float offset, ReadonlySpan<RequestedRelativeScroll> scrolls)
+{
+ for (auto const& scroll : scrolls)
+ offset = scroll.applied_to({ 0, offset }).y();
+ return offset;
+}
+
+TEST_CASE(folded_relative_scrolls_clamp_where_each_did)
+{
+ Array scrolls {
+ scroll_by(300, 0, 500),
+ scroll_by(-450, 0, 500),
+ scroll_by(100, 0, 800),
+ };
+ auto folded = scrolls[0];
+ folded.then(scrolls[1]);
+ folded.then(scrolls[2]);
+
+ for (float offset : { 0.0f, 100.0f, 250.0f, 400.0f, 500.0f, 700.0f })
+ EXPECT_EQ(folded.applied_to({ 0, offset }).y(), scrolled_in_turn(offset, scrolls));
+}
+
+TEST_CASE(relative_scroll_after_a_jump_replaces_the_one_before)
+{
+ ScrollRequest request { .stable_node_id = {}, .request_id = 1, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = scroll_by(100, 0, 500), .smooth_scroll = {} };
+ ScrollRequest jump { .stable_node_id = {}, .request_id = 2, .jump_offset = Gfx::FloatPoint { 0, 40 }, .visual_viewport_scale = {}, .relative_scroll = scroll_by(10, 0, 500), .smooth_scroll = {} };
+ request.fold_later(jump);
+
+ EXPECT_EQ(request.request_id, 2u);
+ EXPECT_EQ(request.jump_offset, Gfx::FloatPoint(0, 40));
+ EXPECT_EQ(request.relative_scroll->applied_to({ 0, 40 }).y(), 50.0f);
+}
+
+TEST_CASE(later_relative_scroll_folds_into_the_one_before)
+{
+ ScrollRequest request { .stable_node_id = {}, .request_id = 1, .jump_offset = Gfx::FloatPoint { 0, 40 }, .visual_viewport_scale = {}, .relative_scroll = scroll_by(500, 0, 500), .smooth_scroll = {} };
+ ScrollRequest later { .stable_node_id = {}, .request_id = 2, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = scroll_by(-100, 0, 500), .smooth_scroll = {} };
+ request.fold_later(later);
+
+ EXPECT_EQ(request.jump_offset, Gfx::FloatPoint(0, 40));
+ EXPECT_EQ(request.relative_scroll->applied_to({ 0, 40 }).y(), 400.0f);
+}
+
+static RequestedSmoothScroll smooth_scroll(Gfx::FloatPoint destination_offset, bool destination_is_relative, bool retargets_operation)
+{
+ return {
+ .operation_id = first_operation_id_the_main_thread_requests + 1,
+ .destination_offset = destination_offset,
+ .destination_is_relative = destination_is_relative,
+ .animation_kind = ScrollAnimationKind::SmoothScroll,
+ .initiator = SmoothScrollInitiator::UserInput,
+ .retargets_operation = retargets_operation,
+ .settled_user_input_scroll_count = {},
+ };
+}
+
+TEST_CASE(later_step_of_an_unsent_operation_goes_on_from_where_it_was_headed)
+{
+ auto by_a_distance = smooth_scroll({ 0, 100 }, true, false);
+ by_a_distance.fold_later(smooth_scroll({ 0, 100 }, true, true));
+ EXPECT_EQ(by_a_distance.destination_offset, Gfx::FloatPoint(0, 200));
+ EXPECT(by_a_distance.destination_is_relative);
+ EXPECT(by_a_distance.retargets_operation);
+
+ auto to_an_offset = smooth_scroll({ 0, 300 }, false, false);
+ to_an_offset.fold_later(smooth_scroll({ 0, 100 }, true, true));
+ EXPECT_EQ(to_an_offset.destination_offset, Gfx::FloatPoint(0, 400));
+ EXPECT(!to_an_offset.destination_is_relative);
+
+ auto then_to_an_offset = smooth_scroll({ 0, 100 }, true, true);
+ then_to_an_offset.fold_later(smooth_scroll({ 0, 50 }, false, false));
+ EXPECT_EQ(then_to_an_offset.destination_offset, Gfx::FloatPoint(0, 50));
+ EXPECT(!then_to_an_offset.destination_is_relative);
+ EXPECT(then_to_an_offset.retargets_operation);
+}
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/key-step-after-an-unadopted-pan.txt b/Tests/LibWeb/Text/expected/async-scrolling/key-step-after-an-unadopted-pan.txt
new file mode 100644
index 00000000000..948ed6ab97e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/key-step-after-an-unadopted-pan.txt
@@ -0,0 +1 @@
+once the step has settled: 200
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/key-step-during-a-key-step.txt b/Tests/LibWeb/Text/expected/async-scrolling/key-step-during-a-key-step.txt
new file mode 100644
index 00000000000..b0e65abfe96
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/key-step-during-a-key-step.txt
@@ -0,0 +1 @@
+once the steps have settled: 200
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/relative-script-scroll-during-a-pan.txt b/Tests/LibWeb/Text/expected/async-scrolling/relative-script-scroll-during-a-pan.txt
new file mode 100644
index 00000000000..eb3bd3f9b0f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/relative-script-scroll-during-a-pan.txt
@@ -0,0 +1,3 @@
+right after scrolling by 50: 50
+once the compositor has the scroll: 150
+once the pan has ended: 150
diff --git a/Tests/LibWeb/Text/input/async-scrolling/key-step-after-an-unadopted-pan.html b/Tests/LibWeb/Text/input/async-scrolling/key-step-after-an-unadopted-pan.html
new file mode 100644
index 00000000000..25c064b0a89
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/key-step-after-an-unadopted-pan.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 5000px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ const busyWait = milliseconds => {
+ const end = performance.now() + milliseconds;
+ while (performance.now() < end) {}
+ };
+
+ // The compositor pans the page by 100 while the main thread is busy, so the main thread has not heard of the
+ // pan when an arrow key steps the page by another 100. The step goes on from where the pan took the page.
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ await animationFrame();
+ await animationFrame();
+ internals.wheelThroughUIProcess(50, 50, 0, 100, true, "ongoing");
+ const panEnded = internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended");
+ busyWait(200);
+ internals.sendKey(document.body, "Down");
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ await panEnded;
+ println(`once the step has settled: ${window.scrollY}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/key-step-during-a-key-step.html b/Tests/LibWeb/Text/input/async-scrolling/key-step-during-a-key-step.html
new file mode 100644
index 00000000000..e77d9533e3a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/key-step-during-a-key-step.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 5000px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ // The second step arrives while the compositor animates the first, and goes on from where the first is headed.
+ await scrollSettled(document, async () => {
+ internals.sendKey(document.body, "Down");
+ await animationFrame();
+ await animationFrame();
+ internals.sendKey(document.body, "Down");
+ });
+ println(`once the steps have settled: ${window.scrollY}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/relative-script-scroll-during-a-pan.html b/Tests/LibWeb/Text/input/async-scrolling/relative-script-scroll-during-a-pan.html
new file mode 100644
index 00000000000..cf4125fc27c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/relative-script-scroll-during-a-pan.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 5000px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ const busyWait = milliseconds => {
+ const end = performance.now() + milliseconds;
+ while (performance.now() < end) {}
+ };
+
+ // The compositor pans the page by 100 while the main thread is busy, so the main thread has not heard of the
+ // pan when the page scrolls itself by 50. The page goes 50 past where the pan took it, which is where it is.
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ internals.wheelThroughUIProcess(50, 50, 0, 100, true, "ongoing");
+ busyWait(200);
+ window.scrollBy(0, 50);
+ println(`right after scrolling by 50: ${window.scrollY}`);
+ while (window.scrollY === 50)
+ await new Promise(resolve => document.addEventListener("scroll", resolve, { once: true }));
+ println(`once the compositor has the scroll: ${window.scrollY}`);
+
+ await scrollSettled(document, () => internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended"));
+ println(`once the pan has ended: ${window.scrollY}`);
+ });
+</script>