15#include "../telemetry/telemetry.hpp"
16#include "../macros.hpp"
18hi_export_module(hikogui.widgets.async_widget);
20hi_export
namespace hi {
inline namespace v1 {
22template<
typename Context>
51 struct attributes_type {
54 keyboard_focus_group focus_group = keyboard_focus_group::normal;
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;
62 explicit attributes_type(Attributes &&...attributes)
noexcept
67 void set_attributes()
noexcept
72 void set_attributes(First&& first, Rest&&...rest)
noexcept
84 hi_static_no_default();
97 template<
typename... Args>
101 return make_shared_ctad_not_null<default_async_delegate>(
std::forward<Args>(args)...);
106 this->delegate->deinit(*
this);
120 this->delegate->init(*
this);
121 _delegate_cbt = this->delegate->subscribe([&] {
122 set_value(this->delegate->state(*
this));
133 template<incompatible_with<attributes_type> Value, async_w
idget_attribute... Attributes>
137 Attributes &&...attributes)
requires requires
151 _button_size = {theme().size(), theme().size()};
152 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
155 void set_layout(widget_layout
const& context)
noexcept override
158 _button_rectangle =
align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
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());
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());
171 void draw(draw_context
const& context)
noexcept override
178 case widget_value::on:
179 context.draw_glyph(
layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
181 case widget_value::off:
184 context.draw_glyph(
layout(), translate_z(0.1f) * _minus_glyph_rectangle, _minus_glyph, accent_color());
189 [[nodiscard]] color background_color() const noexcept
override
191 hi_axiom(loop::main().on_thread());
192 if (phase() == widget_phase::pressed) {
193 return theme().color(semantic_color::fill, _layout.layer + 2);
195 return super::background_color();
199 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept override
201 hi_axiom(loop::main().on_thread());
204 return {
id, _layout.elevation, hitbox_type::button};
210 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
212 hi_axiom(loop::main().on_thread());
216 bool handle_event(gui_event
const& event)
noexcept override
218 hi_axiom(loop::main().on_thread());
220 switch (event.type()) {
221 case gui_event_type::gui_activate:
229 case gui_event_type::mouse_down:
236 case gui_event_type::mouse_up:
243 if (event.mouse().hitbox.widget_id ==
id) {
244 handle_event(gui_event_type::gui_activate);
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;
265 callback<void()> _delegate_cbt;
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:72
@ invisible
The widget is invisible.
Definition widget_state.hpp:40
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
The HikoGUI API version 1.
Definition recursive_iterator.hpp:16
@ 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
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:46
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:114
not_null< std::shared_ptr< delegate_type > > delegate
The delegate that controls the button widget.
Definition async_widget.hpp:95
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:134
Definition async_widget.hpp:51
Add menu-button around a small-button.
Definition menu_button_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
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:109
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
Add labels to a button.
Definition with_label_widget.hpp:41
True if T is a forwarded type of Forward.
Definition concepts.hpp:136
Definition async_widget.hpp:23
Definition label_widget.hpp:30