GridTrackSize.h 5.3 KB

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