UIDimensions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. [[nodiscard]] inline bool is_special_value() const
  37. {
  38. return m_value < 0;
  39. }
  40. [[nodiscard]] inline bool is_int() const
  41. {
  42. return m_value >= 0;
  43. }
  44. [[nodiscard]] inline bool is_shrink() const
  45. {
  46. return m_value == to_underlying(SpecialDimension::Shrink);
  47. }
  48. [[nodiscard]] inline bool is_grow() const
  49. {
  50. return m_value == to_underlying(SpecialDimension::Grow);
  51. }
  52. [[nodiscard]] inline bool is_opportunistic_grow() const
  53. {
  54. return m_value == to_underlying(SpecialDimension::OpportunisticGrow);
  55. }
  56. [[nodiscard]] inline bool is_fit() const
  57. {
  58. return m_value == to_underlying(SpecialDimension::Fit);
  59. }
  60. [[nodiscard]] inline bool is_one_of(std::initializer_list<SpecialDimension> valid_values) const
  61. {
  62. for (SpecialDimension v : valid_values) {
  63. if (m_value == to_underlying(v) || (v == SpecialDimension::Regular && is_int()))
  64. return true;
  65. }
  66. return false;
  67. }
  68. [[nodiscard]] ALWAYS_INLINE constexpr bool is(SpecialDimension special_value) const
  69. {
  70. return m_value == to_underlying(special_value) || (special_value == SpecialDimension::Regular && is_int());
  71. }
  72. template<typename... Ts>
  73. [[nodiscard]] bool is_one_of(Ts... valid_values) const
  74. {
  75. return (... || (is(forward<Ts>(valid_values))));
  76. }
  77. [[nodiscard]] inline bool operator==(UIDimension other) const
  78. {
  79. return m_value == other.m_value;
  80. }
  81. [[nodiscard]] inline UIDimension must_sum_with(UIDimension other) const
  82. {
  83. VERIFY(is_int() && other.is_int());
  84. return UIDimension { m_value + other.m_value };
  85. }
  86. inline void must_add(int to_add)
  87. {
  88. VERIFY(is_int());
  89. VERIFY(m_value >= -to_add);
  90. m_value += to_add;
  91. }
  92. inline void add_if_int(int to_add)
  93. {
  94. if (is_int()) {
  95. m_value += to_add;
  96. }
  97. }
  98. [[nodiscard]] inline ErrorOr<int> shrink_value() const
  99. {
  100. if (m_value >= 0)
  101. return m_value;
  102. if (m_value == to_underlying(SpecialDimension::Shrink))
  103. return 0;
  104. return Error::from_string_literal("value is neither shrink nor an integer ≥0");
  105. }
  106. [[nodiscard]] inline int as_int() const
  107. {
  108. VERIFY(is_int());
  109. return m_value;
  110. }
  111. [[nodiscard]] AK::JsonValue as_json_value() const
  112. {
  113. if (is_int())
  114. return m_value;
  115. if (is_shrink())
  116. return "shrink";
  117. if (is_grow())
  118. return "grow";
  119. if (is_opportunistic_grow())
  120. return "opportunistic_grow";
  121. if (is_fit())
  122. return "fit";
  123. VERIFY_NOT_REACHED();
  124. }
  125. [[nodiscard]] static Optional<UIDimension> construct_from_json_value(AK::JsonValue const value)
  126. {
  127. if (value.is_string()) {
  128. String value_literal = value.as_string();
  129. if (value_literal == "shrink")
  130. return UIDimension { SpecialDimension::Shrink };
  131. else if (value_literal == "grow")
  132. return UIDimension { SpecialDimension::Grow };
  133. else if (value_literal == "opportunistic_grow")
  134. return UIDimension { SpecialDimension::OpportunisticGrow };
  135. else if (value_literal == "fit")
  136. return UIDimension { SpecialDimension::Fit };
  137. else
  138. return {};
  139. } else {
  140. int value_int = value.to_i32();
  141. if (value_int < 0)
  142. return {};
  143. return UIDimension(value_int);
  144. }
  145. }
  146. private:
  147. int m_value;
  148. };
  149. class UISize : public Gfx::Size<UIDimension> {
  150. public:
  151. UISize() = delete;
  152. UISize(int in_width, int in_height)
  153. : Gfx::Size<UIDimension>(in_width, in_height)
  154. {
  155. }
  156. UISize(Gfx::IntSize size)
  157. : UISize(size.width(), size.height())
  158. {
  159. }
  160. UISize(SpecialDimension special)
  161. : Gfx::Size<UIDimension>(UIDimension { special }, UIDimension { special })
  162. {
  163. }
  164. UISize(UIDimension width, UIDimension height)
  165. : Gfx::Size<UIDimension>(width, height)
  166. {
  167. }
  168. inline UISize replace_component_if_matching_with(UIDimension to_match, UISize replacement)
  169. {
  170. if (width() == to_match)
  171. set_width(replacement.width());
  172. if (height() == to_match)
  173. set_height(replacement.height());
  174. return *this;
  175. }
  176. [[nodiscard]] inline bool has_only_int_values() const
  177. {
  178. return width().is_int() && height().is_int();
  179. }
  180. [[nodiscard]] inline bool either_is(UIDimension to_match) const
  181. {
  182. return (width() == to_match || height() == to_match);
  183. }
  184. explicit operator Gfx::IntSize() const
  185. {
  186. return Gfx::IntSize(width().as_int(), height().as_int());
  187. }
  188. };
  189. }
  190. namespace AK {
  191. template<>
  192. inline auto max<GUI::UIDimension>(GUI::UIDimension const& a, GUI::UIDimension const& b) -> GUI::UIDimension
  193. {
  194. if ((a.is_int() && b.is_int()) || (a.is_special_value() && b.is_special_value()))
  195. return a.m_value > b.m_value ? a : b;
  196. if (a.is_grow() || b.is_grow())
  197. return GUI::SpecialDimension::Grow;
  198. if (a.is_opportunistic_grow() || b.is_opportunistic_grow())
  199. return GUI::SpecialDimension::OpportunisticGrow;
  200. if (a.is_fit() || b.is_fit())
  201. return GUI::SpecialDimension::Fit;
  202. if (a.is_shrink())
  203. return b;
  204. if (b.is_shrink())
  205. return a;
  206. VERIFY_NOT_REACHED();
  207. }
  208. template<>
  209. inline auto min<GUI::UIDimension>(GUI::UIDimension const& a, GUI::UIDimension const& b) -> GUI::UIDimension
  210. {
  211. if ((a.is_int() && b.is_int()) || (a.is_special_value() && b.is_special_value()))
  212. return a.m_value < b.m_value ? a : b;
  213. if (a.is_shrink() || b.is_shrink())
  214. return GUI::SpecialDimension::Shrink;
  215. if (a.is_int())
  216. return a;
  217. if (b.is_int())
  218. return b;
  219. if (a.is_fit() || b.is_fit())
  220. return GUI::SpecialDimension::Fit;
  221. if (a.is_opportunistic_grow() || b.is_opportunistic_grow())
  222. return GUI::SpecialDimension::OpportunisticGrow;
  223. VERIFY_NOT_REACHED();
  224. }
  225. template<>
  226. inline auto clamp<GUI::UIDimension>(GUI::UIDimension const& input, GUI::UIDimension const& lower_bound, GUI::UIDimension const& upper_bound) -> GUI::UIDimension
  227. {
  228. return min(max(input, lower_bound), upper_bound);
  229. }
  230. }
  231. #define REGISTER_UI_DIMENSION_PROPERTY(property_name, getter, setter) \
  232. register_property( \
  233. property_name, \
  234. [this] { \
  235. return this->getter().as_json_value(); \
  236. }, \
  237. [this](auto& value) { \
  238. auto result = GUI::UIDimension::construct_from_json_value(value); \
  239. if (result.has_value()) \
  240. this->setter(result.value()); \
  241. return result.has_value(); \
  242. });
  243. #define REGISTER_READONLY_UI_DIMENSION_PROPERTY(property_name, getter) \
  244. register_property( \
  245. property_name, \
  246. [this] { \
  247. return this->getter().as_json_value(); \
  248. });
  249. #define REGISTER_UI_SIZE_PROPERTY(property_name, getter, setter) \
  250. register_property( \
  251. property_name, \
  252. [this] { \
  253. auto size = this->getter(); \
  254. JsonObject size_object; \
  255. size_object.set("width"sv, size.width().as_json_value()); \
  256. size_object.set("height"sv, size.height().as_json_value()); \
  257. return size_object; \
  258. }, \
  259. [this](auto& value) { \
  260. if (!value.is_object()) \
  261. return false; \
  262. auto result_width = GUI::UIDimension::construct_from_json_value( \
  263. value.as_object().get("width"sv)); \
  264. auto result_height = GUI::UIDimension::construct_from_json_value( \
  265. value.as_object().get("height"sv)); \
  266. if (result_width.has_value() && result_height.has_value()) { \
  267. GUI::UISize size(result_width.value(), result_height.value()); \
  268. setter(size); \
  269. return true; \
  270. } \
  271. return false; \
  272. });
  273. #define REGISTER_READONLY_UI_SIZE_PROPERTY(property_name, getter) \
  274. register_property( \
  275. property_name, \
  276. [this] { \
  277. auto size = this->getter(); \
  278. JsonObject size_object; \
  279. size_object.set("width", size.width().as_json_value()); \
  280. size_object.set("height", size.height().as_json_value()); \
  281. return size_object; \
  282. });