summaryrefslogtreecommitdiffstats
path: root/dom/animation/ElementAnimationData.h
blob: 9183f0f008b2ec07f748c6965392b234b9e1e83c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_ElementAnimationData_h
#define mozilla_ElementAnimationData_h

#include "mozilla/UniquePtr.h"
#include "mozilla/PseudoStyleType.h"

class nsCycleCollectionTraversalCallback;

namespace mozilla {
enum class PseudoStyleType : uint8_t;
class EffectSet;
template <typename Animation>
class AnimationCollection;
template <typename TimelineType>
class TimelineCollection;
namespace dom {
class Element;
class CSSAnimation;
class CSSTransition;
class ProgressTimelineScheduler;
class ScrollTimeline;
class ViewTimeline;
}  // namespace dom
using CSSAnimationCollection = AnimationCollection<dom::CSSAnimation>;
using CSSTransitionCollection = AnimationCollection<dom::CSSTransition>;
using ScrollTimelineCollection = TimelineCollection<dom::ScrollTimeline>;
using ViewTimelineCollection = TimelineCollection<dom::ViewTimeline>;

// The animation data for a given element (and its pseudo-elements).
class ElementAnimationData {
  struct PerElementOrPseudoData {
    UniquePtr<EffectSet> mEffectSet;
    UniquePtr<CSSAnimationCollection> mAnimations;
    UniquePtr<CSSTransitionCollection> mTransitions;

    // Note: scroll-timeline-name is applied to elements which could be
    // scroll containers, or replaced elements. view-timeline-name is applied to
    // all elements. However, the named timeline is referenceable in
    // animation-timeline by the tree order scope.
    // Spec: https://drafts.csswg.org/scroll-animations-1/#timeline-scope.
    //
    // So it should be fine to create timeline objects only on the elements and
    // pseudo elements which support animations.
    UniquePtr<ScrollTimelineCollection> mScrollTimelines;
    UniquePtr<ViewTimelineCollection> mViewTimelines;

    // This is different from |mScrollTimelines|. We use this to schedule all
    // scroll-driven animations (which use anonymous/named scroll timelines or
    // anonymous/name view timelines) for a specific scroll source (which is the
    // element with nsIScrollableFrame).
    //
    // TimelineCollection owns and manages the named progress timeline generated
    // by specifying scroll-timeline-name property and view-timeline-name
    // property on this element. However, the anonymous progress timelines (e.g.
    // animation-timeline:scroll()) are owned by Animation objects only.
    //
    // Note:
    // 1. For named scroll timelines. The element which specifies
    //    scroll-timeline-name is the scroll source. However, for named view
    //    timelines, the element which specifies view-timeline-name may not be
    //    the scroll source because we use its nearest scroll container as the
    //    scroll source.
    // 2. For anonymous progress timelines, we don't keep their timeline obejcts
    //    in TimelineCollection.
    // So, per 1) and 2), we use |mProgressTimelineScheduler| for the scroll
    // source element to schedule scroll-driven animations.
    UniquePtr<dom::ProgressTimelineScheduler> mProgressTimelineScheduler;

    PerElementOrPseudoData();
    ~PerElementOrPseudoData();

    EffectSet& DoEnsureEffectSet();
    CSSTransitionCollection& DoEnsureTransitions(dom::Element&,
                                                 PseudoStyleType);
    CSSAnimationCollection& DoEnsureAnimations(dom::Element&, PseudoStyleType);
    ScrollTimelineCollection& DoEnsureScrollTimelines(dom::Element&,
                                                      PseudoStyleType);
    ViewTimelineCollection& DoEnsureViewTimelines(dom::Element&,
                                                  PseudoStyleType);
    dom::ProgressTimelineScheduler& DoEnsureProgressTimelineScheduler(
        dom::Element&, PseudoStyleType);

    void DoClearEffectSet();
    void DoClearTransitions();
    void DoClearAnimations();
    void DoClearScrollTimelines();
    void DoClearViewTimelines();
    void DoClearProgressTimelineScheduler();

    void Traverse(nsCycleCollectionTraversalCallback&);
  };

  PerElementOrPseudoData mElementData;

  // TODO(emilio): Maybe this should be a hash map eventually, once we allow
  // animating all pseudo-elements.
  PerElementOrPseudoData mBeforeData;
  PerElementOrPseudoData mAfterData;
  PerElementOrPseudoData mMarkerData;

  const PerElementOrPseudoData& DataFor(PseudoStyleType aType) const {
    switch (aType) {
      case PseudoStyleType::NotPseudo:
        break;
      case PseudoStyleType::before:
        return mBeforeData;
      case PseudoStyleType::after:
        return mAfterData;
      case PseudoStyleType::marker:
        return mMarkerData;
      default:
        MOZ_ASSERT_UNREACHABLE(
            "Should not try to get animation effects for "
            "a pseudo other that :before, :after or ::marker");
        break;
    }
    return mElementData;
  }

  PerElementOrPseudoData& DataFor(PseudoStyleType aType) {
    const auto& data =
        const_cast<const ElementAnimationData*>(this)->DataFor(aType);
    return const_cast<PerElementOrPseudoData&>(data);
  }

 public:
  void Traverse(nsCycleCollectionTraversalCallback&);

  void ClearAllAnimationCollections();

  EffectSet* GetEffectSetFor(PseudoStyleType aType) const {
    return DataFor(aType).mEffectSet.get();
  }

  void ClearEffectSetFor(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (data.mEffectSet) {
      data.DoClearEffectSet();
    }
  }

  EffectSet& EnsureEffectSetFor(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (auto* set = data.mEffectSet.get()) {
      return *set;
    }
    return data.DoEnsureEffectSet();
  }

  CSSTransitionCollection* GetTransitionCollection(PseudoStyleType aType) {
    return DataFor(aType).mTransitions.get();
  }

  void ClearTransitionCollectionFor(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (data.mTransitions) {
      data.DoClearTransitions();
    }
  }

  CSSTransitionCollection& EnsureTransitionCollection(dom::Element& aOwner,
                                                      PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (auto* collection = data.mTransitions.get()) {
      return *collection;
    }
    return data.DoEnsureTransitions(aOwner, aType);
  }

  CSSAnimationCollection* GetAnimationCollection(PseudoStyleType aType) {
    return DataFor(aType).mAnimations.get();
  }

  void ClearAnimationCollectionFor(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (data.mAnimations) {
      data.DoClearAnimations();
    }
  }

  CSSAnimationCollection& EnsureAnimationCollection(dom::Element& aOwner,
                                                    PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (auto* collection = data.mAnimations.get()) {
      return *collection;
    }
    return data.DoEnsureAnimations(aOwner, aType);
  }

  ScrollTimelineCollection* GetScrollTimelineCollection(PseudoStyleType aType) {
    return DataFor(aType).mScrollTimelines.get();
  }

  void ClearScrollTimelineCollectionFor(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (data.mScrollTimelines) {
      data.DoClearScrollTimelines();
    }
  }

  ScrollTimelineCollection& EnsureScrollTimelineCollection(
      dom::Element& aOwner, PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (auto* collection = data.mScrollTimelines.get()) {
      return *collection;
    }
    return data.DoEnsureScrollTimelines(aOwner, aType);
  }

  ViewTimelineCollection* GetViewTimelineCollection(PseudoStyleType aType) {
    return DataFor(aType).mViewTimelines.get();
  }

  void ClearViewTimelineCollectionFor(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (data.mViewTimelines) {
      data.DoClearViewTimelines();
    }
  }

  ViewTimelineCollection& EnsureViewTimelineCollection(dom::Element& aOwner,
                                                       PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (auto* collection = data.mViewTimelines.get()) {
      return *collection;
    }
    return data.DoEnsureViewTimelines(aOwner, aType);
  }

  dom::ProgressTimelineScheduler* GetProgressTimelineScheduler(
      PseudoStyleType aType) {
    return DataFor(aType).mProgressTimelineScheduler.get();
  }

  void ClearProgressTimelineScheduler(PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (data.mProgressTimelineScheduler) {
      data.DoClearProgressTimelineScheduler();
    }
  }

  dom::ProgressTimelineScheduler& EnsureProgressTimelineScheduler(
      dom::Element& aOwner, PseudoStyleType aType) {
    auto& data = DataFor(aType);
    if (auto* collection = data.mProgressTimelineScheduler.get()) {
      return *collection;
    }
    return data.DoEnsureProgressTimelineScheduler(aOwner, aType);
  }

  ElementAnimationData() = default;
};

}  // namespace mozilla

#endif