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 "../observer/observer.hpp"
12#include "../utility/utility.hpp"
13#include "../concurrency/concurrency.hpp"
14#include "../dispatch/dispatch.hpp"
15#include "../GUI/GUI.hpp"
16#include "../macros.hpp"
17#include "../l10n/l10n.hpp"
18#include "../macros.hpp"
19#include <string>
20#include <string_view>
21#include <optional>
22#include <concepts>
23
24hi_export_module(hikogui.widgets.text_field_delegate);
25
26hi_export namespace hi { inline namespace v1 {
27
33public:
34 using notifier_type = notifier<void()>;
35
36 virtual ~text_field_delegate() = default;
37 virtual void init(widget_intf const& sender) noexcept {}
38 virtual void deinit(widget_intf const& sender) noexcept {}
39
46 virtual label validate(widget_intf const& sender, gstring const& text) noexcept
47 {
48 return {};
49 }
50
58 virtual gstring text(widget_intf const& sender) noexcept
59 {
60 return {};
61 }
62
73 virtual void set_text(widget_intf const& sender, gstring const& text) noexcept {}
74
75 template<forward_of<void()> Func>
76 callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
77 {
78 return _notifier.subscribe(std::forward<Func>(func), flags);
79 }
80
81protected:
82 notifier_type _notifier;
83};
84
90template<typename T>
92
100template<std::integral T>
102public:
103 using value_type = T;
104
106
107 template<forward_of<observer<value_type>> Value>
108 default_text_field_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
109 {
110 _value_cbt = this->value.subscribe([&](auto...) {
111 this->_notifier();
112 });
113 }
114
115 label validate(widget_intf const& sender, gstring const& text) noexcept override
116 {
117 try {
118 [[maybe_unused]] auto dummy = from_string<value_type>(to_string(text), 10);
119 } catch (parse_error const&) {
120 return {txt("Invalid integer")};
121 }
122
123 return {};
124 }
125
126 gstring text(widget_intf const& sender) noexcept override
127 {
128 return to_gstring(to_string(*value));
129 }
130
131 void set_text(widget_intf const& sender, gstring const& text) noexcept override
132 {
133 try {
134 value = from_string<value_type>(to_string(text), 10);
135 } catch (std::exception const&) {
136 // Ignore the error, don't modify the value.
137 return;
138 }
139 }
140
141private:
142 callback<void(value_type)> _value_cbt;
143};
144
152template<std::floating_point T>
154public:
155 using value_type = T;
156
158
159 template<forward_of<observer<value_type>> Value>
160 default_text_field_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
161 {
162 _value_cbt = this->value.subscribe([&](auto...) {
163 this->_notifier();
164 });
165 }
166
167 label validate(widget_intf const& sender, gstring const& text) noexcept override
168 {
169 try {
170 [[maybe_unused]] auto dummy = from_string<value_type>(to_string(text));
171 } catch (parse_error const&) {
172 return {elusive_icon::WarningSign, txt("Invalid floating point number")};
173 }
174
175 return {};
176 }
177
178 gstring text(widget_intf const& sender) noexcept override
179 {
180 return to_gstring(to_string(*value));
181 }
182
183 void set_text(widget_intf const& sender, gstring const& text) noexcept override
184 {
185 try {
186 value = from_string<value_type>(to_string(text));
187 } catch (std::exception const&) {
188 // Ignore the error, don't modify the value.
189 return;
190 }
191 }
192
193private:
194 callback<void(value_type)> _value_cbt;
195};
196
204template<typename Value>
206 requires requires { default_text_field_delegate<observer_decay_t<decltype(value)>>{std::forward<Value>(value)}; }
207{
208 using value_type = observer_decay_t<Value>;
209 return std::make_shared<default_text_field_delegate<value_type>>(std::forward<Value>(value));
210}
211
212}} // namespace hi::v1
std::shared_ptr< text_field_delegate > make_default_text_field_delegate(Value &&value) noexcept
Create a shared pointer to a default text delegate.
Definition text_field_delegate.hpp:205
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:77
Definition widget_intf.hpp:24
A localizable message.
Definition txt.hpp:100
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
callback< void(value_type)> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback to this observer.
Definition observer_intf.hpp:456
Exception thrown during parsing on an error.
Definition exception_intf.hpp:48
A delegate that controls the state of a text_field_widget.
Definition text_field_delegate.hpp:32
virtual label validate(widget_intf const &sender, gstring const &text) noexcept
Validate the text field.
Definition text_field_delegate.hpp:46
virtual gstring text(widget_intf const &sender) noexcept
Get the text to show in the text field.
Definition text_field_delegate.hpp:58
virtual void set_text(widget_intf const &sender, gstring const &text) noexcept
Set the text as entered by the user.
Definition text_field_delegate.hpp:73
A default text delegate.
Definition text_field_delegate.hpp:91
void set_text(widget_intf const &sender, gstring const &text) noexcept override
Set the text as entered by the user.
Definition text_field_delegate.hpp:131
label validate(widget_intf const &sender, gstring const &text) noexcept override
Validate the text field.
Definition text_field_delegate.hpp:115
gstring text(widget_intf const &sender) noexcept override
Get the text to show in the text field.
Definition text_field_delegate.hpp:126