GridTrackSize.h 5.2 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/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. struct GridLineNames {
  69. Vector<String> names;
  70. String to_string() const;
  71. bool operator==(GridLineNames const& other) const { return names == other.names; }
  72. };
  73. class GridTrackSizeList {
  74. public:
  75. GridTrackSizeList(Vector<Variant<ExplicitGridTrack, GridLineNames>>&& list);
  76. GridTrackSizeList();
  77. static GridTrackSizeList make_none();
  78. Vector<CSS::ExplicitGridTrack> track_list() const;
  79. Vector<Variant<ExplicitGridTrack, GridLineNames>> list() const { return m_list; }
  80. String to_string() const;
  81. bool operator==(GridTrackSizeList const& other) const;
  82. private:
  83. Vector<Variant<ExplicitGridTrack, GridLineNames>> m_list;
  84. };
  85. class GridRepeat {
  86. public:
  87. enum class Type {
  88. AutoFit,
  89. AutoFill,
  90. Default,
  91. };
  92. GridRepeat(GridTrackSizeList, int repeat_count);
  93. GridRepeat(GridTrackSizeList, Type);
  94. GridRepeat();
  95. bool is_auto_fill() const { return m_type == Type::AutoFill; }
  96. bool is_auto_fit() const { return m_type == Type::AutoFit; }
  97. bool is_default() const { return m_type == Type::Default; }
  98. int repeat_count() const
  99. {
  100. VERIFY(is_default());
  101. return m_repeat_count;
  102. }
  103. GridTrackSizeList grid_track_size_list() const& { return m_grid_track_size_list; }
  104. Type type() const& { return m_type; }
  105. String to_string() const;
  106. bool operator==(GridRepeat const& other) const
  107. {
  108. if (m_type != other.type())
  109. return false;
  110. if (m_type == Type::Default && m_repeat_count != other.repeat_count())
  111. return false;
  112. return m_grid_track_size_list == other.grid_track_size_list();
  113. }
  114. private:
  115. Type m_type;
  116. GridTrackSizeList m_grid_track_size_list;
  117. int m_repeat_count { 0 };
  118. };
  119. class ExplicitGridTrack {
  120. public:
  121. enum class Type {
  122. MinMax,
  123. Repeat,
  124. Default,
  125. };
  126. ExplicitGridTrack(CSS::GridRepeat);
  127. ExplicitGridTrack(CSS::GridMinMax);
  128. ExplicitGridTrack(CSS::GridSize);
  129. bool is_repeat() const { return m_type == Type::Repeat; }
  130. GridRepeat repeat() const
  131. {
  132. VERIFY(is_repeat());
  133. return m_grid_repeat;
  134. }
  135. bool is_minmax() const { return m_type == Type::MinMax; }
  136. GridMinMax minmax() const
  137. {
  138. VERIFY(is_minmax());
  139. return m_grid_minmax;
  140. }
  141. bool is_default() const { return m_type == Type::Default; }
  142. GridSize grid_size() const
  143. {
  144. VERIFY(is_default());
  145. return m_grid_size;
  146. }
  147. Type type() const { return m_type; }
  148. String to_string() const;
  149. bool operator==(ExplicitGridTrack const& other) const
  150. {
  151. if (is_repeat() && other.is_repeat())
  152. return m_grid_repeat == other.repeat();
  153. if (is_minmax() && other.is_minmax())
  154. return m_grid_minmax == other.minmax();
  155. if (is_default() && other.is_default())
  156. return m_grid_size == other.grid_size();
  157. return false;
  158. }
  159. private:
  160. Type m_type;
  161. GridRepeat m_grid_repeat;
  162. GridMinMax m_grid_minmax;
  163. GridSize m_grid_size;
  164. };
  165. }