13#include "toggle_delegate.hpp"
14#include "../telemetry/telemetry.hpp"
15#include "../macros.hpp"
17hi_export_module(hikogui.widgets.toggle_widget);
19hi_export
namespace hi {
22template<
typename Context>
69 keyboard_focus_group focus_group = keyboard_focus_group::normal;
79 set_attributes(std::forward<Attributes>(attributes)...);
82 void set_attributes()
noexcept {}
85 void set_attributes(First&& first, Rest&&...rest)
noexcept
91 focus_group = std::forward<First>(first);
94 hi_static_no_default();
97 set_attributes(std::forward<Rest>(rest)...);
108 hi_call_left_arguments(
static, make_default_delegate, make_shared_ctad<default_toggle_delegate>);
113 this->delegate->deinit(*
this);
127 hi_axiom_not_null(this->delegate);
128 this->delegate->init(*
this);
129 _delegate_cbt = this->delegate->subscribe([&] {
130 set_value(this->delegate->state(*
this));
141 template<
typename... Args>
143 requires(num_default_delegate_arguments<Args...>() != 0)
147 make_attributes<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...),
148 make_default_delegate<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...))
155 _button_size = {theme().size() * 2.0f, theme().size()};
156 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
159 void set_layout(widget_layout
const& context)
noexcept override
162 _button_rectangle =
align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
164 auto const button_square =
165 aarectangle{get<0>(_button_rectangle), extent2{_button_rectangle.height(), _button_rectangle.height()}};
167 _pip_circle =
align(button_square, circle{theme().size() * 0.5f - 3.0f}, alignment::middle_center());
169 auto const pip_to_button_margin_x2 = _button_rectangle.height() - _pip_circle.diameter();
170 _pip_move_range = _button_rectangle.width() - _pip_circle.diameter() - pip_to_button_margin_x2;
175 void draw(draw_context
const& context)
noexcept override
183 theme().border_width(),
185 corner_radii{_button_rectangle.height() * 0.5f});
187 switch (_animated_value.update(value() == widget_value::on ? 1.0f : 0.0f, context.display_time_point)) {
188 case animator_state::idle:
190 case animator_state::running:
193 case animator_state::end:
200 auto const positioned_pip_circle = translate3{_pip_move_range * _animated_value.current_value(), 0.0f, 0.1f} * _pip_circle;
202 auto const foreground_color_ = value() == widget_value::on ? accent_color() : foreground_color();
203 context.draw_circle(
layout(), positioned_pip_circle * 1.02f, foreground_color_);
207 [[nodiscard]] color background_color() const noexcept
override
209 hi_axiom(loop::main().on_thread());
210 if (phase() == widget_phase::pressed) {
211 return theme().color(semantic_color::fill, _layout.layer + 2);
213 return super::background_color();
217 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept override
219 hi_axiom(loop::main().on_thread());
222 return {
id, _layout.elevation, hitbox_type::button};
228 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
230 hi_axiom(loop::main().on_thread());
234 bool handle_event(gui_event
const& event)
noexcept override
236 hi_axiom(loop::main().on_thread());
238 switch (event.type()) {
239 case gui_event_type::gui_activate:
242 ++global_counter<
"toggle_widget:handle_event:relayout">;
248 case gui_event_type::mouse_down:
255 case gui_event_type::mouse_up:
262 if (event.mouse().hitbox.widget_id ==
id) {
263 handle_event(gui_event_type::gui_activate);
280 extent2 _button_size;
281 aarectangle _button_rectangle;
282 animator<float> _animated_value = _animation_duration;
284 float _pip_move_range;
286 callback<void()> _delegate_cbt;
289 void set_attributes() noexcept
293 template<
size_t I, button_widget_attribute First, button_widget_attribute... Rest>
294 void set_attributes(First&& first, Rest&&...rest)
noexcept
296 if constexpr (forward_of<
decltype(first), observer<hi::alignment>>) {
297 alignment = std::forward<First>(first);
298 set_attributes<I>(std::forward<Rest>(rest)...);
301 hi_static_no_default();
306using toggle_with_label_widget = with_label_widget<toggle_widget>;
Defines with_label_widget.
@ window_relayout
Request that widgets get laid out on the next frame.
@ 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
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
Definition widget_intf.hpp:24
notifier< void()> notifier
Notifier which is called after an action is completed by a widget.
Definition widget_intf.hpp:39
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
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
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition toggle_widget.hpp:105
toggle_widget(widget_intf const *parent, Args &&...args)
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:142
toggle_widget(widget_intf const *parent, attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a toggle widget.
Definition toggle_widget.hpp:121
Definition toggle_widget.hpp:67
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 process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:130
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 toggle_widget.hpp:23