HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "widget_layout.hpp"
12#include "widget_mode.hpp"
13#include "../GFX/draw_context.hpp"
14#include "../GUI/theme.hpp"
15#include "../GUI/hitbox.hpp"
16#include "../GUI/keyboard_focus_direction.hpp"
17#include "../GUI/keyboard_focus_group.hpp"
18#include "../GUI/gui_event.hpp"
19#include "../layout/box_constraints.hpp"
22#include "../geometry/transform.hpp"
23#include "../observer.hpp"
24#include "../chrono.hpp"
25#include "../generator.hpp"
26#include "../cache.hpp"
27#include "../os_settings.hpp"
28#include <memory>
29#include <vector>
30#include <string>
31#include <ranges>
32
33namespace hi { inline namespace v1 {
34class gui_window;
35class gfx_surface;
36
46class widget : public std::enable_shared_from_this<widget> {
47public:
51 widget *parent = nullptr;
52
57 uint32_t id = 0;
58
62 observer<widget_mode> mode = widget_mode::enabled;
63
66 observer<bool> hover = false;
67
70 observer<bool> focus = false;
71
87
96
99 observer<extent2i> minimum = extent2i{};
100
103 observer<extent2i> maximum = extent2i::large();
104
107 widget(widget *parent) noexcept;
108
109 virtual ~widget();
110 widget(const widget&) = delete;
111 widget& operator=(const widget&) = delete;
112 widget(widget&&) = delete;
113 widget& operator=(widget&&) = delete;
114
122 [[nodiscard]] virtual hitbox hitbox_test(point2i position) const noexcept
123 {
124 return {};
125 }
126
133 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point2i position) const noexcept
134 {
135 return hitbox_test(_layout.from_parent * position);
136 }
137
145 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point2i position, hitbox sibling_hitbox) const noexcept
146 {
147 return std::max(sibling_hitbox, hitbox_test(_layout.from_parent * position));
148 }
149
153 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
154 {
155 hi_axiom(loop::main().on_thread());
156 return false;
157 }
158
170 virtual [[nodiscard]] box_constraints update_constraints() noexcept
171 {
172 _layout = {};
173 return {*minimum, *minimum, *maximum};
174 }
175
187 virtual void set_layout(widget_layout const& context) noexcept
188 {
189 _layout = context;
190 }
191
194 widget_layout const& layout() const noexcept
195 {
196 return _layout;
197 }
198
213 virtual void draw(draw_context const& context) noexcept {}
214
215 virtual bool process_event(gui_event const& event) const noexcept
216 {
217 if (parent != nullptr) {
218 return parent->process_event(event);
219 } else {
220 return true;
221 }
222 }
223
226 void request_redraw() const noexcept
227 {
229 }
230
235 virtual bool handle_event(gui_event const& event) noexcept;
236
245 gui_event const& event,
246 std::vector<widget const *> const& reject_list = std::vector<widget const *>{}) noexcept;
247
261 [[nodiscard]] virtual widget const *find_next_widget(
262 widget const *current_keyboard_widget,
263 keyboard_focus_group group,
264 keyboard_focus_direction direction) const noexcept;
265
266 [[nodiscard]] widget const *find_first_widget(keyboard_focus_group group) const noexcept;
267
268 [[nodiscard]] widget const *find_last_widget(keyboard_focus_group group) const noexcept;
269
272 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
273
276 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
277
284 virtual void scroll_to_show(hi::aarectanglei rectangle) noexcept;
285
288 void scroll_to_show() noexcept
289 {
291 }
292
296 [[nodiscard]] std::vector<widget const *> parent_chain() const noexcept;
297
298 [[nodiscard]] virtual gui_window *window() const noexcept
299 {
300 if (parent) {
301 return parent->window();
302 } else {
303 return nullptr;
304 }
305 }
306
307 [[nodiscard]] virtual hi::theme const& theme() const noexcept
308 {
310 return parent->theme();
311 }
312
313 [[nodiscard]] virtual gfx_surface const *surface() const noexcept
314 {
315 if (parent) {
316 return parent->surface();
317 } else {
318 return nullptr;
319 }
320 }
321
322 [[nodiscard]] virtual color background_color() const noexcept;
323
324 [[nodiscard]] virtual color foreground_color() const noexcept;
325
326 [[nodiscard]] virtual color focus_color() const noexcept;
327
328 [[nodiscard]] virtual color accent_color() const noexcept;
329
330 [[nodiscard]] virtual color label_color() const noexcept;
331
332protected:
333 widget_layout _layout;
334
335 decltype(mode)::callback_token _mode_cbt;
336
337 [[nodiscard]] virtual generator<widget *> children() const noexcept
338 {
339 co_return;
340 }
341
351 [[nodiscard]] aarectanglei make_overlay_rectangle(aarectanglei requested_rectangle) const noexcept;
352};
353}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:118
Defined the geo::extent, extent2 and extent3 types.
Defines widget_layout.
Defines widget_mode.
Definition of GUI event types.
@ window_redraw
Request that part of the window gets redrawn on the next frame.
@ rectangle
The gui_event has rectangle data.
@ enabled
The widget is fully enabled.
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
A user interface event.
Definition gui_event.hpp:77
2D constraints.
Definition box_constraints.hpp:25
An interactive graphical object as part of the user-interface.
Definition widget.hpp:46
virtual void draw(draw_context const &context) noexcept
Draw the widget.
Definition widget.hpp:213
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:194
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
virtual widget const * find_next_widget(widget const *current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept
Find the next widget that handles keyboard focus.
int logical_layer
The logical layer of the widget.
Definition widget.hpp:95
virtual void set_layout(widget_layout const &context) noexcept
Update the internal layout of the widget.
Definition widget.hpp:187
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:86
observer< extent2i > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:99
observer< bool > hover
Mouse cursor is hovering over the widget.
Definition widget.hpp:66
virtual void scroll_to_show(hi::aarectanglei rectangle) noexcept
Scroll to show the given rectangle on the window.
virtual bool handle_event_recursive(gui_event const &event, std::vector< widget const * > const &reject_list=std::vector< widget const * >{}) noexcept
Handle command recursive.
widget(widget *parent) noexcept
virtual hitbox hitbox_test_from_parent(point2i position, hitbox sibling_hitbox) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:145
virtual box_constraints update_constraints() noexcept
Update the constraints of the widget.
Definition widget.hpp:170
void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
Definition widget.hpp:226
std::vector< widget const * > parent_chain() const noexcept
Get a list of parents of a given widget.
virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
Check if the widget will accept keyboard focus.
Definition widget.hpp:153
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
observer< extent2i > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:103
widget * parent
Pointer to the parent widget.
Definition widget.hpp:51
bool is_last(keyboard_focus_group group) const noexcept
Is this widget the last widget in the parent container.
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget.hpp:288
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:62
observer< bool > focus
The widget has keyboard focus.
Definition widget.hpp:70
virtual hitbox hitbox_test(point2i position) const noexcept
Find the widget that is under the mouse cursor.
Definition widget.hpp:122
virtual hitbox hitbox_test_from_parent(point2i position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:133
The layout of a widget.
Definition widget_layout.hpp:41
translate2i from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:60
constexpr aarectanglei clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:149
T max(T... args)