Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
4 * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#pragma once
10
11#include <AK/Concepts.h>
12#include <AK/DeprecatedString.h>
13#include <LibGUI/Icon.h>
14#include <LibGfx/Bitmap.h>
15#include <LibGfx/Font/Font.h>
16#include <LibGfx/SystemTheme.h>
17
18namespace GUI {
19
20namespace Detail {
21struct Boolean {
22 bool value;
23};
24using 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>;
25}
26
27class Variant : public Detail::VariantUnderlyingType {
28public:
29 using Detail::VariantUnderlyingType::Variant;
30 using Detail::VariantUnderlyingType::operator=;
31
32 Variant(JsonValue const&);
33 Variant& operator=(JsonValue const&);
34 Variant(bool v)
35 : Variant(Detail::Boolean { v })
36 {
37 }
38 Variant& operator=(bool v)
39 {
40 set(Detail::Boolean { v });
41 return *this;
42 }
43
44 template<typename T>
45 Variant(T&& value)
46 requires(IsConstructible<DeprecatedString, T>)
47 : Variant(DeprecatedString(forward<T>(value)))
48 {
49 }
50 template<typename T>
51 Variant& operator=(T&& v)
52 requires(IsConstructible<DeprecatedString, T>)
53 {
54 set(DeprecatedString(v));
55 return *this;
56 }
57
58 template<OneOfIgnoringCV<Gfx::Bitmap, Gfx::Font> T>
59 Variant(T const& value)
60 : Variant(NonnullRefPtr<RemoveCV<T> const>(value))
61 {
62 }
63 template<OneOfIgnoringCV<Gfx::Bitmap, Gfx::Font> T>
64 Variant& operator=(T&& value)
65 {
66 set(NonnullRefPtr<RemoveCV<T>>(forward<T>(value)));
67 return *this;
68 }
69
70 ~Variant() = default;
71
72 bool is_valid() const { return !has<Empty>(); }
73 bool is_bool() const { return has<Detail::Boolean>(); }
74 bool is_i32() const { return has<i32>(); }
75 bool is_i64() const { return has<i64>(); }
76 bool is_u32() const { return has<u32>(); }
77 bool is_u64() const { return has<u64>(); }
78 bool is_float() const { return has<float>(); }
79 bool is_string() const { return has<DeprecatedString>(); }
80 bool is_bitmap() const { return has<NonnullRefPtr<Gfx::Bitmap const>>(); }
81 bool is_color() const { return has<Color>(); }
82 bool is_icon() const { return has<GUI::Icon>(); }
83 bool is_point() const { return has<Gfx::IntPoint>(); }
84 bool is_size() const { return has<Gfx::IntSize>(); }
85 bool is_rect() const { return has<Gfx::IntRect>(); }
86 bool is_font() const { return has<NonnullRefPtr<Gfx::Font const>>(); }
87 bool is_text_alignment() const { return has<Gfx::TextAlignment>(); }
88 bool is_color_role() const { return has<Gfx::ColorRole>(); }
89 bool is_alignment_role() const { return has<Gfx::AlignmentRole>(); }
90 bool is_flag_role() const { return has<Gfx::FlagRole>(); }
91 bool is_metric_role() const { return has<Gfx::MetricRole>(); }
92 bool is_path_role() const { return has<Gfx::PathRole>(); }
93
94 bool as_bool() const { return get<Detail::Boolean>().value; }
95
96 bool to_bool() const
97 {
98 return visit(
99 [](Empty) { return false; },
100 [](Detail::Boolean v) { return v.value; },
101 [](DeprecatedString const& v) { return !v.is_null(); },
102 [](Integral auto v) { return v != 0; },
103 [](Gfx::IntPoint const& v) { return !v.is_zero(); },
104 [](OneOf<Gfx::IntRect, Gfx::IntSize> auto const& v) { return !v.is_empty(); },
105 [](Enum auto const&) { return true; },
106 [](OneOf<float, DeprecatedString, Color, NonnullRefPtr<Gfx::Font const>, NonnullRefPtr<Gfx::Bitmap const>, GUI::Icon> auto const&) { return true; });
107 }
108
109 i32 as_i32() const { return get<i32>(); }
110 i64 as_i64() const { return get<i64>(); }
111 u32 as_u32() const { return get<u32>(); }
112 u64 as_u64() const { return get<u64>(); }
113
114 template<Integral T>
115 T to_integer() const
116 {
117 return visit(
118 [](Empty) -> T { return 0; },
119 [](Integral auto v) { return static_cast<T>(v); },
120 [](FloatingPoint auto v) { return (T)v; },
121 [](Detail::Boolean v) -> T { return v.value ? 1 : 0; },
122 [](DeprecatedString const& v) {
123 if constexpr (IsUnsigned<T>)
124 return v.to_uint<T>().value_or(0u);
125 else
126 return v.to_int<T>().value_or(0);
127 },
128 [](Enum auto const&) -> T { return 0; },
129 [](OneOf<Gfx::IntPoint, Gfx::IntRect, Gfx::IntSize, Color, NonnullRefPtr<Gfx::Font const>, NonnullRefPtr<Gfx::Bitmap const>, GUI::Icon> auto const&) -> T { return 0; });
130 }
131
132 i32 to_i32() const { return to_integer<i32>(); }
133 i64 to_i64() const { return to_integer<i64>(); }
134 float as_float() const { return get<float>(); }
135
136 float as_float_or(float fallback) const
137 {
138 if (auto const* p = get_pointer<float>())
139 return *p;
140 return fallback;
141 }
142
143 Gfx::IntPoint as_point() const { return get<Gfx::IntPoint>(); }
144 Gfx::IntSize as_size() const { return get<Gfx::IntSize>(); }
145 Gfx::IntRect as_rect() const { return get<Gfx::IntRect>(); }
146 DeprecatedString as_string() const { return get<DeprecatedString>(); }
147 Gfx::Bitmap const& as_bitmap() const { return *get<NonnullRefPtr<Gfx::Bitmap const>>(); }
148 GUI::Icon as_icon() const { return get<GUI::Icon>(); }
149 Color as_color() const { return get<Color>(); }
150 Gfx::Font const& as_font() const { return *get<NonnullRefPtr<Gfx::Font const>>(); }
151
152 Gfx::TextAlignment to_text_alignment(Gfx::TextAlignment default_value) const
153 {
154 if (auto const* p = get_pointer<Gfx::TextAlignment>())
155 return *p;
156 return default_value;
157 }
158
159 Gfx::ColorRole to_color_role() const
160 {
161 if (auto const* p = get_pointer<Gfx::ColorRole>())
162 return *p;
163 return Gfx::ColorRole::NoRole;
164 }
165
166 Gfx::AlignmentRole to_alignment_role() const
167 {
168 if (auto const* p = get_pointer<Gfx::AlignmentRole>())
169 return *p;
170 return Gfx::AlignmentRole::NoRole;
171 }
172
173 Gfx::FlagRole to_flag_role() const
174 {
175 if (auto const* p = get_pointer<Gfx::FlagRole>())
176 return *p;
177 return Gfx::FlagRole::NoRole;
178 }
179
180 Gfx::MetricRole to_metric_role() const
181 {
182 if (auto const* p = get_pointer<Gfx::MetricRole>())
183 return *p;
184 return Gfx::MetricRole::NoRole;
185 }
186
187 Gfx::PathRole to_path_role() const
188 {
189 if (auto const* p = get_pointer<Gfx::PathRole>())
190 return *p;
191 return Gfx::PathRole::NoRole;
192 }
193
194 Color to_color(Color default_value = {}) const
195 {
196 if (auto const* p = get_pointer<Color>())
197 return *p;
198 if (auto const* p = get_pointer<DeprecatedString>())
199 return Color::from_string(*p).value_or(default_value);
200 return default_value;
201 }
202
203 DeprecatedString to_deprecated_string() const
204 {
205 return visit(
206 [](Empty) -> DeprecatedString { return "[null]"; },
207 [](DeprecatedString v) { return v; },
208 [](Gfx::TextAlignment v) { return DeprecatedString::formatted("Gfx::TextAlignment::{}", Gfx::to_string(v)); },
209 [](Gfx::ColorRole v) { return DeprecatedString::formatted("Gfx::ColorRole::{}", Gfx::to_string(v)); },
210 [](Gfx::AlignmentRole v) { return DeprecatedString::formatted("Gfx::AlignmentRole::{}", Gfx::to_string(v)); },
211 [](Gfx::FlagRole v) { return DeprecatedString::formatted("Gfx::FlagRole::{}", Gfx::to_string(v)); },
212 [](Gfx::MetricRole v) { return DeprecatedString::formatted("Gfx::MetricRole::{}", Gfx::to_string(v)); },
213 [](Gfx::PathRole v) { return DeprecatedString::formatted("Gfx::PathRole::{}", Gfx::to_string(v)); },
214 [](NonnullRefPtr<Gfx::Font const> const& font) { return DeprecatedString::formatted("[Font: {}]", font->name()); },
215 [](NonnullRefPtr<Gfx::Bitmap const> const&) -> DeprecatedString { return "[Gfx::Bitmap]"; },
216 [](GUI::Icon const&) -> DeprecatedString { return "[GUI::Icon]"; },
217 [](Detail::Boolean v) { return DeprecatedString::formatted("{}", v.value); },
218 [](auto const& v) { return DeprecatedString::formatted("{}", v); });
219 }
220
221 bool operator==(Variant const&) const;
222 bool operator<(Variant const&) const;
223};
224
225}