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();
87 this->set_attributes<0>(
hi_forward(attributes)...);
93 _delegate_cbt = this->delegate->subscribe([&] {
94 ++global_counter<
"checkbox_widget:delegate:redraw">;
96 state = this->delegate->state(
this);
99 this->delegate->init(*
this);
133 different_from<std::shared_ptr<delegate_type>> Value,
134 forward_of<observer<observer_decay_t<Value>>> OnValue,
135 checkbox_widget_attribute... Attributes>
159 different_from<std::shared_ptr<delegate_type>> Value,
160 forward_of<observer<observer_decay_t<Value>>> OnValue,
161 forward_of<observer<observer_decay_t<Value>>> OffValue,
162 checkbox_widget_attribute... Attributes>
176 _on_label_constraints = _on_label_widget->update_constraints();
177 _off_label_constraints = _off_label_widget->update_constraints();
178 _other_label_constraints = _other_label_widget->update_constraints();
179 _label_constraints = max(_on_label_constraints, _off_label_constraints, _other_label_constraints);
181 _button_size = theme<prefix>.size(
this);
182 hilet extra_size =
extent2i{theme<prefix>.spacing_horizontal(
this) + _button_size.
width(), 0};
184 auto constraints = max(_label_constraints + extra_size, _button_size);
185 constraints.margins = theme<prefix>.margin(
this);
186 constraints.alignment = *this->
alignment;
190 void set_layout(widget_layout
const& context)
noexcept override
193 auto alignment_ = os_settings::left_to_right() ? *this->
alignment :
mirror(*this->alignment);
195 if (alignment_ == horizontal_alignment::left or alignment_ == horizontal_alignment::right) {
196 _button_rectangle =
align(context.rectangle(), _button_size, alignment_);
201 hilet inner_margin = theme<prefix>.spacing_horizontal(
this);
202 hilet baseline_offset = theme<prefix>.cap_height(
this);
204 hilet label_width = context.width() - (_button_rectangle.width() + inner_margin);
205 if (alignment_ == horizontal_alignment::left) {
206 hilet label_left = _button_rectangle.right() + inner_margin;
207 hilet label_rectangle = aarectanglei{label_left, 0, label_width, context.height()};
208 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
209 box_shape(_label_constraints, label_rectangle, baseline_offset);
211 }
else if (alignment_ == horizontal_alignment::right) {
212 hilet label_rectangle = aarectanglei{0, 0, label_width, context.height()};
213 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
214 box_shape(_label_constraints, label_rectangle, baseline_offset);
220 hilet check_glyph_bb =
221 narrow_cast<aarectanglei>(_check_glyph.get_bounding_rectangle() * theme<prefix>.line_height(
this));
222 _check_glyph_rectangle =
align(_button_rectangle, check_glyph_bb, alignment::middle_center());
224 _minus_glyph =
find_glyph(elusive_icon::Minus);
225 hilet minus_glyph_bb =
226 narrow_cast<aarectanglei>(_minus_glyph.get_bounding_rectangle() * theme<prefix>.line_height(
this));
227 _minus_glyph_rectangle =
align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
234 _on_label_widget->set_layout(context.transform(_on_label_shape));
235 _off_label_widget->set_layout(context.transform(_off_label_shape));
236 _other_label_widget->set_layout(context.transform(_other_label_shape));
239 void draw(widget_draw_context& context)
noexcept override
242 draw_check_box(context);
243 draw_check_mark(context);
246 _on_label_widget->draw(context);
247 _off_label_widget->draw(context);
248 _other_label_widget->draw(context);
251 [[nodiscard]] generator<widget const&> children(
bool include_invisible)
const noexcept override
253 co_yield *_on_label_widget;
254 co_yield *_off_label_widget;
255 co_yield *_other_label_widget;
258 [[nodiscard]] hitbox hitbox_test(point2i position)
const noexcept final
263 return {
id, layout.
elevation, hitbox_type::button};
269 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
275 void activate() noexcept
279 this->_state_changed();
282 bool handle_event(gui_event
const& event)
noexcept override
286 switch (event.type()) {
287 case gui_event_type::gui_activate:
294 case gui_event_type::mouse_down:
302 case gui_event_type::mouse_up:
306 if (layout.rectangle().
contains(event.mouse().position)) {
307 handle_event(gui_event_type::gui_activate);
322 box_constraints _on_label_constraints;
323 box_shape _on_label_shape;
326 box_constraints _off_label_constraints;
327 box_shape _off_label_shape;
330 box_constraints _other_label_constraints;
331 box_shape _other_label_shape;
333 notifier<>::callback_token _delegate_cbt;
335 box_constraints _label_constraints;
338 aarectanglei _button_rectangle;
339 font_book::font_glyph_type _check_glyph;
340 aarectanglei _check_glyph_rectangle;
341 font_book::font_glyph_type _minus_glyph;
342 aarectanglei _minus_glyph_rectangle;
344 void draw_check_box(widget_draw_context& context)
noexcept
349 theme<prefix>.background_color(
this),
350 theme<prefix>.border_color(
this),
351 theme<prefix>.border_width(
this),
355 void draw_check_mark(widget_draw_context& context)
noexcept
361 translate_z(0.1f) * narrow_cast<aarectangle>(_check_glyph_rectangle),
364 theme<prefix>.fill_color(
this));
373 translate_z(0.1f) * narrow_cast<aarectangle>(_minus_glyph_rectangle),
376 theme<prefix>.fill_color(
this));
380 template<
size_t LabelCount>
381 void set_attributes() noexcept
385 template<
size_t LabelCount>
386 void set_attributes(checkbox_widget_attribute
auto&& first, checkbox_widget_attribute
auto&&...rest)
noexcept
388 if constexpr (forward_of<
decltype(first), observer<hi::label>>) {
389 if constexpr (LabelCount == 0) {
393 }
else if constexpr (LabelCount == 1) {
397 }
else if constexpr (LabelCount == 2) {
402 set_attributes<LabelCount + 1>(
hi_forward(rest)...);
404 }
else if constexpr (forward_of<
decltype(first), observer<hi::alignment>>) {
406 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_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:335
#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
geo::extent< int, 2 > extent2i
A 2D extent.
Definition extent.hpp:512
@ 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: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
auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:223
constexpr horizontal_alignment mirror(horizontal_alignment const &rhs) noexcept
Mirror the horizontal alignment.
Definition alignment.hpp:192
constexpr auto join_path(fixed_string< L > const &lhs, fixed_string< R > const &rhs) noexcept
lhs / rhs
Definition fixed_string.hpp:273
geometry/margins.hpp
Definition cache.hpp:11
@ inside
The border is drawn inside 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 bool contains(point< value_type, 2 > const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:265
constexpr value_type & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:166
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:265
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:274
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
constexpr bool contains(point3i mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:126
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:72
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:163
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:112
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:136
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