12#include "radio_delegate.hpp"
13#include "../macros.hpp"
15hi_export_module(hikogui.widgets.toolbar_tab_button_widget);
17hi_export
namespace hi {
20template<
typename Context>
59 struct attributes_type {
70 attributes_type(attributes_type
const&)
noexcept =
default;
71 attributes_type(attributes_type&&) noexcept = default;
72 attributes_type& operator=(attributes_type const&) noexcept = default;
73 attributes_type& operator=(attributes_type&&) noexcept = default;
76 explicit attributes_type(Attributes&&...attributes) noexcept
81 template<
size_t NumLabels>
82 void set_attributes() noexcept
87 void set_attributes(First&& first, Rest&&...rest)
noexcept
90 if constexpr (NumLabels == 0) {
94 }
else if constexpr (NumLabels == 1) {
99 hi_static_no_default(
"Maximum two label attributes (on/off) are allowed on a toolbar-tab-button");
103 }
else if constexpr (forward_of<First, observer<hi::alignment>>) {
108 hi_static_no_default();
120 hi_call_left_arguments(
static, make_default_delegate, make_shared_ctad<default_radio_delegate>);
142 _on_label_widget = std::make_unique<label_widget>(this->attributes.on_label, this->attributes.alignment);
143 _on_label_widget->set_parent(
this);
145 _off_label_widget = std::make_unique<label_widget>(this->attributes.off_label, this->attributes.alignment);
146 _off_label_widget->set_parent(
this);
148 hi_axiom_not_null(this->delegate);
149 this->delegate->init(*
this);
150 _delegate_cbt = this->delegate->subscribe([&] {
151 set_value(this->delegate->state(*
this));
162 template<
typename... Args>
164 requires(num_default_delegate_arguments<Args...>() != 0)
167 make_attributes<num_default_delegate_arguments<Args...>()>(
std::forward<Args>(args)...),
168 make_default_delegate<num_default_delegate_arguments<Args...>()>(
std::forward<Args>(args)...))
188 _on_label_constraints = _on_label_widget->update_constraints();
189 _off_label_constraints = _off_label_widget->update_constraints();
191 _label_constraints = max(_on_label_constraints, _off_label_constraints);
194 auto const extra_size =
extent2{theme().margin<
float>() * 2.0f, theme().margin<
float>()};
195 return _label_constraints + extra_size;
198 void set_layout(widget_layout
const& context)
noexcept override
201 auto const label_rectangle = aarectangle{
202 theme().margin<
float>(),
204 context.width() - theme().margin<
float>() * 2.0f,
205 context.height() - theme().margin<
float>()};
206 _on_label_shape = _off_label_shape =
207 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
213 _on_label_widget->set_layout(context.transform(_on_label_shape));
214 _off_label_widget->set_layout(context.transform(_off_label_shape));
217 void draw(draw_context
const& context)
noexcept override
220 draw_toolbar_tab_button(context);
221 _on_label_widget->draw(context);
222 _off_label_widget->draw(context);
226 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
231 [[nodiscard]] generator<widget_intf&> children(
bool include_invisible)
noexcept override
233 co_yield *_on_label_widget;
234 co_yield *_off_label_widget;
237 [[nodiscard]]
color background_color() const noexcept
override
239 hi_axiom(loop::main().on_thread());
240 if (phase() == widget_phase::pressed) {
241 return theme().fill_color(_layout.layer + 2);
243 return super::background_color();
247 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept override
249 hi_axiom(loop::main().on_thread());
252 return {
id, _layout.elevation, hitbox_type::button};
258 void activate() noexcept
265 bool handle_event(gui_event
const& event)
noexcept override
267 hi_axiom(loop::main().on_thread());
269 switch (event.type()) {
270 case gui_event_type::gui_activate:
277 case gui_event_type::mouse_down:
284 case gui_event_type::mouse_up:
289 handle_event(gui_event_type::gui_activate);
302 std::unique_ptr<label_widget> _on_label_widget;
303 box_constraints _on_label_constraints;
304 box_shape _on_label_shape;
306 std::unique_ptr<label_widget> _off_label_widget;
307 box_constraints _off_label_constraints;
308 box_shape _off_label_shape;
310 callback<void()> _delegate_cbt;
313 box_constraints _label_constraints;
315 void draw_toolbar_tab_button(draw_context
const& context)
noexcept
319 auto const offset = theme().margin<
float>() + theme().border_width();
320 auto const outline_rectangle = aarectangle{0, -offset,
layout().width(),
layout().height() + offset};
323 auto const button_z = focus() ? translate_z(0.6f) : translate_z(0.0f);
326 auto button_color = (phase() == widget_phase::hover or value() == widget_value::on) ?
327 theme().fill_color(_layout.layer - 1) :
328 theme().fill_color(_layout.layer);
331 auto const corner_radii = hi::corner_radii(0.0f, 0.0f, theme().rounding_radius<float>(), theme().rounding_radius<float>());
335 button_z * outline_rectangle,
337 focus() ? focus_color() : button_color,
338 theme().border_width(),
@ rectangle
The gui_event has rectangle data.
Definition gui_event_variant.hpp:44
@ partial
A widget is partially enabled.
Definition widget_state.hpp:73
@ invisible
The widget is invisible.
Definition widget_state.hpp:41
@ display
The widget is in display-only mode.
Definition widget_state.hpp:55
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
A high-level geometric extent.
Definition extent2.hpp:32
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
widget_intf * parent() const noexcept
Pointer to the parent widget.
Definition widget_intf.hpp:113
A localizable message.
Definition txt.hpp:100
2D constraints.
Definition box_constraints.hpp:25
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
void reset() noexcept
Reset the observer.
Definition observer_intf.hpp:423
A radio delegate controls the state of a radio button widget.
Definition radio_delegate.hpp:16
A default radio button delegate.
Definition radio_delegate.hpp:56
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition toolbar_tab_button_widget.hpp:54
toolbar_tab_button_widget(Args &&...args)
Construct a toolbar tab button widget with a default radio delegate.
Definition toolbar_tab_button_widget.hpp:163
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition toolbar_tab_button_widget.hpp:117
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition toolbar_tab_button_widget.hpp:172
toolbar_tab_button_widget(attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a toolbar tab button widget.
Definition toolbar_tab_button_widget.hpp:137
Definition toolbar_tab_button_widget.hpp:59
observer< label > off_label
The label to show when the button is in the 'off' state.
Definition toolbar_tab_button_widget.hpp:66
observer< label > on_label
The label to show when the button is in the 'on' state.
Definition toolbar_tab_button_widget.hpp:62
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
True if T is a forwarded type of Forward.
Definition concepts.hpp:137
Definition label_widget.hpp:30
Definition toolbar_tab_button_widget.hpp:21