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 "l10n/module.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
43
44
47 [[nodiscard]] constexpr friend gstring to_gstring(hi::text const& rhs) noexcept
48 {
49 // clang-format off
50 return std::visit(
52 [](std::monostate const &) { return gstring{}; },
53 [](std::string const &x) { return to_gstring(std::string_view{x}); },
54 [](gstring const &x) { return x; },
55 [](translate const &x) { return x(); }
56 },
57 rhs);
58 // clang-format on
59 }
60
63 [[nodiscard]] constexpr friend std::string to_string(hi::text const& rhs) noexcept
64 {
65 return to_string(to_gstring(rhs));
66 }
67};
68
78class icon : public std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixmap<sfloat_rgba16>>
79{
80 using std::variant<std::monostate, elusive_icon, hikogui_icon, glyph_ids, pixmap<sfloat_rgba16>>::variant;
81
84 [[nodiscard]] constexpr friend bool to_bool(icon const& rhs) noexcept
85 {
86 return not std::holds_alternative<std::monostate>(rhs);
87 }
88};
89
90} // namespace hi::inline v1
91
92template<typename CharT>
93struct std::formatter<hi::text, CharT> : std::formatter<std::string_view, CharT> {
94 auto format(hi::text const& t, auto& fc)
95 {
96 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
97 }
98};
99
100namespace hi::inline v1 {
101
112class label {
113public:
116 hi::icon icon;
117
121 hi::text text;
122
127 constexpr label(std::convertible_to<hi::icon> auto&& icon, std::convertible_to<hi::text> auto&& text) noexcept :
129 {
130 }
131
135 constexpr label(std::convertible_to<hi::text> auto&& text) noexcept : icon(), text(hi_forward(text)) {}
136
140 constexpr label(std::convertible_to<hi::icon> auto&& icon) noexcept : icon(hi_forward(icon)), text() {}
141
144 constexpr label() noexcept : icon(), text() {}
145
146 constexpr label(label const& other) noexcept = default;
147 constexpr label& operator=(label const& other) noexcept = default;
148 constexpr label(label&& other) noexcept = default;
149 constexpr label& operator=(label&& other) noexcept = default;
150
151 [[nodiscard]] constexpr bool empty() const noexcept
152 {
153 return not(to_bool(icon) or to_bool(text));
154 }
155
156 constexpr explicit operator bool() const noexcept
157 {
158 return not empty();
159 }
160
166 [[nodiscard]] constexpr friend bool operator==(label const& lhs, label const& rhs) noexcept = default;
167};
168
169template<>
171 template<fixed_string>
172 [[nodiscard]] auto& get(label&) const noexcept;
173
174 template<>
175 [[nodiscard]] auto& get<"text">(label& rhs) const noexcept
176 {
177 return rhs.text;
178 }
179
180 template<>
181 [[nodiscard]] auto& get<"icon">(label& rhs) const noexcept
182 {
183 return rhs.icon;
184 }
185};
186
187} // namespace hi::inline v1
188
189template<typename CharT>
190struct std::formatter<hi::label, CharT> : std::formatter<hi::text, CharT> {
191 auto format(hi::label const& t, auto& fc)
192 {
193 return std::formatter<hi::text, CharT>::format(t.text, fc);
194 }
195};
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr gstring to_gstring(std::u32string_view rhs, unicode_normalize_config config=unicode_normalize_config::NFC()) noexcept
Convert a UTF-32 string-view to a grapheme-string.
Definition gstring.hpp:158
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:135
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:47
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:63
A variant of icon.
Definition label.hpp:79
constexpr friend bool to_bool(icon const &rhs) noexcept
Check if icon contains an image.
Definition label.hpp:84
A label consisting of localizable text and an icon.
Definition label.hpp:112
hi::icon icon
The icon.
Definition label.hpp:116
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:140
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:127
hi::text text
Localizable text.
Definition label.hpp:121
constexpr label() noexcept
Construct a empty label.
Definition label.hpp:144
constexpr label(std::convertible_to< hi::text > auto &&text) noexcept
Construct a new label from text.
Definition label.hpp:135
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)