Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Unified Diff: ui/gfx/geometry/scroll_offset.h

Issue 584503005: Make scroll offset type of float in cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: link crbug to TODO Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/geometry/safe_integer_conversions_unittest.cc ('k') | ui/gfx/geometry/scroll_offset.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/scroll_offset.h
diff --git a/ui/gfx/geometry/scroll_offset.h b/ui/gfx/geometry/scroll_offset.h
new file mode 100644
index 0000000000000000000000000000000000000000..496b5f4299e3e0de978b9ffc119124367c68b29a
--- /dev/null
+++ b/ui/gfx/geometry/scroll_offset.h
@@ -0,0 +1,125 @@
+// Copyright 2014 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 UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
+#define UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
+
+#include <iosfwd>
+#include <string>
+
+#include "ui/gfx/geometry/safe_integer_conversions.h"
+#include "ui/gfx/geometry/vector2d.h"
+#include "ui/gfx/gfx_export.h"
+
+namespace gfx {
+
+class GFX_EXPORT ScrollOffset {
+ public:
+ ScrollOffset() : x_(0), y_(0) {}
+ ScrollOffset(double x, double y) : x_(x), y_(y) {}
+ explicit ScrollOffset(const Vector2dF& v) : x_(v.x()), y_(v.y()) {}
+ explicit ScrollOffset(const Vector2d& v) : x_(v.x()), y_(v.y()) {}
+
+ double x() const { return x_; }
+ void set_x(double x) { x_ = x; }
+
+ double y() const { return y_; }
+ void set_y(double y) { y_ = y; }
+
+ // True if both components are 0.
+ bool IsZero() const {
+ return x_ == 0 && y_ == 0;
+ }
+
+ // Add the components of the |other| ScrollOffset to the current ScrollOffset.
+ void Add(const ScrollOffset& other) {
+ x_ += other.x_;
+ y_ += other.y_;
+ }
+
+ // Subtract the components of the |other| ScrollOffset from the current
+ // ScrollOffset.
+ void Subtract(const ScrollOffset& other) {
+ x_ -= other.x_;
+ y_ -= other.y_;
+ }
+
+ Vector2dF DeltaFrom(const ScrollOffset& v) const {
+ return Vector2dF(x_ - v.x(), y_ - v.y());
+ }
+
+ void operator+=(const ScrollOffset& other) { Add(other); }
+ void operator-=(const ScrollOffset& other) { Subtract(other); }
+
+ void SetToMin(const ScrollOffset& other) {
+ x_ = x_ <= other.x_ ? x_ : other.x_;
+ y_ = y_ <= other.y_ ? y_ : other.y_;
+ }
+
+ void SetToMax(const ScrollOffset& other) {
+ x_ = x_ >= other.x_ ? x_ : other.x_;
+ y_ = y_ >= other.y_ ? y_ : other.y_;
+ }
+
+ void Scale(double scale) { Scale(scale, scale); }
+ void Scale(double x_scale, double y_scale) {
+ x_ *= x_scale;
+ y_ *= y_scale;
+ }
+
+ std::string ToString() const;
+
+ private:
+ double x_;
+ double y_;
+};
+
+inline bool operator==(const ScrollOffset& lhs, const ScrollOffset& rhs) {
+ return lhs.x() == rhs.x() && lhs.y() == rhs.y();
+}
+
+inline bool operator!=(const ScrollOffset& lhs, const ScrollOffset& rhs) {
+ return lhs.x() != rhs.x() || lhs.y() != rhs.y();
+}
+
+inline ScrollOffset operator-(const ScrollOffset& v) {
+ return ScrollOffset(-v.x(), -v.y());
+}
+
+inline ScrollOffset operator+(const ScrollOffset& lhs,
+ const ScrollOffset& rhs) {
+ ScrollOffset result = lhs;
+ result.Add(rhs);
+ return result;
+}
+
+inline ScrollOffset operator-(const ScrollOffset& lhs,
+ const ScrollOffset& rhs) {
+ ScrollOffset result = lhs;
+ result.Add(-rhs);
+ return result;
+}
+
+inline Vector2d ScrollOffsetToFlooredVector2d(const ScrollOffset& v) {
+ return Vector2d(ToFlooredInt(v.x()), ToFlooredInt(v.y()));
+}
+
+inline Vector2dF ScrollOffsetToVector2dF(const ScrollOffset& v) {
+ return Vector2dF(v.x(), v.y());
+}
+
+inline ScrollOffset ScrollOffsetWithDelta(const ScrollOffset& offset,
+ const Vector2dF& delta) {
+ return ScrollOffset(offset.x() + delta.x(),
+ offset.y() + delta.y());
+}
+
+// This is declared here for use in gtest-based unit tests but is defined in
+// the gfx_test_support target. Depend on that to use this in your unit test.
+// This should not be used in production code - call ToString() instead.
+void PrintTo(const ScrollOffset& scroll_offset, ::std::ostream* os);
+
+} // namespace gfx
+
+#endif // UI_GFX_GEOMETRY_SCROLL_OFFSET_H_
« no previous file with comments | « ui/gfx/geometry/safe_integer_conversions_unittest.cc ('k') | ui/gfx/geometry/scroll_offset.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698