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

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

Issue 506803004: gfx: Don't template gfx::Rect and gfx::RectF (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: inlines: rm-rectbase Created 6 years, 2 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/BUILD.gn ('k') | ui/gfx/geometry/rect.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/rect.h
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h
index cf386c55a8b505d55e1443cba2fc196386c74ea1..fed202d2bd30b769f91ee041c70224f618495010 100644
--- a/ui/gfx/geometry/rect.h
+++ b/ui/gfx/geometry/rect.h
@@ -17,7 +17,6 @@
#include <string>
#include "ui/gfx/geometry/point.h"
-#include "ui/gfx/geometry/rect_base.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
@@ -34,18 +33,14 @@ namespace gfx {
class Insets;
-class GFX_EXPORT Rect
- : public RectBase<Rect, Point, Size, Insets, Vector2d, int> {
+class GFX_EXPORT Rect {
public:
- Rect() : RectBase<Rect, Point, Size, Insets, Vector2d, int>(Point()) {}
-
- Rect(int width, int height)
- : RectBase<Rect, Point, Size, Insets, Vector2d, int>
- (Size(width, height)) {}
-
+ Rect() {}
+ Rect(int width, int height) : size_(width, height) {}
Rect(int x, int y, int width, int height)
- : RectBase<Rect, Point, Size, Insets, Vector2d, int>
- (Point(x, y), Size(width, height)) {}
+ : origin_(x, y), size_(width, height) {}
+ explicit Rect(const Size& size) : size_(size) {}
+ Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {}
#if defined(OS_WIN)
explicit Rect(const RECT& r);
@@ -53,12 +48,6 @@ class GFX_EXPORT Rect
explicit Rect(const CGRect& r);
#endif
- explicit Rect(const gfx::Size& size)
- : RectBase<Rect, Point, Size, Insets, Vector2d, int>(size) {}
-
- Rect(const gfx::Point& origin, const gfx::Size& size)
- : RectBase<Rect, Point, Size, Insets, Vector2d, int>(origin, size) {}
-
~Rect() {}
#if defined(OS_WIN)
@@ -73,7 +62,132 @@ class GFX_EXPORT Rect
return RectF(origin().x(), origin().y(), size().width(), size().height());
}
+ int x() const { return origin_.x(); }
+ void set_x(int x) { origin_.set_x(x); }
+
+ int y() const { return origin_.y(); }
+ void set_y(int y) { origin_.set_y(y); }
+
+ int width() const { return size_.width(); }
+ void set_width(int width) { size_.set_width(width); }
+
+ int height() const { return size_.height(); }
+ void set_height(int height) { size_.set_height(height); }
+
+ const Point& origin() const { return origin_; }
+ void set_origin(const Point& origin) { origin_ = origin; }
+
+ const Size& size() const { return size_; }
+ void set_size(const Size& size) { size_ = size; }
+
+ int right() const { return x() + width(); }
+ int bottom() const { return y() + height(); }
+
+ Point top_right() const { return Point(right(), y()); }
+ Point bottom_left() const { return Point(x(), bottom()); }
+ Point bottom_right() const { return Point(right(), bottom()); }
+
+ Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
+
+ void SetRect(int x, int y, int width, int height) {
+ origin_.SetPoint(x, y);
+ size_.SetSize(width, height);
+ }
+
+ // Shrink the rectangle by a horizontal and vertical distance on all sides.
+ void Inset(int horizontal, int vertical) {
+ Inset(horizontal, vertical, horizontal, vertical);
+ }
+
+ // Shrink the rectangle by the given insets.
+ void Inset(const Insets& insets);
+
+ // Shrink the rectangle by the specified amount on each side.
+ void Inset(int left, int top, int right, int bottom);
+
+ // Move the rectangle by a horizontal and vertical distance.
+ void Offset(int horizontal, int vertical);
+ void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
+ void operator+=(const Vector2d& offset);
+ void operator-=(const Vector2d& offset);
+
+ Insets InsetsFrom(const Rect& inner) const;
+
+ // Returns true if the area of the rectangle is zero.
+ bool IsEmpty() const { return size_.IsEmpty(); }
+
+ // A rect is less than another rect if its origin is less than
+ // the other rect's origin. If the origins are equal, then the
+ // shortest rect is less than the other. If the origin and the
+ // height are equal, then the narrowest rect is less than.
+ // This comparison is required to use Rects in sets, or sorted
+ // vectors.
+ bool operator<(const Rect& other) const;
+
+ // Returns true if the point identified by point_x and point_y falls inside
+ // this rectangle. The point (x, y) is inside the rectangle, but the
+ // point (x + width, y + height) is not.
+ bool Contains(int point_x, int point_y) const;
+
+ // Returns true if the specified point is contained by this rectangle.
+ bool Contains(const Point& point) const {
+ return Contains(point.x(), point.y());
+ }
+
+ // Returns true if this rectangle contains the specified rectangle.
+ bool Contains(const Rect& rect) const;
+
+ // Returns true if this rectangle intersects the specified rectangle.
+ // An empty rectangle doesn't intersect any rectangle.
+ bool Intersects(const Rect& rect) const;
+
+ // Computes the intersection of this rectangle with the given rectangle.
+ void Intersect(const Rect& rect);
+
+ // Computes the union of this rectangle with the given rectangle. The union
+ // is the smallest rectangle containing both rectangles.
+ void Union(const Rect& rect);
+
+ // Computes the rectangle resulting from subtracting |rect| from |*this|,
+ // i.e. the bounding rect of |Region(*this) - Region(rect)|.
+ void Subtract(const Rect& rect);
+
+ // Fits as much of the receiving rectangle into the supplied rectangle as
+ // possible, becoming the result. For example, if the receiver had
+ // a x-location of 2 and a width of 4, and the supplied rectangle had
+ // an x-location of 0 with a width of 5, the returned rectangle would have
+ // an x-location of 1 with a width of 4.
+ void AdjustToFit(const Rect& rect);
+
+ // Returns the center of this rectangle.
+ Point CenterPoint() const;
+
+ // Becomes a rectangle that has the same center point but with a size capped
+ // at given |size|.
+ void ClampToCenteredSize(const Size& size);
+
+ // Splits |this| in two halves, |left_half| and |right_half|.
+ void SplitVertically(Rect* left_half, Rect* right_half) const;
+
+ // Returns true if this rectangle shares an entire edge (i.e., same width or
+ // same height) with the given rectangle, and the rectangles do not overlap.
+ bool SharesEdgeWith(const Rect& rect) const;
+
+ // Returns the manhattan distance from the rect to the point. If the point is
+ // inside the rect, returns 0.
+ int ManhattanDistanceToPoint(const Point& point) const;
+
+ // Returns the manhattan distance between the contents of this rect and the
+ // contents of the given rect. That is, if the intersection of the two rects
+ // is non-empty then the function returns 0. If the rects share a side, it
+ // returns the smallest non-zero value appropriate for int.
+ int ManhattanInternalDistance(const Rect& rect) const;
+
std::string ToString() const;
+
+ private:
+ gfx::Point origin_;
+ gfx::Size size_;
};
inline bool operator==(const Rect& lhs, const Rect& rhs) {
@@ -131,10 +245,6 @@ inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
return ScaleToEnclosedRect(rect, scale, scale);
}
-#if !defined(COMPILER_MSVC) && !defined(__native_client__)
-extern template class RectBase<Rect, Point, Size, Insets, Vector2d, int>;
-#endif
-
// 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.
« no previous file with comments | « ui/gfx/geometry/BUILD.gn ('k') | ui/gfx/geometry/rect.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698