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
8
9#pragma once
10
11#include "widget.hpp"
12#include "scroll_bar_widget.hpp"
14#include "../geometry/geometry.hpp"
15#include "../layout/layout.hpp"
16#include "../macros.hpp"
17#include <coroutine>
18
19hi_export_module(hikogui.widgets.scroll_widget);
20
21hi_export namespace hi {
22inline namespace v1 {
23
50template<axis Axis = axis::both>
51class scroll_widget : public widget {
52public:
53 using super = widget;
54 using horizontal_scroll_bar_type = scroll_bar_widget<axis::horizontal>;
55 using vertical_scroll_bar_type = scroll_bar_widget<axis::vertical>;
56
57 constexpr static hi::axis axis = Axis;
58
60
65 scroll_widget() noexcept : super()
66 {
67 hi_axiom(loop::main().on_thread());
68
69 auto aperture = std::make_unique<scroll_aperture_widget>();
70 aperture->set_parent(this);
71
72 auto horizontal_scroll_bar =
73 std::make_unique<horizontal_scroll_bar_type>(aperture->content_width, aperture->aperture_width, aperture->offset_x);
74 horizontal_scroll_bar->set_parent(this);
75
76 auto vertical_scroll_bar =
77 std::make_unique<vertical_scroll_bar_type>(aperture->content_height, aperture->aperture_height, aperture->offset_y);
78 vertical_scroll_bar->set_parent(this);
79
80 if (to_bool(axis & axis::horizontal)) {
81 minimum->width() = 0;
82 } else {
83 horizontal_scroll_bar->set_mode(widget_mode::collapse);
84 }
85
86 if (to_bool(axis & axis::vertical)) {
87 minimum->height() = 0;
88 } else {
89 vertical_scroll_bar->set_mode(widget_mode::collapse);
90 }
91
92 _aperture = aperture.get();
93 _horizontal_scroll_bar = horizontal_scroll_bar.get();
94 _vertical_scroll_bar = vertical_scroll_bar.get();
95
96 _grid.add_cell(0, 0, std::move(aperture));
97 _grid.add_cell(1, 0, std::move(vertical_scroll_bar));
98 _grid.add_cell(0, 1, std::move(horizontal_scroll_bar));
99 }
100
110 template<typename Widget, typename... Args>
111 Widget& emplace(Args&&... args) noexcept
112 {
113 return _aperture->emplace<Widget>(std::forward<Args>(args)...);
114 }
115
117 [[nodiscard]] generator<widget_intf&> children(bool include_invisible) noexcept override
118 {
119 co_yield *_aperture;
120 co_yield *_vertical_scroll_bar;
121 co_yield *_horizontal_scroll_bar;
122 }
123
124 [[nodiscard]] box_constraints update_constraints() noexcept override
125 {
126 _layout = {};
127
128 for (auto& cell : _grid) {
129 cell.set_constraints(cell.value->update_constraints());
130 }
131 auto grid_constraints = _grid.constraints(os_settings::left_to_right());
132 return grid_constraints.constrain(*minimum, *maximum);
133 }
134
135 void set_layout(widget_layout const& context) noexcept override
136 {
137 if (compare_store(_layout, context)) {
138 _grid.set_layout(context.shape, theme().baseline_adjustment());
139 }
140
141 for (auto const& cell : _grid) {
142 auto shape = cell.shape;
143
144 if (cell.value.get() == _aperture) {
145 // This is the content. Move the content slightly when the scroll-bars aren't visible.
146 // The grid cells are always ordered in row-major.
147 // This the vertical scroll bar is _grid[1] and the horizontal scroll bar is _grid[2].
148 if (not _vertical_scroll_bar->visible()) {
149 shape.rectangle = aarectangle{0, shape.y(), _layout.width(), shape.height()};
150 }
151 if (not _horizontal_scroll_bar->visible()) {
152 shape.rectangle = aarectangle{shape.x(), 0, shape.width(), _layout.height()};
153 }
154 }
155
156 cell.value->set_layout(context.transform(shape, transform_command::level));
157 }
158 }
159
160 void draw(draw_context const& context) noexcept override
161 {
162 if (mode() > widget_mode::invisible) {
163 for (auto const& cell : _grid) {
164 cell.value->draw(context);
165 }
166 }
167 }
168
169 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
170 {
171 hi_axiom(loop::main().on_thread());
172
173 if (mode() >= widget_mode::partial) {
174 auto r = _aperture->hitbox_test_from_parent(position);
175 r = _horizontal_scroll_bar->hitbox_test_from_parent(position, r);
176 r = _vertical_scroll_bar->hitbox_test_from_parent(position, r);
177
178 if (layout().contains(position)) {
179 r = std::max(r, hitbox{id, _layout.elevation});
180 }
181 return r;
182
183 } else {
184 return {};
185 }
186 }
187 // @endprivatesection
188private:
189 grid_layout<std::unique_ptr<widget>> _grid;
190
191 scroll_aperture_widget* _aperture;
192 horizontal_scroll_bar_type* _horizontal_scroll_bar;
193 vertical_scroll_bar_type* _vertical_scroll_bar;
194};
195
204
213
214} // namespace v1
215} // namespace hi::v1
Defines widget.
Defines scroll_aperture_widget.
Defines scroll_bar_widget.
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:24
@ partial
A widget is partially enabled.
Definition widget_state.hpp:73
@ collapse
The widget is collapsed.
Definition widget_state.hpp:34
@ invisible
The widget is invisible.
Definition widget_state.hpp:41
scroll_widget< axis::horizontal > horizontal_scroll_widget
Horizontal scroll widget.
Definition scroll_widget.hpp:212
scroll_widget< axis::vertical > vertical_scroll_widget
Vertical scroll widget.
Definition scroll_widget.hpp:203
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
@ level
The child widget stays at the same elevation and layer.
Definition widget_layout.hpp:26
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:31
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:241
2D constraints.
Definition box_constraints.hpp:25
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:38
The scroll widget allows a content widget to be shown in less space than is required.
Definition scroll_widget.hpp:51
scroll_widget() noexcept
Constructs an empty scroll widget.
Definition scroll_widget.hpp:65
Widget & emplace(Args &&... args) noexcept
Add a content widget directly to this scroll widget.
Definition scroll_widget.hpp:111
observer< extent2 > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:42
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:50
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:110
observer< extent2 > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:46
T forward(T... args)
T max(T... args)
T move(T... args)