GridTrackSize.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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(Flex);
  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_value.get<LengthPercentage>(); }
  32. double flex_factor() const { return m_value.get<Flex>().to_fr(); }
  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 && !length_percentage().is_auto();
  40. }
  41. Size css_size() const;
  42. String to_string() const;
  43. bool operator==(GridSize const& other) const
  44. {
  45. return m_type == other.type()
  46. && m_value == other.m_value;
  47. }
  48. private:
  49. Type m_type;
  50. Variant<Empty, LengthPercentage, Flex> m_value;
  51. };
  52. class GridMinMax {
  53. public:
  54. GridMinMax(CSS::GridSize min_grid_size, CSS::GridSize max_grid_size);
  55. GridMinMax() = default;
  56. GridSize min_grid_size() const& { return m_min_grid_size; }
  57. GridSize max_grid_size() const& { return m_max_grid_size; }
  58. String to_string() const;
  59. bool operator==(GridMinMax const& other) const
  60. {
  61. return m_min_grid_size == other.min_grid_size()
  62. && m_max_grid_size == other.max_grid_size();
  63. }
  64. private:
  65. GridSize m_min_grid_size;
  66. GridSize m_max_grid_size;
  67. };
  68. class GridTrackSizeList {
  69. public:
  70. GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list, Vector<Vector<String>> line_names);
  71. GridTrackSizeList();
  72. static GridTrackSizeList make_none();
  73. Vector<CSS::ExplicitGridTrack> track_list() const { return m_track_list; }
  74. Vector<Vector<String>> line_names() const { return m_line_names; }
  75. String to_string() const;
  76. bool operator==(GridTrackSizeList const& other) const
  77. {
  78. return m_line_names == other.line_names() && m_track_list == other.track_list();
  79. }
  80. private:
  81. Vector<CSS::ExplicitGridTrack> m_track_list;
  82. Vector<Vector<String>> m_line_names;
  83. };
  84. class GridRepeat {
  85. public:
  86. enum class Type {
  87. AutoFit,
  88. AutoFill,
  89. Default,
  90. };
  91. GridRepeat(GridTrackSizeList, int repeat_count);
  92. GridRepeat(GridTrackSizeList, Type);
  93. GridRepeat();
  94. bool is_auto_fill() const { return m_type == Type::AutoFill; }
  95. bool is_auto_fit() const { return m_type == Type::AutoFit; }
  96. bool is_default() const { return m_type == Type::Default; }
  97. int repeat_count() const
  98. {
  99. VERIFY(is_default());
  100. return m_repeat_count;
  101. }
  102. GridTrackSizeList grid_track_size_list() const& { return m_grid_track_size_list; }
  103. Type type() const& { return m_type; }
  104. String to_string() const;
  105. bool operator==(GridRepeat const& other) const
  106. {
  107. if (m_type != other.type())
  108. return false;
  109. if (m_type == Type::Default && m_repeat_count != other.repeat_count())
  110. return false;
  111. return m_grid_track_size_list == other.grid_track_size_list();
  112. }
  113. private:
  114. Type m_type;
  115. GridTrackSizeList m_grid_track_size_list;
  116. int m_repeat_count { 0 };
  117. };
  118. class ExplicitGridTrack {
  119. public:
  120. enum class Type {
  121. MinMax,
  122. Repeat,
  123. Default,
  124. };
  125. ExplicitGridTrack(CSS::GridRepeat);
  126. ExplicitGridTrack(CSS::GridMinMax);
  127. ExplicitGridTrack(CSS::GridSize);
  128. bool is_repeat() const { return m_type == Type::Repeat; }
  129. GridRepeat repeat() const
  130. {
  131. VERIFY(is_repeat());
  132. return m_grid_repeat;
  133. }
  134. bool is_minmax() const { return m_type == Type::MinMax; }
  135. GridMinMax minmax() const
  136. {
  137. VERIFY(is_minmax());
  138. return m_grid_minmax;
  139. }
  140. bool is_default() const { return m_type == Type::Default; }
  141. GridSize grid_size() const
  142. {
  143. VERIFY(is_default());
  144. return m_grid_size;
  145. }
  146. Type type() const { return m_type; }
  147. String to_string() const;
  148. bool operator==(ExplicitGridTrack const& other) const
  149. {
  150. if (is_repeat() && other.is_repeat())
  151. return m_grid_repeat == other.repeat();
  152. if (is_minmax() && other.is_minmax())
  153. return m_grid_minmax == other.minmax();
  154. if (is_default() && other.is_default())
  155. return m_grid_size == other.grid_size();
  156. return false;
  157. }
  158. private:
  159. Type m_type;
  160. GridRepeat m_grid_repeat;
  161. GridMinMax m_grid_minmax;
  162. GridSize m_grid_size;
  163. };
  164. }