Time.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefPtr.h>
  8. #include <AK/String.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::CSS {
  11. class Time {
  12. public:
  13. enum class Type {
  14. Calculated,
  15. S,
  16. Ms,
  17. };
  18. static Optional<Type> unit_from_name(StringView);
  19. Time(int value, Type type);
  20. Time(float value, Type type);
  21. static Time make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  22. static Time make_seconds(float);
  23. Time percentage_of(Percentage const&) const;
  24. bool is_calculated() const { return m_type == Type::Calculated; }
  25. String to_string() const;
  26. float to_seconds() const;
  27. bool operator==(Time const& other) const
  28. {
  29. if (is_calculated())
  30. return m_calculated_style == other.m_calculated_style;
  31. return m_type == other.m_type && m_value == other.m_value;
  32. }
  33. bool operator!=(Time const& other) const
  34. {
  35. return !(*this == other);
  36. }
  37. private:
  38. StringView unit_name() const;
  39. Type m_type;
  40. float m_value { 0 };
  41. RefPtr<CalculatedStyleValue> m_calculated_style;
  42. };
  43. }
  44. template<>
  45. struct AK::Formatter<Web::CSS::Time> : Formatter<StringView> {
  46. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Time const& time)
  47. {
  48. return Formatter<StringView>::format(builder, time.to_string());
  49. }
  50. };