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>
82 set_attributes<0>(std::forward<Attributes>(attributes)...);
85 template<
size_t NumLabels>
86 void set_attributes() noexcept
91 void set_attributes(First&& first, Rest&&...rest)
noexcept
94 if constexpr (NumLabels == 0) {
98 }
else if constexpr (NumLabels == 1) {
103 hi_static_no_default(
"Maximum two label attributes (on/off) are allowed on a toolbar-tab-button");
105 return set_attributes<NumLabels + 1>(std::forward<Rest>(rest)...);
107 }
else if constexpr (forward_of<First, observer<hi::alignment>>) {
108 alignment = std::forward<First>(first);
109 return set_attributes<NumLabels>(std::forward<Rest>(rest)...);
111 }
else if constexpr (forward_of<First, observer<hi::semantic_text_style>>) {
113 return set_attributes<NumLabels>(std::forward<Rest>(rest)...);
116 hi_static_no_default();
121 attributes_type attributes;
128 hi_call_left_arguments(
static, make_default_delegate, make_shared_ctad<default_radio_delegate>);
151 _on_label_widget = std::make_unique<label_widget>(
152 this, this->attributes.on_label, this->attributes.alignment, this->attributes.text_style);
153 _off_label_widget = std::make_unique<label_widget>(
154 this, this->attributes.off_label, this->attributes.alignment, this->attributes.text_style);
156 hi_axiom_not_null(this->delegate);
157 this->delegate->init(*
this);
158 _delegate_cbt = this->delegate->subscribe([&] {
159 set_value(this->delegate->state(*
this));
170 template<
typename... Args>
172 requires(num_default_delegate_arguments<Args...>() != 0)
176 make_attributes<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...),
177 make_default_delegate<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...))
197 _on_label_constraints = _on_label_widget->update_constraints();
198 _off_label_constraints = _off_label_widget->update_constraints();
200 _label_constraints = max(_on_label_constraints, _off_label_constraints);
203 auto const extra_size =
extent2{theme().margin<
float>() * 2.0f, theme().margin<
float>()};
204 return _label_constraints + extra_size;
207 void set_layout(widget_layout
const& context)
noexcept override
210 auto const label_rectangle = aarectangle{
211 theme().margin<
float>(),
213 context.width() - theme().margin<
float>() * 2.0f,
214 context.height() - theme().margin<
float>()};
215 _on_label_shape = _off_label_shape =
216 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
222 _on_label_widget->set_layout(context.transform(_on_label_shape));
223 _off_label_widget->set_layout(context.transform(_off_label_shape));
226 void draw(draw_context
const& context)
noexcept override
229 draw_toolbar_tab_button(context);
230 _on_label_widget->draw(context);
231 _off_label_widget->draw(context);
235 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
240 [[nodiscard]] generator<widget_intf&> children(
bool include_invisible)
noexcept override
242 co_yield *_on_label_widget;
243 co_yield *_off_label_widget;
246 [[nodiscard]] color background_color() const noexcept
override
248 hi_axiom(loop::main().on_thread());
249 if (phase() == widget_phase::pressed) {
250 return theme().color(semantic_color::fill, _layout.layer + 2);
252 return super::background_color();
256 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept override
258 hi_axiom(loop::main().on_thread());
261 return {
id, _layout.elevation, hitbox_type::button};
267 void activate() noexcept
274 bool handle_event(gui_event
const& event)
noexcept override
276 hi_axiom(loop::main().on_thread());
278 switch (event.type()) {
279 case gui_event_type::gui_activate:
286 case gui_event_type::mouse_down:
293 case gui_event_type::mouse_up:
298 handle_event(gui_event_type::gui_activate);
312 box_constraints _on_label_constraints;
313 box_shape _on_label_shape;
316 box_constraints _off_label_constraints;
317 box_shape _off_label_shape;
319 callback<void()> _delegate_cbt;
322 box_constraints _label_constraints;
324 void draw_toolbar_tab_button(draw_context
const& context)
noexcept
328 auto const offset = theme().margin<
float>() + theme().border_width();
329 auto const outline_rectangle = aarectangle{0, -offset,
layout().width(),
layout().height() + offset};
332 auto const button_z = focus() ? translate_z(0.6f) : translate_z(0.0f);
335 auto button_color = (phase() == widget_phase::hover or value() == widget_value::on) ?
336 theme().color(semantic_color::fill, _layout.layer - 1) :
337 theme().color(semantic_color::fill, _layout.layer);
340 auto const corner_radii =
hi::corner_radii(0.0f, 0.0f, theme().rounding_radius<float>(), theme().rounding_radius<float>());
344 button_z * outline_rectangle,
346 focus() ? focus_color() : button_color,
347 theme().border_width(),
@ rectangle
The gui_event has rectangle data.
widget_mode
The mode that the widget is operating at.
Definition widget_state.hpp:25
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
@ display
The widget is in display-only mode.
The HikoGUI namespace.
Definition array_generic.hpp:20
border_side
The side where the border is drawn.
Definition draw_context_intf.hpp:28
@ 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
The 4 radii of the corners of a quad or rectangle.
Definition corner_radii.hpp:26
A high-level geometric extent.
Definition extent2.hpp:32
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
virtual void request_redraw() const noexcept=0
Request the widget to be redrawn on the next frame.
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
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:421
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(widget_intf const *parent, attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a toolbar tab button widget.
Definition toolbar_tab_button_widget.hpp:145
toolbar_tab_button_widget(widget_intf const *parent, Args &&...args)
Construct a toolbar tab button widget with a default radio delegate.
Definition toolbar_tab_button_widget.hpp:171
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition toolbar_tab_button_widget.hpp:125
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition toolbar_tab_button_widget.hpp:181
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< semantic_text_style > text_style
The text style to button's label.
Definition toolbar_tab_button_widget.hpp:70
observer< label > on_label
The label to show when the button is in the 'on' state.
Definition toolbar_tab_button_widget.hpp:62
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
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 label_widget.hpp:30
Definition toolbar_tab_button_widget.hpp:21