Margins.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/Rect.h>
  8. namespace GUI {
  9. class Margins {
  10. public:
  11. Margins() = default;
  12. Margins(int all)
  13. : m_top(all)
  14. , m_right(all)
  15. , m_bottom(all)
  16. , m_left(all)
  17. {
  18. }
  19. Margins(int vertical, int horizontal)
  20. : m_top(vertical)
  21. , m_right(horizontal)
  22. , m_bottom(vertical)
  23. , m_left(horizontal)
  24. {
  25. }
  26. Margins(int top, int horizontal, int bottom)
  27. : m_top(top)
  28. , m_right(horizontal)
  29. , m_bottom(bottom)
  30. , m_left(horizontal)
  31. {
  32. }
  33. Margins(int top, int right, int bottom, int left)
  34. : m_top(top)
  35. , m_right(right)
  36. , m_bottom(bottom)
  37. , m_left(left)
  38. {
  39. }
  40. ~Margins() = default;
  41. [[nodiscard]] Gfx::IntRect applied_to(Gfx::IntRect const& input) const
  42. {
  43. Gfx::IntRect output = input;
  44. output.take_from_left(left());
  45. output.take_from_top(top());
  46. output.take_from_right(right());
  47. output.take_from_bottom(bottom());
  48. return output;
  49. }
  50. bool is_null() const { return !m_left && !m_top && !m_right && !m_bottom; }
  51. int top() const { return m_top; }
  52. int right() const { return m_right; }
  53. int bottom() const { return m_bottom; }
  54. int left() const { return m_left; }
  55. void set_top(int value) { m_top = value; }
  56. void set_right(int value) { m_right = value; }
  57. void set_bottom(int value) { m_bottom = value; }
  58. void set_left(int value) { m_left = value; }
  59. bool operator==(Margins const& other) const
  60. {
  61. return m_left == other.m_left
  62. && m_top == other.m_top
  63. && m_right == other.m_right
  64. && m_bottom == other.m_bottom;
  65. }
  66. Margins operator+(Margins const& other) const
  67. {
  68. return Margins { top() + other.top(), right() + other.right(), bottom() + other.bottom(), left() + other.left() };
  69. }
  70. private:
  71. int m_top { 0 };
  72. int m_right { 0 };
  73. int m_bottom { 0 };
  74. int m_left { 0 };
  75. };
  76. }
  77. #define REGISTER_MARGINS_PROPERTY(property_name, getter, setter) \
  78. register_property( \
  79. property_name, [this]() { \
  80. auto m = getter(); \
  81. JsonObject margins_object; \
  82. margins_object.set("left", m.left()); \
  83. margins_object.set("right", m.right()); \
  84. margins_object.set("top", m.top()); \
  85. margins_object.set("bottom", m.bottom()); \
  86. return margins_object; }, \
  87. [this](auto& value) { \
  88. if (!value.is_array()) \
  89. return false; \
  90. auto size = value.as_array().size(); \
  91. if (size == 0 || size > 4) \
  92. return false; \
  93. int m[4]; \
  94. for (size_t i = 0; i < size; ++i) \
  95. m[i] = value.as_array().at(i).to_i32(); \
  96. if (size == 1) \
  97. setter({ m[0] }); \
  98. else if (size == 2) \
  99. setter({ m[0], m[1] }); \
  100. else if (size == 3) \
  101. setter({ m[0], m[1], m[2] }); \
  102. else \
  103. setter({ m[0], m[1], m[2], m[3] }); \
  104. return true; \
  105. });