Skip to content

Truly independent web browser

LibWeb+Compositor: Give the visual viewport its own scroll node

Zaggy1024d0c880728705
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index e0db1143b53..4a6767e224f 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -4802,6 +4802,7 @@ static GC::Ptr<DOM::Element> adopt_async_element_scroll_delta(DOM::Document& doc
Optional<CSS::PseudoElement> pseudo_element;
switch (stable_id.kind) {
case Web::AsyncScrollNodeKind::Viewport:
+ case Web::AsyncScrollNodeKind::VisualViewport:
return {};
case Web::AsyncScrollNodeKind::Element:
break;
@@ -5659,21 +5660,43 @@ void LocalNavigable::process_main_thread_smooth_scrolls()
page().client().request_frame();
}
-static bool adopt_async_viewport_scroll_delta(LocalNavigable& navigable, CSSPixelPoint scroll_delta)
+bool LocalNavigable::adopt_async_viewport_scroll_delta(CSSPixelPoint scroll_delta)
{
- auto document = navigable.active_document();
+ if (!active_document())
+ return false;
+
+ defer_user_scroll_settlement();
+
+ auto viewport_scroll_offset = m_viewport_scroll_offset;
+ // The direction of the scroll was recorded from the compositor's report, which knows whether it was relative.
+ perform_a_scroll_of_the_layout_viewport(scroll_delta, Bindings::ScrollBehavior::Instant, ScrollTrigger::UserInput, {}, Painting::ScrollKind::Absolute, Compositing::ScrollAnimationKind::SmoothScroll);
+ return m_viewport_scroll_offset != viewport_scroll_offset;
+}
+
+bool LocalNavigable::adopt_async_visual_viewport_scroll_delta(CSSPixelPoint scroll_delta)
+{
+ auto document = active_document();
if (!document)
return false;
+ defer_user_scroll_settlement();
+
auto visual_viewport = document->visual_viewport();
- CSSPixelPoint page_position { CSSPixels(visual_viewport->page_left()), CSSPixels(visual_viewport->page_top()) };
- auto viewport_scroll_offset = navigable.viewport_scroll_offset();
- // The direction of the scroll was recorded from the compositor's report, which knows whether it was relative.
- navigable.scroll_viewport_by_delta(scroll_delta, Bindings::ScrollBehavior::Instant, Painting::ScrollKind::Absolute);
- CSSPixelPoint new_page_position { CSSPixels(visual_viewport->page_left()), CSSPixels(visual_viewport->page_top()) };
- return new_page_position != page_position
- || navigable.viewport_scroll_offset() != viewport_scroll_offset;
+ auto max_x = m_viewport_size.width().to_double() - visual_viewport->width();
+ auto max_y = m_viewport_size.height().to_double() - visual_viewport->height();
+ auto visual_x = visual_viewport->offset_left();
+ auto visual_y = visual_viewport->offset_top();
+ CSSPixelPoint visual_delta {
+ min(max_x, max(0.0, visual_x + scroll_delta.x().to_double())) - visual_x,
+ min(max_y, max(0.0, visual_y + scroll_delta.y().to_double())) - visual_y,
+ };
+ if (visual_delta.is_zero())
+ return false;
+
+ visual_viewport->scroll_by(visual_delta);
+ queue_scrollend_event(*document, *visual_viewport, {}, ScrollTrigger::UserInput);
+ return true;
}
void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScrollUpdateFreshness freshness)
@@ -5768,13 +5791,24 @@ void LocalNavigable::adopt_pending_async_scroll_offsets(Compositing::AsyncScroll
continue;
}
+ if (async_scroll_offset.stable_node_id.kind == Web::AsyncScrollNodeKind::VisualViewport) {
+ if (async_scroll_offset.stable_node_id.node_id != document->unique_id())
+ continue;
+ if (adopt_async_visual_viewport_scroll_delta(css_scroll_delta)) {
+ adopted_any_scroll_offset = true;
+ dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Main thread adopting async visual viewport delta {},{}",
+ async_scroll_offset.unadopted_scroll_delta.x(), async_scroll_offset.unadopted_scroll_delta.y());
+ }
+ continue;
+ }
+
// The compositor process reports which of its scrolling was relative, since dragging a scrollbar thumb is not.
record_relative_scroll(*document, async_scroll_offset.stable_node_id, async_scroll_offset_to_css_pixels(async_scroll_offset.last_relative_scroll_delta, device_pixels_per_css_pixel));
if (async_scroll_offset.stable_node_id.kind == Web::AsyncScrollNodeKind::Viewport) {
if (async_scroll_offset.stable_node_id.node_id != document->unique_id())
continue;
- if (adopt_async_viewport_scroll_delta(*this, css_scroll_delta)) {
+ if (adopt_async_viewport_scroll_delta(css_scroll_delta)) {
adopted_any_scroll_offset = true;
dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Main thread adopting async viewport delta {},{}",
async_scroll_offset.unadopted_scroll_delta.x(), async_scroll_offset.unadopted_scroll_delta.y());
@@ -7539,27 +7573,35 @@ GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_viewport(CSSPix
else
queue_scrollend_event(*doc, *vv, {}, trigger);
+ auto scroll_promise = perform_a_scroll_of_the_layout_viewport({ layout_dx, layout_dy }, behavior, trigger, relative_displacement, scroll_kind, animation_kind);
+
+ // 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.
+ return scroll_promise;
+}
+
+GC::Ref<WebIDL::Promise> LocalNavigable::perform_a_scroll_of_the_layout_viewport(CSSPixelPoint delta, Bindings::ScrollBehavior behavior, ScrollTrigger trigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind scroll_kind, Compositing::ScrollAnimationKind animation_kind)
+{
+ auto doc = active_document();
+
// NB: Must update layout before accessing paintables.
Layout::ForcedReadScope read { *doc };
doc->update_layout(DOM::UpdateLayoutReason::NavigableViewportScroll);
auto minimum_scroll_offset = Painting::minimum_scroll_offset(*doc->layout_node(read)).to_type<double>();
auto maximum_scroll_offset = Painting::maximum_scroll_offset(*doc->layout_node(read)).to_type<double>();
- auto new_viewport_scroll_offset = m_viewport_scroll_offset.to_type<double>() + Gfx::Point(layout_dx, layout_dy);
+ auto new_viewport_scroll_offset = m_viewport_scroll_offset.to_type<double>() + delta.to_type<double>();
// NOTE: Clamp to the scrolling area.
new_viewport_scroll_offset.set_x(clamp(new_viewport_scroll_offset.x(), minimum_scroll_offset.x(), maximum_scroll_offset.x()));
new_viewport_scroll_offset.set_y(clamp(new_viewport_scroll_offset.y(), minimum_scroll_offset.y(), maximum_scroll_offset.y()));
- auto scroll_promise = perform_a_scroll_of_a_scrolling_box({
- .node_id = doc->unique_id(),
- .kind = Web::AsyncScrollNodeKind::Viewport,
- },
+ TemporaryExecutionContext temporary_execution_context { HTML::relevant_realm(*doc) };
+ return perform_a_scroll_of_a_scrolling_box({
+ .node_id = doc->unique_id(),
+ .kind = Web::AsyncScrollNodeKind::Viewport,
+ },
new_viewport_scroll_offset.to_type<CSSPixels>(), behavior, doc->document_element(), trigger, relative_displacement, DestinationSnapping::SelectSnapPosition, animation_kind, scroll_kind);
-
- // 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.
- return scroll_promise;
}
void LocalNavigable::reset_zoom()
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index da31ae7ff8e..beb6c55f992 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -493,6 +493,9 @@ private:
DestinationIsSnapPosition,
};
GC::Ref<WebIDL::Promise> perform_a_scroll_of_a_scrolling_box(Web::AsyncScrollNodeStableID, CSSPixelPoint position, Bindings::ScrollBehavior, GC::Ptr<DOM::Element> associated_element, ScrollTrigger, Optional<CSSPixelPoint> relative_displacement = {}, DestinationSnapping = DestinationSnapping::SelectSnapPosition, Compositing::ScrollAnimationKind = Compositing::ScrollAnimationKind::SmoothScroll, Painting::ScrollKind = Painting::ScrollKind::Absolute);
+ GC::Ref<WebIDL::Promise> perform_a_scroll_of_the_layout_viewport(CSSPixelPoint delta, Bindings::ScrollBehavior, ScrollTrigger, Optional<CSSPixelPoint> relative_displacement, Painting::ScrollKind, Compositing::ScrollAnimationKind);
+ bool adopt_async_viewport_scroll_delta(CSSPixelPoint);
+ bool adopt_async_visual_viewport_scroll_delta(CSSPixelPoint);
Gfx::FloatPoint device_scroll_offset(CSSPixelPoint) const;
GC::Ptr<WebIDL::Promise> continue_in_flight_user_smooth_scroll(Web::AsyncScrollNodeStableID, CSSPixelPoint position, Compositing::ScrollAnimationKind);
Optional<CSSPixelPoint> scroll_offset_for(Web::AsyncScrollNodeStableID) const;
diff --git a/Libraries/LibWebCommon/Page/AsyncScrollNodeStableID.h b/Libraries/LibWebCommon/Page/AsyncScrollNodeStableID.h
index df09d5a98b8..12e06f4378d 100644
--- a/Libraries/LibWebCommon/Page/AsyncScrollNodeStableID.h
+++ b/Libraries/LibWebCommon/Page/AsyncScrollNodeStableID.h
@@ -20,6 +20,7 @@ enum class AsyncScrollNodeKind : u8 {
Viewport,
Element,
PseudoElement,
+ VisualViewport,
};
// Stable identity for reconciling compositor-side scroll offsets after the paint snapshot has been rebuilt.
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index c66a25df0cf..99fb93eae92 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -36,12 +36,11 @@ static constexpr auto scrollbar_track_press_inverting_modifier = Web::UIEvents::
static constexpr auto scrollbar_track_press_inverting_modifier = Web::UIEvents::Mod_Shift;
#endif
-static Web::AsyncScrollNodeStableID viewport_stable_id_from(Compositing::AsyncScrollNodeID node_id)
+// The offset of the visual viewport in the layout viewport, in device pixels of the content it scales.
+static Gfx::FloatPoint visual_viewport_scroll_offset(Compositing::TransformWithOrigin const& transform)
{
- return {
- .node_id = node_id.document_id,
- .kind = Web::AsyncScrollNodeKind::Viewport,
- };
+ auto scale = transform.matrix[0, 0];
+ return Gfx::FloatPoint { -transform.matrix[0, 3], -transform.matrix[1, 3] }.scaled(1.0f / scale);
}
static void clamp_visual_viewport_transform_to_viewport(Compositing::TransformWithOrigin& transform, Gfx::IntRect viewport_rect)
@@ -1703,21 +1702,9 @@ Optional<ContextState::VisualViewportScrollDelta> ContextState::apply_visual_vie
if (!m_scene.visual_context_tree().has_value())
return {};
- // A page whose viewport cannot be scrolled records no viewport scroll node, and its viewport stays where it is.
- Web::AsyncScrollNodeStableID viewport_stable_id;
- Gfx::FloatPoint viewport_scroll_offset;
- if (auto viewport_node_id = m_async_scroll_tree.viewport_scroll_node_id(); viewport_node_id.has_value()) {
- auto scroll_offset = m_async_scroll_tree.scroll_offset_for_node(*viewport_node_id, m_scroll_state_snapshot);
- if (!scroll_offset.has_value())
- return {};
- viewport_stable_id = viewport_stable_id_from(*viewport_node_id);
- viewport_scroll_offset = *scroll_offset;
- } else if (auto document_id = m_async_scroll_tree.document_id(); document_id.has_value()) {
- viewport_stable_id = { .node_id = *document_id, .kind = Web::AsyncScrollNodeKind::Viewport };
- viewport_scroll_offset = m_async_scrolling_viewport_rect.location().to_type<float>();
- } else {
+ auto document_id = m_async_scroll_tree.document_id();
+ if (!document_id.has_value())
return {};
- }
auto transform = m_scene.visual_viewport_transform();
auto scale = transform.matrix[0, 0];
@@ -1742,8 +1729,8 @@ Optional<ContextState::VisualViewportScrollDelta> ContextState::apply_visual_vie
return VisualViewportScrollDelta {
.scroll_offset = {
- .stable_node_id = viewport_stable_id,
- .compositor_scroll_offset = viewport_scroll_offset,
+ .stable_node_id = { .node_id = document_id.value(), .kind = Web::AsyncScrollNodeKind::VisualViewport },
+ .compositor_scroll_offset = visual_viewport_scroll_offset(transform),
.unadopted_scroll_delta = consumed_delta.scaled(1.0f / scale),
.last_relative_scroll_delta = {},
},
diff --git a/Tests/Compositor/TestContextState.cpp b/Tests/Compositor/TestContextState.cpp
index a854e4fd584..c23312bdbdb 100644
--- a/Tests/Compositor/TestContextState.cpp
+++ b/Tests/Compositor/TestContextState.cpp
@@ -94,12 +94,12 @@ TEST_CASE(wheel_pans_the_zoomed_visual_viewport_of_a_page_without_scroll_nodes)
EXPECT(result.accepted);
EXPECT(result.frame_to_present.has_value());
- // The main thread adopts the pan as a scroll of the document's viewport, at half the delta since the zoom is 2x.
+ // The main thread adopts the pan as a scroll of the document's visual viewport, at half the delta at 2x zoom.
auto updates = context.take_pending_async_scroll_updates();
EXPECT_EQ(updates.document_id, document_id);
EXPECT_EQ(updates.scroll_offsets.size(), 1u);
auto const& scroll_offset = updates.scroll_offsets.first();
EXPECT_EQ(scroll_offset.stable_node_id.node_id, document_id);
- EXPECT_EQ(scroll_offset.stable_node_id.kind, Web::AsyncScrollNodeKind::Viewport);
+ EXPECT_EQ(scroll_offset.stable_node_id.kind, Web::AsyncScrollNodeKind::VisualViewport);
EXPECT_EQ(scroll_offset.unadopted_scroll_delta, Gfx::FloatPoint(0, 25));
}