summaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
Diffstat (limited to 'ui')
-rw-r--r--ui/gfx/geometry/insets_f.h5
-rw-r--r--ui/gfx/geometry/mojo/BUILD.gn2
-rw-r--r--ui/gfx/geometry/mojo/DEPS4
-rw-r--r--ui/gfx/geometry/mojo/geometry.mojom12
-rw-r--r--ui/gfx/geometry/mojo/geometry.typemap2
-rw-r--r--ui/gfx/geometry/mojo/geometry_struct_traits.h13
-rw-r--r--ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc50
-rw-r--r--ui/gfx/geometry/point.h18
-rw-r--r--ui/gfx/geometry/rect.cc18
-rw-r--r--ui/gfx/geometry/rect.h39
-rw-r--r--ui/gfx/geometry/rect_f.cc52
-rw-r--r--ui/gfx/geometry/scroll_offset.cc15
-rw-r--r--ui/gfx/geometry/scroll_offset.h129
-rw-r--r--ui/gfx/geometry/size.cc6
-rw-r--r--ui/gfx/geometry/vector2d.cc10
-rw-r--r--ui/gfx/geometry/vector2d.h9
-rw-r--r--ui/gfx/geometry/vector2d_f.h6
-rw-r--r--ui/gfx/range/BUILD.gn4
-rw-r--r--ui/gfx/range/mojo/DEPS4
-rw-r--r--ui/gfx/range/mojo/range_struct_traits_unittest.cc15
-rw-r--r--ui/gfx/range/range.h5
21 files changed, 313 insertions, 105 deletions
diff --git a/ui/gfx/geometry/insets_f.h b/ui/gfx/geometry/insets_f.h
index 30c2ff2b79..3230287957 100644
--- a/ui/gfx/geometry/insets_f.h
+++ b/ui/gfx/geometry/insets_f.h
@@ -75,6 +75,11 @@ class GFX_EXPORT InsetsF {
return InsetsF(-top_, -left_, -bottom_, -right_);
}
+ InsetsF Scale(float scale) const {
+ return InsetsF(scale * top(), scale * left(), scale * bottom(),
+ scale * right());
+ }
+
// Returns a string representation of the insets.
std::string ToString() const;
diff --git a/ui/gfx/geometry/mojo/BUILD.gn b/ui/gfx/geometry/mojo/BUILD.gn
index f6cda9d3be..2d0e1efbcf 100644
--- a/ui/gfx/geometry/mojo/BUILD.gn
+++ b/ui/gfx/geometry/mojo/BUILD.gn
@@ -10,6 +10,8 @@ mojom("mojo") {
sources = [
"geometry.mojom",
]
+
+ check_includes_blink = false
}
mojom("test_interfaces") {
diff --git a/ui/gfx/geometry/mojo/DEPS b/ui/gfx/geometry/mojo/DEPS
deleted file mode 100644
index 3ad6543823..0000000000
--- a/ui/gfx/geometry/mojo/DEPS
+++ /dev/null
@@ -1,4 +0,0 @@
-include_rules = [
- "+mojo/public",
- "+ui/gfx/geometry",
-]
diff --git a/ui/gfx/geometry/mojo/geometry.mojom b/ui/gfx/geometry/mojo/geometry.mojom
index 97143866da..9b8342f7cc 100644
--- a/ui/gfx/geometry/mojo/geometry.mojom
+++ b/ui/gfx/geometry/mojo/geometry.mojom
@@ -4,11 +4,18 @@
module gfx.mojom;
+// Don't make backwards-incompatible changes to this definition!
+// It's used in PageState serialization, so backwards incompatible changes
+// would cause stored PageState objects to be un-parseable.
struct Point {
int32 x;
int32 y;
};
+// Don't make backwards-incompatible changes to this definition!
+// It's used in PageState serialization, so backwards incompatible changes
+// would cause stored PageState objects to be un-parseable. Please contact the
+// page state serialization owners before making such a change.
struct PointF {
float x;
float y;
@@ -61,3 +68,8 @@ struct Vector2dF {
float x;
float y;
};
+
+struct ScrollOffset {
+ float x;
+ float y;
+};
diff --git a/ui/gfx/geometry/mojo/geometry.typemap b/ui/gfx/geometry/mojo/geometry.typemap
index 686ea05cfb..52775304c6 100644
--- a/ui/gfx/geometry/mojo/geometry.typemap
+++ b/ui/gfx/geometry/mojo/geometry.typemap
@@ -10,6 +10,7 @@ public_headers = [
"//ui/gfx/geometry/rect.h",
"//ui/gfx/geometry/rect_f.h",
"//ui/gfx/geometry/safe_integer_conversions.h",
+ "//ui/gfx/geometry/scroll_offset.h",
"//ui/gfx/geometry/insets.h",
"//ui/gfx/geometry/vector2d.h",
"//ui/gfx/geometry/vector2d_f.h",
@@ -29,4 +30,5 @@ type_mappings = [
"gfx.mojom.InsetsF=gfx::InsetsF",
"gfx.mojom.Vector2d=gfx::Vector2d",
"gfx.mojom.Vector2dF=gfx::Vector2dF",
+ "gfx.mojom.ScrollOffset=gfx::ScrollOffset",
]
diff --git a/ui/gfx/geometry/mojo/geometry_struct_traits.h b/ui/gfx/geometry/mojo/geometry_struct_traits.h
index a31d7385b6..2fcd37cce7 100644
--- a/ui/gfx/geometry/mojo/geometry_struct_traits.h
+++ b/ui/gfx/geometry/mojo/geometry_struct_traits.h
@@ -12,6 +12,7 @@
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
+#include "ui/gfx/geometry/scroll_offset.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/vector2d.h"
@@ -141,6 +142,18 @@ struct StructTraits<gfx::mojom::Vector2dFDataView, gfx::Vector2dF> {
}
};
+template <>
+struct StructTraits<gfx::mojom::ScrollOffsetDataView, gfx::ScrollOffset> {
+ static float x(const gfx::ScrollOffset& v) { return v.x(); }
+ static float y(const gfx::ScrollOffset& v) { return v.y(); }
+ static bool Read(gfx::mojom::ScrollOffsetDataView data,
+ gfx::ScrollOffset* out) {
+ out->set_x(data.x());
+ out->set_y(data.y());
+ return true;
+ }
+};
+
} // namespace mojo
#endif // UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_
diff --git a/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc b/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc
index d59579443a..2be935bd46 100644
--- a/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc
+++ b/ui/gfx/geometry/mojo/geometry_struct_traits_unittest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <utility>
+
#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -19,54 +21,52 @@ class GeometryStructTraitsTest : public testing::Test,
protected:
mojom::GeometryTraitsTestServicePtr GetTraitsTestProxy() {
- return traits_test_bindings_.CreateInterfacePtrAndBind(this);
+ mojom::GeometryTraitsTestServicePtr proxy;
+ traits_test_bindings_.AddBinding(this, mojo::MakeRequest(&proxy));
+ return proxy;
}
private:
// GeometryTraitsTestService:
- void EchoPoint(const Point& p, const EchoPointCallback& callback) override {
- callback.Run(p);
+ void EchoPoint(const Point& p, EchoPointCallback callback) override {
+ std::move(callback).Run(p);
}
- void EchoPointF(const PointF& p,
- const EchoPointFCallback& callback) override {
- callback.Run(p);
+ void EchoPointF(const PointF& p, EchoPointFCallback callback) override {
+ std::move(callback).Run(p);
}
- void EchoSize(const Size& s, const EchoSizeCallback& callback) override {
- callback.Run(s);
+ void EchoSize(const Size& s, EchoSizeCallback callback) override {
+ std::move(callback).Run(s);
}
- void EchoSizeF(const SizeF& s, const EchoSizeFCallback& callback) override {
- callback.Run(s);
+ void EchoSizeF(const SizeF& s, EchoSizeFCallback callback) override {
+ std::move(callback).Run(s);
}
- void EchoRect(const Rect& r, const EchoRectCallback& callback) override {
- callback.Run(r);
+ void EchoRect(const Rect& r, EchoRectCallback callback) override {
+ std::move(callback).Run(r);
}
- void EchoRectF(const RectF& r, const EchoRectFCallback& callback) override {
- callback.Run(r);
+ void EchoRectF(const RectF& r, EchoRectFCallback callback) override {
+ std::move(callback).Run(r);
}
- void EchoInsets(const Insets& i,
- const EchoInsetsCallback& callback) override {
- callback.Run(i);
+ void EchoInsets(const Insets& i, EchoInsetsCallback callback) override {
+ std::move(callback).Run(i);
}
- void EchoInsetsF(const InsetsF& i,
- const EchoInsetsFCallback& callback) override {
- callback.Run(i);
+ void EchoInsetsF(const InsetsF& i, EchoInsetsFCallback callback) override {
+ std::move(callback).Run(i);
}
- void EchoVector2d(const Vector2d& v,
- const EchoVector2dCallback& callback) override {
- callback.Run(v);
+ void EchoVector2d(const Vector2d& v, EchoVector2dCallback callback) override {
+ std::move(callback).Run(v);
}
void EchoVector2dF(const Vector2dF& v,
- const EchoVector2dFCallback& callback) override {
- callback.Run(v);
+ EchoVector2dFCallback callback) override {
+ std::move(callback).Run(v);
}
base::MessageLoop loop_;
diff --git a/ui/gfx/geometry/point.h b/ui/gfx/geometry/point.h
index bb248d5432..b1ba5065de 100644
--- a/ui/gfx/geometry/point.h
+++ b/ui/gfx/geometry/point.h
@@ -9,7 +9,7 @@
#include <string>
#include <tuple>
-#include "base/numerics/saturated_arithmetic.h"
+#include "base/numerics/clamped_math.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/gfx_export.h"
@@ -56,18 +56,18 @@ class GFX_EXPORT Point {
}
void Offset(int delta_x, int delta_y) {
- x_ = base::SaturatedAddition(x_, delta_x);
- y_ = base::SaturatedAddition(y_, delta_y);
+ x_ = base::ClampAdd(x_, delta_x);
+ y_ = base::ClampAdd(y_, delta_y);
}
void operator+=(const Vector2d& vector) {
- x_ = base::SaturatedAddition(x_, vector.x());
- y_ = base::SaturatedAddition(y_, vector.y());
+ x_ = base::ClampAdd(x_, vector.x());
+ y_ = base::ClampAdd(y_, vector.y());
}
void operator-=(const Vector2d& vector) {
- x_ = base::SaturatedSubtraction(x_, vector.x());
- y_ = base::SaturatedSubtraction(y_, vector.y());
+ x_ = base::ClampSub(x_, vector.x());
+ y_ = base::ClampSub(y_, vector.y());
}
void SetToMin(const Point& other);
@@ -116,8 +116,8 @@ inline Point operator-(const Point& lhs, const Vector2d& rhs) {
}
inline Vector2d operator-(const Point& lhs, const Point& rhs) {
- return Vector2d(base::SaturatedSubtraction(lhs.x(), rhs.x()),
- base::SaturatedSubtraction(lhs.y(), rhs.y()));
+ return Vector2d(base::ClampSub(lhs.x(), rhs.x()),
+ base::ClampSub(lhs.y(), rhs.y()));
}
inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) {
diff --git a/ui/gfx/geometry/rect.cc b/ui/gfx/geometry/rect.cc
index b5ceda5829..63c0bbf04f 100644
--- a/ui/gfx/geometry/rect.cc
+++ b/ui/gfx/geometry/rect.cc
@@ -15,7 +15,7 @@
#endif
#include "base/logging.h"
-#include "base/numerics/saturated_arithmetic.h"
+#include "base/numerics/clamped_math.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/insets.h"
@@ -69,8 +69,8 @@ static void SaturatedClampRange(int min, int max, int* origin, int* span) {
return;
}
- int effective_span = base::SaturatedSubtraction(max, min);
- int span_loss = base::SaturatedSubtraction(max, min + effective_span);
+ int effective_span = base::ClampSub(max, min);
+ int span_loss = base::ClampSub(max, min + effective_span);
// If the desired width is within the limits of ints, we can just
// use the simple computations to represent the range precisely.
@@ -83,12 +83,12 @@ static void SaturatedClampRange(int min, int max, int* origin, int* span) {
// Now we have to approximate. If one of min or max is close enough
// to zero we choose to represent that one precisely. The other side is
// probably practically "infinite", so we move it.
- if (base::SaturatedAbsolute(max) < std::numeric_limits<int>::max() / 2) {
+ constexpr unsigned kMaxDimension = std::numeric_limits<int>::max() / 2;
+ if (base::SafeUnsignedAbs(max) < kMaxDimension) {
// Maintain origin + span == max.
*span = effective_span;
*origin = max - effective_span;
- } else if (base::SaturatedAbsolute(min) <
- std::numeric_limits<int>::max() / 2) {
+ } else if (base::SafeUnsignedAbs(min) < kMaxDimension) {
// Maintain origin == min.
*span = effective_span;
*origin = min;
@@ -116,10 +116,8 @@ void Rect::Inset(int left, int top, int right, int bottom) {
origin_ += Vector2d(left, top);
// left+right might overflow/underflow, but width() - (left+right) might
// overflow as well.
- set_width(base::SaturatedSubtraction(width(),
- base::SaturatedAddition(left, right)));
- set_height(base::SaturatedSubtraction(height(),
- base::SaturatedAddition(top, bottom)));
+ set_width(base::ClampSub(width(), base::ClampAdd(left, right)));
+ set_height(base::ClampSub(height(), base::ClampAdd(top, bottom)));
}
void Rect::Offset(int horizontal, int vertical) {
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h
index 1858d44d2c..3109912b6a 100644
--- a/ui/gfx/geometry/rect.h
+++ b/ui/gfx/geometry/rect.h
@@ -61,12 +61,14 @@ class GFX_EXPORT Rect {
#endif
constexpr int x() const { return origin_.x(); }
+ // Sets the X position while preserving the width.
void set_x(int x) {
origin_.set_x(x);
size_.set_width(GetClampedValue(x, width()));
}
constexpr int y() const { return origin_.y(); }
+ // Sets the Y position while preserving the height.
void set_y(int y) {
origin_.set_y(y);
size_.set_height(GetClampedValue(y, height()));
@@ -227,7 +229,7 @@ class GFX_EXPORT Rect {
// Clamp the size to avoid integer overflow in bottom() and right().
// This returns the width given an origin and a width.
- // TODO(enne): this should probably use base::SaturatedAddition, but that
+ // TODO(enne): this should probably use base::ClampAdd, but that
// function is not a constexpr.
static constexpr int GetClampedValue(int origin, int size) {
return AddWouldOverflow(origin, size)
@@ -340,6 +342,41 @@ inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
return ScaleToEnclosedRect(rect, scale, scale);
}
+// Scales |rect| by scaling its four corner points. If the corner points lie on
+// non-integral coordinate after scaling, their values are rounded to the
+// nearest integer.
+// This is helpful during layout when relative positions of multiple gfx::Rect
+// in a given coordinate space needs to be same after scaling as it was before
+// scaling. ie. this gives a lossless relative positioning of rects.
+inline Rect ScaleToRoundedRect(const Rect& rect, float x_scale, float y_scale) {
+ if (x_scale == 1.f && y_scale == 1.f)
+ return rect;
+
+ DCHECK(
+ base::IsValueInRangeForNumericType<int>(std::round(rect.x() * x_scale)));
+ DCHECK(
+ base::IsValueInRangeForNumericType<int>(std::round(rect.y() * y_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::round(rect.right() * x_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::round(rect.bottom() * y_scale)));
+
+ int x = static_cast<int>(std::round(rect.x() * x_scale));
+ int y = static_cast<int>(std::round(rect.y() * y_scale));
+ int r = rect.width() == 0
+ ? x
+ : static_cast<int>(std::round(rect.right() * x_scale));
+ int b = rect.height() == 0
+ ? y
+ : static_cast<int>(std::round(rect.bottom() * y_scale));
+
+ return Rect(x, y, r - x, b - y);
+}
+
+inline Rect ScaleToRoundedRect(const Rect& rect, float scale) {
+ return ScaleToRoundedRect(rect, scale, scale);
+}
+
// This is declared here for use in gtest-based unit tests but is defined in
// the //ui/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.
diff --git a/ui/gfx/geometry/rect_f.cc b/ui/gfx/geometry/rect_f.cc
index a08e384776..bb474ba63d 100644
--- a/ui/gfx/geometry/rect_f.cc
+++ b/ui/gfx/geometry/rect_f.cc
@@ -5,12 +5,7 @@
#include "ui/gfx/geometry/rect_f.h"
#include <algorithm>
-
-#if defined(OS_IOS)
-#include <CoreGraphics/CoreGraphics.h>
-#elif defined(OS_MACOSX)
-#include <ApplicationServices/ApplicationServices.h>
-#endif
+#include <limits>
#include "base/logging.h"
#include "base/strings/stringprintf.h"
@@ -18,6 +13,12 @@
#include "ui/gfx/geometry/insets_f.h"
#include "ui/gfx/geometry/safe_integer_conversions.h"
+#if defined(OS_IOS)
+#include <CoreGraphics/CoreGraphics.h>
+#elif defined(OS_MACOSX)
+#include <ApplicationServices/ApplicationServices.h>
+#endif
+
namespace gfx {
static void AdjustAlongAxis(float dst_origin,
@@ -47,8 +48,8 @@ void RectF::Inset(const InsetsF& insets) {
void RectF::Inset(float left, float top, float right, float bottom) {
origin_ += Vector2dF(left, top);
- set_width(std::max(width() - left - right, static_cast<float>(0)));
- set_height(std::max(height() - top - bottom, static_cast<float>(0)));
+ set_width(std::max(width() - left - right, 0.0f));
+ set_height(std::max(height() - top - bottom, 0.0f));
}
void RectF::Offset(float horizontal, float vertical) {
@@ -71,30 +72,27 @@ InsetsF RectF::InsetsFrom(const RectF& inner) const {
}
bool RectF::operator<(const RectF& other) const {
- if (origin_ == other.origin_) {
- if (width() == other.width()) {
- return height() < other.height();
- } else {
- return width() < other.width();
- }
- } else {
+ if (origin_ != other.origin_)
return origin_ < other.origin_;
- }
+
+ if (width() == other.width())
+ return height() < other.height();
+ return width() < other.width();
}
bool RectF::Contains(float point_x, float point_y) const {
- return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
- (point_y < bottom());
+ return point_x >= x() && point_x < right() && point_y >= y() &&
+ point_y < bottom();
}
bool RectF::Contains(const RectF& rect) const {
- return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
- rect.bottom() <= bottom());
+ return rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
+ rect.bottom() <= bottom();
}
bool RectF::Intersects(const RectF& rect) const {
- return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
- rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
+ return !IsEmpty() && !rect.IsEmpty() && rect.x() < right() &&
+ rect.right() > x() && rect.y() < bottom() && rect.bottom() > y();
}
void RectF::Intersect(const RectF& rect) {
@@ -108,8 +106,10 @@ void RectF::Intersect(const RectF& rect) {
float rr = std::min(right(), rect.right());
float rb = std::min(bottom(), rect.bottom());
- if (rx >= rr || ry >= rb)
- rx = ry = rr = rb = 0; // non-intersecting
+ if (rx >= rr || ry >= rb) {
+ SetRect(0, 0, 0, 0);
+ return;
+ }
SetRect(rx, ry, rr - rx, rb - ry);
}
@@ -133,7 +133,7 @@ void RectF::Union(const RectF& rect) {
void RectF::Subtract(const RectF& rect) {
if (!Intersects(rect))
return;
- if (rect.Contains(*static_cast<const RectF*>(this))) {
+ if (rect.Contains(*this)) {
SetRect(0, 0, 0, 0);
return;
}
@@ -212,7 +212,7 @@ float RectF::ManhattanInternalDistance(const RectF& rect) const {
RectF c(*this);
c.Union(rect);
- static const float kEpsilon = std::numeric_limits<float>::epsilon();
+ static constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
float x = std::max(0.f, c.width() - width() - rect.width() + kEpsilon);
float y = std::max(0.f, c.height() - height() - rect.height() + kEpsilon);
return x + y;
diff --git a/ui/gfx/geometry/scroll_offset.cc b/ui/gfx/geometry/scroll_offset.cc
new file mode 100644
index 0000000000..063b050aca
--- /dev/null
+++ b/ui/gfx/geometry/scroll_offset.cc
@@ -0,0 +1,15 @@
+// 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.
+
+#include "ui/gfx/geometry/scroll_offset.h"
+
+#include "base/strings/stringprintf.h"
+
+namespace gfx {
+
+std::string ScrollOffset::ToString() const {
+ return base::StringPrintf("[%f %f]", x_, y_);
+}
+
+} // namespace gfx
diff --git a/ui/gfx/geometry/scroll_offset.h b/ui/gfx/geometry/scroll_offset.h
new file mode 100644
index 0000000000..d7aa2f97d8
--- /dev/null
+++ b/ui/gfx/geometry/scroll_offset.h
@@ -0,0 +1,129 @@
+// 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 {
+
+// TODO(szager): Reconcile terminology with blink. An 'offset' here corresponds
+// to a 'position' in blink. In blink, 'offset' means something else. See
+// third_party/WebKit/Source/core/layout/README.md for more information.
+
+class GFX_EXPORT ScrollOffset {
+ public:
+ ScrollOffset() : x_(0), y_(0) {}
+ ScrollOffset(float x, float 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()) {}
+
+ float x() const { return x_; }
+ void set_x(float x) { x_ = x; }
+
+ float y() const { return y_; }
+ void set_y(float 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(float scale) { Scale(scale, scale); }
+ void Scale(float x_scale, float y_scale) {
+ x_ *= x_scale;
+ y_ *= y_scale;
+ }
+
+ std::string ToString() const;
+
+ private:
+ float x_;
+ float 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 //ui/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_
diff --git a/ui/gfx/geometry/size.cc b/ui/gfx/geometry/size.cc
index 69486723a1..781b84d7f5 100644
--- a/ui/gfx/geometry/size.cc
+++ b/ui/gfx/geometry/size.cc
@@ -12,8 +12,8 @@
#include <ApplicationServices/ApplicationServices.h>
#endif
+#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_math.h"
-#include "base/numerics/saturated_arithmetic.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/safe_integer_conversions.h"
@@ -58,8 +58,8 @@ base::CheckedNumeric<int> Size::GetCheckedArea() const {
}
void Size::Enlarge(int grow_width, int grow_height) {
- SetSize(base::SaturatedAddition(width(), grow_width),
- base::SaturatedAddition(height(), grow_height));
+ SetSize(base::ClampAdd(width(), grow_width),
+ base::ClampAdd(height(), grow_height));
}
void Size::SetToMin(const Size& other) {
diff --git a/ui/gfx/geometry/vector2d.cc b/ui/gfx/geometry/vector2d.cc
index 2b4875c39c..0ce3b20baa 100644
--- a/ui/gfx/geometry/vector2d.cc
+++ b/ui/gfx/geometry/vector2d.cc
@@ -6,7 +6,7 @@
#include <cmath>
-#include "base/numerics/saturated_arithmetic.h"
+#include "base/numerics/clamped_math.h"
#include "base/strings/stringprintf.h"
namespace gfx {
@@ -16,13 +16,13 @@ bool Vector2d::IsZero() const {
}
void Vector2d::Add(const Vector2d& other) {
- x_ = base::SaturatedAddition(other.x_, x_);
- y_ = base::SaturatedAddition(other.y_, y_);
+ x_ = base::ClampAdd(other.x_, x_);
+ y_ = base::ClampAdd(other.y_, y_);
}
void Vector2d::Subtract(const Vector2d& other) {
- x_ = base::SaturatedSubtraction(x_, other.x_);
- y_ = base::SaturatedSubtraction(y_, other.y_);
+ x_ = base::ClampSub(x_, other.x_);
+ y_ = base::ClampSub(y_, other.y_);
}
int64_t Vector2d::LengthSquared() const {
diff --git a/ui/gfx/geometry/vector2d.h b/ui/gfx/geometry/vector2d.h
index 4b45667adf..d9964532f5 100644
--- a/ui/gfx/geometry/vector2d.h
+++ b/ui/gfx/geometry/vector2d.h
@@ -39,6 +39,9 @@ class GFX_EXPORT Vector2d {
// Subtract the components of the |other| vector from the current vector.
void Subtract(const Vector2d& other);
+ constexpr bool operator==(const Vector2d& other) const {
+ return x_ == other.x_ && y_ == other.y_;
+ }
void operator+=(const Vector2d& other) { Add(other); }
void operator-=(const Vector2d& other) { Subtract(other); }
@@ -70,11 +73,7 @@ class GFX_EXPORT Vector2d {
int y_;
};
-inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) {
- return lhs.x() == rhs.x() && lhs.y() == rhs.y();
-}
-
-inline Vector2d operator-(const Vector2d& v) {
+inline constexpr Vector2d operator-(const Vector2d& v) {
return Vector2d(-v.x(), -v.y());
}
diff --git a/ui/gfx/geometry/vector2d_f.h b/ui/gfx/geometry/vector2d_f.h
index 92f7f87753..7e40ea881f 100644
--- a/ui/gfx/geometry/vector2d_f.h
+++ b/ui/gfx/geometry/vector2d_f.h
@@ -67,15 +67,15 @@ class GFX_EXPORT Vector2dF {
float y_;
};
-inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
+inline constexpr bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
return lhs.x() == rhs.x() && lhs.y() == rhs.y();
}
-inline bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) {
+inline constexpr bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) {
return !(lhs == rhs);
}
-inline Vector2dF operator-(const Vector2dF& v) {
+inline constexpr Vector2dF operator-(const Vector2dF& v) {
return Vector2dF(-v.x(), -v.y());
}
diff --git a/ui/gfx/range/BUILD.gn b/ui/gfx/range/BUILD.gn
index 0a8d8b2338..2a2568a287 100644
--- a/ui/gfx/range/BUILD.gn
+++ b/ui/gfx/range/BUILD.gn
@@ -2,7 +2,9 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-component("range") {
+import("//build/config/jumbo.gni")
+
+jumbo_component("range") {
sources = [
"gfx_range_export.h",
"range.cc",
diff --git a/ui/gfx/range/mojo/DEPS b/ui/gfx/range/mojo/DEPS
deleted file mode 100644
index 418fc69e2e..0000000000
--- a/ui/gfx/range/mojo/DEPS
+++ /dev/null
@@ -1,4 +0,0 @@
-include_rules = [
- "+mojo/public",
- "+ui/gfx/range",
-]
diff --git a/ui/gfx/range/mojo/range_struct_traits_unittest.cc b/ui/gfx/range/mojo/range_struct_traits_unittest.cc
index 70b32f3ff7..31705ca900 100644
--- a/ui/gfx/range/mojo/range_struct_traits_unittest.cc
+++ b/ui/gfx/range/mojo/range_struct_traits_unittest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <utility>
+
#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -18,18 +20,19 @@ class RangeStructTraitsTest : public testing::Test,
protected:
mojom::RangeTraitsTestServicePtr GetTraitsTestProxy() {
- return traits_test_bindings_.CreateInterfacePtrAndBind(this);
+ mojom::RangeTraitsTestServicePtr proxy;
+ traits_test_bindings_.AddBinding(this, mojo::MakeRequest(&proxy));
+ return proxy;
}
private:
// RangeTraitsTestService:
- void EchoRange(const Range& p, const EchoRangeCallback& callback) override {
- callback.Run(p);
+ void EchoRange(const Range& p, EchoRangeCallback callback) override {
+ std::move(callback).Run(p);
}
- void EchoRangeF(const RangeF& p,
- const EchoRangeFCallback& callback) override {
- callback.Run(p);
+ void EchoRangeF(const RangeF& p, EchoRangeFCallback callback) override {
+ std::move(callback).Run(p);
}
base::MessageLoop loop_;
diff --git a/ui/gfx/range/range.h b/ui/gfx/range/range.h
index e785eb6c70..5ea9e3c690 100644
--- a/ui/gfx/range/range.h
+++ b/ui/gfx/range/range.h
@@ -24,8 +24,7 @@ typedef struct _NSRange NSRange;
#endif // defined(OS_MACOSX)
#if defined(OS_WIN)
-#include <windows.h>
-#include <richedit.h>
+typedef struct _charrange CHARRANGE;
#endif
namespace gfx {
@@ -52,7 +51,7 @@ class GFX_RANGE_EXPORT Range {
#elif defined(OS_WIN)
// The |total_length| paramater should be used if the CHARRANGE is set to
// {0,-1} to indicate the whole range.
- Range(const CHARRANGE& range, LONG total_length = -1);
+ Range(const CHARRANGE& range, long total_length = -1);
#endif
// Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}.