12#include "../macros.hpp"
14namespace hi {
inline namespace v1 {
27 observer<float> content_width;
28 observer<float> content_height;
29 observer<float> aperture_width;
30 observer<float> aperture_height;
31 observer<float> offset_x;
32 observer<float> offset_y;
36 hi_axiom(loop::main().on_thread());
37 hi_assert_not_null(
parent);
42 _content_width_cbt = content_width.subscribe([&](
auto...) {
43 ++global_counter<
"scroll_aperture_widget:content_width:relayout">;
46 _content_height_cbt = content_height.subscribe([&](
auto...) {
47 ++global_counter<
"scroll_aperture_widget:content_height:relayout">;
50 _aperture_width_cbt = aperture_width.subscribe([&](
auto...) {
51 ++global_counter<
"scroll_aperture_widget:aperture_width:relayout">;
54 _aperture_height_cbt = aperture_height.subscribe([&](
auto...) {
55 ++global_counter<
"scroll_aperture_widget:aperture_height:relayout">;
58 _offset_x_cbt = offset_x.subscribe([&](
auto...) {
59 ++global_counter<
"scroll_aperture_widget:offset_x:relayout">;
62 _offset_y_cbt = offset_y.subscribe([&](
auto...) {
63 ++global_counter<
"scroll_aperture_widget:offset_y:relayout">;
66 _minimum_cbt =
minimum.subscribe([&](
auto...) {
67 ++global_counter<
"scroll_aperture_widget:minimum:reconstrain">;
72 template<
typename Widget,
typename... Args>
73 Widget& make_widget(Args&&...args)
noexcept
75 hi_axiom(loop::main().on_thread());
76 hi_axiom(_content ==
nullptr);
84 [[nodiscard]]
bool x_axis_scrolls()
const noexcept
86 return *content_width > *aperture_width;
89 [[nodiscard]]
bool y_axis_scrolls()
const noexcept
91 return *content_height > *aperture_height;
95 [[nodiscard]] generator<widget_intf &>
children(
bool include_invisible)
noexcept override
103 _content_constraints = _content->update_constraints();
106 auto aperture_constraints = _content_constraints;
107 aperture_constraints.minimum =
extent2{0, 0};
109 return aperture_constraints.internalize_margins().constrain(*
minimum, *
maximum);
115 aperture_width = context.width() - _content_constraints.margins.left() - _content_constraints.margins.right();
116 aperture_height = context.height() - _content_constraints.margins.bottom() - _content_constraints.margins.top();
120 content_width = *aperture_width < _content_constraints.preferred.
width() ? _content_constraints.preferred.
width() :
122 content_height = *aperture_height < _content_constraints.preferred.
height() ?
123 _content_constraints.preferred.
height() :
128 hilet offset_x_max =
std::max(*content_width - *aperture_width, 0.0f);
129 hilet offset_y_max =
std::max(*content_height - *aperture_height, 0.0f);
130 offset_x = std::clamp(*offset_x, 0.0f, offset_x_max);
131 offset_y = std::clamp(*offset_y, 0.0f, offset_y_max);
136 _content_constraints,
138 -*offset_x + _content_constraints.margins.left(),
139 -*offset_y + _content_constraints.margins.bottom(),
142 theme().baseline_adjustment()};
146 _content->set_layout(context.
transform(_content_shape, 1.0f, context.rectangle()));
152 _content->draw(context);
156 [[nodiscard]] hitbox
hitbox_test(point2 position)
const noexcept override
158 hi_axiom(loop::main().on_thread());
161 auto r = _content->hitbox_test_from_parent(position);
163 if (
layout().contains(position)) {
164 r =
std::max(r, hitbox{
id, _layout.elevation});
175 hi_axiom(loop::main().on_thread());
177 if (event == gui_event_type::mouse_wheel) {
180 hilet max_offset_x =
std::max(0.0f, *content_width - *aperture_width);
181 hilet max_offset_y =
std::max(0.0f, *content_height - *aperture_height);
183 offset_x = std::clamp(new_offset_x, 0.0f, max_offset_x);
184 offset_y = std::clamp(new_offset_y, 0.0f, max_offset_y);
185 ++global_counter<
"scroll_aperture_widget:mouse_wheel:relayout">;
196 auto safe_rectangle = intersect(_layout.rectangle(), _layout.clipping_rectangle);
200 if (safe_rectangle.width() > theme().margin<float>() * 2.0f and safe_rectangle.height() > theme().margin<float>() * 2.0f) {
204 safe_rectangle = safe_rectangle - theme().margin<
float>();
206 if (to_show.right() > safe_rectangle.right()) {
207 delta_x = to_show.right() - safe_rectangle.right();
208 }
else if (to_show.left() < safe_rectangle.left()) {
209 delta_x = to_show.left() - safe_rectangle.left();
212 if (to_show.top() > safe_rectangle.top()) {
213 delta_y = to_show.top() - safe_rectangle.top();
214 }
else if (to_show.bottom() < safe_rectangle.bottom()) {
215 delta_y = to_show.bottom() - safe_rectangle.bottom();
225 parent->scroll_to_show(_layout.to_parent *
translate2(delta_x, delta_y) * to_show);
237 decltype(content_width)::callback_token _content_width_cbt;
238 decltype(content_height)::callback_token _content_height_cbt;
239 decltype(aperture_width)::callback_token _aperture_width_cbt;
240 decltype(aperture_height)::callback_token _aperture_height_cbt;
241 decltype(offset_x)::callback_token _offset_x_cbt;
242 decltype(offset_y)::callback_token _offset_y_cbt;
243 decltype(
minimum)::callback_token _minimum_cbt;
@ window_relayout
Request that widgets get laid out on the next frame.
Definition gui_event_type.hpp:47
@ window_reconstrain
Request that widget get constraint on the next frame.
Definition gui_event_type.hpp:48
@ partial
A widget is partially enabled.
Definition widget_mode.hpp:68
@ invisible
The widget is invisible.
Definition widget_mode.hpp:36
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:56
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:29
A high-level geometric extent.
Definition extent2.hpp:29
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:104
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:115
Definition translate2.hpp:14
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:57
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector2.hpp:65
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:208
vector2 wheel_delta
Change in wheel rotation, in points (pt).
Definition gui_event.hpp:51
A user interface event.
Definition gui_event.hpp:75
mouse_event_data & mouse() noexcept
Get the mouse event information.
Definition gui_event.hpp:257
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:23
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
The layout of a widget.
Definition widget_layout.hpp:38
constexpr widget_layout transform(box_shape const &child_shape, float child_elevation, aarectangle new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:206
2D constraints.
Definition box_constraints.hpp:25
Definition box_shape.hpp:18
A scroll aperture widget.
Definition scroll_aperture_widget.hpp:23
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:169
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:66
observer< extent2 > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:79
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget_intf.hpp:196
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:164
widget(widget *parent) noexcept
Definition widget.hpp:87
generator< widget_intf & > children(bool include_invisible) noexcept override
Get a list of child widgets.
Definition widget.hpp:109
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition widget.hpp:174
hitbox hitbox_test(point2 position) const noexcept override
Find the widget that is under the mouse cursor.
Definition widget.hpp:121
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:158
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:178
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:198
observer< extent2 > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:83