Skip to content

Truly independent web browser

Everywhere: End a box's scroll once nothing scrolls it any more

Zaggy10246f8618f09d53
diff --git a/Libraries/LibCompositing/Types.cpp b/Libraries/LibCompositing/Types.cpp
index afb65653bce..b6c619f815d 100644
--- a/Libraries/LibCompositing/Types.cpp
+++ b/Libraries/LibCompositing/Types.cpp
@@ -143,6 +143,7 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::AppliedScrollRequest const&
{
TRY(encoder.encode(applied_scroll_request.stable_node_id));
TRY(encoder.encode(applied_scroll_request.request_id));
+ TRY(encoder.encode(applied_scroll_request.moved_the_box));
return {};
}
@@ -152,6 +153,7 @@ ErrorOr<Compositing::AppliedScrollRequest> decode(Decoder& decoder)
return Compositing::AppliedScrollRequest {
.stable_node_id = TRY(decoder.decode<Web::AsyncScrollNodeStableID>()),
.request_id = TRY(decoder.decode<u64>()),
+ .moved_the_box = TRY(decoder.decode<bool>()),
};
}
@@ -165,8 +167,9 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::PendingAsyncScrollUpdates co
TRY(encoder.encode(updates.operation_ids_taken_over_by_user_input));
TRY(encoder.encode(updates.started_user_scrolls));
TRY(encoder.encode(updates.applied_scroll_requests));
- TRY(encoder.encode(updates.user_scroll_gesture_in_progress));
- TRY(encoder.encode(updates.user_scroll_gesture_ended));
+ TRY(encoder.encode(updates.boxes_in_user_scroll_gesture));
+ TRY(encoder.encode(updates.boxes_whose_user_scroll_gesture_ended));
+ TRY(encoder.encode(updates.wheel_latched_scroller));
return {};
}
@@ -181,8 +184,9 @@ ErrorOr<Compositing::PendingAsyncScrollUpdates> decode(Decoder& decoder)
.operation_ids_taken_over_by_user_input = TRY(decoder.decode<Vector<Compositing::AsyncScrollOperationID>>()),
.started_user_scrolls = TRY(decoder.decode<Vector<Compositing::StartedUserScroll>>()),
.applied_scroll_requests = TRY(decoder.decode<Vector<Compositing::AppliedScrollRequest>>()),
- .user_scroll_gesture_in_progress = TRY(decoder.decode<bool>()),
- .user_scroll_gesture_ended = TRY(decoder.decode<bool>()),
+ .boxes_in_user_scroll_gesture = TRY(decoder.decode<Vector<Web::AsyncScrollNodeStableID>>()),
+ .boxes_whose_user_scroll_gesture_ended = TRY(decoder.decode<Vector<Web::AsyncScrollNodeStableID>>()),
+ .wheel_latched_scroller = TRY(decoder.decode<Optional<Web::AsyncScrollNodeStableID>>()),
};
}
diff --git a/Libraries/LibCompositing/Types.h b/Libraries/LibCompositing/Types.h
index ac36bdff99c..cc3ebf69504 100644
--- a/Libraries/LibCompositing/Types.h
+++ b/Libraries/LibCompositing/Types.h
@@ -81,6 +81,7 @@ struct KeyboardScrollState {
struct AppliedScrollRequest {
Web::AsyncScrollNodeStableID stable_node_id;
u64 request_id { 0 };
+ bool moved_the_box { false };
};
struct PendingAsyncScrollUpdates {
@@ -93,8 +94,11 @@ struct PendingAsyncScrollUpdates {
Vector<AsyncScrollOperationID> operation_ids_taken_over_by_user_input;
Vector<StartedUserScroll> started_user_scrolls;
Vector<AppliedScrollRequest> applied_scroll_requests;
- bool user_scroll_gesture_in_progress { false };
- bool user_scroll_gesture_ended { false };
+ // The boxes whose gesture held input of the compositor's own keeps going, and those whose gesture ended since the
+ // previous updates, which may never have been published as going on.
+ Vector<Web::AsyncScrollNodeStableID> boxes_in_user_scroll_gesture;
+ Vector<Web::AsyncScrollNodeStableID> boxes_whose_user_scroll_gesture_ended;
+ Optional<Web::AsyncScrollNodeStableID> wheel_latched_scroller;
};
struct AsyncScrollEnqueueResult {
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
index 38466f4f315..ac72d744fdb 100644
--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp
+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
@@ -594,8 +594,9 @@ Compositing::PendingAsyncScrollUpdates CompositorConnection::take_pending_async_
updates.started_user_scrolls = move(pending->started_user_scrolls);
updates.applied_scroll_requests = move(pending->applied_scroll_requests);
updates.document_id = pending->document_id;
- updates.user_scroll_gesture_in_progress = pending->user_scroll_gesture_in_progress;
- updates.user_scroll_gesture_ended = pending->user_scroll_gesture_ended;
+ updates.boxes_in_user_scroll_gesture = pending->boxes_in_user_scroll_gesture;
+ updates.boxes_whose_user_scroll_gesture_ended = move(pending->boxes_whose_user_scroll_gesture_ended);
+ updates.wheel_latched_scroller = pending->wheel_latched_scroller;
// Whether a gesture is in progress is a state the compositor process keeps current; the rest
// was consumed here.
pending->scroll_offsets.clear();
@@ -603,7 +604,7 @@ Compositing::PendingAsyncScrollUpdates CompositorConnection::take_pending_async_
pending->operation_ids_taken_over_by_user_input.clear();
pending->started_user_scrolls.clear();
pending->applied_scroll_requests.clear();
- pending->user_scroll_gesture_ended = false;
+ pending->boxes_whose_user_scroll_gesture_ended.clear();
return updates;
}
@@ -632,9 +633,13 @@ void CompositorConnection::merge_async_scroll_updates(Web::CompositorContextId c
pending.applied_scroll_requests.extend(move(updates.applied_scroll_requests));
if (is_newest) {
pending.document_id = updates.document_id;
- pending.user_scroll_gesture_in_progress = updates.user_scroll_gesture_in_progress;
+ pending.boxes_in_user_scroll_gesture = move(updates.boxes_in_user_scroll_gesture);
+ pending.wheel_latched_scroller = updates.wheel_latched_scroller;
+ }
+ for (auto box : updates.boxes_whose_user_scroll_gesture_ended) {
+ if (!pending.boxes_whose_user_scroll_gesture_ended.contains_slow(box))
+ pending.boxes_whose_user_scroll_gesture_ended.append(box);
}
- pending.user_scroll_gesture_ended |= updates.user_scroll_gesture_ended;
}
void CompositorConnection::viewport_size_updated(Web::CompositorContextId context_id, Gfx::IntSize viewport_size, double device_pixel_ratio, Compositing::WindowResizingInProgress window_resize_in_progress)
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index c39f0875b70..200567aa529 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -911,6 +911,7 @@ void EventLoop::update_the_rendering()
if (auto navigable = document->navigable()) {
navigable->process_main_thread_smooth_scrolls();
navigable->adopt_pending_async_scroll_offsets();
+ navigable->end_scroll_sequences_that_completed_since_the_last_frame();
}
// AD-HOC: A text control that script edited reveals its cursor now, once for all of the edits. See the note
// at FormAssociatedTextControlElement::CursorRevealAfterEdit.
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index d816a8d511e..5729d3d7e60 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -8,6 +8,7 @@
#include <AK/AllOf.h>
#include <AK/AnyOf.h>
+#include <AK/InsertionSort.h>
#include <AK/NeverDestroyed.h>
#include <AK/TemporaryChange.h>
#include <AK/Utf16String.h>
@@ -764,7 +765,7 @@ void LocalNavigable::set_has_been_destroyed()
m_has_been_destroyed = true;
DOM::Document::invalidate_full_activity();
resolve_all_pending_async_scroll_operations();
- cancel_user_scroll_settlement();
+ cancel_scroll_sequences();
}
Vector<GC::Root<LocalNavigable>> LocalNavigable::hosted_inclusive_descendant_navigables()
@@ -814,7 +815,7 @@ void LocalNavigable::remove_from_all_local_navigables()
cancel_hover_update_after_async_scroll();
destroy_compositor_context();
resolve_all_pending_async_scroll_operations();
- cancel_user_scroll_settlement();
+ cancel_scroll_sequences();
if (m_active_document)
m_active_document->set_navigable(nullptr);
@@ -824,7 +825,7 @@ void LocalNavigable::remove_from_all_local_navigables()
void LocalNavigable::finalize()
{
cancel_hover_update_after_async_scroll();
- cancel_user_scroll_settlement();
+ cancel_scroll_sequences();
destroy_compositor_context();
all_local_navigables().remove(*this);
Base::finalize();
@@ -850,8 +851,8 @@ void LocalNavigable::visit_edges(Cell::Visitor& visitor)
visitor.visit(instant_scroll.promise);
for (auto& smooth_scroll : m_main_thread_smooth_scrolls)
visitor.visit(smooth_scroll.promises);
- for (auto& entry : m_pending_user_scrollend_targets)
- visitor.visit(entry.target);
+ for (auto& sequence : m_scroll_sequences)
+ visitor.visit(sequence.target);
if (m_recording_in_flight)
visitor.visit(m_recording_in_flight->document);
}
@@ -1615,7 +1616,7 @@ void LocalNavigable::set_active_document(GC::Ptr<DOM::Document> document)
// The pending post-scroll hover refresh and scrollend settlement belong to the outgoing document; drop them.
// And so does the hover the page reported to its client; end it.
cancel_hover_update_after_async_scroll();
- cancel_user_scroll_settlement();
+ cancel_scroll_sequences();
m_event_handler.reset_hover_for_document_replacement({});
// NB: A composition belongs to the outgoing document, not the navigable that survives it.
forget_input_method_composition_session();
@@ -4843,31 +4844,16 @@ static void queue_async_scroll_operation_promise_resolution(GC::Ref<WebIDL::Prom
LocalNavigable::FinishedScroll LocalNavigable::finished_scroll(PendingAsyncScrollOperation const& operation)
{
- return {
- .stable_node_id = operation.stable_node_id,
- .initial_scroll_offset = operation.initial_scroll_offset,
- .promises = GC::RootVector<GC::Ref<WebIDL::Promise>> { operation.promises.span() },
- .trigger = operation.trigger,
- };
+ return { .promises = GC::RootVector<GC::Ref<WebIDL::Promise>> { operation.promises.span() } };
}
LocalNavigable::FinishedScroll LocalNavigable::finished_scroll(MainThreadSmoothScroll const& smooth_scroll)
{
- return {
- .stable_node_id = smooth_scroll.stable_node_id,
- .initial_scroll_offset = smooth_scroll.initial_scroll_offset,
- .promises = GC::RootVector<GC::Ref<WebIDL::Promise>> { smooth_scroll.promises.span() },
- .trigger = smooth_scroll.trigger,
- };
+ return { .promises = GC::RootVector<GC::Ref<WebIDL::Promise>> { smooth_scroll.promises.span() } };
}
-void LocalNavigable::queue_scrollend_event_and_promise_resolution_for_finished_scroll(Optional<Web::AsyncScrollNodeStableID> stable_node_id, ScrollTrigger trigger, Optional<CSSPixelPoint> scroll_offset_before_scroll, ReadonlySpan<GC::Ref<WebIDL::Promise>> promises)
+void LocalNavigable::resolve_promises_of_finished_scroll(ReadonlySpan<GC::Ref<WebIDL::Promise>> promises)
{
- if (stable_node_id.has_value() && scroll_offset_before_scroll.has_value()) {
- auto final_scroll_offset = scroll_offset_for(*stable_node_id);
- if (final_scroll_offset.has_value() && *final_scroll_offset != *scroll_offset_before_scroll)
- queue_scrollend_event_for_finished_scroll(*stable_node_id, trigger, scroll_offset_before_scroll);
- }
for (auto const& promise : promises)
queue_async_scroll_operation_promise_resolution(promise);
}
@@ -4893,7 +4879,6 @@ LocalNavigable::PendingAsyncScrollOperation& LocalNavigable::ensure_pending_asyn
.operation_id = operation_id,
.promises = {},
.stable_node_id = {},
- .initial_scroll_offset = {},
.destination_scroll_offset = {},
});
index = m_pending_async_scroll_operations.size() - 1;
@@ -4933,7 +4918,7 @@ void LocalNavigable::wait_for_wheel_scrolls(Vector<GC::Ref<WebIDL::Promise>>& pr
}
}
-void LocalNavigable::resolve_async_scroll_operation(Compositing::AsyncScrollOperationID operation_id, AsyncScrollCompletion completion)
+void LocalNavigable::resolve_async_scroll_operation(Compositing::AsyncScrollOperationID operation_id)
{
// Notifying a scroll's completion can start the next scroll of the same scrolling box, so the finished scroll
// leaves the list of scrolls in progress before it is reported.
@@ -4947,15 +4932,8 @@ void LocalNavigable::resolve_async_scroll_operation(Compositing::AsyncScrollOper
if (!finished.has_value())
return;
- // A scroll that user input took over belongs to the gesture that input continues, which reports the end of the
- // combined scrolling operation; reporting no offset to have scrolled from resolves such a scroll's promise
- // without queueing an event of its own.
- auto scroll_offset_the_finished_scroll_reports = completion == AsyncScrollCompletion::Finished
- ? finished->initial_scroll_offset
- : Optional<CSSPixelPoint> {};
-
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(finished->stable_node_id, finished->trigger, scroll_offset_the_finished_scroll_reports, finished->promises);
- settle_user_scroll_gesture_if_input_deadline_passed();
+ resolve_promises_of_finished_scroll(finished->promises);
+ note_scroll_sequences_may_have_completed();
}
// https://drafts.csswg.org/cssom-view-1/#perform-a-scroll
@@ -4964,18 +4942,15 @@ void LocalNavigable::resolve_async_scroll_operation(Compositing::AsyncScrollOper
// 2. Resolve scrollPromise.
void LocalNavigable::finish_instant_scrolls_answered_by(Compositing::AppliedScrollRequest const& applied_scroll_request)
{
- auto document = active_document();
+ // NB: The box's scroll sequence fires the scrollend event, if the compositor says the scroll moved the box, once
+ // nothing else keeps it going. The main thread's own write of the box, if it changed anything, said when.
+ if (applied_scroll_request.moved_the_box) {
+ if (auto* sequence = scroll_sequence_for(applied_scroll_request.stable_node_id))
+ sequence->moved = true;
+ }
m_instant_scrolls_awaiting_answers.remove_all_matching([&](auto const& instant_scroll) {
if (instant_scroll.stable_node_id != applied_scroll_request.stable_node_id || instant_scroll.request_id > applied_scroll_request.request_id)
return false;
- auto final_scroll_offset = scroll_offset_for(instant_scroll.stable_node_id);
- auto target = document ? scroll_event_target_for_async_scroll_node(*document, instant_scroll.stable_node_id) : nullptr;
- // NB: A user's gesture that holds the box has not completed the scroll, and delivers the event once it does.
- if (target && final_scroll_offset.has_value() && final_scroll_offset.value() != instant_scroll.initial_scroll_offset
- && !latched_user_scroll_gesture_for(*target, instant_scroll.stable_node_id)) {
- document->append_pending_scroll_event({ *target, EventNames::scrollend });
- page().client().request_frame();
- }
queue_async_scroll_operation_promise_resolution(instant_scroll.promise);
return true;
});
@@ -4987,17 +4962,13 @@ void LocalNavigable::resolve_all_pending_async_scroll_operations()
queue_async_scroll_operation_promise_resolution(instant_scroll.promise);
m_instant_scrolls_awaiting_answers.clear();
- while (!m_pending_async_scroll_operations.is_empty()) {
- auto pending = finished_scroll(m_pending_async_scroll_operations.take_last());
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(pending.stable_node_id, pending.trigger, pending.initial_scroll_offset, pending.promises);
- }
+ while (!m_pending_async_scroll_operations.is_empty())
+ resolve_promises_of_finished_scroll(finished_scroll(m_pending_async_scroll_operations.take_last()).promises);
- while (!m_main_thread_smooth_scrolls.is_empty()) {
- auto smooth_scroll = finished_scroll(m_main_thread_smooth_scrolls.take_last());
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(smooth_scroll.stable_node_id, smooth_scroll.trigger, smooth_scroll.initial_scroll_offset, smooth_scroll.promises);
- }
+ while (!m_main_thread_smooth_scrolls.is_empty())
+ resolve_promises_of_finished_scroll(finished_scroll(m_main_thread_smooth_scrolls.take_last()).promises);
- settle_user_scroll_gesture_if_input_deadline_passed();
+ note_scroll_sequences_may_have_completed();
}
Optional<CSSPixelPoint> LocalNavigable::scroll_offset_for(Web::AsyncScrollNodeStableID stable_node_id) const
@@ -5187,97 +5158,107 @@ static GC::Ptr<DOM::EventTarget> scroll_event_target_for_async_scroll_node(DOM::
return element_for_async_scroll_node_stable_id(document, stable_node_id);
}
-LocalNavigable::PendingUserScrollendTarget* LocalNavigable::latched_user_scroll_gesture_for(GC::Ref<DOM::EventTarget> target, Optional<Web::AsyncScrollNodeStableID> const& stable_node_id)
+LocalNavigable::ScrollSequence* LocalNavigable::scroll_sequence_for(Web::AsyncScrollNodeStableID stable_node_id)
{
- auto index = m_pending_user_scrollend_targets.find_first_index_if([&](auto const& entry) {
- if (stable_node_id.has_value() && entry.stable_node_id.has_value())
- return *entry.stable_node_id == *stable_node_id;
- return entry.target.ptr() == target.ptr();
- });
+ auto index = m_scroll_sequences.find_first_index_if([&](auto const& sequence) { return sequence.stable_node_id == stable_node_id; });
if (!index.has_value())
return nullptr;
- return &m_pending_user_scrollend_targets[*index];
+ return &m_scroll_sequences[index.value()];
}
-void LocalNavigable::queue_scrollend_event(Web::AsyncScrollNodeStableID stable_node_id, ScrollTrigger trigger, Optional<CSSPixelPoint> scroll_offset_before_scroll)
+LocalNavigable::ScrollSequence* LocalNavigable::begin_scroll_sequence(Web::AsyncScrollNodeStableID stable_node_id)
{
+ if (auto* sequence = scroll_sequence_for(stable_node_id))
+ return sequence;
auto document = active_document();
if (!document)
- return;
-
+ return nullptr;
auto target = scroll_event_target_for_async_scroll_node(*document, stable_node_id);
if (!target)
- return;
-
- queue_scrollend_event(*document, *target, stable_node_id, trigger, scroll_offset_before_scroll);
+ return nullptr;
+ m_scroll_sequences.append({ .target = *target, .stable_node_id = stable_node_id, .moved = false, .user_gesture = {} });
+ return &m_scroll_sequences.last();
}
-void LocalNavigable::queue_scrollend_event(DOM::Document& document, GC::Ref<DOM::EventTarget> target, Optional<Web::AsyncScrollNodeStableID> stable_node_id, ScrollTrigger trigger, Optional<CSSPixelPoint> scroll_offset_before_scroll)
+void LocalNavigable::note_scroll_sequence_moved(Web::AsyncScrollNodeStableID stable_node_id)
{
- if (trigger == ScrollTrigger::UserInput)
- queue_scrollend_event_after_user_scroll(target, stable_node_id, scroll_offset_before_scroll);
- else
- document.append_pending_scroll_event({ target, EventNames::scrollend });
+ if (auto* sequence = scroll_sequence_for(stable_node_id)) {
+ sequence->moved = true;
+ sequence->last_moved_ordinal = ++m_last_scroll_move_ordinal;
+ }
}
-void LocalNavigable::queue_scrollend_event_for_finished_scroll(Web::AsyncScrollNodeStableID stable_node_id, ScrollTrigger trigger, Optional<CSSPixelPoint> scroll_offset_before_scroll)
+LocalNavigable::UserScrollGesture* LocalNavigable::user_scroll_gesture_for(Web::AsyncScrollNodeStableID stable_node_id)
{
- auto document = active_document();
- if (!document)
- return;
- auto target = scroll_event_target_for_async_scroll_node(*document, stable_node_id);
- if (!target)
- return;
+ auto* sequence = scroll_sequence_for(stable_node_id);
+ if (!sequence || !sequence->user_gesture.has_value())
+ return nullptr;
+ return &sequence->user_gesture.value();
+}
- if (trigger != ScrollTrigger::UserInput) {
- document->append_pending_scroll_event({ *target, EventNames::scrollend });
+void LocalNavigable::note_user_scroll_gesture_of(Web::AsyncScrollNodeStableID stable_node_id, Optional<CSSPixelPoint> scroll_offset_at_gesture_start, UserScrollMovedBox moved_box, SnapPositionSelection snap_position_selection)
+{
+ auto* sequence = begin_scroll_sequence(stable_node_id);
+ if (!sequence)
return;
- }
+ if (moved_box == UserScrollMovedBox::Yes)
+ sequence->moved = true;
- if (latched_user_scroll_gesture_for(*target, stable_node_id))
- return;
+ if (!sequence->user_gesture.has_value())
+ sequence->user_gesture = UserScrollGesture {};
+ auto& gesture = sequence->user_gesture.value();
+ if (!gesture.scroll_offset_at_gesture_start.has_value())
+ gesture.scroll_offset_at_gesture_start = scroll_offset_at_gesture_start;
+ gesture.intent = m_user_scroll_input_intent;
+ gesture.travels_under_momentum = m_user_scroll_gesture_travels_under_momentum;
+ gesture.snap_position_selection = snap_position_selection;
+ gesture.snaps_at_its_end = true;
+ gesture.awaits_layout_for_snapping = false;
+ gesture.waited_for_layout_for_snapping = false;
+ gesture.input_deadline = MonotonicTime::now() + Compositing::user_scroll_settle_delay;
- if (m_user_scroll_gesture_hold_count > 0) {
- queue_scrollend_event_after_user_scroll(*target, stable_node_id, scroll_offset_before_scroll);
- return;
- }
+ if (m_user_scroll_gesture_hold_of_current_input)
+ m_user_scroll_gesture_hold_of_current_input->hold(stable_node_id);
+ schedule_end_of_user_scroll_gesture_input();
+}
- document->append_pending_scroll_event({ *target, EventNames::scrollend });
+void LocalNavigable::note_user_scroll_input_intent(Compositing::SnapSelectionStrategy::Type intent)
+{
+ m_user_scroll_input_intent = intent;
}
-void LocalNavigable::queue_scrollend_event_after_user_scroll(GC::Ref<DOM::EventTarget> target, Optional<Web::AsyncScrollNodeStableID> stable_node_id, Optional<CSSPixelPoint> scroll_offset_before_scroll, SnapPositionSelection snap_position_selection)
+// User input activity postpones the end of a gesture that is going on, but never begins one, so scrolling boxes that do
+// not move still receive no scrollend event.
+void LocalNavigable::extend_user_scroll_gesture_of(Web::AsyncScrollNodeStableID stable_node_id)
{
- if (auto* existing_entry = latched_user_scroll_gesture_for(target, stable_node_id)) {
- if (!existing_entry->scroll_offset_at_gesture_start.has_value())
- existing_entry->scroll_offset_at_gesture_start = scroll_offset_before_scroll;
- existing_entry->intent = m_user_scroll_input_intent;
- existing_entry->travels_under_momentum = m_user_scroll_gesture_travels_under_momentum;
- existing_entry->snap_position_selection = snap_position_selection;
- existing_entry->awaits_layout_for_snapping = false;
- } else {
- m_pending_user_scrollend_targets.append({ target, stable_node_id, scroll_offset_before_scroll, {}, m_user_scroll_input_intent, m_user_scroll_gesture_travels_under_momentum, snap_position_selection });
- }
+ auto* gesture = user_scroll_gesture_for(stable_node_id);
+ if (!gesture)
+ return;
+ gesture->input_deadline = MonotonicTime::now() + Compositing::user_scroll_settle_delay;
+ schedule_end_of_user_scroll_gesture_input();
+}
- if (!m_user_scroll_settle_timer) {
- m_user_scroll_settle_timer = Core::Timer::create_single_shot(Compositing::user_scroll_settle_delay, [this] {
- user_scroll_did_settle();
- });
+void LocalNavigable::extend_wheel_user_scroll_gesture(Optional<Web::AsyncScrollNodeStableID> box_latched_on_this_thread)
+{
+ if (box_latched_on_this_thread.has_value())
+ extend_user_scroll_gesture_of(box_latched_on_this_thread.value());
+ if (m_compositor_wheel_latched_scroller.has_value())
+ extend_user_scroll_gesture_of(m_compositor_wheel_latched_scroller.value());
+ if (m_wheel_user_scroll_gesture_hold) {
+ for (auto box : m_wheel_user_scroll_gesture_hold->held_boxes())
+ extend_user_scroll_gesture_of(box);
}
- m_user_scroll_settle_timer->restart();
}
-void LocalNavigable::note_user_scroll_input_intent(Compositing::SnapSelectionStrategy::Type intent)
+void LocalNavigable::note_wheel_user_scroll_gesture_scrolls(Web::AsyncScrollNodeStableID stable_node_id)
{
- m_user_scroll_input_intent = intent;
+ if (m_wheel_user_scroll_gesture_hold)
+ m_wheel_user_scroll_gesture_hold->hold(stable_node_id);
}
-void LocalNavigable::defer_user_scroll_settlement()
+TemporaryChange<UserScrollGestureHold*> LocalNavigable::hold_boxes_user_input_scrolls_with(UserScrollGestureHold& hold)
{
- // User input activity postpones settlement of already latched targets, but never latches new ones, so scrolling
- // boxes that do not move still receive no scrollend event.
- if (m_pending_user_scrollend_targets.is_empty())
- return;
- m_user_scroll_settle_timer->restart();
+ return TemporaryChange<UserScrollGestureHold*> { m_user_scroll_gesture_hold_of_current_input, &hold };
}
void LocalNavigable::note_user_scroll_gesture_phase(Web::ScrollGesturePhase phase)
@@ -5311,7 +5292,7 @@ void LocalNavigable::note_user_scroll_gesture_phase(Web::ScrollGesturePhase phas
release_wheel_user_scroll_gesture_hold();
break;
}
- settle_user_scroll_gesture();
+ note_scroll_sequences_may_have_completed();
break;
}
}
@@ -5320,6 +5301,9 @@ void LocalNavigable::did_handle_wheel_event()
{
if (!m_wheel_user_scroll_gesture_hold)
return;
+ // The compositor latched the gesture to the box it scrolls before it handed the event on.
+ if (m_compositor_wheel_latched_scroller.has_value())
+ m_wheel_user_scroll_gesture_hold->hold(m_compositor_wheel_latched_scroller.value());
if (!m_wheel_user_scroll_gesture_hold_expiry_timer) {
m_wheel_user_scroll_gesture_hold_expiry_timer = Core::Timer::create_single_shot(Compositing::user_scroll_settle_delay, [this] {
m_wheel_user_scroll_gesture_hold = nullptr;
@@ -5341,23 +5325,18 @@ void LocalNavigable::reset_momentum_fling_state()
m_momentum_fling_estimator.reset();
}
-void LocalNavigable::settle_user_scroll_gesture()
-{
- if (m_pending_user_scrollend_targets.is_empty())
- return;
-
- if (m_user_scroll_gesture_hold_count > 0)
- return;
-
- m_user_scroll_settle_timer->stop();
- user_scroll_did_settle();
-}
-
void LocalNavigable::snap_user_scroll_gestures_that_awaited_layout()
{
- if (!any_of(m_pending_user_scrollend_targets, [](auto const& entry) { return entry.awaits_layout_for_snapping; }))
- return;
- user_scroll_did_settle(UserScrollSettlement::SnappingDeferredUntilLayout);
+ bool any_gesture_awaited_layout = false;
+ for (auto& sequence : m_scroll_sequences) {
+ if (!sequence.user_gesture.has_value() || !sequence.user_gesture->awaits_layout_for_snapping)
+ continue;
+ sequence.user_gesture->awaits_layout_for_snapping = false;
+ sequence.user_gesture->waited_for_layout_for_snapping = true;
+ any_gesture_awaited_layout = true;
+ }
+ if (any_gesture_awaited_layout)
+ end_completed_scroll_sequences();
}
// https://drafts.csswg.org/css-scroll-snap-1/#re-snap
@@ -5387,9 +5366,6 @@ void LocalNavigable::re_snap_scroll_containers_after_layout_change(Layout::Begun
if (!viewport_layout_node || !Painting::has_committed_box(*viewport_layout_node))
return;
- if (m_user_scroll_gesture_hold_count > 0)
- return;
-
document->cancel_scheduled_scroll_container_resnap();
TemporaryChange re_snapping_in_progress { m_is_re_snapping_scroll_containers, true };
@@ -5406,9 +5382,9 @@ void LocalNavigable::re_snap_scroll_containers_after_layout_change(Layout::Begun
// A scrolling box that is being scrolled re-snaps once that scroll settles from wherever it comes to rest,
// rather than having the position it is moving toward re-evaluated out from under it.
- auto target = scroll_event_target_for_async_scroll_node(*document, *stable_node_id);
- bool has_latched_gesture = target && latched_user_scroll_gesture_for(*target, stable_node_id);
- if (has_latched_gesture || in_flight_scroll_for(*stable_node_id).has_value()) {
+ bool is_being_scrolled = user_scroll_gesture_for(*stable_node_id) || in_flight_scroll_for(*stable_node_id).has_value()
+ || m_user_scroll_gesture_hold_counts.contains(*stable_node_id);
+ if (is_being_scrolled) {
any_snap_container_deferred = true;
continue;
}
@@ -5464,28 +5440,37 @@ void LocalNavigable::re_snap_scroll_containers_after_layout_change(Layout::Begun
document->schedule_scroll_container_resnap();
}
-void LocalNavigable::cancel_user_scroll_settlement()
+void LocalNavigable::cancel_scroll_sequences()
{
- if (m_user_scroll_settle_timer)
- m_user_scroll_settle_timer->stop();
- m_pending_user_scrollend_targets.clear();
+ if (m_user_scroll_gesture_input_timer)
+ m_user_scroll_gesture_input_timer->stop();
+ m_scroll_sequences.clear();
m_compositor_user_scroll_gesture_hold = nullptr;
release_wheel_user_scroll_gesture_hold();
}
-void LocalNavigable::begin_user_scroll_gesture_hold(Badge<UserScrollGestureHold>)
+void LocalNavigable::begin_user_scroll_gesture_hold(Badge<UserScrollGestureHold>, Web::AsyncScrollNodeStableID stable_node_id)
{
- ++m_user_scroll_gesture_hold_count;
+ m_user_scroll_gesture_hold_counts.ensure(stable_node_id, [] { return 0; })++;
}
-void LocalNavigable::end_user_scroll_gesture_hold(Badge<UserScrollGestureHold>)
+void LocalNavigable::end_user_scroll_gesture_hold(Badge<UserScrollGestureHold>, Web::AsyncScrollNodeStableID stable_node_id)
{
- VERIFY(m_user_scroll_gesture_hold_count > 0);
- if (--m_user_scroll_gesture_hold_count > 0)
+ auto count = m_user_scroll_gesture_hold_counts.find(stable_node_id);
+ VERIFY(count != m_user_scroll_gesture_hold_counts.end());
+ if (--count->value > 0)
return;
+ m_user_scroll_gesture_hold_counts.remove(count);
+
+ // The release of the last input holding the box completes its gesture.
+ note_user_scroll_gesture_input_ended(stable_node_id);
+}
- // The release of the last held input completes the scroll gesture.
- settle_user_scroll_gesture();
+void LocalNavigable::note_user_scroll_gesture_input_ended(Web::AsyncScrollNodeStableID stable_node_id)
+{
+ if (auto* gesture = user_scroll_gesture_for(stable_node_id))
+ gesture->input_deadline.clear();
+ note_scroll_sequences_may_have_completed();
}
Optional<LocalNavigable::InFlightScroll> LocalNavigable::in_flight_scroll_for(Optional<Web::AsyncScrollNodeStableID> const& stable_node_id) const
@@ -5512,191 +5497,214 @@ Optional<LocalNavigable::InFlightScroll> LocalNavigable::in_flight_scroll_for(Op
void LocalNavigable::abandon_snapping_of_user_scroll_gesture(Web::AsyncScrollNodeStableID stable_node_id)
{
- auto document = active_document();
- if (!document)
- return;
- auto target = scroll_event_target_for_async_scroll_node(*document, stable_node_id);
- if (!target)
+ auto* gesture = user_scroll_gesture_for(stable_node_id);
+ if (!gesture)
return;
- auto* entry = latched_user_scroll_gesture_for(*target, stable_node_id);
- if (!entry)
- return;
+ gesture->scroll_offset_at_gesture_start = {};
+ gesture->unsnapped_scroll_destination = {};
+ gesture->intent = Compositing::SnapSelectionStrategy::Type::EndPosition;
+ gesture->snap_position_selection = SnapPositionSelection::AtGestureEnd;
+ gesture->snaps_at_its_end = false;
+}
- // The entry remains only to deliver the scrollend event the gesture owes.
- entry->scroll_offset_at_gesture_start = {};
- entry->unsnapped_scroll_destination = {};
- entry->intent = Compositing::SnapSelectionStrategy::Type::EndPosition;
- entry->snap_position_selection = SnapPositionSelection::AtGestureEnd;
+bool LocalNavigable::has_scroll_in_progress(ScrollSequence const& sequence, MonotonicTime now) const
+{
+ auto const& stable_node_id = sequence.stable_node_id;
+ if (m_user_scroll_gesture_hold_counts.contains(stable_node_id))
+ return true;
+ if (any_of(m_pending_async_scroll_operations, [&](auto const& pending) { return pending.stable_node_id == stable_node_id; }))
+ return true;
+ if (any_of(m_main_thread_smooth_scrolls, [&](auto const& smooth_scroll) { return smooth_scroll.stable_node_id == stable_node_id; }))
+ return true;
+ if (any_of(m_instant_scrolls_awaiting_answers, [&](auto const& instant_scroll) { return instant_scroll.stable_node_id == stable_node_id; }))
+ return true;
+ if (!sequence.user_gesture.has_value())
+ return false;
+ auto const& gesture = sequence.user_gesture.value();
+ return gesture.awaits_layout_for_snapping || (gesture.input_deadline.has_value() && now < gesture.input_deadline.value());
}
-void LocalNavigable::settle_user_scroll_gesture_if_input_deadline_passed()
+void LocalNavigable::schedule_end_of_user_scroll_gesture_input()
{
- if (m_pending_user_scrollend_targets.is_empty())
- return;
- if (m_user_scroll_settle_timer && m_user_scroll_settle_timer->is_active())
+ Optional<MonotonicTime> earliest_deadline;
+ for (auto const& sequence : m_scroll_sequences) {
+ if (!sequence.user_gesture.has_value() || !sequence.user_gesture->input_deadline.has_value())
+ continue;
+ auto deadline = sequence.user_gesture->input_deadline.value();
+ if (!earliest_deadline.has_value() || deadline < earliest_deadline.value())
+ earliest_deadline = deadline;
+ }
+ if (!earliest_deadline.has_value()) {
+ if (m_user_scroll_gesture_input_timer)
+ m_user_scroll_gesture_input_timer->stop();
return;
+ }
+ if (!m_user_scroll_gesture_input_timer) {
+ m_user_scroll_gesture_input_timer = Core::Timer::create_single_shot(AK::Duration::zero(), [this] {
+ note_scroll_sequences_may_have_completed();
+ });
+ }
+ m_user_scroll_gesture_input_timer->restart(max(earliest_deadline.value() - MonotonicTime::now(), AK::Duration::zero()));
+}
- // Starting a scroll of a scrolling box aborts the scrolls already running for it, and settling in the middle of
- // that would enqueue a snap scroll of the same box alongside the one being started. The scroll that is starting
- // settles the gesture once it is under way instead.
- if (m_scrolls_being_started > 0) {
- m_user_scroll_settlement_awaits_scroll_start = true;
- return;
+// Returns whether the sequence goes on, with the scroll to the snap position its gesture ends at or until layout is up
+// to date to select one.
+bool LocalNavigable::snap_at_end_of_user_scroll_gesture(ScrollSequence& sequence, Layout::BegunRead const& read, bool can_snap)
+{
+ // 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).
+ // NB: A gesture whose snap position cannot be selected yet snaps once layout is up to date. One that already waited
+ // for layout once takes what it can get, so that a scrolling box cannot stay in its gesture.
+ if (!can_snap && !sequence.user_gesture->waited_for_layout_for_snapping) {
+ sequence.user_gesture->awaits_layout_for_snapping = true;
+ page().client().request_frame();
+ return true;
+ }
+
+ auto gesture = sequence.user_gesture.release_value();
+ if (!can_snap)
+ return false;
+
+ auto document = active_document();
+ auto stable_node_id = sequence.stable_node_id;
+ auto const* snap_container = layout_node_for_async_scroll_node(read, *document, stable_node_id);
+ auto current_scroll_offset = scroll_offset_for(stable_node_id);
+ if (!snap_container || !current_scroll_offset.has_value())
+ return false;
+
+ Compositing::SnapSelectionStrategy strategy;
+ if (gesture.scroll_offset_at_gesture_start.has_value() && gesture.snap_position_selection == SnapPositionSelection::AtGestureEnd) {
+ strategy.displacement = current_scroll_offset.value() - gesture.scroll_offset_at_gesture_start.value();
+ if (!strategy.displacement.is_zero()) {
+ strategy.type = gesture.intent;
+ if (gesture.intent != Compositing::SnapSelectionStrategy::Type::EndPosition || gesture.travels_under_momentum)
+ strategy.start_offset = gesture.scroll_offset_at_gesture_start.value();
+ }
}
+ auto snap_destination = Painting::adjust_scroll_destination_for_snapping(*snap_container, current_scroll_offset.value(), strategy);
+ record_snapped_areas_of_scroll_container(*document, stable_node_id, snap_destination);
+ if (snap_destination.position == current_scroll_offset.value())
+ return false;
- user_scroll_did_settle();
+ TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*document) };
+ perform_a_scroll_of_a_scrolling_box(stable_node_id, {
+ .position = snap_destination.position,
+ .behavior = Bindings::ScrollBehavior::Smooth,
+ .trigger = ScrollTrigger::GestureEndSnap,
+ });
+ return true;
}
-void LocalNavigable::user_scroll_did_settle(UserScrollSettlement settlement)
+// Sequences end as late as they can, just before the scroll steps of the next frame fire their events, so that a
+// scroll that starts before then joins the sequence of its box rather than following a scrollend event.
+void LocalNavigable::note_scroll_sequences_may_have_completed()
{
- if (has_been_destroyed())
+ m_scroll_sequences_may_have_completed = true;
+ page().client().request_frame();
+}
+
+void LocalNavigable::end_scroll_sequences_that_completed_since_the_last_frame()
+{
+ if (!exchange(m_scroll_sequences_may_have_completed, false))
return;
+ end_completed_scroll_sequences();
+}
- // A held input keeps the scroll gesture in progress; its release completes the settlement instead.
- if (m_user_scroll_gesture_hold_count > 0)
+// https://drafts.csswg.org/cssom-view-1/#scroll-completed
+void LocalNavigable::end_completed_scroll_sequences()
+{
+ if (has_been_destroyed())
return;
- GC::ConservativeVector targets { move(m_pending_user_scrollend_targets) };
auto document = active_document();
if (!document)
return;
Layout::ForcedReadScope read { *document };
- // Settlement can occur inside a layout update when tearing down a scrollbar whose thumb is grabbed, so snapping
- // geometry is only consulted when layout is already up to date.
+ // Snapping geometry is only consulted when layout is already up to date.
bool can_snap = document->layout_is_up_to_date() && !document->is_running_update_layout();
- bool queued_any_scrollend_event = false;
- for (auto& entry : targets) {
- // A settlement performed once layout is up to date is only for the gestures an earlier one left waiting for
- // it; the rest are still waiting for their own input to run out.
- if (settlement == UserScrollSettlement::SnappingDeferredUntilLayout && !entry.awaits_layout_for_snapping) {
- m_pending_user_scrollend_targets.append(move(entry));
- continue;
- }
+ // The boxes end in the order they last moved. The ordinals compare as serial numbers, which survive a wrap.
+ AK::insertion_sort(m_scroll_sequences, [](ScrollSequence const& a, ScrollSequence const& b) {
+ return a.last_moved_ordinal != b.last_moved_ordinal && b.last_moved_ordinal - a.last_moved_ordinal < (1ull << 63);
+ });
- auto const& target = entry.target;
- if (auto* element = as_if<DOM::Element>(*target)) {
- if (&element->document() != document.ptr() || !element->is_connected())
- continue;
- } else if (target.ptr() != document.ptr() && target.ptr() != document->visual_viewport().ptr()) {
+ bool queued_any_scrollend_event = false;
+ auto now = MonotonicTime::now();
+ for (size_t index = 0; index < m_scroll_sequences.size();) {
+ auto& sequence = m_scroll_sequences[index];
+ auto const& target = sequence.target;
+ bool target_is_in_document = false;
+ if (auto* element = as_if<DOM::Element>(*target))
+ target_is_in_document = &element->document() == document.ptr() && element->is_connected();
+ else
+ target_is_in_document = target.ptr() == document.ptr() || target.ptr() == document->visual_viewport().ptr();
+ if (!target_is_in_document) {
+ m_scroll_sequences.remove(index);
continue;
}
- auto const& stable_node_id = entry.stable_node_id;
- auto in_flight_scroll_trigger = in_flight_scroll_for(stable_node_id).map([](auto const& in_flight_scroll) { return in_flight_scroll.trigger; });
+ // Input that went quiet no longer holds the gesture, and must not wake the deadline timer again.
+ if (sequence.user_gesture.has_value() && sequence.user_gesture->input_deadline.has_value() && now >= sequence.user_gesture->input_deadline.value())
+ sequence.user_gesture->input_deadline.clear();
- // A user scroll that is still running has not reached the position the gesture ends at. The completion of that
- // scroll settles the gesture instead.
- if (in_flight_scroll_trigger == ScrollTrigger::UserInput) {
- m_pending_user_scrollend_targets.append(move(entry));
+ if (has_scroll_in_progress(sequence, now)) {
+ index++;
continue;
}
- // 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).
- // NB: A programmatic scroll of the scrolling box decides where it comes to rest, and snapped its own
- // destination when it started, so the gesture only delivers the scrollend event it owes.
- bool snaps_at_this_settlement = in_flight_scroll_trigger != ScrollTrigger::Programmatic && stable_node_id.has_value();
-
- // A gesture whose snap position cannot be selected yet keeps the scrolling box latched and snaps once layout
- // is up to date. A settlement that already waited for layout once takes what it can get, so that a scrolling
- // box cannot stay latched.
- if (snaps_at_this_settlement && !can_snap && !entry.awaits_layout_for_snapping) {
- entry.awaits_layout_for_snapping = true;
- m_pending_user_scrollend_targets.append(move(entry));
- page().client().request_frame();
+ // NB: The snap scroll joins this sequence rather than beginning another, so the index stays valid.
+ if (sequence.user_gesture.has_value() && sequence.user_gesture->snaps_at_its_end && snap_at_end_of_user_scroll_gesture(sequence, read, can_snap)) {
+ index++;
continue;
}
- if (snaps_at_this_settlement && can_snap) {
- auto const* snap_container = layout_node_for_async_scroll_node(read, *document, *stable_node_id);
- auto current_scroll_offset = scroll_offset_for(*stable_node_id);
- if (snap_container && current_scroll_offset.has_value()) {
- Compositing::SnapSelectionStrategy strategy;
- if (entry.scroll_offset_at_gesture_start.has_value() && entry.snap_position_selection == SnapPositionSelection::AtGestureEnd) {
- strategy.displacement = *current_scroll_offset - *entry.scroll_offset_at_gesture_start;
- if (!strategy.displacement.is_zero()) {
- strategy.type = entry.intent;
- if (entry.intent != Compositing::SnapSelectionStrategy::Type::EndPosition || entry.travels_under_momentum)
- strategy.start_offset = *entry.scroll_offset_at_gesture_start;
- }
- }
- auto snap_destination = Painting::adjust_scroll_destination_for_snapping(*snap_container, *current_scroll_offset, strategy);
- record_snapped_areas_of_scroll_container(*document, *stable_node_id, snap_destination);
- 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, {
- .position = snap_destination.position,
- .behavior = Bindings::ScrollBehavior::Smooth,
- .trigger = ScrollTrigger::GestureEndSnap,
- });
- continue;
- }
- }
- }
-
- if (document->append_pending_scroll_event({ target, EventNames::scrollend }))
+ auto ended_sequence = m_scroll_sequences.take(index);
+ if (ended_sequence.moved && document->append_pending_scroll_event({ ended_sequence.target, EventNames::scrollend }))
queued_any_scrollend_event = true;
}
if (queued_any_scrollend_event)
page().client().request_frame();
+ schedule_end_of_user_scroll_gesture_input();
}
-void LocalNavigable::resolve_pending_smooth_scrolls(Web::AsyncScrollNodeStableID stable_node_id, SmoothScrollAbortCause abort_cause)
+void LocalNavigable::resolve_pending_smooth_scrolls(Web::AsyncScrollNodeStableID stable_node_id)
{
// NB: A script's instant scroll that another scroll of the box replaces before the compositor answered ends with
// where this navigable has the box.
- auto document = active_document();
m_instant_scrolls_awaiting_answers.remove_all_matching([&](auto const& instant_scroll) {
if (instant_scroll.stable_node_id != stable_node_id)
return false;
- auto target = document ? scroll_event_target_for_async_scroll_node(*document, stable_node_id) : nullptr;
- if (target && scroll_offset_for(stable_node_id) != instant_scroll.initial_scroll_offset)
- document->append_pending_scroll_event({ *target, EventNames::scrollend });
queue_async_scroll_operation_promise_resolution(instant_scroll.promise);
return true;
});
- Vector<FinishedScroll> finished_async_scroll_operations;
+ Vector<FinishedScroll> finished_scrolls;
m_pending_async_scroll_operations.remove_all_matching([&](auto const& pending) {
if (pending.stable_node_id != stable_node_id)
return false;
- finished_async_scroll_operations.append(finished_scroll(pending));
+ finished_scrolls.append(finished_scroll(pending));
return true;
});
-
- Vector<FinishedScroll> finished_smooth_scrolls;
m_main_thread_smooth_scrolls.remove_all_matching([&](auto const& smooth_scroll) {
if (smooth_scroll.stable_node_id != stable_node_id)
return false;
- finished_smooth_scrolls.append(finished_scroll(smooth_scroll));
+ finished_scrolls.append(finished_scroll(smooth_scroll));
return true;
});
+ for (auto const& finished : finished_scrolls)
+ resolve_promises_of_finished_scroll(finished.promises);
- // A smooth scroll aborted by a new scroll of the same scrolling box hands the reporting of the scrolling
- // operation's end to its replacement; reporting no offset to have scrolled from resolves such a scroll's promise
- // without queueing an event of its own.
- auto scroll_offset_a_finished_scroll_reports = [&](Optional<CSSPixelPoint> initial_scroll_offset) {
- if (abort_cause == SmoothScrollAbortCause::ReplacedByNewScroll)
- return Optional<CSSPixelPoint> {};
- return initial_scroll_offset;
- };
- for (auto const& finished : finished_async_scroll_operations)
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(stable_node_id, finished.trigger, scroll_offset_a_finished_scroll_reports(finished.initial_scroll_offset), finished.promises);
- for (auto const& finished : finished_smooth_scrolls)
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(stable_node_id, finished.trigger, scroll_offset_a_finished_scroll_reports(finished.initial_scroll_offset), finished.promises);
-
- settle_user_scroll_gesture_if_input_deadline_passed();
+ note_scroll_sequences_may_have_completed();
}
void LocalNavigable::process_main_thread_smooth_scrolls()
{
auto now = MonotonicTime::now();
+ auto smooth_scroll_count = m_main_thread_smooth_scrolls.size();
Vector<FinishedScroll> finished_smooth_scrolls;
for (size_t index = 0; index < m_main_thread_smooth_scrolls.size();) {
@@ -5720,11 +5728,11 @@ void LocalNavigable::process_main_thread_smooth_scrolls()
}
for (auto const& finished : finished_smooth_scrolls)
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(finished.stable_node_id, finished.trigger, finished.initial_scroll_offset, finished.promises);
+ resolve_promises_of_finished_scroll(finished.promises);
- // A scroll whose scrolling box went away is dropped above without being reported, so settlement is retried for
- // every pass rather than only for the scrolls that ran to their destination.
- settle_user_scroll_gesture_if_input_deadline_passed();
+ // A scroll ends when it runs to its destination or when its scrolling box goes away.
+ if (m_main_thread_smooth_scrolls.size() != smooth_scroll_count)
+ note_scroll_sequences_may_have_completed();
if (!m_main_thread_smooth_scrolls.is_empty())
page().client().request_frame();
@@ -5736,13 +5744,12 @@ bool LocalNavigable::adopt_async_viewport_scroll_offset(CSSPixelPoint scroll_off
if (!document)
return false;
- defer_user_scroll_settlement();
-
+ Web::AsyncScrollNodeStableID viewport_stable_node_id { .node_id = document->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport };
auto scroll_offset_before_scroll = m_viewport_scroll_offset;
perform_scroll_of_viewport_scrolling_box(scroll_offset);
if (m_viewport_scroll_offset == scroll_offset_before_scroll)
return false;
- queue_scrollend_event({ .node_id = document->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport }, ScrollTrigger::UserInput, scroll_offset_before_scroll);
+ note_user_scroll_gesture_of(viewport_stable_node_id, scroll_offset_before_scroll, UserScrollMovedBox::Yes);
return true;
}
@@ -5752,8 +5759,7 @@ bool LocalNavigable::adopt_async_visual_viewport_scroll(Optional<float> scale, C
if (!document)
return false;
- defer_user_scroll_settlement();
-
+ Web::AsyncScrollNodeStableID visual_viewport_stable_node_id { .node_id = document->unique_id(), .kind = Web::AsyncScrollNodeKind::VisualViewport };
auto visual_viewport = document->visual_viewport();
auto scale_before_adoption = visual_viewport->scale();
if (scale.has_value())
@@ -5764,7 +5770,7 @@ bool LocalNavigable::adopt_async_visual_viewport_scroll(Optional<float> scale, C
return visual_viewport->scale() != scale_before_adoption;
visual_viewport->scroll_by(visual_delta);
- queue_scrollend_event(*document, *visual_viewport, {}, ScrollTrigger::UserInput);
+ note_user_scroll_gesture_of(visual_viewport_stable_node_id, {}, UserScrollMovedBox::Yes);
return true;
}
@@ -5781,17 +5787,31 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
// published before the active document's scroll tree was installed still describe the previous one.
auto document = active_document();
bool gesture_belongs_to_active_document = document && async_scroll_updates.document_id == document->unique_id();
- bool user_scroll_gesture_in_progress = gesture_belongs_to_active_document && async_scroll_updates.user_scroll_gesture_in_progress;
- bool user_scroll_gesture_ended = gesture_belongs_to_active_document && async_scroll_updates.user_scroll_gesture_ended;
+ if (gesture_belongs_to_active_document)
+ m_compositor_wheel_latched_scroller = async_scroll_updates.wheel_latched_scroller;
- // A gesture that both began and ended since the previous update is held for the length of this one, so that it
- // settles here rather than once its input deadline passes.
- if ((user_scroll_gesture_in_progress || user_scroll_gesture_ended) && !m_compositor_user_scroll_gesture_hold)
- m_compositor_user_scroll_gesture_hold = make<UserScrollGestureHold>(*this);
+ // The hold mirrors the boxes whose gesture the compositor's own input keeps going. A box is let go of, and a box
+ // whose gesture ended is told so, once the scrolls of this update are adopted, so that its gesture ends here rather
+ // than once its input deadline passes.
+ if (gesture_belongs_to_active_document) {
+ if (!m_compositor_user_scroll_gesture_hold)
+ m_compositor_user_scroll_gesture_hold = make<UserScrollGestureHold>(*this);
+ for (auto box : async_scroll_updates.boxes_in_user_scroll_gesture)
+ m_compositor_user_scroll_gesture_hold->hold(box);
+ }
- ScopeGuard release_gesture_hold_once_its_scrolls_are_adopted = [&] {
- if (gesture_belongs_to_active_document && !user_scroll_gesture_in_progress)
- m_compositor_user_scroll_gesture_hold = nullptr;
+ ScopeGuard end_gestures_once_their_scrolls_are_adopted = [&] {
+ if (!gesture_belongs_to_active_document)
+ return;
+ Vector<Web::AsyncScrollNodeStableID> boxes_to_release;
+ for (auto box : m_compositor_user_scroll_gesture_hold->held_boxes()) {
+ if (!async_scroll_updates.boxes_in_user_scroll_gesture.contains_slow(box))
+ boxes_to_release.append(box);
+ }
+ for (auto box : boxes_to_release)
+ m_compositor_user_scroll_gesture_hold->release(box);
+ for (auto box : async_scroll_updates.boxes_whose_user_scroll_gesture_ended)
+ note_user_scroll_gesture_input_ended(box);
};
if (async_scroll_updates.scroll_offsets.is_empty() && async_scroll_updates.completed_operation_ids.is_empty() && async_scroll_updates.started_user_scrolls.is_empty() && async_scroll_updates.applied_scroll_requests.is_empty())
@@ -5832,6 +5852,9 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
bool adopted_any_scroll_offset = false;
for (auto const& async_scroll_offset : async_scroll_updates.scroll_offsets) {
auto& user_input_scroll_count = m_compositor_user_input_scroll_counts.ensure(async_scroll_offset.stable_node_id);
+ // User input the compositor scrolled the box for keeps the box's gesture going, even where the box stayed put.
+ if (async_scroll_offset.user_input_scroll_count > user_input_scroll_count)
+ extend_user_scroll_gesture_of(async_scroll_offset.stable_node_id);
user_input_scroll_count = max(user_input_scroll_count, async_scroll_offset.user_input_scroll_count);
// What the compositor reported of a box before it applied the last scroll this navigable asked of it describes
@@ -5857,12 +5880,9 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
async_scroll_offset.compositor_scroll_offset.x(), async_scroll_offset.compositor_scroll_offset.y());
}
- // The gesture of the input that took the scroll over is latched here, so that the scrollend event the
- // taken-over scroll owes is delivered once that gesture settles rather than in the middle of it.
- if (user_input_took_over_the_scroll_of(async_scroll_offset.stable_node_id)) {
- if (auto target = scroll_event_target_for_async_scroll_node(*document, async_scroll_offset.stable_node_id))
- queue_scrollend_event_after_user_scroll(*target, async_scroll_offset.stable_node_id, scroll_offset_before_scroll);
- }
+ // The input that took the scroll over goes on with the box's sequence as a gesture of its own.
+ if (user_input_took_over_the_scroll_of(async_scroll_offset.stable_node_id))
+ note_user_scroll_gesture_of(async_scroll_offset.stable_node_id, scroll_offset_before_scroll, UserScrollMovedBox::No);
continue;
}
@@ -5894,7 +5914,7 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
auto scroll_offset_before_scroll = scroll_offset_for(async_scroll_offset.stable_node_id);
if (auto element = adopt_async_element_scroll_offset(*document, async_scroll_offset.stable_node_id, css_scroll_offset)) {
adopted_any_scroll_offset = true;
- queue_scrollend_event_after_user_scroll(*element, async_scroll_offset.stable_node_id, scroll_offset_before_scroll);
+ note_user_scroll_gesture_of(async_scroll_offset.stable_node_id, scroll_offset_before_scroll, UserScrollMovedBox::Yes);
dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Main thread adopting async element offset {},{}",
async_scroll_offset.compositor_scroll_offset.x(), async_scroll_offset.compositor_scroll_offset.y());
}
@@ -5906,12 +5926,9 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
for (auto const& applied_scroll_request : async_scroll_updates.applied_scroll_requests)
finish_instant_scrolls_answered_by(applied_scroll_request);
- for (auto operation_id : async_scroll_updates.completed_operation_ids) {
- auto completion = async_scroll_updates.operation_ids_taken_over_by_user_input.contains_slow(operation_id)
- ? AsyncScrollCompletion::TakenOverByUserInput
- : AsyncScrollCompletion::Finished;
- resolve_async_scroll_operation(operation_id, completion);
- }
+ for (auto operation_id : async_scroll_updates.completed_operation_ids)
+ resolve_async_scroll_operation(operation_id);
+ note_scroll_sequences_may_have_completed();
}
void LocalNavigable::adopt_started_user_scroll(DOM::Document& document, Compositing::StartedUserScroll const& started_user_scroll)
@@ -5923,10 +5940,9 @@ void LocalNavigable::adopt_started_user_scroll(DOM::Document& document, Composit
bool replaced_by_programmatic_scroll = in_flight_scroll.has_value() && in_flight_scroll->trigger == ScrollTrigger::Programmatic;
// The scroll is in flight under the operation a caller may already be waiting for.
+ (void)begin_scroll_sequence(stable_node_id);
auto& pending_operation = ensure_pending_async_scroll_operation(started_user_scroll.operation_id);
pending_operation.stable_node_id = stable_node_id;
- if (!pending_operation.initial_scroll_offset.has_value())
- pending_operation.initial_scroll_offset = started_user_scroll.initial_scroll_offset;
pending_operation.destination_scroll_offset = started_user_scroll.selection.position;
pending_operation.trigger = ScrollTrigger::UserInput;
if (started_user_scroll.animation_kind.has_value())
@@ -5940,24 +5956,20 @@ void LocalNavigable::adopt_started_user_scroll(DOM::Document& document, Composit
if (!started_user_scroll.settles_gesture && !started_user_scroll.is_absolute_scroll)
record_relative_scroll(document, stable_node_id, started_user_scroll.relative_scroll_delta);
- auto target = scroll_event_target_for_async_scroll_node(document, stable_node_id);
- if (!target)
- return;
-
auto snap_destination = started_user_scroll.selection;
record_snapped_areas_of_scroll_container(document, stable_node_id, snap_destination);
- // A snap scroll that settles the gesture ends it: the scroll's completion delivers the scrollend event.
+ // A snap scroll that settles the gesture ends it, and the box's sequence ends with the snap scroll.
if (started_user_scroll.settles_gesture) {
- m_pending_user_scrollend_targets.remove_all_matching([&](auto const& entry) { return entry.target == target && entry.stable_node_id == stable_node_id; });
+ if (auto* sequence = scroll_sequence_for(stable_node_id))
+ sequence->user_gesture.clear();
return;
}
- // The gesture the step belongs to owes the scrollend event, and its next step travels on from the offset its
- // steps have asked for.
- queue_scrollend_event_after_user_scroll(*target, stable_node_id, started_user_scroll.initial_scroll_offset, SnapPositionSelection::PerScroll);
- if (auto* entry = latched_user_scroll_gesture_for(*target, stable_node_id))
- entry->unsnapped_scroll_destination = started_user_scroll.unsnapped_scroll_destination;
+ // The gesture's next step travels on from the offset its steps have asked for.
+ note_user_scroll_gesture_of(stable_node_id, started_user_scroll.initial_scroll_offset, UserScrollMovedBox::No, SnapPositionSelection::PerScroll);
+ if (auto* gesture = user_scroll_gesture_for(stable_node_id))
+ gesture->unsnapped_scroll_destination = started_user_scroll.unsnapped_scroll_destination;
}
void LocalNavigable::schedule_hover_update_after_async_scroll()
@@ -7124,11 +7136,11 @@ void LocalNavigable::render_screenshot(NonnullRefPtr<Gfx::Bitmap> target, PaintC
compositor_context().request_screenshot(move(target), move(callback));
}
-void LocalNavigable::abort_in_flight_smooth_scrolls(Web::AsyncScrollNodeStableID stable_node_id, SmoothScrollAbortCause abort_cause)
+void LocalNavigable::abort_in_flight_smooth_scrolls(Web::AsyncScrollNodeStableID stable_node_id)
{
if (has_compositor_context())
request_abort_of_smooth_scroll(stable_node_id);
- resolve_pending_smooth_scrolls(stable_node_id, abort_cause);
+ resolve_pending_smooth_scrolls(stable_node_id);
}
Compositing::ScrollRequest& LocalNavigable::unsent_scroll_request_for(Web::AsyncScrollNodeStableID stable_node_id)
@@ -7166,7 +7178,8 @@ void LocalNavigable::drop_unsent_smooth_scroll(Compositing::ScrollRequest& scrol
return true;
});
if (finished.has_value())
- queue_scrollend_event_and_promise_resolution_for_finished_scroll(finished->stable_node_id, finished->trigger, {}, finished->promises);
+ resolve_promises_of_finished_scroll(finished->promises);
+ note_scroll_sequences_may_have_completed();
}
void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_node_id, ScrollTrigger trigger)
@@ -7268,6 +7281,7 @@ bool LocalNavigable::set_scroll_offset_for_user_input(Layout::Node& scrolling_bo
void LocalNavigable::did_change_scroll_offset(Web::AsyncScrollNodeStableID stable_node_id)
{
+ note_scroll_sequence_moved(stable_node_id);
if (m_scroll_offset_changes_need_no_request || !has_compositor_context())
return;
request_scroll_jump(stable_node_id, ScrollTrigger::Programmatic);
@@ -7298,19 +7312,13 @@ void LocalNavigable::give_back_unsent_scroll_requests(Vector<Compositing::Scroll
void LocalNavigable::abort_in_flight_smooth_scrolls_taken_over_by_user_input(Web::AsyncScrollNodeStableID stable_node_id, CSSPixelPoint scroll_offset_at_gesture_start)
{
- auto document = active_document();
- auto target = document ? scroll_event_target_for_async_scroll_node(*document, stable_node_id) : nullptr;
-
- // A user scroll that is still running belongs to the gesture this input continues, so that gesture is latched
- // before the scroll is taken over from it and the scrollend event the scroll owes is delivered once the gesture
- // settles.
+ // A user scroll that is still running belongs to the gesture this input continues, which goes on with the box's
+ // sequence once the scroll is taken over from it.
auto in_flight_scroll = in_flight_scroll_for(stable_node_id);
- if (target && in_flight_scroll.has_value() && in_flight_scroll->trigger == ScrollTrigger::UserInput) {
- if (!latched_user_scroll_gesture_for(*target, stable_node_id))
- queue_scrollend_event_after_user_scroll(*target, stable_node_id, scroll_offset_at_gesture_start);
- }
+ if (in_flight_scroll.has_value() && in_flight_scroll->trigger == ScrollTrigger::UserInput)
+ note_user_scroll_gesture_of(stable_node_id, scroll_offset_at_gesture_start, UserScrollMovedBox::No);
- abort_in_flight_smooth_scrolls(stable_node_id, SmoothScrollAbortCause::TakenOverByUserInput);
+ abort_in_flight_smooth_scrolls(stable_node_id);
}
static bool scrolls_smoothly(Bindings::ScrollBehavior behavior, GC::Ptr<DOM::Element> associated_element)
@@ -7341,22 +7349,17 @@ LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_
if (settles_user_scroll_gesture)
trigger = ScrollTrigger::UserInput;
- // A gesture latched for this scrolling box may run out of input while this scroll is being started, so its
- // settlement waits until this scroll is under way rather than enqueuing a scroll of its own alongside it.
- ++m_scrolls_being_started;
- ScopeGuard settle_gesture_that_ran_out_of_input = [this] {
- if (--m_scrolls_being_started > 0)
- return;
- if (!m_user_scroll_settlement_awaits_scroll_start)
- return;
- m_user_scroll_settlement_awaits_scroll_start = false;
- settle_user_scroll_gesture_if_input_deadline_passed();
- };
-
auto initial_scroll_offset = scroll_offset_for(stable_node_id);
if (!initial_scroll_offset.has_value())
return { .promise = WebIDL::create_resolved_promise_for(*document, JS::js_undefined()) };
+ // The scroll joins the box's sequence, which ends without a scrollend event if no scroll in it moves the box.
+ (void)begin_scroll_sequence(stable_node_id);
+ // Nothing may be left running once the scroll is started, as for an instant scroll that no compositor answers.
+ ScopeGuard check_sequence_once_scroll_is_started = [this] { note_scroll_sequences_may_have_completed(); };
+ if (trigger == ScrollTrigger::UserInput && !settles_user_scroll_gesture && !user_scroll_gesture_for(stable_node_id))
+ note_user_scroll_gesture_of(stable_node_id, initial_scroll_offset, UserScrollMovedBox::No);
+
// 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;
@@ -7412,21 +7415,14 @@ LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_
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()), 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))
- queue_scrollend_event_after_user_scroll(*target, stable_node_id, *initial_scroll_offset);
- }
- }
+ // AD-HOC: A wheel step's gesture goes on once no further steps arrive.
+ if (should_scroll_smoothly && trigger == ScrollTrigger::UserInput && animation_kind == Compositing::ScrollAnimationKind::Wheel && position == *initial_scroll_offset)
+ return { WebIDL::create_resolved_promise_for(*document, JS::js_undefined()), goes_to_a_snap_position };
// https://drafts.csswg.org/cssom-view-1/#perform-a-scroll
// 1. Abort any ongoing smooth scroll for box.
// 2. Resolve all pending scroll promises for box.
- abort_in_flight_smooth_scrolls(stable_node_id, SmoothScrollAbortCause::ReplacedByNewScroll);
+ abort_in_flight_smooth_scrolls(stable_node_id);
// 3. Let scrollPromise be a new promise and return it while the remaining
// steps run in parallel.
@@ -7436,10 +7432,9 @@ LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_
// requested behavior or the associated element's computed behavior is
// smooth, perform a smooth scroll. Otherwise, perform an instant scroll.
if (!should_scroll_smoothly) {
- 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);
+ (void)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.
@@ -7454,13 +7449,10 @@ LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_
m_instant_scrolls_awaiting_answers.append({
.stable_node_id = stable_node_id,
.request_id = last_scroll_request_id_for(stable_node_id),
- .initial_scroll_offset = *initial_scroll_offset,
.promise = scroll_promise,
});
return { scroll_promise, goes_to_a_snap_position };
}
- if (did_scroll)
- queue_scrollend_event(stable_node_id, trigger, initial_scroll_offset);
WebIDL::resolve_promise(scroll_promise);
return { scroll_promise, goes_to_a_snap_position };
}
@@ -7484,7 +7476,6 @@ LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_
.operation_id = operation_id,
.promises = { scroll_promise },
.stable_node_id = stable_node_id,
- .initial_scroll_offset = *initial_scroll_offset,
.destination_scroll_offset = position,
.trigger = trigger,
.animation_kind = animation_kind,
@@ -7501,7 +7492,6 @@ LocalNavigable::PerformedScroll LocalNavigable::perform_a_scroll_of_a_scrolling_
.animation = Compositing::SmoothScrollAnimation { initial_scroll_offset->to_type<float>(), position.to_type<float>(), 1.0, animation_kind },
.last_tick = MonotonicTime::now(),
.elapsed = AK::Duration::zero(),
- .initial_scroll_offset = *initial_scroll_offset,
.destination_scroll_offset = position,
.promises = { scroll_promise },
.trigger = trigger,
@@ -7631,10 +7621,6 @@ bool LocalNavigable::perform_a_snapped_relative_user_scroll(Layout::Node& scroll
if (!current_scroll_offset.has_value())
return false;
- auto target = scroll_event_target_for_async_scroll_node(*document, *stable_node_id);
- if (!target)
- return false;
-
// A scroll started for any reason other than user input is going somewhere the gesture never asked for, so a
// gesture's steps then travel from the scrolling box itself instead.
auto in_flight_scroll = in_flight_scroll_for(*stable_node_id);
@@ -7645,7 +7631,7 @@ bool LocalNavigable::perform_a_snapped_relative_user_scroll(Layout::Node& scroll
// A step selects its snap position from the offset the gesture's input deltas have reached rather than from the
// snap position it is scrolling to, so a burst of steps advances by the distance they asked for instead of by one
// snap position each.
- auto* latched_gesture = latched_user_scroll_gesture_for(*target, *stable_node_id);
+ auto* latched_gesture = user_scroll_gesture_for(*stable_node_id);
bool travels_from_input_deltas = latched_gesture
&& (step_accumulation == SnapStepAccumulation::UntilGestureSettles || in_flight_destination.has_value());
auto step_start = travels_from_input_deltas
@@ -7675,11 +7661,11 @@ bool LocalNavigable::perform_a_snapped_relative_user_scroll(Layout::Node& scroll
// consumed without disturbing where it is going.
bool step_rests_at_its_snap_position = snap_destination.position == in_flight_destination.value_or(*current_scroll_offset);
if (!step_rests_at_its_snap_position)
- queue_scrollend_event_after_user_scroll(*target, *stable_node_id, *current_scroll_offset, SnapPositionSelection::PerScroll);
+ note_user_scroll_gesture_of(*stable_node_id, *current_scroll_offset, UserScrollMovedBox::No, SnapPositionSelection::PerScroll);
- // NB: Latching the gesture above may have moved the entry the offset is recorded on, so it is looked up again.
- if (auto* entry = latched_user_scroll_gesture_for(*target, *stable_node_id))
- entry->unsnapped_scroll_destination = unsnapped_destination;
+ // NB: Noting the gesture above may have moved the sequence the offset is recorded on, so it is looked up again.
+ if (auto* gesture = user_scroll_gesture_for(*stable_node_id))
+ gesture->unsnapped_scroll_destination = unsnapped_destination;
if (step_rests_at_its_snap_position)
return true;
@@ -7743,8 +7729,7 @@ bool LocalNavigable::continued_scroll_step_moves(Layout::Node& scrolling_box, CS
step_start = *destination;
// A key travels from the offset its gesture's input deltas have reached while a snap scroll is in flight,
// so a box scrolling to a snap position at its extent goes on taking keys until their input reaches it.
- auto target = scroll_event_target_for_async_scroll_node(*document, *stable_node_id);
- auto* gesture = target ? latched_user_scroll_gesture_for(*target, stable_node_id) : nullptr;
+ auto* gesture = user_scroll_gesture_for(*stable_node_id);
if (animation_kind == Compositing::ScrollAnimationKind::SmoothScroll && gesture && gesture->unsnapped_scroll_destination.has_value())
step_start = *gesture->unsnapped_scroll_destination;
}
@@ -7776,7 +7761,7 @@ bool LocalNavigable::scroll_scrolling_box_by_delta(Layout::Node& scrolling_box,
auto start_offset = in_flight_user_scroll_destination(*stable_node_id, animation_kind).value_or(Painting::scroll_offset(scrolling_box));
auto position = Painting::clamp_scroll_offset(scrolling_box, start_offset + delta);
if (position == start_offset) {
- defer_user_scroll_settlement();
+ extend_user_scroll_gesture_of(*stable_node_id);
return false;
}
@@ -7825,14 +7810,16 @@ bool LocalNavigable::scroll_viewport_by_delta(CSSPixelPoint delta, Bindings::Scr
// https://drafts.csswg.org/cssom-view/#viewport-perform-a-scroll
GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPixelPoint position, Bindings::ScrollBehavior behavior, ScrollTrigger trigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind scroll_kind, Compositing::ScrollAnimationKind animation_kind)
{
- // AD-HOC: User input keeps the scroll gesture in progress even when this scroll does not move the viewport, such
- // as when a held scroll key repeats at the scroll extent.
- if (trigger == ScrollTrigger::UserInput)
- defer_user_scroll_settlement();
-
// 1. Let doc be the viewport’s associated Document.
auto doc = active_document();
+ // AD-HOC: User input keeps the scroll gesture in progress even when this scroll does not move the viewport, such
+ // as when a held scroll key repeats at the scroll extent.
+ if (trigger == ScrollTrigger::UserInput) {
+ extend_user_scroll_gesture_of({ .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport });
+ extend_user_scroll_gesture_of({ .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::VisualViewport });
+ }
+
// 2. Let vv be the VisualViewport whose associated document is doc.
auto vv = doc->visual_viewport();
@@ -7991,16 +7978,39 @@ bool LocalNavigable::has_inclusive_ancestor_with_visibility_hidden() const
return false;
}
-UserScrollGestureHold::UserScrollGestureHold(LocalNavigable& navigable)
+UserScrollGestureHold::UserScrollGestureHold(LocalNavigable& navigable, Optional<Web::AsyncScrollNodeStableID> held_box)
: m_navigable(navigable)
{
- m_navigable->begin_user_scroll_gesture_hold({});
+ if (held_box.has_value())
+ hold(held_box.value());
}
UserScrollGestureHold::~UserScrollGestureHold()
{
+ // NB: Ending the hold of a box can end the gesture of every box that is not held, this one's included, so each box
+ // is let go of before it is reported.
+ while (!m_held_boxes.is_empty()) {
+ auto box = m_held_boxes.take_last();
+ if (m_navigable)
+ m_navigable->end_user_scroll_gesture_hold({}, box);
+ }
+}
+
+void UserScrollGestureHold::hold(Web::AsyncScrollNodeStableID box)
+{
+ if (m_held_boxes.contains_slow(box))
+ return;
+ m_held_boxes.append(box);
+ if (m_navigable)
+ m_navigable->begin_user_scroll_gesture_hold({}, box);
+}
+
+void UserScrollGestureHold::release(Web::AsyncScrollNodeStableID box)
+{
+ if (!m_held_boxes.remove_first_matching([&](auto const& held_box) { return held_box == box; }))
+ return;
if (m_navigable)
- m_navigable->end_user_scroll_gesture_hold({});
+ m_navigable->end_user_scroll_gesture_hold({}, box);
}
}
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 95d7dc6896d..6709b347587 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -11,6 +11,7 @@
#include <AK/HashMap.h>
#include <AK/HashTable.h>
#include <AK/OwnPtr.h>
+#include <AK/TemporaryChange.h>
#include <AK/Time.h>
#include <AK/Tuple.h>
#include <AK/Utf16String.h>
@@ -400,16 +401,6 @@ public:
UntilGestureSettles,
};
- enum class SmoothScrollAbortCause {
- ReplacedByNewScroll,
- TakenOverByUserInput,
- };
-
- enum class AsyncScrollCompletion {
- Finished,
- TakenOverByUserInput,
- };
-
bool scroll_scrolling_box_by_delta(Layout::Node&, CSSPixelPoint delta, Painting::ScrollKind, Compositing::ScrollAnimationKind = Compositing::ScrollAnimationKind::SmoothScroll);
bool continued_scroll_step_moves(Layout::Node&, CSSPixelPoint delta, Compositing::ScrollAnimationKind);
bool scroll_viewport_by_delta(CSSPixelPoint delta, Bindings::ScrollBehavior, Painting::ScrollKind, Compositing::ScrollAnimationKind = Compositing::ScrollAnimationKind::SmoothScroll);
@@ -420,19 +411,29 @@ public:
bool perform_a_snapped_momentum_scroll(Layout::Node&, CSSPixelPoint momentum_delta);
Layout::Node* layout_node_for_async_scroll_node_stable_id(Web::AsyncScrollNodeStableID);
void re_snap_scroll_containers_after_layout_change(Layout::BegunRead const& read);
- void abort_in_flight_smooth_scrolls(Web::AsyncScrollNodeStableID, SmoothScrollAbortCause);
+ void abort_in_flight_smooth_scrolls(Web::AsyncScrollNodeStableID);
void abort_in_flight_smooth_scrolls_taken_over_by_user_input(Web::AsyncScrollNodeStableID, CSSPixelPoint scroll_offset_at_gesture_start);
- void queue_scrollend_event_after_user_scroll(GC::Ref<DOM::EventTarget>, Optional<Web::AsyncScrollNodeStableID>, Optional<CSSPixelPoint> scroll_offset_before_scroll = {}, SnapPositionSelection = SnapPositionSelection::AtGestureEnd);
+ // Whether the user input a box's gesture is noted for has moved the box already, or only started a scroll of it.
+ enum class UserScrollMovedBox {
+ No,
+ Yes,
+ };
+ void note_user_scroll_gesture_of(Web::AsyncScrollNodeStableID, Optional<CSSPixelPoint> scroll_offset_at_gesture_start, UserScrollMovedBox, SnapPositionSelection = SnapPositionSelection::AtGestureEnd);
void note_user_scroll_input_intent(Compositing::SnapSelectionStrategy::Type);
RefPtr<Painting::Scrollbar> scrollbar_dragged_by_compositor(Layout::BegunRead const& read, Web::ScrollbarDraggedByCompositor const&);
// Returns false if the scroller is not in this navigable's active document.
bool set_overlay_scrollbar_hidden(Web::AsyncScrollNodeStableID, bool vertical, bool hidden);
void note_user_scroll_gesture_phase(Web::ScrollGesturePhase);
void did_handle_wheel_event();
- void defer_user_scroll_settlement();
+ void note_wheel_user_scroll_gesture_scrolls(Web::AsyncScrollNodeStableID);
+ void extend_user_scroll_gesture_of(Web::AsyncScrollNodeStableID);
+ void extend_wheel_user_scroll_gesture(Optional<Web::AsyncScrollNodeStableID> box_latched_on_this_thread);
void snap_user_scroll_gestures_that_awaited_layout();
- void begin_user_scroll_gesture_hold(Badge<UserScrollGestureHold>);
- void end_user_scroll_gesture_hold(Badge<UserScrollGestureHold>);
+ void end_scroll_sequences_that_completed_since_the_last_frame();
+ // The boxes that user input scrolls on this thread while the returned change is alive are held by the hold.
+ [[nodiscard]] TemporaryChange<UserScrollGestureHold*> hold_boxes_user_input_scrolls_with(UserScrollGestureHold&);
+ void begin_user_scroll_gesture_hold(Badge<UserScrollGestureHold>, Web::AsyncScrollNodeStableID);
+ void end_user_scroll_gesture_hold(Badge<UserScrollGestureHold>, Web::AsyncScrollNodeStableID);
void reset_zoom();
protected:
@@ -490,9 +491,9 @@ private:
// scroll can owe more than one promise.
using ScrollPromises = Vector<GC::Ref<WebIDL::Promise>, 1>;
- void resolve_async_scroll_operation(Compositing::AsyncScrollOperationID, AsyncScrollCompletion = AsyncScrollCompletion::Finished);
+ void resolve_async_scroll_operation(Compositing::AsyncScrollOperationID);
void resolve_all_pending_async_scroll_operations();
- void resolve_pending_smooth_scrolls(Web::AsyncScrollNodeStableID, SmoothScrollAbortCause);
+ void resolve_pending_smooth_scrolls(Web::AsyncScrollNodeStableID);
// Whether a programmatic scroll still needs a snap position selected for its destination, or was given a
// destination that snap position selection already produced.
enum class DestinationSnapping {
@@ -529,10 +530,7 @@ private:
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 = {});
- void queue_scrollend_event(DOM::Document&, GC::Ref<DOM::EventTarget>, Optional<Web::AsyncScrollNodeStableID>, ScrollTrigger, Optional<CSSPixelPoint> scroll_offset_before_scroll = {});
- void queue_scrollend_event_for_finished_scroll(Web::AsyncScrollNodeStableID, ScrollTrigger, Optional<CSSPixelPoint> scroll_offset_before_scroll);
- void queue_scrollend_event_and_promise_resolution_for_finished_scroll(Optional<Web::AsyncScrollNodeStableID>, ScrollTrigger, Optional<CSSPixelPoint> scroll_offset_before_scroll, ReadonlySpan<GC::Ref<WebIDL::Promise>>);
+ void resolve_promises_of_finished_scroll(ReadonlySpan<GC::Ref<WebIDL::Promise>>);
ScrollPromises* promises_of_smooth_scroll_in_flight_toward(Web::AsyncScrollNodeStableID, CSSPixelPoint position, ScrollTrigger, Compositing::ScrollAnimationKind);
// The scroll a new input to a scrolling box would interact with; a scroll driven by user input is reported over
// any programmatic scroll also in flight.
@@ -543,30 +541,47 @@ private:
};
Optional<InFlightScroll> in_flight_scroll_for(Optional<Web::AsyncScrollNodeStableID> const&) const;
Optional<CSSPixelPoint> in_flight_user_scroll_destination(Web::AsyncScrollNodeStableID, Compositing::ScrollAnimationKind) const;
- struct PendingUserScrollendTarget {
- GC::Ref<DOM::EventTarget> target;
- Optional<Web::AsyncScrollNodeStableID> stable_node_id;
+ // The part user input plays in a scroll sequence, which the box snaps at the end of.
+ struct UserScrollGesture {
Optional<CSSPixelPoint> scroll_offset_at_gesture_start;
Optional<CSSPixelPoint> unsnapped_scroll_destination;
Compositing::SnapSelectionStrategy::Type intent { Compositing::SnapSelectionStrategy::Type::EndPosition };
bool travels_under_momentum { false };
SnapPositionSelection snap_position_selection { SnapPositionSelection::AtGestureEnd };
+ // A programmatic scroll of the box decides where the box comes to rest instead.
+ bool snaps_at_its_end { true };
bool awaits_layout_for_snapping { false };
+ bool waited_for_layout_for_snapping { false };
+ // The input goes on until then, unless the release of a hold on the box ends it first.
+ Optional<MonotonicTime> input_deadline;
+ };
+ // https://drafts.csswg.org/cssom-view-1/#scroll-completed
+ // Scroll is completed when the scroll position has no more pending updates or translations and the user has
+ // completed their gesture.
+ // NB: The scrolls of a box run from the first of them until none is left in progress, and the box's scrollend
+ // event is fired once at the end, if any of them moved it.
+ struct ScrollSequence {
+ GC::Ref<DOM::EventTarget> target;
+ Web::AsyncScrollNodeStableID stable_node_id;
+ bool moved { false };
+ // Which of the boxes' moves was the last of this one, which the sequences end in the order of.
+ u64 last_moved_ordinal { 0 };
+ Optional<UserScrollGesture> user_gesture;
};
- PendingUserScrollendTarget* latched_user_scroll_gesture_for(GC::Ref<DOM::EventTarget>, Optional<Web::AsyncScrollNodeStableID> const&);
+ ScrollSequence* scroll_sequence_for(Web::AsyncScrollNodeStableID);
+ ScrollSequence* begin_scroll_sequence(Web::AsyncScrollNodeStableID);
+ void note_scroll_sequence_moved(Web::AsyncScrollNodeStableID);
+ UserScrollGesture* user_scroll_gesture_for(Web::AsyncScrollNodeStableID);
+ bool has_scroll_in_progress(ScrollSequence const&, MonotonicTime now) const;
+ void note_scroll_sequences_may_have_completed();
+ void end_completed_scroll_sequences();
+ bool snap_at_end_of_user_scroll_gesture(ScrollSequence&, Layout::BegunRead const&, bool can_snap);
+ void schedule_end_of_user_scroll_gesture_input();
void abandon_snapping_of_user_scroll_gesture(Web::AsyncScrollNodeStableID);
- void settle_user_scroll_gesture();
- void settle_user_scroll_gesture_if_input_deadline_passed();
+ void note_user_scroll_gesture_input_ended(Web::AsyncScrollNodeStableID);
void release_wheel_user_scroll_gesture_hold();
void reset_momentum_fling_state();
- // Which of the latched gestures a settlement is for: every gesture that ran out of input, or only those left
- // waiting for layout by an earlier settlement.
- enum class UserScrollSettlement {
- GestureRanOutOfInput,
- SnappingDeferredUntilLayout,
- };
- void user_scroll_did_settle(UserScrollSettlement = UserScrollSettlement::GestureRanOutOfInput);
- void cancel_user_scroll_settlement();
+ void cancel_scroll_sequences();
void schedule_hover_update_after_async_scroll();
void update_hover_after_async_scroll_stops();
void cancel_hover_update_after_async_scroll();
@@ -655,12 +670,16 @@ private:
u64 m_committed_generation { 0 };
OwnPtr<Compositor::CompositorContextHandle> m_compositor_context;
RefPtr<Core::Timer> m_async_scroll_hover_update_timer;
- Vector<PendingUserScrollendTarget> m_pending_user_scrollend_targets;
- RefPtr<Core::Timer> m_user_scroll_settle_timer;
+ Vector<ScrollSequence> m_scroll_sequences;
+ u64 m_last_scroll_move_ordinal { 0 };
+ RefPtr<Core::Timer> m_user_scroll_gesture_input_timer;
OwnPtr<UserScrollGestureHold> m_compositor_user_scroll_gesture_hold;
OwnPtr<UserScrollGestureHold> m_wheel_user_scroll_gesture_hold;
RefPtr<Core::Timer> m_wheel_user_scroll_gesture_hold_expiry_timer;
- size_t m_user_scroll_gesture_hold_count { 0 };
+ HashMap<Web::AsyncScrollNodeStableID, size_t> m_user_scroll_gesture_hold_counts;
+ UserScrollGestureHold* m_user_scroll_gesture_hold_of_current_input { nullptr };
+ Optional<Web::AsyncScrollNodeStableID> m_compositor_wheel_latched_scroller;
+ bool m_scroll_sequences_may_have_completed { false };
Compositing::SnapSelectionStrategy::Type m_user_scroll_input_intent { Compositing::SnapSelectionStrategy::Type::EndPosition };
bool m_user_scroll_gesture_travels_under_momentum { false };
// Momentum that selects no snap position is scrolled by for the rest of the gesture rather than being asked again
@@ -672,15 +691,12 @@ private:
};
MomentumSnapPositionSelection m_momentum_snap_position_selection { MomentumSnapPositionSelection::NotSelectedYet };
Compositing::MomentumFlingEstimator m_momentum_fling_estimator;
- size_t m_scrolls_being_started { 0 };
- bool m_user_scroll_settlement_awaits_scroll_start { false };
bool m_is_re_snapping_scroll_containers { false };
struct PendingAsyncScrollOperation {
Compositing::AsyncScrollOperationID operation_id { 0 };
ScrollPromises promises;
Optional<Web::AsyncScrollNodeStableID> stable_node_id;
- Optional<CSSPixelPoint> initial_scroll_offset;
Optional<CSSPixelPoint> destination_scroll_offset;
ScrollTrigger trigger { ScrollTrigger::Programmatic };
Compositing::ScrollAnimationKind animation_kind { Compositing::ScrollAnimationKind::SmoothScroll };
@@ -690,7 +706,6 @@ private:
struct InstantScrollAwaitingAnswer {
Web::AsyncScrollNodeStableID stable_node_id;
u64 request_id { 0 };
- CSSPixelPoint initial_scroll_offset;
GC::Ref<WebIDL::Promise> promise;
};
Vector<InstantScrollAwaitingAnswer> m_instant_scrolls_awaiting_answers;
@@ -728,7 +743,6 @@ private:
Compositing::SmoothScrollAnimation animation;
MonotonicTime last_tick;
AK::Duration elapsed;
- CSSPixelPoint initial_scroll_offset;
CSSPixelPoint destination_scroll_offset;
ScrollPromises promises;
ScrollTrigger trigger { ScrollTrigger::Programmatic };
@@ -738,15 +752,14 @@ private:
// A scroll taken out of the lists of scrolls in progress, which visit_edges() traces, to be reported. Reporting a
// scroll allocates, so its promises stay rooted until it is reported.
struct FinishedScroll {
- Optional<Web::AsyncScrollNodeStableID> stable_node_id;
- Optional<CSSPixelPoint> initial_scroll_offset;
GC::RootVector<GC::Ref<WebIDL::Promise>> promises;
- ScrollTrigger trigger { ScrollTrigger::Programmatic };
};
static FinishedScroll finished_scroll(PendingAsyncScrollOperation const&);
static FinishedScroll finished_scroll(MainThreadSmoothScroll const&);
};
+// Held user input, such as a pressed key or a grabbed scrollbar thumb, keeps the gesture of each box it holds going until
+// it is released.
class WEB_API UserScrollGestureHold {
AK_MAKE_NONCOPYABLE(UserScrollGestureHold);
AK_MAKE_NONMOVABLE(UserScrollGestureHold);
@@ -754,11 +767,16 @@ class WEB_API UserScrollGestureHold {
public:
AK_ALLOC_WITH_KMALLOC;
- explicit UserScrollGestureHold(LocalNavigable&);
+ explicit UserScrollGestureHold(LocalNavigable&, Optional<Web::AsyncScrollNodeStableID> held_box = {});
~UserScrollGestureHold();
+ void hold(Web::AsyncScrollNodeStableID);
+ void release(Web::AsyncScrollNodeStableID);
+ ReadonlySpan<Web::AsyncScrollNodeStableID> held_boxes() const { return m_held_boxes; }
+
private:
GC::Weak<LocalNavigable> m_navigable;
+ Vector<Web::AsyncScrollNodeStableID, 1> m_held_boxes;
};
struct PopulateSessionHistoryEntryDocumentOutput final : public JS::Cell {
diff --git a/Libraries/LibWeb/Page/AutoScrollHandler.cpp b/Libraries/LibWeb/Page/AutoScrollHandler.cpp
index f2157f6deb8..b5c7c7ea730 100644
--- a/Libraries/LibWeb/Page/AutoScrollHandler.cpp
+++ b/Libraries/LibWeb/Page/AutoScrollHandler.cpp
@@ -205,6 +205,8 @@ void AutoScrollHandler::perform_tick()
int scroll_y = m_fractional_delta.y().to_int();
m_fractional_delta -= CSSPixelPoint { scroll_x, scroll_y };
+ if (auto stable_node_id = Painting::async_scroll_node_stable_id(*layout_node); stable_node_id.has_value() && m_scroll_gesture_hold)
+ m_scroll_gesture_hold->hold(stable_node_id.value());
m_navigable->note_user_scroll_input_intent(Compositing::SnapSelectionStrategy::Type::EndPosition);
if (Painting::scroll_by(*layout_node, scroll_x, scroll_y) == Painting::ScrollHandled::No)
return;
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 848ccbdb5e4..f17432b07f7 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -1028,8 +1028,14 @@ EventResult EventHandler::handle_mousewheel(CSSPixelPoint visual_viewport_positi
m_navigable->note_user_scroll_gesture_phase(scroll_gesture_phase);
- // Wheel activity marks the scroll gesture as still in progress even when it no longer moves any scrolling box.
- m_navigable->defer_user_scroll_settlement();
+ // Wheel activity marks the scroll gesture as still in progress even when it no longer moves any scrolling box. The
+ // end of the gesture is not such activity.
+ if (scroll_gesture_phase != Web::ScrollGesturePhase::Ended) {
+ Optional<AsyncScrollNodeStableID> box_latched_on_this_thread;
+ if (m_wheel_scroll_latch.has_value())
+ box_latched_on_this_thread = m_wheel_scroll_latch->scrolling_box;
+ m_navigable->extend_wheel_user_scroll_gesture(box_latched_on_this_thread);
+ }
m_navigable->note_user_scroll_input_intent(wheel_delta_precision == Web::WheelDeltaPrecision::Discrete
? Compositing::SnapSelectionStrategy::Type::Direction
: Compositing::SnapSelectionStrategy::Type::EndPosition);
@@ -1120,8 +1126,11 @@ EventResult EventHandler::handle_mousewheel(CSSPixelPoint visual_viewport_positi
};
auto latch_gesture_to_scrolling_box = [&](Layout::Node& scrolling_box) {
+ auto stable_node_id = Painting::async_scroll_node_stable_id(scrolling_box);
if (m_wheel_scroll_latch.has_value())
- m_wheel_scroll_latch->scrolling_box = Painting::async_scroll_node_stable_id(scrolling_box);
+ m_wheel_scroll_latch->scrolling_box = stable_node_id;
+ if (stable_node_id.has_value())
+ m_navigable->note_wheel_user_scroll_gesture_scrolls(stable_node_id.value());
};
// The latched scrolling box takes every step of the gesture and drops what it cannot scroll by, so that neither an
@@ -2214,12 +2223,13 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
auto page_scroll_distance = page_scroll_distance_for_key_input();
- // The held key keeps the scroll gesture in progress until it is released.
+ // The held key keeps the gesture of each box it scrolls in progress until it is released.
auto hold_scroll_gesture_until_key_release = [&](Compositing::SnapSelectionStrategy::Type intent) {
m_held_scroll_key = key;
if (!m_scroll_key_gesture_hold)
m_scroll_key_gesture_hold = make<HTML::UserScrollGestureHold>(*m_navigable);
m_navigable->note_user_scroll_input_intent(intent);
+ return m_navigable->hold_boxes_user_input_scrolls_with(*m_scroll_key_gesture_hold);
};
// https://drafts.csswg.org/css-overscroll-1/#overscroll-behavior-properties
// This property must be applied to all scrolling methods supported by the user agent.
@@ -2274,7 +2284,7 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
return m_navigable->perform_a_snapped_relative_user_scroll(*scrolling_box, delta, Compositing::SnapSelectionStrategy::Type::EndPosition, HTML::LocalNavigable::SnapStepAccumulation::UntilScrollFinishes, Compositing::ScrollAnimationKind::SmoothScroll, Painting::ScrollKind::Absolute);
};
auto scroll_by_for_key_input = [&](CSSPixels delta_x, CSSPixels delta_y, Compositing::SnapSelectionStrategy::Type intent) {
- hold_scroll_gesture_until_key_release(intent);
+ auto key_holds_boxes_it_scrolls = hold_scroll_gesture_until_key_release(intent);
if (perform_scroll_step_for_key_input({ delta_x, delta_y }, intent))
return;
if (!viewport_is_in_scroll_chain_for_key_input({ delta_x, delta_y }))
@@ -2282,7 +2292,7 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
m_navigable->scroll_viewport_by_delta({ delta_x, delta_y }, Bindings::ScrollBehavior::Smooth, Painting::ScrollKind::Relative);
};
auto scroll_to_the_beginning_for_key_input = [&] {
- hold_scroll_gesture_until_key_release(Compositing::SnapSelectionStrategy::Type::EndPosition);
+ auto key_holds_boxes_it_scrolls = hold_scroll_gesture_until_key_release(Compositing::SnapSelectionStrategy::Type::EndPosition);
// https://drafts.csswg.org/css-scroll-snap-1/#scroll-types
// Scrolling to the beginning or end is an absolute scroll.
if (perform_snapped_scroll_to_extent_for_key_input({ 0, -CSSPixels::max() }))
@@ -2294,7 +2304,7 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
m_navigable->scroll_viewport_by_delta({ 0, -CSSPixels::max() }, Bindings::ScrollBehavior::Smooth, Painting::ScrollKind::Absolute);
};
auto scroll_to_the_end_for_key_input = [&] {
- hold_scroll_gesture_until_key_release(Compositing::SnapSelectionStrategy::Type::EndPosition);
+ auto key_holds_boxes_it_scrolls = hold_scroll_gesture_until_key_release(Compositing::SnapSelectionStrategy::Type::EndPosition);
if (perform_snapped_scroll_to_extent_for_key_input({ 0, CSSPixels::max() }))
return;
if (scroll_container_of_scroll_target_by({ 0, CSSPixels::max() }, Painting::ScrollKind::Absolute))
@@ -2466,7 +2476,8 @@ EventResult EventHandler::handle_pinch_event(CSSPixelPoint point, u32 modifiers,
return EventResult::Dropped;
// Pinch activity keeps the scroll gesture in progress even when it no longer moves the visual viewport.
- m_navigable->defer_user_scroll_settlement();
+ AsyncScrollNodeStableID visual_viewport_stable_node_id { .node_id = document->unique_id(), .kind = AsyncScrollNodeKind::VisualViewport };
+ m_navigable->extend_user_scroll_gesture_of(visual_viewport_stable_node_id);
auto scale = 1.0 + scale_delta;
if (scale == 1.0)
@@ -2489,7 +2500,7 @@ EventResult EventHandler::handle_pinch_event(CSSPixelPoint point, u32 modifiers,
auto offset_top_before_zoom = visual_viewport->offset_top();
visual_viewport->zoom(point, scale_delta);
if (visual_viewport->offset_left() != offset_left_before_zoom || visual_viewport->offset_top() != offset_top_before_zoom)
- m_navigable->queue_scrollend_event_after_user_scroll(*visual_viewport, {});
+ m_navigable->note_user_scroll_gesture_of(visual_viewport_stable_node_id, {}, HTML::LocalNavigable::UserScrollMovedBox::Yes);
return EventResult::Handled;
}
diff --git a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
index bd31483b64f..fcda25d912e 100644
--- a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
+++ b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
@@ -93,6 +93,8 @@ void MiddleButtonScrollHandler::perform_tick()
// until it runs out of scrolls, so it is held open for as long as this handler lives.
if (!m_scroll_gesture_hold)
m_scroll_gesture_hold = make<HTML::UserScrollGestureHold>(*navigable);
+ if (auto stable_node_id = Painting::async_scroll_node_stable_id(*layout_node); stable_node_id.has_value())
+ m_scroll_gesture_hold->hold(stable_node_id.value());
navigable->note_user_scroll_input_intent(Compositing::SnapSelectionStrategy::Type::EndPosition);
}
Painting::scroll_by(*layout_node, scroll_x, scroll_y);
diff --git a/Libraries/LibWeb/Painting/Scrollbar.cpp b/Libraries/LibWeb/Painting/Scrollbar.cpp
index 1926bf69d5a..a8d44f8f117 100644
--- a/Libraries/LibWeb/Painting/Scrollbar.cpp
+++ b/Libraries/LibWeb/Painting/Scrollbar.cpp
@@ -141,7 +141,7 @@ bool Scrollbar::scroll_to_mouse_position(CSSPixelPoint position)
: max(min(offset_relative_to_gutter, thumb_size / 2), offset_relative_to_gutter - gutter_size + thumb_size);
push_enlarged_state();
if (auto navigable = node->document().navigable())
- m_thumb_grab_gesture_hold = make<HTML::UserScrollGestureHold>(*navigable);
+ m_thumb_grab_gesture_hold = make<HTML::UserScrollGestureHold>(*navigable, async_scroll_node_stable_id(*node));
}
auto constrained_offset = AK::clamp(offset_relative_to_gutter - m_thumb_grab_position.value(), 0, gutter_size - thumb_size);
diff --git a/Libraries/LibWeb/Painting/Scrolling.cpp b/Libraries/LibWeb/Painting/Scrolling.cpp
index c57a868d5a9..15a82450ab0 100644
--- a/Libraries/LibWeb/Painting/Scrolling.cpp
+++ b/Libraries/LibWeb/Painting/Scrolling.cpp
@@ -225,12 +225,13 @@ ScrollHandled set_scroll_offset_from_user_input(Layout::Node& node, CSSPixelPoin
if (!navigable)
return scroll_handled;
+ if (!stable_node_id.has_value())
+ return scroll_handled;
if (scroll_handled == ScrollHandled::Yes) {
- if (auto event_target = scroll_event_target(node))
- navigable->queue_scrollend_event_after_user_scroll(*event_target, stable_node_id, scroll_offset_before_scroll);
+ navigable->note_user_scroll_gesture_of(stable_node_id.value(), scroll_offset_before_scroll, HTML::LocalNavigable::UserScrollMovedBox::Yes);
} else {
// User input keeps the scroll gesture in progress even when it does not move the scrolling box.
- navigable->defer_user_scroll_settlement();
+ navigable->extend_user_scroll_gesture_of(stable_node_id.value());
}
return scroll_handled;
}
diff --git a/Services/Compositor/AsyncScrollUpdateOutbox.cpp b/Services/Compositor/AsyncScrollUpdateOutbox.cpp
index e0eed82927b..bd48c7c36ed 100644
--- a/Services/Compositor/AsyncScrollUpdateOutbox.cpp
+++ b/Services/Compositor/AsyncScrollUpdateOutbox.cpp
@@ -28,18 +28,26 @@ void AsyncScrollUpdateOutbox::record_scroll_offsets(ReadonlySpan<Compositing::As
set_or_append_pending_scroll_offset(m_pending_scroll_offsets, scroll_offset);
}
-bool AsyncScrollUpdateOutbox::has_updates(bool user_scroll_gesture_in_progress) const
+void AsyncScrollUpdateOutbox::note_user_scroll_gesture_ended(ReadonlySpan<Web::AsyncScrollNodeStableID> stable_node_ids)
+{
+ for (auto stable_node_id : stable_node_ids) {
+ if (!m_boxes_whose_user_scroll_gesture_ended.contains_slow(stable_node_id))
+ m_boxes_whose_user_scroll_gesture_ended.append(stable_node_id);
+ }
+}
+
+bool AsyncScrollUpdateOutbox::has_updates(UserScrollGestureState const& user_scroll_gesture_state) const
{
return !m_pending_scroll_offsets.is_empty()
|| !m_completed_operation_ids.is_empty()
|| !m_operation_ids_taken_over_by_user_input.is_empty()
|| !m_started_user_scrolls.is_empty()
|| !m_applied_scroll_requests.is_empty()
- || m_user_scroll_gesture_ended
- || user_scroll_gesture_in_progress != m_published_user_scroll_gesture_in_progress;
+ || !m_boxes_whose_user_scroll_gesture_ended.is_empty()
+ || user_scroll_gesture_state != m_published_user_scroll_gesture_state;
}
-Compositing::PendingAsyncScrollUpdates AsyncScrollUpdateOutbox::take_updates(bool user_scroll_gesture_in_progress)
+Compositing::PendingAsyncScrollUpdates AsyncScrollUpdateOutbox::take_updates(UserScrollGestureState user_scroll_gesture_state)
{
Compositing::PendingAsyncScrollUpdates updates;
updates.sequence = ++m_last_publication_sequence;
@@ -48,10 +56,10 @@ Compositing::PendingAsyncScrollUpdates AsyncScrollUpdateOutbox::take_updates(boo
AK::swap(updates.operation_ids_taken_over_by_user_input, m_operation_ids_taken_over_by_user_input);
AK::swap(updates.started_user_scrolls, m_started_user_scrolls);
AK::swap(updates.applied_scroll_requests, m_applied_scroll_requests);
- updates.user_scroll_gesture_in_progress = user_scroll_gesture_in_progress;
- updates.user_scroll_gesture_ended = m_user_scroll_gesture_ended;
- m_user_scroll_gesture_ended = false;
- m_published_user_scroll_gesture_in_progress = updates.user_scroll_gesture_in_progress;
+ AK::swap(updates.boxes_whose_user_scroll_gesture_ended, m_boxes_whose_user_scroll_gesture_ended);
+ updates.boxes_in_user_scroll_gesture = user_scroll_gesture_state.boxes_in_user_scroll_gesture;
+ updates.wheel_latched_scroller = user_scroll_gesture_state.wheel_latched_scroller;
+ m_published_user_scroll_gesture_state = move(user_scroll_gesture_state);
return updates;
}
diff --git a/Services/Compositor/AsyncScrollUpdateOutbox.h b/Services/Compositor/AsyncScrollUpdateOutbox.h
index f87ebbf225f..c90dcab5cb8 100644
--- a/Services/Compositor/AsyncScrollUpdateOutbox.h
+++ b/Services/Compositor/AsyncScrollUpdateOutbox.h
@@ -20,11 +20,18 @@ public:
void note_operation_taken_over_by_user_input(Compositing::AsyncScrollOperationID operation_id) { m_operation_ids_taken_over_by_user_input.append(operation_id); }
void record_scroll_offsets(ReadonlySpan<Compositing::AsyncScrollOffset>);
void record_started_user_scroll(Compositing::StartedUserScroll started_user_scroll) { m_started_user_scrolls.append(move(started_user_scroll)); }
- void note_user_scroll_gesture_ended() { m_user_scroll_gesture_ended = true; }
+ void note_user_scroll_gesture_ended(ReadonlySpan<Web::AsyncScrollNodeStableID>);
void note_scroll_request_applied(Compositing::AppliedScrollRequest applied_scroll_request) { m_applied_scroll_requests.append(applied_scroll_request); }
- bool has_updates(bool user_scroll_gesture_in_progress) const;
- Compositing::PendingAsyncScrollUpdates take_updates(bool user_scroll_gesture_in_progress);
+ // What the compositor holds on to, published whenever it changes.
+ struct UserScrollGestureState {
+ Vector<Web::AsyncScrollNodeStableID> boxes_in_user_scroll_gesture;
+ Optional<Web::AsyncScrollNodeStableID> wheel_latched_scroller;
+
+ bool operator==(UserScrollGestureState const&) const = default;
+ };
+ bool has_updates(UserScrollGestureState const&) const;
+ Compositing::PendingAsyncScrollUpdates take_updates(UserScrollGestureState);
private:
Vector<Compositing::AsyncScrollOffset> m_pending_scroll_offsets;
@@ -34,8 +41,8 @@ private:
Vector<Compositing::AsyncScrollOperationID> m_operation_ids_taken_over_by_user_input;
Vector<Compositing::StartedUserScroll> m_started_user_scrolls;
Vector<Compositing::AppliedScrollRequest> m_applied_scroll_requests;
- bool m_user_scroll_gesture_ended { false };
- bool m_published_user_scroll_gesture_in_progress { false };
+ Vector<Web::AsyncScrollNodeStableID> m_boxes_whose_user_scroll_gesture_ended;
+ UserScrollGestureState m_published_user_scroll_gesture_state;
};
}
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 833b9207d36..941ad71f273 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -7,6 +7,7 @@
#include <AK/GenericShorthands.h>
#include <AK/JsonObject.h>
#include <AK/Math.h>
+#include <AK/ScopeGuard.h>
#include <AK/StdLibExtras.h>
#include <Compositor/CompositorState.h>
#include <Compositor/ContextState.h>
@@ -164,11 +165,11 @@ void ContextState::install_display_list_update(
m_visual_viewport_document_id.clear();
}
- auto was_dragging_scrollbar = m_scrollbar_controller.has_captured_scrollbar();
+ auto box_of_dragged_scrollbar = box_of_captured_scrollbar();
m_scrollbar_controller.set_scrollbars(async_scrolling_state.scrollbars);
if (!m_scrollbar_controller.has_captured_scrollbar())
stop_scrollbar_paging();
- note_user_scroll_gesture_end_if_drag_ended(was_dragging_scrollbar);
+ note_user_scroll_gesture_end_if_drag_ended(box_of_dragged_scrollbar);
m_async_scroll_tree.set_state(move(async_scrolling_state));
m_elastic_overscroll_controller.did_install_scrolling_state(m_async_scroll_tree);
m_scroll_snap_controller.did_install_scrolling_state(m_async_scroll_tree);
@@ -259,7 +260,7 @@ void ContextState::end_keyboard_scroll_gesture()
{
if (!m_keyboard_scroll_controller.release_all_keys())
return;
- m_async_scroll_update_outbox.note_user_scroll_gesture_ended();
+ end_user_scroll_gesture_of_held_keys();
request_rendering_update();
}
@@ -275,7 +276,7 @@ ContextState::ContextUpdateResult ContextState::handle_key_event(Web::KeyEvent c
if (release == KeyboardScrollController::KeyRelease::Ignored)
return {};
if (release == KeyboardScrollController::KeyRelease::ReleasedLastHeldKey)
- m_async_scroll_update_outbox.note_user_scroll_gesture_ended();
+ end_user_scroll_gesture_of_held_keys();
return { .accepted = true, .frame_to_present = {}, .should_request_rendering_update = true };
}
@@ -308,6 +309,8 @@ ContextState::ContextUpdateResult ContextState::handle_key_event(Web::KeyEvent c
m_keyboard_scroll_controller.hold_key(event.key);
auto stable_node_id = m_async_scroll_tree.scroll_node_for_id(*target)->stable_node_id;
+ if (!m_boxes_scrolled_by_held_keys.contains_slow(stable_node_id))
+ m_boxes_scrolled_by_held_keys.append(stable_node_id);
Optional<Gfx::FloatPoint> scroll_in_flight_destination;
for (auto const& running_animation : m_smooth_scroll_animations) {
if (running_animation.stable_node_id == stable_node_id && running_animation.is_user_scroll && running_animation.animation.kind() == Compositing::ScrollAnimationKind::SmoothScroll)
@@ -518,7 +521,7 @@ ContextState::ContextUpdateResult ContextState::handle_mouse_event(Web::MouseEve
if (event.button != Web::UIEvents::MouseButton::Primary)
return ContextUpdateResult::declined(Web::InputRoutingDecision::NotPrimaryButton);
- auto was_dragging_scrollbar = m_scrollbar_controller.has_captured_scrollbar();
+ auto box_of_dragged_scrollbar = box_of_captured_scrollbar();
auto scrollbar_dragged_by_compositor = m_scrollbar_controller.captured_scrollbar_painted_by_display_list();
auto drag = m_scrollbar_controller.release_captured_drag(m_scene.visual_context_tree_for_compositing(), m_scroll_state_snapshot, position);
auto was_paging = m_scrollbar_paging.has_value();
@@ -526,7 +529,7 @@ ContextState::ContextUpdateResult ContextState::handle_mouse_event(Web::MouseEve
if (!drag.has_value())
return ContextUpdateResult::declined(Web::InputRoutingDecision::NoScrollbarDrag);
- note_user_scroll_gesture_end_if_drag_ended(was_dragging_scrollbar);
+ note_user_scroll_gesture_end_if_drag_ended(box_of_dragged_scrollbar);
ContextUpdateResult result;
result.accepted = true;
@@ -568,10 +571,11 @@ ContextState::ContextUpdateResult ContextState::handle_mouse_event(Web::MouseEve
// A cancel ends a drag where it last scrolled to, as a cancel has no position of its own, and the pointer then
// counts as having left. The page still sees the cancel, so it is not accepted.
auto was_dragging_scrollbar = m_scrollbar_controller.has_captured_scrollbar();
+ auto box_of_dragged_scrollbar = box_of_captured_scrollbar();
auto scrollbar_dragged_by_compositor = m_scrollbar_controller.captured_scrollbar_painted_by_display_list();
(void)m_scrollbar_controller.release_captured_drag(m_scene.visual_context_tree_for_compositing(), m_scroll_state_snapshot, position);
stop_scrollbar_paging();
- note_user_scroll_gesture_end_if_drag_ended(was_dragging_scrollbar);
+ note_user_scroll_gesture_end_if_drag_ended(box_of_dragged_scrollbar);
auto hovered_scrollbar_changed = m_scrollbar_controller.set_hovered_scrollbar({});
ContextUpdateResult result;
@@ -1008,9 +1012,12 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
for (auto const& scroll_request : scroll_requests) {
// The box's offsets are reported as of this request from here on, whether or not the box is here yet.
m_applied_scroll_request_ids.set(scroll_request.stable_node_id, scroll_request.request_id);
- m_async_scroll_update_outbox.note_scroll_request_applied({ scroll_request.stable_node_id, scroll_request.request_id });
+ bool moved_the_box = false;
+ ScopeGuard note_request_applied = [&] {
+ m_async_scroll_update_outbox.note_scroll_request_applied({ scroll_request.stable_node_id, scroll_request.request_id, moved_the_box });
+ };
if (scroll_request.stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport) {
- apply_visual_viewport_scroll_request(scroll_request, answers);
+ moved_the_box = apply_visual_viewport_scroll_request(scroll_request, answers);
continue;
}
@@ -1027,6 +1034,7 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
// The answer tells the main thread where the box went for the request, even where it stayed where it was.
auto answer_with_the_offset_of_the_box = [&] {
auto new_offset = m_async_scroll_tree.scroll_offset_for_node(node_id.value(), m_scroll_state_snapshot).value();
+ moved_the_box = new_offset != old_offset.value();
if (m_async_scroll_tree.scroll_node_is_viewport(node_id.value()))
m_async_scrolling_viewport_rect.set_location(new_offset.to_type<int>());
answers.append({
@@ -1222,13 +1230,13 @@ void ContextState::scroll_visual_viewport_position_in_page(Compositing::AsyncScr
});
}
-void ContextState::apply_visual_viewport_scroll_request(Compositing::ScrollRequest const& scroll_request, Vector<Compositing::AsyncScrollOffset>& answers)
+bool 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.
if (scroll_request.smooth_scroll.has_value())
m_async_scroll_update_outbox.complete_operation(scroll_request.smooth_scroll->operation_id);
if (!m_scene.visual_context_tree().has_value() || scroll_request.stable_node_id.node_id != m_async_scroll_tree.document_id())
- return;
+ return false;
auto transform = m_scene.visual_viewport_transform();
auto old_scroll_offset = visual_viewport_scroll_offset(transform);
@@ -1254,6 +1262,7 @@ void ContextState::apply_visual_viewport_scroll_request(Compositing::ScrollReque
.applied_scroll_request_id = 0,
.user_input_scroll_count = 0,
});
+ return new_scroll_offset != old_scroll_offset;
}
Optional<Gfx::FloatPoint> ContextState::in_flight_wheel_destination(Web::AsyncScrollNodeStableID stable_node_id) const
@@ -1791,14 +1800,14 @@ Optional<Compositing::ScrollStateSnapshot> ContextState::scroll_state_snapshot_s
Compositing::PendingAsyncScrollUpdates ContextState::take_pending_async_scroll_updates()
{
- auto updates = m_async_scroll_update_outbox.take_updates(user_scroll_gesture_in_progress());
+ auto updates = m_async_scroll_update_outbox.take_updates(user_scroll_gesture_state());
updates.document_id = m_async_scroll_tree.document_id();
return updates;
}
bool ContextState::has_pending_async_scroll_updates() const
{
- return m_async_scroll_update_outbox.has_updates(user_scroll_gesture_in_progress());
+ return m_async_scroll_update_outbox.has_updates(user_scroll_gesture_state());
}
void ContextState::viewport_size_updated(Gfx::IntSize viewport_size, double device_pixel_ratio, Compositing::WindowResizingInProgress window_resize_in_progress)
@@ -2159,17 +2168,41 @@ void ContextState::cancel_smooth_scroll_taken_over_by_user_input(Compositing::As
}
}
-void ContextState::note_user_scroll_gesture_end_if_drag_ended(bool was_dragging_scrollbar)
+Optional<Web::AsyncScrollNodeStableID> ContextState::box_of_captured_scrollbar() const
+{
+ auto scrollbar_index = m_scrollbar_controller.captured_scrollbar_index();
+ if (!scrollbar_index.has_value())
+ return {};
+ return m_scrollbar_controller.scrollbars()[scrollbar_index.value()].scroller_stable_node_id;
+}
+
+void ContextState::note_user_scroll_gesture_end_if_drag_ended(Optional<Web::AsyncScrollNodeStableID> box_of_dragged_scrollbar)
{
- if (was_dragging_scrollbar && !m_scrollbar_controller.has_captured_scrollbar())
- m_async_scroll_update_outbox.note_user_scroll_gesture_ended();
+ if (box_of_dragged_scrollbar.has_value() && !m_scrollbar_controller.has_captured_scrollbar())
+ m_async_scroll_update_outbox.note_user_scroll_gesture_ended({ &box_of_dragged_scrollbar.value(), 1 });
}
-bool ContextState::user_scroll_gesture_in_progress() const
+void ContextState::end_user_scroll_gesture_of_held_keys()
{
- return m_scrollbar_controller.has_captured_scrollbar()
- || m_keyboard_scroll_controller.has_held_keys()
- || m_scroll_snap_controller.has_gesture_awaiting_input();
+ m_async_scroll_update_outbox.note_user_scroll_gesture_ended(m_boxes_scrolled_by_held_keys);
+ m_boxes_scrolled_by_held_keys.clear();
+}
+
+AsyncScrollUpdateOutbox::UserScrollGestureState ContextState::user_scroll_gesture_state() const
+{
+ AsyncScrollUpdateOutbox::UserScrollGestureState state;
+ if (auto box = box_of_captured_scrollbar(); box.has_value())
+ state.boxes_in_user_scroll_gesture.append(box.value());
+ for (auto box : m_boxes_scrolled_by_held_keys) {
+ if (!state.boxes_in_user_scroll_gesture.contains_slow(box))
+ state.boxes_in_user_scroll_gesture.append(box);
+ }
+ for (auto box : m_scroll_snap_controller.boxes_with_gesture_awaiting_input()) {
+ if (!state.boxes_in_user_scroll_gesture.contains_slow(box))
+ state.boxes_in_user_scroll_gesture.append(box);
+ }
+ state.wheel_latched_scroller = m_wheel_gesture_routing.latched_scroller();
+ return state;
}
void ContextState::schedule_end_of_scroll_step_gestures(MonotonicTime now)
@@ -2190,8 +2223,8 @@ void ContextState::schedule_end_of_scroll_step_gestures(MonotonicTime now)
void ContextState::end_scroll_step_gestures_whose_input_ran_out(MonotonicTime now)
{
- if (m_scroll_snap_controller.end_gestures_whose_input_ran_out(now)) {
- m_async_scroll_update_outbox.note_user_scroll_gesture_ended();
+ if (auto ended_boxes = m_scroll_snap_controller.end_gestures_whose_input_ran_out(now); !ended_boxes.is_empty()) {
+ m_async_scroll_update_outbox.note_user_scroll_gesture_ended(ended_boxes);
request_rendering_update();
}
schedule_end_of_scroll_step_gestures(now);
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 75239dcf84d..6266ea43cac 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -277,7 +277,8 @@ private:
// 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);
+ // Returns whether the request moved the visual viewport.
+ bool 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.
Gfx::FloatPoint pan_visual_viewport_by(Gfx::FloatPoint delta_in_screen_pixels);
@@ -333,8 +334,10 @@ private:
AsyncScrollResult smooth_wheel_scroll_by(Optional<Compositing::WheelScrollTarget> const&, Gfx::IntRect viewport_rect, MonotonicTime now);
void retarget_user_scroll(ActiveSmoothScrollAnimation&, Gfx::FloatPoint destination_offset, MonotonicTime now);
void report_started_user_scroll(Compositing::AsyncScrollOperationID, Web::AsyncScrollNodeStableID, Gfx::FloatPoint step_start_offset, Gfx::FloatPoint destination_offset, Compositing::ScrollAnimationKind, bool is_absolute_scroll = false);
- void note_user_scroll_gesture_end_if_drag_ended(bool was_dragging_scrollbar);
- bool user_scroll_gesture_in_progress() const;
+ Optional<Web::AsyncScrollNodeStableID> box_of_captured_scrollbar() const;
+ void note_user_scroll_gesture_end_if_drag_ended(Optional<Web::AsyncScrollNodeStableID> box_of_dragged_scrollbar);
+ void end_user_scroll_gesture_of_held_keys();
+ AsyncScrollUpdateOutbox::UserScrollGestureState user_scroll_gesture_state() const;
void schedule_end_of_scroll_step_gestures(MonotonicTime now);
// A gesture whose steps this context chains ends once they stop arriving, which is reported to WebContent so
// that it settles the gesture then rather than on a clock of its own.
@@ -380,6 +383,8 @@ private:
// The document whose visual viewport this context zoomed or panned, which it presents as it left it from then on.
Optional<Web::UniqueNodeID> m_visual_viewport_document_id;
KeyboardScrollController m_keyboard_scroll_controller;
+ // The boxes that the keys held since none was have scrolled, whose gesture lasts until the last of them is released.
+ Vector<Web::AsyncScrollNodeStableID> m_boxes_scrolled_by_held_keys;
RefPtr<Core::Timer> m_scroll_step_gesture_input_timer;
WheelGestureRouting m_wheel_gesture_routing;
Vector<ActiveSmoothScrollAnimation> m_smooth_scroll_animations;
diff --git a/Services/Compositor/ScrollSnapController.cpp b/Services/Compositor/ScrollSnapController.cpp
index 1b8f639069b..c8164e1f542 100644
--- a/Services/Compositor/ScrollSnapController.cpp
+++ b/Services/Compositor/ScrollSnapController.cpp
@@ -46,13 +46,14 @@ void ScrollSnapController::end_gesture()
state.gesture_start = {};
}
-bool ScrollSnapController::has_gesture_awaiting_input() const
+Vector<Web::AsyncScrollNodeStableID> ScrollSnapController::boxes_with_gesture_awaiting_input() const
{
+ Vector<Web::AsyncScrollNodeStableID> boxes;
for (auto const& [stable_node_id, state] : m_nodes) {
if (state.gesture_input_deadline.has_value())
- return true;
+ boxes.append(stable_node_id);
}
- return false;
+ return boxes;
}
Optional<MonotonicTime> ScrollSnapController::earliest_gesture_input_deadline() const
@@ -67,17 +68,17 @@ Optional<MonotonicTime> ScrollSnapController::earliest_gesture_input_deadline()
return earliest;
}
-bool ScrollSnapController::end_gestures_whose_input_ran_out(MonotonicTime now)
+Vector<Web::AsyncScrollNodeStableID> ScrollSnapController::end_gestures_whose_input_ran_out(MonotonicTime now)
{
- bool ended_any = false;
+ Vector<Web::AsyncScrollNodeStableID> ended_boxes;
for (auto& [stable_node_id, state] : m_nodes) {
if (!state.gesture_input_deadline.has_value() || now <= *state.gesture_input_deadline)
continue;
state.gesture_input_deadline = {};
state.unsnapped_scroll_destination = {};
- ended_any = true;
+ ended_boxes.append(stable_node_id);
}
- return ended_any;
+ return ended_boxes;
}
void ScrollSnapController::did_scroll_node_plainly(Web::AsyncScrollNodeStableID stable_node_id, Web::ScrollGesturePhase phase, Web::CSSPixelPoint scroll_offset_before_scroll)
diff --git a/Services/Compositor/ScrollSnapController.h b/Services/Compositor/ScrollSnapController.h
index f4b98c7a728..be47d130817 100644
--- a/Services/Compositor/ScrollSnapController.h
+++ b/Services/Compositor/ScrollSnapController.h
@@ -90,9 +90,10 @@ public:
// A gesture whose steps this controller chains awaits its next step until its input deadline passes, and ends
// once that happens without one.
- bool has_gesture_awaiting_input() const;
+ Vector<Web::AsyncScrollNodeStableID> boxes_with_gesture_awaiting_input() const;
Optional<MonotonicTime> earliest_gesture_input_deadline() const;
- bool end_gestures_whose_input_ran_out(MonotonicTime now);
+ // Returns the boxes whose gesture ended.
+ Vector<Web::AsyncScrollNodeStableID> end_gestures_whose_input_ran_out(MonotonicTime now);
private:
struct InFlightSnapScroll {
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/fling-ends-with-its-momentum-under-a-wheel-listener.txt b/Tests/LibWeb/Text/expected/async-scrolling/fling-ends-with-its-momentum-under-a-wheel-listener.txt
new file mode 100644
index 00000000000..17e4edf0f51
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/fling-ends-with-its-momentum-under-a-wheel-listener.txt
@@ -0,0 +1 @@
+scrollend events within 3 frames of the end of the momentum: 1
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/script-smooth-scroll-during-a-wheel-gesture-ends-once.txt b/Tests/LibWeb/Text/expected/async-scrolling/script-smooth-scroll-during-a-wheel-gesture-ends-once.txt
new file mode 100644
index 00000000000..d98960c4302
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/script-smooth-scroll-during-a-wheel-gesture-ends-once.txt
@@ -0,0 +1 @@
+scrollend events at: 3000
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/scrollend-of-a-box-flung-during-a-thumb-drag-of-another.txt b/Tests/LibWeb/Text/expected/async-scrolling/scrollend-of-a-box-flung-during-a-thumb-drag-of-another.txt
new file mode 100644
index 00000000000..978ad3df84a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/async-scrolling/scrollend-of-a-box-flung-during-a-thumb-drag-of-another.txt
@@ -0,0 +1,4 @@
+flung box at rest while the thumb is held: dragged box 0, flung box 1
+thumb released: dragged box 1, flung box 1
+thumb released while the other box is under momentum: dragged box 2, flung box 1
+momentum ended: dragged box 2, flung box 2
diff --git a/Tests/LibWeb/Text/expected/visual-viewport-scrollend-after-pan.txt b/Tests/LibWeb/Text/expected/visual-viewport-scrollend-after-pan.txt
index bcb7300a343..29c42332b2a 100644
--- a/Tests/LibWeb/Text/expected/visual-viewport-scrollend-after-pan.txt
+++ b/Tests/LibWeb/Text/expected/visual-viewport-scrollend-after-pan.txt
@@ -5,7 +5,7 @@ visual viewport scrolled down: true
window.scrollY: 0
visual viewport scroll and scrollend: received
window scrolled: true
-scrollend order: visualViewport,document
+scrollend order: document,visualViewport
after reset scale: 1
after reset offsets: 0,0
reset scroll and scrollend: received
diff --git a/Tests/LibWeb/Text/input/async-scrolling/fling-ends-with-its-momentum-under-a-wheel-listener.html b/Tests/LibWeb/Text/input/async-scrolling/fling-ends-with-its-momentum-under-a-wheel-listener.html
new file mode 100644
index 00000000000..efa7cafcd9c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/fling-ends-with-its-momentum-under-a-wheel-listener.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+
+ #scroller {
+ width: 200px;
+ height: 200px;
+ overflow: scroll;
+ }
+
+ .content {
+ height: 5000px;
+ }
+</style>
+<div id="scroller"><div class="content"></div></div>
+<div id="wheels"></div>
+<script>
+ promiseTest(async () => {
+ const scroller = document.getElementById("scroller");
+ const wheels = document.getElementById("wheels");
+ // The listener leaves layout out of date as the momentum ends.
+ let wheelCount = 0;
+ scroller.addEventListener("wheel", () => (wheels.textContent = ++wheelCount), { passive: true });
+ let scrollends = 0;
+ scroller.addEventListener("scrollend", () => scrollends++);
+ await animationFrame();
+ await animationFrame();
+
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ for (let step = 0; step < 3; step++)
+ await internals.wheelThroughUIProcess(50, 50, 0, 50, true, "ongoing");
+ for (let step = 0; step < 5; step++) {
+ await internals.wheelThroughUIProcess(50, 50, 0, 30, true, "momentum");
+ await animationFrame();
+ }
+ internals.wheelThroughUIProcess(50, 50, 0, 30, true, "momentum");
+ await internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended");
+ for (let frame = 0; frame < 3; frame++)
+ await animationFrame();
+ println(`scrollend events within 3 frames of the end of the momentum: ${scrollends}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/script-smooth-scroll-during-a-wheel-gesture-ends-once.html b/Tests/LibWeb/Text/input/async-scrolling/script-smooth-scroll-during-a-wheel-gesture-ends-once.html
new file mode 100644
index 00000000000..48974a21521
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/script-smooth-scroll-during-a-wheel-gesture-ends-once.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+
+ #scroller {
+ width: 200px;
+ height: 200px;
+ overflow: scroll;
+ }
+
+ .content {
+ height: 5000px;
+ }
+</style>
+<div id="scroller"><div class="content"></div></div>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ const scroller = document.getElementById("scroller");
+ const scrollends = [];
+ scroller.addEventListener("scrollend", () => scrollends.push(scroller.scrollTop));
+
+ // A script starts a smooth scroll while the fingers are still down, then they lift. The box's scroll ends once,
+ // when the script's scroll arrives.
+ internals.wheelThroughUIProcess(50, 50, 0, 0, true, "began");
+ for (let step = 0; step < 3; step++)
+ await internals.wheelThroughUIProcess(50, 50, 0, 50, true, "ongoing");
+ scroller.scrollTo({ top: 3000, behavior: "smooth" });
+ await new Promise(resolve => setTimeout(resolve, 100));
+ await internals.wheelThroughUIProcess(50, 50, 0, 0, true, "ended");
+ await new Promise(resolve => setTimeout(resolve, 2500));
+ println(`scrollend events at: ${scrollends.join(", ")}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/async-scrolling/scrollend-of-a-box-flung-during-a-thumb-drag-of-another.html b/Tests/LibWeb/Text/input/async-scrolling/scrollend-of-a-box-flung-during-a-thumb-drag-of-another.html
new file mode 100644
index 00000000000..a05c65cfc81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/async-scrolling/scrollend-of-a-box-flung-during-a-thumb-drag-of-another.html
@@ -0,0 +1,86 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+
+ .scroller {
+ position: absolute;
+ top: 0;
+ width: 150px;
+ height: 100px;
+ overflow-x: hidden;
+ overflow-y: scroll;
+ }
+
+ #dragged {
+ left: 0;
+ }
+
+ #flung {
+ left: 200px;
+ }
+
+ .content {
+ height: 5000px;
+ }
+</style>
+<div class="scroller" id="dragged"><div class="content"></div></div>
+<div class="scroller" id="flung"><div class="content"></div></div>
+<script>
+ promiseTest(async () => {
+ await animationFrame();
+ await animationFrame();
+
+ const dragged = document.getElementById("dragged");
+ const flung = document.getElementById("flung");
+ const scrollends = new Map([[dragged, 0], [flung, 0]]);
+ for (const scroller of scrollends.keys())
+ scroller.addEventListener("scrollend", () => scrollends.set(scroller, scrollends.get(scroller) + 1));
+ const report = when => println(`${when}: dragged box ${scrollends.get(dragged)}, flung box ${scrollends.get(flung)}`);
+ const settle = async () => {
+ for (let frame = 0; frame < 4; frame++)
+ await animationFrame();
+ await new Promise(resolve => setTimeout(resolve, 700));
+ };
+ const flick = async () => {
+ internals.wheelThroughUIProcess(250, 50, 0, 0, true, "began");
+ for (let step = 0; step < 3; step++)
+ await internals.wheelThroughUIProcess(250, 50, 0, 50, true, "ongoing");
+ };
+ const momentumFor = async milliseconds => {
+ const end = performance.now() + milliseconds;
+ while (performance.now() < end) {
+ await internals.wheelThroughUIProcess(250, 50, 0, 2, true, "momentum");
+ await animationFrame();
+ }
+ };
+
+ // The flung box comes to rest while the thumb of the other is held, and its scroll ends then.
+ await internals.mouseEventThroughUIProcess("mousemove", 147, 5);
+ await internals.mouseEventThroughUIProcess("mousedown", 147, 5);
+ await internals.mouseEventThroughUIProcess("mousemove", 147, 40, { buttons: 1 });
+ await flick();
+ await momentumFor(100);
+ await internals.wheelThroughUIProcess(250, 50, 0, 0, true, "ended");
+ await settle();
+ report("flung box at rest while the thumb is held");
+ await internals.mouseEventThroughUIProcess("mouseup", 147, 40);
+ await settle();
+ report("thumb released");
+
+ // The thumb of the other box is let go of while the flung box is still under momentum, and its scroll ends
+ // then.
+ await flick();
+ await internals.mouseEventThroughUIProcess("mousemove", 147, 40);
+ await internals.mouseEventThroughUIProcess("mousedown", 147, 40);
+ await internals.mouseEventThroughUIProcess("mousemove", 147, 70, { buttons: 1 });
+ await internals.mouseEventThroughUIProcess("mouseup", 147, 70);
+ await momentumFor(1000);
+ report("thumb released while the other box is under momentum");
+ await internals.wheelThroughUIProcess(250, 50, 0, 0, true, "ended");
+ await settle();
+ report("momentum ended");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/scroll-snap-wheel-step-of-a-new-gesture-during-snap-scroll.html b/Tests/LibWeb/Text/input/scroll-snap-wheel-step-of-a-new-gesture-during-snap-scroll.html
index 0106c2de86a..03cade02364 100644
--- a/Tests/LibWeb/Text/input/scroll-snap-wheel-step-of-a-new-gesture-during-snap-scroll.html
+++ b/Tests/LibWeb/Text/input/scroll-snap-wheel-step-of-a-new-gesture-during-snap-scroll.html
@@ -56,11 +56,12 @@
await settled;
};
- await wheelStepsWhileMovingThePointer(50, 50, [200, -40], [reversalScroller]);
+ // The second step turns the box back before it has moved, so the box gets no scrollend event.
+ await wheelStepsWhileMovingThePointer(50, 50, [200, -40], []);
println(`Reversal in a nested scroller: ${reversalScroller.scrollTop}, window: ${window.scrollY}`);
document.documentElement.style.scrollSnapType = "y mandatory";
- await wheelStepsWhileMovingThePointer(300, 50, [200, -40], [document]);
+ await wheelStepsWhileMovingThePointer(300, 50, [200, -40], []);
println(`Reversal on the root scroller: ${window.scrollY}`);
document.documentElement.style.scrollSnapType = "";
diff --git a/Tests/LibWeb/Text/input/visual-viewport-scrollend-after-pan.html b/Tests/LibWeb/Text/input/visual-viewport-scrollend-after-pan.html
index 6e02befca80..919612f0bd7 100644
--- a/Tests/LibWeb/Text/input/visual-viewport-scrollend-after-pan.html
+++ b/Tests/LibWeb/Text/input/visual-viewport-scrollend-after-pan.html
@@ -25,14 +25,20 @@
println(`window.scrollY: ${window.scrollY}`);
println("visual viewport scroll and scrollend: received");
+ // The window's larger wheel step animates faster than the visual viewport's, so its scroll completes first.
const scrollendOrder = [];
- visualViewport.addEventListener("scrollend", () => scrollendOrder.push("visualViewport"), { once: true });
- const documentScrollend = new Promise(resolve => document.addEventListener("scrollend", () => {
- scrollendOrder.push("document");
- resolve();
- }, { once: true }));
+ const bothScrollends = Promise.all([
+ new Promise(resolve => visualViewport.addEventListener("scrollend", () => {
+ scrollendOrder.push("visualViewport");
+ resolve();
+ }, { once: true })),
+ new Promise(resolve => document.addEventListener("scrollend", () => {
+ scrollendOrder.push("document");
+ resolve();
+ }, { once: true })),
+ ]);
await internals.wheel(100, 100, 0, 800);
- await documentScrollend;
+ await bothScrollends;
println(`window scrolled: ${window.scrollY > 0}`);
println(`scrollend order: ${scrollendOrder.join(",")}`);