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
8
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
51 struct attributes_type {
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 attributes_type attributes,
117 super(), attributes(std::move(attributes)), delegate(std::move(delegate))
118 {
119 this->delegate->init(*this);
120 _delegate_cbt = this->delegate->subscribe([&] {
121 set_value(this->delegate->state(*this));
122 });
123 _delegate_cbt();
124 }
125
132 template<incompatible_with<attributes_type> Value, async_widget_attribute... Attributes>
134 Value&& value,
135 Attributes &&...attributes) requires requires
136 {
137 make_default_delegate(std::forward<Value>(value));
139 } : async_widget(
140 attributes_type{std::forward<Attributes>(attributes)...},
141 make_default_delegate(std::forward<Value>(value)))
142 {
143 }
144
146 [[nodiscard]] box_constraints update_constraints() noexcept override
147 {
148 _button_size = {theme().size(), theme().size()};
149 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
150 }
151
152 void set_layout(widget_layout const& context) noexcept override
153 {
154 if (compare_store(_layout, context)) {
155 _button_rectangle = align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
156
157 _check_glyph = find_glyph(elusive_icon::Ok);
158 auto const check_glyph_bb = _check_glyph.front_glyph_metrics().bounding_rectangle * theme().icon_size();
159 _check_glyph_rectangle = align(_button_rectangle, check_glyph_bb, alignment::middle_center());
160
161 _minus_glyph = find_glyph(elusive_icon::Minus);
162 auto const minus_glyph_bb = _minus_glyph.front_glyph_metrics().bounding_rectangle * theme().icon_size();
163 _minus_glyph_rectangle = align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
164 }
165 super::set_layout(context);
166 }
167
168 void draw(draw_context const& context) noexcept override
169 {
170 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
171 context.draw_box(
172 layout(), _button_rectangle, background_color(), focus_color(), theme().border_width(), border_side::inside);
173
174 switch (value()) {
175 case widget_value::on:
176 context.draw_glyph(layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
177 break;
178 case widget_value::off:
179 break;
180 default:
181 context.draw_glyph(layout(), translate_z(0.1f) * _minus_glyph_rectangle, _minus_glyph, accent_color());
182 }
183 }
184 }
185
186 [[nodiscard]] color background_color() const noexcept override
187 {
188 hi_axiom(loop::main().on_thread());
189 if (phase() == widget_phase::pressed) {
190 return theme().fill_color(_layout.layer + 2);
191 } else {
192 return super::background_color();
193 }
194 }
195
196 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
197 {
198 hi_axiom(loop::main().on_thread());
199
200 if (mode() >= widget_mode::partial and layout().contains(position)) {
201 return {id, _layout.elevation, hitbox_type::button};
202 } else {
203 return {};
204 }
205 }
206
207 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
208 {
209 hi_axiom(loop::main().on_thread());
210 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::normal);
211 }
212
213 bool handle_event(gui_event const& event) noexcept override
214 {
215 hi_axiom(loop::main().on_thread());
216
217 switch (event.type()) {
218 case gui_event_type::gui_activate:
219 if (mode() >= widget_mode::partial) {
220 delegate->activate(*this);
222 return true;
223 }
224 break;
225
226 case gui_event_type::mouse_down:
227 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
228 set_pressed(true);
229 return true;
230 }
231 break;
232
233 case gui_event_type::mouse_up:
234 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
235 set_pressed(false);
236
237 // with_label_widget or other widgets may have accepted the hitbox
238 // for this widget. Which means the widget_id in the mouse-event
239 // may match up with the async.
240 if (event.mouse().hitbox.widget_id == id) {
241 handle_event(gui_event_type::gui_activate);
242 }
243 return true;
244 }
245 break;
246
247 default:;
248 }
249
250 return super::handle_event(event);
251 }
253
254private:
255 extent2 _button_size;
256 aarectangle _button_rectangle;
257 font_glyph_ids _check_glyph;
258 aarectangle _check_glyph_rectangle;
259 font_glyph_ids _minus_glyph;
260 aarectangle _minus_glyph_rectangle;
261
262 callback<void()> _delegate_cbt;
263};
264
265using async_with_label_widget = with_label_widget<async_widget>;
266using async_menu_button_widget = menu_button_widget<async_widget>;
267
268}} // namespace hi::v1
Defines widget.
Defines menu_button_widget.
Defines with_label_widget.
Defines async_delegate and some default async delegates.
@ partial
A widget is partially enabled.
Definition widget_state.hpp:73
@ invisible
The widget is invisible.
Definition widget_state.hpp:41
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
@ color
A color value was modified.
Definition style_modify_mask.hpp:27
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context_intf.hpp:35
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
font_glyph_ids find_glyph(font_id font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:362
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:31
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:241
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(attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a async widget.
Definition async_widget.hpp:114
async_widget(Value &&value, Attributes &&...attributes)
Construct a async widget with a default button delegate.
Definition async_widget.hpp:133
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition async_widget.hpp:95
Definition async_widget.hpp:51
Add menu-button around a small-button.
Definition menu_button_widget.hpp:38
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:116
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:136
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:50
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:110
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:145
Add labels to a button.
Definition with_label_widget.hpp:41
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 forward(T... args)
T move(T... args)