UIDimensions.h 11 KB

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