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
46class async_widget : public widget {
47public:
48 using super = widget;
49 using delegate_type = async_delegate;
50
52 observer<alignment> alignment = alignment::top_left();
53 observer<hi::label> label = hi::txt{"<label>"};
54 keyboard_focus_group focus_group = keyboard_focus_group::normal;
55
56 attributes_type(attributes_type const &) noexcept = default;
57 attributes_type(attributes_type &&) noexcept = default;
58 attributes_type &operator=(attributes_type const &) noexcept = default;
59 attributes_type &operator=(attributes_type &&) noexcept = default;
60
61 template<async_widget_attribute... Attributes>
62 explicit attributes_type(Attributes &&...attributes) noexcept
63 {
64 set_attributes(std::forward<Attributes>(attributes)...);
65 }
66
67 void set_attributes() noexcept
68 {
69 }
70
71 template<async_widget_attribute First, async_widget_attribute... Rest>
72 void set_attributes(First&& first, Rest&&...rest) noexcept
73 {
75 alignment = std::forward<First>(first);
76
77 } else if constexpr (forward_of<First, observer<hi::label>>) {
78 label = std::forward<First>(first);
79
80 } else if constexpr (forward_of<First, keyboard_focus_group>) {
81 focus_group = std::forward<First>(first);
82
83 } else {
84 hi_static_no_default();
85 }
86
87 set_attributes(std::forward<Rest>(rest)...);
88 }
89 };
90
91 attributes_type attributes;
92
96
97 template<typename... Args>
98 [[nodiscard]] static std::shared_ptr<delegate_type> make_default_delegate(Args &&...args)
99 requires requires { default_async_delegate{std::forward<Args>(args)...}; }
100 {
101 return make_shared_ctad<default_async_delegate>(std::forward<Args>(args)...);
102 }
103
105 {
106 this->delegate->deinit(*this);
107 }
108
115 widget_intf const* parent,
116 attributes_type attributes,
118 super(parent), attributes(std::move(attributes)), delegate(std::move(delegate))
119 {
120 this->delegate->init(*this);
121 _delegate_cbt = this->delegate->subscribe([&] {
122 set_value(this->delegate->state(*this));
123 });
124 _delegate_cbt();
125 }
126
133 template<incompatible_with<attributes_type> Value, async_widget_attribute... Attributes>
135 widget_intf const* parent,
136 Value&& value,
137 Attributes &&...attributes) requires requires
138 {
139 make_default_delegate(std::forward<Value>(value));
140 attributes_type{std::forward<Attributes>(attributes)...};
141 } : async_widget(
142 parent,
143 attributes_type{std::forward<Attributes>(attributes)...},
144 make_default_delegate(std::forward<Value>(value)))
145 {
146 }
147
149 [[nodiscard]] box_constraints update_constraints() noexcept override
150 {
151 _button_size = {theme().size(), theme().size()};
152 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
153 }
154
155 void set_layout(widget_layout const& context) noexcept override
156 {
157 if (compare_store(_layout, context)) {
158 _button_rectangle = align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
159
160 _check_glyph = find_glyph(elusive_icon::Ok);
161 auto const check_glyph_bb = _check_glyph.get_metrics().bounding_rectangle * theme().icon_size();
162 _check_glyph_rectangle = align(_button_rectangle, check_glyph_bb, alignment::middle_center());
163
164 _minus_glyph = find_glyph(elusive_icon::Minus);
165 auto const minus_glyph_bb = _minus_glyph.get_metrics().bounding_rectangle * theme().icon_size();
166 _minus_glyph_rectangle = align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
167 }
168 super::set_layout(context);
169 }
170
171 void draw(draw_context const& context) noexcept override
172 {
173 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
174 context.draw_box(
175 layout(), _button_rectangle, background_color(), focus_color(), theme().border_width(), border_side::inside);
176
177 switch (value()) {
178 case widget_value::on:
179 context.draw_glyph(layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
180 break;
181 case widget_value::off:
182 break;
183 default:
184 context.draw_glyph(layout(), translate_z(0.1f) * _minus_glyph_rectangle, _minus_glyph, accent_color());
185 }
186 }
187 }
188
189 [[nodiscard]] color background_color() const noexcept override
190 {
191 hi_axiom(loop::main().on_thread());
192 if (phase() == widget_phase::pressed) {
193 return theme().color(semantic_color::fill, _layout.layer + 2);
194 } else {
195 return super::background_color();
196 }
197 }
198
199 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
200 {
201 hi_axiom(loop::main().on_thread());
202
203 if (mode() >= widget_mode::partial and layout().contains(position)) {
204 return {id, _layout.elevation, hitbox_type::button};
205 } else {
206 return {};
207 }
208 }
209
210 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
211 {
212 hi_axiom(loop::main().on_thread());
213 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::normal);
214 }
215
216 bool handle_event(gui_event const& event) noexcept override
217 {
218 hi_axiom(loop::main().on_thread());
219
220 switch (event.type()) {
221 case gui_event_type::gui_activate:
222 if (mode() >= widget_mode::partial) {
223 delegate->activate(*this);
225 return true;
226 }
227 break;
228
229 case gui_event_type::mouse_down:
230 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
231 set_pressed(true);
232 return true;
233 }
234 break;
235
236 case gui_event_type::mouse_up:
237 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
238 set_pressed(false);
239
240 // with_label_widget or other widgets may have accepted the hitbox
241 // for this widget. Which means the widget_id in the mouse-event
242 // may match up with the async.
243 if (event.mouse().hitbox.widget_id == id) {
244 handle_event(gui_event_type::gui_activate);
245 }
246 return true;
247 }
248 break;
249
250 default:;
251 }
252
253 return super::handle_event(event);
254 }
256
257private:
258 extent2 _button_size;
259 aarectangle _button_rectangle;
260 font_book::font_glyph_type _check_glyph;
261 aarectangle _check_glyph_rectangle;
262 font_book::font_glyph_type _minus_glyph;
263 aarectangle _minus_glyph_rectangle;
264
265 callback<void()> _delegate_cbt;
266};
267
268using async_with_label_widget = with_label_widget<async_widget>;
269using async_menu_button_widget = menu_button_widget<async_widget>;
270
271}} // 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.
The HikoGUI namespace.
Definition array_generic.hpp:20
@ 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
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
hi_export auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:440
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
Definition widget_intf.hpp:24
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
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:46
async_widget(widget_intf const *parent, attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a async widget.
Definition async_widget.hpp:114
async_widget(widget_intf const *parent, Value &&value, Attributes &&...attributes)
Construct a async widget with a default button delegate.
Definition async_widget.hpp:134
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition async_widget.hpp:95
Definition async_widget.hpp:51
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:121
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:141
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:55
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:150
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)