1 // Copyright 2014 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 #ifndef UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
6 #define UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
7
8 #include <iosfwd>
9 #include <string>
10
11 #include "ui/gfx/geometry/safe_integer_conversions.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 #include "ui/gfx/gfx_export.h"
14
15 namespace gfx {
16
17 // TODO(szager): Reconcile terminology with blink. An 'offset' here corresponds
18 // to a 'position' in blink. In blink, 'offset' means something else. See
19 // third_party/WebKit/Source/core/layout/README.md for more information.
20
21 class GFX_EXPORT ScrollOffset {
22 public:
ScrollOffset()23 ScrollOffset() : x_(0), y_(0) {}
ScrollOffset(float x,float y)24 ScrollOffset(float x, float y) : x_(x), y_(y) {}
ScrollOffset(const Vector2dF & v)25 explicit ScrollOffset(const Vector2dF& v) : x_(v.x()), y_(v.y()) {}
ScrollOffset(const Vector2d & v)26 explicit ScrollOffset(const Vector2d& v) : x_(v.x()), y_(v.y()) {}
27
x()28 float x() const { return x_; }
set_x(float x)29 void set_x(float x) { x_ = x; }
30
y()31 float y() const { return y_; }
set_y(float y)32 void set_y(float y) { y_ = y; }
33
34 // True if both components are 0.
IsZero()35 bool IsZero() const {
36 return x_ == 0 && y_ == 0;
37 }
38
39 // Add the components of the |other| ScrollOffset to the current ScrollOffset.
Add(const ScrollOffset & other)40 void Add(const ScrollOffset& other) {
41 x_ += other.x_;
42 y_ += other.y_;
43 }
44
45 // Subtract the components of the |other| ScrollOffset from the current
46 // ScrollOffset.
Subtract(const ScrollOffset & other)47 void Subtract(const ScrollOffset& other) {
48 x_ -= other.x_;
49 y_ -= other.y_;
50 }
51
DeltaFrom(const ScrollOffset & v)52 Vector2dF DeltaFrom(const ScrollOffset& v) const {
53 return Vector2dF(x_ - v.x(), y_ - v.y());
54 }
55
56 void operator+=(const ScrollOffset& other) { Add(other); }
57 void operator-=(const ScrollOffset& other) { Subtract(other); }
58
SetToMin(const ScrollOffset & other)59 void SetToMin(const ScrollOffset& other) {
60 x_ = x_ <= other.x_ ? x_ : other.x_;
61 y_ = y_ <= other.y_ ? y_ : other.y_;
62 }
63
SetToMax(const ScrollOffset & other)64 void SetToMax(const ScrollOffset& other) {
65 x_ = x_ >= other.x_ ? x_ : other.x_;
66 y_ = y_ >= other.y_ ? y_ : other.y_;
67 }
68
Scale(float scale)69 void Scale(float scale) { Scale(scale, scale); }
Scale(float x_scale,float y_scale)70 void Scale(float x_scale, float y_scale) {
71 x_ *= x_scale;
72 y_ *= y_scale;
73 }
74
75 std::string ToString() const;
76
77 private:
78 float x_;
79 float y_;
80 };
81
82 inline bool operator==(const ScrollOffset& lhs, const ScrollOffset& rhs) {
83 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
84 }
85
86 inline bool operator!=(const ScrollOffset& lhs, const ScrollOffset& rhs) {
87 return lhs.x() != rhs.x() || lhs.y() != rhs.y();
88 }
89
90 inline ScrollOffset operator-(const ScrollOffset& v) {
91 return ScrollOffset(-v.x(), -v.y());
92 }
93
94 inline ScrollOffset operator+(const ScrollOffset& lhs,
95 const ScrollOffset& rhs) {
96 ScrollOffset result = lhs;
97 result.Add(rhs);
98 return result;
99 }
100
101 inline ScrollOffset operator-(const ScrollOffset& lhs,
102 const ScrollOffset& rhs) {
103 ScrollOffset result = lhs;
104 result.Add(-rhs);
105 return result;
106 }
107
ScrollOffsetToFlooredVector2d(const ScrollOffset & v)108 inline Vector2d ScrollOffsetToFlooredVector2d(const ScrollOffset& v) {
109 return Vector2d(ToFlooredInt(v.x()), ToFlooredInt(v.y()));
110 }
111
ScrollOffsetToVector2dF(const ScrollOffset & v)112 inline Vector2dF ScrollOffsetToVector2dF(const ScrollOffset& v) {
113 return Vector2dF(v.x(), v.y());
114 }
115
ScrollOffsetWithDelta(const ScrollOffset & offset,const Vector2dF & delta)116 inline ScrollOffset ScrollOffsetWithDelta(const ScrollOffset& offset,
117 const Vector2dF& delta) {
118 return ScrollOffset(offset.x() + delta.x(),
119 offset.y() + delta.y());
120 }
121
122 // This is declared here for use in gtest-based unit tests but is defined in
123 // the //ui/gfx:test_support target. Depend on that to use this in your unit
124 // test. This should not be used in production code - call ToString() instead.
125 void PrintTo(const ScrollOffset& scroll_offset, ::std::ostream* os);
126
127 } // namespace gfx
128
129 #endif // UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
130