1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_SCROLL_SCROLL_TYPES_H_
27 #define THIRD_PARTY_BLINK_RENDERER_CORE_SCROLL_SCROLL_TYPES_H_
28
29 #include "third_party/blink/public/common/input/web_gesture_event.h"
30 #include "third_party/blink/public/mojom/input/scroll_direction.mojom-blink.h"
31 #include "third_party/blink/public/mojom/scroll/scroll_enums.mojom-blink.h"
32 #include "third_party/blink/public/mojom/scroll/scroll_into_view_params.mojom-blink.h"
33 #include "third_party/blink/renderer/platform/geometry/float_point.h"
34 #include "third_party/blink/renderer/platform/geometry/float_size.h"
35 #include "third_party/blink/renderer/platform/wtf/assertions.h"
36
37 namespace blink {
38
39 // A ScrollOffset represents an offset from the scroll origin of a
40 // ScrollableArea. Note that "scroll origin" is not the same as the layout
41 // concept of "location", nor is it necessarily coincident with the top/left of
42 // the ScrollableArea's overflow rect. See core/layout/README.md for more
43 // information.
44 typedef FloatSize ScrollOffset;
45
ToScrollOffset(const FloatPoint & p)46 inline ScrollOffset ToScrollOffset(const FloatPoint& p) {
47 return ScrollOffset(p.X(), p.Y());
48 }
49
ToScrollOffset(const gfx::PointF & p)50 inline ScrollOffset ToScrollOffset(const gfx::PointF& p) {
51 return ScrollOffset(p.x(), p.y());
52 }
53
54 using ui::ScrollGranularity;
55
56 enum ScrollDirectionPhysical {
57 kScrollUp,
58 kScrollDown,
59 kScrollLeft,
60 kScrollRight
61 };
62
63 // An explicit scroll is one that was requested by the user or the webpage.
64 // An implicit scroll is a side effect of a layout change.
IsExplicitScrollType(mojom::blink::ScrollType scroll_type)65 inline bool IsExplicitScrollType(mojom::blink::ScrollType scroll_type) {
66 return scroll_type == mojom::blink::ScrollType::kUser ||
67 scroll_type == mojom::blink::ScrollType::kProgrammatic ||
68 scroll_type == mojom::blink::ScrollType::kCompositor ||
69 scroll_type == mojom::blink::ScrollType::kSequenced;
70 }
71
72 // Convert logical scroll direction to physical. Physical scroll directions are
73 // unaffected.
ToPhysicalDirection(mojom::blink::ScrollDirection direction,bool is_vertical,bool is_flipped)74 inline ScrollDirectionPhysical ToPhysicalDirection(
75 mojom::blink::ScrollDirection direction,
76 bool is_vertical,
77 bool is_flipped) {
78 switch (direction) {
79 case mojom::blink::ScrollDirection::kScrollBlockDirectionBackward: {
80 if (is_vertical) {
81 if (!is_flipped)
82 return kScrollUp;
83 return kScrollDown;
84 }
85 if (!is_flipped)
86 return kScrollLeft;
87 return kScrollRight;
88 }
89 case mojom::blink::ScrollDirection::kScrollBlockDirectionForward: {
90 if (is_vertical) {
91 if (!is_flipped)
92 return kScrollDown;
93 return kScrollUp;
94 }
95 if (!is_flipped)
96 return kScrollRight;
97 return kScrollLeft;
98 }
99 case mojom::blink::ScrollDirection::kScrollInlineDirectionBackward: {
100 if (is_vertical) {
101 if (!is_flipped)
102 return kScrollLeft;
103 return kScrollRight;
104 }
105 if (!is_flipped)
106 return kScrollUp;
107 return kScrollDown;
108 }
109 case mojom::blink::ScrollDirection::kScrollInlineDirectionForward: {
110 if (is_vertical) {
111 if (!is_flipped)
112 return kScrollRight;
113 return kScrollLeft;
114 }
115 if (!is_flipped)
116 return kScrollDown;
117 return kScrollUp;
118 }
119 // Direction is already physical
120 case mojom::blink::ScrollDirection::kScrollUpIgnoringWritingMode:
121 return kScrollUp;
122 case mojom::blink::ScrollDirection::kScrollDownIgnoringWritingMode:
123 return kScrollDown;
124 case mojom::blink::ScrollDirection::kScrollLeftIgnoringWritingMode:
125 return kScrollLeft;
126 case mojom::blink::ScrollDirection::kScrollRightIgnoringWritingMode:
127 return kScrollRight;
128 default:
129 NOTREACHED();
130 break;
131 }
132 return kScrollUp;
133 }
134
ToScrollDirection(ScrollDirectionPhysical direction)135 inline mojom::blink::ScrollDirection ToScrollDirection(
136 ScrollDirectionPhysical direction) {
137 switch (direction) {
138 case kScrollUp:
139 return mojom::blink::ScrollDirection::kScrollUpIgnoringWritingMode;
140 case kScrollDown:
141 return mojom::blink::ScrollDirection::kScrollDownIgnoringWritingMode;
142 case kScrollLeft:
143 return mojom::blink::ScrollDirection::kScrollLeftIgnoringWritingMode;
144 case kScrollRight:
145 return mojom::blink::ScrollDirection::kScrollRightIgnoringWritingMode;
146 default:
147 NOTREACHED();
148 break;
149 }
150 return mojom::blink::ScrollDirection::kScrollUpIgnoringWritingMode;
151 }
152
153 enum ScrollInertialPhase {
154 kScrollInertialPhaseUnknown,
155 kScrollInertialPhaseNonMomentum,
156 kScrollInertialPhaseMomentum
157 };
158
159 enum ScrollbarOrientation { kHorizontalScrollbar, kVerticalScrollbar };
160
161 enum ScrollOrientation { kHorizontalScroll, kVerticalScroll };
162
163 typedef unsigned ScrollbarControlState;
164
165 enum ScrollbarControlStateMask {
166 kActiveScrollbarState = 1,
167 kEnabledScrollbarState = 1 << 1,
168 kPressedScrollbarState = 1 << 2
169 };
170
171 enum ScrollbarPart {
172 kNoPart = 0,
173 kBackButtonStartPart = 1,
174 kForwardButtonStartPart = 1 << 1, // For custom scrollbars only.
175 kBackTrackPart = 1 << 2,
176 kThumbPart = 1 << 3,
177 kForwardTrackPart = 1 << 4,
178 kBackButtonEndPart = 1 << 5, // For custom scrollbars only.
179 kForwardButtonEndPart = 1 << 6,
180 kScrollbarBGPart = 1 << 7, // For custom scrollbars only.
181 kTrackBGPart = 1 << 8,
182 kAllParts = 0xffffffff
183 };
184
185 enum ScrollbarOverlayColorTheme {
186 kScrollbarOverlayColorThemeDark,
187 kScrollbarOverlayColorThemeLight
188 };
189
190 // The result of an attempt to scroll. If didScroll is true, then
191 // unusedScrollDelta gives the amount of the scroll delta that was not consumed
192 // by scrolling.
193 struct ScrollResult {
194 STACK_ALLOCATED();
195
196 public:
ScrollResultScrollResult197 explicit ScrollResult()
198 : did_scroll_x(false),
199 did_scroll_y(false),
200 unused_scroll_delta_x(0),
201 unused_scroll_delta_y(0) {}
ScrollResultScrollResult202 ScrollResult(bool did_scroll_x,
203 bool did_scroll_y,
204 float unused_scroll_delta_x,
205 float unused_scroll_delta_y)
206 : did_scroll_x(did_scroll_x),
207 did_scroll_y(did_scroll_y),
208 unused_scroll_delta_x(unused_scroll_delta_x),
209 unused_scroll_delta_y(unused_scroll_delta_y) {}
210
DidScrollScrollResult211 bool DidScroll() { return did_scroll_x || did_scroll_y; }
212
213 bool did_scroll_x;
214 bool did_scroll_y;
215
216 // In pixels.
217 float unused_scroll_delta_x;
218 float unused_scroll_delta_y;
219 };
220
ToScrollDelta(ScrollbarOrientation orientation,float delta)221 inline ScrollOffset ToScrollDelta(ScrollbarOrientation orientation,
222 float delta) {
223 return orientation == kHorizontalScrollbar ? ScrollOffset(delta, 0.0f)
224 : ScrollOffset(0.0f, delta);
225 }
226
ToScrollDelta(ScrollDirectionPhysical dir,float delta)227 inline ScrollOffset ToScrollDelta(ScrollDirectionPhysical dir, float delta) {
228 if (dir == kScrollUp || dir == kScrollLeft)
229 delta = -delta;
230
231 return (dir == kScrollLeft || dir == kScrollRight) ? ScrollOffset(delta, 0)
232 : ScrollOffset(0, delta);
233 }
234
235 } // namespace blink
236
237 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_SCROLL_SCROLL_TYPES_H_
238