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