Size.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/CSS/Length.h>
  8. #include <LibWeb/CSS/Percentage.h>
  9. namespace Web::CSS {
  10. class Size {
  11. public:
  12. enum class Type {
  13. Auto,
  14. Length,
  15. Percentage,
  16. MinContent,
  17. MaxContent,
  18. FitContent,
  19. None, // NOTE: This is only valid for max-width and max-height.
  20. };
  21. static Size make_auto();
  22. static Size make_length(Length);
  23. static Size make_percentage(Percentage);
  24. static Size make_min_content();
  25. static Size make_max_content();
  26. static Size make_fit_content(Length available_space);
  27. static Size make_none();
  28. bool is_auto() const { return m_type == Type::Auto; }
  29. bool is_length() const { return m_type == Type::Length; }
  30. bool is_percentage() const { return m_type == Type::Percentage; }
  31. bool is_min_content() const { return m_type == Type::MinContent; }
  32. bool is_max_content() const { return m_type == Type::MaxContent; }
  33. bool is_fit_content() const { return m_type == Type::FitContent; }
  34. bool is_none() const { return m_type == Type::None; }
  35. // FIXME: This is a stopgap API that will go away once all layout code is aware of CSS::Size.
  36. CSS::Length resolved(Layout::Node const&, Length const& reference_value) const;
  37. bool contains_percentage() const;
  38. CSS::Length const& length() const
  39. {
  40. VERIFY(is_length());
  41. return m_length_percentage.length();
  42. }
  43. CSS::Percentage const& percentage() const
  44. {
  45. VERIFY(is_percentage());
  46. return m_length_percentage.percentage();
  47. }
  48. CSS::Length const& fit_content_available_space() const
  49. {
  50. VERIFY(is_fit_content());
  51. return m_length_percentage.length();
  52. }
  53. private:
  54. Size(Type type, LengthPercentage);
  55. Type m_type {};
  56. CSS::LengthPercentage m_length_percentage;
  57. };
  58. }