GridTrackSize.h 5.4 KB

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