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 "widget.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>
46class scroll_widget final : public widget {
47public:
48 using super = widget;
49 using horizontal_scroll_bar_type = scroll_bar_widget<axis::horizontal>;
50 using vertical_scroll_bar_type = scroll_bar_widget<axis::vertical>;
51
52 static constexpr hi::axis axis = Axis;
53
55
61 {
62 hi_axiom(loop::main().on_thread());
64
65 // The scroll-widget will not draw itself, only its selected content.
67
68 auto aperture = std::make_unique<scroll_aperture_widget>(this);
69 auto horizontal_scroll_bar = std::make_unique<horizontal_scroll_bar_type>(
70 this, aperture->content_width, aperture->aperture_width, aperture->offset_x);
71 auto vertical_scroll_bar = std::make_unique<vertical_scroll_bar_type>(
72 this, aperture->content_height, aperture->aperture_height, aperture->offset_y);
73
74 if (to_bool(axis & axis::horizontal)) {
75 minimum.copy()->width() = 0;
76 } else {
77 horizontal_scroll_bar->mode = widget_mode::collapse;
78 }
79
80 if (to_bool(axis & axis::vertical)) {
81 minimum.copy()->height() = 0;
82 } else {
83 vertical_scroll_bar->mode = widget_mode::collapse;
84 }
85
86 _aperture = aperture.get();
87 _horizontal_scroll_bar = horizontal_scroll_bar.get();
88 _vertical_scroll_bar = vertical_scroll_bar.get();
89
90 _grid.add_cell(0, 0, std::move(aperture));
91 _grid.add_cell(1, 0, std::move(vertical_scroll_bar));
92 _grid.add_cell(0, 1, std::move(horizontal_scroll_bar));
93 }
94
104 template<typename Widget, typename... Args>
105 Widget& make_widget(Args&&...args) noexcept
106 {
107 return _aperture->make_widget<Widget>(std::forward<Args>(args)...);
108 }
109
111 [[nodiscard]] generator<widget const &> children(bool include_invisible) const noexcept override
112 {
113 co_yield *_aperture;
114 co_yield *_vertical_scroll_bar;
115 co_yield *_horizontal_scroll_bar;
116 }
117
118 [[nodiscard]] box_constraints update_constraints() noexcept override
119 {
120 _layout = {};
121
122 for (auto& cell : _grid) {
123 cell.set_constraints(cell.value->update_constraints());
124 }
125 auto grid_constraints = _grid.constraints(os_settings::left_to_right());
126 return grid_constraints.constrain(*minimum, *maximum);
127 }
128
129 void set_layout(widget_layout const& context) noexcept override
130 {
131 if (compare_store(_layout, context)) {
132 _grid.set_layout(context.shape, theme().baseline_adjustment());
133 }
134
135 for (hilet& cell : _grid) {
136 auto shape = cell.shape;
137
138 if (cell.value.get() == _aperture) {
139 // This is the content. Move the content slightly when the scroll-bars aren't visible.
140 // The grid cells are always ordered in row-major.
141 // This the vertical scroll bar is _grid[1] and the horizontal scroll bar is _grid[2].
142 if (not _vertical_scroll_bar->visible()) {
143 shape.rectangle = aarectanglei{0, shape.y(), _layout.width(), shape.height()};
144 }
145 if (not _horizontal_scroll_bar->visible()) {
146 shape.rectangle = aarectanglei{shape.x(), 0, shape.width(), _layout.height()};
147 }
148 }
149
150 cell.value->set_layout(context.transform(shape, 0.0f));
151 }
152 }
153
154 void draw(draw_context const& context) noexcept override
155 {
157 for (hilet& cell : _grid) {
158 cell.value->draw(context);
159 }
160 }
161 }
162
163 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override
164 {
165 hi_axiom(loop::main().on_thread());
166
167 if (*mode >= widget_mode::partial) {
168 auto r = _aperture->hitbox_test_from_parent(position);
169 r = _horizontal_scroll_bar->hitbox_test_from_parent(position, r);
170 r = _vertical_scroll_bar->hitbox_test_from_parent(position, r);
171
172 if (layout().contains(position)) {
173 r = std::max(r, hitbox{id, _layout.elevation});
174 }
175 return r;
176
177 } else {
178 return {};
179 }
180 }
181 // @endprivatesection
182private:
183 grid_layout<std::unique_ptr<widget>> _grid;
184
185 scroll_aperture_widget *_aperture;
186 horizontal_scroll_bar_type *_horizontal_scroll_bar;
187 vertical_scroll_bar_type *_vertical_scroll_bar;
188};
189
198
207
208}} // namespace hi::v1
Defines widget.
Defines scroll_bar_widget.
Defines scroll_aperture_widget.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
#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
2D constraints.
Definition box_constraints.hpp:22
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:34
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:105
scroll_widget(widget *parent) noexcept
Constructs an empty scroll widget.
Definition scroll_widget.hpp:60
An interactive graphical object as part of the user-interface.
Definition widget.hpp:46
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:208
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:57
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
widget(widget *parent) noexcept
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
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:62
virtual hitbox hitbox_test_from_parent(point2i position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:147
T max(T... args)
T move(T... args)