HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
checkbox_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 "toggle_delegate.hpp"
15#include "../telemetry/telemetry.hpp"
16#include "../macros.hpp"
17
18hi_export_module(hikogui.widgets.checkbox_widget);
19
20hi_export namespace hi {
21inline namespace v1 {
22
23template<typename Context>
25 constexpr static bool value = forward_of<Context, observer<hi::alignment>> or forward_of<Context, keyboard_focus_group>;
26};
27
28template<typename Context>
29concept checkbox_widget_attribute = is_checkbox_widget_attribute<Context>::value;
30
57class checkbox_widget : public widget {
58public:
59 using super = widget;
60 using delegate_type = toggle_delegate;
61
62 struct attributes_type {
63 observer<alignment> alignment = alignment::top_left();
64 keyboard_focus_group focus_group = keyboard_focus_group::normal;
65
66 attributes_type(attributes_type const&) noexcept = default;
67 attributes_type(attributes_type&&) noexcept = default;
68 attributes_type& operator=(attributes_type const&) noexcept = default;
69 attributes_type& operator=(attributes_type&&) noexcept = default;
70
71 template<checkbox_widget_attribute... Attributes>
72 explicit attributes_type(Attributes&&...attributes) noexcept
73 {
74 set_attributes(std::forward<Attributes>(attributes)...);
75 }
76
77 void set_attributes() noexcept {}
78
80 void set_attributes(First&& first, Rest&&...rest) noexcept
81 {
83 alignment = std::forward<First>(first);
84
85 } else if constexpr (forward_of<First, keyboard_focus_group>) {
86 focus_group = std::forward<First>(first);
87
88 } else {
89 hi_static_no_default();
90 }
91
92 set_attributes(std::forward<Rest>(rest)...);
93 }
94 };
95
96 attributes_type attributes;
97
101
102 hi_num_valid_arguments(consteval static, num_default_delegate_arguments, default_toggle_delegate);
103 hi_call_left_arguments(static, make_default_delegate, make_shared_ctad<default_toggle_delegate>);
104 hi_call_right_arguments(static, make_attributes, attributes_type);
105
107 {
108 this->delegate->deinit(*this);
109 }
110
117 attributes_type attributes,
119 super(), attributes(std::move(attributes)), delegate(std::move(delegate))
120 {
121 hi_axiom_not_null(this->delegate);
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<typename... Args>
136 checkbox_widget(Args&&...args)
137 requires(num_default_delegate_arguments<Args...>() != 0)
138 :
140 parent,
141 make_attributes<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...),
142 make_default_delegate<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...))
143 {
144 }
145
147 [[nodiscard]] box_constraints update_constraints() noexcept override
148 {
149 _button_size = {theme().size(), theme().size()};
150 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
151 }
152
153 void set_layout(widget_layout const& context) noexcept override
154 {
155 if (compare_store(_layout, context)) {
156 _button_rectangle = align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
157
158 _check_glyph = find_glyph(elusive_icon::Ok);
159 auto const check_glyph_bb = _check_glyph.front_glyph_metrics().bounding_rectangle * theme().icon_size();
160 _check_glyph_rectangle = align(_button_rectangle, check_glyph_bb, alignment::middle_center());
161
162 _minus_glyph = find_glyph(elusive_icon::Minus);
163 auto const minus_glyph_bb = _minus_glyph.front_glyph_metrics().bounding_rectangle * theme().icon_size();
164 _minus_glyph_rectangle = align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
165 }
166 super::set_layout(context);
167 }
168
169 void draw(draw_context const& context) noexcept override
170 {
171 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
172 context.draw_box(
173 layout(), _button_rectangle, background_color(), focus_color(), theme().border_width(), border_side::inside);
174
175 switch (value()) {
176 case widget_value::on:
177 context.draw_glyph(layout(), translate_z(0.1f) * _check_glyph_rectangle, _check_glyph, accent_color());
178 break;
179 case widget_value::off:
180 break;
181 default:
182 context.draw_glyph(layout(), translate_z(0.1f) * _minus_glyph_rectangle, _minus_glyph, accent_color());
183 }
184 }
185 }
186
187 [[nodiscard]] color background_color() const noexcept override
188 {
189 hi_axiom(loop::main().on_thread());
190 if (phase() == widget_phase::pressed) {
191 return theme().fill_color(_layout.layer + 2);
192 } else {
193 return super::background_color();
194 }
195 }
196
197 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
198 {
199 hi_axiom(loop::main().on_thread());
200
201 if (mode() >= widget_mode::partial and layout().contains(position)) {
202 return {id, _layout.elevation, hitbox_type::button};
203 } else {
204 return {};
205 }
206 }
207
208 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
209 {
210 hi_axiom(loop::main().on_thread());
211 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::normal);
212 }
213
214 bool handle_event(gui_event const& event) noexcept override
215 {
216 hi_axiom(loop::main().on_thread());
217
218 switch (event.type()) {
219 case gui_event_type::gui_activate:
220 if (mode() >= widget_mode::partial) {
221 delegate->activate(*this);
223 return true;
224 }
225 break;
226
227 case gui_event_type::mouse_down:
228 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
229 set_pressed(true);
230 return true;
231 }
232 break;
233
234 case gui_event_type::mouse_up:
235 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
236 set_pressed(false);
237
238 // with_label_widget or other widgets may have accepted the hitbox
239 // for this widget. Which means the widget_id in the mouse-event
240 // may match up with the checkbox.
241 if (event.mouse().hitbox.widget_id == id) {
242 handle_event(gui_event_type::gui_activate);
243 }
244 return true;
245 }
246 break;
247
248 default:;
249 }
250
251 return super::handle_event(event);
252 }
254
255private:
256 extent2 _button_size;
257 aarectangle _button_rectangle;
258 font_glyph_ids _check_glyph;
259 aarectangle _check_glyph_rectangle;
260 font_glyph_ids _minus_glyph;
261 aarectangle _minus_glyph_rectangle;
262
263 callback<void()> _delegate_cbt;
264};
265
266using checkbox_with_label_widget = with_label_widget<checkbox_widget>;
267using checkbox_menu_button_widget = menu_button_widget<checkbox_widget>;
268
269} // namespace v1
270} // namespace hi::v1
Defines widget.
Defines menu_button_widget.
Defines with_label_widget.
@ partial
A widget is partially enabled.
Definition widget_state.hpp:73
@ invisible
The widget is invisible.
Definition widget_state.hpp:41
STL namespace.
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
widget_intf * parent() const noexcept
Pointer to the parent widget.
Definition widget_intf.hpp:113
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(Args &&...args)
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:136
checkbox_widget(attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a checkbox widget.
Definition checkbox_widget.hpp:116
Definition checkbox_widget.hpp:62
Add menu-button around a small-button.
Definition menu_button_widget.hpp:38
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
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 checkbox_widget.hpp:29
T align(T... args)
T forward(T... args)
T move(T... args)