AvailableSpace.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace Web::Layout {
  11. class AvailableSize {
  12. public:
  13. enum class Type {
  14. Definite,
  15. Indefinite,
  16. MinContent,
  17. MaxContent,
  18. };
  19. static AvailableSize make_definite(float);
  20. static AvailableSize make_indefinite();
  21. static AvailableSize make_min_content();
  22. static AvailableSize make_max_content();
  23. bool is_definite() const { return m_type == Type::Definite; }
  24. bool is_indefinite() const { return m_type == Type::Indefinite; }
  25. bool is_min_content() const { return m_type == Type::MinContent; }
  26. bool is_max_content() const { return m_type == Type::MaxContent; }
  27. bool is_intrinsic_sizing_constraint() const { return is_min_content() || is_max_content(); }
  28. float to_px() const
  29. {
  30. return m_value;
  31. }
  32. float to_px_or_zero() const
  33. {
  34. if (!is_definite())
  35. return 0.0f;
  36. return m_value;
  37. }
  38. String to_string() const;
  39. private:
  40. AvailableSize(Type type, float);
  41. Type m_type {};
  42. float m_value {};
  43. };
  44. class AvailableSpace {
  45. public:
  46. AvailableSpace(AvailableSize w, AvailableSize h)
  47. : width(move(w))
  48. , height(move(h))
  49. {
  50. }
  51. AvailableSize width;
  52. AvailableSize height;
  53. String to_string() const;
  54. };
  55. }
  56. template<>
  57. struct AK::Formatter<Web::Layout::AvailableSize> : Formatter<StringView> {
  58. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSize const& available_size)
  59. {
  60. return Formatter<StringView>::format(builder, available_size.to_string());
  61. }
  62. };
  63. template<>
  64. struct AK::Formatter<Web::Layout::AvailableSpace> : Formatter<StringView> {
  65. ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSpace const& available_space)
  66. {
  67. return Formatter<StringView>::format(builder, available_space.to_string());
  68. }
  69. };