AvailableSpace.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/String.h>
  9. #include <LibWeb/Forward.h>
  10. #include <LibWeb/PixelUnits.h>
  11. namespace Web::Layout {
  12. class AvailableSize {
  13. public:
  14. enum class Type {
  15. Definite,
  16. Indefinite,
  17. MinContent,
  18. MaxContent,
  19. };
  20. static AvailableSize make_definite(CSSPixels);
  21. static AvailableSize make_indefinite();
  22. static AvailableSize make_min_content();
  23. static AvailableSize make_max_content();
  24. bool is_definite() const { return m_type == Type::Definite; }
  25. bool is_indefinite() const { return m_type == Type::Indefinite; }
  26. bool is_min_content() const { return m_type == Type::MinContent; }
  27. bool is_max_content() const { return m_type == Type::MaxContent; }
  28. bool is_intrinsic_sizing_constraint() const { return is_min_content() || is_max_content(); }
  29. CSSPixels to_px_or_zero() const
  30. {
  31. if (!is_definite())
  32. return 0;
  33. return m_value;
  34. }
  35. String to_string() const;
  36. bool operator==(AvailableSize const& other) const = default;
  37. bool operator<(AvailableSize const& other) const { return m_value < other.m_value; }
  38. private:
  39. AvailableSize(Type type, CSSPixels);
  40. Type m_type {};
  41. CSSPixels m_value {};
  42. };
  43. inline bool operator>(CSSPixels left, AvailableSize const& right)
  44. {
  45. if (right.is_max_content() || right.is_indefinite())
  46. return false;
  47. if (right.is_min_content())
  48. return true;
  49. return left > right.to_px_or_zero();
  50. }
  51. inline bool operator<(CSSPixels left, AvailableSize const& right)
  52. {
  53. if (right.is_max_content() || right.is_indefinite())
  54. return true;
  55. if (right.is_min_content())
  56. return false;
  57. return left < right.to_px_or_zero();
  58. }
  59. inline bool operator<(AvailableSize const& left, CSSPixels right)
  60. {
  61. if (left.is_min_content())
  62. return true;
  63. if (left.is_max_content() || left.is_indefinite())
  64. return false;
  65. return left.to_px_or_zero() < right;
  66. }
  67. class AvailableSpace {
  68. public:
  69. AvailableSpace(AvailableSize w, AvailableSize h)
  70. : width(move(w))
  71. , height(move(h))
  72. {
  73. }
  74. bool operator==(AvailableSpace const& other) const = default;
  75. AvailableSize width;
  76. AvailableSize height;
  77. String to_string() const;
  78. };
  79. }
  80. template<>
  81. struct AK::Formatter<Web::Layout::AvailableSize> : Formatter<StringView> {
  82. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSize const& available_size)
  83. {
  84. return Formatter<StringView>::format(builder, available_size.to_string());
  85. }
  86. };
  87. template<>
  88. struct AK::Formatter<Web::Layout::AvailableSpace> : Formatter<StringView> {
  89. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSpace const& available_space)
  90. {
  91. return Formatter<StringView>::format(builder, available_space.to_string());
  92. }
  93. };