Skip to content

Commit

Permalink
Prepare for removing multi-parameter gfx::Rect[F]::Inset/Outset
Browse files Browse the repository at this point in the history
- Add TODO(crbug.com/1302500) for removal of the multi-parameter
  Inset/Outset functions.
- Move TLBR/VH into InsetsOutsetsBase[F] so that we can have
  gfx::Outset[F]::TLBR/VH

Bug: 1302500
Change-Id: I322702d03628561112c3009c50a9194cd2810873
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3563605
Reviewed-by: Scott Violet <sky@chromium.org>
Commit-Queue: Xianzhu Wang <wangxianzhu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#988207}
  • Loading branch information
wangxianzhu authored and Chromium LUCI CQ committed Apr 2, 2022
1 parent 740afe1 commit aff187c
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 83 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions ui/gfx/geometry/insets.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ class GEOMETRY_EXPORT Insets : public InsetsOutsetsBase<Insets> {
public:
using InsetsOutsetsBase::InsetsOutsetsBase;

// These are for Chromium UI code to replace the original usages of
// Insets::Insets(top, left, bottom, right) and Insets(vertical, horizontal).
static constexpr Insets TLBR(int top, int left, int bottom, int right) {
return Insets().set_top_bottom(top, bottom).set_left_right(left, right);
}
static constexpr Insets VH(int vertical, int horizontal) {
return Insets()
.set_top_bottom(vertical, vertical)
.set_left_right(horizontal, horizontal);
}

// Conversion from Insets to Outsets negates all components.
Outsets ToOutsets() const;

Expand Down
17 changes: 0 additions & 17 deletions ui/gfx/geometry/insets_f.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ class GEOMETRY_EXPORT InsetsF : public InsetsOutsetsFBase<InsetsF> {
public:
using InsetsOutsetsFBase::InsetsOutsetsFBase;

// These are for Chromium UI code to replace the original usages of
// InsetsF(top, left, bottom, right) and InsetsF(vertical, horizontal).
static constexpr inline InsetsF TLBR(float top,
float left,
float bottom,
float right) {
return InsetsF().set_top(top).set_left(left).set_bottom(bottom).set_right(
right);
}
static constexpr inline InsetsF VH(float vertical, float horizontal) {
return InsetsF()
.set_top(vertical)
.set_left(horizontal)
.set_bottom(vertical)
.set_right(horizontal);
}

// Conversion from InsetsF to OutsetsF negates all components.
OutsetsF ToOutsets() const;
};
Expand Down
12 changes: 12 additions & 0 deletions ui/gfx/geometry/insets_outsets_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ class InsetsOutsetsBase {
return *static_cast<T*>(this);
}

// In addition to the above, we can also use the following methods to
// construct Insets/Outsets.
// TLBR() is for Chomium UI code. We should not use it in blink code because
// the order of parameters is different from the normal orders used in blink.
// Blink code can use the above setters and VH().
static constexpr T TLBR(int top, int left, int bottom, int right) {
return T().set_top_bottom(top, bottom).set_left_right(left, right);
}
static constexpr T VH(int vertical, int horizontal) {
return TLBR(vertical, horizontal, vertical, horizontal);
}

// Sets each side to the maximum of the side and the corresponding side of
// |other|.
void SetToMax(const T& other) {
Expand Down
15 changes: 15 additions & 0 deletions ui/gfx/geometry/insets_outsets_f_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ class InsetsOutsetsFBase {
return *static_cast<T*>(this);
}

// In addition to the above, we can also use the following methods to
// construct InsetsF/OutsetsF.
// TLBR() is for Chomium UI code. We should not use it in blink code because
// the order of parameters is different from the normal orders used in blink.
// Blink code can use the above setters and VH().
static constexpr inline T TLBR(float top,
float left,
float bottom,
float right) {
return T().set_top(top).set_left(left).set_bottom(bottom).set_right(right);
}
static constexpr inline T VH(float vertical, float horizontal) {
return TLBR(vertical, horizontal, vertical, horizontal);
}

// Sets each side to the maximum of the side and the corresponding side of
// |other|.
void SetToMax(const T& other) {
Expand Down
16 changes: 3 additions & 13 deletions ui/gfx/geometry/rect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,9 @@ void Rect::AdjustForSaturatedBottom(int bottom) {
}

void Rect::Inset(const Insets& insets) {
Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
}

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::ClampSub(width(), base::ClampAdd(left, right)));
set_height(base::ClampSub(height(), base::ClampAdd(top, bottom)));
}

void Rect::Outset(const Outsets& outsets) {
Outset(outsets.left(), outsets.top(), outsets.right(), outsets.bottom());
origin_ += Vector2d(insets.left(), insets.top());
set_width(base::ClampSub(width(), insets.width()));
set_height(base::ClampSub(height(), insets.height()));
}

void Rect::Offset(const Vector2d& distance) {
Expand Down
32 changes: 17 additions & 15 deletions ui/gfx/geometry/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_conversions.h"
#include "build/build_config.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/outsets.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
Expand All @@ -32,9 +34,6 @@ typedef struct CGRect CGRect;

namespace gfx {

class Insets;
class Outsets;

class GEOMETRY_EXPORT Rect {
public:
constexpr Rect() = default;
Expand Down Expand Up @@ -144,25 +143,28 @@ class GEOMETRY_EXPORT Rect {
}

// Shrink the rectangle by |inset| on all sides.
void Inset(int inset) { Inset(inset, inset); }
// 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(int inset) { Inset(Insets(inset)); }
// 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);

// Expand the rectangle by the specified amount on each side.
// Expand the rectangle by |outset| on all sides.
void Outset(int outset) { Inset(-outset); }
// Expand the rectangle by the given |outsets|.
void Outset(const Outsets& outsets) { Inset(outsets.ToInsets()); }

// TODO(crbug.com/1302500): Remove these functions in favor of the above
// functions, because these functions are error-prone about the order of the
// parameters.
void Inset(int horizontal, int vertical) {
Inset(Insets::VH(vertical, horizontal));
}
void Outset(int horizontal, int vertical) { Inset(-horizontal, -vertical); }
void Inset(int left, int top, int right, int bottom) {
Inset(Insets::TLBR(top, left, bottom, right));
}
void Outset(int left, int top, int right, int bottom) {
Inset(-left, -top, -right, -bottom);
}
void Outset(const Outsets& outsets);

// Move the rectangle by a horizontal and vertical distance.
void Offset(int horizontal, int vertical) {
Expand Down
14 changes: 3 additions & 11 deletions ui/gfx/geometry/rect_f.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,9 @@ CGRect RectF::ToCGRect() const {
#endif

void RectF::Inset(const InsetsF& insets) {
Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
}

void RectF::Inset(float left, float top, float right, float bottom) {
origin_ += Vector2dF(left, top);
set_width(std::max(width() - left - right, 0.0f));
set_height(std::max(height() - top - bottom, 0.0f));
}

void RectF::Outset(const OutsetsF& outsets) {
Outset(outsets.left(), outsets.top(), outsets.right(), outsets.bottom());
origin_ += Vector2dF(insets.left(), insets.top());
set_width(width() - insets.width());
set_height(height() - insets.height());
}

void RectF::Offset(float horizontal, float vertical) {
Expand Down
34 changes: 18 additions & 16 deletions ui/gfx/geometry/rect_f.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <string>

#include "build/build_config.h"
#include "ui/gfx/geometry/insets_f.h"
#include "ui/gfx/geometry/outsets_f.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_f.h"
Expand All @@ -20,9 +22,6 @@ typedef struct CGRect CGRect;

namespace gfx {

class InsetsF;
class OutsetsF;

// A floating version of gfx::Rect.
class GEOMETRY_EXPORT RectF {
public:
Expand Down Expand Up @@ -89,28 +88,31 @@ class GEOMETRY_EXPORT RectF {
size_.SetSize(width, height);
}

// Shrink the rectangle by |inset| on all sides.
void Inset(float inset) { Inset(inset, inset); }
// Shrink the rectangle by a horizontal and vertical distance on all sides.
void Inset(float horizontal, float vertical) {
Inset(horizontal, vertical, horizontal, vertical);
}

// Shrink the rectangle by the given insets.
// Shrinks the rectangle by |inset| on all sides.
void Inset(float inset) { Inset(InsetsF(inset)); }
// Shrinks the rectangle by the given |insets|.
void Inset(const InsetsF& insets);

// Shrink the rectangle by the specified amount on each side.
void Inset(float left, float top, float right, float bottom);

// Expand the rectangle by the specified amount on each side.
// Expands the rectangle by |outset| on all sides.
void Outset(float outset) { Inset(-outset); }
// Expands the rectangle by the given |outsets|.
void Outset(const OutsetsF& outsets) { Inset(outsets.ToInsets()); }

// TODO(crbug.com/1302500): Remove these functions in favor of the above
// functions, because these functions are error-prone about the order of the
// parameters.
void Inset(float horizontal, float vertical) {
Inset(InsetsF::VH(vertical, horizontal));
}
void Outset(float horizontal, float vertical) {
Inset(-horizontal, -vertical);
}
void Inset(float left, float top, float right, float bottom) {
Inset(InsetsF::TLBR(top, left, bottom, right));
}
void Outset(float left, float top, float right, float bottom) {
Inset(-left, -top, -right, -bottom);
}
void Outset(const OutsetsF& outsets);

// Move the rectangle by a horizontal and vertical distance.
void Offset(float horizontal, float vertical);
Expand Down

0 comments on commit aff187c

Please sign in to comment.