Chromium Code Reviews| Index: ui/gfx/geometry/size_f.h |
| diff --git a/ui/gfx/geometry/size_f.h b/ui/gfx/geometry/size_f.h |
| index 9d1bb5d7e3ed6c18d4ff3d3193a510ae85c70c48..e7ca382c5ff3bf873c7d502b11b0196aae5ed63d 100644 |
| --- a/ui/gfx/geometry/size_f.h |
| +++ b/ui/gfx/geometry/size_f.h |
| @@ -9,17 +9,22 @@ |
| #include <string> |
| #include "base/compiler_specific.h" |
| +#include "base/gtest_prod_util.h" |
| #include "ui/gfx/geometry/size.h" |
| #include "ui/gfx/gfx_export.h" |
| namespace gfx { |
| +FORWARD_DECLARE_TEST(SizeTest, TrivialDimensionTests); |
| +FORWARD_DECLARE_TEST(SizeTest, ClampsToZero); |
| +FORWARD_DECLARE_TEST(SizeTest, ConsistentClamping); |
| + |
| // A floating version of gfx::Size. |
| class GFX_EXPORT SizeF { |
| public: |
| constexpr SizeF() : width_(0.f), height_(0.f) {} |
| constexpr SizeF(float width, float height) |
| - : width_(width >= 0 ? width : 0), height_(height >= 0 ? height : 0) {} |
| + : width_(clamp(width)), height_(clamp(height)) {} |
| constexpr explicit SizeF(const Size& size) |
| : SizeF(static_cast<float>(size.width()), |
| @@ -28,8 +33,8 @@ class GFX_EXPORT SizeF { |
| constexpr float width() const { return width_; } |
| constexpr float height() const { return height_; } |
| - void set_width(float width) { width_ = fmaxf(0, width); } |
| - void set_height(float height) { height_ = fmaxf(0, height); } |
| + void set_width(float width) { width_ = clamp(width); } |
| + void set_height(float height) { height_ = clamp(height); } |
| float GetArea() const; |
| @@ -43,7 +48,7 @@ class GFX_EXPORT SizeF { |
| void SetToMin(const SizeF& other); |
| void SetToMax(const SizeF& other); |
| - bool IsEmpty() const { return !width() || !height(); } |
| + bool IsEmpty() const { return !height_ || !width_; } |
|
danakj
2017/03/29 19:10:52
this doesn't seem like a related change anymore, I
Peter Mayo
2017/03/29 20:14:48
Done.
|
| void Scale(float scale) { |
| Scale(scale, scale); |
| @@ -56,8 +61,14 @@ class GFX_EXPORT SizeF { |
| std::string ToString() const; |
| private: |
| + FRIEND_TEST_ALL_PREFIXES(SizeTest, TrivialDimensionTests); |
| + FRIEND_TEST_ALL_PREFIXES(SizeTest, ClampsToZero); |
| + FRIEND_TEST_ALL_PREFIXES(SizeTest, ConsistentClamping); |
| + |
| float width_; |
| float height_; |
| + static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon(); |
|
danakj
2017/03/29 19:10:52
nit: normally statics and methods come before prim
Peter Mayo
2017/03/29 20:14:48
Done.
|
| + static constexpr float clamp(float f) { return f > kTrivial ? f : 0.f; } |
| }; |
| inline bool operator==(const SizeF& lhs, const SizeF& rhs) { |