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
42 [[nodiscard]] virtual gstring read(widget_intf const& sender) noexcept = 0;
43
46 virtual void write(widget_intf const& sender, gstring const& text) noexcept = 0;
47
50 template<forward_of<void()> Func>
51 [[nodiscard]] callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
52 {
53 return _notifier.subscribe(std::forward<Func>(func), flags);
54 }
55
56protected:
57 notifier<void()> _notifier;
58};
59
65template<typename T>
67
72template<>
73class default_text_delegate<char const *> : public text_delegate {
74public:
75 using value_type = char const *;
76
78
83 template<forward_of<observer<value_type>> Value>
84 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
85 {
86 _value_cbt = this->value.subscribe([&](auto...) {
87 this->_notifier();
88 });
89 }
90
91 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
92 {
93 return to_gstring(std::string{*value});
94 }
95
96 void write(widget_intf const& sender, gstring const& text) noexcept override
97 {
98 hi_no_default();
99 }
100
101private:
102 callback<void(value_type)> _value_cbt;
103};
104
109template<>
110class default_text_delegate<std::string> : public text_delegate {
111public:
112 using value_type = std::string;
113
115
120 template<forward_of<observer<value_type>> Value>
121 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
122 {
123 _value_cbt = this->value.subscribe([&](auto...) {
124 this->_notifier();
125 });
126 }
127
128 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
129 {
130 return to_gstring(*value);
131 }
132
133 void write(widget_intf const& sender, gstring const& text) noexcept override
134 {
135 value = to_string(text);
136 }
137
138private:
139 callback<void(value_type)> _value_cbt;
140};
141
146template<>
147class default_text_delegate<gstring> : public text_delegate {
148public:
149 using value_type = gstring;
150
152
157 template<forward_of<observer<value_type>> Value>
158 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
159 {
160 _value_cbt = this->value.subscribe([&](auto...) {
161 this->_notifier();
162 });
163 }
164
165 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
166 {
167 return *value;
168 }
169
170 void write(widget_intf const& sender, gstring const& text) noexcept override
171 {
172 value = text;
173 }
174
175private:
176 callback<void(value_type)> _value_cbt;
177};
178
183template<>
185public:
186 using value_type = txt;
187
189
194 template<forward_of<observer<value_type>> Value>
195 explicit default_text_delegate(Value&& value) noexcept : value(std::forward<Value>(value))
196 {
197 _value_cbt = this->value.subscribe([&](auto...) {
198 this->_notifier();
199 });
200 }
201
202 [[nodiscard]] gstring read(widget_intf const& sender) noexcept override
203 {
204 return value->translate();
205 }
206
207 void write(widget_intf const& sender, gstring const& text) noexcept override
208 {
209 hi_no_default();
210 }
211
212private:
213 callback<void(value_type)> _value_cbt;
214};
215
223template<typename Value>
225 requires requires { default_text_delegate<observer_decay_t<Value>>{std::forward<Value>(value)}; }
226{
227 return std::make_shared<default_text_delegate<observer_decay_t<Value>>>(std::forward<Value>(value));
228}
229
230}} // 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:224
STL namespace.
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
A delegate that controls the state of a text_widget.
Definition text_delegate.hpp:33
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback for notifying the widget of a data change.
Definition text_delegate.hpp:51
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:66
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:91
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:84
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:96
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:128
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:121
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:133
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:158
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:165
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:170
default_text_delegate(Value &&value) noexcept
Construct a delegate.
Definition text_delegate.hpp:195
void write(widget_intf const &sender, gstring const &text) noexcept override
Write text from a string of graphemes.
Definition text_delegate.hpp:207
gstring read(widget_intf const &sender) noexcept override
Read text as a string of graphemes.
Definition text_delegate.hpp:202