14#include "toggle_delegate.hpp"
15#include "../telemetry/telemetry.hpp"
16#include "../macros.hpp"
18hi_export_module(hikogui.widgets.checkbox_widget);
20hi_export
namespace hi {
23template<
typename Context>
25 constexpr static bool value = forward_of<Context, observer<hi::alignment>> or forward_of<Context, keyboard_focus_group>;
28template<
typename Context>
64 keyboard_focus_group focus_group = keyboard_focus_group::normal;
74 set_attributes(std::forward<Attributes>(attributes)...);
77 void set_attributes()
noexcept {}
80 void set_attributes(First&& first, Rest&&...rest)
noexcept
86 focus_group = std::forward<First>(first);
89 hi_static_no_default();
92 set_attributes(std::forward<Rest>(rest)...);
103 hi_call_left_arguments(
static, make_default_delegate, make_shared_ctad<default_toggle_delegate>);
108 this->delegate->deinit(*
this);
122 hi_axiom_not_null(this->delegate);
123 this->delegate->init(*
this);
124 _delegate_cbt = this->delegate->subscribe([&] {
125 set_value(this->delegate->state(*
this));
136 template<
typename... Args>
138 requires(num_default_delegate_arguments<Args...>() != 0)
142 make_attributes<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...),
143 make_default_delegate<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...))
150 _button_size = {theme().size(), theme().size()};
151 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
154 void set_layout(widget_layout
const& context)
noexcept override
157 _button_rectangle =
align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
160 auto const check_glyph_bb = _check_glyph.get_metrics().bounding_rectangle * theme().icon_size();
161 _check_glyph_rectangle =
align(_button_rectangle, check_glyph_bb, alignment::middle_center());
163 _minus_glyph =
find_glyph(elusive_icon::Minus);
164 auto const minus_glyph_bb = _minus_glyph.get_metrics().bounding_rectangle * theme().icon_size();
165 _minus_glyph_rectangle =
align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
170 void draw(draw_context
const& context)
noexcept override
177 case widget_value::on:
178 context.draw_glyph(
layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
180 case widget_value::off:
183 context.draw_glyph(
layout(), translate_z(0.1f) * _minus_glyph_rectangle, _minus_glyph, accent_color());
188 [[nodiscard]] color background_color() const noexcept
override
190 hi_axiom(loop::main().on_thread());
191 if (phase() == widget_phase::pressed) {
192 return theme().color(semantic_color::fill, _layout.layer + 2);
194 return super::background_color();
198 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept override
200 hi_axiom(loop::main().on_thread());
203 return {
id, _layout.elevation, hitbox_type::button};
209 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
211 hi_axiom(loop::main().on_thread());
215 bool handle_event(gui_event
const& event)
noexcept override
217 hi_axiom(loop::main().on_thread());
219 switch (event.type()) {
220 case gui_event_type::gui_activate:
228 case gui_event_type::mouse_down:
235 case gui_event_type::mouse_up:
242 if (event.mouse().hitbox.widget_id ==
id) {
243 handle_event(gui_event_type::gui_activate);
257 extent2 _button_size;
258 aarectangle _button_rectangle;
259 font_book::font_glyph_type _check_glyph;
260 aarectangle _check_glyph_rectangle;
261 font_book::font_glyph_type _minus_glyph;
262 aarectangle _minus_glyph_rectangle;
264 callback<void()> _delegate_cbt;
267using checkbox_with_label_widget = with_label_widget<checkbox_widget>;
268using checkbox_menu_button_widget = menu_button_widget<checkbox_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
2D constraints.
Definition box_constraints.hpp:25
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
Definition checkbox_widget.hpp:24
A GUI widget that permits the user to make a binary choice.
Definition checkbox_widget.hpp:57
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition checkbox_widget.hpp:100
checkbox_widget(widget_intf const *parent, attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a checkbox widget.
Definition checkbox_widget.hpp:116
checkbox_widget(widget_intf const *parent, Args &&...args)
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:137
Definition checkbox_widget.hpp:62
A button delegate controls the state of a button widget.
Definition toggle_delegate.hpp:18
A default toggle button delegate.
Definition toggle_delegate.hpp:58
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 checkbox_widget.hpp:29