Size.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_px(float);
  23. static Size make_length(Length);
  24. static Size make_percentage(Percentage);
  25. static Size make_min_content();
  26. static Size make_max_content();
  27. static Size make_fit_content(Length available_space);
  28. static Size make_none();
  29. bool is_auto() const { return m_type == Type::Auto; }
  30. bool is_length() const { return m_type == Type::Length; }
  31. bool is_percentage() const { return m_type == Type::Percentage; }
  32. bool is_min_content() const { return m_type == Type::MinContent; }
  33. bool is_max_content() const { return m_type == Type::MaxContent; }
  34. bool is_fit_content() const { return m_type == Type::FitContent; }
  35. bool is_none() const { return m_type == Type::None; }
  36. // FIXME: This is a stopgap API that will go away once all layout code is aware of CSS::Size.
  37. CSS::Length resolved(Layout::Node const&, Length const& reference_value) const;
  38. bool contains_percentage() const;
  39. CSS::Length const& length() const
  40. {
  41. VERIFY(is_length());
  42. return m_length_percentage.length();
  43. }
  44. CSS::Percentage const& percentage() const
  45. {
  46. VERIFY(is_percentage());
  47. return m_length_percentage.percentage();
  48. }
  49. CSS::Length const& fit_content_available_space() const
  50. {
  51. VERIFY(is_fit_content());
  52. return m_length_percentage.length();
  53. }
  54. private:
  55. Size(Type type, LengthPercentage);
  56. Type m_type {};
  57. CSS::LengthPercentage m_length_percentage;
  58. };
  59. }