8#include "widget_layout.hpp"
9#include "widget_id.hpp"
10#include "widget_state.hpp"
11#include "keyboard_focus_group.hpp"
12#include "../layout/layout.hpp"
13#include "../GFX/GFX.hpp"
14#include "../telemetry/telemetry.hpp"
15#include "../theme/theme.hpp"
16#include "../macros.hpp"
19hi_export_module(hikogui.GUI : widget_intf);
21hi_export
namespace hi {
41 gui_window *window =
nullptr;
51 virtual ~widget_intf()
53 release_widget_id(
id);
59 if (path_has_changed) {
63 ++global_counter<
"widget:style:path">;
65 child.style.set_parent_path(
style.path());
72 ++global_counter<
"widget:style:reconstrain">;
78 ++global_counter<
"widget:style:redraw">;
85 _state_cbt =
state.subscribe([&](widget_state new_state) {
86 style.set_pseudo_class(new_state.pseudo_class());
88 static std::optional<widget_state> old_state = std::nullopt;
91 if (need_reconstrain(*old_state, *
state)) {
92 ++global_counter<
"widget:state:reconstrain">;
95 }
else if (need_relayout(*old_state, *
state)) {
96 ++global_counter<
"widget:state:relayout">;
99 }
else if (need_redraw(*old_state, *
state)) {
100 ++global_counter<
"widget:state:redraw">;
113 [[nodiscard]] widget_intf *
parent() const noexcept
127 template<forward_of<
void()> Func>
128 [[nodiscard]]
callback<void()>
subscribe(Func&& func, callback_flags flags = callback_flags::synchronous)
noexcept
135 [[nodiscard]]
auto operator co_await()
const noexcept
137 return notifier.operator
co_await();
140 [[nodiscard]]
size_t layer() const noexcept
142 return state->layer();
145 void set_layer(
size_t new_layer)
noexcept
147 state->set_layer(new_layer);
152 return state->mode();
157 state->set_mode(new_mode);
160 [[nodiscard]] widget_value value() const noexcept
162 return state->value();
165 void set_value(widget_value new_value)
noexcept
167 state->set_value(new_value);
170 [[nodiscard]] widget_phase phase() const noexcept
172 return state->phase();
175 void set_pressed(
bool pressed)
noexcept
177 state->set_pressed(pressed);
180 void set_hover(
bool hover)
noexcept
182 state->set_hover(hover);
185 void set_active(
bool active)
noexcept
187 state->set_active(active);
190 [[nodiscard]]
bool focus() const noexcept
192 return state->focus();
195 void set_focus(
bool new_focus)
noexcept
197 state->set_focus(new_focus);
202 [[nodiscard]]
virtual generator<widget_intf&>
children(
bool include_invisible =
true) noexcept = 0;
206 [[nodiscard]] virtual generator<widget_intf const&>
children(
bool include_invisible = true) const noexcept final
208 for (
auto& child :
const_cast<widget_intf *
>(
this)->
children(include_invisible)) {
269 [[nodiscard]]
virtual hitbox
hitbox_test(point2 position)
const noexcept = 0;
299 std::vector<widget_id> const& reject_list =
std::vector<widget_id>{})
noexcept = 0;
316 widget_id current_keyboard_widget,
317 keyboard_focus_group group,
318 keyboard_focus_direction direction)
const noexcept = 0;
329 while ((w = w->parent())) {
354 hi::style::callback_type _style_cbt;
364 hi_assert_not_null(start);
366 if (start->id ==
id) {
369 for (
auto& child : start->children(include_invisible)) {
370 if (
auto const r = get_if(&child,
id, include_invisible); r !=
nullptr) {
379 if (
auto r = get_if(
std::addressof(start),
id, include_invisible); r !=
nullptr) {
385template<std::invocable<w
idget_
intf&> Func>
386inline void apply(
widget_intf& start, Func &&func,
bool include_invisible =
true)
388 auto todo = std::vector<widget_intf *>{&start};
390 while (not todo.empty()) {
391 auto *tmp = todo.back();
396 for (
auto &child : tmp->children(include_invisible)) {
397 todo.push_back(&child);
402inline void apply_window_data(
widget_intf& start, gui_window *new_window,
unit::pixel_density const& new_density, style::attributes_from_theme_type
const& new_attributes_from_theme)
405 w.window = new_window;
406 w.style.set_pixel_density(new_density);
407 w.style.set_attributes_from_theme(new_attributes_from_theme);
413 _parent = new_parent;
416 new_parent ? new_parent->window :
nullptr,
418 new_parent ? new_parent->style.attributes_from_theme() : style::attributes_from_theme_type{});
421 style.set_parent_path(new_parent ? new_parent->style.path() :
style_path{});
@ window_relayout
Request that widgets get laid out on the next frame.
Definition gui_event_type.hpp:47
@ window_reconstrain
Request that widget get constraint on the next frame.
Definition gui_event_type.hpp:48
@ rectangle
The gui_event has rectangle data.
Definition gui_event_variant.hpp:44
widget_mode
The mode that the widget is operating at.
Definition widget_state.hpp:26
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
style_modify_mask
Definition style_modify_mask.hpp:16
@ layout
A layout (size, alignment) value was modified.
Definition style_modify_mask.hpp:55
@ redraw
Only visual changes.
Definition style_modify_mask.hpp:51
Definition callback.hpp:77
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:25
Draw context for drawing using the HikoGUI shaders.
Definition draw_context_intf.hpp:209
A user interface event.
Definition gui_event.hpp:82
Definition widget_intf.hpp:25
notifier< void()> notifier
Notifier which is called after an action is completed by a widget.
Definition widget_intf.hpp:45
std::vector< widget_id > parent_chain() const noexcept
Get a list of parents of a given widget.
Definition widget_intf.hpp:323
virtual bool handle_event_recursive(gui_event const &event, std::vector< widget_id > const &reject_list=std::vector< widget_id >{}) noexcept=0
Handle command recursive.
virtual void request_redraw() const noexcept=0
Request the widget to be redrawn on the next frame.
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:241
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget_intf.hpp:347
virtual generator< widget_intf & > children(bool include_invisible=true) noexcept=0
Get a list of child widgets.
widget_intf * parent() const noexcept
Pointer to the parent widget.
Definition widget_intf.hpp:113
virtual bool process_event(gui_event const &event) const noexcept=0
Send a event to the window.
virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept=0
Check if the widget will accept keyboard focus.
virtual box_constraints update_constraints() noexcept=0
Update the constraints of the widget.
virtual widget_id find_next_widget(widget_id current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept=0
Find the next widget that handles keyboard focus.
virtual void set_layout(widget_layout const &context) noexcept=0
Update the internal layout of the widget.
virtual void scroll_to_show(hi::aarectangle rectangle) noexcept=0
Scroll to show the given rectangle on the window.
virtual bool handle_event(gui_event const &event) noexcept=0
Handle command.
virtual hitbox hitbox_test(point2 position) const noexcept=0
Find the widget that is under the mouse cursor.
virtual void draw(draw_context const &context) noexcept=0
Draw the widget.
virtual void set_parent(widget_intf *new_parent) noexcept
Set the parent widget.
Definition widget_intf.hpp:411
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback to be called when an action is completed by the widget.
Definition widget_intf.hpp:128
hi::style style
The style of this widget.
Definition widget_intf.hpp:39
observer< widget_state > state
The current state of the widget.
Definition widget_intf.hpp:49
The layout of a widget.
Definition widget_layout.hpp:56
The state the widget is in.
Definition widget_state.hpp:105
2D constraints.
Definition box_constraints.hpp:25
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
callback_type subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Add a callback to the style.
Definition style.hpp:246
Definition style_path.hpp:33
Definition pixel_density.hpp:18
Exception thrown when an item was not found.
Definition exception_intf.hpp:149