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 {
74 attributes_type(attributes_type
const&)
noexcept =
default;
75 attributes_type(attributes_type&&) noexcept = default;
76 attributes_type& operator=(attributes_type const&) noexcept = default;
77 attributes_type& operator=(attributes_type&&) noexcept = default;
80 explicit attributes_type(Attributes&&...attributes) noexcept
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");
107 }
else if constexpr (forward_of<First, observer<hi::alignment>>) {
111 }
else if constexpr (forward_of<First, observer<hi::semantic_text_style>>) {
116 hi_static_no_default();
128 hi_call_left_arguments(
static, make_default_delegate, make_shared_ctad_not_null<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 this->delegate->init(*
this);
157 _delegate_cbt = this->delegate->subscribe([&] {
158 set_value(this->delegate->state(*
this));
169 template<
typename... Args>
171 requires(num_default_delegate_arguments<Args...>() != 0)
175 make_attributes<num_default_delegate_arguments<Args...>()>(
std::forward<Args>(args)...),
176 make_default_delegate<num_default_delegate_arguments<Args...>()>(
std::forward<Args>(args)...))
196 _on_label_constraints = _on_label_widget->update_constraints();
197 _off_label_constraints = _off_label_widget->update_constraints();
199 _label_constraints = max(_on_label_constraints, _off_label_constraints);
202 auto const extra_size =
extent2{theme().margin<
float>() * 2.0f, theme().margin<
float>()};
203 return _label_constraints + extra_size;
206 void set_layout(widget_layout
const& context)
noexcept override
209 auto const label_rectangle = aarectangle{
210 theme().margin<
float>(),
212 context.width() - theme().margin<
float>() * 2.0f,
213 context.height() - theme().margin<
float>()};
214 _on_label_shape = _off_label_shape =
215 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
221 _on_label_widget->set_layout(context.transform(_on_label_shape));
222 _off_label_widget->set_layout(context.transform(_off_label_shape));
225 void draw(draw_context
const& context)
noexcept override
228 draw_toolbar_tab_button(context);
229 _on_label_widget->draw(context);
230 _off_label_widget->draw(context);
234 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
239 [[nodiscard]] generator<widget_intf&> children(
bool include_invisible)
noexcept override
241 co_yield *_on_label_widget;
242 co_yield *_off_label_widget;
245 [[nodiscard]] color background_color() const noexcept
override
247 hi_axiom(loop::main().on_thread());
248 if (phase() == widget_phase::pressed) {
249 return theme().color(semantic_color::fill, _layout.layer + 2);
251 return super::background_color();
255 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept override
257 hi_axiom(loop::main().on_thread());
260 return {
id, _layout.elevation, hitbox_type::button};
266 void activate() noexcept
273 bool handle_event(gui_event
const& event)
noexcept override
275 hi_axiom(loop::main().on_thread());
277 switch (event.type()) {
278 case gui_event_type::gui_activate:
285 case gui_event_type::mouse_down:
292 case gui_event_type::mouse_up:
297 handle_event(gui_event_type::gui_activate);
310 std::unique_ptr<label_widget> _on_label_widget;
311 box_constraints _on_label_constraints;
312 box_shape _on_label_shape;
314 std::unique_ptr<label_widget> _off_label_widget;
315 box_constraints _off_label_constraints;
316 box_shape _off_label_shape;
318 callback<void()> _delegate_cbt;
321 box_constraints _label_constraints;
323 void draw_toolbar_tab_button(draw_context
const& context)
noexcept
327 auto const offset = theme().margin<
float>() + theme().border_width();
328 auto const outline_rectangle = aarectangle{0, -offset,
layout().width(),
layout().height() + offset};
331 auto const button_z = focus() ? translate_z(0.6f) : translate_z(0.0f);
334 auto button_color = (phase() == widget_phase::hover or value() == widget_value::on) ?
335 theme().color(semantic_color::fill, _layout.layer - 1) :
336 theme().color(semantic_color::fill, _layout.layer);
339 auto const corner_radii = hi::corner_radii(0.0f, 0.0f, theme().rounding_radius<float>(), theme().rounding_radius<float>());
343 button_z * outline_rectangle,
345 focus() ? focus_color() : button_color,
346 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:72
@ invisible
The widget is invisible.
Definition widget_state.hpp:40
@ display
The widget is in display-only mode.
Definition widget_state.hpp:54
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
The HikoGUI API version 1.
Definition recursive_iterator.hpp:16
@ 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: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
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
Definition not_null.hpp:22
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(not_null< widget_intf const * > parent, Args &&...args)
Construct a toolbar tab button widget with a default radio delegate.
Definition toolbar_tab_button_widget.hpp:170
not_null< 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:180
toolbar_tab_button_widget(not_null< widget_intf const * > parent, attributes_type attributes, not_null< std::shared_ptr< delegate_type > > delegate) noexcept
Construct a toolbar tab button widget.
Definition toolbar_tab_button_widget.hpp:145
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
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:135
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:109
widget(widget_intf const *parent) noexcept
Definition widget.hpp:49
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:144
True if T is a forwarded type of Forward.
Definition concepts.hpp:136
Definition label_widget.hpp:30
Definition toolbar_tab_button_widget.hpp:21