2022-02-21 19:29:43 +00:00
|
|
|
/*
|
2025-05-16 20:02:16 +01:00
|
|
|
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
2022-02-21 19:29:43 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-06 19:02:26 +01:00
|
|
|
#include <AK/String.h>
|
2025-05-16 20:02:16 +01:00
|
|
|
#include <LibWeb/CSS/SerializationMode.h>
|
2025-09-02 13:48:49 +01:00
|
|
|
#include <LibWeb/CSS/Units.h>
|
2022-02-21 19:29:43 +00:00
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
|
class Time {
|
|
|
|
public:
|
2025-09-02 13:48:49 +01:00
|
|
|
Time(double value, TimeUnit unit);
|
2023-05-27 21:10:21 +02:00
|
|
|
static Time make_seconds(double);
|
2022-02-21 19:29:43 +00:00
|
|
|
Time percentage_of(Percentage const&) const;
|
|
|
|
|
2025-05-16 20:02:16 +01:00
|
|
|
String to_string(SerializationMode = SerializationMode::Normal) const;
|
2023-05-26 23:30:11 +03:30
|
|
|
double to_milliseconds() const;
|
2023-05-27 21:10:21 +02:00
|
|
|
double to_seconds() const;
|
2022-02-21 19:29:43 +00:00
|
|
|
|
2023-05-27 21:10:21 +02:00
|
|
|
double raw_value() const { return m_value; }
|
2025-09-02 13:48:49 +01:00
|
|
|
TimeUnit unit() const { return m_unit; }
|
|
|
|
StringView unit_name() const { return CSS::to_string(m_unit); }
|
2023-04-11 12:57:32 +01:00
|
|
|
|
2022-02-21 19:29:43 +00:00
|
|
|
bool operator==(Time const& other) const
|
|
|
|
{
|
2025-09-02 13:48:49 +01:00
|
|
|
return m_unit == other.m_unit && m_value == other.m_value;
|
2022-02-21 19:29:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:55:18 +01:00
|
|
|
int operator<=>(Time const& other) const
|
|
|
|
{
|
|
|
|
auto this_seconds = to_seconds();
|
|
|
|
auto other_seconds = other.to_seconds();
|
|
|
|
|
|
|
|
if (this_seconds < other_seconds)
|
|
|
|
return -1;
|
|
|
|
if (this_seconds > other_seconds)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-15 15:18:27 -06:00
|
|
|
static Time resolve_calculated(NonnullRefPtr<CalculatedStyleValue const> const&, Layout::Node const&, Time const& reference_value);
|
2024-08-02 14:28:24 +02:00
|
|
|
|
2022-02-21 19:29:43 +00:00
|
|
|
private:
|
2025-09-02 13:48:49 +01:00
|
|
|
TimeUnit m_unit;
|
2023-05-27 21:10:21 +02:00
|
|
|
double m_value { 0 };
|
2022-02-21 19:29:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2022-07-27 11:41:31 +01:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Web::CSS::Time> : Formatter<StringView> {
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Time const& time)
|
|
|
|
{
|
2023-08-22 12:25:30 +01:00
|
|
|
return Formatter<StringView>::format(builder, time.to_string());
|
2022-07-27 11:41:31 +01:00
|
|
|
}
|
|
|
|
};
|