HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_field_delegate.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-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
9#pragma once
10
11#include "../label.hpp"
12#include "../observer.hpp"
13#include <string>
14#include <string_view>
15#include <optional>
16#include <concepts>
17
18namespace hi { inline namespace v1 {
19class text_field_widget;
20
26public:
27 using notifier_type = notifier<>;
28 using callback_token = notifier_type::callback_token;
29 using callback_proto = notifier_type::callback_proto;
30
31 virtual ~text_field_delegate() = default;
32 virtual void init(text_field_widget const& sender) noexcept {}
33 virtual void deinit(text_field_widget const& sender) noexcept {}
34
41 virtual label validate(text_field_widget& sender, std::string_view text) noexcept
42 {
43 return {};
44 }
45
53 virtual std::string text(text_field_widget& sender) noexcept
54 {
55 return {};
56 }
57
68 virtual void set_text(text_field_widget& sender, std::string_view text) noexcept {}
69
70 callback_token
71 subscribe(forward_of<callback_proto> auto&& callback, callback_flags flags = callback_flags::synchronous) noexcept
72 {
73 return _notifier.subscribe(hi_forward(callback), flags);
74 }
75
76protected:
77 notifier_type _notifier;
78};
79
85template<typename T>
87
95template<std::integral T>
97public:
98 using value_type = T;
99
100 observer<value_type> value;
101
102 default_text_field_delegate(forward_of<observer<value_type>> auto&& value) noexcept : value(hi_forward(value))
103 {
104 _value_cbt = this->value.subscribe([&](auto...) {
105 this->_notifier();
106 });
107 }
108
109 std::optional<label> validate(text_field_widget& sender, std::string_view text) noexcept override
110 {
111 try {
112 [[maybe_unused]] auto dummy = from_string<value_type>(text, 10);
113 } catch (parse_error const&) {
114 return {tr{"Invalid integer"}};
115 }
116
117 return {};
118 }
119
120 std::string text(text_field_widget& sender) noexcept override
121 {
122 return to_string(*value);
123 }
124
125 void set_text(text_field_widget& sender, std::string_view text) noexcept override
126 {
127 try {
128 value = from_string<value_type>(text, 10);
129 } catch (std::exception const&) {
130 // Ignore the error, don't modify the value.
131 return;
132 }
133 }
134
135private:
136 typename decltype(value)::callback_token _value_cbt;
137};
138
146template<std::floating_point T>
148public:
149 using value_type = T;
150
151 observer<value_type> value;
152
153 default_text_field_delegate(forward_of<observer<value_type>> auto&& value) noexcept : value(hi_forward(value))
154 {
155 _value_cbt = this->value.subscribe([&](auto...) {
156 this->_notifier();
157 });
158 }
159
160 label validate(text_field_widget& sender, std::string_view text) noexcept override
161 {
162 try {
163 [[maybe_unused]] auto dummy = from_string<value_type>(text);
164 } catch (parse_error const&) {
165 return {elusive_icon::WarningSign, tr{"Invalid floating point number"}};
166 }
167
168 return {};
169 }
170
171 std::string text(text_field_widget& sender) noexcept override
172 {
173 return to_string(*value);
174 }
175
176 void set_text(text_field_widget& sender, std::string_view text) noexcept override
177 {
178 try {
179 value = from_string<value_type>(text);
180 } catch (std::exception const&) {
181 // Ignore the error, don't modify the value.
182 return;
183 }
184 }
185
186private:
187 typename decltype(value)::callback_token _value_cbt;
188};
189
197[[nodiscard]] std::shared_ptr<text_field_delegate> make_default_text_field_delegate(auto&& value) noexcept requires requires
198{
199 default_text_field_delegate<observer_decay_t<decltype(value)>>{hi_forward(value)};
200}
201{
202 using value_type = observer_decay_t<decltype(value)>;
203 return std::make_shared<default_text_field_delegate<value_type>>(hi_forward(value));
204}
205
206}} // namespace hi::v1
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
Functionality for labels, text and icons.
constexpr std::string to_string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:215
std::shared_ptr< text_field_delegate > make_default_text_field_delegate(auto &&value) noexcept
Create a shared pointer to a default text delegate.
Definition text_field_delegate.hpp:197
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
Exception thrown during parsing on an error.
Definition exception.hpp:58
A delegate that controls the state of a text_field_widget.
Definition text_field_delegate.hpp:25
virtual label validate(text_field_widget &sender, std::string_view text) noexcept
Validate the text field.
Definition text_field_delegate.hpp:41
virtual std::string text(text_field_widget &sender) noexcept
Get the text to show in the text field.
Definition text_field_delegate.hpp:53
virtual void set_text(text_field_widget &sender, std::string_view text) noexcept
Set the text as entered by the user.
Definition text_field_delegate.hpp:68
A default text delegate.
Definition text_field_delegate.hpp:86
std::string text(text_field_widget &sender) noexcept override
Get the text to show in the text field.
Definition text_field_delegate.hpp:120
void set_text(text_field_widget &sender, std::string_view text) noexcept override
Set the text as entered by the user.
Definition text_field_delegate.hpp:125
label validate(text_field_widget &sender, std::string_view text) noexcept override
Validate the text field.
Definition text_field_delegate.hpp:160
std::optional< label > validate(text_field_widget &sender, std::string_view text) noexcept override
Validate the text field.
Definition text_field_delegate.hpp:109
A single line text field.
Definition text_field_widget.hpp:62