GridTrackSize.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/Length.h>
  9. #include <LibWeb/CSS/Percentage.h>
  10. namespace Web::CSS {
  11. class GridSize {
  12. public:
  13. enum class Type {
  14. Length,
  15. Percentage,
  16. FlexibleLength,
  17. MaxContent,
  18. MinContent,
  19. };
  20. GridSize(Length);
  21. GridSize(Percentage);
  22. GridSize(float);
  23. GridSize(Type);
  24. GridSize();
  25. ~GridSize();
  26. static GridSize make_auto();
  27. Type type() const { return m_type; }
  28. bool is_length() const { return m_type == Type::Length; }
  29. bool is_percentage() const { return m_type == Type::Percentage; }
  30. bool is_flexible_length() const { return m_type == Type::FlexibleLength; }
  31. bool is_max_content() const { return m_type == Type::MaxContent; }
  32. bool is_min_content() const { return m_type == Type::MinContent; }
  33. Length length() const;
  34. Percentage percentage() const { return m_percentage; }
  35. float flexible_length() const { return m_flexible_length; }
  36. // https://www.w3.org/TR/css-grid-2/#layout-algorithm
  37. // An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  38. // FIXME: Add missing properties once implemented.
  39. bool is_intrinsic_track_sizing() const
  40. {
  41. return (m_type == Type::Length && m_length.is_auto()) || m_type == Type::MaxContent || m_type == Type::MinContent;
  42. }
  43. bool is_definite() const
  44. {
  45. return (m_type == Type::Length && !m_length.is_auto()) || is_percentage();
  46. }
  47. ErrorOr<String> to_string() const;
  48. bool operator==(GridSize const& other) const
  49. {
  50. return m_type == other.type()
  51. && m_length == other.length()
  52. && m_percentage == other.percentage()
  53. && m_flexible_length == other.flexible_length();
  54. }
  55. private:
  56. Type m_type;
  57. // Length includes a RefPtr<CalculatedStyleValue> member, but we can't include the header StyleValue.h as it includes
  58. // this file already. To break the cyclic dependency, we must initialize m_length in the constructor.
  59. Length m_length;
  60. Percentage m_percentage { Percentage(0) };
  61. float m_flexible_length { 0 };
  62. };
  63. class GridMinMax {
  64. public:
  65. GridMinMax(CSS::GridSize min_grid_size, CSS::GridSize max_grid_size);
  66. GridMinMax() = default;
  67. GridSize min_grid_size() const& { return m_min_grid_size; }
  68. GridSize max_grid_size() const& { return m_max_grid_size; }
  69. ErrorOr<String> to_string() const;
  70. bool operator==(GridMinMax const& other) const
  71. {
  72. return m_min_grid_size == other.min_grid_size()
  73. && m_max_grid_size == other.max_grid_size();
  74. }
  75. private:
  76. GridSize m_min_grid_size;
  77. GridSize m_max_grid_size;
  78. };
  79. class GridTrackSizeList {
  80. public:
  81. GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list, Vector<Vector<String>> line_names);
  82. GridTrackSizeList();
  83. static GridTrackSizeList make_auto();
  84. Vector<CSS::ExplicitGridTrack> track_list() const { return m_track_list; }
  85. Vector<Vector<String>> line_names() const { return m_line_names; }
  86. ErrorOr<String> to_string() const;
  87. bool operator==(GridTrackSizeList const& other) const
  88. {
  89. return m_line_names == other.line_names() && m_track_list == other.track_list();
  90. }
  91. private:
  92. Vector<CSS::ExplicitGridTrack> m_track_list;
  93. Vector<Vector<String>> m_line_names;
  94. };
  95. class GridRepeat {
  96. public:
  97. enum class Type {
  98. AutoFit,
  99. AutoFill,
  100. Default,
  101. };
  102. GridRepeat(GridTrackSizeList, int repeat_count);
  103. GridRepeat(GridTrackSizeList, Type);
  104. GridRepeat();
  105. bool is_auto_fill() const { return m_type == Type::AutoFill; }
  106. bool is_auto_fit() const { return m_type == Type::AutoFit; }
  107. bool is_default() const { return m_type == Type::Default; }
  108. int repeat_count() const
  109. {
  110. VERIFY(is_default());
  111. return m_repeat_count;
  112. }
  113. GridTrackSizeList grid_track_size_list() const& { return m_grid_track_size_list; }
  114. Type type() const& { return m_type; }
  115. ErrorOr<String> to_string() const;
  116. bool operator==(GridRepeat const& other) const
  117. {
  118. if (m_type != other.type())
  119. return false;
  120. if (m_type == Type::Default && m_repeat_count != other.repeat_count())
  121. return false;
  122. return m_grid_track_size_list == other.grid_track_size_list();
  123. }
  124. private:
  125. Type m_type;
  126. GridTrackSizeList m_grid_track_size_list;
  127. int m_repeat_count { 0 };
  128. };
  129. class ExplicitGridTrack {
  130. public:
  131. enum class Type {
  132. MinMax,
  133. Repeat,
  134. Default,
  135. };
  136. ExplicitGridTrack(CSS::GridRepeat);
  137. ExplicitGridTrack(CSS::GridMinMax);
  138. ExplicitGridTrack(CSS::GridSize);
  139. bool is_repeat() const { return m_type == Type::Repeat; }
  140. GridRepeat repeat() const
  141. {
  142. VERIFY(is_repeat());
  143. return m_grid_repeat;
  144. }
  145. bool is_minmax() const { return m_type == Type::MinMax; }
  146. GridMinMax minmax() const
  147. {
  148. VERIFY(is_minmax());
  149. return m_grid_minmax;
  150. }
  151. bool is_default() const { return m_type == Type::Default; }
  152. GridSize grid_size() const
  153. {
  154. VERIFY(is_default());
  155. return m_grid_size;
  156. }
  157. Type type() const { return m_type; }
  158. ErrorOr<String> to_string() const;
  159. bool operator==(ExplicitGridTrack const& other) const
  160. {
  161. if (is_repeat() && other.is_repeat())
  162. return m_grid_repeat == other.repeat();
  163. if (is_minmax() && other.is_minmax())
  164. return m_grid_minmax == other.minmax();
  165. if (is_default() && other.is_default())
  166. return m_grid_size == other.grid_size();
  167. return false;
  168. }
  169. private:
  170. Type m_type;
  171. GridRepeat m_grid_repeat;
  172. GridMinMax m_grid_minmax;
  173. GridSize m_grid_size;
  174. };
  175. }