HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
label.hpp
1// Copyright Take Vos 2020.
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
5#pragma once
6
7#include "required.hpp"
8#include "strings.hpp"
9#include "observable.hpp"
10#include "icon.hpp"
11#include "l10n.hpp"
12#include "text/translation.hpp"
13#include <string>
14#include <type_traits>
15#include <memory>
16
17namespace tt {
18namespace detail {
19
21public:
22 label_arguments_base() noexcept = default;
23 virtual ~label_arguments_base() = default;
26 label_arguments_base &operator=(label_arguments_base const &) = delete;
27 label_arguments_base &operator=(label_arguments_base &&) = delete;
28
29 [[nodiscard]] virtual std::u8string format(std::u8string_view fmt) const noexcept = 0;
30 [[nodiscard]] virtual std::unique_ptr<label_arguments_base> make_unique_copy() const noexcept = 0;
31 [[nodiscard]] virtual bool eq(label_arguments_base &other) const noexcept = 0;
32};
33
34template<typename... Types>
36public:
37 template<typename... Args>
38 label_arguments(Args &&... args) noexcept : _args(std::forward<Args>(args)...) {}
39
40 [[nodiscard]] std::u8string format(std::u8string_view fmt) const noexcept override
41 {
42 return std::apply(
43 [&fmt](auto const &... args) {
44 return tt::format(fmt, args...);
45 },
46 _args);
47 }
48
49 [[nodiscard]] std::unique_ptr<label_arguments_base> make_unique_copy() const noexcept override
50 {
51 return std::apply(
52 [](auto const &... args) {
53 return std::make_unique<label_arguments<Types...>>(args...);
54 },
55 _args);
56 }
57
58 [[nodiscard]] bool eq(label_arguments_base &other) const noexcept override
59 {
60 auto *other_ = dynamic_cast<label_arguments *>(&other);
61 if (other_ == nullptr) {
62 return false;
63 }
64
65 return _args == other_->_args;
66 }
67
68private:
69 std::tuple<Types...> _args;
70};
71
72} // namespace detail
73
76class label {
77public:
78 template<typename... Args>
79 label(tt::icon icon, l10n fmt, Args &&... args) noexcept :
80 _icon(std::move(icon)),
81 _msgid(std::move(fmt.msgid)),
82 _args(std::make_unique<detail::label_arguments<std::remove_cvref_t<Args>...>>(std::forward<Args>(args)...))
83 {
84 }
85
86 template<typename... Args>
87 label(l10n fmt, Args &&... args) noexcept : label(tt::icon{}, std::move(fmt), std::forward<Args>(args)...)
88 {
89 }
90
91 label(tt::icon icon) noexcept : label(std::move(icon), l10n{}) {}
92
93 label() noexcept : label(tt::icon{}, std::u8string_view{}) {}
94
95 label(label const &other) noexcept :
96 _icon(other._icon), _msgid(other._msgid), _args(other._args->make_unique_copy())
97 {
98 }
99
100 label &operator=(label const &other) noexcept
101 {
102 _icon = other._icon;
103 _msgid = other._msgid;
104 _args = other._args->make_unique_copy();
105 return *this;
106 }
107
108 label(label &&other) noexcept = default;
109 label &operator=(label &&other) noexcept = default;
110
111 [[nodiscard]] bool has_icon() const noexcept
112 {
113 return static_cast<bool>(_icon);
114 }
115
116 [[nodiscard]] icon icon() const noexcept
117 {
118 return _icon;
119 }
120
121 [[nodiscard]] bool has_text() const noexcept
122 {
123 return _msgid.size() != 0;
124 }
125
126 [[nodiscard]] std::u8string text() const noexcept
127 {
128 auto fmt = get_translation(_msgid);
129 tt_axiom(_args);
130 return _args->format(fmt);
131 }
132
133 [[nodiscard]] friend bool operator==(label const &lhs, label const &rhs) noexcept
134 {
135 return lhs._icon == rhs._icon && lhs._msgid == rhs._msgid && lhs._args->eq(*rhs._args);
136 }
137
138 [[nodiscard]] friend std::u8string to_u8string(label const &rhs) noexcept
139 {
140 return rhs.text();
141 }
142
143 [[nodiscard]] friend std::string to_string(label const &rhs) noexcept
144 {
145 auto tmp = to_u8string(rhs);
146 return std::string{reinterpret_cast<char const *>(tmp.data()), tmp.size()};
147 }
148
149 friend std::ostream &operator<<(std::ostream &lhs, label const &rhs)
150 {
151 return lhs << to_string(rhs);
152 }
153
154private:
155 tt::icon _icon;
156 std::u8string _msgid;
158};
159
160} // namespace tt
An image, in different formats.
Definition icon.hpp:19
A localizable string.
Definition l10n.hpp:12
Definition label.hpp:20
Definition label.hpp:35
A localized text + icon label.
Definition label.hpp:76
T move(T... args)
T size(T... args)