GridTrackSize.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_length_percentage(length_percentage) {};
  13. GridSize::GridSize(Flex flex_factor)
  14. : m_type(Type::FlexibleLength)
  15. , m_length_percentage { Length::make_px(0) }
  16. , m_flex_factor(flex_factor)
  17. {
  18. }
  19. GridSize::GridSize(Type type)
  20. : m_length_percentage { Length::make_auto() }
  21. {
  22. VERIFY(type == Type::MinContent || type == Type::MaxContent);
  23. m_type = type;
  24. }
  25. GridSize::GridSize()
  26. : m_type(Type::LengthPercentage)
  27. , m_length_percentage { Length::make_auto() }
  28. {
  29. }
  30. GridSize::~GridSize() = default;
  31. bool GridSize::is_auto(Layout::AvailableSize const& available_size) const
  32. {
  33. if (m_type == Type::LengthPercentage) {
  34. if (m_length_percentage.contains_percentage())
  35. return !available_size.is_definite();
  36. return m_length_percentage.is_auto();
  37. }
  38. return false;
  39. }
  40. bool GridSize::is_fixed(Layout::AvailableSize const& available_size) const
  41. {
  42. if (m_type == Type::LengthPercentage) {
  43. if (m_length_percentage.contains_percentage())
  44. return available_size.is_definite();
  45. return !m_length_percentage.is_auto();
  46. }
  47. return false;
  48. }
  49. bool GridSize::is_intrinsic(Layout::AvailableSize const& available_size) const
  50. {
  51. return is_auto(available_size) || is_max_content() || is_min_content();
  52. }
  53. GridSize GridSize::make_auto()
  54. {
  55. return GridSize(CSS::Length::make_auto());
  56. }
  57. Size GridSize::css_size() const
  58. {
  59. VERIFY(m_type == Type::LengthPercentage);
  60. if (m_length_percentage.is_auto())
  61. return CSS::Size::make_auto();
  62. if (m_length_percentage.is_length())
  63. return CSS::Size::make_length(m_length_percentage.length());
  64. if (m_length_percentage.is_calculated()) {
  65. return CSS::Size::make_calculated(m_length_percentage.calculated());
  66. }
  67. return CSS::Size::make_percentage(m_length_percentage.percentage());
  68. }
  69. String GridSize::to_string() const
  70. {
  71. switch (m_type) {
  72. case Type::LengthPercentage:
  73. return m_length_percentage.to_string();
  74. case Type::FlexibleLength:
  75. return m_flex_factor.to_string();
  76. case Type::MaxContent:
  77. return "max-content"_string;
  78. case Type::MinContent:
  79. return "min-content"_string;
  80. }
  81. VERIFY_NOT_REACHED();
  82. }
  83. GridMinMax::GridMinMax(GridSize min_grid_size, GridSize max_grid_size)
  84. : m_min_grid_size(min_grid_size)
  85. , m_max_grid_size(max_grid_size)
  86. {
  87. }
  88. String GridMinMax::to_string() const
  89. {
  90. StringBuilder builder;
  91. builder.append("minmax("sv);
  92. builder.appendff("{}", m_min_grid_size.to_string());
  93. builder.append(", "sv);
  94. builder.appendff("{}", m_max_grid_size.to_string());
  95. builder.append(")"sv);
  96. return MUST(builder.to_string());
  97. }
  98. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, int repeat_count)
  99. : m_type(Type::Default)
  100. , m_grid_track_size_list(grid_track_size_list)
  101. , m_repeat_count(repeat_count)
  102. {
  103. }
  104. GridRepeat::GridRepeat(GridTrackSizeList grid_track_size_list, Type type)
  105. : m_type(type)
  106. , m_grid_track_size_list(grid_track_size_list)
  107. {
  108. }
  109. GridRepeat::GridRepeat()
  110. {
  111. }
  112. String GridRepeat::to_string() const
  113. {
  114. StringBuilder builder;
  115. builder.append("repeat("sv);
  116. switch (m_type) {
  117. case Type::AutoFit:
  118. builder.append("auto-fill"sv);
  119. break;
  120. case Type::AutoFill:
  121. builder.append("auto-fit"sv);
  122. break;
  123. case Type::Default:
  124. builder.appendff("{}", m_repeat_count);
  125. break;
  126. default:
  127. VERIFY_NOT_REACHED();
  128. }
  129. builder.append(", "sv);
  130. builder.appendff("{}", m_grid_track_size_list.to_string());
  131. builder.append(")"sv);
  132. return MUST(builder.to_string());
  133. }
  134. ExplicitGridTrack::ExplicitGridTrack(CSS::GridMinMax grid_minmax)
  135. : m_type(Type::MinMax)
  136. , m_grid_minmax(grid_minmax)
  137. {
  138. }
  139. ExplicitGridTrack::ExplicitGridTrack(CSS::GridRepeat grid_repeat)
  140. : m_type(Type::Repeat)
  141. , m_grid_repeat(grid_repeat)
  142. {
  143. }
  144. ExplicitGridTrack::ExplicitGridTrack(CSS::GridSize grid_size)
  145. : m_type(Type::Default)
  146. , m_grid_size(grid_size)
  147. {
  148. }
  149. String ExplicitGridTrack::to_string() const
  150. {
  151. switch (m_type) {
  152. case Type::MinMax:
  153. return m_grid_minmax.to_string();
  154. case Type::Repeat:
  155. return m_grid_repeat.to_string();
  156. case Type::Default:
  157. return m_grid_size.to_string();
  158. default:
  159. VERIFY_NOT_REACHED();
  160. }
  161. }
  162. GridTrackSizeList::GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list, Vector<Vector<String>> line_names)
  163. : m_track_list(track_list)
  164. , m_line_names(line_names)
  165. {
  166. }
  167. GridTrackSizeList::GridTrackSizeList()
  168. : m_track_list({})
  169. , m_line_names({})
  170. {
  171. }
  172. GridTrackSizeList GridTrackSizeList::make_none()
  173. {
  174. return GridTrackSizeList();
  175. }
  176. String GridTrackSizeList::to_string() const
  177. {
  178. StringBuilder builder;
  179. auto print_line_names = [&](size_t index) -> void {
  180. builder.append("["sv);
  181. for (size_t y = 0; y < m_line_names[index].size(); ++y) {
  182. builder.append(m_line_names[index][y]);
  183. if (y != m_line_names[index].size() - 1)
  184. builder.append(" "sv);
  185. }
  186. builder.append("]"sv);
  187. };
  188. for (size_t i = 0; i < m_track_list.size(); ++i) {
  189. if (m_line_names.size() > 0 && m_line_names[i].size() > 0) {
  190. print_line_names(i);
  191. builder.append(" "sv);
  192. }
  193. builder.append(m_track_list[i].to_string());
  194. if (i < m_track_list.size() - 1)
  195. builder.append(" "sv);
  196. }
  197. if (m_line_names.size() > 0 && m_line_names[m_track_list.size()].size() > 0) {
  198. builder.append(" "sv);
  199. print_line_names(m_track_list.size());
  200. }
  201. return MUST(builder.to_string());
  202. }
  203. }