GridTrackSize.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GridTrackSize.h"
  7. #include <AK/String.h>
  8. #include <LibWeb/CSS/Size.h>
  9. namespace Web::CSS {
  10. GridSize::GridSize(LengthPercentage length_percentage)
  11. : m_type(Type::LengthPercentage)
  12. , m_value(move(length_percentage))
  13. {
  14. }
  15. GridSize::GridSize(Flex flex_factor)
  16. : m_type(Type::FlexibleLength)
  17. , m_value(move(flex_factor))
  18. {
  19. }
  20. GridSize::GridSize(Type type)
  21. : m_value { Empty() }
  22. {
  23. VERIFY(type == Type::MinContent || type == Type::MaxContent);
  24. m_type = type;
  25. }
  26. GridSize::GridSize()
  27. : m_type(Type::LengthPercentage)
  28. , m_value { Length::make_auto() }
  29. {
  30. }
  31. GridSize::~GridSize() = default;
  32. bool GridSize::is_auto(Layout::AvailableSize const& available_size) const
  33. {
  34. if (m_type == Type::LengthPercentage) {
  35. auto& length_percentage = m_value.get<LengthPercentage>();
  36. if (length_percentage.contains_percentage())
  37. return !available_size.is_definite();
  38. return length_percentage.is_auto();
  39. }
  40. return false;
  41. }
  42. bool GridSize::is_fixed(Layout::AvailableSize const& available_size) const
  43. {
  44. if (m_type == Type::LengthPercentage) {
  45. auto& length_percentage = m_value.get<LengthPercentage>();
  46. if (length_percentage.contains_percentage())
  47. return available_size.is_definite();
  48. return !length_percentage.is_auto();
  49. }
  50. return false;
  51. }
  52. bool GridSize::is_intrinsic(Layout::AvailableSize const& available_size) const
  53. {
  54. return is_auto(available_size) || is_max_content() || is_min_content();
  55. }
  56. GridSize GridSize::make_auto()
  57. {
  58. return GridSize(CSS::Length::make_auto());
  59. }
  60. Size GridSize::css_size() const
  61. {
  62. VERIFY(m_type == Type::LengthPercentage);
  63. auto& length_percentage = m_value.get<LengthPercentage>();
  64. if (length_percentage.is_auto())
  65. return CSS::Size::make_auto();
  66. if (length_percentage.is_length())
  67. return CSS::Size::make_length(length_percentage.length());
  68. if (length_percentage.is_calculated())
  69. return CSS::Size::make_calculated(length_percentage.calculated());
  70. return CSS::Size::make_percentage(length_percentage.percentage());
  71. }
  72. String GridSize::to_string() const
  73. {
  74. switch (m_type) {
  75. case Type::LengthPercentage:
  76. return m_value.get<LengthPercentage>().to_string();
  77. case Type::FlexibleLength:
  78. return m_value.get<Flex>().to_string();
  79. case Type::MaxContent:
  80. return "max-content"_string;
  81. case Type::MinContent:
  82. return "min-content"_string;
  83. }
  84. VERIFY_NOT_REACHED();
  85. }
  86. GridMinMax::GridMinMax(GridSize min_grid_size, GridSize max_grid_size)
  87. : m_min_grid_size(min_grid_size)
  88. , m_max_grid_size(max_grid_size)
  89. {
  90. }
  91. String GridMinMax::to_string() const
  92. {
  93. StringBuilder builder;
  94. builder.append("minmax("sv);
  95. builder.appendff("{}", m_min_grid_size.to_string());
  96. builder.append(", "sv);
  97. builder.appendff("{}", m_max_grid_size.to_string());
  98. builder.append(")"sv);
  99. return MUST(builder.to_string());
  100. }
  101. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, int repeat_count)
  102. : m_type(Type::Default)
  103. , m_grid_track_size_list(grid_track_size_list)
  104. , m_repeat_count(repeat_count)
  105. {
  106. }
  107. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, Type type)
  108. : m_type(type)
  109. , m_grid_track_size_list(grid_track_size_list)
  110. {
  111. }
  112. GridRepeat::GridRepeat()
  113. {
  114. }
  115. String GridRepeat::to_string() const
  116. {
  117. StringBuilder builder;
  118. builder.append("repeat("sv);
  119. switch (m_type) {
  120. case Type::AutoFit:
  121. builder.append("auto-fill"sv);
  122. break;
  123. case Type::AutoFill:
  124. builder.append("auto-fit"sv);
  125. break;
  126. case Type::Default:
  127. builder.appendff("{}", m_repeat_count);
  128. break;
  129. default:
  130. VERIFY_NOT_REACHED();
  131. }
  132. builder.append(", "sv);
  133. builder.appendff("{}", m_grid_track_size_list.to_string());
  134. builder.append(")"sv);
  135. return MUST(builder.to_string());
  136. }
  137. ExplicitGridTrack::ExplicitGridTrack(CSS::GridMinMax grid_minmax)
  138. : m_type(Type::MinMax)
  139. , m_grid_minmax(grid_minmax)
  140. {
  141. }
  142. ExplicitGridTrack::ExplicitGridTrack(CSS::GridRepeat grid_repeat)
  143. : m_type(Type::Repeat)
  144. , m_grid_repeat(grid_repeat)
  145. {
  146. }
  147. ExplicitGridTrack::ExplicitGridTrack(CSS::GridSize grid_size)
  148. : m_type(Type::Default)
  149. , m_grid_size(grid_size)
  150. {
  151. }
  152. String ExplicitGridTrack::to_string() const
  153. {
  154. switch (m_type) {
  155. case Type::MinMax:
  156. return m_grid_minmax.to_string();
  157. case Type::Repeat:
  158. return m_grid_repeat.to_string();
  159. case Type::Default:
  160. return m_grid_size.to_string();
  161. default:
  162. VERIFY_NOT_REACHED();
  163. }
  164. }
  165. String GridLineNames::to_string() const
  166. {
  167. StringBuilder builder;
  168. builder.append("["sv);
  169. builder.join(' ', names);
  170. builder.append("]"sv);
  171. return MUST(builder.to_string());
  172. }
  173. GridTrackSizeList::GridTrackSizeList(Vector<Variant<ExplicitGridTrack, GridLineNames>>&& list)
  174. : m_list(move(list))
  175. {
  176. }
  177. GridTrackSizeList::GridTrackSizeList()
  178. {
  179. }
  180. GridTrackSizeList GridTrackSizeList::make_none()
  181. {
  182. return GridTrackSizeList();
  183. }
  184. String GridTrackSizeList::to_string() const
  185. {
  186. StringBuilder builder;
  187. for (auto const& line_definition_or_name : m_list) {
  188. if (!builder.is_empty())
  189. builder.append(" "sv);
  190. if (line_definition_or_name.has<ExplicitGridTrack>()) {
  191. builder.append(line_definition_or_name.get<ExplicitGridTrack>().to_string());
  192. } else if (line_definition_or_name.has<GridLineNames>()) {
  193. auto const& line_names = line_definition_or_name.get<GridLineNames>();
  194. builder.append(line_names.to_string());
  195. }
  196. }
  197. return MUST(builder.to_string());
  198. }
  199. Vector<ExplicitGridTrack> GridTrackSizeList::track_list() const
  200. {
  201. Vector<ExplicitGridTrack> track_list;
  202. for (auto const& line_definition_or_name : m_list) {
  203. if (line_definition_or_name.has<ExplicitGridTrack>())
  204. track_list.append(line_definition_or_name.get<ExplicitGridTrack>());
  205. }
  206. return track_list;
  207. }
  208. bool GridTrackSizeList::operator==(GridTrackSizeList const& other) const
  209. {
  210. if (m_list.size() != other.m_list.size())
  211. return false;
  212. for (size_t i = 0; i < m_list.size(); ++i) {
  213. if (m_list[i] != other.m_list[i])
  214. return false;
  215. }
  216. return true;
  217. }
  218. }