HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
row_column_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-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 "../GUI/module.hpp"
12#include "../geometry/module.hpp"
13#include "../layout/row_column_layout.hpp"
14#include <memory>
15#include <type_traits>
16
17namespace hi { inline namespace v1 {
18
38template<fixed_string Prefix, axis Axis>
39class row_column_widget final : public widget {
40public:
41 static_assert(Axis == axis::horizontal or Axis == axis::vertical);
42
43 using super = widget;
44 constexpr static hi::axis axis = Axis;
45 constexpr static auto prefix = Prefix;
46
48
54 {
55 hi_axiom(loop::main().on_thread());
56
57 if (parent) {
59 }
60 }
61
74 template<typename Widget, typename... Args>
75 Widget& make_widget(Args&&...args)
76 {
77 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
78 auto& ref = *tmp;
79 _children.push_back(std::move(tmp));
80
81 ++global_counter<"row_column_widget:make_widget:constrain">;
83 return ref;
84 }
85
88 void clear() noexcept
89 {
90 hi_axiom(loop::main().on_thread());
91 _children.clear();
92 ++global_counter<"row_column_widget:clear:constrain">;
94 }
95
97 [[nodiscard]] generator<widget const&> children(bool include_invisible) const noexcept override
98 {
99 for (hilet& child : _children) {
100 co_yield *child.value;
101 }
102 }
103
104 [[nodiscard]] box_constraints update_constraints() noexcept override
105 {
106 for (auto& child : _children) {
107 child.set_constraints(child.value->update_constraints());
108 }
109
110 return _children.constraints(os_settings::left_to_right());
111 }
112
113 void set_layout(widget_layout const& context) noexcept override
114 {
115 if (compare_store(layout, context)) {
116 _children.set_layout(context.shape, theme<prefix>.cap_height(this));
117
118 for (hilet& child : _children) {
119 child.value->set_layout(context.transform(child.shape, 0.0f));
120 }
121 }
122 }
123
124 void draw(widget_draw_context const& context) noexcept override
125 {
127 for (hilet& child : _children) {
128 child.value->draw(context);
129 }
130 }
131 }
132
133 hitbox hitbox_test(point2i position) const noexcept override
134 {
135 hi_axiom(loop::main().on_thread());
136
137 if (*mode >= widget_mode::partial) {
138 auto r = hitbox{};
139 for (hilet& child : _children) {
140 r = child.value->hitbox_test_from_parent(position, r);
141 }
142 return r;
143 } else {
144 return {};
145 }
146 }
148private:
149 row_column_layout<Axis, std::unique_ptr<widget>> _children;
150};
151
155template<fixed_string Name = "">
156using row_widget = row_column_widget<join_path(Name, "row"), axis::x>;
157
161template<fixed_string Name = "">
162using column_widget = row_column_widget<join_path(Name, "column"), axis::y>;
163
164}} // namespace hi::v1
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:18
@ window_reconstrain
Request that widget get constraint on the next frame.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
Definition widget.hpp:26
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:53
size_t semantic_layer
The draw layer of the widget.
Definition widget.hpp:85
A row/column widget lays out child widgets along a row or column.
Definition row_column_widget.hpp:39
void clear() noexcept
Remove and deallocate all child widgets.
Definition row_column_widget.hpp:88
Widget & make_widget(Args &&...args)
Add a widget directly to this grid-widget.
Definition row_column_widget.hpp:75
row_column_widget(widget *parent) noexcept
Constructs an empty row/column widget.
Definition row_column_widget.hpp:53
T move(T... args)