Skip to content

Truly independent web browser

LibWeb: Follow the scroll range the render clock's ticks laid out

Andreas Kling28dc49651170
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.cpp b/Libraries/LibWeb/Animations/ScrollTimeline.cpp
index 133a1d1c8fa..510c1eaa5e6 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.cpp
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.cpp
@@ -145,6 +145,9 @@ Optional<ScrollTimeline::ScrollerMetrics> ScrollTimeline::scroller_metrics() con
return ScrollerMetrics {
.scroll_node = Painting::async_scroll_node_stable_id(*layout_node),
+ .scroller_style_node = propagated_source.visit(
+ [](GC::Ptr<DOM::Element const> const& element) { return element->style_node_id().value(); },
+ [](GC::Ptr<DOM::Document> const&) -> u32 { return 0; }),
.is_vertical = computed_axis.is_vertical,
.scroll_offset = computed_axis.is_vertical
? Painting::scroll_offset(*layout_node).y().to_double()
@@ -225,7 +228,7 @@ void ScrollTimeline::update_current_time(double)
auto progress = scroll_offset_data->scroll_offset / scroll_offset_data->max_scroll_offset;
if (scroll_offset_data->scroll_node.has_value())
- m_followed_scroller = FollowedScroller { *scroll_offset_data->scroll_node, scroll_offset_data->is_vertical, scroll_offset_data->max_scroll_offset };
+ m_followed_scroller = FollowedScroller { *scroll_offset_data->scroll_node, scroll_offset_data->scroller_style_node, scroll_offset_data->is_vertical, scroll_offset_data->scroll_offset, scroll_offset_data->max_scroll_offset };
set_current_time(TimeValue { TimeValue::Type::Percentage, progress * 100 });
}
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.h b/Libraries/LibWeb/Animations/ScrollTimeline.h
index 546b3c468e1..cda21ef0110 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.h
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.h
@@ -52,11 +52,13 @@ public:
virtual bool is_stale() const;
virtual void update_current_time(double timestamp) override;
- // The scroll node the timeline followed at its last update, and the scroll offset at 100% progress there, in CSS
- // pixels: empty while it was inactive.
+ // The scroll node the timeline followed at its last update, the style node of the element whose box scrolls, or 0
+ // for the viewport, and the scroll offset there and at 100% progress, in CSS pixels: empty while it was inactive.
struct FollowedScroller {
AsyncScrollNodeStableID scroll_node;
+ u32 scroller_style_node { 0 };
bool vertical { true };
+ double scroll_offset { 0 };
double max_scroll_offset { 0 };
};
Optional<FollowedScroller> const& followed_scroller() const { return m_followed_scroller; }
@@ -81,6 +83,7 @@ protected:
// The scroll container the timeline follows, along the timeline's axis, in CSS pixels.
struct ScrollerMetrics {
Optional<AsyncScrollNodeStableID> scroll_node;
+ u32 scroller_style_node { 0 };
bool is_vertical { true };
double scroll_offset { 0 };
double max_scroll_offset { 0 };
diff --git a/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp b/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp
index fedbf9b64bc..5dd9adbb1e8 100644
--- a/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp
@@ -50,9 +50,10 @@ static bool plan_scroll_timeline(Vector<Layout::RustFFI::FfiPlannedScrollTimelin
scroll_timelines.append({
.timeline = timeline.clock_identity(),
.scroller = scroller->scroll_node.node_id.value(),
+ .scroller_element = scroller->scroller_style_node,
.vertical = scroller->vertical,
+ .scroll_offset = scroller->scroll_offset,
.max_scroll_offset = scroller->max_scroll_offset,
- .progress = progress->value,
});
}
return true;
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock.rs b/Libraries/LibWeb/Rust/src/render_state/clock.rs
index 04334576e1f..c183d8d7dad 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock.rs
@@ -40,7 +40,7 @@ use crate::layout::used_values::FfiCssPixelRect;
use crate::layout::{ClockRound, ClockRoundDeclined, HostStyle, LayoutNodeArena, build_keeps_box};
use crate::paint_stage::Presenting;
use crate::painting::paint_passes::{ClockTickVisualContexts, VisualContextsNeedHost, prepare_for_clock_tick};
-use crate::painting::paintable_geometry::absolute_border_box_rect;
+use crate::painting::paintable_geometry::{absolute_border_box_rect, committed_content_size, scrollable_overflow_rect};
use crate::painting::presentation::Presentation;
use crate::painting::record::damage::PaintDamage;
use crate::painting::record::inputs::UnframedRecordingInputs;
@@ -94,11 +94,40 @@ pub struct FfiPlannedScrollTimeline {
/// The unique node id of the element whose scroll node the timeline follows, or of the document for its viewport,
/// and whether along the vertical axis.
pub scroller: i64,
+ /// The style node of the element whose box scrolls, or 0 for the viewport.
+ pub scroller_element: u32,
pub vertical: bool,
- /// The scroll offset at 100% progress, in CSS pixels, in the layout the host sealed the plan from.
+ /// The scroll offset the host laid out, in CSS pixels.
+ pub scroll_offset: f64,
+ /// The scroll offset at 100% progress, in CSS pixels, in the layout the host sealed the plan from, and in the layout
+ /// of the last frame a tick presented from then on.
pub max_scroll_offset: f64,
- /// The progress, in percent, at the scroll offset the host laid out.
- pub progress: f64,
+}
+
+impl FfiPlannedScrollTimeline {
+ /// The progress, in percent, of the timeline at the scroll offset `position`.
+ fn progress_at(&self, position: f64) -> f64 {
+ position / self.max_scroll_offset * 100.0
+ }
+
+ /// Has the timeline follow the layout `arena` holds, as a host's layout decides its next rendering update's samples.
+ /// What makes the timeline inactive there leaves it where it was: the host decides that.
+ fn follow_layout(&mut self, arena: &LayoutNodeArena) {
+ let rows = arena.paintable_rows();
+ let scroller = StyleNodeID::from_raw(self.scroller_element)
+ .map_or_else(|| arena.layout_root(), |element| arena.bound_row(element));
+ let Some(overflow) = scrollable_overflow_rect(&rows, scroller) else {
+ return;
+ };
+ let content = committed_content_size(&rows, scroller);
+ let max_scroll_offset = match self.vertical {
+ true => overflow.height.to_double() - content.height.to_double(),
+ false => overflow.width.to_double() - content.width.to_double(),
+ };
+ if max_scroll_offset > 0.0 {
+ self.max_scroll_offset = max_scroll_offset;
+ }
+ }
}
/// Where the compositor had scrolled the scroll node of an element, or of a document's viewport, by its unique node id,
@@ -139,16 +168,15 @@ impl ClockPlan {
self.scroll_timelines
.iter()
.map(|timeline| {
- let progress = scroll_offsets
+ let position = scroll_offsets
.iter()
.find(|offset| offset.scroller == timeline.scroller)
- .map_or(timeline.progress, |offset| {
- let position = if timeline.vertical { offset.y } else { offset.x };
- position / timeline.max_scroll_offset * 100.0
+ .map_or(timeline.scroll_offset, |offset| {
+ if timeline.vertical { offset.y } else { offset.x }
});
ScrollProgress {
timeline: timeline.timeline,
- progress,
+ progress: timeline.progress_at(position),
}
})
.collect()
@@ -1492,6 +1520,9 @@ impl Lane {
Some((element, generated_for, color))
})
}));
+ for timeline in &mut self.plan.scroll_timelines {
+ timeline.follow_layout(arena);
+ }
Ok(())
}
}
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-follows-the-scroll-range-it-laid-out.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-follows-the-scroll-range-it-laid-out.txt
new file mode 100644
index 00000000000..a07fff9c222
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-follows-the-scroll-range-it-laid-out.txt
@@ -0,0 +1,3 @@
+the ticks grew the page: true
+the ticks settled: true
+the bar shows the progress in the scroll range the ticks laid out: true
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-follows-the-scroll-range-it-laid-out.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-follows-the-scroll-range-it-laid-out.html
new file mode 100644
index 00000000000..ee784b00ff1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-follows-the-scroll-range-it-laid-out.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes grow-page {
+ from { height: 0px; }
+ to { height: 1000px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #bar {
+ height: 10px;
+ animation: widen linear both;
+ animation-timeline: scroll(root);
+ }
+ #spacer {
+ height: 2000px;
+ }
+ #growing {
+ height: 0px;
+ animation: grow-page linear both;
+ animation-timeline: scroll(root);
+ }
+</style>
+<div id="bar"></div>
+<div id="spacer"></div>
+<div id="growing"></div>
+<script>
+ // The boxes a tick lays out decide the scroll range of the next tick's scroll timelines, as a host's layout decides
+ // that of its next rendering update. Otherwise what a tick shows jumps once the host lays out the same page.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => bar.getAnimations()[0], frameTime => {
+ const position = (document.documentElement.scrollHeight - window.innerHeight) / 2;
+ const hostBottom = growing.getBoundingClientRect().bottom;
+ const belowThePage = document.documentElement.scrollHeight - hostBottom;
+ const rows = [];
+ for (let tick = 1; tick <= 40; tick++) {
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ rows.push({
+ barWidth: internals.presentedBorderBox(bar).width,
+ pageBottom: internals.presentedBorderBox(growing).bottom,
+ });
+ }
+ return { position, hostBottom, belowThePage, rows };
+ });
+ const last = result.rows[result.rows.length - 1];
+ const maxScroll = last.pageBottom + result.belowThePage - window.innerHeight;
+ println(`the ticks grew the page: ${last.pageBottom > result.hostBottom}`);
+ println(`the ticks settled: ${Math.abs(last.barWidth - result.rows[result.rows.length - 2].barWidth) < 0.5}`);
+ println(`the bar shows the progress in the scroll range the ticks laid out: ${Math.abs(last.barWidth - (100 + 200 * result.position / maxScroll)) < 0.5}`);
+ done();
+ });
+</script>