summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/scroll_timeline.h
blob: e71e10f884566f9f1226ad7a0d5667aa12a9aa31 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_SCROLL_TIMELINE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_SCROLL_TIMELINE_H_

#include "third_party/blink/renderer/core/animation/animation_timeline.h"
#include "third_party/blink/renderer/core/animation/scroll_timeline_options.h"
#include "third_party/blink/renderer/core/animation/timing.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

// Implements the ScrollTimeline concept from the Scroll-linked Animations spec.
//
// A ScrollTimeline is a special form of AnimationTimeline whose time values are
// not determined by wall-clock time but instead the progress of scrolling in a
// scroll container. The user is able to specify which scroll container to
// track, the direction of scroll they care about, and various attributes to
// control the conversion of scroll amount to time output.
//
// Spec: https://wicg.github.io/scroll-animations/#scroll-timelines
class CORE_EXPORT ScrollTimeline final : public AnimationTimeline {
  DEFINE_WRAPPERTYPEINFO();

 public:
  enum ScrollDirection {
    Block,
    Inline,
    Horizontal,
    Vertical,
  };

  static ScrollTimeline* Create(Document&,
                                ScrollTimelineOptions*,
                                ExceptionState&);

  ScrollTimeline(Element*,
                 ScrollDirection,
                 CSSPrimitiveValue*,
                 CSSPrimitiveValue*,
                 double,
                 Timing::FillMode);

  // AnimationTimeline implementation.
  double currentTime(bool& is_null) final;
  bool IsScrollTimeline() const override { return true; }
  // ScrollTimeline is not active if scrollSource is null, does not currently
  // have a CSS layout box, or if its layout box is not a scroll container.
  // https://github.com/WICG/scroll-animations/issues/31
  bool IsActive() const override;

  // IDL API implementation.
  Element* scrollSource();
  String orientation();
  String startScrollOffset();
  String endScrollOffset();
  void timeRange(DoubleOrScrollTimelineAutoKeyword&);
  String fill();

  // Returns the Node that should actually have the ScrollableArea (if one
  // exists). This can differ from |scrollSource| when |scroll_source_| is the
  // Document's scrollingElement, and it may be null if the document was removed
  // before the ScrollTimeline was created.
  Node* ResolvedScrollSource() const { return resolved_scroll_source_; }

  ScrollDirection GetOrientation() const { return orientation_; }
  Timing::FillMode GetFillMode() const { return fill_; }

  void GetCurrentAndMaxOffset(const LayoutBox*,
                              double& current_offset,
                              double& max_offset) const;
  void ResolveScrollStartAndEnd(const LayoutBox*,
                                double max_offset,
                                double& resolved_start_scroll_offset,
                                double& resolved_end_scroll_offset) const;

  // Must be called when this ScrollTimeline is attached/detached from an
  // animation.
  void AttachAnimation();
  void DetachAnimation();

  void Trace(blink::Visitor*) override;

  // For the AnimationWorklet origin trial, we need to automatically composite
  // elements that are targets of ScrollTimelines (http://crbug.com/776533). We
  // expose a static lookup method to enable this.
  //
  // TODO(crbug.com/839341): Remove once WorkletAnimations can run on main.
  static bool HasActiveScrollTimeline(Node* node);

 private:
  // Use |scroll_source_| only to implement the web-exposed API but use
  // resolved_scroll_source_ to actually access the scroll related properties.
  Member<Element> scroll_source_;
  Member<Node> resolved_scroll_source_;

  ScrollDirection orientation_;
  Member<CSSPrimitiveValue> start_scroll_offset_;
  Member<CSSPrimitiveValue> end_scroll_offset_;
  double time_range_;
  Timing::FillMode fill_;
};

DEFINE_TYPE_CASTS(ScrollTimeline,
                  AnimationTimeline,
                  value,
                  value->IsScrollTimeline(),
                  value.IsScrollTimeline());

}  // namespace blink

#endif