GridTrackSize.h 5.3 KB

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