2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-01 18:26:19 +01:00
|
|
|
* Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
|
2022-04-17 09:24:55 +04:30
|
|
|
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-02-28 16:20:29 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
#include <AK/Concepts.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-09-18 09:49:51 +02:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2022-04-09 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2021-09-05 21:38:36 +02:00
|
|
|
#include <LibGfx/SystemTheme.h>
|
2019-02-28 16:20:29 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
namespace Detail {
|
|
|
|
|
struct Boolean {
|
|
|
|
|
bool value;
|
|
|
|
|
};
|
2023-02-20 19:03:44 +01:00
|
|
|
using VariantUnderlyingType = AK::Variant<Empty, Boolean, float, i32, i64, u32, u64, DeprecatedString, Color, Gfx::IntPoint, Gfx::IntSize, Gfx::IntRect, Gfx::TextAlignment, Gfx::ColorRole, Gfx::AlignmentRole, Gfx::FlagRole, Gfx::MetricRole, Gfx::PathRole, NonnullRefPtr<Gfx::Bitmap const>, NonnullRefPtr<Gfx::Font const>, GUI::Icon>;
|
2022-04-17 09:24:55 +04:30
|
|
|
}
|
2020-01-27 10:55:10 +01:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
class Variant : public Detail::VariantUnderlyingType {
|
|
|
|
|
public:
|
|
|
|
|
using Detail::VariantUnderlyingType::Variant;
|
|
|
|
|
using Detail::VariantUnderlyingType::operator=;
|
2019-02-28 16:20:29 +01:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
Variant(JsonValue const&);
|
|
|
|
|
Variant& operator=(JsonValue const&);
|
|
|
|
|
Variant(bool v)
|
|
|
|
|
: Variant(Detail::Boolean { v })
|
2019-07-31 07:07:59 +02:00
|
|
|
{
|
2021-05-26 23:23:31 +10:00
|
|
|
}
|
2022-04-17 09:24:55 +04:30
|
|
|
Variant& operator=(bool v)
|
2021-05-26 23:23:31 +10:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
set(Detail::Boolean { v });
|
|
|
|
|
return *this;
|
2019-07-31 07:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-27 10:55:10 +01:00
|
|
|
template<typename T>
|
2022-10-17 00:06:11 +02:00
|
|
|
Variant(T&& value)
|
2022-12-04 18:02:33 +00:00
|
|
|
requires(IsConstructible<DeprecatedString, T>)
|
|
|
|
|
: Variant(DeprecatedString(forward<T>(value)))
|
2019-04-13 12:35:19 +02:00
|
|
|
{
|
|
|
|
|
}
|
2022-04-17 09:24:55 +04:30
|
|
|
template<typename T>
|
2022-10-17 00:06:11 +02:00
|
|
|
Variant& operator=(T&& v)
|
2022-12-04 18:02:33 +00:00
|
|
|
requires(IsConstructible<DeprecatedString, T>)
|
2020-01-27 10:55:10 +01:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
set(DeprecatedString(v));
|
2022-04-17 09:24:55 +04:30
|
|
|
return *this;
|
2020-01-27 10:55:10 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
template<OneOfIgnoringCV<Gfx::Bitmap, Gfx::Font> T>
|
|
|
|
|
Variant(T const& value)
|
2023-02-20 19:03:44 +01:00
|
|
|
: Variant(NonnullRefPtr<RemoveCV<T> const>(value))
|
2019-02-28 16:20:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
2022-04-17 09:24:55 +04:30
|
|
|
template<OneOfIgnoringCV<Gfx::Bitmap, Gfx::Font> T>
|
|
|
|
|
Variant& operator=(T&& value)
|
2021-07-27 17:23:04 +02:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
set(NonnullRefPtr<RemoveCV<T>>(forward<T>(value)));
|
|
|
|
|
return *this;
|
2021-07-27 17:23:04 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
~Variant() = default;
|
2019-04-11 22:52:34 +02:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
bool is_valid() const { return !has<Empty>(); }
|
|
|
|
|
bool is_bool() const { return has<Detail::Boolean>(); }
|
|
|
|
|
bool is_i32() const { return has<i32>(); }
|
|
|
|
|
bool is_i64() const { return has<i64>(); }
|
|
|
|
|
bool is_u32() const { return has<u32>(); }
|
|
|
|
|
bool is_u64() const { return has<u64>(); }
|
|
|
|
|
bool is_float() const { return has<float>(); }
|
2022-12-04 18:02:33 +00:00
|
|
|
bool is_string() const { return has<DeprecatedString>(); }
|
2023-02-20 19:03:44 +01:00
|
|
|
bool is_bitmap() const { return has<NonnullRefPtr<Gfx::Bitmap const>>(); }
|
2022-04-17 09:24:55 +04:30
|
|
|
bool is_color() const { return has<Color>(); }
|
|
|
|
|
bool is_icon() const { return has<GUI::Icon>(); }
|
|
|
|
|
bool is_point() const { return has<Gfx::IntPoint>(); }
|
|
|
|
|
bool is_size() const { return has<Gfx::IntSize>(); }
|
|
|
|
|
bool is_rect() const { return has<Gfx::IntRect>(); }
|
2023-02-20 19:03:44 +01:00
|
|
|
bool is_font() const { return has<NonnullRefPtr<Gfx::Font const>>(); }
|
2022-04-17 09:24:55 +04:30
|
|
|
bool is_text_alignment() const { return has<Gfx::TextAlignment>(); }
|
|
|
|
|
bool is_color_role() const { return has<Gfx::ColorRole>(); }
|
|
|
|
|
bool is_alignment_role() const { return has<Gfx::AlignmentRole>(); }
|
|
|
|
|
bool is_flag_role() const { return has<Gfx::FlagRole>(); }
|
|
|
|
|
bool is_metric_role() const { return has<Gfx::MetricRole>(); }
|
|
|
|
|
bool is_path_role() const { return has<Gfx::PathRole>(); }
|
2019-04-11 22:52:34 +02:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
bool as_bool() const { return get<Detail::Boolean>().value; }
|
2019-04-11 22:52:34 +02:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
bool to_bool() const
|
2019-02-28 16:20:29 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
return visit(
|
|
|
|
|
[](Empty) { return false; },
|
|
|
|
|
[](Detail::Boolean v) { return v.value; },
|
|
|
|
|
[](Integral auto v) { return v != 0; },
|
2022-12-28 22:43:30 +01:00
|
|
|
[](Gfx::IntPoint const& v) { return !v.is_zero(); },
|
|
|
|
|
[](OneOf<Gfx::IntRect, Gfx::IntSize> auto const& v) { return !v.is_empty(); },
|
2022-04-17 09:24:55 +04:30
|
|
|
[](Enum auto const&) { return true; },
|
2023-02-20 19:03:44 +01:00
|
|
|
[](OneOf<float, DeprecatedString, Color, NonnullRefPtr<Gfx::Font const>, NonnullRefPtr<Gfx::Bitmap const>, GUI::Icon> auto const&) { return true; });
|
2019-02-28 16:20:29 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
i32 as_i32() const { return get<i32>(); }
|
|
|
|
|
i64 as_i64() const { return get<i64>(); }
|
|
|
|
|
u32 as_u32() const { return get<u32>(); }
|
|
|
|
|
u64 as_u64() const { return get<u64>(); }
|
2019-02-28 16:20:29 +01:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
template<Integral T>
|
|
|
|
|
T to_integer() const
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
return visit(
|
|
|
|
|
[](Empty) -> T { return 0; },
|
|
|
|
|
[](Integral auto v) { return static_cast<T>(v); },
|
|
|
|
|
[](FloatingPoint auto v) { return (T)v; },
|
|
|
|
|
[](Detail::Boolean v) -> T { return v.value ? 1 : 0; },
|
2022-12-04 18:02:33 +00:00
|
|
|
[](DeprecatedString const& v) {
|
2022-04-17 09:24:55 +04:30
|
|
|
if constexpr (IsUnsigned<T>)
|
|
|
|
|
return v.to_uint<T>().value_or(0u);
|
|
|
|
|
else
|
|
|
|
|
return v.to_int<T>().value_or(0);
|
|
|
|
|
},
|
|
|
|
|
[](Enum auto const&) -> T { return 0; },
|
2023-02-20 19:03:44 +01:00
|
|
|
[](OneOf<Gfx::IntPoint, Gfx::IntRect, Gfx::IntSize, Color, NonnullRefPtr<Gfx::Font const>, NonnullRefPtr<Gfx::Bitmap const>, GUI::Icon> auto const&) -> T { return 0; });
|
2022-04-17 09:24:55 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i32 to_i32() const { return to_integer<i32>(); }
|
|
|
|
|
i64 to_i64() const { return to_integer<i64>(); }
|
|
|
|
|
float as_float() const { return get<float>(); }
|
2019-03-24 04:28:36 +01:00
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
float as_float_or(float fallback) const
|
2019-03-18 04:54:07 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<float>())
|
|
|
|
|
return *p;
|
|
|
|
|
return fallback;
|
2019-03-18 04:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
Gfx::IntPoint as_point() const { return get<Gfx::IntPoint>(); }
|
|
|
|
|
Gfx::IntSize as_size() const { return get<Gfx::IntSize>(); }
|
|
|
|
|
Gfx::IntRect as_rect() const { return get<Gfx::IntRect>(); }
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString as_string() const { return get<DeprecatedString>(); }
|
2023-02-20 19:03:44 +01:00
|
|
|
Gfx::Bitmap const& as_bitmap() const { return *get<NonnullRefPtr<Gfx::Bitmap const>>(); }
|
2022-04-17 09:24:55 +04:30
|
|
|
GUI::Icon as_icon() const { return get<GUI::Icon>(); }
|
|
|
|
|
Color as_color() const { return get<Color>(); }
|
2023-02-20 19:03:44 +01:00
|
|
|
Gfx::Font const& as_font() const { return *get<NonnullRefPtr<Gfx::Font const>>(); }
|
2019-10-22 21:37:11 +02:00
|
|
|
|
2020-05-21 19:36:09 +02:00
|
|
|
Gfx::TextAlignment to_text_alignment(Gfx::TextAlignment default_value) const
|
|
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Gfx::TextAlignment>())
|
|
|
|
|
return *p;
|
|
|
|
|
return default_value;
|
2020-05-21 19:36:09 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-05 21:38:36 +02:00
|
|
|
Gfx::ColorRole to_color_role() const
|
|
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Gfx::ColorRole>())
|
|
|
|
|
return *p;
|
|
|
|
|
return Gfx::ColorRole::NoRole;
|
2021-09-05 21:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-01 18:26:19 +01:00
|
|
|
Gfx::AlignmentRole to_alignment_role() const
|
|
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Gfx::AlignmentRole>())
|
|
|
|
|
return *p;
|
|
|
|
|
return Gfx::AlignmentRole::NoRole;
|
2022-01-01 18:26:19 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:25:49 +00:00
|
|
|
Gfx::FlagRole to_flag_role() const
|
|
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Gfx::FlagRole>())
|
|
|
|
|
return *p;
|
|
|
|
|
return Gfx::FlagRole::NoRole;
|
2021-11-01 16:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 16:58:57 +01:00
|
|
|
Gfx::MetricRole to_metric_role() const
|
|
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Gfx::MetricRole>())
|
|
|
|
|
return *p;
|
|
|
|
|
return Gfx::MetricRole::NoRole;
|
2021-10-27 16:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gfx::PathRole to_path_role() const
|
|
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Gfx::PathRole>())
|
|
|
|
|
return *p;
|
|
|
|
|
return Gfx::PathRole::NoRole;
|
2021-10-27 16:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-03 11:34:02 +02:00
|
|
|
Color to_color(Color default_value = {}) const
|
2019-03-18 04:54:07 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (auto const* p = get_pointer<Color>())
|
|
|
|
|
return *p;
|
2022-12-04 18:02:33 +00:00
|
|
|
if (auto const* p = get_pointer<DeprecatedString>())
|
2022-04-17 09:24:55 +04:30
|
|
|
return Color::from_string(*p).value_or(default_value);
|
2019-03-18 04:54:07 +01:00
|
|
|
return default_value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string() const
|
2022-04-17 09:24:55 +04:30
|
|
|
{
|
|
|
|
|
return visit(
|
2022-12-04 18:02:33 +00:00
|
|
|
[](Empty) -> DeprecatedString { return "[null]"; },
|
2023-02-09 19:19:17 +00:00
|
|
|
[](DeprecatedString v) { return v; },
|
2022-12-04 18:02:33 +00:00
|
|
|
[](Gfx::TextAlignment v) { return DeprecatedString::formatted("Gfx::TextAlignment::{}", Gfx::to_string(v)); },
|
|
|
|
|
[](Gfx::ColorRole v) { return DeprecatedString::formatted("Gfx::ColorRole::{}", Gfx::to_string(v)); },
|
|
|
|
|
[](Gfx::AlignmentRole v) { return DeprecatedString::formatted("Gfx::AlignmentRole::{}", Gfx::to_string(v)); },
|
|
|
|
|
[](Gfx::FlagRole v) { return DeprecatedString::formatted("Gfx::FlagRole::{}", Gfx::to_string(v)); },
|
|
|
|
|
[](Gfx::MetricRole v) { return DeprecatedString::formatted("Gfx::MetricRole::{}", Gfx::to_string(v)); },
|
|
|
|
|
[](Gfx::PathRole v) { return DeprecatedString::formatted("Gfx::PathRole::{}", Gfx::to_string(v)); },
|
2023-02-20 19:03:44 +01:00
|
|
|
[](NonnullRefPtr<Gfx::Font const> const& font) { return DeprecatedString::formatted("[Font: {}]", font->name()); },
|
|
|
|
|
[](NonnullRefPtr<Gfx::Bitmap const> const&) -> DeprecatedString { return "[Gfx::Bitmap]"; },
|
2022-12-04 18:02:33 +00:00
|
|
|
[](GUI::Icon const&) -> DeprecatedString { return "[GUI::Icon]"; },
|
|
|
|
|
[](Detail::Boolean v) { return DeprecatedString::formatted("{}", v.value); },
|
|
|
|
|
[](auto const& v) { return DeprecatedString::formatted("{}", v); });
|
2022-04-17 09:24:55 +04:30
|
|
|
}
|
2019-02-28 16:20:29 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(Variant const&) const;
|
|
|
|
|
bool operator<(Variant const&) const;
|
2019-02-28 16:20:29 +01:00
|
|
|
};
|
2019-06-23 07:54:46 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|