diff options
author | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-23 16:51:47 +0000 |
---|---|---|
committer | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-23 16:51:47 +0000 |
commit | d40305001bfd61bef7e118c8580895f68d36b163 (patch) | |
tree | 05a4a731233c5cb3deb56306019231e22d474deb /ui/gfx/rect_base_impl.h | |
parent | ea75f30985b5b24627bed4c613a1ef403235aa02 (diff) | |
download | chromium_src-d40305001bfd61bef7e118c8580895f68d36b163.zip chromium_src-d40305001bfd61bef7e118c8580895f68d36b163.tar.gz chromium_src-d40305001bfd61bef7e118c8580895f68d36b163.tar.bz2 |
Make gfx::Rect class operations consistently mutate the class they are called on.
Currently some methods mutate the class, and some return a new value, requiring
API users to know what kind of method they are calling each time, and making
inconsistent code. For example:
gfx::Rect rect;
rect.Inset(1, 1, 1, 1);
rect = rect.Intersect(other_rect);
rect.Offset(1, 1);
Instead of:
gfx::Rect rect;
rect.Inset(1, 1, 1, 1);
rect.Intersect(other_rect);
rect.Offset(1, 1);
We could go either way - making the class immutable or all methods return a new
instance - but I believe it is better to instead make all methods mutate the
class. This allows for shorter lines of code by avoiding having to repeat the
object's name twice in order to change it.
This patch changes gfx::Rect classes and all the callsites that uses these
methods. It should make no change in behaviour, so no new tests added.
R=sky
BUG=147395
Review URL: https://codereview.chromium.org/11110004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163579 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/rect_base_impl.h')
-rw-r--r-- | ui/gfx/rect_base_impl.h | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/ui/gfx/rect_base_impl.h b/ui/gfx/rect_base_impl.h index ab71f42..291929d 100644 --- a/ui/gfx/rect_base_impl.h +++ b/ui/gfx/rect_base_impl.h @@ -165,8 +165,13 @@ template<typename Class, typename SizeClass, typename InsetsClass, typename Type> -Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect( - const Class& rect) const { +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect( + const Class& rect) { + if (IsEmpty() || rect.IsEmpty()) { + SetRect(0, 0, 0, 0); + return; + } + Type rx = std::max(x(), rect.x()); Type ry = std::max(y(), rect.y()); Type rr = std::min(right(), rect.right()); @@ -175,7 +180,7 @@ Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Intersect( if (rx >= rr || ry >= rb) rx = ry = rr = rb = 0; // non-intersecting - return Class(rx, ry, rr - rx, rb - ry); + SetRect(rx, ry, rr - rx, rb - ry); } template<typename Class, @@ -183,20 +188,21 @@ template<typename Class, typename SizeClass, typename InsetsClass, typename Type> -Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Union( - const Class& rect) const { - // special case empty rects... - if (IsEmpty()) - return rect; +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Union( + const Class& rect) { + if (IsEmpty()) { + *this = rect; + return; + } if (rect.IsEmpty()) - return *static_cast<const Class*>(this); + return; Type rx = std::min(x(), rect.x()); Type ry = std::min(y(), rect.y()); Type rr = std::max(right(), rect.right()); Type rb = std::max(bottom(), rect.bottom()); - return Class(rx, ry, rr - rx, rb - ry); + SetRect(rx, ry, rr - rx, rb - ry); } template<typename Class, @@ -204,13 +210,14 @@ template<typename Class, typename SizeClass, typename InsetsClass, typename Type> -Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( - const Class& rect) const { - // boundary cases: +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( + const Class& rect) { if (!Intersects(rect)) - return *static_cast<const Class*>(this); - if (rect.Contains(*static_cast<const Class*>(this))) - return Class(); + return; + if (rect.Contains(*static_cast<const Class*>(this))) { + SetRect(0, 0, 0, 0); + return; + } Type rx = x(); Type ry = y(); @@ -232,7 +239,7 @@ Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Subtract( rb = rect.y(); } } - return Class(rx, ry, rr - rx, rb - ry); + SetRect(rx, ry, rr - rx, rb - ry); } template<typename Class, @@ -240,15 +247,15 @@ template<typename Class, typename SizeClass, typename InsetsClass, typename Type> -Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit( - const Class& rect) const { +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::AdjustToFit( + const Class& rect) { Type new_x = x(); Type new_y = y(); Type new_width = width(); Type new_height = height(); AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); - return Class(new_x, new_y, new_width, new_height); + SetRect(new_x, new_y, new_width, new_height); } template<typename Class, @@ -266,13 +273,13 @@ template<typename Class, typename SizeClass, typename InsetsClass, typename Type> -Class RectBase<Class, PointClass, SizeClass, InsetsClass, Type>::Center( - const SizeClass& size) const { +void RectBase<Class, PointClass, SizeClass, InsetsClass, Type>:: + ClampToCenteredSize(const SizeClass& size) { Type new_width = std::min(width(), size.width()); Type new_height = std::min(height(), size.height()); Type new_x = x() + (width() - new_width) / 2; Type new_y = y() + (height() - new_height) / 2; - return Class(new_x, new_y, new_width, new_height); + SetRect(new_x, new_y, new_width, new_height); } template<typename Class, |