File: scroll_timeline.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (307 lines) | stat: -rw-r--r-- 11,040 bytes parent folder | download | duplicates (3)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/animation/scroll_timeline.h"

#include <optional>

#include "third_party/blink/renderer/bindings/core/v8/v8_scroll_timeline_options.h"
#include "third_party/blink/renderer/core/animation/scroll_timeline_util.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/layout/layout_box.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"

namespace blink {

namespace {

Node* ResolveSource(Element* source) {
  if (source && source == source->GetDocument().ScrollingElementNoLayout()) {
    return &source->GetDocument();
  }
  return source;
}

}  // namespace

ScrollTimeline* ScrollTimeline::Create(Document& document,
                                       ScrollTimelineOptions* options,
                                       ExceptionState& exception_state) {
  std::optional<Element*> source = options->hasSource()
                                       ? std::make_optional(options->source())
                                       : std::nullopt;

  ScrollAxis axis =
      options->hasAxis() ? options->axis().AsEnum() : ScrollAxis::kBlock;

  // The scrollingElement depends on style/layout-tree in quirks mode. Update
  // such that subsequent calls to ScrollingElementNoLayout returns up-to-date
  // information.
  if (document.InQuirksMode())
    document.UpdateStyleAndLayoutTree();

  return Create(&document, source.value_or(document.ScrollingElementNoLayout()),
                axis);
}

ScrollTimeline* ScrollTimeline::Create(Document* document,
                                       Element* source,
                                       ScrollAxis axis) {
  ScrollTimeline* scroll_timeline = MakeGarbageCollected<ScrollTimeline>(
      document, ReferenceType::kSource, source, axis);
  scroll_timeline->UpdateSnapshot();

  return scroll_timeline;
}

ScrollTimeline::ScrollTimeline(Document* document,
                               ReferenceType reference_type,
                               Element* reference,
                               ScrollAxis axis)
    : ScrollSnapshotTimeline(document),
      reference_type_(reference_type),
      reference_element_(reference),
      axis_(axis) {}

Element* ScrollTimeline::RetainingElement() const {
  return reference_element_.Get();
}

// TODO(crbug.com/1060384): This section is missing from the spec rewrite.
// Resolved to remove the before and after phases in
// https://github.com/w3c/csswg-drafts/issues/7240.
// https://drafts.csswg.org/scroll-animations-1/#current-time-algorithm
ScrollTimeline::TimelineState ScrollTimeline::ComputeTimelineState() const {
  TimelineState state;
  state.resolved_source = ComputeResolvedSource();

  // 1. If scroll timeline is inactive, return an unresolved time value.
  // https://github.com/WICG/scroll-animations/issues/31
  // https://wicg.github.io/scroll-animations/#current-time-algorithm
  LayoutBox* scroll_container = ComputeScrollContainer(state.resolved_source);
  if (!scroll_container) {
    return state;
  }

  // The scrollable area must exist since the timeline is active.
  DCHECK(scroll_container->GetScrollableArea());

  // Depending on the writing-mode and direction, the scroll origin shifts and
  // the scroll offset may be negative. The easiest way to deal with this is to
  // use only the magnitude of the scroll offset, and compare it to (max_offset
  // - min_offset).
  PaintLayerScrollableArea* scrollable_area =
      scroll_container->GetScrollableArea();
  // Scrollable area must exist since the timeline is active.
  DCHECK(scrollable_area);

  // Using the absolute value of the scroll offset only makes sense if either
  // the max or min scroll offset for a given axis is 0. This should be
  // guaranteed by the scroll origin code, but these DCHECKs ensure that.
  DCHECK(scrollable_area->MaximumScrollOffset().y() == 0 ||
         scrollable_area->MinimumScrollOffset().y() == 0);
  DCHECK(scrollable_area->MaximumScrollOffset().x() == 0 ||
         scrollable_area->MinimumScrollOffset().x() == 0);

  ScrollOffset scroll_offset = scrollable_area->GetScrollOffset();
  auto physical_orientation =
      ToPhysicalScrollOrientation(GetAxis(), *scroll_container);
  double current_offset = (physical_orientation == kHorizontalScroll)
                              ? scroll_offset.x()
                              : scroll_offset.y();
  // When using a rtl direction, current_offset grows correctly from 0 to
  // max_offset, but is negative. Since our offsets are all just deltas along
  // the orientation direction, we can just take the absolute current_offset and
  // use that everywhere.
  current_offset = std::abs(current_offset);

  CalculateOffsets(scrollable_area, physical_orientation, &state);
  if (!state.scroll_offsets) {
    // Scroll Offsets may be null if the type of subject element is not
    // supported.
    return state;
  }

  state.zoom = scroll_container->StyleRef().EffectiveZoom();
  // Timeline is inactive unless the scroll offset range is positive.
  // github.com/w3c/csswg-drafts/issues/7401
  if (state.scroll_offsets->end - state.scroll_offsets->start > 0) {
    state.phase = TimelinePhase::kActive;
    double offset = current_offset - state.scroll_offsets->start;
    double range = state.scroll_offsets->end - state.scroll_offsets->start;
    double duration_in_microseconds =
        range * kScrollTimelineMicrosecondsPerPixel;
    state.duration = std::make_optional(ANIMATION_TIME_DELTA_FROM_MILLISECONDS(
        duration_in_microseconds / 1000));
    state.current_time =
        base::Microseconds(offset * kScrollTimelineMicrosecondsPerPixel);
  }
  return state;
}

void ScrollTimeline::CalculateOffsets(PaintLayerScrollableArea* scrollable_area,
                                      ScrollOrientation physical_orientation,
                                      TimelineState* state) const {
  CalculateScrollLimits(scrollable_area, physical_orientation, state);
  state->scroll_offsets = state->scroll_limits;
}

Element* ScrollTimeline::source() const {
  return ComputeSource();
}

Element* ScrollTimeline::ComputeSource() const {
  if (reference_type_ == ReferenceType::kNearestAncestor &&
      reference_element_) {
    reference_element_->GetDocument().UpdateStyleAndLayout(
        DocumentUpdateReason::kJavaScript);
  }
  return ComputeSourceNoLayout();
}

Element* ScrollTimeline::ComputeSourceNoLayout() const {
  if (reference_type_ == ReferenceType::kSource) {
    return reference_element_.Get();
  }
  DCHECK_EQ(ReferenceType::kNearestAncestor, reference_type_);

  if (!reference_element_) {
    return nullptr;
  }

  LayoutObject* layout_object = reference_element_->GetLayoutObject();
  if (!layout_object) {
    return nullptr;
  }

  const LayoutBox* scroll_container =
      layout_object->ContainingScrollContainer();
  if (!scroll_container) {
    return reference_element_->GetDocument().ScrollingElementNoLayout();
  }

  Node* node = scroll_container->GetNode();
  DCHECK(node || scroll_container->IsAnonymous());
  if (!node) {
    // The content scroller for a FieldSet is an anonymous block.  In this case,
    // the parent's node is the fieldset element.
    const LayoutBox* parent = DynamicTo<LayoutBox>(scroll_container->Parent());
    if (parent && parent->StyleRef().IsScrollContainer()) {
      node = parent->GetNode();
    }
  }

  if (!node) {
    NOTREACHED();
  }

  if (node->IsElementNode()) {
    return DynamicTo<Element>(node);
  }
  if (node->IsDocumentNode()) {
    return DynamicTo<Document>(node)->ScrollingElementNoLayout();
  }

  NOTREACHED();
}

void ScrollTimeline::AnimationAttached(Animation* animation) {
  if (RetainingElement() && !HasAnimations()) {
    RetainingElement()->RegisterScrollTimeline(this);
  }

  AnimationTimeline::AnimationAttached(animation);
}

void ScrollTimeline::AnimationDetached(Animation* animation) {
  AnimationTimeline::AnimationDetached(animation);

  if (RetainingElement() && !HasAnimations()) {
    RetainingElement()->UnregisterScrollTimeline(this);
  }
}

Node* ScrollTimeline::ComputeResolvedSource() const {
  return ResolveSource(ComputeSourceNoLayout());
}

void ScrollTimeline::Trace(Visitor* visitor) const {
  visitor->Trace(reference_element_);
  ScrollSnapshotTimeline::Trace(visitor);
}

bool ScrollTimeline::Matches(ReferenceType reference_type,
                             Element* reference_element,
                             ScrollAxis axis) const {
  return (reference_type_ == reference_type) &&
         (reference_element_ == reference_element) && (axis_ == axis);
}

ScrollAxis ScrollTimeline::GetAxis() const {
  return axis_;
}

std::optional<double> ScrollTimeline::GetMaximumScrollPosition() const {
  std::optional<ScrollOffsets> scroll_offsets = GetResolvedScrollOffsets();
  if (!scroll_offsets) {
    return std::nullopt;
  }
  LayoutBox* scroll_container = ScrollContainer();
  if (!scroll_container) {
    return std::nullopt;
  }

  PaintLayerScrollableArea* scrollable_area =
      scroll_container->GetScrollableArea();
  if (!scrollable_area) {
    return std::nullopt;
  }
  ScrollOffset scroll_dimensions = scrollable_area->MaximumScrollOffset() -
                                   scrollable_area->MinimumScrollOffset();
  auto physical_orientation =
      ToPhysicalScrollOrientation(GetAxis(), *scroll_container);
  return physical_orientation == kHorizontalScroll ? scroll_dimensions.x()
                                                   : scroll_dimensions.y();
}

std::optional<double> ScrollTimeline::GetCurrentScrollPosition() const {
  Node* source = ComputeResolvedSource();
  LayoutBox* scroll_container = ComputeScrollContainer(source);
  if (!scroll_container) {
    return std::nullopt;
  }

  PaintLayerScrollableArea* scrollable_area =
      scroll_container->GetScrollableArea();
  if (!scrollable_area) {
    return std::nullopt;
  }

  ScrollOffset scroll_offset = scrollable_area->GetScrollOffset();
  auto physical_orientation =
      ToPhysicalScrollOrientation(GetAxis(), *scroll_container);
  return (physical_orientation == kHorizontalScroll) ? scroll_offset.x()
                                                     : scroll_offset.y();
}

// static
ScrollOrientation ScrollTimeline::ToPhysicalScrollOrientation(
    ScrollAxis axis,
    const LayoutBox& source_box) {
  bool is_horizontal = source_box.IsHorizontalWritingMode();
  switch (axis) {
    case ScrollAxis::kBlock:
      return is_horizontal ? kVerticalScroll : kHorizontalScroll;
    case ScrollAxis::kInline:
      return is_horizontal ? kHorizontalScroll : kVerticalScroll;
    case ScrollAxis::kX:
      return kHorizontalScroll;
    case ScrollAxis::kY:
      return kVerticalScroll;
  }
}

}  // namespace blink