UIDimensions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) 2022, Frhun <serenitystuff@frhun.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/JsonValue.h>
  8. #include <AK/Optional.h>
  9. #include <LibGfx/Rect.h>
  10. #include <LibGfx/Size.h>
  11. #include <initializer_list>
  12. namespace GUI {
  13. // The constants used for special values
  14. // Their order here, also defines their order among each other for min, max; operations, excluding Regular
  15. enum class SpecialDimension : int {
  16. Regular = 0, // only really useful for is_one_of
  17. Grow = -1,
  18. OpportunisticGrow = -2,
  19. Fit = -3,
  20. Shrink = -4,
  21. };
  22. class UIDimension {
  23. friend constexpr auto AK::max<GUI::UIDimension>(GUI::UIDimension const&, GUI::UIDimension const&) -> GUI::UIDimension;
  24. friend constexpr auto AK::min<GUI::UIDimension>(GUI::UIDimension const&, GUI::UIDimension const&) -> GUI::UIDimension;
  25. public:
  26. UIDimension() = delete;
  27. UIDimension(int value)
  28. : m_value(value)
  29. {
  30. VERIFY(value >= 0);
  31. }
  32. UIDimension(SpecialDimension special)
  33. : m_value(to_underlying(special))
  34. {
  35. }
  36. // This is a temporary hack to get this compiling
  37. operator int() const
  38. {
  39. return m_value;
  40. }
  41. [[nodiscard]] inline bool is_special_value() const
  42. {
  43. return m_value < 0;
  44. }
  45. [[nodiscard]] inline bool is_int() const
  46. {
  47. return m_value >= 0;
  48. }
  49. [[nodiscard]] inline bool is_shrink() const
  50. {
  51. return m_value == to_underlying(SpecialDimension::Shrink);
  52. }
  53. [[nodiscard]] inline bool is_grow() const
  54. {
  55. return m_value == to_underlying(SpecialDimension::Grow);
  56. }
  57. [[nodiscard]] inline bool is_opportunistic_grow() const
  58. {
  59. return m_value == to_underlying(SpecialDimension::OpportunisticGrow);
  60. }
  61. [[nodiscard]] inline bool is_fit() const
  62. {
  63. return m_value == to_underlying(SpecialDimension::Fit);
  64. }
  65. [[nodiscard]] inline bool is_one_of(std::initializer_list<SpecialDimension> valid_values) const
  66. {
  67. for (SpecialDimension v : valid_values) {
  68. if (m_value == to_underlying(v) || (v == SpecialDimension::Regular && is_int()))
  69. return true;
  70. }
  71. return false;
  72. }
  73. [[nodiscard]] ALWAYS_INLINE constexpr bool is(SpecialDimension special_value) const
  74. {
  75. return m_value == to_underlying(special_value) || (special_value == SpecialDimension::Regular && is_int());
  76. }
  77. template<typename... Ts>
  78. [[nodiscard]] bool is_one_of(Ts... valid_values) const
  79. {
  80. return (... || (is(forward<Ts>(valid_values))));
  81. }
  82. [[nodiscard]] inline bool operator==(UIDimension other) const
  83. {
  84. return m_value == other.m_value;
  85. }
  86. [[nodiscard]] inline UIDimension must_sum_with(UIDimension other) const
  87. {
  88. VERIFY(is_int() && other.is_int());
  89. return UIDimension { m_value + other.m_value };
  90. }
  91. inline void must_add(int to_add)
  92. {
  93. VERIFY(is_int());
  94. VERIFY(m_value >= -to_add);
  95. m_value += to_add;
  96. }
  97. inline void add_if_int(int to_add)
  98. {
  99. if (is_int()) {
  100. m_value += to_add;
  101. }
  102. }
  103. [[nodiscard]] inline ErrorOr<int> shrink_value() const
  104. {
  105. if (m_value >= 0)
  106. return m_value;
  107. if (m_value == to_underlying(SpecialDimension::Shrink))
  108. return 0;
  109. return Error::from_string_literal("value is neither shrink nor an integer ≥0");
  110. }
  111. [[nodiscard]] inline int as_int() const
  112. {
  113. VERIFY(is_int());
  114. return m_value;
  115. }
  116. [[nodiscard]] AK::JsonValue as_json_value() const
  117. {
  118. if (is_int())
  119. return m_value;
  120. if (is_shrink())
  121. return "shrink";
  122. if (is_grow())
  123. return "grow";
  124. if (is_opportunistic_grow())
  125. return "opportunistic_grow";
  126. if (is_fit())
  127. return "fit";
  128. VERIFY_NOT_REACHED();
  129. }
  130. operator AK::JsonValue() const
  131. {
  132. return this->as_json_value();
  133. }
  134. [[nodiscard]] static Optional<UIDimension> construct_from_json_value(AK::JsonValue const value)
  135. {
  136. if (value.is_string()) {
  137. String value_literal = value.as_string();
  138. if (value_literal == "shrink")
  139. return UIDimension { SpecialDimension::Shrink };
  140. else if (value_literal == "grow")
  141. return UIDimension { SpecialDimension::Grow };
  142. else if (value_literal == "opportunistic_grow")
  143. return UIDimension { SpecialDimension::OpportunisticGrow };
  144. else if (value_literal == "fit")
  145. return UIDimension { SpecialDimension::Fit };
  146. else
  147. return {};
  148. } else {
  149. int value_int = value.to_i32();
  150. if (value_int < 0)
  151. return {};
  152. return UIDimension(value_int);
  153. }
  154. }
  155. // FIXME: Remove these following methods when the move to the new layout system is completed
  156. [[nodiscard]] inline bool operator==(int other) const
  157. {
  158. return m_value == other;
  159. }
  160. private:
  161. int m_value;
  162. };
  163. class UISize : public Gfx::Size<UIDimension> {
  164. public:
  165. UISize() = delete;
  166. UISize(int in_width, int in_height)
  167. : Gfx::Size<UIDimension>(in_width, in_height)
  168. {
  169. }
  170. UISize(Gfx::IntSize size)
  171. : UISize(size.width(), size.height())
  172. {
  173. }
  174. UISize(SpecialDimension special)
  175. : Gfx::Size<UIDimension>(UIDimension { special }, UIDimension { special })
  176. {
  177. }
  178. UISize(UIDimension width, UIDimension height)
  179. : Gfx::Size<UIDimension>(width, height)
  180. {
  181. }
  182. inline UISize replace_component_if_matching_with(UIDimension to_match, UISize replacement)
  183. {
  184. if (width() == to_match)
  185. set_width(replacement.width());
  186. if (height() == to_match)
  187. set_height(replacement.height());
  188. return *this;
  189. }
  190. [[nodiscard]] inline bool has_only_int_values() const
  191. {
  192. return width().is_int() && height().is_int();
  193. }
  194. [[nodiscard]] inline bool either_is(UIDimension to_match) const
  195. {
  196. return (width() == to_match || height() == to_match);
  197. }
  198. operator Gfx::IntSize() const
  199. {
  200. return Gfx::IntSize(width().as_int(), height().as_int());
  201. }
  202. };
  203. }
  204. namespace AK {
  205. template<>
  206. inline auto max<GUI::UIDimension>(GUI::UIDimension const& a, GUI::UIDimension const& b) -> GUI::UIDimension
  207. {
  208. if ((a.is_int() && b.is_int()) || (a.is_special_value() && b.is_special_value()))
  209. return a.m_value > b.m_value ? a : b;
  210. if (a.is_grow() || b.is_grow())
  211. return GUI::SpecialDimension::Grow;
  212. if (a.is_opportunistic_grow() || b.is_opportunistic_grow())
  213. return GUI::SpecialDimension::OpportunisticGrow;
  214. if (a.is_fit() || b.is_fit())
  215. return GUI::SpecialDimension::Fit;
  216. if (a.is_shrink())
  217. return b;
  218. if (b.is_shrink())
  219. return a;
  220. VERIFY_NOT_REACHED();
  221. }
  222. template<>
  223. inline auto min<GUI::UIDimension>(GUI::UIDimension const& a, GUI::UIDimension const& b) -> GUI::UIDimension
  224. {
  225. if ((a.is_int() && b.is_int()) || (a.is_special_value() && b.is_special_value()))
  226. return a.m_value < b.m_value ? a : b;
  227. if (a.is_shrink() || b.is_shrink())
  228. return GUI::SpecialDimension::Shrink;
  229. if (a.is_int())
  230. return a;
  231. if (b.is_int())
  232. return b;
  233. if (a.is_fit() || b.is_fit())
  234. return GUI::SpecialDimension::Fit;
  235. if (a.is_opportunistic_grow() || b.is_opportunistic_grow())
  236. return GUI::SpecialDimension::OpportunisticGrow;
  237. VERIFY_NOT_REACHED();
  238. }
  239. template<>
  240. inline auto clamp<GUI::UIDimension>(GUI::UIDimension const& input, GUI::UIDimension const& lower_bound, GUI::UIDimension const& upper_bound) -> GUI::UIDimension
  241. {
  242. return min(max(input, lower_bound), upper_bound);
  243. }
  244. }
  245. #define REGISTER_UI_DIMENSION_PROPERTY(property_name, getter, setter) \
  246. register_property( \
  247. property_name, \
  248. [this] { \
  249. return this->getter().as_json_value(); \
  250. }, \
  251. [this](auto& value) { \
  252. auto result = GUI::UIDimension::construct_from_json_value(value); \
  253. if (result.has_value()) \
  254. this->setter(result.value()); \
  255. return result.has_value(); \
  256. });
  257. #define REGISTER_UI_SIZE_PROPERTY(property_name, getter, setter) \
  258. register_property( \
  259. property_name, \
  260. [this] { \
  261. auto size = this->getter(); \
  262. JsonObject size_object; \
  263. size_object.set("width", size.width().as_json_value()); \
  264. size_object.set("height", size.height().as_json_value()); \
  265. return size_object; \
  266. }, \
  267. [this](auto& value) { \
  268. if (!value.is_object()) \
  269. return false; \
  270. auto result_width = GUI::UIDimension::construct_from_json_value( \
  271. value.as_object().get("width")); \
  272. auto result_height = GUI::UIDimension::construct_from_json_value( \
  273. value.as_object().get("height")); \
  274. if (result_width.has_value() && result_height.has_value()) { \
  275. GUI::UISize size(result_width.value(), result_height.value()); \
  276. setter(size); \
  277. return true; \
  278. } \
  279. return false; \
  280. });