GridTrackSize.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibWeb/CSS/PercentageOr.h>
  9. #include <LibWeb/Layout/AvailableSpace.h>
  10. namespace Web::CSS {
  11. class GridSize {
  12. public:
  13. enum class Type {
  14. LengthPercentage,
  15. FlexibleLength,
  16. FitContent,
  17. MaxContent,
  18. MinContent,
  19. };
  20. GridSize(Type, LengthPercentage);
  21. GridSize(LengthPercentage);
  22. GridSize(Flex);
  23. GridSize(Type);
  24. GridSize();
  25. ~GridSize();
  26. static GridSize make_auto();
  27. Type type() const { return m_type; }
  28. bool is_auto(Layout::AvailableSize const&) const;
  29. bool is_fixed(Layout::AvailableSize const&) const;
  30. bool is_flexible_length() const { return m_type == Type::FlexibleLength; }
  31. bool is_fit_content() const { return m_type == Type::FitContent; }
  32. bool is_max_content() const { return m_type == Type::MaxContent; }
  33. bool is_min_content() const { return m_type == Type::MinContent; }
  34. LengthPercentage length_percentage() const { return m_value.get<LengthPercentage>(); }
  35. double flex_factor() const { return m_value.get<Flex>().to_fr(); }
  36. // https://www.w3.org/TR/css-grid-2/#layout-algorithm
  37. // An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  38. bool is_intrinsic(Layout::AvailableSize const&) const;
  39. bool is_definite() const
  40. {
  41. return type() == Type::LengthPercentage && !length_percentage().is_auto();
  42. }
  43. Size css_size() const;
  44. String to_string() const;
  45. bool operator==(GridSize const& other) const
  46. {
  47. return m_type == other.type()
  48. && m_value == other.m_value;
  49. }
  50. private:
  51. Type m_type;
  52. Variant<Empty, LengthPercentage, Flex> m_value;
  53. };
  54. class GridFitContent {
  55. public:
  56. GridFitContent(GridSize);
  57. GridFitContent();
  58. GridSize max_grid_size() const& { return m_max_grid_size; }
  59. String to_string() const;
  60. bool operator==(GridFitContent const& other) const
  61. {
  62. return m_max_grid_size == other.m_max_grid_size;
  63. }
  64. private:
  65. GridSize m_max_grid_size;
  66. };
  67. class GridMinMax {
  68. public:
  69. GridMinMax(CSS::GridSize min_grid_size, CSS::GridSize max_grid_size);
  70. GridMinMax() = default;
  71. GridSize min_grid_size() const& { return m_min_grid_size; }
  72. GridSize max_grid_size() const& { return m_max_grid_size; }
  73. String to_string() const;
  74. bool operator==(GridMinMax const& other) const
  75. {
  76. return m_min_grid_size == other.min_grid_size()
  77. && m_max_grid_size == other.max_grid_size();
  78. }
  79. private:
  80. GridSize m_min_grid_size;
  81. GridSize m_max_grid_size;
  82. };
  83. struct GridLineNames {
  84. Vector<String> names;
  85. String to_string() const;
  86. bool operator==(GridLineNames const& other) const { return names == other.names; }
  87. };
  88. class GridTrackSizeList {
  89. public:
  90. GridTrackSizeList(Vector<Variant<ExplicitGridTrack, GridLineNames>>&& list);
  91. GridTrackSizeList();
  92. static GridTrackSizeList make_none();
  93. Vector<CSS::ExplicitGridTrack> track_list() const;
  94. Vector<Variant<ExplicitGridTrack, GridLineNames>> list() const { return m_list; }
  95. String to_string() const;
  96. bool operator==(GridTrackSizeList const& other) const;
  97. private:
  98. Vector<Variant<ExplicitGridTrack, GridLineNames>> m_list;
  99. };
  100. class GridRepeat {
  101. public:
  102. enum class Type {
  103. AutoFit,
  104. AutoFill,
  105. Default,
  106. };
  107. GridRepeat(GridTrackSizeList, int repeat_count);
  108. GridRepeat(GridTrackSizeList, Type);
  109. GridRepeat();
  110. bool is_auto_fill() const { return m_type == Type::AutoFill; }
  111. bool is_auto_fit() const { return m_type == Type::AutoFit; }
  112. bool is_default() const { return m_type == Type::Default; }
  113. int repeat_count() const
  114. {
  115. VERIFY(is_default());
  116. return m_repeat_count;
  117. }
  118. GridTrackSizeList grid_track_size_list() const& { return m_grid_track_size_list; }
  119. Type type() const& { return m_type; }
  120. String to_string() const;
  121. bool operator==(GridRepeat const& other) const
  122. {
  123. if (m_type != other.type())
  124. return false;
  125. if (m_type == Type::Default && m_repeat_count != other.repeat_count())
  126. return false;
  127. return m_grid_track_size_list == other.grid_track_size_list();
  128. }
  129. private:
  130. Type m_type;
  131. GridTrackSizeList m_grid_track_size_list;
  132. int m_repeat_count { 0 };
  133. };
  134. class ExplicitGridTrack {
  135. public:
  136. enum class Type {
  137. FitContent,
  138. MinMax,
  139. Repeat,
  140. Default,
  141. };
  142. ExplicitGridTrack(CSS::GridFitContent);
  143. ExplicitGridTrack(CSS::GridRepeat);
  144. ExplicitGridTrack(CSS::GridMinMax);
  145. ExplicitGridTrack(CSS::GridSize);
  146. bool is_fit_content() const { return m_type == Type::FitContent; }
  147. GridFitContent fit_content() const
  148. {
  149. VERIFY(is_fit_content());
  150. return m_grid_fit_content;
  151. }
  152. bool is_repeat() const { return m_type == Type::Repeat; }
  153. GridRepeat repeat() const
  154. {
  155. VERIFY(is_repeat());
  156. return m_grid_repeat;
  157. }
  158. bool is_minmax() const { return m_type == Type::MinMax; }
  159. GridMinMax minmax() const
  160. {
  161. VERIFY(is_minmax());
  162. return m_grid_minmax;
  163. }
  164. bool is_default() const { return m_type == Type::Default; }
  165. GridSize grid_size() const
  166. {
  167. VERIFY(is_default());
  168. return m_grid_size;
  169. }
  170. Type type() const { return m_type; }
  171. String to_string() const;
  172. bool operator==(ExplicitGridTrack const& other) const
  173. {
  174. if (is_fit_content() && other.is_fit_content())
  175. return m_grid_fit_content == other.fit_content();
  176. if (is_repeat() && other.is_repeat())
  177. return m_grid_repeat == other.repeat();
  178. if (is_minmax() && other.is_minmax())
  179. return m_grid_minmax == other.minmax();
  180. if (is_default() && other.is_default())
  181. return m_grid_size == other.grid_size();
  182. return false;
  183. }
  184. private:
  185. Type m_type;
  186. GridFitContent m_grid_fit_content;
  187. GridRepeat m_grid_repeat;
  188. GridMinMax m_grid_minmax;
  189. GridSize m_grid_size;
  190. };
  191. }