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/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 { inline namespace v1 {
22
49template<axis Axis = axis::both>
50class scroll_widget : public widget {
51public:
52 using super = widget;
53 using horizontal_scroll_bar_type = scroll_bar_widget<axis::horizontal>;
54 using vertical_scroll_bar_type = scroll_bar_widget<axis::vertical>;
55
56 constexpr static hi::axis axis = Axis;
57
59
65 {
66 hi_axiom(loop::main().on_thread());
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->width() = 0;
76 } else {
77 horizontal_scroll_bar->set_mode(widget_mode::collapse);
78 }
79
80 if (to_bool(axis & axis::vertical)) {
81 minimum->height() = 0;
82 } else {
83 vertical_scroll_bar->set_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& emplace(Args&&...args) noexcept
106 {
107 return _aperture->emplace<Widget>(std::forward<Args>(args)...);
108 }
109
111 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) 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 (auto const& 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 = aarectangle{0, shape.y(), _layout.width(), shape.height()};
144 }
145 if (not _horizontal_scroll_bar->visible()) {
146 shape.rectangle = aarectangle{shape.x(), 0, shape.width(), _layout.height()};
147 }
148 }
149
150 cell.value->set_layout(context.transform(shape, transform_command::level));
151 }
152 }
153
154 void draw(draw_context const& context) noexcept override
155 {
156 if (mode() > widget_mode::invisible) {
157 for (auto const& cell : _grid) {
158 cell.value->draw(context);
159 }
160 }
161 }
162
163 [[nodiscard]] hitbox hitbox_test(point2 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.
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:24
@ partial
A widget is partially enabled.
@ collapse
The widget is collapsed.
@ invisible
The widget is invisible.
The HikoGUI namespace.
Definition array_generic.hpp:20
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.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition widget_intf.hpp:24
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:30
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:206
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:35
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:50
scroll_widget(widget_intf const *parent) noexcept
Constructs an empty scroll widget.
Definition scroll_widget.hpp:64
Widget & emplace(Args &&...args) noexcept
Add a content widget directly to this scroll widget.
Definition scroll_widget.hpp:105
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
observer< extent2 > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:41
virtual hitbox hitbox_test_from_parent(point2 position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:89
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:55
observer< extent2 > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:45
T max(T... args)
T move(T... args)