xref: /aosp_15_r20/external/libchrome/ui/gfx/geometry/insets_f.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef UI_GFX_GEOMETRY_INSETS_F_H_
6*635a8641SAndroid Build Coastguard Worker #define UI_GFX_GEOMETRY_INSETS_F_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <string>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "ui/gfx/gfx_export.h"
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker namespace gfx {
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker // A floating point version of gfx::Insets.
15*635a8641SAndroid Build Coastguard Worker class GFX_EXPORT InsetsF {
16*635a8641SAndroid Build Coastguard Worker  public:
InsetsF()17*635a8641SAndroid Build Coastguard Worker   constexpr InsetsF() : top_(0.f), left_(0.f), bottom_(0.f), right_(0.f) {}
InsetsF(float all)18*635a8641SAndroid Build Coastguard Worker   constexpr explicit InsetsF(float all)
19*635a8641SAndroid Build Coastguard Worker       : top_(all), left_(all), bottom_(all), right_(all) {}
InsetsF(float vertical,float horizontal)20*635a8641SAndroid Build Coastguard Worker   constexpr InsetsF(float vertical, float horizontal)
21*635a8641SAndroid Build Coastguard Worker       : top_(vertical),
22*635a8641SAndroid Build Coastguard Worker         left_(horizontal),
23*635a8641SAndroid Build Coastguard Worker         bottom_(vertical),
24*635a8641SAndroid Build Coastguard Worker         right_(horizontal) {}
InsetsF(float top,float left,float bottom,float right)25*635a8641SAndroid Build Coastguard Worker   constexpr InsetsF(float top, float left, float bottom, float right)
26*635a8641SAndroid Build Coastguard Worker       : top_(top), left_(left), bottom_(bottom), right_(right) {}
27*635a8641SAndroid Build Coastguard Worker 
top()28*635a8641SAndroid Build Coastguard Worker   constexpr float top() const { return top_; }
left()29*635a8641SAndroid Build Coastguard Worker   constexpr float left() const { return left_; }
bottom()30*635a8641SAndroid Build Coastguard Worker   constexpr float bottom() const { return bottom_; }
right()31*635a8641SAndroid Build Coastguard Worker   constexpr float right() const { return right_; }
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker   // Returns the total width taken up by the insets, which is the sum of the
34*635a8641SAndroid Build Coastguard Worker   // left and right insets.
width()35*635a8641SAndroid Build Coastguard Worker   constexpr float width() const { return left_ + right_; }
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker   // Returns the total height taken up by the insets, which is the sum of the
38*635a8641SAndroid Build Coastguard Worker   // top and bottom insets.
height()39*635a8641SAndroid Build Coastguard Worker   constexpr float height() const { return top_ + bottom_; }
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker   // Returns true if the insets are empty.
IsEmpty()42*635a8641SAndroid Build Coastguard Worker   bool IsEmpty() const { return width() == 0.f && height() == 0.f; }
43*635a8641SAndroid Build Coastguard Worker 
Set(float top,float left,float bottom,float right)44*635a8641SAndroid Build Coastguard Worker   void Set(float top, float left, float bottom, float right) {
45*635a8641SAndroid Build Coastguard Worker     top_ = top;
46*635a8641SAndroid Build Coastguard Worker     left_ = left;
47*635a8641SAndroid Build Coastguard Worker     bottom_ = bottom;
48*635a8641SAndroid Build Coastguard Worker     right_ = right;
49*635a8641SAndroid Build Coastguard Worker   }
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker   bool operator==(const InsetsF& insets) const {
52*635a8641SAndroid Build Coastguard Worker     return top_ == insets.top_ && left_ == insets.left_ &&
53*635a8641SAndroid Build Coastguard Worker            bottom_ == insets.bottom_ && right_ == insets.right_;
54*635a8641SAndroid Build Coastguard Worker   }
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker   bool operator!=(const InsetsF& insets) const {
57*635a8641SAndroid Build Coastguard Worker     return !(*this == insets);
58*635a8641SAndroid Build Coastguard Worker   }
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker   void operator+=(const InsetsF& insets) {
61*635a8641SAndroid Build Coastguard Worker     top_ += insets.top_;
62*635a8641SAndroid Build Coastguard Worker     left_ += insets.left_;
63*635a8641SAndroid Build Coastguard Worker     bottom_ += insets.bottom_;
64*635a8641SAndroid Build Coastguard Worker     right_ += insets.right_;
65*635a8641SAndroid Build Coastguard Worker   }
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker   void operator-=(const InsetsF& insets) {
68*635a8641SAndroid Build Coastguard Worker     top_ -= insets.top_;
69*635a8641SAndroid Build Coastguard Worker     left_ -= insets.left_;
70*635a8641SAndroid Build Coastguard Worker     bottom_ -= insets.bottom_;
71*635a8641SAndroid Build Coastguard Worker     right_ -= insets.right_;
72*635a8641SAndroid Build Coastguard Worker   }
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker   InsetsF operator-() const {
75*635a8641SAndroid Build Coastguard Worker     return InsetsF(-top_, -left_, -bottom_, -right_);
76*635a8641SAndroid Build Coastguard Worker   }
77*635a8641SAndroid Build Coastguard Worker 
Scale(float scale)78*635a8641SAndroid Build Coastguard Worker   InsetsF Scale(float scale) const {
79*635a8641SAndroid Build Coastguard Worker     return InsetsF(scale * top(), scale * left(), scale * bottom(),
80*635a8641SAndroid Build Coastguard Worker                    scale * right());
81*635a8641SAndroid Build Coastguard Worker   }
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker   // Returns a string representation of the insets.
84*635a8641SAndroid Build Coastguard Worker   std::string ToString() const;
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker  private:
87*635a8641SAndroid Build Coastguard Worker   float top_;
88*635a8641SAndroid Build Coastguard Worker   float left_;
89*635a8641SAndroid Build Coastguard Worker   float bottom_;
90*635a8641SAndroid Build Coastguard Worker   float right_;
91*635a8641SAndroid Build Coastguard Worker };
92*635a8641SAndroid Build Coastguard Worker 
93*635a8641SAndroid Build Coastguard Worker inline InsetsF operator+(InsetsF lhs, const InsetsF& rhs) {
94*635a8641SAndroid Build Coastguard Worker   lhs += rhs;
95*635a8641SAndroid Build Coastguard Worker   return lhs;
96*635a8641SAndroid Build Coastguard Worker }
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker inline InsetsF operator-(InsetsF lhs, const InsetsF& rhs) {
99*635a8641SAndroid Build Coastguard Worker   lhs -= rhs;
100*635a8641SAndroid Build Coastguard Worker   return lhs;
101*635a8641SAndroid Build Coastguard Worker }
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker }  // namespace gfx
104*635a8641SAndroid Build Coastguard Worker 
105*635a8641SAndroid Build Coastguard Worker #endif  // UI_GFX_GEOMETRY_INSETS_F_H_
106