2022-02-21 14:49:47 -03:00
|
|
|
/*
|
2023-03-30 11:54:38 -03:00
|
|
|
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.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>
|
2022-02-21 14:49:47 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
class Frequency {
|
|
|
|
|
public:
|
|
|
|
|
enum class Type {
|
|
|
|
|
Hz,
|
|
|
|
|
kHz
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Optional<Type> unit_from_name(StringView);
|
|
|
|
|
|
2023-05-27 16:10:21 -03:00
|
|
|
Frequency(double value, Type type);
|
|
|
|
|
static Frequency make_hertz(double);
|
2022-02-21 14:49:47 -03:00
|
|
|
Frequency percentage_of(Percentage const&) const;
|
|
|
|
|
|
2023-08-22 08:25:30 -03:00
|
|
|
String to_string() const;
|
2023-05-27 16:10:21 -03:00
|
|
|
double to_hertz() const;
|
2022-02-21 14:49:47 -03:00
|
|
|
|
2023-04-11 08:57:32 -03:00
|
|
|
Type type() const { return m_type; }
|
2023-05-27 16:10:21 -03:00
|
|
|
double raw_value() const { return m_value; }
|
2024-08-14 12:25:48 -03:00
|
|
|
StringView unit_name() const;
|
2023-04-11 08:57:32 -03:00
|
|
|
|
2022-02-21 14:49:47 -03:00
|
|
|
bool operator==(Frequency const& other) const
|
|
|
|
|
{
|
|
|
|
|
return m_type == other.m_type && m_value == other.m_value;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 12:05:56 -03:00
|
|
|
static Frequency resolve_calculated(NonnullRefPtr<CalculatedStyleValue> 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:
|
|
|
|
|
Type m_type;
|
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
|
|
|
}
|
|
|
|
|
};
|