11#include "../GUI/module.hpp"
14namespace hi {
inline namespace v1 {
16template<
typename Context>
51template<fixed_string Name =
"">
56 constexpr static auto prefix = Name /
"toggle";
76 observer<hi::alignment>
alignment = alignment::top_left();
91 this->set_attributes<0>(
hi_forward(attributes)...);
97 _grid.add_cell(0, 0, cell_type::button);
98 _grid.add_cell(1, 0, cell_type::label);
100 _delegate_cbt = this->delegate->subscribe([&] {
101 ++global_counter<
"toggle_widget:delegate:redraw">;
103 state = this->delegate->state(
this);
111 this->delegate->init(*
this);
125 template<different_from<std::shared_ptr<delegate_type>> Value, toggle_w
idget_attribute... Attributes>
144 different_from<std::shared_ptr<delegate_type>> Value,
145 forward_of<observer<observer_decay_t<Value>>> OnValue,
146 toggle_widget_attribute... Attributes>
166 different_from<std::shared_ptr<delegate_type>> Value,
167 forward_of<observer<observer_decay_t<Value>>> OnValue,
168 forward_of<observer<observer_decay_t<Value>>> OffValue,
169 toggle_widget_attribute... Attributes>
183 for (
auto& cell : _grid) {
184 if (cell.value == cell_type::button) {
186 theme<prefix>.size(
this),
187 theme<prefix>.size(
this),
188 theme<prefix>.size(
this),
190 theme<prefix>.margin(
this),
191 -vector2::infinity()});
193 }
else if (cell.value == cell_type::label) {
194 cell.set_constraints(
195 max(_on_label_widget->update_constraints(),
196 _off_label_widget->update_constraints(),
197 _other_label_widget->update_constraints()));
204 return _grid.constraints(os_settings::left_to_right());
207 void set_layout(widget_layout
const& context)
noexcept override
210 _grid.set_layout(context.shape, theme<prefix>.cap_height(
this));
213 for (
hilet& cell : _grid) {
214 if (cell.value == cell_type::button) {
215 _button_rectangle =
align(cell.shape.rectangle, theme<prefix>.
size(
this), *
alignment);
220 _pip_edge_distance = (_button_rectangle.height() -
theme<prefix /
"pip">.height(
this)) / 2;
221 _pip_move_range = _button_rectangle.width() - _pip_edge_distance * 2 -
theme<prefix /
"pip">.width(
this);
223 }
else if (cell.value == cell_type::label) {
224 _on_label_widget->set_layout(context.transform(cell.shape, 0.0f));
225 _off_label_widget->set_layout(context.transform(cell.shape, 0.0f));
226 _other_label_widget->set_layout(context.transform(cell.shape, 0.0f));
234 void draw(widget_draw_context& context)
noexcept override
237 for (
hilet& cell : _grid) {
238 if (cell.value == cell_type::button) {
239 draw_button(context, _button_rectangle);
240 draw_pip(context, _button_rectangle);
242 }
else if (cell.value == cell_type::label) {
243 _on_label_widget->draw(context);
244 _off_label_widget->draw(context);
245 _other_label_widget->draw(context);
254 [[nodiscard]] generator<widget const&> children(
bool include_invisible)
const noexcept override
256 co_yield *_on_label_widget;
257 co_yield *_off_label_widget;
258 co_yield *_other_label_widget;
261 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept final
266 return {
id, layout.elevation, hitbox_type::button};
272 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
278 void activate() noexcept
285 bool handle_event(gui_event
const& event)
noexcept override
289 switch (event.type()) {
290 case gui_event_type::gui_activate:
297 case gui_event_type::mouse_down:
305 case gui_event_type::mouse_up:
309 if (layout.rectangle().contains(event.mouse().position)) {
310 handle_event(gui_event_type::gui_activate);
324 enum class cell_type { button, label };
328 grid_layout<cell_type> _grid;
334 notifier<>::callback_token _delegate_cbt;
336 aarectangle _button_rectangle;
338 animator<float> _animated_value = _animation_duration;
339 float _pip_move_range;
340 float _pip_edge_distance;
342 void draw_button(widget_draw_context& context, aarectangle shape)
noexcept
347 theme<prefix>.background_color(
this),
348 theme<prefix>.border_color(
this),
349 theme<prefix>.border_width(
this),
351 theme<prefix>.border_radius(
this));
354 void draw_pip(widget_draw_context& context, aarectangle shape)
noexcept
357 if (_animated_value.is_animating()) {
361 hilet move_offset = [&] {
362 if (os_settings::left_to_right()) {
363 return _pip_move_range * _animated_value.current_value();
365 return _pip_move_range * (1.0f - _animated_value.current_value());
369 hilet pip_translate = translate3{shape.x() + _pip_edge_distance + move_offset, shape.y() + _pip_edge_distance, 0.1f};
370 hilet pip_rectangle = pip_translate *aarectangle{
theme<prefix /
"pip">.size(
this)};
375 theme<prefix / "pip">.background_color(
this),
376 theme<prefix /
"pip">.border_color(
this),
377 theme<prefix /
"pip">.border_width(
this),
379 theme<prefix /
"pip">.border_radius(
this));
382 template<
size_t LabelCount>
383 void set_attributes() noexcept
387 template<
size_t LabelCount>
388 void set_attributes(toggle_widget_attribute
auto&& first, toggle_widget_attribute
auto&&...rest)
noexcept
390 if constexpr (forward_of<
decltype(first), observer<hi::label>>) {
391 if constexpr (LabelCount == 0) {
395 }
else if constexpr (LabelCount == 1) {
399 }
else if constexpr (LabelCount == 2) {
404 set_attributes<LabelCount + 1>(
hi_forward(rest)...);
406 }
else if constexpr (forward_of<
decltype(first), observer<hi::alignment>>) {
408 set_attributes<LabelCount>(
hi_forward(rest)...);
Defines toggle_delegate and some default toggle_delegate delegates.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:323
#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< toggle_delegate > make_default_toggle_delegate(auto &&value, auto &&...args) noexcept
Make a shared pointer to a toggle-button delegate.
Definition toggle_delegate.hpp:144
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
@ display
The widget is in display-only mode.
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.
@ other
The widget is in the other-state.
@ on
The widget is in the on-state.
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
auto theme
A tagged global variable to a theme model for a widget's component.
Definition theme_model.hpp:545
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
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:42
A toggle delegate controls the state of a toggle widget.
Definition toggle_delegate.hpp:18
A GUI widget that permits the user to make a binary choice.
Definition toggle_widget.hpp:52
observer< label > other_label
The label to show when the button is in the 'other' state.
Definition toggle_widget.hpp:72
observer< label > on_label
The label to show when the button is in the 'on' state.
Definition toggle_widget.hpp:64
toggle_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:147
observer< hi::alignment > alignment
The alignment of the button and on/off/other label.
Definition toggle_widget.hpp:76
toggle_widget(widget *parent, Value &&value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:126
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition toggle_widget.hpp:60
toggle_widget(widget *parent, std::shared_ptr< delegate_type > delegate, toggle_widget_attribute auto &&...attributes) noexcept
Construct a toggle widget.
Definition toggle_widget.hpp:87
toggle_widget(widget *parent, Value &&value, OnValue &&on_value, OffValue &&off_value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:170
observer< label > off_label
The label to show when the button is in the 'off' state.
Definition toggle_widget.hpp:68
Definition label_widget.hpp:26
Definition toggle_widget.hpp:17