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