AvailableSpace.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. private:
  41. AvailableSize(Type type, CSSPixels);
  42. Type m_type {};
  43. CSSPixels m_value {};
  44. };
  45. class AvailableSpace {
  46. public:
  47. AvailableSpace(AvailableSize w, AvailableSize h)
  48. : width(move(w))
  49. , height(move(h))
  50. {
  51. }
  52. AvailableSize width;
  53. AvailableSize height;
  54. DeprecatedString to_deprecated_string() const;
  55. };
  56. }
  57. template<>
  58. struct AK::Formatter<Web::Layout::AvailableSize> : Formatter<StringView> {
  59. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSize const& available_size)
  60. {
  61. return Formatter<StringView>::format(builder, available_size.to_deprecated_string());
  62. }
  63. };
  64. template<>
  65. struct AK::Formatter<Web::Layout::AvailableSpace> : Formatter<StringView> {
  66. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSpace const& available_space)
  67. {
  68. return Formatter<StringView>::format(builder, available_space.to_deprecated_string());
  69. }
  70. };