Skip to content

Truly independent web browser

Everywhere: Split viewport scrolls by a distance in the compositor

Zaggy1024432fced4644c
diff --git a/Libraries/LibCompositing/Types.cpp b/Libraries/LibCompositing/Types.cpp
index 90a3721dfb5..da763e8d363 100644
--- a/Libraries/LibCompositing/Types.cpp
+++ b/Libraries/LibCompositing/Types.cpp
@@ -221,6 +221,7 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::ScrollRequest const& request
TRY(encoder.encode(request.jump_offset));
TRY(encoder.encode(request.visual_viewport_scale));
TRY(encoder.encode(request.relative_scroll));
+ TRY(encoder.encode(request.relative_scroll_moves_visual_viewport_first));
TRY(encoder.encode(request.instant_scroll_is_for_user_input));
TRY(encoder.encode(request.smooth_scroll));
return {};
@@ -235,6 +236,7 @@ ErrorOr<Compositing::ScrollRequest> decode(Decoder& decoder)
.jump_offset = TRY(decoder.decode<Optional<Gfx::FloatPoint>>()),
.visual_viewport_scale = TRY(decoder.decode<Optional<float>>()),
.relative_scroll = TRY(decoder.decode<Optional<Compositing::RequestedRelativeScroll>>()),
+ .relative_scroll_moves_visual_viewport_first = TRY(decoder.decode<bool>()),
.instant_scroll_is_for_user_input = TRY(decoder.decode<bool>()),
.smooth_scroll = TRY(decoder.decode<Optional<Compositing::RequestedSmoothScroll>>()),
};
diff --git a/Libraries/LibCompositing/Types.h b/Libraries/LibCompositing/Types.h
index b3223e046c3..a3945f13758 100644
--- a/Libraries/LibCompositing/Types.h
+++ b/Libraries/LibCompositing/Types.h
@@ -189,8 +189,10 @@ 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.
+ // A relative scroll after the jump. For the viewport, it can move the visual viewport's position in the page, which
+ // the visual viewport takes as much of as it can before the layout viewport takes the rest.
Optional<RequestedRelativeScroll> relative_scroll;
+ bool relative_scroll_moves_visual_viewport_first { false };
// Whether the instant scroll is the main thread's response to user input, rather than a script's or its own.
bool instant_scroll_is_for_user_input { false };
Optional<RequestedSmoothScroll> smooth_scroll;
@@ -205,11 +207,16 @@ struct ScrollRequest {
jump_offset = later.jump_offset;
visual_viewport_scale = later.visual_viewport_scale;
relative_scroll = later.relative_scroll;
+ relative_scroll_moves_visual_viewport_first = later.relative_scroll_moves_visual_viewport_first;
} else if (later.relative_scroll.has_value()) {
- if (relative_scroll.has_value())
+ // A box is scrolled by distances of one kind: the viewport by its position in the page, the rest by offset.
+ if (relative_scroll.has_value()) {
+ VERIFY(relative_scroll_moves_visual_viewport_first == later.relative_scroll_moves_visual_viewport_first);
relative_scroll->then(later.relative_scroll.value());
- else
+ } else {
relative_scroll = later.relative_scroll;
+ relative_scroll_moves_visual_viewport_first = later.relative_scroll_moves_visual_viewport_first;
+ }
}
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());
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index e9dcdd90f9a..c544bfb1656 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -5420,7 +5420,13 @@ void LocalNavigable::re_snap_scroll_containers_after_layout_change(Layout::Begun
: element_for_async_scroll_node_stable_id(*document, *stable_node_id);
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*document) };
- perform_a_scroll_of_a_scrolling_box(*stable_node_id, snap_destination.position, behavior, associated_element, ScrollTrigger::Programmatic, {}, DestinationSnapping::DestinationIsSnapPosition);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, {
+ .position = snap_destination.position,
+ .behavior = behavior,
+ .trigger = ScrollTrigger::Programmatic,
+ .associated_element = associated_element,
+ .destination_snapping = DestinationSnapping::DestinationIsSnapPosition,
+ });
}
// A deferred snap container re-snaps once the scroll that owns it completes and settlement runs this again.
@@ -5593,7 +5599,11 @@ void LocalNavigable::user_scroll_did_settle(UserScrollSettlement settlement)
if (snap_destination.position != *current_scroll_offset) {
// The snap animation queues this target's scrollend event once it completes.
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*document) };
- perform_a_scroll_of_a_scrolling_box(*stable_node_id, snap_destination.position, Bindings::ScrollBehavior::Smooth, nullptr, ScrollTrigger::GestureEndSnap);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, {
+ .position = snap_destination.position,
+ .behavior = Bindings::ScrollBehavior::Smooth,
+ .trigger = ScrollTrigger::GestureEndSnap,
+ });
continue;
}
}
@@ -7078,7 +7088,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 = {}, .relative_scroll = {}, .instant_scroll_is_for_user_input = false, .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 = {}, .relative_scroll_moves_visual_viewport_first = false, .instant_scroll_is_for_user_input = false, .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);
@@ -7120,6 +7130,7 @@ void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_nod
drop_unsent_smooth_scroll(scroll_request);
scroll_request.jump_offset = device_scroll_offset(scroll_offset.value());
scroll_request.relative_scroll.clear();
+ scroll_request.relative_scroll_moves_visual_viewport_first = false;
scroll_request.instant_scroll_is_for_user_input = trigger != ScrollTrigger::Programmatic;
if (stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport)
scroll_request.visual_viewport_scale = active_document()->visual_viewport()->scale();
@@ -7153,6 +7164,7 @@ void LocalNavigable::request_relative_scroll(Web::AsyncScrollNodeStableID stable
}
auto& scroll_request = unsent_scroll_request_for(stable_node_id);
drop_unsent_smooth_scroll(scroll_request);
+ VERIFY(!scroll_request.relative_scroll_moves_visual_viewport_first);
Compositing::RequestedRelativeScroll relative_scroll {
.delta = device_scroll_offset(displacement),
.lower_bound = device_scroll_offset(Painting::minimum_scroll_offset(*scrolling_box)),
@@ -7165,6 +7177,35 @@ void LocalNavigable::request_relative_scroll(Web::AsyncScrollNodeStableID stable
scroll_request.instant_scroll_is_for_user_input = trigger != ScrollTrigger::Programmatic;
}
+void LocalNavigable::request_viewport_scroll_by_a_distance(CSSPixelPoint displacement, double visual_viewport_max_x, double visual_viewport_max_y, ScrollTrigger trigger)
+{
+ auto document = active_document();
+ Layout::ForcedReadScope read { *document };
+ Web::AsyncScrollNodeStableID viewport_stable_node_id { .node_id = document->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport };
+ auto const* viewport_box = document->layout_node(read);
+ if (!viewport_box || !Painting::has_committed_box(*viewport_box)) {
+ request_scroll_jump(viewport_stable_node_id, trigger);
+ return;
+ }
+ auto& scroll_request = unsent_scroll_request_for(viewport_stable_node_id);
+ drop_unsent_smooth_scroll(scroll_request);
+ VERIFY(!scroll_request.relative_scroll.has_value() || scroll_request.relative_scroll_moves_visual_viewport_first);
+ // The visual viewport's position in the page ranges over the layout viewport's scroll range, plus how far the
+ // visual viewport moves within the layout viewport.
+ auto maximum_page_position = Painting::maximum_scroll_offset(*viewport_box) + CSSPixelPoint { visual_viewport_max_x, visual_viewport_max_y };
+ Compositing::RequestedRelativeScroll relative_scroll {
+ .delta = device_scroll_offset(displacement),
+ .lower_bound = device_scroll_offset(Painting::minimum_scroll_offset(*viewport_box)),
+ .upper_bound = device_scroll_offset(maximum_page_position),
+ };
+ if (scroll_request.relative_scroll.has_value())
+ scroll_request.relative_scroll->then(relative_scroll);
+ else
+ scroll_request.relative_scroll = relative_scroll;
+ scroll_request.relative_scroll_moves_visual_viewport_first = true;
+ scroll_request.instant_scroll_is_for_user_input = trigger != ScrollTrigger::Programmatic;
+}
+
bool LocalNavigable::set_scroll_offset_for_user_input(Layout::Node& scrolling_box, Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint scroll_offset)
{
bool did_scroll = false;
@@ -7224,11 +7265,29 @@ void LocalNavigable::abort_in_flight_smooth_scrolls_taken_over_by_user_input(Web
abort_in_flight_smooth_scrolls(stable_node_id, SmoothScrollAbortCause::TakenOverByUserInput);
}
-GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint position, Bindings::ScrollBehavior behavior, GC::Ptr<DOM::Element> associated_element, ScrollTrigger trigger, Optional<CSSPixelPoint> relative_displacement, DestinationSnapping destination_snapping, Compositing::ScrollAnimationKind animation_kind, Painting::ScrollKind scroll_kind)
+static bool scrolls_smoothly(Bindings::ScrollBehavior behavior, GC::Ptr<DOM::Element> associated_element)
+{
+ if (behavior == Bindings::ScrollBehavior::Auto && associated_element) {
+ if (auto const* values = associated_element->style_group<CSS::ComputedValues::MiscResetValues>())
+ return static_cast<CSS::ScrollBehavior>(values->scroll_behavior) == CSS::ScrollBehavior::Smooth;
+ }
+ return behavior == Bindings::ScrollBehavior::Smooth;
+}
+
+LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web::AsyncScrollNodeStableID stable_node_id, ScrollOfScrollingBox const& scroll)
{
auto document = active_document();
VERIFY(document);
+ auto position = scroll.position;
+ auto behavior = scroll.behavior;
+ auto trigger = scroll.trigger;
+ auto const& associated_element = scroll.associated_element;
+ auto const& relative_displacement = scroll.relative_displacement;
+ auto destination_snapping = scroll.destination_snapping;
+ auto animation_kind = scroll.animation_kind;
+ auto scroll_kind = scroll.scroll_kind;
+
// The scroll that settles a gesture is the user's, and runs as one with the user's scrolls in flight.
bool settles_user_scroll_gesture = trigger == ScrollTrigger::GestureEndSnap;
if (settles_user_scroll_gesture)
@@ -7248,7 +7307,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
auto initial_scroll_offset = scroll_offset_for(stable_node_id);
if (!initial_scroll_offset.has_value())
- return WebIDL::create_resolved_promise_for(*document, JS::js_undefined());
+ return { .promise = 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.
@@ -7286,11 +7345,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
record_relative_scroll(*document, stable_node_id, relative_scroll_start, Painting::clamp_scroll_offset(*scrolling_box, position));
}
- auto should_scroll_smoothly = behavior == Bindings::ScrollBehavior::Smooth;
- if (behavior == Bindings::ScrollBehavior::Auto && associated_element) {
- if (auto const* values = associated_element->style_group<CSS::ComputedValues::MiscResetValues>())
- should_scroll_smoothly = static_cast<CSS::ScrollBehavior>(values->scroll_behavior) == CSS::ScrollBehavior::Smooth;
- }
+ auto should_scroll_smoothly = scrolls_smoothly(behavior, associated_element);
// AD-HOC: A smooth scroll requested while a smooth scroll of the same scrolling box toward the same position is in
// flight continues that scroll instead of restarting it, matching other engines.
@@ -7298,7 +7353,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
if (auto* promises = promises_of_smooth_scroll_in_flight_toward(stable_node_id, position, trigger, animation_kind)) {
auto scroll_promise = WebIDL::create_promise_for(*document);
promises->append(scroll_promise);
- return scroll_promise;
+ return { scroll_promise, goes_to_a_snap_position };
}
}
@@ -7306,13 +7361,13 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
// 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, relative_request_displacement, animation_kind))
- return *scroll_promise;
+ return { *scroll_promise, goes_to_a_snap_position };
}
// AD-HOC: A wheel step's gesture delivers the scrollend event once no further steps arrive.
if (should_scroll_smoothly && trigger == ScrollTrigger::UserInput && animation_kind == Compositing::ScrollAnimationKind::Wheel) {
if (position == *initial_scroll_offset)
- return WebIDL::create_resolved_promise_for(*document, JS::js_undefined());
+ return { WebIDL::create_resolved_promise_for(*document, JS::js_undefined()), goes_to_a_snap_position };
if (auto target = scroll_event_target_for_async_scroll_node(*document, stable_node_id)) {
if (!latched_user_scroll_gesture_for(*target, stable_node_id))
@@ -7340,7 +7395,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
}
// 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()) {
+ if (has_compositor_context() && scroll.instant_scroll_requester == InstantScrollRequester::ThisScroll) {
if (relative_request_displacement.has_value())
request_relative_scroll(stable_node_id, relative_request_displacement.value(), trigger);
else
@@ -7349,7 +7404,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
if (did_scroll)
queue_scrollend_event(stable_node_id, trigger, initial_scroll_offset);
WebIDL::resolve_promise(scroll_promise);
- return scroll_promise;
+ return { scroll_promise, goes_to_a_snap_position };
}
// NB: The compositor has no scroll node for the visual viewport, so the main thread animates it.
@@ -7376,12 +7431,12 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
.trigger = trigger,
.animation_kind = animation_kind,
});
- return scroll_promise;
+ return { scroll_promise, goes_to_a_snap_position };
}
if (position == *initial_scroll_offset) {
WebIDL::resolve_promise(scroll_promise);
- return scroll_promise;
+ return { scroll_promise, goes_to_a_snap_position };
}
m_main_thread_smooth_scrolls.append(MainThreadSmoothScroll {
.stable_node_id = stable_node_id,
@@ -7394,7 +7449,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
.trigger = trigger,
});
page().client().request_frame();
- return scroll_promise;
+ return { scroll_promise, goes_to_a_snap_position };
}
Gfx::FloatPoint LocalNavigable::device_scroll_offset(CSSPixelPoint offset) const
@@ -7450,11 +7505,15 @@ GC::Ptr<WebIDL::Promise> LocalNavigable::continue_in_flight_user_smooth_scroll(W
GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_an_element(DOM::Element& element, CSSPixelPoint position, Bindings::ScrollBehavior behavior, Optional<CSSPixelPoint> relative_displacement)
{
- return perform_a_scroll_of_a_scrolling_box({
- .node_id = element.unique_id(),
- .kind = Web::AsyncScrollNodeKind::Element,
- },
- position, behavior, element, ScrollTrigger::Programmatic, relative_displacement);
+ Web::AsyncScrollNodeStableID stable_node_id { .node_id = element.unique_id(), .kind = Web::AsyncScrollNodeKind::Element };
+ return perform_a_scroll_of_a_scrolling_box(stable_node_id, {
+ .position = position,
+ .behavior = behavior,
+ .trigger = ScrollTrigger::Programmatic,
+ .associated_element = element,
+ .relative_displacement = relative_displacement,
+ })
+ .promise;
}
bool LocalNavigable::perform_a_scroll_step_for_key_input(Layout::Node& scroll_container, CSSPixelPoint delta, Compositing::SnapSelectionStrategy::Type strategy_type)
@@ -7490,7 +7549,13 @@ 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, delta, DestinationSnapping::SelectSnapPosition, Compositing::ScrollAnimationKind::SmoothScroll, Painting::ScrollKind::Relative);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, {
+ .position = destination,
+ .behavior = Bindings::ScrollBehavior::Smooth,
+ .trigger = ScrollTrigger::UserInput,
+ .relative_displacement = delta,
+ .scroll_kind = Painting::ScrollKind::Relative,
+ });
return true;
}
@@ -7562,7 +7627,13 @@ bool LocalNavigable::perform_a_snapped_relative_user_scroll(Layout::Node& scroll
return true;
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*document) };
- perform_a_scroll_of_a_scrolling_box(*stable_node_id, snap_destination.position, Bindings::ScrollBehavior::Smooth, nullptr, ScrollTrigger::UserInput, {}, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, {
+ .position = snap_destination.position,
+ .behavior = Bindings::ScrollBehavior::Smooth,
+ .trigger = ScrollTrigger::UserInput,
+ .animation_kind = animation_kind,
+ .scroll_kind = scroll_kind,
+ });
return true;
}
@@ -7655,7 +7726,14 @@ bool LocalNavigable::scroll_scrolling_box_by_delta(Layout::Node& scrolling_box,
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, relative_displacement, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
+ perform_a_scroll_of_a_scrolling_box(*stable_node_id, {
+ .position = position,
+ .behavior = Bindings::ScrollBehavior::Smooth,
+ .trigger = ScrollTrigger::UserInput,
+ .relative_displacement = relative_displacement,
+ .animation_kind = animation_kind,
+ .scroll_kind = scroll_kind,
+ });
return true;
}
@@ -7749,9 +7827,35 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix
if (visual_delta.is_zero())
doc->set_needs_repaint(Badge<HTML::LocalNavigable> {}, InvalidateDisplayList::No);
Web::AsyncScrollNodeStableID visual_viewport_stable_node_id { .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::VisualViewport };
- auto scroll_promise_2 = perform_a_scroll_of_a_scrolling_box(visual_viewport_stable_node_id, vv->offset() + visual_delta, behavior, doc->document_element(), trigger, {}, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
-
- auto scroll_promise_1 = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind);
+ Web::AsyncScrollNodeStableID viewport_stable_node_id { .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport };
+
+ // NB: The compositor may have the visual viewport somewhere this navigable has yet to hear of, so it splits an
+ // instant scroll by a distance itself, from where it has both viewports. The split here is what the main thread
+ // sees until the compositor answers.
+ bool compositor_splits_the_scroll = has_compositor_context() && relative_displacement.has_value() && !scrolls_smoothly(behavior, doc->document_element());
+ auto instant_scroll_requester = compositor_splits_the_scroll ? InstantScrollRequester::Caller : InstantScrollRequester::ThisScroll;
+
+ auto visual_viewport_scroll = perform_a_scroll_of_a_scrolling_box(visual_viewport_stable_node_id, {
+ .position = vv->offset() + visual_delta,
+ .behavior = behavior,
+ .trigger = trigger,
+ .associated_element = doc->document_element(),
+ .animation_kind = animation_kind,
+ .scroll_kind = scroll_kind,
+ .instant_scroll_requester = instant_scroll_requester,
+ });
+
+ auto layout_viewport_scroll = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind, instant_scroll_requester);
+
+ if (compositor_splits_the_scroll) {
+ // A layout viewport that came to rest at a snap position goes there, and the visual viewport with it.
+ if (layout_viewport_scroll.destination_is_a_snap_position) {
+ request_scroll_jump(viewport_stable_node_id, trigger);
+ request_scroll_jump(visual_viewport_stable_node_id, trigger);
+ } else {
+ request_viewport_scroll_by_a_distance(relative_displacement.value(), max_x, max_y, trigger);
+ }
+ }
// 16. Let scrollPromise be a new Promise.
auto scroll_promise = WebIDL::create_promise_for(*doc);
@@ -7761,13 +7865,13 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix
auto& realm = HTML::relevant_realm(*doc);
TemporaryExecutionContext execution_context_for_promise_reactions { realm, TemporaryExecutionContext::CallbacksEnabled::Yes };
WebIDL::wait_for_all(
- realm, { { scroll_promise_1, scroll_promise_2 } },
+ realm, { { layout_viewport_scroll.promise, visual_viewport_scroll.promise } },
[scroll_promise](auto const&) { WebIDL::resolve_promise(scroll_promise); },
[scroll_promise](JS::Value) { WebIDL::resolve_promise(scroll_promise); });
return scroll_promise;
}
-GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_layout_viewport(CSSPixelPoint delta, Bindings::ScrollBehavior behavior, ScrollTrigger trigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind scroll_kind, Compositing::ScrollAnimationKind animation_kind)
+LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_the_layout_viewport(CSSPixelPoint delta, Bindings::ScrollBehavior behavior, ScrollTrigger trigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind scroll_kind, Compositing::ScrollAnimationKind animation_kind, InstantScrollRequester instant_scroll_requester)
{
auto doc = active_document();
@@ -7783,10 +7887,10 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_layout_viewport
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.
+ Web::AsyncScrollNodeStableID viewport_stable_node_id { .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport };
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();
@@ -7794,11 +7898,16 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_layout_viewport
}
TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*doc) };
- return perform_a_scroll_of_a_scrolling_box({
- .node_id = doc->unique_id(),
- .kind = Web::AsyncScrollNodeKind::Viewport,
- },
- new_viewport_scroll_offset.to_type<CSSPixels>(), behavior, doc->document_element(), trigger, relative_displacement, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
+ return perform_a_scroll_of_a_scrolling_box(viewport_stable_node_id, {
+ .position = new_viewport_scroll_offset.to_type<CSSPixels>(),
+ .behavior = behavior,
+ .trigger = trigger,
+ .associated_element = doc->document_element(),
+ .relative_displacement = relative_displacement,
+ .animation_kind = animation_kind,
+ .scroll_kind = scroll_kind,
+ .instant_scroll_requester = instant_scroll_requester,
+ });
}
void LocalNavigable::reset_zoom()
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 398908bf827..7b668200940 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -499,8 +499,30 @@ private:
SelectSnapPosition,
DestinationIsSnapPosition,
};
- GC::Ref<WebIDL::Promise> perform_a_scroll_of_a_scrolling_box(Web::AsyncScrollNodeStableID, CSSPixelPoint position, Bindings::ScrollBehavior, GC::Ptr<DOM::Element> associated_element, ScrollTrigger, Optional<CSSPixelPoint> relative_displacement = {}, DestinationSnapping = DestinationSnapping::SelectSnapPosition, Compositing::ScrollAnimationKind = Compositing::ScrollAnimationKind::SmoothScroll, Painting::ScrollKind = Painting::ScrollKind::Absolute);
- GC::Ref<WebIDL::Promise> perform_a_scroll_of_the_layout_viewport(CSSPixelPoint delta, Bindings::ScrollBehavior, ScrollTrigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind, Compositing::ScrollAnimationKind);
+ // Who asks the compositor for the instant part of a scroll: the scroll itself, or a caller that asks for several
+ // scrolls as one.
+ enum class InstantScrollRequester {
+ ThisScroll,
+ Caller,
+ };
+ struct ScrollOfScrollingBox {
+ CSSPixelPoint position;
+ Bindings::ScrollBehavior behavior;
+ ScrollTrigger trigger;
+ GC::Ptr<DOM::Element> associated_element {};
+ Optional<CSSPixelPoint> relative_displacement {};
+ DestinationSnapping destination_snapping { DestinationSnapping::SelectSnapPosition };
+ Compositing::ScrollAnimationKind animation_kind { Compositing::ScrollAnimationKind::SmoothScroll };
+ Painting::ScrollKind scroll_kind { Painting::ScrollKind::Absolute };
+ InstantScrollRequester instant_scroll_requester { InstantScrollRequester::ThisScroll };
+ };
+ struct PerformedScroll {
+ GC::Ref<WebIDL::Promise> promise;
+ // Whether the scroll went to a snap position, which a scroll by a distance then goes to as an offset.
+ bool destination_is_a_snap_position { false };
+ };
+ PerformedScroll perform_a_scroll_of_a_scrolling_box(Web::AsyncScrollNodeStableID, ScrollOfScrollingBox const&);
+ PerformedScroll perform_a_scroll_of_the_layout_viewport(CSSPixelPoint delta, Bindings::ScrollBehavior, ScrollTrigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind, Compositing::ScrollAnimationKind, InstantScrollRequester);
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;
@@ -685,6 +707,7 @@ private:
u64 last_scroll_request_id_for(Web::AsyncScrollNodeStableID) const;
void request_scroll_jump(Web::AsyncScrollNodeStableID, ScrollTrigger);
void request_relative_scroll(Web::AsyncScrollNodeStableID, CSSPixelPoint displacement, ScrollTrigger);
+ void request_viewport_scroll_by_a_distance(CSSPixelPoint displacement, double visual_viewport_max_x, double visual_viewport_max_y, ScrollTrigger);
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/ContextState.cpp b/Services/Compositor/ContextState.cpp
index f697b61fcc2..d8d89d6818e 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -1106,8 +1106,12 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
// 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 (scroll_request.relative_scroll_moves_visual_viewport_first && m_async_scroll_tree.scroll_node_is_viewport(node_id.value())) {
+ scroll_visual_viewport_position_in_page(node_id.value(), relative_scroll.value(), answers);
+ } else {
+ 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 (axis_held_by_scrollbar.has_value() && instant_scroll_is_for_script) {
auto offset = m_async_scroll_tree.scroll_offset_for_node(node_id.value(), m_scroll_state_snapshot).value();
@@ -1175,6 +1179,46 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
return PendingFrame::repainting_changes(m_async_scrolling_viewport_rect);
}
+// https://drafts.csswg.org/cssom-view/#viewport-perform-a-scroll
+// The visual viewport takes as much of the scroll as it can before the layout viewport takes the rest.
+void ContextState::scroll_visual_viewport_position_in_page(Compositing::AsyncScrollNodeID viewport_node_id, Compositing::RequestedRelativeScroll const& relative_scroll, Vector<Compositing::AsyncScrollOffset>& answers)
+{
+ auto layout_viewport_offset = m_async_scroll_tree.scroll_offset_for_node(viewport_node_id, m_scroll_state_snapshot).value();
+ auto transform = m_scene.visual_viewport_transform();
+ auto scale = transform.matrix[0, 0];
+ auto old_visual_viewport_offset = visual_viewport_scroll_offset(transform);
+ auto page_position = layout_viewport_offset.translated(old_visual_viewport_offset);
+ auto delta = relative_scroll.applied_to(page_position) - page_position;
+
+ auto share_of_viewport_the_visual_viewport_moves_over = scale <= 1.0f ? 0.0f : (scale - 1.0f) / scale;
+ Gfx::FloatPoint maximum_visual_viewport_offset {
+ static_cast<float>(m_async_scrolling_viewport_rect.width()) * share_of_viewport_the_visual_viewport_moves_over,
+ static_cast<float>(m_async_scrolling_viewport_rect.height()) * share_of_viewport_the_visual_viewport_moves_over,
+ };
+ Gfx::FloatPoint new_visual_viewport_offset {
+ AK::clamp(old_visual_viewport_offset.x() + delta.x(), 0.0f, maximum_visual_viewport_offset.x()),
+ AK::clamp(old_visual_viewport_offset.y() + delta.y(), 0.0f, maximum_visual_viewport_offset.y()),
+ };
+ auto visual_delta = new_visual_viewport_offset - old_visual_viewport_offset;
+ (void)m_async_scroll_tree.set_scroll_offset(viewport_node_id, layout_viewport_offset.translated(delta - visual_delta), m_scene.current_visual_context_tree(), m_scroll_state_snapshot);
+ if (visual_delta.is_zero())
+ return;
+
+ transform.matrix[0, 3] = -new_visual_viewport_offset.x() * scale;
+ transform.matrix[1, 3] = -new_visual_viewport_offset.y() * scale;
+ m_scene.set_async_visual_viewport_transform(transform);
+ m_visual_viewport_document_id = m_async_scroll_tree.document_id();
+ answers.append({
+ .stable_node_id = { .node_id = m_async_scroll_tree.document_id().value(), .kind = Web::AsyncScrollNodeKind::VisualViewport },
+ .compositor_scroll_offset = new_visual_viewport_offset,
+ .unadopted_scroll_delta = visual_delta,
+ .last_relative_scroll_delta = {},
+ .visual_viewport_scale = scale,
+ .applied_scroll_request_id = 0,
+ .user_input_scroll_count = 0,
+ });
+}
+
void ContextState::apply_visual_viewport_scroll_request(Compositing::ScrollRequest const& scroll_request, Vector<Compositing::AsyncScrollOffset>& answers)
{
// The main thread animates the visual viewport itself, one jump at a time.
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 12caf8affa2..75239dcf84d 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -276,6 +276,7 @@ private:
Optional<Gfx::Orientation> axis_held_by_scrollbar_of(Compositing::AsyncScrollNodeID) const;
// A script that scrolls the box a gesture is latched to stops the momentum the gesture carries on with.
void stop_user_scroll_carried_on_by_momentum_of(Web::AsyncScrollNodeStableID);
+ void scroll_visual_viewport_position_in_page(Compositing::AsyncScrollNodeID viewport_node_id, Compositing::RequestedRelativeScroll const&, Vector<Compositing::AsyncScrollOffset>& answers);
void apply_visual_viewport_scroll_request(Compositing::ScrollRequest const&, Vector<Compositing::AsyncScrollOffset>& answers);
// Pans the visual viewport of a zoomed page by as much of a delta in screen pixels as it has room for, and returns
// that part of the delta.
diff --git a/Tests/Compositor/TestContextState.cpp b/Tests/Compositor/TestContextState.cpp
index 27b45f8e63a..aa099801d6e 100644
--- a/Tests/Compositor/TestContextState.cpp
+++ b/Tests/Compositor/TestContextState.cpp
@@ -112,6 +112,7 @@ TEST_CASE(pinch_zoom_is_presented_until_the_main_thread_asks_for_another)
.jump_offset = Gfx::FloatPoint {},
.visual_viewport_scale = 1.0f,
.relative_scroll = {},
+ .relative_scroll_moves_visual_viewport_first = false,
.instant_scroll_is_for_user_input = false,
.smooth_scroll = {},
};
diff --git a/Tests/LibCompositing/TestScrollRequest.cpp b/Tests/LibCompositing/TestScrollRequest.cpp
index b75f65081a4..dbf03ed3645 100644
--- a/Tests/LibCompositing/TestScrollRequest.cpp
+++ b/Tests/LibCompositing/TestScrollRequest.cpp
@@ -43,8 +43,8 @@ TEST_CASE(folded_relative_scrolls_clamp_where_each_did)
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), .instant_scroll_is_for_user_input = false, .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), .instant_scroll_is_for_user_input = false, .smooth_scroll = {} };
+ ScrollRequest request { .stable_node_id = {}, .request_id = 1, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = scroll_by(100, 0, 500), .relative_scroll_moves_visual_viewport_first = false, .instant_scroll_is_for_user_input = false, .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), .relative_scroll_moves_visual_viewport_first = false, .instant_scroll_is_for_user_input = false, .smooth_scroll = {} };
request.fold_later(jump);
EXPECT_EQ(request.request_id, 2u);
@@ -54,8 +54,8 @@ TEST_CASE(relative_scroll_after_a_jump_replaces_the_one_before)
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), .instant_scroll_is_for_user_input = false, .smooth_scroll = {} };
- ScrollRequest later { .stable_node_id = {}, .request_id = 2, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = scroll_by(-100, 0, 500), .instant_scroll_is_for_user_input = false, .smooth_scroll = {} };
+ ScrollRequest request { .stable_node_id = {}, .request_id = 1, .jump_offset = Gfx::FloatPoint { 0, 40 }, .visual_viewport_scale = {}, .relative_scroll = scroll_by(500, 0, 500), .relative_scroll_moves_visual_viewport_first = false, .instant_scroll_is_for_user_input = false, .smooth_scroll = {} };
+ ScrollRequest later { .stable_node_id = {}, .request_id = 2, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = scroll_by(-100, 0, 500), .relative_scroll_moves_visual_viewport_first = false, .instant_scroll_is_for_user_input = false, .smooth_scroll = {} };
request.fold_later(later);
EXPECT_EQ(request.jump_offset, Gfx::FloatPoint(0, 40));
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/viewport-scroll-by-a-distance-while-zoomed.txt b/Tests/LibWeb/Text/expected/async-scrolling/viewport-scroll-by-a-distance-while-zoomed.txt
new file mode 100644
index 00000000000..fc85489d452
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/viewport-scroll-by-a-distance-while-zoomed.txt
@@ -0,0 +1,2 @@
+visual viewport offset: 80
+layout viewport offset: 0
diff --git a/Tests/LibWeb/Text/input/async-scrolling/viewport-scroll-by-a-distance-while-zoomed.html b/Tests/LibWeb/Text/input/async-scrolling/viewport-scroll-by-a-distance-while-zoomed.html
new file mode 100644
index 00000000000..7a6a81fe743
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/viewport-scroll-by-a-distance-while-zoomed.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 3000px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ const vv = window.visualViewport;
+ internals.pinchThroughUIProcess(0, 0, 1.0);
+ while (vv.scale === 1)
+ await animationFrame();
+ await animationFrame();
+ await animationFrame();
+
+ const busyWait = milliseconds => {
+ const end = performance.now() + milliseconds;
+ while (performance.now() < end) {}
+ };
+
+ // The compositor pans the zoomed visual viewport by 50 while the main thread is busy, so the main thread has not
+ // heard of it when the page scrolls itself by 30. The visual viewport takes that from where the pan left it.
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ await animationFrame();
+ await animationFrame();
+ internals.wheelThroughUIProcess(50, 50, 0, 100, true, "ongoing");
+ busyWait(200);
+ window.scrollBy(0, 30);
+ for (let frame = 0; frame < 4; frame++)
+ await animationFrame();
+ println(`visual viewport offset: ${vv.offsetTop}`);
+ println(`layout viewport offset: ${window.scrollY}`);
+ await internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended");
+ });
+</script>