7#include "widget_id.hpp"
8#include "widget_mode.hpp"
9#include "widget_state.hpp"
10#include "widget_layout.hpp"
12#include "keyboard_focus_group.hpp"
14#include "widget_draw_context.hpp"
15#include "../theme/module.hpp"
16#include "../geometry/module.hpp"
17#include "../layout/box_constraints.hpp"
18#include "../observer.hpp"
19#include "../generator.hpp"
22namespace hi {
inline namespace v1 {
28 using callback_token = notifier<void()>::callback_token;
29 using awaiter_type = notifier<void()>::awaiter_type;
89 observer<extent2i>
maximum = extent2i::large();
102 _mode_cbt =
mode.subscribe([&](
auto...) {
103 ++global_counter<
"widget:mode:constrain">;
108 widget(widget
const&) =
delete;
109 widget(widget&&) =
delete;
110 widget& operator=(widget&&) =
delete;
111 widget& operator=(widget
const&) =
delete;
115 [[nodiscard]]
virtual generator<widget const&>
children(
bool include_invisible)
const noexcept
120 [[nodiscard]] generator<widget&>
children(
bool include_invisible)
noexcept
122 for (
auto& child : const_cast<
widget const *>(this)->
children(include_invisible)) {
123 co_yield const_cast<widget&
>(child);
134 [[nodiscard]]
virtual hitbox
hitbox_test(point2i position)
const noexcept
178 _scale = narrow_cast<int>(
std::round(new_scale * -4.0));
231 auto s = theme_state{};
233 s |= theme_state::disabled;
235 s |= theme_state::active;
237 s |= theme_state::hover;
239 s |= theme_state::enabled;
243 s |= theme_state::focus;
247 s |= theme_state::on;
250 s |=
static_cast<theme_state
>((
semantic_layer % 4) * to_underlying(theme_state::layer_1));
251 return sub_theme_selector_type{s, _scale};
254 virtual bool process_event(gui_event
const& event)
const noexcept
257 return parent->process_event(event);
278 switch (event.type()) {
283 ++global_counter<
"widget:keyboard_enter:redraw">;
289 ++global_counter<
"widget:keyboard_exit:redraw">;
295 ++global_counter<
"widget:mouse_enter:redraw">;
301 ++global_counter<
"widget:mouse_exit:redraw">;
305 case gui_widget_next:
307 gui_event::window_set_keyboard_target(
id, keyboard_focus_group::normal, keyboard_focus_direction::forward));
310 case gui_widget_prev:
312 gui_event::window_set_keyboard_target(
id, keyboard_focus_group::normal, keyboard_focus_direction::backward));
315 case gui_activate_next:
316 process_event(gui_activate);
317 return process_event(gui_widget_next);
319 case gui_event_type::gui_toolbar_next:
321 not is_last(keyboard_focus_group::toolbar)) {
323 gui_event::window_set_keyboard_target(
id, keyboard_focus_group::toolbar, keyboard_focus_direction::forward));
328 case gui_event_type::gui_toolbar_prev:
330 not is_first(keyboard_focus_group::toolbar)) {
332 gui_event::window_set_keyboard_target(
id, keyboard_focus_group::toolbar, keyboard_focus_direction::backward));
355 auto handled =
false;
357 for (
auto& child :
children(false)) {
358 handled |= child.handle_event_recursive(event, reject_list);
361 if (not std::ranges::any_of(reject_list, [&](
hilet& x) {
384 widget_id current_keyboard_widget,
385 keyboard_focus_group group,
386 keyboard_focus_direction direction)
const noexcept
396 }
else if (current_keyboard_widget ==
id) {
401 for (
auto& child :
children(
false)) {
405 if (direction == keyboard_focus_direction::backward) {
409 for (
auto *child : children_) {
414 if (
auto tmp = child->find_next_widget({}, group, direction)) {
419 auto tmp = child->find_next_widget(current_keyboard_widget, group, direction);
420 if (tmp == current_keyboard_widget) {
425 }
else if (tmp !=
nullptr) {
437 return current_keyboard_widget;
443 [[nodiscard]] widget_id find_first_widget(keyboard_focus_group group)
const noexcept
447 for (
auto& child :
children(false)) {
448 if (child.accepts_keyboard_focus(group)) {
455 [[nodiscard]] widget_id find_last_widget(keyboard_focus_group group)
const noexcept
459 auto found = widget_id{};
460 for (
auto& child :
children(false)) {
461 if (child.accepts_keyboard_focus(group)) {
469 [[nodiscard]]
bool is_first(keyboard_focus_group group)
const noexcept
472 return parent->find_first_widget(group) ==
id;
475 [[nodiscard]]
bool is_last(keyboard_focus_group group)
const noexcept
478 return parent->find_last_widget(group) ==
id;
507 while (to_bool(w = w->parent)) {
525 template<forward_of<
void()> Callback>
526 [[nodiscard]] callback_token subscribe(Callback&& callback, callback_flags flags = callback_flags::synchronous)
const noexcept
528 return _state_changed.subscribe(std::forward<Callback>(callback), flags);
531 [[nodiscard]] awaiter_type
operator co_await()
const noexcept
533 return _state_changed.operator
co_await();
548 mutable notifier<void()> _state_changed;
559 [[nodiscard]] aarectanglei make_overlay_rectangle(aarectanglei requested_rectangle)
const noexcept
567 hilet response_window_rectangle =
fit(window_bounds, requested_window_rectangle);
568 return layout.
from_window * response_window_rectangle;
572 decltype(
mode)::callback_token _mode_cbt;
575inline widget *get_if(widget *start, widget_id
id,
bool include_invisible)
noexcept
579 if (start->id ==
id) {
582 for (
auto& child : start->children(include_invisible)) {
583 if (
hilet r = get_if(&child,
id, include_invisible); r !=
nullptr) {
590inline widget& get(widget& start, widget_id
id,
bool include_invisible)
592 if (
auto r = get_if(
std::addressof(start),
id, include_invisible); r !=
nullptr) {
595 throw not_found_error(
"get widget by id");
598template<std::invocable<w
idget&> F>
599inline void apply(widget& start,
bool include_invisible, F
const& f)
601 for (
auto& child : start.children(include_invisible)) {
602 apply(child, include_invisible, f);
607template<std::invocable<w
idget&> F>
608inline void apply(widget& start, F
const& f)
610 return apply(start,
true, f);
Definition of GUI event types.
#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 hi_axiom_not_null(expression,...)
Assert if an expression is not nullptr.
Definition assert.hpp:272
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ end
Start from the end of the file.
@ begin
Start from the beginning of the file.
gui_event_type
GUI event type.
Definition gui_event_type.hpp:21
@ window_redraw
Request that part of the window gets redrawn on the next frame.
@ window_reconstrain
Request that widget get constraint on the next frame.
@ rectangle
The gui_event has rectangle data.
@ disabled
The widget is disabled.
@ partial
A widget is partially enabled.
@ enabled
The widget is fully enabled.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
aarectangle fit(aarectangle const &bounds, aarectangle const &rectangle) noexcept
Make a rectangle fit inside bounds.
@ off
The widget in the off-state.
auto theme
A tagged global variable to a theme model for a widget's component.
Definition theme_model.hpp:578
A user interface event.
Definition gui_event.hpp:75
virtual void draw(widget_draw_context &context) noexcept
Draw the widget.
Definition widget.hpp:227
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:35
virtual void set_layout(widget_layout const &context) noexcept
Update the internal layout of the widget.
Definition widget.hpp:208
observer< extent2i > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:85
observer< bool > hover
Mouse cursor is hovering over the widget.
Definition widget.hpp:53
virtual void scroll_to_show(hi::aarectanglei rectangle) noexcept
Scroll to show the given rectangle on the window.
Definition widget.hpp:487
virtual void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
Definition widget.hpp:265
virtual bool handle_event_recursive(gui_event const &event, std::vector< widget_id > const &reject_list=std::vector< widget_id >{}) noexcept
Handle command recursive.
Definition widget.hpp:351
void reset_layout(gfx_surface *new_surface, float new_scale) noexcept
Reset the layout.
Definition widget.hpp:174
virtual hitbox hitbox_test_from_parent(point2i position, hitbox sibling_hitbox) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:157
virtual generator< widget const & > children(bool include_invisible) const noexcept
Get a list of child widgets.
Definition widget.hpp:115
virtual bool is_tab_button() const noexcept
Check if this widget is a tab-button.
Definition widget.hpp:520
virtual box_constraints update_constraints() noexcept
Update the constraints of the widget.
Definition widget.hpp:192
observer< bool > clicked
The widget is being clicked by the mouse.
Definition widget.hpp:57
gfx_surface * surface
The surface this widget is drawn on.
Definition widget.hpp:44
virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
Check if the widget will accept keyboard focus.
Definition widget.hpp:165
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
virtual widget_id find_next_widget(widget_id current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept
Find the next widget that handles keyboard focus.
Definition widget.hpp:383
std::vector< widget_id > parent_chain() const noexcept
Get a list of parents of a given widget.
Definition widget.hpp:499
observer< extent2i > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:89
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
observer< bool > focus
The widget has keyboard focus.
Definition widget.hpp:61
size_t semantic_layer
The draw layer of the widget.
Definition widget.hpp:81
virtual hitbox hitbox_test(point2i position) const noexcept
Find the widget that is under the mouse cursor.
Definition widget.hpp:134
virtual hitbox hitbox_test_from_parent(point2i position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:145
Draw context for drawing using the HikoGUI shaders.
Definition widget_draw_context.hpp:205
The layout of a widget.
Definition widget_layout.hpp:37
extent2i window_size
Size of the window.
Definition widget_layout.hpp:68
translate2i to_parent
This matrix transforms local coordinates to the coordinates of the parent widget.
Definition widget_layout.hpp:52
translate2i from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:56
translate2i from_window
This matrix transforms window coordinates to local coordinates.
Definition widget_layout.hpp:64
constexpr aarectanglei clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:145
2D constraints.
Definition box_constraints.hpp:22
Definition theme_model.hpp:279