13namespace hi {
inline namespace v1 {
42template<fixed_string Name =
"">
47 constexpr static auto prefix = Name /
"radio";
55 observer<label>
label = tr(
"<not set>");
59 observer<hi::alignment>
alignment = alignment::top_left();
77 this->set_attributes<0>(
hi_forward(attributes)...);
79 _label_widget = std::make_unique<label_widget<prefix>>(
this, on_label,
alignment);
81 _grid.add_cell(0, 0, cell_type::button);
82 _grid.add_cell(1, 0, cell_type::label);
84 _delegate_cbt = this->delegate->subscribe([&] {
85 ++global_counter<
"radio_button_widget:delegate:redraw">;
87 state = this->delegate->state(
this);
91 this->delegate->init(*
this);
108 different_from<std::shared_ptr<delegate_type>> Value,
109 forward_of<observer<observer_decay_t<Value>>> OnValue,
124 for (
auto& cell : _grid) {
125 if (cell.value == cell_type::button) {
127 theme<prefix>.size(
this),
128 theme<prefix>.size(
this),
129 theme<prefix>.size(
this),
131 theme<prefix>.margin(
this),
132 -vector2::infinity()});
134 }
else if (cell.value == cell_type::label) {
135 cell.set_constraints(
max(_label_widget->update_constraints()));
142 return _grid.constraints(os_settings::left_to_right());
145 void set_layout(widget_layout
const& context)
noexcept override
148 _grid.set_layout(context.shape, theme<prefix>.cap_height(
this));
151 for (
hilet& cell : _grid) {
152 if (cell.value == cell_type::button) {
153 _button_rectangle =
align(cell.shape.rectangle, theme<prefix>.
size(
this), *
alignment);
154 _pip_rectangle =
align(_button_rectangle, theme<prefix / "pip">.size(
this), alignment::middle_center());
156 }
else if (cell.value == cell_type::label) {
157 _label_widget->set_layout(context.transform(cell.shape, 0.0f));
165 void draw(widget_draw_context& context)
noexcept override
168 for (
hilet& cell : _grid) {
169 if (cell.value == cell_type::button) {
170 draw_button(context);
173 }
else if (cell.value == cell_type::label) {
174 _label_widget->draw(context);
183 [[nodiscard]] generator<widget const&> children(
bool include_invisible)
const noexcept override
185 co_yield *_label_widget;
188 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept final
193 return {
id, layout.elevation, hitbox_type::button};
199 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
205 void activate() noexcept
209 this->_state_changed();
212 bool handle_event(gui_event
const& event)
noexcept override
216 switch (event.type()) {
217 case gui_event_type::gui_activate:
224 case gui_event_type::mouse_down:
232 case gui_event_type::mouse_up:
236 if (layout.rectangle().contains(event.mouse().position)) {
237 handle_event(gui_event_type::gui_activate);
251 enum class cell_type { button,
label };
255 grid_layout<cell_type> _grid;
258 notifier<>::callback_token _delegate_cbt;
260 aarectangle _button_rectangle;
261 aarectangle _pip_rectangle;
263 animator<float> _animated_value = _animation_duration;
265 void draw_button(widget_draw_context& context)
noexcept
270 theme<prefix>.background_color(
this),
271 theme<prefix>.border_color(
this),
272 theme<prefix>.border_width(
this),
274 theme<prefix>.border_radius(
this));
277 void draw_pip(widget_draw_context& context)
noexcept
280 if (_animated_value.is_animating()) {
285 auto float_value = _animated_value.current_value();
286 if (float_value > 0.0f) {
289 _pip_rectangle * float_value,
290 theme<prefix / "pip">.background_color(
this),
291 theme<prefix / "pip">.border_color(
this),
292 theme<prefix / "pip">.border_width(
this),
294 theme<prefix / "pip">.border_radius(
this) * float_value);
Defines radio_button_delegate and some default radio_button_delegate delegates.
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
@ window_redraw
Request that part of the window gets redrawn on the next frame.
std::shared_ptr< radio_button_delegate > make_default_radio_button_delegate(auto &&value, auto &&...args) noexcept
Make a shared pointer to a toggle-button delegate.
Definition radio_button_delegate.hpp:113
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
@ inside
The border is drawn inside the edge of a quad.
@ outside
The border is drawn outside the edge of a quad.
@ off
The widget in the off-state.
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
constexpr extent< value_type, 2 > size() const noexcept
Get size of the rectangle.
Definition axis_aligned_rectangle.hpp:183
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:35
virtual void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
Definition widget.hpp:227
observer< bool > clicked
The widget is being clicked by the mouse.
Definition widget.hpp:57
observer< widget_state > state
The state of the widget.
Definition widget.hpp:65
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
Definition widget.hpp:236
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
2D constraints.
Definition box_constraints.hpp:22
A radio button delegate controls the state of a radio button widget.
Definition radio_button_delegate.hpp:18
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition radio_button_widget.hpp:43
radio_button_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a radio button widget with a default button delegate.
Definition radio_button_widget.hpp:111
observer< label > label
The label to show when the button is in the 'on' state.
Definition radio_button_widget.hpp:55
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition radio_button_widget.hpp:51
radio_button_widget(widget *parent, std::shared_ptr< delegate_type > delegate, button_widget_attribute auto &&...attributes) noexcept
Construct a radio button widget.
Definition radio_button_widget.hpp:70
observer< hi::alignment > alignment
The alignment of the button and on/off/other label.
Definition radio_button_widget.hpp:59
Definition abstract_button_widget.hpp:26