HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_delegate.hpp
Go to the documentation of this file.
1// Copyright Take Vos 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 "../unicode/unicode.hpp"
18#include "../l10n/l10n.hpp"
19#include "../macros.hpp"
20#include <string>
21#include <memory>
22#include <functional>
23
24hi_export_module(hikogui.widgets.text_delegate);
25
26hi_export namespace hi { inline namespace v1 {
27class text_widget;
28
34public:
35 virtual ~text_delegate() = default;
36
37 virtual void init(widget_intf const& sender) noexcept {}
38 virtual void deinit(widget_intf const& sender) noexcept {}
39
40 [[nodiscard]] virtual gstring get_text(widget_intf const& sender) noexcept = 0;
41 [[nodiscard]] virtual void set_text(widget_intf const& sender, gstring const& text) noexcept = 0;
42 [[nodiscard]] virtual bool can_set_text(widget_intf const& sender) noexcept = 0;
43
44
47 [[nodiscard]] virtual gstring read(widget_intf const& sender) noexcept = 0;
48
51 virtual void write(widget_intf const& sender, gstring const& text) noexcept = 0;
52
53protected:
54 notifier<void()> _notifier;
55};
56
62template<typename T>
64
69template<>
71public:
72 using value_type = char const *;
73
75
80 template<forward_of<observer<value_type>> Value>
81 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
82 {
83 _value_cbt = this->value.subscribe([&](auto...) {
84 this->_notifier();
85 });
86 }
87
88 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
89 {
90 return to_gstring(std::string{*value});
91 }
92
93 void write(widget_intf const& sender, gstring const& text) noexcept override
94 {
95 hi_no_default();
96 }
97
98private:
99 callback<void(value_type)> _value_cbt;
100};
101
106template<>
107class default_text_delegate<std::string> : public text_delegate {
108public:
109 using value_type = std::string;
110
112
117 template<forward_of<observer<value_type>> Value>
118 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
119 {
120 _value_cbt = this->value.subscribe([&](auto...) {
121 this->_notifier();
122 });
123 }
124
125 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
126 {
127 return to_gstring(*value);
128 }
129
130 void write(widget_intf const& sender, gstring const& text) noexcept override
131 {
132 value = to_string(text);
133 }
134
135private:
136 callback<void(value_type)> _value_cbt;
137};
138
143template<>
144class default_text_delegate<gstring> : public text_delegate {
145public:
146 using value_type = gstring;
147
149
154 template<forward_of<observer<value_type>> Value>
155 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
156 {
157 _value_cbt = this->value.subscribe([&](auto...) {
158 this->_notifier();
159 });
160 }
161
162 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
163 {
164 return *value;
165 }
166
167 void write(widget_intf const& sender, gstring const& text) noexcept override
168 {
169 value = text;
170 }
171
172private:
173 callback<void(value_type)> _value_cbt;
174};
175
180template<>
182public:
183 using value_type = txt;
184
186
191 template<forward_of<observer<value_type>> Value>
192 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
193 {
194 _value_cbt = this->value.subscribe([&](auto...) {
195 this->_notifier();
196 });
197 }
198
199 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
200 {
201 return value->translate();
202 }
203
204 void write(widget_intf const& sender, gstring const& text) noexcept override
205 {
206 hi_no_default();
207 }
208
209private:
210 callback<void(value_type)> _value_cbt;
211};
212
220template<typename Value>
222 requires requires { default_text_delegate<observer_decay_t<Value>>{std::forward<Value>(value)}; }
223{
224 return std::make_shared<default_text_delegate<observer_decay_t<Value>>>(std::forward<Value>(value));
225}
226
227}} // namespace hi::v1
std::shared_ptr< text_delegate > make_default_text_delegate(Value &&value) noexcept
Create a shared pointer to a default text delegate.
Definition text_delegate.hpp:221
STL namespace.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
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
A delegate that controls the state of a text_widget.
Definition text_delegate.hpp:33
virtual gstring read(widget_intf const &sender) noexcept=0
Read text as a string of graphemes.
virtual void write(widget_intf const &sender, gstring const &text) noexcept=0
Write text from a string of graphemes.
A default text delegate.
Definition text_delegate.hpp:63
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:88
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:81
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:93
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:125
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:118
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:130
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:155
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:162
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:167
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:192
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:204
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:199