Bug 1814786 - Part 2: Factor out the tree traversal part into nsAnimationManager. r=emilio

So we can reuse it for view timeline. Also, tweak the function name a
little bit so it'd be easier to use template functions.

Differential Revision: https://phabricator.services.mozilla.com/D169270
This commit is contained in:
Boris Chiou committed 2023-03-07 23:57:53 +00:00
1 parent 18c853467e
commit 15511012d1
3 files changed
+79 -81

No files matched your search

+26 -70
View File
@@ -52,8 +52,24 @@ ScrollTimeline::ScrollTimeline(Document* aDocument, const Scroller& aScroller,
RegisterWithScrollSource();
}
static Element* FindNearestScroller(const Element* aSubject) {
MOZ_ASSERT(aSubject);
Element* curr = aSubject->GetFlattenedTreeParentElement();
Element* root = aSubject->OwnerDoc()->GetDocumentElement();
while (curr && curr != root) {
const ComputedStyle* style = Servo_Element_GetMaybeOutOfDateStyle(curr);
MOZ_ASSERT(style, "The ancestor should be styled.");
if (style->StyleDisplay()->IsScrollableOverflow()) {
break;
}
curr = curr->GetFlattenedTreeParentElement();
}
// If there is no scroll container, we use root.
return curr ? curr : root;
}
/* static */
already_AddRefed<ScrollTimeline> ScrollTimeline::FromAnonymousScroll(
already_AddRefed<ScrollTimeline> ScrollTimeline::MakeAnonymous(
Document* aDocument, const NonOwningAnimationTarget& aTarget,
StyleScrollAxis aAxis, StyleScroller aScroller) {
MOZ_ASSERT(aTarget);
@@ -62,19 +78,10 @@ already_AddRefed<ScrollTimeline> ScrollTimeline::FromAnonymousScroll(
case StyleScroller::Root:
scroller = Scroller::Root(aTarget.mElement->OwnerDoc());
break;
case StyleScroller::Nearest: {
Element* curr = aTarget.mElement->GetFlattenedTreeParentElement();
Element* root = aTarget.mElement->OwnerDoc()->GetDocumentElement();
while (curr && curr != root) {
const ComputedStyle* style = Servo_Element_GetMaybeOutOfDateStyle(curr);
MOZ_ASSERT(style, "The ancestor should be styled.");
if (style->StyleDisplay()->IsScrollableOverflow()) {
break;
}
curr = curr->GetFlattenedTreeParentElement();
}
// If there is no scroll container, we use root.
scroller = Scroller::Nearest(curr ? curr : root);
scroller = Scroller::Nearest(FindNearestScroller(aTarget.mElement));
break;
}
}
@@ -88,65 +95,14 @@ already_AddRefed<ScrollTimeline> ScrollTimeline::FromAnonymousScroll(
return MakeAndAddRef<ScrollTimeline>(aDocument, scroller, aAxis);
}
/* static*/ already_AddRefed<ScrollTimeline> ScrollTimeline::FromNamedScroll(
Document* aDocument, const NonOwningAnimationTarget& aTarget,
const nsAtom* aName) {
/* static*/ already_AddRefed<ScrollTimeline> ScrollTimeline::MakeNamed(
Document* aDocument, Element* aReferenceElement,
const StyleScrollTimeline& aStyleTimeline) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aTarget);
// A named scroll progress timeline is referenceable in animation-timeline by:
// 1. the declaring element itself
// 2. that element’s descendants
// 3. that element’s following siblings and their descendants
// https://drafts.csswg.org/scroll-animations-1/#timeline-scope
Element* result = nullptr;
StyleScrollAxis axis = StyleScrollAxis::Block;
for (Element* curr = aTarget.mElement; curr;
curr = curr->GetParentElement()) {
// If multiple elements have declared the same timeline name, the matching
// timeline is the one declared on the nearest element in tree order, which
// considers siblings closer than parents.
// Note: This should be fine for parallel traversal because we update
// animations by SequentialTask.
for (Element* e = curr; e; e = e->GetPreviousElementSibling()) {
const ComputedStyle* style = Servo_Element_GetMaybeOutOfDateStyle(e);
// The elements in the shadow dom might not be in the flat tree.
if (!style) {
continue;
}
const nsStyleUIReset* ui = style->StyleUIReset();
// Note: scroll-timeline is a coordinated property list, so we use the
// count of the base property, scroll-timeline-name, as the max length.
for (uint32_t i = 0; i < ui->mScrollTimelineNameCount; ++i) {
const auto& timeline = ui->mScrollTimelines[i];
if (timeline.GetName() == aName) {
result = e;
axis = timeline.GetAxis();
break;
}
}
if (result) {
break;
}
}
if (result) {
break;
}
}
// If we cannot find a matched scroll-timeline-name, this animation is not
// associated with a timeline.
// https://drafts.csswg.org/css-animations-2/#typedef-timeline-name
if (!result) {
return nullptr;
}
// TODO: Will reuse named progress timelines in the following patches.
Scroller scroller = Scroller::Named(result);
return MakeAndAddRef<ScrollTimeline>(aDocument, std::move(scroller), axis);
Scroller scroller = Scroller::Named(aReferenceElement);
return MakeAndAddRef<ScrollTimeline>(aDocument, std::move(scroller),
aStyleTimeline.GetAxis());
}
Nullable<TimeDuration> ScrollTimeline::GetCurrentTimeAsDuration() const {
+4 -4
View File
@@ -104,13 +104,13 @@ class ScrollTimeline final : public AnimationTimeline {
}
};
static already_AddRefed<ScrollTimeline> FromAnonymousScroll(
static already_AddRefed<ScrollTimeline> MakeAnonymous(
Document* aDocument, const NonOwningAnimationTarget& aTarget,
StyleScrollAxis aAxis, StyleScroller aScroller);
static already_AddRefed<ScrollTimeline> FromNamedScroll(
Document* aDocument, const NonOwningAnimationTarget& aTarget,
const nsAtom* aName);
static already_AddRefed<ScrollTimeline> MakeNamed(
Document* aDocument, Element* aReferenceElement,
const StyleScrollTimeline& aStyleTimeline);
bool operator==(const ScrollTimeline& aOther) const {
return mDocument == aOther.mDocument && mSource == aOther.mSource &&
+49 -7
View File
@@ -205,23 +205,65 @@ static void UpdateOldAnimationPropertiesWithNew(
}
}
static already_AddRefed<dom::AnimationTimeline> GetNamedProgressTimeline(
dom::Document* aDocument, const NonOwningAnimationTarget& aTarget,
const nsAtom* aName) {
// A named progress timeline is referenceable in animation-timeline by:
// 1. the declaring element itself
// 2. that element’s descendants
// 3. that element’s following siblings and their descendants
// https://drafts.csswg.org/scroll-animations-1/#timeline-scope
for (Element* curr = aTarget.mElement; curr;
curr = curr->GetParentElement()) {
// If multiple elements have declared the same timeline name, the matching
// timeline is the one declared on the nearest element in tree order, which
// considers siblings closer than parents.
// Note: This is fine for parallel traversal because we update animations by
// SequentialTask.
for (Element* e = curr; e; e = e->GetPreviousElementSibling()) {
const ComputedStyle* style = Servo_Element_GetMaybeOutOfDateStyle(e);
// The elements in the shadow dom might not be in the flat tree.
if (!style) {
continue;
}
const nsStyleUIReset* ui = style->StyleUIReset();
// In case of a name conflict on the same element, scroll progress
// timelines take precedence over view progress timelines.
// TODO: Will reuse named progress timelines in the following patches.
for (uint32_t i = 0; i < ui->mScrollTimelineNameCount; ++i) {
const auto& timeline = ui->mScrollTimelines[i];
if (timeline.GetName() == aName) {
return ScrollTimeline::MakeNamed(aDocument, e, timeline);
}
}
// TODO: Bug 1737920. Support view-timeline.
}
}
// If we cannot find a matched scroll-timeline-name, this animation is not
// associated with a timeline.
// https://drafts.csswg.org/css-animations-2/#valdef-animation-timeline-custom-ident
return nullptr;
}
static already_AddRefed<dom::AnimationTimeline> GetTimeline(
const StyleAnimationTimeline& aStyleTimeline, nsPresContext* aPresContext,
const NonOwningAnimationTarget& aTarget) {
switch (aStyleTimeline.tag) {
case StyleAnimationTimeline::Tag::Timeline: {
// Check scroll-timeline-name property.
nsAtom* name = aStyleTimeline.AsTimeline().AsAtom();
// Check scroll-timeline-name property or view-timeline-property.
const nsAtom* name = aStyleTimeline.AsTimeline().AsAtom();
return name != nsGkAtoms::_empty
? ScrollTimeline::FromNamedScroll(aPresContext->Document(),
aTarget, name)
? GetNamedProgressTimeline(aPresContext->Document(), aTarget,
name)
: nullptr;
}
case StyleAnimationTimeline::Tag::Scroll: {
const auto& scroll = aStyleTimeline.AsScroll();
return ScrollTimeline::FromAnonymousScroll(aPresContext->Document(),
aTarget, scroll._0, scroll._1);
break;
return ScrollTimeline::MakeAnonymous(aPresContext->Document(), aTarget,
scroll._0, scroll._1);
}
case StyleAnimationTimeline::Tag::Auto:
return do_AddRef(aTarget.mElement->OwnerDoc()->Timeline());