Skip to content

Truly independent web browser

Everywhere: Decide what a script's scroll does to the user's scrolling

Zaggy1024c1dfa25674a5
diff --git a/Libraries/LibCompositing/Scrolling/SmoothScrollAnimation.h b/Libraries/LibCompositing/Scrolling/SmoothScrollAnimation.h
index 165104cfab4..c0a7f61ee46 100644
--- a/Libraries/LibCompositing/Scrolling/SmoothScrollAnimation.h
+++ b/Libraries/LibCompositing/Scrolling/SmoothScrollAnimation.h
@@ -33,6 +33,12 @@ public:
Sample sample(AK::Duration elapsed) const;
void retarget(Gfx::FloatPoint destination_offset, AK::Duration elapsed);
+ // Moves where the scroll comes from and goes to by the same distance, which keeps its pace.
+ void shift_by(Gfx::FloatPoint delta)
+ {
+ m_start_offset.translate_by(delta);
+ m_destination_offset.translate_by(delta);
+ }
private:
AK::Duration duration_for_distance(Gfx::FloatPoint distance) const;
diff --git a/Libraries/LibCompositing/Types.cpp b/Libraries/LibCompositing/Types.cpp
index 94dd605c92b..90a3721dfb5 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.instant_scroll_is_for_user_input));
TRY(encoder.encode(request.smooth_scroll));
return {};
}
@@ -234,6 +235,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>>()),
+ .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 65c39b4e25d..b3223e046c3 100644
--- a/Libraries/LibCompositing/Types.h
+++ b/Libraries/LibCompositing/Types.h
@@ -191,12 +191,16 @@ struct ScrollRequest {
Optional<float> visual_viewport_scale;
// A relative scroll after the jump.
Optional<RequestedRelativeScroll> relative_scroll;
+ // 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;
// A request made later in the same frame aborts the smooth scroll of this one, which never got under way.
void fold_later(ScrollRequest const& later)
{
request_id = later.request_id;
+ if (later.jump_offset.has_value() || later.relative_scroll.has_value())
+ instant_scroll_is_for_user_input = later.instant_scroll_is_for_user_input;
if (later.jump_offset.has_value()) {
jump_offset = later.jump_offset;
visual_viewport_scale = later.visual_viewport_scale;
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 5ec66c38e3d..e9dcdd90f9a 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -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 = {}, .relative_scroll = {}, .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 = {}, .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);
@@ -7111,7 +7111,7 @@ void LocalNavigable::drop_unsent_smooth_scroll(Compositing::ScrollRequest& scrol
queue_scrollend_event_and_promise_resolution_for_finished_scroll(finished->stable_node_id, finished->trigger, {}, finished->promises);
}
-void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_node_id)
+void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_node_id, ScrollTrigger trigger)
{
auto scroll_offset = scroll_offset_for(stable_node_id);
if (!scroll_offset.has_value())
@@ -7120,6 +7120,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.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();
}
@@ -7141,13 +7142,13 @@ 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)
+void LocalNavigable::request_relative_scroll(Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint displacement, ScrollTrigger trigger)
{
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);
+ request_scroll_jump(stable_node_id, trigger);
return;
}
auto& scroll_request = unsent_scroll_request_for(stable_node_id);
@@ -7161,13 +7162,26 @@ void LocalNavigable::request_relative_scroll(Web::AsyncScrollNodeStableID stable
scroll_request.relative_scroll->then(relative_scroll);
else
scroll_request.relative_scroll = relative_scroll;
+ 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;
+ {
+ TemporaryChange requesting_scroll { m_scroll_offset_changes_need_no_request, true };
+ did_scroll = Painting::set_scroll_offset(scrolling_box, scroll_offset) == Painting::ScrollHandled::Yes;
+ }
+ if (did_scroll && has_compositor_context())
+ request_scroll_jump(stable_node_id, ScrollTrigger::UserInput);
+ return did_scroll;
}
void LocalNavigable::did_change_scroll_offset(Web::AsyncScrollNodeStableID stable_node_id)
{
if (m_scroll_offset_changes_need_no_request || !has_compositor_context())
return;
- request_scroll_jump(stable_node_id);
+ request_scroll_jump(stable_node_id, ScrollTrigger::Programmatic);
}
Vector<Compositing::ScrollRequest> LocalNavigable::take_unsent_scroll_requests()
@@ -7328,9 +7342,9 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web
// position even where its offset here is unchanged.
if (has_compositor_context()) {
if (relative_request_displacement.has_value())
- request_relative_scroll(stable_node_id, relative_request_displacement.value());
+ request_relative_scroll(stable_node_id, relative_request_displacement.value(), trigger);
else
- request_scroll_jump(stable_node_id);
+ request_scroll_jump(stable_node_id, trigger);
}
if (did_scroll)
queue_scrollend_event(stable_node_id, trigger, initial_scroll_offset);
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index fb9dffdd156..398908bf827 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -249,6 +249,8 @@ public:
void perform_scroll_of_viewport_scrolling_box(CSSPixelPoint position);
// Every change of a scroll offset here, other than one the compositor made, goes to the compositor as a request.
void did_change_scroll_offset(Web::AsyncScrollNodeStableID);
+ // Changes a scrolling box's offset for user input on this thread, which the compositor is asked for as the user's.
+ bool set_scroll_offset_for_user_input(Layout::Node& scrolling_box, Web::AsyncScrollNodeStableID, CSSPixelPoint);
void adopt_pending_async_scroll_offsets(Compositing::AsyncScrollUpdateFreshness = Compositing::AsyncScrollUpdateFreshness::Pushed);
void adopt_started_user_scroll(DOM::Document&, Compositing::StartedUserScroll const&);
void process_main_thread_smooth_scrolls();
@@ -681,8 +683,8 @@ private:
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_scroll_jump(Web::AsyncScrollNodeStableID, ScrollTrigger);
+ void request_relative_scroll(Web::AsyncScrollNodeStableID, CSSPixelPoint displacement, 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/Libraries/LibWeb/Painting/Scrolling.cpp b/Libraries/LibWeb/Painting/Scrolling.cpp
index e1eb5fa723d..c57a868d5a9 100644
--- a/Libraries/LibWeb/Painting/Scrolling.cpp
+++ b/Libraries/LibWeb/Painting/Scrolling.cpp
@@ -213,7 +213,13 @@ ScrollHandled set_scroll_offset_from_user_input(Layout::Node& node, CSSPixelPoin
if (navigable && stable_node_id.has_value())
navigable->abort_in_flight_smooth_scrolls_taken_over_by_user_input(*stable_node_id, scroll_offset_before_scroll);
- auto scroll_handled = set_scroll_offset(node, offset);
+ auto scroll_handled = ScrollHandled::No;
+ if (navigable && stable_node_id.has_value()) {
+ if (navigable->set_scroll_offset_for_user_input(node, *stable_node_id, offset))
+ scroll_handled = ScrollHandled::Yes;
+ } else {
+ scroll_handled = set_scroll_offset(node, offset);
+ }
if (scroll_handled == ScrollHandled::Yes && scroll_kind == ScrollKind::Relative)
node.document().scroll_state_query_containers().did_scroll_relatively(node, scroll_offset(node) - scroll_offset_before_scroll);
if (!navigable)
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 82ad9cd2d67..f697b61fcc2 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -345,6 +345,7 @@ ContextState::ContextUpdateResult ContextState::handle_key_event(Web::KeyEvent c
if (animation.stable_node_id != stable_node_id || !animation.is_user_scroll || animation.animation.kind() != Compositing::ScrollAnimationKind::SmoothScroll)
continue;
retarget_user_scroll(animation, destination, now);
+ animation.heads_to_an_extent = scrolls_to_extent;
report_started_user_scroll(animation.operation_id, stable_node_id, step_start, destination, Compositing::ScrollAnimationKind::SmoothScroll, scrolls_to_extent);
return {
.accepted = true,
@@ -361,6 +362,7 @@ ContextState::ContextUpdateResult ContextState::handle_key_event(Web::KeyEvent c
.animation = Compositing::SmoothScrollAnimation { *current_offset, destination, m_async_scroll_tree.device_pixels_per_css_pixel() },
.started_at = now,
.is_user_scroll = true,
+ .heads_to_an_extent = scrolls_to_extent,
});
report_started_user_scroll(operation_id, stable_node_id, step_start, destination, Compositing::ScrollAnimationKind::SmoothScroll, scrolls_to_extent);
return {
@@ -669,6 +671,7 @@ Compositing::AsyncScrollOperationID ContextState::start_snap_scroll(Compositing:
.animation = Compositing::SmoothScrollAnimation { *current_offset, m_async_scroll_tree.device_offset_from_css_pixels(destination), m_async_scroll_tree.device_pixels_per_css_pixel(), snap_scroll.animation_kind },
.started_at = now,
.is_user_scroll = true,
+ .heads_to_an_extent = false,
});
m_scroll_snap_controller.did_start_snap_scroll(stable_node_id, operation_id, destination);
note_user_input_scrolled(stable_node_id);
@@ -977,6 +980,28 @@ void ContextState::note_user_input_scrolled(Web::AsyncScrollNodeStableID stable_
m_user_input_scroll_counts.ensure(stable_node_id)++;
}
+Optional<Gfx::Orientation> ContextState::axis_held_by_scrollbar_of(Compositing::AsyncScrollNodeID node_id) const
+{
+ // Paging steps toward the pointer like keys do, and takes a script's scroll along like them.
+ auto scrollbar_index = m_scrollbar_controller.captured_scrollbar_index();
+ if (!scrollbar_index.has_value() || m_scrollbar_paging.has_value())
+ return {};
+ auto const& scrollbar = m_scrollbar_controller.scrollbars()[scrollbar_index.value()];
+ if (scrollbar.scroll_node_id != node_id)
+ return {};
+ return scrollbar.vertical ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal;
+}
+
+void ContextState::stop_user_scroll_carried_on_by_momentum_of(Web::AsyncScrollNodeStableID stable_node_id)
+{
+ if (m_wheel_gesture_routing.latched_scroller() != stable_node_id)
+ return;
+ if (m_latched_gesture_carries_on_by_momentum)
+ m_momentum_of_latched_gesture_is_stopped = true;
+ if (m_scroll_fling_controller.is_active())
+ m_scroll_fling_controller.end_on_next_step();
+}
+
Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Compositing::ScrollRequest> scroll_requests)
{
Vector<Compositing::AsyncScrollOffset> answers;
@@ -1042,11 +1067,40 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
}
}
- if (running_animation != m_smooth_scroll_animations.end() && !retargets_running_animation)
- cancel_smooth_scroll(scroll_request.stable_node_id);
- else if (!retargets_running_animation && smooth_scroll.has_value())
+ bool has_instant_scroll = scroll_request.jump_offset.has_value() || scroll_request.relative_scroll.has_value();
+ bool instant_scroll_is_for_script = has_instant_scroll && !scroll_request.instant_scroll_is_for_user_input;
+ bool smooth_scroll_is_for_script = smooth_scroll.has_value() && smooth_scroll->initiator == Compositing::SmoothScrollInitiator::Programmatic;
+
+ // The thumb of a scrollbar that the user holds stays under the pointer: the user scrolls the box along that axis
+ // from the moment a script does, which aborts a smooth scroll of the script's.
+ auto axis_held_by_scrollbar = axis_held_by_scrollbar_of(node_id.value());
+ if (axis_held_by_scrollbar.has_value() && smooth_scroll_is_for_script) {
+ 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();
+ smooth_scroll_is_for_script = false;
+ }
+
+ // A smooth scroll in progress is aborted by any other, and by an instant scroll, except that a script's instant
+ // scroll carries the steps of the user's scroll along with it. A request that only aborts is for the main
+ // thread's own scrolls.
+ bool shifts_running_animation = false;
+ if (running_animation != m_smooth_scroll_animations.end() && !retargets_running_animation) {
+ bool running_animation_is_requested = running_animation->operation_id >= Compositing::first_operation_id_the_main_thread_requests;
+ bool running_animation_is_user_step = running_animation->is_user_scroll
+ && running_animation->animation.kind() != Compositing::ScrollAnimationKind::Momentum
+ && !m_scroll_snap_controller.is_snap_scroll(scroll_request.stable_node_id, running_animation->operation_id);
+ if (smooth_scroll.has_value())
+ cancel_smooth_scroll(scroll_request.stable_node_id);
+ else if (has_instant_scroll && instant_scroll_is_for_script && running_animation_is_user_step)
+ shifts_running_animation = true;
+ else if (has_instant_scroll || running_animation_is_requested)
+ cancel_smooth_scroll(scroll_request.stable_node_id);
+ } else if (!retargets_running_animation && smooth_scroll.has_value()) {
m_scroll_snap_controller.did_start_main_thread_scroll(scroll_request.stable_node_id);
+ }
+ auto offset_before_instant_scroll = m_async_scroll_tree.scroll_offset_for_node(node_id.value(), m_scroll_state_snapshot).value();
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.
@@ -1055,6 +1109,30 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
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();
+ offset.set_primary_offset_for_orientation(axis_held_by_scrollbar.value(), offset_before_instant_scroll.primary_offset_for_orientation(axis_held_by_scrollbar.value()));
+ (void)m_async_scroll_tree.set_scroll_offset(node_id.value(), offset, m_scene.current_visual_context_tree(), m_scroll_state_snapshot);
+ }
+ auto instant_scroll_delta = m_async_scroll_tree.scroll_offset_for_node(node_id.value(), m_scroll_state_snapshot).value() - offset_before_instant_scroll;
+
+ if (shifts_running_animation) {
+ // A step that goes to an end of the scroll range still goes there.
+ if (!running_animation->heads_to_an_extent) {
+ running_animation->animation.shift_by(instant_scroll_delta);
+ auto destination_offset = running_animation->animation.destination_offset();
+ auto clamped_destination_offset = m_async_scroll_tree.clamped_scroll_offset_for_node(node_id.value(), destination_offset);
+ if (clamped_destination_offset != destination_offset)
+ running_animation->animation.retarget(clamped_destination_offset, MonotonicTime::now() - running_animation->started_at);
+ }
+ }
+
+ if (instant_scroll_is_for_script || smooth_scroll_is_for_script)
+ stop_user_scroll_carried_on_by_momentum_of(scroll_request.stable_node_id);
+ if (instant_scroll_is_for_script)
+ m_elastic_overscroll_controller.drop_stretch_moved_by(scroll_request.stable_node_id, instant_scroll_delta);
+ if (smooth_scroll_is_for_script)
+ m_elastic_overscroll_controller.release_stretch_of(scroll_request.stable_node_id, MonotonicTime::now());
if (smooth_scroll.has_value()) {
auto destination_offset = smooth_scroll->destination_offset;
@@ -1079,6 +1157,7 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
.animation = move(animation),
.started_at = MonotonicTime::now(),
.is_user_scroll = smooth_scroll->initiator == Compositing::SmoothScrollInitiator::UserInput,
+ .heads_to_an_extent = false,
});
}
}
@@ -1174,6 +1253,7 @@ ContextState::AsyncScrollResult ContextState::smooth_wheel_scroll_by(Optional<Co
.animation = Compositing::SmoothScrollAnimation { scroll_target->current_offset, scroll_target->destination_offset, m_async_scroll_tree.device_pixels_per_css_pixel(), Compositing::ScrollAnimationKind::Wheel },
.started_at = now,
.is_user_scroll = true,
+ .heads_to_an_extent = false,
});
report_started_user_scroll(operation_id, scroll_target->stable_node_id, scroll_target->start_offset, scroll_target->destination_offset, Compositing::ScrollAnimationKind::Wheel);
m_async_scrolling_viewport_rect = viewport_rect;
@@ -1369,6 +1449,13 @@ ContextState::ContextUpdateResult ContextState::scroll_for_wheel_event(Web::Mous
if (latch.nested_context_id.has_value())
return { .nested_context_wheel_step = NestedContextWheelStep { .context_id = *latch.nested_context_id } };
+ // The fingers start a gesture of their own, while momentum that a script's scroll stopped scrolls nothing more.
+ m_latched_gesture_carries_on_by_momentum = event.scroll_gesture_phase == Web::ScrollGesturePhase::Momentum;
+ if (event.scroll_gesture_phase != Web::ScrollGesturePhase::Momentum)
+ m_momentum_of_latched_gesture_is_stopped = false;
+ else if (m_momentum_of_latched_gesture_is_stopped)
+ return { .accepted = true };
+
// The first step of a gesture over the content of a nested context goes to that context. A gesture whose first step
// went elsewhere is not handed to a nested context that comes under the pointer later.
auto nested_context_regions = Compositing::NestedContextRegions::LeaveToMainThread;
@@ -2072,6 +2159,7 @@ Optional<PendingFrame> ContextState::start_scrollbar_press_scroll(Compositing::A
.animation = Compositing::SmoothScrollAnimation { old_scroll_offset, destination, m_async_scroll_tree.device_pixels_per_css_pixel() },
.started_at = MonotonicTime::now(),
.is_user_scroll = true,
+ .heads_to_an_extent = false,
});
report_started_user_scroll(operation_id, node.stable_node_id, old_scroll_offset, destination, Compositing::ScrollAnimationKind::SmoothScroll, is_absolute_scroll);
m_scrollbar_press_scroll_operation = operation_id;
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index fb975d2a194..12caf8affa2 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -259,6 +259,8 @@ private:
Compositing::SmoothScrollAnimation animation;
MonotonicTime started_at;
bool is_user_scroll { false };
+ // Whether the scroll goes to an end of the scroll range, as the Home and End keys scroll.
+ bool heads_to_an_extent { false };
};
struct VisualViewportScrollDelta {
@@ -271,6 +273,9 @@ private:
void adopt_keyboard_scroll_state(Compositing::KeyboardScrollState);
Optional<VisualViewportScrollDelta> apply_visual_viewport_scroll_delta(Gfx::FloatPoint);
void note_user_input_scrolled(Web::AsyncScrollNodeStableID);
+ 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 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.
@@ -364,6 +369,9 @@ 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;
+ // Whether the gesture the wheel is latched to carries on by momentum, and whether a script's scroll stopped that.
+ bool m_latched_gesture_carries_on_by_momentum { false };
+ bool m_momentum_of_latched_gesture_is_stopped { false };
// 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.
diff --git a/Services/Compositor/ElasticOverscrollController.cpp b/Services/Compositor/ElasticOverscrollController.cpp
index 5b183072fd8..fc3a41d0244 100644
--- a/Services/Compositor/ElasticOverscrollController.cpp
+++ b/Services/Compositor/ElasticOverscrollController.cpp
@@ -109,6 +109,19 @@ void ElasticOverscrollController::release_held_by_fingers(MonotonicTime now)
active.overscroll.release(velocity, now);
}
+void ElasticOverscrollController::drop_stretch_moved_by(Web::AsyncScrollNodeStableID stable_node_id, Gfx::FloatPoint scroll_delta)
+{
+ auto stretch = stretch_of(stable_node_id);
+ if ((stretch.x() != 0 && scroll_delta.x() != 0) || (stretch.y() != 0 && scroll_delta.y() != 0))
+ m_active_overscrolls.remove_all_matching([&](auto const& active) { return active.stable_node_id == stable_node_id; });
+}
+
+void ElasticOverscrollController::release_stretch_of(Web::AsyncScrollNodeStableID stable_node_id, MonotonicTime now)
+{
+ if (auto* active = active_overscroll_of(stable_node_id); active && !active->overscroll.is_rebounding())
+ active->overscroll.release({}, now);
+}
+
void ElasticOverscrollController::end_gesture(MonotonicTime now)
{
release_held_by_fingers(now);
diff --git a/Services/Compositor/ElasticOverscrollController.h b/Services/Compositor/ElasticOverscrollController.h
index fd7720ac4d9..82ceb92828a 100644
--- a/Services/Compositor/ElasticOverscrollController.h
+++ b/Services/Compositor/ElasticOverscrollController.h
@@ -38,6 +38,10 @@ public:
// Returns whether the scroller is at an edge that the step points past, which is what lets the step pull it.
bool pull_by(Compositing::AsyncScrollNode const&, Gfx::FloatPoint scroll_offset, Gfx::FloatPoint step_delta, Gfx::FloatPoint delta_the_scroller_did_not_scroll_by, float visual_viewport_magnification);
void release_held_by_fingers(MonotonicTime now);
+ // A scroller that something else scrolls along an axis it is stretched in lets go of the stretch at once, while one
+ // that something else scrolls smoothly springs back from it.
+ void drop_stretch_moved_by(Web::AsyncScrollNodeStableID, Gfx::FloatPoint scroll_delta);
+ void release_stretch_of(Web::AsyncScrollNodeStableID, MonotonicTime now);
void end_gesture(MonotonicTime now);
void forget();
void did_install_scrolling_state(Compositing::AsyncScrollTree const&);
diff --git a/Tests/Compositor/TestContextState.cpp b/Tests/Compositor/TestContextState.cpp
index 43bd372d72b..27b45f8e63a 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 = {},
+ .instant_scroll_is_for_user_input = false,
.smooth_scroll = {},
};
(void)context.apply_scroll_requests({ &reset_zoom, 1 });
diff --git a/Tests/LibCompositing/TestScrollRequest.cpp b/Tests/LibCompositing/TestScrollRequest.cpp
index 1120c62ca48..b75f65081a4 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), .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 = {} };
+ 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 = {} };
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), .smooth_scroll = {} };
- ScrollRequest later { .stable_node_id = {}, .request_id = 2, .jump_offset = {}, .visual_viewport_scale = {}, .relative_scroll = scroll_by(-100, 0, 500), .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), .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 = {} };
request.fold_later(later);
EXPECT_EQ(request.jump_offset, Gfx::FloatPoint(0, 40));
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index ba610aeff99..37256e38a12 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1092,6 +1092,7 @@ Text/input/wpt-import/css/css-animations/event-dispatch.tentative.html
; Waits for scrolls that animate in real time, which uncapped frames only spin through
Text/input/async-scrolling/overscroll-behavior-on-a-box-with-nothing-to-scroll.html
Text/input/async-scrolling/overscroll-behavior-stops-wheel-chaining.html
+Text/input/async-scrolling/script-scroll-of-a-stretched-scroller.html
Text/input/scroll-snap-scroll-step-while-pinch-zoomed.html
Text/input/scroll-snap-wheel-step-after-instant-programmatic-scroll.html
Text/input/scroll-snap-wheel-step-during-programmatic-scroll.html
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-carries-a-wheel-step-along.txt b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-carries-a-wheel-step-along.txt
new file mode 100644
index 00000000000..db0da5419fc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-carries-a-wheel-step-along.txt
@@ -0,0 +1,2 @@
+step in flight when the script scrolled: true
+scrollY once the step ends: 1100
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-during-a-thumb-drag.txt b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-during-a-thumb-drag.txt
new file mode 100644
index 00000000000..24de2f7dfa8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-during-a-thumb-drag.txt
@@ -0,0 +1,3 @@
+dragged away from the top: true
+held where the drag left it: true
+still there once released: true
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-of-a-stretched-scroller.txt b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-of-a-stretched-scroller.txt
new file mode 100644
index 00000000000..715e6a9220c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-of-a-stretched-scroller.txt
@@ -0,0 +1,4 @@
+stretched: true
+after an instant scroll: scrollTop=50 stretch=0
+stretched again: true
+after a smooth scroll: scrollTop=50 stretch=0
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-stops-a-fling.txt b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-stops-a-fling.txt
new file mode 100644
index 00000000000..dfe6d62cb0c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/script-scroll-stops-a-fling.txt
@@ -0,0 +1,2 @@
+scrollY after the fling: 500
+scrollY after the next gesture: 550
diff --git a/Tests/LibWeb/Text/input/async-scrolling/script-scroll-carries-a-wheel-step-along.html b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-carries-a-wheel-step-along.html
new file mode 100644
index 00000000000..0e9f2129248
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-carries-a-wheel-step-along.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 5000px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ // A script that scrolls the page while a wheel step animates it takes the step along, which still travels its
+ // whole distance.
+ const stepEnded = scrollendEvent(document);
+ const stepMoved = new Promise(resolve => document.addEventListener("scroll", resolve, { once: true }));
+ await internals.wheelThroughUIProcess(50, 50, 0, 100);
+ await stepMoved;
+ println(`step in flight when the script scrolled: ${window.scrollY > 0 && window.scrollY < 100}`);
+ window.scrollBy(0, 1000);
+ await stepEnded;
+ println(`scrollY once the step ends: ${window.scrollY}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/script-scroll-during-a-thumb-drag.html b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-during-a-thumb-drag.html
new file mode 100644
index 00000000000..e692d3db99f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-during-a-thumb-drag.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ #scroller {
+ width: 150px;
+ height: 100px;
+ overflow-y: scroll;
+ overflow-x: hidden;
+ }
+ #content {
+ height: 1000px;
+ }
+</style>
+<div id="scroller">
+ <div id="content"></div>
+</div>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ await internals.mouseEventThroughUIProcess("mousemove", 147, 5);
+ await internals.mouseEventThroughUIProcess("mousedown", 147, 5);
+ await internals.mouseEventThroughUIProcess("mousemove", 147, 60, { buttons: 1 });
+ await animationFrame();
+ await animationFrame();
+ const draggedTo = scroller.scrollTop;
+ println(`dragged away from the top: ${draggedTo > 0}`);
+
+ // The thumb stays under the pointer that holds it, so the script's scroll does not move the box along its axis.
+ scroller.scrollTop = 0;
+ for (let frame = 0; frame < 4; frame++)
+ await animationFrame();
+ println(`held where the drag left it: ${scroller.scrollTop === draggedTo}`);
+
+ await internals.mouseEventThroughUIProcess("mouseup", 147, 60);
+ await animationFrame();
+ await animationFrame();
+ println(`still there once released: ${scroller.scrollTop === draggedTo}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/script-scroll-of-a-stretched-scroller.html b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-of-a-stretched-scroller.html
new file mode 100644
index 00000000000..597c590c090
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-of-a-stretched-scroller.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 2000px;
+ }
+ #nested {
+ width: 100px;
+ height: 100px;
+ overflow: scroll;
+ }
+ #nested > div {
+ height: 300px;
+ }
+</style>
+<div id="nested"><div></div></div>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+ const stretch = () => internals.compositorScrollState(nested).stretch.y;
+ const stretchReleased = async () => {
+ while (stretch() !== 0)
+ await animationFrame();
+ };
+
+ // A script that scrolls the box along the axis it is stretched in lets go of the stretch at once.
+ await scrollSettled(nested, () => nested.scrollTo(0, 190));
+ await internals.wheel(50, 50, 0, 410, true, "ongoing");
+ println(`stretched: ${stretch() > 0}`);
+ nested.scrollTo(0, 50);
+ await stretchReleased();
+ println(`after an instant scroll: scrollTop=${nested.scrollTop} stretch=${stretch()}`);
+ await internals.wheel(50, 50, 0, 0, true, "ended");
+
+ // A smooth scroll lets the stretch spring back as it goes.
+ await scrollSettled(nested, () => nested.scrollTo(0, 190));
+ await internals.wheel(50, 50, 0, 410, true, "ongoing");
+ println(`stretched again: ${stretch() > 0}`);
+ await nested.scrollTo({ top: 50, behavior: "smooth" });
+ await stretchReleased();
+ println(`after a smooth scroll: scrollTop=${nested.scrollTop} stretch=${stretch()}`);
+ await internals.wheel(50, 50, 0, 0, true, "ended");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/script-scroll-stops-a-fling.html b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-stops-a-fling.html
new file mode 100644
index 00000000000..c616315d1c2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/script-scroll-stops-a-fling.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ height: 5000px;
+ }
+</style>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ await internals.wheelThroughUIProcess(50, 50, 0, 50, true, "ongoing");
+ await internals.wheelThroughUIProcess(50, 50, 0, 40, true, "momentum");
+ await animationFrame();
+ await animationFrame();
+
+ // The script takes the page somewhere of its own, and the rest of the fling goes nowhere.
+ window.scrollTo(0, 500);
+ await animationFrame();
+ await animationFrame();
+ for (let step = 0; step < 3; step++)
+ await internals.wheelThroughUIProcess(50, 50, 0, 40, true, "momentum");
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended");
+ for (let frame = 0; frame < 4; frame++)
+ await animationFrame();
+ println(`scrollY after the fling: ${window.scrollY}`);
+
+ // The next gesture scrolls as usual.
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ await internals.wheelThroughUIProcess(50, 50, 0, 50, true, "ongoing");
+ await internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended");
+ for (let frame = 0; frame < 4; frame++)
+ await animationFrame();
+ println(`scrollY after the next gesture: ${window.scrollY}`);
+ });
+</script>