HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
async_widget.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 "widget.hpp"
12#include "with_label_widget.hpp"
14#include "async_delegate.hpp"
15#include "../telemetry/telemetry.hpp"
16#include "../macros.hpp"
17
18hi_export_module(hikogui.widgets.async_widget);
19
20hi_export namespace hi { inline namespace v1 {
21
22template<typename Context>
25
46template<fixed_string Tag = {}>
47class async_widget : public widget {
48public:
49 using super = widget;
50 using delegate_type = async_delegate;
51 constexpr static auto tag = theme_tag<"async", Tag>();
52
54 observer<alignment> alignment = alignment::top_left();
55 observer<hi::label> label = hi::txt{"<label>"};
56 keyboard_focus_group focus_group = keyboard_focus_group::normal;
57
58 attributes_type(attributes_type const &) noexcept = default;
59 attributes_type(attributes_type &&) noexcept = default;
60 attributes_type &operator=(attributes_type const &) noexcept = default;
61 attributes_type &operator=(attributes_type &&) noexcept = default;
62
64 explicit attributes_type(Attributes &&...attributes) noexcept
65 {
66 set_attributes(std::forward<Attributes>(attributes)...);
67 }
68
69 void set_attributes() noexcept
70 {
71 }
72
74 void set_attributes(First&& first, Rest&&...rest) noexcept
75 {
77 alignment = std::forward<First>(first);
78
79 } else if constexpr (forward_of<First, observer<hi::label>>) {
80 label = std::forward<First>(first);
81
82 } else if constexpr (forward_of<First, keyboard_focus_group>) {
83 focus_group = std::forward<First>(first);
84
85 } else {
86 hi_static_no_default();
87 }
88
89 set_attributes(std::forward<Rest>(rest)...);
90 }
91 };
92
93 attributes_type attributes;
94
98
99 template<typename... Args>
100 [[nodiscard]] static not_null<std::shared_ptr<delegate_type>> make_default_delegate(Args &&...args)
101 requires requires { default_async_delegate{std::forward<Args>(args)...}; }
102 {
103 return make_shared_ctad_not_null<default_async_delegate>(std::forward<Args>(args)...);
104 }
105
107 {
108 this->delegate->deinit(*this);
109 }
110
118 attributes_type attributes,
120 super(parent), attributes(std::move(attributes)), delegate(std::move(delegate))
121 {
122 this->delegate->init(*this);
123 _delegate_cbt = this->delegate->subscribe([&] {
124 set_value(this->delegate->state(*this));
125 });
126 _delegate_cbt();
127 }
128
135 template<incompatible_with<attributes_type> Value, async_widget_attribute... Attributes>
138 Value&& value,
139 Attributes &&...attributes) requires requires
140 {
141 make_default_delegate(std::forward<Value>(value));
142 attributes_type{std::forward<Attributes>(attributes)...};
143 } : async_widget(
144 parent,
145 attributes_type{std::forward<Attributes>(attributes)...},
146 make_default_delegate(std::forward<Value>(value)))
147 {
148 }
149
151 [[nodiscard]] box_constraints update_constraints() noexcept override
152 {
153 _button_size = {theme().size(), theme().size()};
154 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
155 }
156
157 void set_layout(widget_layout const& context) noexcept override
158 {
159 if (compare_store(_layout, context)) {
160 _button_rectangle = align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
161
162 _check_glyph = find_glyph(elusive_icon::Ok);
163 auto const check_glyph_bb = _check_glyph.get_metrics().bounding_rectangle * theme().icon_size();
164 _check_glyph_rectangle = align(_button_rectangle, check_glyph_bb, alignment::middle_center());
165
166 _minus_glyph = find_glyph(elusive_icon::Minus);
167 auto const minus_glyph_bb = _minus_glyph.get_metrics().bounding_rectangle * theme().icon_size();
168 _minus_glyph_rectangle = align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
169 }
171 }
172
173 void draw(draw_context const& context) noexcept override
174 {
175 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
176 context.draw_box(
177 layout(), _button_rectangle, background_color(), focus_color(), theme().border_width(), border_side::inside);
178
179 switch (value()) {
180 case widget_value::on:
181 context.draw_glyph(layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
182 break;
183 case widget_value::off:
184 break;
185 default:
186 context.draw_glyph(layout(), translate_z(0.1f) * _minus_glyph_rectangle, _minus_glyph, accent_color());
187 }
188 }
189 }
190
191 [[nodiscard]] color background_color() const noexcept override
192 {
193 hi_axiom(loop::main().on_thread());
194 if (phase() == widget_phase::pressed) {
195 return theme().color(semantic_color::fill, _layout.layer + 2);
196 } else {
197 return super::background_color();
198 }
199 }
200
201 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
202 {
203 hi_axiom(loop::main().on_thread());
204
205 if (mode() >= widget_mode::partial and layout().contains(position)) {
206 return {id, _layout.elevation, hitbox_type::button};
207 } else {
208 return {};
209 }
210 }
211
212 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
213 {
214 hi_axiom(loop::main().on_thread());
215 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::normal);
216 }
217
218 bool handle_event(gui_event const& event) noexcept override
219 {
220 hi_axiom(loop::main().on_thread());
221
222 switch (event.type()) {
223 case gui_event_type::gui_activate:
224 if (mode() >= widget_mode::partial) {
225 delegate->activate(*this);
227 return true;
228 }
229 break;
230
231 case gui_event_type::mouse_down:
232 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
233 set_pressed(true);
234 return true;
235 }
236 break;
237
238 case gui_event_type::mouse_up:
239 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
240 set_pressed(false);
241
242 // with_label_widget or other widgets may have accepted the hitbox
243 // for this widget. Which means the widget_id in the mouse-event
244 // may match up with the async.
245 if (event.mouse().hitbox.widget_id == id) {
246 handle_event(gui_event_type::gui_activate);
247 }
248 return true;
249 }
250 break;
251
252 default:;
253 }
254
256 }
258
259private:
260 extent2 _button_size;
261 aarectangle _button_rectangle;
262 font_book::font_glyph_type _check_glyph;
263 aarectangle _check_glyph_rectangle;
264 font_book::font_glyph_type _minus_glyph;
265 aarectangle _minus_glyph_rectangle;
266
267 callback<void()> _delegate_cbt;
268};
269
270using async_with_label_widget = with_label_widget<async_widget>;
271using async_menu_button_widget = menu_button_widget<async_widget>;
272
273}} // namespace hi::v1
Defines async_delegate and some default async delegates.
Defines widget.
Defines with_label_widget.
Defines menu_button_widget.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_export hi_inline auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:440
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
@ inside
The border is drawn inside the edge of a quad.
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:30
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:206
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:35
A localizable message.
Definition txt.hpp:100
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
Definition not_null.hpp:22
A button delegate controls the state of a button widget.
Definition async_delegate.hpp:28
A default async button delegate.
Definition async_delegate.hpp:75
A GUI widget that permits the user to make a binary choice.
Definition async_widget.hpp:47
async_widget(not_null< widget_intf const * > parent, attributes_type attributes, not_null< std::shared_ptr< delegate_type > > delegate) noexcept
Construct a async widget.
Definition async_widget.hpp:116
not_null< std::shared_ptr< delegate_type > > delegate
The delegate that controls the button widget.
Definition async_widget.hpp:97
async_widget(not_null< widget_intf const * > parent, Value &&value, Attributes &&...attributes)
Construct a async widget with a default button delegate.
Definition async_widget.hpp:136
Definition async_widget.hpp:53
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:115
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:135
widget(widget_intf const *parent) noexcept
Definition widget.hpp:49
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:144
True if T is a forwarded type of Forward.
Definition concepts.hpp:137
Definition async_widget.hpp:23
Definition label_widget.hpp:30
T align(T... args)
T move(T... args)