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/module.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
30class text_variant : public std::variant<std::monostate, hi::text, hi::translate> {
31 using std::variant<std::monostate, hi::text, hi::translate>::variant;
32
37 [[nodiscard]] constexpr friend bool to_bool(text_variant const& rhs) noexcept
38 {
39 return not std::holds_alternative<std::monostate>(rhs);
40 }
41
44 [[nodiscard]] constexpr friend text to_text(hi::text_variant const& rhs) noexcept
45 {
46 // clang-format off
47 return std::visit(
49 [](std::monostate const &) { return text{}; },
50 [](hi::text const &x) { return x; },
51 [](hi::translate const &x) { return x(); }
52 },
53 rhs);
54 // clang-format on
55 }
56
57 [[nodiscard]] constexpr friend gstring to_gstring(hi::text_variant const& rhs) noexcept
58 {
59 return to_gstring(to_text(rhs));
60 }
61
62 [[nodiscard]] constexpr friend std::string to_string(hi::text_variant const& rhs) noexcept
63 {
64 return to_string(to_text(rhs));
65 }
66
67 [[nodiscard]] constexpr friend std::wstring to_wstring(hi::text_variant const& rhs) noexcept
68 {
69 return to_wstring(to_string(rhs));
70 }
71};
72
81class icon : public std::variant<std::monostate, elusive_icon, hikogui_icon, font_book::font_glyph_type, pixmap<sfloat_rgba16>> {
82 using std::variant<std::monostate, elusive_icon, hikogui_icon, font_book::font_glyph_type, pixmap<sfloat_rgba16>>::variant;
83
86 [[nodiscard]] constexpr friend bool to_bool(icon const& rhs) noexcept
87 {
88 return not std::holds_alternative<std::monostate>(rhs);
89 }
90};
91
92} // namespace hi::inline v1
93
94template<typename CharT>
95struct std::formatter<hi::text_variant, CharT> : std::formatter<std::string_view, CharT> {
96 auto format(hi::text_variant const& t, auto& fc)
97 {
98 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
99 }
100};
101
102namespace hi::inline v1 {
103
114class label {
115public:
118 hi::icon icon;
119
123 hi::text_variant text;
124
129 constexpr label(std::convertible_to<hi::icon> auto&& icon, std::convertible_to<hi::text_variant> auto&& text) noexcept :
130 icon(hi_forward(icon)), text(hi_forward(text))
131 {
132 }
133
137 constexpr label(std::convertible_to<hi::text_variant> auto&& text) noexcept : icon(), text(hi_forward(text)) {}
138
142 constexpr label(std::convertible_to<hi::icon> auto&& icon) noexcept : icon(hi_forward(icon)), text() {}
143
146 constexpr label() noexcept : icon(), text() {}
147
148 constexpr label(label const& other) noexcept = default;
149 constexpr label& operator=(label const& other) noexcept = default;
150 constexpr label(label&& other) noexcept = default;
151 constexpr label& operator=(label&& other) noexcept = default;
152
153 [[nodiscard]] constexpr bool empty() const noexcept
154 {
155 return not(to_bool(icon) or to_bool(text));
156 }
157
158 constexpr explicit operator bool() const noexcept
159 {
160 return not empty();
161 }
162
168 [[nodiscard]] constexpr friend bool operator==(label const& lhs, label const& rhs) noexcept = default;
169};
170
171template<>
173 template<fixed_string>
174 [[nodiscard]] auto& get(label&) const noexcept;
175
176 template<>
177 [[nodiscard]] auto& get<"text">(label& rhs) const noexcept
178 {
179 return rhs.text;
180 }
181
182 template<>
183 [[nodiscard]] auto& get<"icon">(label& rhs) const noexcept
184 {
185 return rhs.icon;
186 }
187};
188
189} // namespace hi::inline v1
190
191template<typename CharT>
192struct std::formatter<hi::label, CharT> : std::formatter<hi::text_variant, CharT> {
193 auto format(hi::label const& t, auto& fc)
194 {
195 return std::formatter<hi::text_variant, CharT>::format(t.text, fc);
196 }
197};
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Definition font_book.hpp:31
A variant of text.
Definition label.hpp:30
constexpr friend bool to_bool(text_variant const &rhs) noexcept
Check if text contains a string.
Definition label.hpp:37
constexpr friend text to_text(hi::text_variant const &rhs) noexcept
Convert the text into a gstring.
Definition label.hpp:44
A variant of icon.
Definition label.hpp:81
constexpr friend bool to_bool(icon const &rhs) noexcept
Check if icon contains an image.
Definition label.hpp:86
A label consisting of localizable text and an icon.
Definition label.hpp:114
constexpr label(std::convertible_to< hi::text_variant > auto &&text) noexcept
Construct a new label from text.
Definition label.hpp:137
hi::icon icon
The icon.
Definition label.hpp:118
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:142
hi::text_variant text
Localizable text.
Definition label.hpp:123
constexpr label(std::convertible_to< hi::icon > auto &&icon, std::convertible_to< hi::text_variant > auto &&text) noexcept
Construct a new label from an icon and text.
Definition label.hpp:129
constexpr label() noexcept
Construct a empty label.
Definition label.hpp:146
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)
T to_wstring(T... args)