HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
label.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10#include "utility.hpp"
11#include "strings.hpp"
12#include "type_traits.hpp"
13#include "fixed_string.hpp"
14#include "i18n/translate.hpp"
15#include "unicode/gstring.hpp"
16#include "URL.hpp"
17#include "pixel_map.hpp"
18#include "rapid/sfloat_rgba16.hpp"
19#include "text/glyph_ids.hpp"
20#include "text/elusive_icon.hpp"
21#include "text/hikogui_icon.hpp"
22#include <string>
23#include <type_traits>
24#include <memory>
25#include <variant>
26
27namespace hi::inline v1 {
28
37class text : public std::variant<std::monostate, std::string, gstring, translate> {
38 using std::variant<std::monostate, std::string, gstring, translate>::variant;
39
44 [[nodiscard]] constexpr friend bool to_bool(text const& rhs) noexcept
45 {
46 return not std::holds_alternative<std::monostate>(rhs);
47 }
48
51 [[nodiscard]] constexpr friend std::string to_string(hi::text const& rhs) noexcept
52 {
53 // clang-format off
54 return std::visit(
56 [](std::monostate const &) { return std::string{}; },
57 [](std::string const &x) { return x; },
58 [](gstring const &x) { return hi::to_string(x); },
59 [](translate const &x) { return x(); }
60 },
61 rhs);
62 // clang-format on
63 }
64
67 [[nodiscard]] constexpr friend gstring to_gstring(hi::text const& rhs) noexcept
68 {
69 // clang-format off
70 return std::visit(
72 [](std::monostate const &) { return gstring{}; },
73 [](std::string const &x) { return to_gstring(std::string_view{x}); },
74 [](gstring const &x) { return x; },
75 [](translate const &x) { return to_gstring(std::string_view{x()}); }
76 },
77 rhs);
78 // clang-format on
79 }
80};
81
91class icon : public std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixel_map<sfloat_rgba16>>
92{
93 using std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixel_map<sfloat_rgba16>>::variant;
94
97 [[nodiscard]] constexpr friend bool to_bool(icon const& rhs) noexcept
98 {
99 return not std::holds_alternative<std::monostate>(rhs);
100 }
101};
102
103} // namespace hi::inline v1
104
105template<typename CharT>
106struct std::formatter<hi::text, CharT> : std::formatter<std::string_view, CharT> {
107 auto format(hi::text const& t, auto& fc)
108 {
109 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
110 }
111};
112
113namespace hi::inline v1 {
114
125class label {
126public:
129 hi::icon icon;
130
134 hi::text text;
135
140 constexpr label(std::convertible_to<hi::icon> auto&& icon, std::convertible_to<hi::text> auto&& text) noexcept :
142 {
143 }
144
148 constexpr label(std::convertible_to<hi::text> auto&& text) noexcept : icon(), text(hi_forward(text)) {}
149
153 constexpr label(std::convertible_to<hi::icon> auto&& icon) noexcept : icon(hi_forward(icon)), text() {}
154
157 constexpr label() noexcept : icon(), text() {}
158
159 constexpr label(label const& other) noexcept = default;
160 constexpr label& operator=(label const& other) noexcept = default;
161 constexpr label(label&& other) noexcept = default;
162 constexpr label& operator=(label&& other) noexcept = default;
163
164 [[nodiscard]] constexpr bool empty() const noexcept
165 {
166 return not(to_bool(icon) or to_bool(text));
167 }
168
169 constexpr explicit operator bool() const noexcept
170 {
171 return not empty();
172 }
173
179 [[nodiscard]] constexpr friend bool operator==(label const& lhs, label const& rhs) noexcept = default;
180};
181
182template<>
184 template<fixed_string>
185 [[nodiscard]] auto& get(label&) const noexcept;
186
187 template<>
188 [[nodiscard]] auto& get<"text">(label& rhs) const noexcept
189 {
190 return rhs.text;
191 }
192
193 template<>
194 [[nodiscard]] auto& get<"icon">(label& rhs) const noexcept
195 {
196 return rhs.icon;
197 }
198};
199
200} // namespace hi::inline v1
201
202template<typename CharT>
203struct std::formatter<hi::label, CharT> : std::formatter<hi::text, CharT> {
204 auto format(hi::label const& t, auto& fc)
205 {
206 return std::formatter<hi::text, CharT>::format(t.text, fc);
207 }
208};
Utilities used by the HikoGUI library itself.
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
constexpr std::string to_string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:215
DOXYGEN BUG.
Definition algorithm.hpp:15
gstring to_gstring(std::u32string_view rhs, char32_t new_line_char=U'\u2029') noexcept
Convert a UTF-32 string to a grapheme-string.
The HikoGUI namespace.
Definition ascii.hpp:19
A 2D canvas of pixels.
Definition pixel_map.hpp:111
A localizable message.
Definition translate.hpp:155
A variant of text.
Definition label.hpp:37
constexpr friend gstring to_gstring(hi::text const &rhs) noexcept
Convert the text into a gstring.
Definition label.hpp:67
constexpr friend bool to_bool(text const &rhs) noexcept
Check if text contains a string.
Definition label.hpp:44
constexpr friend std::string to_string(hi::text const &rhs) noexcept
Convert the text into a std::string.
Definition label.hpp:51
A variant of icon.
Definition label.hpp:92
constexpr friend bool to_bool(icon const &rhs) noexcept
Check if icon contains an image.
Definition label.hpp:97
A label consisting of localizable text and an icon.
Definition label.hpp:125
hi::icon icon
The icon.
Definition label.hpp:129
constexpr friend bool operator==(label const &lhs, label const &rhs) noexcept=default
Compare if both labels are equal.
constexpr label(std::convertible_to< hi::icon > auto &&icon) noexcept
Construct a new label from an icon.
Definition label.hpp:153
constexpr label(std::convertible_to< hi::icon > auto &&icon, std::convertible_to< hi::text > auto &&text) noexcept
Construct a new label from an icon and text.
Definition label.hpp:140
hi::text text
Localizable text.
Definition label.hpp:134
constexpr label() noexcept
Construct a empty label.
Definition label.hpp:157
constexpr label(std::convertible_to< hi::text > auto &&text) noexcept
Construct a new label from text.
Definition label.hpp:148
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:135
This selector allows access to member variable by name.
Definition type_traits.hpp:583
Helper type to turn a set of lambdas into a single overloaded type to pass to std::visit().
Definition type_traits.hpp:617
T to_string(T... args)