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/module.hpp"
11#include "strings.hpp"
12#include "i18n/translate.hpp"
13#include "unicode/gstring.hpp"
14#include "image/module.hpp"
15#include "font/module.hpp"
16#include <string>
17#include <type_traits>
18#include <memory>
19#include <variant>
20
21namespace hi::inline v1 {
22
31class text : public std::variant<std::monostate, std::string, gstring, translate> {
32 using std::variant<std::monostate, std::string, gstring, translate>::variant;
33
38 [[nodiscard]] constexpr friend bool to_bool(text const& rhs) noexcept
39 {
40 return not std::holds_alternative<std::monostate>(rhs);
41 }
42
45 [[nodiscard]] constexpr friend std::string to_string(hi::text const& rhs) noexcept
46 {
47 // clang-format off
48 return std::visit(
50 [](std::monostate const &) { return std::string{}; },
51 [](std::string const &x) { return x; },
52 [](gstring const &x) { return hi::to_string(x); },
53 [](translate const &x) { return x(); }
54 },
55 rhs);
56 // clang-format on
57 }
58
61 [[nodiscard]] constexpr friend gstring to_gstring(hi::text const& rhs) noexcept
62 {
63 // clang-format off
64 return std::visit(
66 [](std::monostate const &) { return gstring{}; },
67 [](std::string const &x) { return to_gstring(std::string_view{x}); },
68 [](gstring const &x) { return x; },
69 [](translate const &x) { return to_gstring(std::string_view{x()}); }
70 },
71 rhs);
72 // clang-format on
73 }
74};
75
85class icon : public std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixmap<sfloat_rgba16>>
86{
87 using std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixmap<sfloat_rgba16>>::variant;
88
91 [[nodiscard]] constexpr friend bool to_bool(icon const& rhs) noexcept
92 {
93 return not std::holds_alternative<std::monostate>(rhs);
94 }
95};
96
97} // namespace hi::inline v1
98
99template<typename CharT>
100struct std::formatter<hi::text, CharT> : std::formatter<std::string_view, CharT> {
101 auto format(hi::text const& t, auto& fc)
102 {
103 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
104 }
105};
106
107namespace hi::inline v1 {
108
119class label {
120public:
123 hi::icon icon;
124
128 hi::text text;
129
134 constexpr label(std::convertible_to<hi::icon> auto&& icon, std::convertible_to<hi::text> auto&& text) noexcept :
136 {
137 }
138
142 constexpr label(std::convertible_to<hi::text> auto&& text) noexcept : icon(), text(hi_forward(text)) {}
143
147 constexpr label(std::convertible_to<hi::icon> auto&& icon) noexcept : icon(hi_forward(icon)), text() {}
148
151 constexpr label() noexcept : icon(), text() {}
152
153 constexpr label(label const& other) noexcept = default;
154 constexpr label& operator=(label const& other) noexcept = default;
155 constexpr label(label&& other) noexcept = default;
156 constexpr label& operator=(label&& other) noexcept = default;
157
158 [[nodiscard]] constexpr bool empty() const noexcept
159 {
160 return not(to_bool(icon) or to_bool(text));
161 }
162
163 constexpr explicit operator bool() const noexcept
164 {
165 return not empty();
166 }
167
173 [[nodiscard]] constexpr friend bool operator==(label const& lhs, label const& rhs) noexcept = default;
174};
175
176template<>
178 template<fixed_string>
179 [[nodiscard]] auto& get(label&) const noexcept;
180
181 template<>
182 [[nodiscard]] auto& get<"text">(label& rhs) const noexcept
183 {
184 return rhs.text;
185 }
186
187 template<>
188 [[nodiscard]] auto& get<"icon">(label& rhs) const noexcept
189 {
190 return rhs.icon;
191 }
192};
193
194} // namespace hi::inline v1
195
196template<typename CharT>
197struct std::formatter<hi::label, CharT> : std::formatter<hi::text, CharT> {
198 auto format(hi::label const& t, auto& fc)
199 {
200 return std::formatter<hi::text, CharT>::format(t.text, fc);
201 }
202};
#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:13
gstring to_gstring(std::u32string_view rhs, char32_t new_line_char=U'\u2029') noexcept
Convert a UTF-32 string to a grapheme-string.
geometry/margins.hpp
Definition cache.hpp:11
A set of glyph-ids of a font which composites into a single glyph.
Definition glyph_ids.hpp:134
A localizable message.
Definition translate.hpp:155
A variant of text.
Definition label.hpp:31
constexpr friend gstring to_gstring(hi::text const &rhs) noexcept
Convert the text into a gstring.
Definition label.hpp:61
constexpr friend bool to_bool(text const &rhs) noexcept
Check if text contains a string.
Definition label.hpp:38
constexpr friend std::string to_string(hi::text const &rhs) noexcept
Convert the text into a std::string.
Definition label.hpp:45
A variant of icon.
Definition label.hpp:86
constexpr friend bool to_bool(icon const &rhs) noexcept
Check if icon contains an image.
Definition label.hpp:91
A label consisting of localizable text and an icon.
Definition label.hpp:119
hi::icon icon
The icon.
Definition label.hpp:123
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:147
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:134
hi::text text
Localizable text.
Definition label.hpp:128
constexpr label() noexcept
Construct a empty label.
Definition label.hpp:151
constexpr label(std::convertible_to< hi::text > auto &&text) noexcept
Construct a new label from text.
Definition label.hpp:142
This selector allows access to member variable by name.
Definition type_traits.hpp:642
Helper type to turn a set of lambdas into a single overloaded type to pass to std::visit().
Definition type_traits.hpp:671
T to_string(T... args)