14namespace hi {
inline namespace v1 {
16template<
typename Context>
45template<fixed_string Name =
"">
50 constexpr static auto prefix = Name /
"checkbox";
70 observer<hi::alignment>
alignment = alignment::top_left();
88 this->set_attributes<0>(
hi_forward(attributes)...);
94 _grid.add_cell(0, 0, cell_type::button);
95 _grid.add_cell(1, 0, cell_type::label);
97 _delegate_cbt = this->delegate->subscribe([&] {
98 ++global_counter<
"checkbox_widget:delegate:redraw">;
100 state = this->delegate->state(
this);
108 this->delegate->init(*
this);
143 different_from<std::shared_ptr<delegate_type>> Value,
144 forward_of<observer<observer_decay_t<Value>>> OnValue,
145 checkbox_widget_attribute... Attributes>
169 different_from<std::shared_ptr<delegate_type>> Value,
170 forward_of<observer<observer_decay_t<Value>>> OnValue,
171 forward_of<observer<observer_decay_t<Value>>> OffValue,
172 checkbox_widget_attribute... Attributes>
186 _check_glyph = find_glyph(elusive_icon::Ok);
187 _minus_glyph = find_glyph(elusive_icon::Minus);
189 for (
auto& cell : _grid) {
190 if (cell.value == cell_type::button) {
192 theme<prefix>.size(
this),
193 theme<prefix>.size(
this),
194 theme<prefix>.size(
this),
196 theme<prefix>.margin(
this),
197 -vector2::infinity()});
199 }
else if (cell.value == cell_type::label) {
200 cell.set_constraints(
201 max(_on_label_widget->update_constraints(),
202 _off_label_widget->update_constraints(),
203 _other_label_widget->update_constraints()));
210 return _grid.constraints(os_settings::left_to_right());
213 void set_layout(widget_layout
const& context)
noexcept override
216 _grid.set_layout(context.shape, theme<prefix>.cap_height(
this));
219 for (
hilet& cell : _grid) {
220 if (cell.value == cell_type::button) {
221 _button_rectangle =
align(cell.shape.rectangle, theme<prefix>.
size(
this), *
alignment);
223 hilet mark_size =
std::min(theme<prefix / "mark">.width(
this), theme<prefix / "mark">.height(
this));
224 hilet check_glyph_bb = _check_glyph.get_bounding_rectangle() * mark_size;
225 hilet minus_glyph_bb = _minus_glyph.get_bounding_rectangle() * mark_size;
227 _check_glyph_rectangle =
align(_button_rectangle, check_glyph_bb, alignment::middle_center());
228 _minus_glyph_rectangle =
align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
230 }
else if (cell.value == cell_type::label) {
231 _on_label_widget->set_layout(context.transform(cell.shape, 0.0f));
232 _off_label_widget->set_layout(context.transform(cell.shape, 0.0f));
233 _other_label_widget->set_layout(context.transform(cell.shape, 0.0f));
241 void draw(widget_draw_context& context)
noexcept override
244 for (
hilet& cell : _grid) {
245 if (cell.value == cell_type::button) {
246 draw_button(context);
249 }
else if (cell.value == cell_type::label) {
250 _on_label_widget->draw(context);
251 _off_label_widget->draw(context);
252 _other_label_widget->draw(context);
261 [[nodiscard]] generator<widget const&> children(
bool include_invisible)
const noexcept override
263 co_yield *_on_label_widget;
264 co_yield *_off_label_widget;
265 co_yield *_other_label_widget;
268 [[nodiscard]] hitbox hitbox_test(point2 position)
const noexcept final
273 return {
id, layout.elevation, hitbox_type::button};
279 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
285 void activate() noexcept
289 this->_state_changed();
292 bool handle_event(gui_event
const& event)
noexcept override
296 switch (event.type()) {
297 case gui_event_type::gui_activate:
304 case gui_event_type::mouse_down:
312 case gui_event_type::mouse_up:
316 if (layout.rectangle().contains(event.mouse().position)) {
317 handle_event(gui_event_type::gui_activate);
331 enum class cell_type { button, label };
333 grid_layout<cell_type> _grid;
339 notifier<>::callback_token _delegate_cbt;
341 aarectangle _button_rectangle;
343 font_book::font_glyph_type _check_glyph;
344 aarectangle _check_glyph_rectangle;
345 font_book::font_glyph_type _minus_glyph;
346 aarectangle _minus_glyph_rectangle;
348 void draw_button(widget_draw_context& context)
noexcept
353 theme<prefix>.background_color(
this),
354 theme<prefix>.border_color(
this),
355 theme<prefix>.border_width(
this),
357 theme<prefix>.border_radius(
this));
360 void draw_mark(widget_draw_context& context)
noexcept
366 translate_z(0.1f) * _check_glyph_rectangle,
369 theme<prefix>.fill_color(
this));
378 translate_z(0.1f) * _minus_glyph_rectangle,
381 theme<prefix>.fill_color(
this));
385 template<
size_t LabelCount>
386 void set_attributes() noexcept
390 template<
size_t LabelCount>
391 void set_attributes(checkbox_widget_attribute
auto&& first, checkbox_widget_attribute
auto&&...rest)
noexcept
393 if constexpr (forward_of<
decltype(first), observer<hi::label>>) {
394 if constexpr (LabelCount == 0) {
398 }
else if constexpr (LabelCount == 1) {
402 }
else if constexpr (LabelCount == 2) {
407 set_attributes<LabelCount + 1>(
hi_forward(rest)...);
409 }
else if constexpr (forward_of<
decltype(first), observer<hi::alignment>>) {
411 set_attributes<LabelCount>(
hi_forward(rest)...);
Defines checkbox_delegate and some default checkbox_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< checkbox_delegate > make_default_checkbox_delegate(auto &&value, auto &&...args) noexcept
Make a shared pointer to a toggle-button delegate.
Definition checkbox_delegate.hpp:146
@ 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
@ 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
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 toggle delegate controls the state of a toggle widget.
Definition checkbox_delegate.hpp:18
A GUI widget that permits the user to make a binary choice.
Definition checkbox_widget.hpp:46
observer< label > on_label
The label to show when the button is in the 'on' state.
Definition checkbox_widget.hpp:58
observer< label > other_label
The label to show when the button is in the 'other' state.
Definition checkbox_widget.hpp:66
observer< hi::alignment > alignment
The alignment of the button and on/off/other label.
Definition checkbox_widget.hpp:70
checkbox_widget(widget *parent, Value &&value, OnValue &&on_value, OffValue &&off_value, Attributes &&...attributes) noexcept
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:173
checkbox_widget(widget *parent, different_from< std::shared_ptr< delegate_type > > auto &&value, checkbox_widget_attribute auto &&...attributes) noexcept
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:122
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition checkbox_widget.hpp:54
checkbox_widget(widget *parent, std::shared_ptr< delegate_type > delegate, checkbox_widget_attribute auto &&...attributes) noexcept
Construct a checkbox widget.
Definition checkbox_widget.hpp:81
checkbox_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:146
observer< label > off_label
The label to show when the button is in the 'off' state.
Definition checkbox_widget.hpp:62
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:42
Definition checkbox_widget.hpp:17
Definition label_widget.hpp:26