insets_outsets_f_base.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_GEOMETRY_INSETS_OUTSETS_F_BASE_H_
  5. #define UI_GFX_GEOMETRY_INSETS_OUTSETS_F_BASE_H_
  6. #include <string>
  7. #include "base/strings/stringprintf.h"
  8. namespace gfx {
  9. // This is the base template class of InsetsF and OutsetsF.
  10. template <typename T>
  11. class InsetsOutsetsFBase {
  12. public:
  13. constexpr InsetsOutsetsFBase() = default;
  14. constexpr explicit InsetsOutsetsFBase(float all)
  15. : top_(all), left_(all), bottom_(all), right_(all) {}
  16. constexpr float top() const { return top_; }
  17. constexpr float left() const { return left_; }
  18. constexpr float bottom() const { return bottom_; }
  19. constexpr float right() const { return right_; }
  20. // Returns the total width taken up by the insets/outsets, which is the
  21. // sum of the left and right insets/outsets.
  22. constexpr float width() const { return left_ + right_; }
  23. // Returns the total height taken up by the insets/outsets, which is the
  24. // sum of the top and bottom insets/outsets.
  25. constexpr float height() const { return top_ + bottom_; }
  26. // Returns true if the insets/outsets are empty.
  27. bool IsEmpty() const { return width() == 0.f && height() == 0.f; }
  28. // These setters can be used together with the default constructor and the
  29. // single-parameter constructor to construct InsetsF instances, for example:
  30. // // T, L, B, R
  31. // InsetsF a = InsetsF().set_top(2); // 2, 0, 0, 0
  32. // InsetsF b = InsetsF().set_left(2).set_bottom(3); // 0, 2, 3, 0
  33. // InsetsF c = InsetsF(1).set_top(5); // 5, 1, 1, 1
  34. constexpr T& set_top(float top) {
  35. top_ = top;
  36. return *static_cast<T*>(this);
  37. }
  38. constexpr T& set_left(float left) {
  39. left_ = left;
  40. return *static_cast<T*>(this);
  41. }
  42. constexpr T& set_bottom(float bottom) {
  43. bottom_ = bottom;
  44. return *static_cast<T*>(this);
  45. }
  46. constexpr T& set_right(float right) {
  47. right_ = right;
  48. return *static_cast<T*>(this);
  49. }
  50. // In addition to the above, we can also use the following methods to
  51. // construct InsetsF/OutsetsF.
  52. // TLBR() is for Chomium UI code. We should not use it in blink code because
  53. // the order of parameters is different from the normal orders used in blink.
  54. // Blink code can use the above setters and VH().
  55. static constexpr inline T TLBR(float top,
  56. float left,
  57. float bottom,
  58. float right) {
  59. return T().set_top(top).set_left(left).set_bottom(bottom).set_right(right);
  60. }
  61. static constexpr inline T VH(float vertical, float horizontal) {
  62. return TLBR(vertical, horizontal, vertical, horizontal);
  63. }
  64. // Sets each side to the maximum of the side and the corresponding side of
  65. // |other|.
  66. void SetToMax(const T& other) {
  67. top_ = std::max(top_, other.top_);
  68. left_ = std::max(left_, other.left_);
  69. bottom_ = std::max(bottom_, other.bottom_);
  70. right_ = std::max(right_, other.right_);
  71. }
  72. void Scale(float x_scale, float y_scale) {
  73. top_ *= y_scale;
  74. left_ *= x_scale;
  75. bottom_ *= y_scale;
  76. right_ *= x_scale;
  77. }
  78. void Scale(float scale) { Scale(scale, scale); }
  79. bool operator==(const InsetsOutsetsFBase<T>& other) const {
  80. return top_ == other.top_ && left_ == other.left_ &&
  81. bottom_ == other.bottom_ && right_ == other.right_;
  82. }
  83. bool operator!=(const InsetsOutsetsFBase<T>& other) const {
  84. return !(*this == other);
  85. }
  86. void operator+=(const T& other) {
  87. top_ += other.top_;
  88. left_ += other.left_;
  89. bottom_ += other.bottom_;
  90. right_ += other.right_;
  91. }
  92. void operator-=(const T& other) {
  93. top_ -= other.top_;
  94. left_ -= other.left_;
  95. bottom_ -= other.bottom_;
  96. right_ -= other.right_;
  97. }
  98. T operator-() const {
  99. return T().set_left(-left_).set_right(-right_).set_top(-top_).set_bottom(
  100. -bottom_);
  101. }
  102. // Returns a string representation of the insets/outsets.
  103. std::string ToString() const {
  104. return base::StringPrintf("x:%g,%g y:%g,%g", left_, right_, top_, bottom_);
  105. }
  106. private:
  107. float top_ = 0.f;
  108. float left_ = 0.f;
  109. float bottom_ = 0.f;
  110. float right_ = 0.f;
  111. };
  112. } // namespace gfx
  113. #endif // UI_GFX_GEOMETRY_INSETS_OUTSETS_F_BASE_H_