OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/animation/ScrollTimeline.h" | |
6 | |
7 #include "core/dom/ExceptionCode.h" | |
8 #include "core/layout/LayoutBox.h" | |
9 #include "core/paint/PaintLayerScrollableArea.h" | |
10 | |
11 namespace blink { | |
12 | |
13 namespace { | |
14 bool StringToScrollDirection(String scroll_direction, | |
15 ScrollTimeline::ScrollDirection* result) { | |
16 // TODO(smcgruer): Support 'auto' value. | |
17 if (scroll_direction == "block") { | |
18 *result = ScrollTimeline::Block; | |
19 return true; | |
20 } | |
21 if (scroll_direction == "inline") { | |
22 *result = ScrollTimeline::Inline; | |
23 return true; | |
24 } | |
25 return false; | |
26 } | |
27 } // namespace | |
28 | |
29 ScrollTimeline* ScrollTimeline::Create(Document& document, | |
30 ScrollTimelineOptions options, | |
31 ExceptionState& exception_state) { | |
32 Element* scroll_source = options.scrollSource() ? options.scrollSource() | |
33 : document.scrollingElement(); | |
34 | |
35 ScrollDirection orientation; | |
36 if (!StringToScrollDirection(options.orientation(), &orientation)) { | |
37 exception_state.ThrowDOMException(kNotSupportedError, | |
38 "Invalid orientation"); | |
39 return nullptr; | |
40 } | |
41 | |
42 // TODO(smcgruer): Support 'auto' value. | |
43 if (options.timeRange().isScrollTimelineAutoKeyword()) { | |
44 exception_state.ThrowDOMException( | |
45 kNotSupportedError, "'auto' value for timeRange not yet supported"); | |
46 return nullptr; | |
47 } | |
48 | |
49 return new ScrollTimeline(document, scroll_source, orientation, | |
50 options.timeRange().getAsDouble()); | |
51 } | |
52 | |
53 ScrollTimeline::ScrollTimeline(const Document& document, | |
54 Element* scroll_source, | |
55 ScrollDirection orientation, | |
56 double time_range) | |
57 : AnimationTimeline(const_cast<Document*>(&document), nullptr), | |
58 scroll_source_(scroll_source), | |
59 orientation_(orientation), | |
60 time_range_(time_range) {} | |
alancutter (OOO until 2018)
2017/06/08 05:00:31
We should assert that the RuntimeEnabledFeature fl
smcgruer
2017/06/08 14:43:47
Done.
| |
61 | |
62 double ScrollTimeline::currentTime(bool& is_null) { | |
63 // 1. If scrollSource does not currently have a CSS layout box, or if its | |
64 // layout box is not a scroll container, return an unresolved time value. | |
65 LayoutBox* layout_box = scroll_source_->GetLayoutBox(); | |
66 if (!layout_box || !layout_box->HasOverflowClip()) { | |
67 is_null = false; | |
68 return std::numeric_limits<double>::quiet_NaN(); | |
69 } | |
70 | |
71 // 2. Otherwise, let current scroll offset be the current scroll offset of | |
72 // scrollSource in the direction specified by orientation. | |
73 | |
74 // Depending on the writing-mode and direction, the scroll origin shifts and | |
75 // the scroll offset may be negative. The easiest way to deal with this is to | |
76 // use only the magnitude of the scroll offset, and compare it to (max-offset | |
77 // - min_offset). | |
78 PaintLayerScrollableArea* scrollable_area = layout_box->GetScrollableArea(); | |
79 // Using the absolute value of the scroll offset only makes sense if either | |
80 // the max or min scroll offset for a given axis is 0. This should be | |
81 // guaranteed by the scroll origin code, but these DCHECKs ensure that. | |
82 DCHECK(scrollable_area->MaximumScrollOffset().Height() == 0 || | |
83 scrollable_area->MinimumScrollOffset().Height() == 0); | |
84 DCHECK(scrollable_area->MaximumScrollOffset().Width() == 0 || | |
85 scrollable_area->MinimumScrollOffset().Width() == 0); | |
86 ScrollOffset scroll_offset = scrollable_area->GetScrollOffset(); | |
87 ScrollOffset scroll_dimensions = scrollable_area->MaximumScrollOffset() - | |
88 scrollable_area->MinimumScrollOffset(); | |
89 | |
90 double current_offset; | |
91 double max_offset; | |
92 bool is_horizontal = layout_box->IsHorizontalWritingMode(); | |
93 if (orientation_ == Block) { | |
94 current_offset = | |
95 is_horizontal ? scroll_offset.Height() : scroll_offset.Width(); | |
96 max_offset = | |
97 is_horizontal ? scroll_dimensions.Height() : scroll_dimensions.Width(); | |
98 } else { | |
99 current_offset = | |
100 is_horizontal ? scroll_offset.Width() : scroll_offset.Height(); | |
101 max_offset = | |
102 is_horizontal ? scroll_dimensions.Width() : scroll_dimensions.Height(); | |
103 } | |
104 | |
105 // 3. If current scroll offset is less than startScrollOffset, return an | |
106 // unresolved time value if fill is none or forwards, or 0 otherwise. | |
107 // TODO(smcgruer): Implement |startScrollOffset| and |fill|. | |
108 | |
109 // 4. If current scroll offset is greater than or equal to endScrollOffset, | |
110 // return an unresolved time value if fill is none or backwards, or the | |
111 // effective time range otherwise. | |
112 // TODO(smcgruer): Implement |endScrollOffset| and |fill|. | |
113 | |
114 // 5. Return the result of evaluating the following expression: | |
115 // ((current scroll offset - startScrollOffset) / | |
116 // (endScrollOffset - startScrollOffset)) * effective time range | |
117 | |
118 is_null = false; | |
119 return (std::abs(current_offset) / max_offset) * time_range_; | |
120 } | |
121 | |
122 double ScrollTimeline::currentTime() { | |
123 bool ignored; | |
124 return currentTime(ignored); | |
125 } | |
126 | |
127 double ScrollTimeline::CurrentTimeInternal(bool& is_null) { | |
128 return currentTime(is_null); | |
129 } | |
130 | |
131 double ScrollTimeline::CurrentTimeInternal() { | |
132 bool ignored; | |
133 return currentTime(ignored); | |
134 } | |
135 | |
136 Element* ScrollTimeline::scrollSource() { | |
137 return scroll_source_.Get(); | |
138 } | |
139 | |
140 String ScrollTimeline::orientation() { | |
141 switch (orientation_) { | |
142 case Block: | |
143 return "block"; | |
144 case Inline: | |
145 return "inline"; | |
146 default: | |
147 NOTREACHED(); | |
148 return ""; | |
149 } | |
150 } | |
151 | |
152 void ScrollTimeline::timeRange(DoubleOrScrollTimelineAutoKeyword& result) { | |
153 result.setDouble(time_range_); | |
154 } | |
155 | |
156 DEFINE_TRACE(ScrollTimeline) { | |
157 visitor->Trace(scroll_source_); | |
158 AnimationTimeline::Trace(visitor); | |
159 } | |
160 | |
161 } // namespace blink | |
OLD | NEW |