AvailableSpace.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/DeprecatedString.h>
  8. #include <AK/Format.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() const
  30. {
  31. return m_value;
  32. }
  33. CSSPixels to_px_or_zero() const
  34. {
  35. if (!is_definite())
  36. return 0.0f;
  37. return m_value;
  38. }
  39. DeprecatedString to_deprecated_string() const;
  40. bool operator==(AvailableSize const& other) const = default;
  41. private:
  42. AvailableSize(Type type, CSSPixels);
  43. Type m_type {};
  44. CSSPixels m_value {};
  45. };
  46. class AvailableSpace {
  47. public:
  48. AvailableSpace(AvailableSize w, AvailableSize h)
  49. : width(move(w))
  50. , height(move(h))
  51. {
  52. }
  53. bool operator==(AvailableSpace const& other) const = default;
  54. AvailableSize width;
  55. AvailableSize height;
  56. DeprecatedString to_deprecated_string() const;
  57. };
  58. }
  59. template<>
  60. struct AK::Formatter<Web::Layout::AvailableSize> : Formatter<StringView> {
  61. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSize const& available_size)
  62. {
  63. return Formatter<StringView>::format(builder, available_size.to_deprecated_string());
  64. }
  65. };
  66. template<>
  67. struct AK::Formatter<Web::Layout::AvailableSpace> : Formatter<StringView> {
  68. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSpace const& available_space)
  69. {
  70. return Formatter<StringView>::format(builder, available_space.to_deprecated_string());
  71. }
  72. };