HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
toggle_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"
13#include "toggle_delegate.hpp"
14#include "../telemetry/telemetry.hpp"
15#include "../macros.hpp"
16
17hi_export_module(hikogui.widgets.toggle_widget);
18
19hi_export namespace hi {
20inline namespace v1 {
21
22template<typename Context>
24
62class toggle_widget : public widget {
63public:
64 using super = widget;
65 using delegate_type = toggle_delegate;
66
67 struct attributes_type {
68 observer<alignment> alignment = alignment::top_left();
69 keyboard_focus_group focus_group = keyboard_focus_group::normal;
70
71 attributes_type(attributes_type const&) noexcept = default;
72 attributes_type(attributes_type&&) noexcept = default;
73 attributes_type& operator=(attributes_type const&) noexcept = default;
74 attributes_type& operator=(attributes_type&&) noexcept = default;
75
76 template<toggle_widget_attribute... Attributes>
77 explicit attributes_type(Attributes&&...attributes) noexcept
78 {
79 set_attributes(std::forward<Attributes>(attributes)...);
80 }
81
82 void set_attributes() noexcept {}
83
85 void set_attributes(First&& first, Rest&&...rest) noexcept
86 {
88 alignment = std::forward<First>(first);
89
90 } else if constexpr (forward_of<First, keyboard_focus_group>) {
91 focus_group = std::forward<First>(first);
92
93 } else {
94 hi_static_no_default();
95 }
96
97 set_attributes(std::forward<Rest>(rest)...);
98 }
99 };
100
101 attributes_type attributes;
102
106
107 hi_num_valid_arguments(consteval static, num_default_delegate_arguments, default_toggle_delegate);
108 hi_call_left_arguments(static, make_default_delegate, make_shared_ctad<default_toggle_delegate>);
109 hi_call_right_arguments(static, make_attributes, attributes_type);
110
112 {
113 this->delegate->deinit(*this);
114 }
115
122 attributes_type attributes,
124 super(), attributes(std::move(attributes)), delegate(std::move(delegate))
125 {
126 hi_axiom_not_null(this->delegate);
127 this->delegate->init(*this);
128 _delegate_cbt = this->delegate->subscribe([&] {
129 set_value(this->delegate->state(*this));
130 });
131 _delegate_cbt();
132 }
133
140 template<typename... Args>
141 toggle_widget(Args&&...args)
142 requires(num_default_delegate_arguments<Args...>() != 0)
143 :
145 make_attributes<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...),
146 make_default_delegate<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...))
147 {
148 }
149
151 [[nodiscard]] box_constraints update_constraints() noexcept override
152 {
153 _button_size = {theme().size() * 2.0f, theme().size()};
154 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
155 }
156
157 void set_layout(widget_layout const& context) noexcept override
158 {
159 if (compare_store(_layout, context)) {
160 _button_rectangle = align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
161
162 auto const button_square =
163 aarectangle{get<0>(_button_rectangle), extent2{_button_rectangle.height(), _button_rectangle.height()}};
164
165 _pip_circle = align(button_square, circle{theme().size() * 0.5f - 3.0f}, alignment::middle_center());
166
167 auto const pip_to_button_margin_x2 = _button_rectangle.height() - _pip_circle.diameter();
168 _pip_move_range = _button_rectangle.width() - _pip_circle.diameter() - pip_to_button_margin_x2;
169 }
170 super::set_layout(context);
171 }
172
173 void draw(draw_context const& context) noexcept override
174 {
175 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
176 context.draw_box(
177 layout(),
178 _button_rectangle,
179 background_color(),
180 focus_color(),
181 theme().border_width(),
183 corner_radii{_button_rectangle.height() * 0.5f});
184
185 switch (_animated_value.update(value() == widget_value::on ? 1.0f : 0.0f, context.display_time_point)) {
186 case animator_state::idle:
187 break;
188 case animator_state::running:
190 break;
191 case animator_state::end:
192 notifier();
193 break;
194 default:
195 hi_no_default();
196 }
197
198 auto const positioned_pip_circle = translate3{_pip_move_range * _animated_value.current_value(), 0.0f, 0.1f} * _pip_circle;
199
200 auto const foreground_color_ = value() == widget_value::on ? accent_color() : foreground_color();
201 context.draw_circle(layout(), positioned_pip_circle * 1.02f, foreground_color_);
202 }
203 }
204
205 [[nodiscard]] color background_color() const noexcept override
206 {
207 hi_axiom(loop::main().on_thread());
208 if (phase() == widget_phase::pressed) {
209 return theme().fill_color(_layout.layer + 2);
210 } else {
211 return super::background_color();
212 }
213 }
214
215 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
216 {
217 hi_axiom(loop::main().on_thread());
218
219 if (mode() >= widget_mode::partial and layout().contains(position)) {
220 return {id, _layout.elevation, hitbox_type::button};
221 } else {
222 return {};
223 }
224 }
225
226 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
227 {
228 hi_axiom(loop::main().on_thread());
229 return mode() >= widget_mode::partial and to_bool(group & hi::keyboard_focus_group::normal);
230 }
231
232 bool handle_event(gui_event const& event) noexcept override
233 {
234 hi_axiom(loop::main().on_thread());
235
236 switch (event.type()) {
237 case gui_event_type::gui_activate:
238 if (mode() >= widget_mode::partial) {
239 delegate->activate(*this);
240 ++global_counter<"toggle_widget:handle_event:relayout">;
242 return true;
243 }
244 break;
245
246 case gui_event_type::mouse_down:
247 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
248 set_pressed(true);
249 return true;
250 }
251 break;
252
253 case gui_event_type::mouse_up:
254 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
255 set_pressed(false);
256
257 // with_label_widget or other widgets may have accepted the hitbox
258 // for this widget. Which means the widget_id in the mouse-event
259 // may match up with the toggle.
260 if (event.mouse().hitbox.widget_id == id) {
261 handle_event(gui_event_type::gui_activate);
262 }
264 return true;
265 }
266 break;
267
268 default:;
269 }
270
271 return super::handle_event(event);
272 }
274
275private:
276 constexpr static std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
277
278 extent2 _button_size;
279 aarectangle _button_rectangle;
280 animator<float> _animated_value = _animation_duration;
281 circle _pip_circle;
282 float _pip_move_range;
283
284 callback<void()> _delegate_cbt;
285
286 /*template<size_t I>
287 void set_attributes() noexcept
288 {
289 }
290
291 template<size_t I, button_widget_attribute First, button_widget_attribute... Rest>
292 void set_attributes(First&& first, Rest&&...rest) noexcept
293 {
294 if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
295 alignment = std::forward<First>(first);
296 set_attributes<I>(std::forward<Rest>(rest)...);
297
298 } else {
299 hi_static_no_default();
300 }
301 }*/
302};
303
304using toggle_with_label_widget = with_label_widget<toggle_widget>;
305
306} // namespace v1
307} // namespace hi::v1
Defines widget.
Defines with_label_widget.
@ window_relayout
Request that widgets get laid out on the next frame.
Definition gui_event_type.hpp:47
@ 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
notifier< void()> notifier
Notifier which is called after an action is completed by a widget.
Definition widget_intf.hpp:45
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
2D constraints.
Definition box_constraints.hpp:25
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 toggle_delegate.hpp:18
A default toggle button delegate.
Definition toggle_delegate.hpp:58
A GUI widget that permits the user to make a binary choice.
Definition toggle_widget.hpp:62
toggle_widget(attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a toggle widget.
Definition toggle_widget.hpp:121
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition toggle_widget.hpp:105
toggle_widget(Args &&...args)
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:141
Definition toggle_widget.hpp:67
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 process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:125
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 toggle_widget.hpp:23
T align(T... args)
T forward(T... args)
T move(T... args)