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/utility.hpp"
11#include "../unicode/unicode.hpp"
12#include "../image/image.hpp"
13#include "../font/font.hpp"
14#include "txt.hpp"
15#include "../macros.hpp"
16#include <string>
17#include <type_traits>
18#include <memory>
19#include <variant>
20
21hi_export_module(hikogui.l10n.label);
22
23
24hi_export namespace hi::inline v1 {
25
35class icon : public std::variant<std::monostate, elusive_icon, hikogui_icon, font_book::font_glyph_type, pixmap<sfloat_rgba16>>
36{
37 using std::variant<std::monostate, elusive_icon, hikogui_icon, font_book::font_glyph_type, pixmap<sfloat_rgba16>>::variant;
38
41 [[nodiscard]] constexpr friend bool to_bool(icon const& rhs) noexcept
42 {
43 return not std::holds_alternative<std::monostate>(rhs);
44 }
45};
46
57class label {
58public:
61 hi::icon icon;
62
66 txt text;
67
72 template<std::convertible_to<hi::icon> Icon, std::convertible_to<txt> Txt>
73 constexpr label(Icon && icon, Txt && text) noexcept :
74 icon(std::forward<Icon>(icon)), text(std::forward<Txt>(text))
75 {
76 }
77
81 template<std::convertible_to<txt> Txt>
82 constexpr label(Txt && text) noexcept : icon(), text(std::forward<Txt>(text)) {}
83
87 template<std::convertible_to<hi::icon> Icon>
88 constexpr label(Icon&& icon) noexcept : icon(std::forward<Icon>(icon)), text() {}
89
92 constexpr label() noexcept : icon(), text() {}
93
94 label(label const& other) noexcept = default;
95 label& operator=(label const& other) noexcept = default;
96 label(label&& other) noexcept = default;
97 label& operator=(label&& other) noexcept = default;
98
99 [[nodiscard]] constexpr bool empty() const noexcept
100 {
101 return not(to_bool(icon) or to_bool(text));
102 }
103
104 constexpr explicit operator bool() const noexcept
105 {
106 return not empty();
107 }
108
114 [[nodiscard]] constexpr friend bool operator==(label const&, label const&) noexcept = default;
115};
116
117template<>
118struct selector<label> {
119 template<fixed_string>
120 [[nodiscard]] auto& get(label&) const noexcept;
121
122 template<>
123 [[nodiscard]] auto& get<"text">(label& rhs) const noexcept
124 {
125 return rhs.text;
126 }
127
128 template<>
129 [[nodiscard]] auto& get<"icon">(label& rhs) const noexcept
130 {
131 return rhs.icon;
132 }
133};
134
135} // namespace hi::inline v1
136
137// XXX #617 MSVC bug does not handle partial specialization in modules.
138hi_export template<>
139struct std::formatter<hi::label, char> : std::formatter<std::string_view, char> {
140 auto format(hi::label const& t, auto& fc) const
141 {
142 return std::formatter<std::string_view, char>::format(std::string{t.text}, fc);
143 }
144};
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition font_book.hpp:34
A variant of icon.
Definition label.hpp:36
constexpr friend bool to_bool(icon const &rhs) noexcept
Check if icon contains an image.
Definition label.hpp:41
A label consisting of localizable text and an icon.
Definition label.hpp:57
constexpr friend bool operator==(label const &, label const &) noexcept=default
Compare if both labels are equal.
hi::icon icon
The icon.
Definition label.hpp:61
constexpr label(Txt &&text) noexcept
Construct a new label from text.
Definition label.hpp:82
constexpr label(Icon &&icon, Txt &&text) noexcept
Construct a new label from an icon and text.
Definition label.hpp:73
txt text
Localizable text.
Definition label.hpp:66
constexpr label() noexcept
Construct a empty label.
Definition label.hpp:92
constexpr label(Icon &&icon) noexcept
Construct a new label from an icon.
Definition label.hpp:88