Flex.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/String.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::CSS {
  11. // https://drafts.csswg.org/css-grid-2/#typedef-flex
  12. class Flex {
  13. public:
  14. enum class Type {
  15. Fr,
  16. };
  17. static Optional<Type> unit_from_name(StringView);
  18. Flex(double value, Type type);
  19. static Flex make_fr(double);
  20. Flex percentage_of(Percentage const&) const;
  21. String to_string() const;
  22. double to_fr() const;
  23. Type type() const { return m_type; }
  24. double raw_value() const { return m_value; }
  25. StringView unit_name() const;
  26. bool operator==(Flex const& other) const
  27. {
  28. return m_type == other.m_type && m_value == other.m_value;
  29. }
  30. int operator<=>(Flex const& other) const
  31. {
  32. auto this_fr = to_fr();
  33. auto other_fr = other.to_fr();
  34. if (this_fr < other_fr)
  35. return -1;
  36. if (this_fr > other_fr)
  37. return 1;
  38. return 0;
  39. }
  40. private:
  41. Type m_type;
  42. double m_value { 0 };
  43. };
  44. }
  45. template<>
  46. struct AK::Formatter<Web::CSS::Flex> : Formatter<StringView> {
  47. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Flex const& flex)
  48. {
  49. return Formatter<StringView>::format(builder, flex.to_string());
  50. }
  51. };