GridTrackSize.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 GridTrackSize {
  12. public:
  13. enum class Type {
  14. Length,
  15. Percentage,
  16. FlexibleLength,
  17. // TODO: Max-Content
  18. };
  19. GridTrackSize(Length);
  20. GridTrackSize(Percentage);
  21. GridTrackSize(float);
  22. ~GridTrackSize();
  23. static GridTrackSize make_auto();
  24. Type type() const { return m_type; }
  25. bool is_length() const { return m_type == Type::Length; }
  26. bool is_percentage() const { return m_type == Type::Percentage; }
  27. bool is_flexible_length() const { return m_type == Type::FlexibleLength; }
  28. Length length() const;
  29. Percentage percentage() const { return m_percentage; }
  30. float flexible_length() const { return m_flexible_length; }
  31. // https://drafts.csswg.org/css-grid/#layout-algorithm
  32. // Intrinsic sizing function - min-content, max-content, auto, fit-content()
  33. // FIXME: Add missing properties once implemented.
  34. bool is_intrinsic_track_sizing() const
  35. {
  36. return (m_type == Type::Length && m_length.is_auto());
  37. }
  38. bool is_definite() const
  39. {
  40. return (m_type == Type::Length && !m_length.is_auto()) || is_percentage();
  41. }
  42. String to_string() const;
  43. bool operator==(GridTrackSize const& other) const
  44. {
  45. return m_type == other.type()
  46. && m_length == other.length()
  47. && m_percentage == other.percentage()
  48. && m_flexible_length == other.flexible_length();
  49. }
  50. private:
  51. Type m_type;
  52. // Length includes a RefPtr<CalculatedStyleValue> member, but we can't include the header StyleValue.h as it includes
  53. // this file already. To break the cyclic dependency, we must initialize m_length in the constructor.
  54. Length m_length;
  55. Percentage m_percentage { Percentage(0) };
  56. float m_flexible_length { 0 };
  57. };
  58. class MetaGridTrackSize {
  59. public:
  60. MetaGridTrackSize(CSS::GridTrackSize);
  61. MetaGridTrackSize(CSS::GridTrackSize min_grid_track_size, CSS::GridTrackSize max_grid_track_size);
  62. bool is_min_max() const { return m_is_min_max; }
  63. GridTrackSize grid_track_size() const& { return m_min_grid_track_size; }
  64. GridTrackSize min_grid_track_size() const& { return m_min_grid_track_size; }
  65. GridTrackSize max_grid_track_size() const& { return m_max_grid_track_size; }
  66. String to_string() const;
  67. bool operator==(MetaGridTrackSize const& other) const
  68. {
  69. return m_min_grid_track_size == other.min_grid_track_size()
  70. && m_max_grid_track_size == other.max_grid_track_size();
  71. }
  72. private:
  73. GridTrackSize m_min_grid_track_size;
  74. GridTrackSize m_max_grid_track_size;
  75. bool m_is_min_max { false };
  76. };
  77. class ExplicitTrackSizing {
  78. public:
  79. enum class Type {
  80. AutoFit,
  81. AutoFill,
  82. };
  83. ExplicitTrackSizing();
  84. ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>);
  85. ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>, int repeat_count);
  86. ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>, Type);
  87. static ExplicitTrackSizing make_auto() { return ExplicitTrackSizing(); };
  88. bool is_repeat() const { return m_is_repeat; }
  89. bool is_auto_fill() const { return m_is_auto_fill; }
  90. bool is_auto_fit() const { return m_is_auto_fit; }
  91. int repeat_count() const { return m_repeat_count; }
  92. Vector<CSS::MetaGridTrackSize> meta_grid_track_sizes() const& { return m_meta_grid_track_sizes; }
  93. String to_string() const;
  94. bool operator==(ExplicitTrackSizing const& other) const
  95. {
  96. return m_meta_grid_track_sizes == other.meta_grid_track_sizes();
  97. }
  98. private:
  99. Vector<CSS::MetaGridTrackSize> m_meta_grid_track_sizes;
  100. bool m_is_repeat { false };
  101. bool m_is_auto_fill { false };
  102. bool m_is_auto_fit { false };
  103. int m_repeat_count { 0 };
  104. };
  105. }