OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_GEOMETRY_SIZE_F_H_ | 5 #ifndef UI_GFX_GEOMETRY_SIZE_F_H_ |
6 #define UI_GFX_GEOMETRY_SIZE_F_H_ | 6 #define UI_GFX_GEOMETRY_SIZE_F_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
13 #include "ui/gfx/gfx_export.h" | 13 #include "ui/gfx/gfx_export.h" |
14 | 14 |
15 namespace gfx { | 15 namespace gfx { |
16 | 16 |
17 static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon(); | |
18 | |
17 // A floating version of gfx::Size. | 19 // A floating version of gfx::Size. |
18 class GFX_EXPORT SizeF { | 20 class GFX_EXPORT SizeF { |
19 public: | 21 public: |
20 constexpr SizeF() : width_(0.f), height_(0.f) {} | 22 constexpr SizeF() : width_(0.f), height_(0.f) {} |
21 constexpr SizeF(float width, float height) | 23 constexpr SizeF(float width, float height) |
22 : width_(width >= 0 ? width : 0), height_(height >= 0 ? height : 0) {} | 24 : width_(width >= kTrivial ? width : 0.f), |
25 height_(height >= kTrivial ? height : 0.f) {} | |
23 | 26 |
24 constexpr explicit SizeF(const Size& size) | 27 constexpr explicit SizeF(const Size& size) |
25 : SizeF(static_cast<float>(size.width()), | 28 : SizeF(static_cast<float>(size.width()), |
26 static_cast<float>(size.height())) {} | 29 static_cast<float>(size.height())) {} |
27 | 30 |
28 constexpr float width() const { return width_; } | 31 constexpr float width() const { return width_; } |
29 constexpr float height() const { return height_; } | 32 constexpr float height() const { return height_; } |
30 | 33 |
31 void set_width(float width) { width_ = fmaxf(0, width); } | 34 void set_width(float width) { width_ = width >= kTrivial ? width : 0.f; } |
32 void set_height(float height) { height_ = fmaxf(0, height); } | 35 void set_height(float height) { height_ = height >= kTrivial ? height : 0.f; } |
33 | 36 |
34 float GetArea() const; | 37 float GetArea() const; |
35 | 38 |
36 void SetSize(float width, float height) { | 39 void SetSize(float width, float height) { |
37 set_width(width); | 40 set_width(width); |
38 set_height(height); | 41 set_height(height); |
39 } | 42 } |
40 | 43 |
41 void Enlarge(float grow_width, float grow_height); | 44 void Enlarge(float grow_width, float grow_height); |
42 | 45 |
43 void SetToMin(const SizeF& other); | 46 void SetToMin(const SizeF& other); |
44 void SetToMax(const SizeF& other); | 47 void SetToMax(const SizeF& other); |
45 | 48 |
46 bool IsEmpty() const { return !width() || !height(); } | 49 bool HasHeight() const { return height() > kTrivial; } |
danakj
2017/03/21 16:16:36
How would height be < kTrivial? This is now just c
Peter Mayo
2017/03/21 16:45:57
The best thing about these is that they make it cl
danakj
2017/03/21 17:42:05
I think I'm okay with clamping at the setters. But
Peter Mayo
2017/03/23 19:29:22
Dana Wrote:
Peter Mayo
2017/03/23 19:29:22
I think the decision as to whether HasHeight() is
| |
50 bool HasWidth() const { return width() > kTrivial; } | |
51 bool IsEmpty() const { return !HasHeight() || !HasWidth(); } | |
47 | 52 |
48 void Scale(float scale) { | 53 void Scale(float scale) { |
49 Scale(scale, scale); | 54 Scale(scale, scale); |
50 } | 55 } |
51 | 56 |
52 void Scale(float x_scale, float y_scale) { | 57 void Scale(float x_scale, float y_scale) { |
53 SetSize(width() * x_scale, height() * y_scale); | 58 SetSize(width() * x_scale, height() * y_scale); |
54 } | 59 } |
55 | 60 |
56 std::string ToString() const; | 61 std::string ToString() const; |
(...skipping 18 matching lines...) Expand all Loading... | |
75 } | 80 } |
76 | 81 |
77 // This is declared here for use in gtest-based unit tests but is defined in | 82 // This is declared here for use in gtest-based unit tests but is defined in |
78 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 83 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
79 // test. This should not be used in production code - call ToString() instead. | 84 // test. This should not be used in production code - call ToString() instead. |
80 void PrintTo(const SizeF& size, ::std::ostream* os); | 85 void PrintTo(const SizeF& size, ::std::ostream* os); |
81 | 86 |
82 } // namespace gfx | 87 } // namespace gfx |
83 | 88 |
84 #endif // UI_GFX_GEOMETRY_SIZE_F_H_ | 89 #endif // UI_GFX_GEOMETRY_SIZE_F_H_ |
OLD | NEW |