Skip to content

Truly independent web browser

LibWeb: Sample view timelines on the render clock

Andreas Kling25a0767f5ac5
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.cpp b/Libraries/LibWeb/Animations/ScrollTimeline.cpp
index 510c1eaa5e6..e9f445d7284 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.cpp
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.cpp
@@ -166,6 +166,13 @@ Optional<ScrollTimeline::ScrollerMetrics> ScrollTimeline::scroller_metrics() con
};
}
+Optional<ScrollTimeline::FollowedScroller> ScrollTimeline::followed_scroller_from(ScrollerMetrics const& metrics)
+{
+ if (!metrics.scroll_node.has_value())
+ return {};
+ return FollowedScroller { *metrics.scroll_node, metrics.scroller_style_node, metrics.is_vertical, metrics.scroll_offset, metrics.max_scroll_offset };
+}
+
void ScrollTimeline::set_axis(ScrollAxis axis)
{
if (m_axis == axis)
@@ -227,8 +234,7 @@ void ScrollTimeline::update_current_time(double)
// scroll offset ÷ (scrollable overflow size − scroll container size)
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->scroller_style_node, scroll_offset_data->is_vertical, scroll_offset_data->scroll_offset, scroll_offset_data->max_scroll_offset };
+ m_followed_scroller = followed_scroller_from(*scroll_offset_data);
set_current_time(TimeValue { TimeValue::Type::Percentage, progress * 100 });
}
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.h b/Libraries/LibWeb/Animations/ScrollTimeline.h
index cda21ef0110..acd056b4591 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.h
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.h
@@ -60,6 +60,8 @@ public:
bool vertical { true };
double scroll_offset { 0 };
double max_scroll_offset { 0 };
+
+ bool operator==(FollowedScroller const&) const = default;
};
Optional<FollowedScroller> const& followed_scroller() const { return m_followed_scroller; }
@@ -97,6 +99,9 @@ protected:
// Empty while the source has no box, is not a scroll container or has no scrollable overflow.
Optional<ScrollerMetrics> scroller_metrics() const;
+ static Optional<FollowedScroller> followed_scroller_from(ScrollerMetrics const&);
+ void set_followed_scroller(Optional<FollowedScroller> followed_scroller) { m_followed_scroller = move(followed_scroller); }
+
private:
Variant<GC::Ptr<DOM::Element const>, GC::Ptr<DOM::Document>> get_propagated_source() const;
diff --git a/Libraries/LibWeb/Animations/ViewTimeline.cpp b/Libraries/LibWeb/Animations/ViewTimeline.cpp
index 00e22e989f9..2deb9ee879e 100644
--- a/Libraries/LibWeb/Animations/ViewTimeline.cpp
+++ b/Libraries/LibWeb/Animations/ViewTimeline.cpp
@@ -175,10 +175,13 @@ Optional<ViewTimeline::Metrics> ViewTimeline::compute_metrics() const
// 100% progress represents the earliest position at which the end border edge of the element’s principal box
// coincides with the start edge of its view progress visibility range.
return Metrics {
+ .followed_scroller = followed_scroller_from(*scroller),
.scroll_offset = scroller->scroll_offset,
.max_scroll_offset = scroller->max_scroll_offset,
- .cover_start = subject_offset - (scroller->scrollport_size - inset_end),
- .cover_end = subject_offset + subject_size - inset_start,
+ .scrollport_size = scroller->scrollport_size,
+ .inset_start = inset_start,
+ .inset_end = inset_end,
+ .subject_offset = subject_offset,
.subject_size = subject_size,
};
}
@@ -192,6 +195,7 @@ bool ViewTimeline::is_stale() const
void ViewTimeline::update_current_time(double)
{
m_last_metrics = compute_metrics();
+ set_followed_scroller({});
if (!m_last_metrics.has_value()) {
set_current_time({});
@@ -203,8 +207,8 @@ void ViewTimeline::update_current_time(double)
// - distance is the current scroll offset minus the scroll offset corresponding to the start of the cover range
// - range is the scroll offset corresponding to the end of the cover range minus the scroll offset corresponding
// to the start of the cover range
- auto distance = m_last_metrics->scroll_offset - m_last_metrics->cover_start;
- auto range = m_last_metrics->cover_end - m_last_metrics->cover_start;
+ auto distance = m_last_metrics->scroll_offset - m_last_metrics->cover_start();
+ auto range = m_last_metrics->cover_end() - m_last_metrics->cover_start();
// If the 0% position and 100% position coincide (i.e. the denominator in the current time formula is zero), the
// timeline is inactive.
@@ -216,6 +220,7 @@ void ViewTimeline::update_current_time(double)
// FIXME: In paged media, view progress timelines that would otherwise reference the document viewport are also
// inactive.
+ set_followed_scroller(m_last_metrics->followed_scroller);
set_current_time(TimeValue { TimeValue::Type::Percentage, distance / range * 100 });
}
@@ -232,29 +237,29 @@ ViewTimeline::ScrollOffsetInterval ViewTimeline::interval_for_range_name(Metrics
case TimelineRangeName::None:
case TimelineRangeName::Cover:
// Represents the full range of the view progress timeline.
- return { metrics.cover_start, metrics.cover_end };
+ return { metrics.cover_start(), metrics.cover_end() };
case TimelineRangeName::Contain:
// Represents the range during which the principal box is either fully contained by, or fully covers, its view
// progress visibility range within the scrollport.
- return sorted(metrics.cover_start + metrics.subject_size, metrics.cover_end - metrics.subject_size);
+ return sorted(metrics.cover_start() + metrics.subject_size, metrics.cover_end() - metrics.subject_size);
case TimelineRangeName::Entry: {
// 0% is equivalent to 0% of the cover range.
// 100% is equivalent to 0% of the contain range.
auto contain = interval_for_range_name(metrics, TimelineRangeName::Contain);
- return sorted(metrics.cover_start, contain.start);
+ return sorted(metrics.cover_start(), contain.start);
}
case TimelineRangeName::Exit: {
// 0% is equivalent to 100% of the contain range.
// 100% is equivalent to 100% of the cover range.
auto contain = interval_for_range_name(metrics, TimelineRangeName::Contain);
- return sorted(contain.end, metrics.cover_end);
+ return sorted(contain.end, metrics.cover_end());
}
case TimelineRangeName::EntryCrossing:
// Represents the range during which the principal box crosses the end border edge.
- return sorted(metrics.cover_start, metrics.cover_start + metrics.subject_size);
+ return sorted(metrics.cover_start(), metrics.cover_start() + metrics.subject_size);
case TimelineRangeName::ExitCrossing:
// Represents the range during which the principal box crosses the start border edge.
- return sorted(metrics.cover_end - metrics.subject_size, metrics.cover_end);
+ return sorted(metrics.cover_end() - metrics.subject_size, metrics.cover_end());
case TimelineRangeName::Scroll:
// Represents the full range of the scroll container on which the view progress timeline is defined.
return { 0, metrics.max_scroll_offset };
@@ -264,7 +269,7 @@ ViewTimeline::ScrollOffsetInterval ViewTimeline::interval_for_range_name(Metrics
AttachmentInterval ViewTimeline::interval_for_attachment_range(AnimationAttachmentRange const& range) const
{
- auto cover_length = m_last_metrics.has_value() ? m_last_metrics->cover_end - m_last_metrics->cover_start : 0;
+ auto cover_length = m_last_metrics.has_value() ? m_last_metrics->cover_end() - m_last_metrics->cover_start() : 0;
if (cover_length == 0)
return ScrollTimeline::interval_for_attachment_range(range);
@@ -273,7 +278,7 @@ AttachmentInterval ViewTimeline::interval_for_attachment_range(AnimationAttachme
auto time_of = [&](TimelineRangeEdge const& edge) {
auto named_range = interval_for_range_name(*m_last_metrics, edge.name);
auto position = named_range.start + edge.offset_along(named_range.end - named_range.start);
- return TimeValue { TimeValue::Type::Percentage, (position - m_last_metrics->cover_start) / cover_length * 100 };
+ return TimeValue { TimeValue::Type::Percentage, (position - m_last_metrics->cover_start()) / cover_length * 100 };
};
return { time_of(range.start), time_of(range.end) };
}
@@ -281,30 +286,30 @@ AttachmentInterval ViewTimeline::interval_for_attachment_range(AnimationAttachme
// NB: The timeline times at the minimum and the maximum scroll offsets, which need not be 0% and 100%.
Optional<TimeValue> ViewTimeline::minimum_timeline_time() const
{
- if (!m_last_metrics.has_value() || m_last_metrics->cover_end == m_last_metrics->cover_start)
+ if (!m_last_metrics.has_value() || m_last_metrics->cover_end() == m_last_metrics->cover_start())
return {};
- return TimeValue { TimeValue::Type::Percentage, (0 - m_last_metrics->cover_start) / (m_last_metrics->cover_end - m_last_metrics->cover_start) * 100 };
+ return TimeValue { TimeValue::Type::Percentage, (0 - m_last_metrics->cover_start()) / (m_last_metrics->cover_end() - m_last_metrics->cover_start()) * 100 };
}
Optional<TimeValue> ViewTimeline::maximum_timeline_time() const
{
- if (!m_last_metrics.has_value() || m_last_metrics->cover_end == m_last_metrics->cover_start)
+ if (!m_last_metrics.has_value() || m_last_metrics->cover_end() == m_last_metrics->cover_start())
return {};
- return TimeValue { TimeValue::Type::Percentage, (m_last_metrics->max_scroll_offset - m_last_metrics->cover_start) / (m_last_metrics->cover_end - m_last_metrics->cover_start) * 100 };
+ return TimeValue { TimeValue::Type::Percentage, (m_last_metrics->max_scroll_offset - m_last_metrics->cover_start()) / (m_last_metrics->cover_end() - m_last_metrics->cover_start()) * 100 };
}
GC::Ref<CSS::CSSNumericValue> ViewTimeline::start_offset() const
{
// Represents the starting (0% progress) scroll position of the view progress timeline as a length offset (in px)
// from the scroll origin.
- return CSS::CSSUnitValue::create(m_last_metrics.has_value() ? m_last_metrics->cover_start : 0, "px"_utf16_fly_string);
+ return CSS::CSSUnitValue::create(m_last_metrics.has_value() ? m_last_metrics->cover_start() : 0, "px"_utf16_fly_string);
}
GC::Ref<CSS::CSSNumericValue> ViewTimeline::end_offset() const
{
// Represents the ending (100% progress) scroll position of the view progress timeline as a length offset (in px)
// from the scroll origin.
- return CSS::CSSUnitValue::create(m_last_metrics.has_value() ? m_last_metrics->cover_end : 0, "px"_utf16_fly_string);
+ return CSS::CSSUnitValue::create(m_last_metrics.has_value() ? m_last_metrics->cover_end() : 0, "px"_utf16_fly_string);
}
}
diff --git a/Libraries/LibWeb/Animations/ViewTimeline.h b/Libraries/LibWeb/Animations/ViewTimeline.h
index 5cfed257180..df30e8a54fd 100644
--- a/Libraries/LibWeb/Animations/ViewTimeline.h
+++ b/Libraries/LibWeb/Animations/ViewTimeline.h
@@ -45,6 +45,27 @@ public:
virtual Optional<TimeValue> minimum_timeline_time() const override;
virtual Optional<TimeValue> maximum_timeline_time() const override;
+ // What decides the progress of the timeline, along its axis, in CSS pixels.
+ struct Metrics {
+ Optional<FollowedScroller> followed_scroller;
+ double scroll_offset { 0 };
+ double max_scroll_offset { 0 };
+ double scrollport_size { 0 };
+ // The insets of the view progress visibility range, at the start and at the end of the scrollport.
+ double inset_start { 0 };
+ double inset_end { 0 };
+ // Where the subject's border box starts, from the start of the scrollport, and its size.
+ double subject_offset { 0 };
+ double subject_size { 0 };
+ // The start and the end of the cover range.
+ double cover_start() const { return subject_offset - (scrollport_size - inset_end); }
+ double cover_end() const { return subject_offset + subject_size - inset_start; }
+
+ bool operator==(Metrics const&) const = default;
+ };
+ // What the timeline's progress was last calculated from: empty while it was inactive.
+ Optional<Metrics> const& last_metrics() const { return m_last_metrics; }
+
private:
// The scroll offsets at the start and at the end of a named timeline range, in CSS pixels.
struct ScrollOffsetInterval {
@@ -55,17 +76,6 @@ private:
ViewTimeline(DOM::Document&, DOM::AbstractElement subject, ScrollAxis, Inset);
virtual ~ViewTimeline() override = default;
- // What decides the progress of the timeline, along its axis, in CSS pixels.
- struct Metrics {
- double scroll_offset { 0 };
- double max_scroll_offset { 0 };
- // The start and the end of the cover range.
- double cover_start { 0 };
- double cover_end { 0 };
- double subject_size { 0 };
-
- bool operator==(Metrics const&) const = default;
- };
Optional<Metrics> compute_metrics() const;
ScrollOffsetInterval interval_for_range_name(Metrics const&, TimelineRangeName) const;
diff --git a/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp b/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp
index 5dd9adbb1e8..960eb8a7403 100644
--- a/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/ClockPlan.cpp
@@ -10,6 +10,7 @@
#include <LibWeb/Animations/DocumentTimeline.h>
#include <LibWeb/Animations/KeyframeEffect.h>
#include <LibWeb/Animations/ScrollTimeline.h>
+#include <LibWeb/Animations/ViewTimeline.h>
#include <LibWeb/CSS/CSSTransition.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleEngineBridge.h>
@@ -36,8 +37,20 @@ static bool timing_is_in(Animations::KeyframeEffect const& effect, Animations::T
return effect.start_delay().type == type && effect.iteration_duration().type == type && effect.active_duration().type == type;
}
-// Adds the scroll timeline `timeline` that `effect` runs on, at `local_time`, to `scroll_timelines`, whose last entry is
-// the timeline's where an effect of it was planned before. Answers whether a tick may sample it.
+// How much of `edge` is a percentage of the scrollport's size, which a tick that resizes the scrollport resolves again.
+static double inset_percentage(Animations::ViewTimeline::InsetEdge const& edge)
+{
+ return edge.is_auto ? 0 : edge.percentage;
+}
+
+// Whether `edge` is a length plus a percentage, which is all a tick that resizes the scrollport can resolve again.
+static bool inset_is_linear(Animations::ViewTimeline::InsetEdge const& edge)
+{
+ return !edge.calculated || CSS::ComputedValuesFFI::rust_length_percentage_is_linear(edge.calculated.data());
+}
+
+// Adds the scroll timeline `timeline` that `effect` runs on, at `local_time`, to `scroll_timelines`, whose last entry
+// is the timeline's where an effect of it was planned before. Answers whether a tick may sample it.
static bool plan_scroll_timeline(Vector<Layout::RustFFI::FfiPlannedScrollTimeline>& scroll_timelines, Animations::ScrollTimeline const& timeline, Animations::KeyframeEffect const& effect, Optional<Animations::TimeValue> local_time)
{
auto const& scroller = timeline.followed_scroller();
@@ -46,6 +59,13 @@ static bool plan_scroll_timeline(Vector<Layout::RustFFI::FfiPlannedScrollTimelin
|| !local_time.has_value() || local_time->type != Animations::TimeValue::Type::Percentage
|| !timing_is_in(effect, Animations::TimeValue::Type::Percentage))
return false;
+ // A tick finds the subject of a view timeline by its box, which a pseudo-element does not name.
+ auto const* view_timeline = as_if<Animations::ViewTimeline>(timeline);
+ if (view_timeline && (view_timeline->subject_internal().pseudo_element().has_value() || !view_timeline->subject_internal().element().style_node_id() || !view_timeline->last_metrics().has_value()))
+ return false;
+ // A tick resolves the insets of a view timeline as a length plus a percentage, which not every calculation is.
+ if (view_timeline && (!inset_is_linear(view_timeline->inset().start) || !inset_is_linear(view_timeline->inset().end)))
+ return false;
if (scroll_timelines.is_empty() || scroll_timelines.last().timeline != timeline.clock_identity()) {
scroll_timelines.append({
.timeline = timeline.clock_identity(),
@@ -54,6 +74,18 @@ static bool plan_scroll_timeline(Vector<Layout::RustFFI::FfiPlannedScrollTimelin
.vertical = scroller->vertical,
.scroll_offset = scroller->scroll_offset,
.max_scroll_offset = scroller->max_scroll_offset,
+ .view = view_timeline
+ ? Layout::RustFFI::FfiPlannedViewTimeline {
+ .subject = view_timeline->subject_internal().element().style_node_id().value(),
+ .scrollport_size = view_timeline->last_metrics()->scrollport_size,
+ .inset_start = view_timeline->last_metrics()->inset_start,
+ .inset_end = view_timeline->last_metrics()->inset_end,
+ .inset_start_percentage = inset_percentage(view_timeline->inset().start),
+ .inset_end_percentage = inset_percentage(view_timeline->inset().end),
+ .subject_offset = view_timeline->last_metrics()->subject_offset,
+ .subject_size = view_timeline->last_metrics()->subject_size,
+ }
+ : Layout::RustFFI::FfiPlannedViewTimeline {},
});
}
return true;
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 24924fa2bc1..4e7f8fcdd68 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -2408,12 +2408,23 @@ void Internals::reset_rendering_scheduler_counters()
HTML::main_thread_event_loop().reset_rendering_scheduler_counters();
}
+static void inject_clock_tick_at_scroll_offset(DOM::Document& document, double frame_time_ms, Layout::RustFFI::FfiScrollOffset const* scroll_offset)
+{
+ auto frame_time = document.relevant_settings_object().time_origin() + frame_time_ms;
+ Layout::RustFFI::document_host_inject_clock_tick(document.layout_node_arena().host(), static_cast<i64>(frame_time * 1'000'000.0), scroll_offset);
+}
+
void Internals::inject_clock_tick(double frame_time_ms, Optional<double> viewport_scroll_y)
{
auto& document = window().associated_document();
- auto frame_time = document.relevant_settings_object().time_origin() + frame_time_ms;
auto scroll_offset = viewport_scroll_y.map([&](double y) { return Layout::RustFFI::FfiScrollOffset { document.unique_id().value(), 0, y }; });
- Layout::RustFFI::document_host_inject_clock_tick(document.layout_node_arena().host(), static_cast<i64>(frame_time * 1'000'000.0), scroll_offset.has_value() ? &*scroll_offset : nullptr);
+ inject_clock_tick_at_scroll_offset(document, frame_time_ms, scroll_offset.has_value() ? &*scroll_offset : nullptr);
+}
+
+void Internals::inject_clock_tick_scrolling_element(double frame_time_ms, DOM::Element& scroller, double x, double y)
+{
+ Layout::RustFFI::FfiScrollOffset scroll_offset { scroller.unique_id().value(), x, y };
+ inject_clock_tick_at_scroll_offset(window().associated_document(), frame_time_ms, &scroll_offset);
}
void Internals::inject_hover_pointer(double x, double y, Optional<double> frame_time_ms, Optional<u64> input_event_id)
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 1b035e3ba41..6d86656f4be 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -291,6 +291,7 @@ public:
void hold_next_frame(Utf16String const& hold);
Utf16String last_frame_presented_by(DOM::Document&);
void inject_clock_tick(double frame_time_ms, Optional<double> viewport_scroll_y);
+ void inject_clock_tick_scrolling_element(double frame_time_ms, DOM::Element& scroller, double x, double y);
void inject_hover_pointer(double x, double y, Optional<double> frame_time_ms, Optional<u64> input_event_id);
u64 queue_mouse_move(double x, double y, u32 buttons);
void move_hover_pointer(double x, double y, Optional<u64> input_event_id);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 506622b9ab9..55778e3f747 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -305,6 +305,8 @@ interface Internals {
// Hands the clock lanes of the current document a tick at the given time, and waits until it has run.
// With a viewport scroll offset, the compositor had scrolled the viewport down to it at the tick.
undefined injectClockTick(double frameTimeMs, optional double viewportScrollY);
+ // As injectClockTick(), with the compositor having scrolled the given element to the given offset, in CSS pixels.
+ undefined injectClockTickScrollingElement(double frameTimeMs, Element scroller, double x, double y);
// Hands the clock lanes of the current document a pointer move to the given point in CSS pixels as the compositor
// would, beside the mouse event with the given input event id, if any, and waits until the tick that hovers what is
// under it has run, at the given frame time, or now.
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rs
index a3b339ace4d..474f21edead 100644
--- a/Libraries/LibWeb/Rust/src/css/calc.rs
+++ b/Libraries/LibWeb/Rust/src/css/calc.rs
@@ -864,6 +864,24 @@ impl CalcNode {
});
found
}
+
+ /// Whether the node resolves to a length plus a percentage of whatever its percentages are of, as `calc(1% + 1px)`
+ /// does and `min(50%, 10px)` does not.
+ pub(crate) fn is_linear_in_percentages(&self) -> bool {
+ if !self.contains_percentage() {
+ return true;
+ }
+ match self {
+ CalcNode::Numeric(_) => true,
+ CalcNode::Negate(child) => child.is_linear_in_percentages(),
+ CalcNode::Sum(children) => children.iter().all(|child| child.is_linear_in_percentages()),
+ CalcNode::Product(children) => {
+ children.iter().filter(|child| child.contains_percentage()).count() == 1
+ && children.iter().all(|child| child.is_linear_in_percentages())
+ }
+ _ => false,
+ }
+ }
}
#[cfg(test)]
@@ -871,6 +889,23 @@ impl CalcNode {
mod tests {
use super::*;
+ #[test]
+ fn is_linear_in_percentages_looks_at_what_holds_a_percentage() {
+ let percent = Arc::new(CalcNode::Numeric(CalcNumericValue::Percentage(50.0)));
+ let number = Arc::new(CalcNode::Numeric(CalcNumericValue::Number {
+ value: 2.0,
+ number_type: 0,
+ }));
+ let sum = Arc::new(CalcNode::Sum(vec![number.clone(), percent.clone()]));
+ assert!(sum.is_linear_in_percentages());
+ assert!(CalcNode::Negate(sum.clone()).is_linear_in_percentages());
+ assert!(CalcNode::Product(vec![number.clone(), sum.clone()]).is_linear_in_percentages());
+ assert!(!CalcNode::Product(vec![percent.clone(), sum]).is_linear_in_percentages());
+ assert!(!CalcNode::Min(vec![number.clone(), percent.clone()]).is_linear_in_percentages());
+ assert!(!CalcNode::Sum(vec![number.clone(), Arc::new(CalcNode::Invert(percent))]).is_linear_in_percentages());
+ assert!(CalcNode::Min(vec![number.clone(), number]).is_linear_in_percentages());
+ }
+
#[test]
fn contains_percentage_walks_the_tree() {
let percent = Arc::new(CalcNode::Numeric(CalcNumericValue::Percentage(50.0)));
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 4ea4c579ec0..c1bdc3dc6f1 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -6381,6 +6381,16 @@ pub extern "C" fn rust_length_percentage_px(value: &StyleValueData, basis: f64)
.to_double()
}
+/// Whether a computed length-percentage is a length plus a percentage of what it resolves against, which only a
+/// calculation may not be.
+#[unsafe(no_mangle)]
+pub extern "C" fn rust_length_percentage_is_linear(value: &StyleValueData) -> bool {
+ match value {
+ StyleValueData::Calculated(calculated) => calculated.rust_calculation.node().is_linear_in_percentages(),
+ _ => true,
+ }
+}
+
/// A length-percentage computed value as a length plus a percentage of what it resolves against.
fn length_plus_percentage(value: &StyleValueData) -> (f64, f64) {
match value {
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock.rs b/Libraries/LibWeb/Rust/src/render_state/clock.rs
index c183d8d7dad..5593a28821c 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock.rs
@@ -40,7 +40,9 @@ 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, committed_content_size, scrollable_overflow_rect};
+use crate::painting::paintable_geometry::{
+ absolute_border_box_rect, absolute_padding_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;
@@ -99,18 +101,83 @@ pub struct FfiPlannedScrollTimeline {
pub vertical: bool,
/// 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.
+ /// 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,
+ pub view: FfiPlannedViewTimeline,
+}
+
+/// What decides the progress of a view progress timeline, along its axis, in CSS pixels, in the layout the host sealed
+/// the plan from, and where a tick's layout moved them in the layout of the last frame a tick presented from then on.
+#[repr(C)]
+#[derive(Clone, Copy, Debug, Default)]
+pub struct FfiPlannedViewTimeline {
+ /// The style node of the timeline's subject, or 0 for a scroll progress timeline, which has none of this.
+ pub subject: u32,
+ pub scrollport_size: f64,
+ /// The insets of the view progress visibility range, at the start and at the end of the scrollport.
+ pub inset_start: f64,
+ pub inset_end: f64,
+ /// How much of those insets is a percentage of the scrollport's size. Scroll padding counts as none, whose pixels
+ /// hold until the host's next plan.
+ pub inset_start_percentage: f64,
+ pub inset_end_percentage: f64,
+ /// Where the subject's border box starts, from the start of the scrollport, and its size.
+ pub subject_offset: f64,
+ pub subject_size: f64,
+}
+
+impl FfiPlannedViewTimeline {
+ /// The scroll offsets at the start and at the end of the cover range.
+ fn cover_range(&self) -> (f64, f64) {
+ // https://drafts.csswg.org/scroll-animations-1/#valdef-animation-timeline-range-cover
+ // 0% progress represents the latest position at which the start border edge of the element's principal box
+ // coincides with the end edge of its view progress visibility range.
+ // 100% progress represents the earliest position at which the end border edge of the element's principal box
+ // coincides with the start edge of its view progress visibility range.
+ (
+ self.subject_offset - (self.scrollport_size - self.inset_end),
+ self.subject_offset + self.subject_size - self.inset_start,
+ )
+ }
+
+ /// https://drafts.csswg.org/scroll-animations-1/#view-timeline-progress
+ /// The progress, in percent, at the scroll offset `position`.
+ /// NB: The 0% and 100% positions never coincide: the host plans no such timeline, and `follow_layout` leaves one
+ /// where they would.
+ fn progress_at(&self, position: f64) -> f64 {
+ let (cover_start, cover_end) = self.cover_range();
+ (position - cover_start) / (cover_end - cover_start) * 100.0
+ }
}
impl FfiPlannedScrollTimeline {
+ /// Where `scroll_offsets` scrolled the timeline's scroller to, or where the host laid it out without one for it, up
+ /// to the end of the scroll range: a tick's layout may have shrunk it before the compositor hears of that.
+ /// NB: A scroller that overflows towards its start, as with `direction: rtl`, scrolls to negative offsets.
+ fn position_in(&self, scroll_offsets: &[FfiScrollOffset]) -> f64 {
+ let position = scroll_offsets
+ .iter()
+ .find(|offset| offset.scroller == self.scroller)
+ .map_or(
+ self.scroll_offset,
+ |offset| {
+ if self.vertical { offset.y } else { offset.x }
+ },
+ );
+ position.min(self.max_scroll_offset)
+ }
+
/// 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
+ if self.view.subject == 0 {
+ return position / self.max_scroll_offset * 100.0;
+ }
+ self.view.progress_at(position)
}
- /// Has the timeline follow the layout `arena` holds, as a host's layout decides its next rendering update's samples.
+ /// 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();
@@ -124,8 +191,33 @@ impl FfiPlannedScrollTimeline {
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;
+ if max_scroll_offset <= 0.0 {
+ return;
+ }
+ self.max_scroll_offset = max_scroll_offset;
+ let Some(subject) = StyleNodeID::from_raw(self.view.subject) else {
+ return;
+ };
+ let subject = arena.bound_row(subject);
+ if !rows.paintable_row_is_populated(subject) {
+ return;
+ }
+ let along = |rect: CssPixelRect| match self.vertical {
+ true => (rect.y.to_double(), rect.height.to_double()),
+ false => (rect.x.to_double(), rect.width.to_double()),
+ };
+ let mut view = self.view;
+ let (scrollport_start, scrollport_size) = along(absolute_padding_box_rect(&rows, scroller));
+ let (subject_start, subject_size) = along(absolute_border_box_rect(&rows, subject));
+ let resized_by = scrollport_size - view.scrollport_size;
+ view.scrollport_size = scrollport_size;
+ view.inset_start += view.inset_start_percentage / 100.0 * resized_by;
+ view.inset_end += view.inset_end_percentage / 100.0 * resized_by;
+ view.subject_offset = subject_start - scrollport_start;
+ view.subject_size = subject_size;
+ let (cover_start, cover_end) = view.cover_range();
+ if cover_start != cover_end {
+ self.view = view;
}
}
}
@@ -145,11 +237,10 @@ impl ClockPlan {
elements: Vec<StyleNodeID>,
time_origin: f64,
last_end: f64,
- scroll_timelines: Vec<FfiPlannedScrollTimeline>,
+ mut scroll_timelines: Vec<FfiPlannedScrollTimeline>,
round: ClockRound,
hover: Option<hover::HoverPlan>,
) -> Self {
- let mut scroll_timelines = scroll_timelines;
scroll_timelines.sort_unstable_by_key(|timeline| timeline.timeline);
Self {
elements,
@@ -162,22 +253,24 @@ impl ClockPlan {
}
}
- /// The progress of the scroll timelines, sorted by timeline, where `scroll_offsets` scrolled their scrollers to, or
- /// where the host laid out those it has none for.
- fn scroll_progress(&self, scroll_offsets: &[FfiScrollOffset]) -> SmallVec<[ScrollProgress; 2]> {
+ /// Where `scroll_offsets` scrolled the scrollers of the scroll timelines to, or where the host laid out those it
+ /// has none for.
+ fn scroll_positions(&self, scroll_offsets: &[FfiScrollOffset]) -> SmallVec<[f64; 2]> {
self.scroll_timelines
.iter()
- .map(|timeline| {
- let position = scroll_offsets
- .iter()
- .find(|offset| offset.scroller == timeline.scroller)
- .map_or(timeline.scroll_offset, |offset| {
- if timeline.vertical { offset.y } else { offset.x }
- });
- ScrollProgress {
- timeline: timeline.timeline,
- progress: timeline.progress_at(position),
- }
+ .map(|timeline| timeline.position_in(scroll_offsets))
+ .collect()
+ }
+
+ /// The progress of the scroll timelines, sorted by timeline, with their scrollers at `positions`, as
+ /// `scroll_positions` gives them.
+ fn scroll_progress(&self, positions: &[f64]) -> SmallVec<[ScrollProgress; 2]> {
+ self.scroll_timelines
+ .iter()
+ .zip(positions)
+ .map(|(timeline, &position)| ScrollProgress {
+ timeline: timeline.timeline,
+ progress: timeline.progress_at(position),
})
.collect()
}
@@ -198,6 +291,17 @@ impl ClockPlan {
}
}
+/// How many frames after a scroll a tick's layout decides the next tick's scroll timelines, as a host's layout decides
+/// them in its next rendering update. Boxes an animation resizes may lay out so that no layout agrees with the progress
+/// it decides, as where text wraps at a width the progress crosses, and a host goes back and forth between the two with
+/// a rendering update's two rounds of layout showing only one of them. So the timelines stop following once the scroll
+/// stops, and hold still until the next scroll or the host's next plan, which is as far as the ticks settle them.
+const SETTLING_FRAMES_AFTER_A_SCROLL: u8 = 3;
+
+/// How far a scroller moves, in CSS pixels, before it counts as scrolled, past the difference between the fixed point
+/// offsets the host lays out and those the compositor holds.
+const SCROLL_POSITION_TOLERANCE: f64 = 0.05;
+
/// The lane of a frame of a document the host presented, on the render owner: the fork of the render state its ticks
/// write, the recording of the frame its last tick presented, which brings back the recorder state and presentation it
/// records and presents with, and what its ticks showed.
@@ -232,6 +336,10 @@ pub(crate) struct Lane {
/// infinity for the lane's frame, and the progress at which it shows the plan's scroll timelines.
shown_at: f64,
shown_scroll_progress: SmallVec<[ScrollProgress; 2]>,
+ /// Where the scrollers of the plan's scroll timelines were at the latest tick.
+ scroll_positions: SmallVec<[f64; 2]>,
+ /// How many more frames the plan's scroll timelines follow the layout of, since a scroll moved them.
+ settling_frames: u8,
/// Whether a tick found the lane could sample no more: past the animations' ends, or something only the host
/// computes.
parked: bool,
@@ -1124,7 +1232,8 @@ impl Drop for Lane {
impl Lane {
/// A lane whose ticks record and present with `recorder`, follow `plan`, and write what `state` says.
fn new(recorder: ClockRecorder, plan: ClockPlan, state: LaneState) -> Self {
- let shown_scroll_progress = plan.scroll_progress(&[]);
+ let scroll_positions = plan.scroll_positions(&[]);
+ let shown_scroll_progress = plan.scroll_progress(&scroll_positions);
let parked = !plan.animates();
Self {
recording: TickRecording(Riding::landed(recorder)),
@@ -1141,6 +1250,8 @@ impl Lane {
scroll_offsets: Vec::new(),
shown_at: f64::NEG_INFINITY,
shown_scroll_progress,
+ scroll_positions,
+ settling_frames: 0,
parked,
frame_left_to_host: false,
hovered: hover::LaneHover::default(),
@@ -1174,7 +1285,9 @@ impl Lane {
/// one it followed.
fn replan(&mut self, plan: ClockPlan) {
self.parked = !plan.animates();
- self.shown_scroll_progress = plan.scroll_progress(&[]);
+ self.scroll_positions = plan.scroll_positions(&[]);
+ self.shown_scroll_progress = plan.scroll_progress(&self.scroll_positions);
+ self.settling_frames = 0;
self.shown_at = f64::NEG_INFINITY;
self.replan_host_effects(&plan.elements);
self.plan = plan;
@@ -1339,8 +1452,18 @@ impl Lane {
/// The progress of the scroll timelines a tick samples the plan's animations at, or none where a frame that showed
/// the document timeline's animations ended shows them already and nothing scrolled since. The ticks go on past the
/// events of the animations, which the host sends as it gets to them.
- fn sample_progress(&self) -> Option<SmallVec<[ScrollProgress; 2]>> {
- let scroll_progress = self.plan.scroll_progress(&self.scroll_offsets);
+ fn sample_progress(&mut self) -> Option<SmallVec<[ScrollProgress; 2]>> {
+ let positions = self.plan.scroll_positions(&self.scroll_offsets);
+ let scrolled = positions
+ .iter()
+ .zip(&self.scroll_positions)
+ .any(|(position, last)| (position - last).abs() > SCROLL_POSITION_TOLERANCE);
+ // NB: What a slow scroll moves per frame adds up to a scroll, so the positions stay until it does.
+ if scrolled {
+ self.settling_frames = SETTLING_FRAMES_AFTER_A_SCROLL;
+ self.scroll_positions = positions.clone();
+ }
+ let scroll_progress = self.plan.scroll_progress(&positions);
if self.shown_at >= self.plan.last_end && scroll_progress == self.shown_scroll_progress {
return None;
}
@@ -1520,8 +1643,11 @@ impl Lane {
Some((element, generated_for, color))
})
}));
- for timeline in &mut self.plan.scroll_timelines {
- timeline.follow_layout(arena);
+ if self.settling_frames > 0 {
+ self.settling_frames -= 1;
+ 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-a-view-timeline-subject-it-laid-out.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-follows-a-view-timeline-subject-it-laid-out.txt
new file mode 100644
index 00000000000..8d0a77ac4c1
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-follows-a-view-timeline-subject-it-laid-out.txt
@@ -0,0 +1,2 @@
+the ticks moved the subject up: true
+the subject shows the progress where the ticks laid it out: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-holds-a-view-timeline-with-a-nonlinear-inset.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-holds-a-view-timeline-with-a-nonlinear-inset.txt
new file mode 100644
index 00000000000..48594623c47
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-holds-a-view-timeline-with-a-nonlinear-inset.txt
@@ -0,0 +1,3 @@
+the ticks resized the scrollport: true
+the ticks present nothing of the subject: true
+the host computes that progress in its own scrollport: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-keeps-the-scroll-position-in-the-range-it-laid-out.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-keeps-the-scroll-position-in-the-range-it-laid-out.txt
new file mode 100644
index 00000000000..f84ab3a2b77
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-keeps-the-scroll-position-in-the-range-it-laid-out.txt
@@ -0,0 +1,2 @@
+the ticks shrank the page: true
+the bar stays at the end of its animation: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-resolves-a-calculated-view-timeline-inset-in-a-resized-scrollport.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-resolves-a-calculated-view-timeline-inset-in-a-resized-scrollport.txt
new file mode 100644
index 00000000000..8f43b88054d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-resolves-a-calculated-view-timeline-inset-in-a-resized-scrollport.txt
@@ -0,0 +1,3 @@
+the ticks resized the scrollport: true
+the subject shows the progress with its insets in that scrollport: true
+the host computes that progress in its own scrollport: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-resolves-a-view-timeline-inset-in-a-resized-scrollport.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-resolves-a-view-timeline-inset-in-a-resized-scrollport.txt
new file mode 100644
index 00000000000..38bbfc0ff5c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-resolves-a-view-timeline-inset-in-a-resized-scrollport.txt
@@ -0,0 +1,2 @@
+the ticks resized the scrollport: true
+the subject shows the progress with its insets in that scrollport: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline-at-a-negative-scroll-offset.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline-at-a-negative-scroll-offset.txt
new file mode 100644
index 00000000000..7bf98b86e04
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline-at-a-negative-scroll-offset.txt
@@ -0,0 +1 @@
+the subject shows the progress at the negative scroll offset: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline.txt
index a4ef65d3aa8..78b7b40c46e 100644
--- a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline.txt
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-a-view-timeline.txt
@@ -1,7 +1,7 @@
-presented width a quarter through the cover range: none
-presented width half through the cover range: none
-presented width past the cover range: none
-presented width scrolled back to half: none
+presented width a quarter through the cover range: 150.0
+presented width half through the cover range: 200.0
+presented width past the cover range: 300.0
+presented width scrolled back to half: 200.0
render clock: ticking
last frame presented by: clock
the page holds at the top: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-view-timelines-of-element-scrollers.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-view-timelines-of-element-scrollers.txt
new file mode 100644
index 00000000000..b6253a39ff6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-samples-view-timelines-of-element-scrollers.txt
@@ -0,0 +1,6 @@
+verticalMoved: true
+verticalMatches: true
+verticalRangeMatches: true
+horizontalMoved: true
+horizontalMatches: true
+horizontalRangeMatches: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-settles-a-slow-scroll.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-settles-a-slow-scroll.txt
new file mode 100644
index 00000000000..d14dc7102d7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-settles-a-slow-scroll.txt
@@ -0,0 +1,2 @@
+the scroll moved the subject: true
+the subject shows the progress where the scroll moved it: true
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-settles-where-a-view-timeline-feeds-back.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-settles-where-a-view-timeline-feeds-back.txt
new file mode 100644
index 00000000000..36f2b4f419a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/clock-tick-settles-where-a-view-timeline-feeds-back.txt
@@ -0,0 +1,3 @@
+the ticks tried both layouts: true
+the ticks settled on one: true
+render clock: ticking
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-follows-a-view-timeline-subject-it-laid-out.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-follows-a-view-timeline-subject-it-laid-out.html
new file mode 100644
index 00000000000..73dd90784c9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-follows-a-view-timeline-subject-it-laid-out.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes shrink {
+ from { height: 400px; }
+ to { height: 0px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #shrinking {
+ height: 400px;
+ animation: shrink linear both;
+ animation-timeline: scroll(root);
+ }
+ #subject {
+ height: 100px;
+ animation: widen linear both;
+ animation-timeline: view();
+ }
+ #spacer {
+ height: 2000px;
+ }
+</style>
+<div id="shrinking"></div>
+<div id="subject"></div>
+<div id="spacer"></div>
+<script>
+ // Where a tick lays out the subject of a view timeline decides the timeline's progress at the next tick, as a host's
+ // layout decides it at its next rendering update.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ const hostTop = subject.getBoundingClientRect().top;
+ const position = 200;
+ let box;
+ for (let tick = 1; tick <= 40; tick++) {
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ box = internals.presentedBorderBox(subject);
+ }
+ return { hostTop, position, top: box.top, width: box.width };
+ });
+ const coverStart = result.top - window.innerHeight;
+ const coverEnd = result.top + 100;
+ const progress = (result.position - coverStart) / (coverEnd - coverStart);
+ println(`the ticks moved the subject up: ${result.top < result.hostTop}`);
+ println(`the subject shows the progress where the ticks laid it out: ${Math.abs(result.width - (100 + 200 * progress)) < 0.5}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-holds-a-view-timeline-with-a-nonlinear-inset.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-holds-a-view-timeline-with-a-nonlinear-inset.html
new file mode 100644
index 00000000000..1f15b35f27e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-holds-a-view-timeline-with-a-nonlinear-inset.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes resize {
+ from { height: 100px; }
+ to { height: 300px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #scroller {
+ height: 100px;
+ width: 400px;
+ overflow: auto;
+ animation: resize linear both;
+ animation-timeline: scroll(root);
+ }
+ #before {
+ height: 50px;
+ }
+ #subject {
+ height: 50px;
+ animation: widen linear both;
+ animation-timeline: view(min(5%, 10px));
+ }
+ #after {
+ height: 1000px;
+ }
+ #spacer {
+ height: 2000px;
+ }
+</style>
+<div id="scroller">
+ <div id="before"></div>
+ <div id="subject"></div>
+ <div id="after"></div>
+</div>
+<div id="spacer"></div>
+<script>
+ // A tick resolves a view timeline's inset as a length plus a percentage of the scrollport, which min(5%, 10px) is
+ // not, so the ticks present nothing of the subject of that timeline, and the host answers in its next rendering
+ // update.
+ const inset = size => Math.min(0.05 * size, 10);
+ const widthAt = (position, size) => {
+ const coverStart = 50 - (size - inset(size));
+ const coverEnd = 50 + 50 - inset(size);
+ return 100 + 200 * (position - coverStart) / (coverEnd - coverStart);
+ };
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ const position = document.documentElement.scrollHeight - window.innerHeight;
+ for (let tick = 1; tick <= 8; tick++)
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ return {
+ size: internals.presentedBorderBox(scroller).height,
+ subject: internals.presentedBorderBox(subject),
+ };
+ });
+ println(`the ticks resized the scrollport: ${result.size > 100}`);
+ println(`the ticks present nothing of the subject: ${result.subject === null}`);
+ window.scrollTo(0, document.documentElement.scrollHeight);
+ await twoFrames();
+ await twoFrames();
+ const size = scroller.getBoundingClientRect().height;
+ println(`the host computes that progress in its own scrollport: ${Math.abs(subject.getBoundingClientRect().width - widthAt(0, size)) < 0.5}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-keeps-the-scroll-position-in-the-range-it-laid-out.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-keeps-the-scroll-position-in-the-range-it-laid-out.html
new file mode 100644
index 00000000000..37d068cd1bb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-keeps-the-scroll-position-in-the-range-it-laid-out.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes shrink-page {
+ from { height: 1000px; }
+ to { height: 0px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #bar {
+ height: 10px;
+ width: 50px;
+ animation: widen linear;
+ animation-timeline: scroll(root);
+ }
+ #spacer {
+ height: 1000px;
+ }
+ #shrinking {
+ height: 1000px;
+ animation: shrink-page linear both;
+ animation-timeline: scroll(root);
+ }
+</style>
+<div id="bar"></div>
+<div id="spacer"></div>
+<div id="shrinking"></div>
+<script>
+ // A tick at the end of the page shrinks the page, past the offset the compositor still says it is scrolled to. The
+ // next tick is at the end of the range it laid out, as a host's scroll offset is, and not past it, which would end
+ // an animation that does not fill forwards.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => bar.getAnimations()[0], frameTime => {
+ const position = document.documentElement.scrollHeight - window.innerHeight;
+ const widths = [];
+ for (let tick = 1; tick <= 6; tick++) {
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ widths.push(internals.presentedBorderBox(bar).width);
+ }
+ return { widths, height: internals.presentedBorderBox(shrinking).height };
+ });
+ println(`the ticks shrank the page: ${result.height < 1000}`);
+ println(`the bar stays at the end of its animation: ${result.widths.every(width => width === 300)}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-resolves-a-calculated-view-timeline-inset-in-a-resized-scrollport.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-resolves-a-calculated-view-timeline-inset-in-a-resized-scrollport.html
new file mode 100644
index 00000000000..9ad30eeef2f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-resolves-a-calculated-view-timeline-inset-in-a-resized-scrollport.html
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes resize {
+ from { height: 100px; }
+ to { height: 300px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #scroller {
+ height: 100px;
+ width: 400px;
+ overflow: auto;
+ animation: resize linear both;
+ animation-timeline: scroll(root);
+ }
+ #before {
+ height: 50px;
+ }
+ #subject {
+ height: 50px;
+ animation: widen linear both;
+ animation-timeline: view(calc(10% + 1px));
+ }
+ #after {
+ height: 1000px;
+ }
+ #spacer {
+ height: 2000px;
+ }
+</style>
+<div id="scroller">
+ <div id="before"></div>
+ <div id="subject"></div>
+ <div id="after"></div>
+</div>
+<div id="spacer"></div>
+<script>
+ // A tick that resizes the scrollport of a view timeline resolves the percentage of a linear calculated inset against
+ // the new size, as the host's next rendering update does.
+ const inset = size => 0.1 * size + 1;
+ const widthAt = (position, size) => {
+ const coverStart = 50 - (size - inset(size));
+ const coverEnd = 50 + 50 - inset(size);
+ return 100 + 200 * (position - coverStart) / (coverEnd - coverStart);
+ };
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ const position = document.documentElement.scrollHeight - window.innerHeight;
+ for (let tick = 1; tick <= 8; tick++)
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ return {
+ size: internals.presentedBorderBox(scroller).height,
+ width: internals.presentedBorderBox(subject).width,
+ };
+ });
+ println(`the ticks resized the scrollport: ${result.size > 100}`);
+ println(`the subject shows the progress with its insets in that scrollport: ${Math.abs(result.width - widthAt(0, result.size)) < 0.5}`);
+ window.scrollTo(0, document.documentElement.scrollHeight);
+ await twoFrames();
+ await twoFrames();
+ const size = scroller.getBoundingClientRect().height;
+ println(`the host computes that progress in its own scrollport: ${Math.abs(subject.getBoundingClientRect().width - widthAt(0, size)) < 0.5}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-resolves-a-view-timeline-inset-in-a-resized-scrollport.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-resolves-a-view-timeline-inset-in-a-resized-scrollport.html
new file mode 100644
index 00000000000..ee387b708db
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-resolves-a-view-timeline-inset-in-a-resized-scrollport.html
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes resize {
+ from { height: 100px; }
+ to { height: 300px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #scroller {
+ height: 100px;
+ width: 400px;
+ overflow: auto;
+ animation: resize linear both;
+ animation-timeline: scroll(root);
+ }
+ #before {
+ height: 50px;
+ }
+ #subject {
+ height: 50px;
+ animation: widen linear both;
+ animation-timeline: view(10%);
+ }
+ #after {
+ height: 1000px;
+ }
+ #spacer {
+ height: 2000px;
+ }
+</style>
+<div id="scroller">
+ <div id="before"></div>
+ <div id="subject"></div>
+ <div id="after"></div>
+</div>
+<div id="spacer"></div>
+<script>
+ // A tick that resizes the scrollport of a view timeline resolves the timeline's percentage insets against the new
+ // size, as the host's next rendering update does.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ const position = document.documentElement.scrollHeight - window.innerHeight;
+ for (let tick = 1; tick <= 8; tick++)
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ return {
+ size: internals.presentedBorderBox(scroller).height,
+ width: internals.presentedBorderBox(subject).width,
+ };
+ });
+ const coverStart = 50 - (result.size - 0.1 * result.size);
+ const coverEnd = 50 + 50 - 0.1 * result.size;
+ const progress = (0 - coverStart) / (coverEnd - coverStart);
+ println(`the ticks resized the scrollport: ${result.size > 100}`);
+ println(`the subject shows the progress with its insets in that scrollport: ${Math.abs(result.width - (100 + 200 * progress)) < 0.5}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-samples-a-view-timeline-at-a-negative-scroll-offset.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-samples-a-view-timeline-at-a-negative-scroll-offset.html
new file mode 100644
index 00000000000..9b2e8374499
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-samples-a-view-timeline-at-a-negative-scroll-offset.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes tall {
+ from { height: 10px; }
+ to { height: 30px; }
+ }
+ body {
+ margin: 0;
+ }
+ #scroller {
+ direction: rtl;
+ display: flex;
+ height: 100px;
+ width: 200px;
+ overflow: auto;
+ }
+ #scroller > * {
+ flex: none;
+ }
+ #before {
+ width: 300px;
+ height: 10px;
+ }
+ #subject {
+ width: 100px;
+ height: 10px;
+ animation: tall linear both;
+ animation-timeline: view(x);
+ }
+ #after {
+ width: 600px;
+ height: 10px;
+ }
+</style>
+<div id="scroller">
+ <div id="before"></div>
+ <div id="subject"></div>
+ <div id="after"></div>
+</div>
+<script>
+ // A scroller that overflows towards its start scrolls to negative offsets, which a tick samples its timelines at.
+ // The subject starts 200px before the scrollport, so that its cover range runs from -400px to -100px.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ for (let tick = 1; tick <= 4; tick++)
+ internals.injectClockTickScrollingElement(frameTime + 16 * tick, scroller, -250, 0);
+ return internals.presentedBorderBox(subject).height;
+ });
+ println(`the subject shows the progress at the negative scroll offset: ${Math.abs(result - 20) < 0.5}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-samples-view-timelines-of-element-scrollers.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-samples-view-timelines-of-element-scrollers.html
new file mode 100644
index 00000000000..2cc7eb3828a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-samples-view-timelines-of-element-scrollers.html
@@ -0,0 +1,115 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes shrink-height {
+ from { height: 200px; }
+ to { height: 0px; }
+ }
+ @keyframes shrink-width {
+ from { width: 200px; }
+ to { width: 0px; }
+ }
+ @keyframes tall {
+ from { height: 10px; }
+ to { height: 30px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #vertical {
+ height: 200px;
+ width: 400px;
+ overflow: auto;
+ margin-top: 10px;
+ }
+ #vertical-shrinking {
+ height: 200px;
+ animation: shrink-height linear both;
+ animation-timeline: scroll(nearest block);
+ }
+ #vertical-subject {
+ height: 100px;
+ animation: widen linear both;
+ animation-timeline: view();
+ }
+ #vertical-after {
+ height: 600px;
+ }
+ #horizontal {
+ display: flex;
+ height: 100px;
+ width: 200px;
+ overflow: auto;
+ margin-left: 20px;
+ }
+ #horizontal > * {
+ flex: none;
+ }
+ #horizontal-shrinking {
+ width: 200px;
+ animation: shrink-width linear both;
+ animation-timeline: scroll(nearest inline);
+ }
+ #horizontal-subject {
+ width: 100px;
+ height: 10px;
+ animation: tall linear both;
+ animation-timeline: view(inline);
+ }
+ #horizontal-after {
+ width: 600px;
+ }
+</style>
+<div id="vertical">
+ <div id="vertical-shrinking"></div>
+ <div id="vertical-subject"></div>
+ <div id="vertical-after"></div>
+</div>
+<div id="horizontal">
+ <div id="horizontal-shrinking"></div>
+ <div id="horizontal-subject"></div>
+ <div id="horizontal-after"></div>
+</div>
+<script>
+ // The render clock samples view and scroll timelines of an element's scroll node, along either axis, from the boxes
+ // its ticks laid out.
+ asyncTest(async done => {
+ const close = (a, b) => Math.abs(a - b) < 0.5;
+ const scrolling = (frameTime, scroller, x, y) => {
+ for (let tick = 1; tick <= 10; tick++)
+ internals.injectClockTickScrollingElement(frameTime + 16 * tick, scroller, x, y);
+ };
+ const result = await whileLaneAnimates(() => document.getElementById("vertical-subject").getAnimations()[0], frameTime => {
+ const verticalSubject = document.getElementById("vertical-subject");
+ const horizontalSubject = document.getElementById("horizontal-subject");
+ const verticalScrollport = vertical.getBoundingClientRect();
+ const horizontalScrollport = horizontal.getBoundingClientRect();
+
+ scrolling(frameTime, vertical, 0, 100);
+ const verticalBox = internals.presentedBorderBox(verticalSubject);
+ const verticalShrinking = internals.presentedBorderBox(document.getElementById("vertical-shrinking"));
+ scrolling(frameTime + 160, horizontal, 100, 0);
+ const horizontalBox = internals.presentedBorderBox(horizontalSubject);
+ const horizontalShrinking = internals.presentedBorderBox(document.getElementById("horizontal-shrinking"));
+
+ const verticalOffset = verticalBox.top - verticalScrollport.top;
+ const horizontalOffset = horizontalBox.left - horizontalScrollport.left;
+ return {
+ verticalMoved: verticalOffset < 200,
+ verticalMatches: close(verticalBox.width, 100 + 200 * (100 - (verticalOffset - 200)) / (100 + 200)),
+ verticalRangeMatches: close(verticalShrinking.height, 200 * (1 - 100 / (verticalShrinking.height + 100 + 600 - 200))),
+ horizontalMoved: horizontalOffset < 200,
+ horizontalMatches: close(horizontalBox.height, 10 + 20 * (100 - (horizontalOffset - 200)) / (100 + 200)),
+ horizontalRangeMatches: close(horizontalShrinking.width, 200 * (1 - 100 / (horizontalShrinking.width + 100 + 600 - 200))),
+ };
+ });
+ for (const [name, value] of Object.entries(result))
+ println(`${name}: ${value}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-settles-a-slow-scroll.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-settles-a-slow-scroll.html
new file mode 100644
index 00000000000..380bce02cb5
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-settles-a-slow-scroll.html
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes shrink {
+ 0% { height: 400px; }
+ 5%, 100% { height: 0px; }
+ }
+ @keyframes widen {
+ from { width: 100px; }
+ to { width: 300px; }
+ }
+ body {
+ margin: 0;
+ }
+ #shrinking {
+ height: 400px;
+ animation: shrink linear both;
+ animation-timeline: scroll(root);
+ }
+ #subject {
+ height: 100px;
+ animation: widen linear both;
+ animation-timeline: view();
+ }
+ #spacer {
+ height: 2000px;
+ }
+</style>
+<div id="shrinking"></div>
+<div id="subject"></div>
+<div id="spacer"></div>
+<script>
+ // A smooth scroll's tail moves by less per frame than counts as a scroll, but it adds up. The boxes it moved decide
+ // the view timeline's progress all the same.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ const step = 0.04;
+ const ticks = 500;
+ let box;
+ for (let tick = 1; tick <= ticks; tick++) {
+ internals.injectClockTick(frameTime + tick, step * tick);
+ box = internals.presentedBorderBox(subject);
+ }
+ return { position: step * ticks, top: box.top, width: box.width };
+ });
+ const coverStart = result.top - window.innerHeight;
+ const progress = (result.position - coverStart) / (result.top + 100 - coverStart);
+ println(`the scroll moved the subject: ${result.top < 400}`);
+ println(`the subject shows the progress where the scroll moved it: ${Math.abs(result.width - (100 + 200 * progress)) < 0.5}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-settles-where-a-view-timeline-feeds-back.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-settles-where-a-view-timeline-feeds-back.html
new file mode 100644
index 00000000000..d68b0f15359
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/clock-tick-settles-where-a-view-timeline-feeds-back.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<style>
+ @keyframes narrow {
+ from { width: 300px; }
+ to { width: 100px; }
+ }
+ body {
+ margin: 0;
+ }
+ #before {
+ height: 1000px;
+ }
+ #subject {
+ animation: narrow linear both;
+ animation-timeline: view();
+ }
+ #subject div {
+ display: inline-block;
+ width: 100px;
+ height: 100px;
+ }
+ #after {
+ height: 2000px;
+ }
+</style>
+<div id="before"></div>
+<div id="subject"><div></div><div></div></div>
+<div id="after"></div>
+<script>
+ // The subject's boxes fit on one line while it is at least 200px wide. One line leaves its progress where it is too
+ // narrow for them, and two lines where it is wide enough, so no layout agrees with the progress it decides. A stopped
+ // scroll must not have the ticks go back and forth between the two forever.
+ asyncTest(async done => {
+ const result = await whileLaneAnimates(() => subject.getAnimations()[0], frameTime => {
+ const position = 400 + 375;
+ const heights = [];
+ for (let tick = 1; tick <= 30; tick++) {
+ internals.injectClockTick(frameTime + 16 * tick, position);
+ heights.push(internals.presentedBorderBox(subject).height);
+ }
+ return { heights, lane: internals.clockLaneState(document) };
+ });
+ println(`the ticks tried both layouts: ${new Set(result.heights).size === 2}`);
+ println(`the ticks settled on one: ${new Set(result.heights.slice(10)).size === 1}`);
+ println(`render clock: ${result.lane}`);
+ done();
+ });
+</script>