Truly independent web browser
LibWeb: Perform the visual viewport part of viewport scrolls as a scroll
Zaggy1024a1016e16f488
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 85564409515..606faa01adf 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -4984,6 +4984,12 @@ Optional<CSSPixelPoint> LocalNavigable::scroll_offset_for(Web::AsyncScrollNodeStreturn m_viewport_scroll_offset;}+ if (stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport) {+ if (stable_node_id.node_id != document->unique_id())+ return {};+ return document->visual_viewport()->offset();+ }+auto* element = element_for_async_scroll_node_stable_id(*document, stable_node_id);if (!element)return {};@@ -5033,6 +5039,15 @@ bool LocalNavigable::set_scroll_offset_for(Web::AsyncScrollNodeStableID stable_nreturn old_scroll_offset != m_viewport_scroll_offset;}+ if (stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport) {+ if (stable_node_id.node_id != document->unique_id())+ return false;+ auto visual_viewport = document->visual_viewport();+ auto delta = scroll_offset - visual_viewport->offset();+ visual_viewport->scroll_by(delta);+ return !delta.is_zero();+ }+if (!element_for_async_scroll_node_stable_id(*document, stable_node_id))return false;Layout::ForcedReadScope read { *document };@@ -5136,6 +5151,11 @@ static GC::Ptr<DOM::EventTarget> scroll_event_target_for_async_scroll_node(DOM::return {};return document;}+ if (stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport) {+ if (stable_node_id.node_id != document.unique_id())+ return {};+ return document.visual_viewport();+ }return element_for_async_scroll_node_stable_id(document, stable_node_id);}@@ -5636,9 +5656,7 @@ void LocalNavigable::process_main_thread_smooth_scrolls()continue;}- auto elapsed_since_last_tick = now - smooth_scroll.last_tick;- auto minimum_tick_duration = AK::Duration::from_milliseconds(1);- smooth_scroll.elapsed += max(elapsed_since_last_tick, minimum_tick_duration);+ smooth_scroll.elapsed += now - smooth_scroll.last_tick;smooth_scroll.last_tick = now;auto sample = smooth_scroll.animation.sample(smooth_scroll.elapsed);set_scroll_offset_for(smooth_scroll.stable_node_id, sample.offset.to_type<CSSPixels>());@@ -7176,7 +7194,9 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Webreturn scroll_promise;}- if (has_compositor_context()) {+ // NB: The compositor has no scroll node for the visual viewport, so the main thread animates it.+ bool compositor_can_animate = has_compositor_context() && stable_node_id.kind != Web::AsyncScrollNodeKind::VisualViewport;+ if (compositor_can_animate) {// NB: Do not adopt compositor progress while replacing one smooth scroll// with another. All listeners in the current JavaScript task must// observe the same main-thread scroll offset. The compositor starts@@ -7203,7 +7223,7 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_a_scrolling_box(Web// NB: A page can lack compositor scroll state before its first paint. Keep// the same algorithm on the main thread in that case.- if (has_compositor_context()) {+ if (compositor_can_animate) {// NB: The compositor rejected the replacement, so consume its last// offset before falling back to a main-thread animation.adopt_pending_async_scroll_offsets(Compositing::AsyncScrollUpdateFreshness::FromCompositor);@@ -7502,6 +7522,9 @@ bool LocalNavigable::scroll_viewport_by_delta(CSSPixelPoint delta, Bindings::ScrWeb::AsyncScrollNodeStableID viewport_stable_node_id { .node_id = document->unique_id(), .kind = Web::AsyncScrollNodeKind::Viewport };if (auto destination = in_flight_user_scroll_destination(viewport_stable_node_id, animation_kind); destination.has_value())page_position += *destination - viewport_scroll_offset();+ Web::AsyncScrollNodeStableID visual_viewport_stable_node_id { .node_id = document->unique_id(), .kind = Web::AsyncScrollNodeKind::VisualViewport };+ if (auto destination = in_flight_user_scroll_destination(visual_viewport_stable_node_id, animation_kind); destination.has_value())+ page_position += destination.value() - vv->offset();return page_position;};@@ -7562,26 +7585,33 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix// 14. Perform a scroll of the viewport’s scrolling box to its current scroll position + (layout dx, layout dy)// with element as the associated element, and behavior as the scroll behavior. Let scrollPromise1 be the// Promise returned from this step.+ // NB: Performed after step 15, below.TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*doc) };// 15. Perform a scroll of vv’s scrolling box to its current scroll position + (visual dx, visual dy) with element// as the associated element, and behavior as the scroll behavior. Let scrollPromise2 be the Promise returned// from this step.- // FIXME: Get a Promise from this.// AD-HOC: Step 15 is performed before step 14 so that the visual viewport's scroll and scrollend events are// dispatched before the window's.CSSPixelPoint visual_delta { visual_dx, visual_dy };- vv->scroll_by(visual_delta);if (visual_delta.is_zero())doc->set_needs_repaint(Badge<HTML::LocalNavigable> {}, InvalidateDisplayList::No);- else- queue_scrollend_event(*doc, *vv, {}, trigger);+ Web::AsyncScrollNodeStableID visual_viewport_stable_node_id { .node_id = doc->unique_id(), .kind = Web::AsyncScrollNodeKind::VisualViewport };+ auto scroll_promise_2 = perform_a_scroll_of_a_scrolling_box(visual_viewport_stable_node_id, vv->offset() + visual_delta, behavior, doc->document_element(), trigger, {}, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);++ auto scroll_promise_1 = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind);- auto scroll_promise = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind);+ // 16. Let scrollPromise be a new Promise.+ auto scroll_promise = WebIDL::create_promise_for(*doc);// 17. Return scrollPromise, and run the remaining steps in parallel.// 18. Resolve scrollPromise when both scrollPromise1 and scrollPromise2 have settled.- // FIXME: Actually wait for visual viewport scrolling to settle as well.+ auto& realm = HTML::relevant_realm(*doc);+ TemporaryExecutionContext execution_context_for_promise_reactions { realm, TemporaryExecutionContext::CallbacksEnabled::Yes };+ WebIDL::wait_for_all(+ realm, { { scroll_promise_1, scroll_promise_2 } },+ [scroll_promise](auto const&) { WebIDL::resolve_promise(scroll_promise); },+ [scroll_promise](JS::Value) { WebIDL::resolve_promise(scroll_promise); });return scroll_promise;}diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 0a00e814717..afae61def5c 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -1088,6 +1088,16 @@ Text/input/wpt-import/css/cssom-view/scroll-behavior-default-css.htmlText/input/wpt-import/css/cssom-view/scroll-behavior-scrollintoview-nested.html; Expects an animation's 1ms delay to elapse within two framesText/input/wpt-import/css/css-animations/event-dispatch.tentative.html+; Waits for scrolls that animate in real time, which uncapped frames only spin through+Text/input/async-scrolling/overscroll-behavior-on-a-box-with-nothing-to-scroll.html+Text/input/async-scrolling/overscroll-behavior-stops-wheel-chaining.html+Text/input/scroll-snap-scroll-step-while-pinch-zoomed.html+Text/input/scroll-snap-wheel-step-after-instant-programmatic-scroll.html+Text/input/scroll-snap-wheel-step-during-programmatic-scroll.html+Text/input/scroll-snap-wheel-step-past-nested-navigable-extent.html+Text/input/scroll-with-pinch-zoom-applied.html+Text/input/visual-viewport-horizontal-scroll-hit-test.html+Text/input/visual-viewport-scrollend-after-pan.html[LoadFromInsecureOrigin]Text/input/HTML/page-from-an-insecure-origin-is-not-a-secure-context.htmldiff --git a/Tests/LibWeb/Text/expected/visual-viewport-smooth-scroll-of-the-viewport.txt b/Tests/LibWeb/Text/expected/visual-viewport-smooth-scroll-of-the-viewport.txtnew file mode 100644index 00000000000..3ff4ce836a8--- /dev/null+++ b/Tests/LibWeb/Text/expected/visual-viewport-smooth-scroll-of-the-viewport.txt@@ -0,0 +1,5 @@+scale: 2+visual viewport offset right after the call: 0+visual viewport offset once the scroll resolved: 100+layout viewport offset: 0+scrollend events: visual viewportdiff --git a/Tests/LibWeb/Text/input/include.js b/Tests/LibWeb/Text/input/include.jsindex ec4d34459fb..9a305c50d20 100644--- a/Tests/LibWeb/Text/input/include.js+++ b/Tests/LibWeb/Text/input/include.js@@ -146,12 +146,16 @@ function printInputTrace(trace, { stages } = {}) {}}+// The offset has stopped once it holds for as long as five frames take at 60 Hz, however often frames come.async function scrollOffsetStopsChanging(readScrollOffset) {- let previousScrollOffset = null;- while (previousScrollOffset !== readScrollOffset()) {- previousScrollOffset = readScrollOffset();- for (let frame = 0; frame < 5; frame++) {- await animationFrame();+ const settleDuration = (5 * 1000) / 60;+ let scrollOffset = readScrollOffset();+ let lastChangeTime = performance.now();+ while (performance.now() - lastChangeTime < settleDuration) {+ await animationFrame();+ if (readScrollOffset() !== scrollOffset) {+ scrollOffset = readScrollOffset();+ lastChangeTime = performance.now();}}}diff --git a/Tests/LibWeb/Text/input/scroll-with-pinch-zoom-applied.html b/Tests/LibWeb/Text/input/scroll-with-pinch-zoom-applied.htmlindex cc915ee3471..ac7d8968fcd 100644--- a/Tests/LibWeb/Text/input/scroll-with-pinch-zoom-applied.html+++ b/Tests/LibWeb/Text/input/scroll-with-pinch-zoom-applied.html@@ -20,7 +20,7 @@println(`Initial: pageTop=${vv.pageTop}, scale=${vv.scale}`);// First, zoom in- vv.addEventListener("resize", function onResize() {+ vv.addEventListener("resize", async function onResize() {vv.removeEventListener("resize", onResize);println(`After zoom: pageTop=${vv.pageTop}, scale=${vv.scale}, offsetTop=${vv.offsetTop}`);@@ -29,23 +29,21 @@// Scroll downinternals.wheel(100, 100, 0, scrollDelta);+ await scrollOffsetStopsChanging(() => vv.pageTop);- // Poll for changes since scroll events may not fire for visual viewport panning- requestAnimationFrame(() => {- const pageTopAfter = vv.pageTop;- const scrolled = pageTopAfter - pageTopBefore;-- println(`pageTop changed by: ${scrolled}`);-- // The scroll amount should be approximately equal to the requested delta- // Allow small tolerance for floating point- if (Math.abs(scrolled - scrollDelta) < 1) {- println("PASS");- } else {- println(`FAIL: Expected scroll of ${scrollDelta}, got ${scrolled}`);- }- done();- });+ const pageTopAfter = vv.pageTop;+ const scrolled = pageTopAfter - pageTopBefore;++ println(`pageTop changed by: ${scrolled}`);++ // The scroll amount should be approximately equal to the requested delta+ // Allow small tolerance for floating point+ if (Math.abs(scrolled - scrollDelta) < 1) {+ println("PASS");+ } else {+ println(`FAIL: Expected scroll of ${scrollDelta}, got ${scrolled}`);+ }+ done();});requestAnimationFrame(() => {diff --git a/Tests/LibWeb/Text/input/visual-viewport-horizontal-scroll-hit-test.html b/Tests/LibWeb/Text/input/visual-viewport-horizontal-scroll-hit-test.htmlindex a29f1d04203..0d2ab439d11 100644--- a/Tests/LibWeb/Text/input/visual-viewport-horizontal-scroll-hit-test.html+++ b/Tests/LibWeb/Text/input/visual-viewport-horizontal-scroll-hit-test.html@@ -24,7 +24,7 @@asyncTest(done => {const vv = window.visualViewport;- vv.addEventListener("resize", function onResize() {+ vv.addEventListener("resize", async function onResize() {vv.removeEventListener("resize", onResize);const results = [];@@ -38,6 +38,7 @@// Scroll right by 400 to pan the visual viewport to the right halfinternals.wheel(200, 150, 400, 0);+ await scrollOffsetStopsChanging(() => vv.offsetLeft);// Do not call println() here — DOM mutation triggers layout which// rebuilds AccumulatedVisualContext, masking bugs in scroll invalidation.diff --git a/Tests/LibWeb/Text/input/visual-viewport-smooth-scroll-of-the-viewport.html b/Tests/LibWeb/Text/input/visual-viewport-smooth-scroll-of-the-viewport.htmlnew file mode 100644index 00000000000..97887e5362b--- /dev/null+++ b/Tests/LibWeb/Text/input/visual-viewport-smooth-scroll-of-the-viewport.html@@ -0,0 +1,33 @@+<!DOCTYPE html>+<script src="include.js"></script>+<style>+ body {+ margin: 0;+ height: 3000px;+ }+</style>+<script>+ promiseTest(async () => {+ await animationFrame();+ await animationFrame();++ const vv = window.visualViewport;+ internals.pinch(0, 0, 1.0);+ await animationFrame();+ println(`scale: ${vv.scale}`);++ const events = [];+ vv.addEventListener("scrollend", () => events.push("visual viewport"));+ document.addEventListener("scrollend", () => events.push("document"));++ // The visual viewport takes all of the scroll, which is as smooth as the scroll of the viewport asks for.+ const scrollPromise = window.scrollBy({ top: 100, behavior: "smooth" });+ println(`visual viewport offset right after the call: ${vv.offsetTop}`);+ await scrollPromise;+ println(`visual viewport offset once the scroll resolved: ${vv.offsetTop}`);+ println(`layout viewport offset: ${window.scrollY}`);++ await animationFrame();+ println(`scrollend events: ${events.join(", ")}`);+ });+</script>