/src/serenity/Userland/Libraries/LibWeb/CSS/Time.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include "Time.h" |
8 | | #include <LibWeb/CSS/Percentage.h> |
9 | | #include <LibWeb/CSS/StyleValues/CSSMathValue.h> |
10 | | |
11 | | namespace Web::CSS { |
12 | | |
13 | | Time::Time(double value, Type type) |
14 | 0 | : m_type(type) |
15 | 0 | , m_value(value) |
16 | 0 | { |
17 | 0 | } |
18 | | |
19 | | Time Time::make_seconds(double value) |
20 | 0 | { |
21 | 0 | return { value, Type::S }; |
22 | 0 | } |
23 | | |
24 | | Time Time::percentage_of(Percentage const& percentage) const |
25 | 0 | { |
26 | 0 | return Time { percentage.as_fraction() * m_value, m_type }; |
27 | 0 | } |
28 | | |
29 | | String Time::to_string() const |
30 | 0 | { |
31 | 0 | return MUST(String::formatted("{}s", to_seconds())); |
32 | 0 | } |
33 | | |
34 | | double Time::to_seconds() const |
35 | 0 | { |
36 | 0 | switch (m_type) { |
37 | 0 | case Type::S: |
38 | 0 | return m_value; |
39 | 0 | case Type::Ms: |
40 | 0 | return m_value / 1000.0; |
41 | 0 | } |
42 | 0 | VERIFY_NOT_REACHED(); |
43 | 0 | } |
44 | | |
45 | | double Time::to_milliseconds() const |
46 | 0 | { |
47 | 0 | switch (m_type) { |
48 | 0 | case Type::S: |
49 | 0 | return m_value * 1000.0; |
50 | 0 | case Type::Ms: |
51 | 0 | return m_value; |
52 | 0 | } |
53 | 0 | VERIFY_NOT_REACHED(); |
54 | 0 | } |
55 | | |
56 | | StringView Time::unit_name() const |
57 | 0 | { |
58 | 0 | switch (m_type) { |
59 | 0 | case Type::S: |
60 | 0 | return "s"sv; |
61 | 0 | case Type::Ms: |
62 | 0 | return "ms"sv; |
63 | 0 | } |
64 | 0 | VERIFY_NOT_REACHED(); |
65 | 0 | } |
66 | | |
67 | | Optional<Time::Type> Time::unit_from_name(StringView name) |
68 | 0 | { |
69 | 0 | if (name.equals_ignoring_ascii_case("s"sv)) { |
70 | 0 | return Type::S; |
71 | 0 | } else if (name.equals_ignoring_ascii_case("ms"sv)) { |
72 | 0 | return Type::Ms; |
73 | 0 | } |
74 | 0 | return {}; |
75 | 0 | } |
76 | | |
77 | | Time Time::resolve_calculated(NonnullRefPtr<CSSMathValue> const& calculated, Layout::Node const&, Time const& reference_value) |
78 | 0 | { |
79 | 0 | return calculated->resolve_time_percentage(reference_value).value(); |
80 | 0 | } |
81 | | |
82 | | } |