HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
scroll_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-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 "scroll_bar_widget.hpp"
14#include "../geometry/module.hpp"
15#include "../layout/grid_layout.hpp"
16
17namespace hi { inline namespace v1 {
18
45template<axis Axis = axis::both, fixed_string Name = "">
46class scroll_widget final : public widget {
47public:
48 using super = widget;
49 constexpr static auto prefix = Name / "scroll";
50
51 using aperture_type = scroll_aperture_widget<prefix>;
52 using horizontal_bar_type = scroll_bar_widget<axis::horizontal, prefix>;
53 using vertical_bar_type = scroll_bar_widget<axis::vertical, prefix>;
54
55 static constexpr hi::axis axis = Axis;
56
58
64 {
65 hi_axiom(loop::main().on_thread());
67
68 // The scroll-widget will not draw itself, only its selected content.
70
71 auto aperture = std::make_unique<aperture_type>(this);
72 auto horizontal_bar = std::make_unique<horizontal_bar_type>(
73 this, aperture->content_width, aperture->aperture_width, aperture->offset_x);
74 auto vertical_bar = std::make_unique<vertical_bar_type>(
75 this, aperture->content_height, aperture->aperture_height, aperture->offset_y);
76
77 if (to_bool(axis & axis::horizontal)) {
78 minimum.copy()->width() = 0;
79 } else {
80 horizontal_bar->mode = widget_mode::collapse;
81 }
82
83 if (to_bool(axis & axis::vertical)) {
84 minimum.copy()->height() = 0;
85 } else {
86 vertical_bar->mode = widget_mode::collapse;
87 }
88
89 _aperture = aperture.get();
90 _horizontal_bar = horizontal_bar.get();
91 _vertical_bar = vertical_bar.get();
92
93 _grid.add_cell(0, 0, std::move(aperture));
94 _grid.add_cell(1, 0, std::move(vertical_bar));
95 _grid.add_cell(0, 1, std::move(horizontal_bar));
96 }
97
107 template<typename Widget, typename... Args>
108 Widget& make_widget(Args&&...args) noexcept
109 {
110 return _aperture->make_widget<Widget>(std::forward<Args>(args)...);
111 }
112
114 [[nodiscard]] generator<widget const&> children(bool include_invisible) const noexcept override
115 {
116 co_yield *_aperture;
117 co_yield *_vertical_bar;
118 co_yield *_horizontal_bar;
119 }
120
121 [[nodiscard]] box_constraints update_constraints() noexcept override
122 {
123 for (auto& cell : _grid) {
124 cell.set_constraints(cell.value->update_constraints());
125 }
126 auto grid_constraints = _grid.constraints(os_settings::left_to_right());
127 return grid_constraints.constrain(*minimum, *maximum);
128 }
129
130 void set_layout(widget_layout const& context) noexcept override
131 {
132 if (compare_store(layout, context)) {
133 _grid.set_layout(context.shape, theme<prefix>.cap_height(this));
134 }
135
136 for (hilet& cell : _grid) {
137 auto shape = cell.shape;
138
139 if (cell.value.get() == _aperture) {
140 // This is the content. Move the content slightly when the scroll-bars aren't visible.
141 // The grid cells are always ordered in row-major.
142 // This the vertical scroll bar is _grid[1] and the horizontal scroll bar is _grid[2].
143 if (not _vertical_bar->visible()) {
144 shape.rectangle = aarectanglei{0, shape.y(), layout.width(), shape.height()};
145 }
146 if (not _horizontal_bar->visible()) {
147 shape.rectangle = aarectanglei{shape.x(), 0, shape.width(), layout.height()};
148 }
149 }
150
151 cell.value->set_layout(context.transform(shape, 0.0f));
152 }
153 }
154
155 void draw(widget_draw_context const& context) noexcept override
156 {
158 for (hilet& cell : _grid) {
159 cell.value->draw(context);
160 }
161 }
162 }
163
164 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override
165 {
166 hi_axiom(loop::main().on_thread());
167
168 if (*mode >= widget_mode::partial) {
169 auto r = _aperture->hitbox_test_from_parent(position);
170 r = _horizontal_bar->hitbox_test_from_parent(position, r);
171 r = _vertical_bar->hitbox_test_from_parent(position, r);
172
173 if (layout.contains(position)) {
174 r = std::max(r, hitbox{id, layout.elevation});
175 }
176 return r;
177
178 } else {
179 return {};
180 }
181 }
182 // @endprivatesection
183private:
184 grid_layout<std::unique_ptr<widget>> _grid;
185
186 aperture_type *_aperture;
187 horizontal_bar_type *_horizontal_bar;
188 vertical_bar_type *_vertical_bar;
189};
190
198template<fixed_string Name = "">
200
208template<fixed_string Name = "">
210
211}} // namespace hi::v1
Defines scroll_bar_widget.
Defines scroll_aperture_widget.
#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
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:18
@ partial
A widget is partially enabled.
@ collapse
The widget is collapsed.
@ 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_id id
The numeric identifier of a widget.
Definition widget.hpp:35
observer< extent2i > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:89
observer< extent2i > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:93
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
virtual hitbox hitbox_test_from_parent(point2i position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:149
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 scroll aperture widget.
Definition scroll_aperture_widget.hpp:23
Scroll bar widget This widget is used in a pair of a vertical and horizontal scrollbar as a child of ...
Definition scroll_bar_widget.hpp:33
The scroll widget allows a content widget to be shown in less space than is required.
Definition scroll_widget.hpp:46
Widget & make_widget(Args &&...args) noexcept
Add a content widget directly to this scroll widget.
Definition scroll_widget.hpp:108
scroll_widget(widget *parent) noexcept
Constructs an empty scroll widget.
Definition scroll_widget.hpp:63
T max(T... args)
T move(T... args)