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 "pixel_map.hpp"
17#include "rapid/sfloat_rgba16.hpp"
18#include "text/glyph_ids.hpp"
19#include "text/elusive_icon.hpp"
20#include "text/hikogui_icon.hpp"
21#include <string>
22#include <type_traits>
23#include <memory>
24#include <variant>
25
26namespace hi::inline v1 {
27
36class text : public std::variant<std::monostate, std::string, gstring, translate> {
37 using std::variant<std::monostate, std::string, gstring, translate>::variant;
38
43 [[nodiscard]] constexpr friend bool to_bool(text const& rhs) noexcept
44 {
45 return not std::holds_alternative<std::monostate>(rhs);
46 }
47
50 [[nodiscard]] constexpr friend std::string to_string(hi::text const& rhs) noexcept
51 {
52 // clang-format off
53 return std::visit(
55 [](std::monostate const &) { return std::string{}; },
56 [](std::string const &x) { return x; },
57 [](gstring const &x) { return hi::to_string(x); },
58 [](translate const &x) { return x(); }
59 },
60 rhs);
61 // clang-format on
62 }
63
66 [[nodiscard]] constexpr friend gstring to_gstring(hi::text const& rhs) noexcept
67 {
68 // clang-format off
69 return std::visit(
71 [](std::monostate const &) { return gstring{}; },
72 [](std::string const &x) { return to_gstring(std::string_view{x}); },
73 [](gstring const &x) { return x; },
74 [](translate const &x) { return to_gstring(std::string_view{x()}); }
75 },
76 rhs);
77 // clang-format on
78 }
79};
80
90class icon : public std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixel_map<sfloat_rgba16>>
91{
92 using std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixel_map<sfloat_rgba16>>::variant;
93
96 [[nodiscard]] constexpr friend bool to_bool(icon const& rhs) noexcept
97 {
98 return not std::holds_alternative<std::monostate>(rhs);
99 }
100};
101
102} // namespace hi::inline v1
103
104template<typename CharT>
105struct std::formatter<hi::text, CharT> : std::formatter<std::string_view, CharT> {
106 auto format(hi::text const& t, auto& fc)
107 {
108 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
109 }
110};
111
112namespace hi::inline v1 {
113
124class label {
125public:
128 hi::icon icon;
129
133 hi::text text;
134
139 constexpr label(std::convertible_to<hi::icon> auto&& icon, std::convertible_to<hi::text> auto&& text) noexcept :
141 {
142 }
143
147 constexpr label(std::convertible_to<hi::text> auto&& text) noexcept : icon(), text(hi_forward(text)) {}
148
152 constexpr label(std::convertible_to<hi::icon> auto&& icon) noexcept : icon(hi_forward(icon)), text() {}
153
156 constexpr label() noexcept : icon(), text() {}
157
158 constexpr label(label const& other) noexcept = default;
159 constexpr label& operator=(label const& other) noexcept = default;
160 constexpr label(label&& other) noexcept = default;
161 constexpr label& operator=(label&& other) noexcept = default;
162
163 [[nodiscard]] constexpr bool empty() const noexcept
164 {
165 return not(to_bool(icon) or to_bool(text));
166 }
167
168 constexpr explicit operator bool() const noexcept
169 {
170 return not empty();
171 }
172
178 [[nodiscard]] constexpr friend bool operator==(label const& lhs, label const& rhs) noexcept = default;
179};
180
181template<>
183 template<fixed_string>
184 [[nodiscard]] auto& get(label&) const noexcept;
185
186 template<>
187 [[nodiscard]] auto& get<"text">(label& rhs) const noexcept
188 {
189 return rhs.text;
190 }
191
192 template<>
193 [[nodiscard]] auto& get<"icon">(label& rhs) const noexcept
194 {
195 return rhs.icon;
196 }
197};
198
199} // namespace hi::inline v1
200
201template<typename CharT>
202struct std::formatter<hi::label, CharT> : std::formatter<hi::text, CharT> {
203 auto format(hi::label const& t, auto& fc)
204 {
205 return std::formatter<hi::text, CharT>::format(t.text, fc);
206 }
207};
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:36
constexpr friend gstring to_gstring(hi::text const &rhs) noexcept
Convert the text into a gstring.
Definition label.hpp:66
constexpr friend bool to_bool(text const &rhs) noexcept
Check if text contains a string.
Definition label.hpp:43
constexpr friend std::string to_string(hi::text const &rhs) noexcept
Convert the text into a std::string.
Definition label.hpp:50
A variant of icon.
Definition label.hpp:91
constexpr friend bool to_bool(icon const &rhs) noexcept
Check if icon contains an image.
Definition label.hpp:96
A label consisting of localizable text and an icon.
Definition label.hpp:124
hi::icon icon
The icon.
Definition label.hpp:128
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:152
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:139
hi::text text
Localizable text.
Definition label.hpp:133
constexpr label() noexcept
Construct a empty label.
Definition label.hpp:156
constexpr label(std::convertible_to< hi::text > auto &&text) noexcept
Construct a new label from text.
Definition label.hpp:147
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:592
Helper type to turn a set of lambdas into a single overloaded type to pass to std::visit().
Definition type_traits.hpp:626
T to_string(T... args)