Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / geometry / scroll_offset.h
blob496b5f4299e3e0de978b9ffc119124367c68b29a
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.
5 #ifndef UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
6 #define UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
8 #include <iosfwd>
9 #include <string>
11 #include "ui/gfx/geometry/safe_integer_conversions.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 #include "ui/gfx/gfx_export.h"
15 namespace gfx {
17 class GFX_EXPORT ScrollOffset {
18 public:
19 ScrollOffset() : x_(0), y_(0) {}
20 ScrollOffset(double x, double y) : x_(x), y_(y) {}
21 explicit ScrollOffset(const Vector2dF& v) : x_(v.x()), y_(v.y()) {}
22 explicit ScrollOffset(const Vector2d& v) : x_(v.x()), y_(v.y()) {}
24 double x() const { return x_; }
25 void set_x(double x) { x_ = x; }
27 double y() const { return y_; }
28 void set_y(double y) { y_ = y; }
30 // True if both components are 0.
31 bool IsZero() const {
32 return x_ == 0 && y_ == 0;
35 // Add the components of the |other| ScrollOffset to the current ScrollOffset.
36 void Add(const ScrollOffset& other) {
37 x_ += other.x_;
38 y_ += other.y_;
41 // Subtract the components of the |other| ScrollOffset from the current
42 // ScrollOffset.
43 void Subtract(const ScrollOffset& other) {
44 x_ -= other.x_;
45 y_ -= other.y_;
48 Vector2dF DeltaFrom(const ScrollOffset& v) const {
49 return Vector2dF(x_ - v.x(), y_ - v.y());
52 void operator+=(const ScrollOffset& other) { Add(other); }
53 void operator-=(const ScrollOffset& other) { Subtract(other); }
55 void SetToMin(const ScrollOffset& other) {
56 x_ = x_ <= other.x_ ? x_ : other.x_;
57 y_ = y_ <= other.y_ ? y_ : other.y_;
60 void SetToMax(const ScrollOffset& other) {
61 x_ = x_ >= other.x_ ? x_ : other.x_;
62 y_ = y_ >= other.y_ ? y_ : other.y_;
65 void Scale(double scale) { Scale(scale, scale); }
66 void Scale(double x_scale, double y_scale) {
67 x_ *= x_scale;
68 y_ *= y_scale;
71 std::string ToString() const;
73 private:
74 double x_;
75 double y_;
78 inline bool operator==(const ScrollOffset& lhs, const ScrollOffset& rhs) {
79 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
82 inline bool operator!=(const ScrollOffset& lhs, const ScrollOffset& rhs) {
83 return lhs.x() != rhs.x() || lhs.y() != rhs.y();
86 inline ScrollOffset operator-(const ScrollOffset& v) {
87 return ScrollOffset(-v.x(), -v.y());
90 inline ScrollOffset operator+(const ScrollOffset& lhs,
91 const ScrollOffset& rhs) {
92 ScrollOffset result = lhs;
93 result.Add(rhs);
94 return result;
97 inline ScrollOffset operator-(const ScrollOffset& lhs,
98 const ScrollOffset& rhs) {
99 ScrollOffset result = lhs;
100 result.Add(-rhs);
101 return result;
104 inline Vector2d ScrollOffsetToFlooredVector2d(const ScrollOffset& v) {
105 return Vector2d(ToFlooredInt(v.x()), ToFlooredInt(v.y()));
108 inline Vector2dF ScrollOffsetToVector2dF(const ScrollOffset& v) {
109 return Vector2dF(v.x(), v.y());
112 inline ScrollOffset ScrollOffsetWithDelta(const ScrollOffset& offset,
113 const Vector2dF& delta) {
114 return ScrollOffset(offset.x() + delta.x(),
115 offset.y() + delta.y());
118 // This is declared here for use in gtest-based unit tests but is defined in
119 // the gfx_test_support target. Depend on that to use this in your unit test.
120 // This should not be used in production code - call ToString() instead.
121 void PrintTo(const ScrollOffset& scroll_offset, ::std::ostream* os);
123 } // namespace gfx
125 #endif // UI_GFX_GEOMETRY_SCROLL_OFFSET_H_