AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AScrollArea.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14
15#include "AViewContainer.h"
16#include "AScrollAreaViewport.h"
17#include "AScrollbar.h"
18#include "glm/fwd.hpp"
19
20
37class API_AUI_VIEWS AScrollArea: public AViewContainerBase {
38public:
39 class Builder;
40
41public:
43
44 void setSize(glm::ivec2 size) override;
45 void setContents(_<AView> content) {
46 mInner->setContents(std::move(content));
47 }
48
49 int getContentMinimumWidth() override;
50 int getContentMinimumHeight() override;
51
52 void onPointerPressed(const APointerPressedEvent& event) override;
53 void onPointerReleased(const APointerReleasedEvent& event) override;
54
55 void setScroll(glm::uvec2 scroll) {
56 setScrollX(scroll.x);
57 setScrollY(scroll.y);
58 }
59
60 void setScrollX(unsigned scroll) {
61 AUI_NULLSAFE(mHorizontalScrollbar)->setScroll(int(scroll));
62 else mInner->setScrollX(scroll);
63 }
64
65 void setScrollY(unsigned scroll) {
66 AUI_NULLSAFE(mVerticalScrollbar)->setScroll(int(scroll));
67 else mInner->setScrollY(scroll);
68 }
69
74 void setStickToEnd(bool stickToEnd) {
75 AUI_NULLSAFE(mHorizontalScrollbar)->setStickToEnd(stickToEnd);
76 AUI_NULLSAFE(mVerticalScrollbar)->setStickToEnd(stickToEnd);
77 }
78
79 void scroll(glm::ivec2 by) noexcept {
80 scroll(by.x, by.y);
81 }
82
83 void scroll(int deltaByX, int deltaByY) noexcept {
84 AUI_NULLSAFE(mHorizontalScrollbar)->scroll(deltaByX);
85 else mInner->setScrollX(mInner->scroll().x + deltaByX);
86 AUI_NULLSAFE(mVerticalScrollbar)->scroll(deltaByY);
87 else mInner->setScrollY(mInner->scroll().y + deltaByY);
88 }
89
90 bool onGesture(const glm::ivec2 &origin, const AGestureEvent &event) override;
91
92 void onScroll(const AScrollEvent& event) override;
93
94 void setScrollbarAppearance(ass::ScrollbarAppearance scrollbarAppearance) {
95 AUI_NULLSAFE(mHorizontalScrollbar)->setAppearance(scrollbarAppearance.getHorizontal());
96 AUI_NULLSAFE(mVerticalScrollbar)->setAppearance(scrollbarAppearance.getVertical());
97 }
98
106 void scrollTo(const _<AView>& target, bool nearestBorder = true) {
107 if (!target) {
108 // nullptr target???
109 return;
110 }
111 scrollTo(ARect<int>::fromTopLeftPositionAndSize(target->getPositionInWindow(), target->getSize()), nearestBorder);
112 }
113
121 void scrollTo(ARect<int> target, bool nearestBorder = true);
122
126 void setWheelScrollable(bool value) {
127 mIsWheelScrollable = value;
128 }
129
130 class Builder {
131 friend class AScrollArea;
132 private:
133 _<AScrollbar> mExternalVerticalScrollbar;
134 _<AScrollbar> mExternalHorizontalScrollbar;
135 _<AView> mContents;
136 bool mExpanding = false;
137
138 public:
139 Builder() = default;
140
141 Builder& withExternalVerticalScrollbar(_<AScrollbar> externalVerticalScrollbar) {
142 mExternalVerticalScrollbar = std::move(externalVerticalScrollbar);
143 return *this;
144 }
145
146 Builder& withExternalHorizontalScrollbar(_<AScrollbar> externalHorizontalScrollbar) {
147 mExternalHorizontalScrollbar = std::move(externalHorizontalScrollbar);
148 return *this;
149 }
150
151 Builder& withContents(_<AView> contents) {
152 mContents = std::move(contents);
153 return *this;
154 }
155
156 Builder& withExpanding() {
157 mExpanding = true;
158 return *this;
159 }
160
161 _<AScrollArea> build() {
162 return aui::ptr::manage(new AScrollArea(*this));
163 }
164
165 operator _<AView>() {
166 return build();
167 }
168 operator _<AViewContainerBase>() {
169 return build();
170 }
171 };
172
173 [[nodiscard]]
174 const _<AView>& contents() const noexcept {
175 return mInner->contents();
176 }
177
178 [[nodiscard]]
179 const _<AScrollbar>& verticalScrollbar() const noexcept {
180 return mVerticalScrollbar;
181 }
182
183 [[nodiscard]]
184 const _<AScrollbar>& horizontalScrollbar() const noexcept {
185 return mHorizontalScrollbar;
186 }
187
188 [[nodiscard]]
189 glm::uvec2 scroll() const noexcept {
190 return mInner->scroll();
191 }
192protected:
193 explicit AScrollArea(const Builder& builder);
194
195private:
197 _<AScrollbar> mVerticalScrollbar;
198 _<AScrollbar> mHorizontalScrollbar;
199
203 bool mIsWheelScrollable = true;
204};
205
Definition: AScrollArea.h:130
A scrollable container with vertical and horizontal scrollbars.
Definition: AScrollArea.h:37
void setStickToEnd(bool stickToEnd)
Set stick to end.
Definition: AScrollArea.h:74
void setWheelScrollable(bool value)
Definition: AScrollArea.h:126
void scrollTo(const _< AView > &target, bool nearestBorder=true)
Scrolls to the specified target view.
Definition: AScrollArea.h:106
A view that represents a set of views.
Definition: AViewContainerBase.h:68
void onPointerPressed(const APointerPressedEvent &event) override
Called on pointer (mouse) released event.
Definition: AViewContainerBase.cpp:287
bool onGesture(const glm::ivec2 &origin, const AGestureEvent &event) override
Definition: AViewContainerBase.cpp:547
int getContentMinimumWidth() override
Definition: AViewContainerBase.cpp:273
void onPointerReleased(const APointerReleasedEvent &event) override
Called on pointer (mouse) released event.
Definition: AViewContainerBase.cpp:327
void onScroll(const AScrollEvent &event) override
Definition: AViewContainerBase.cpp:359
void setContents(const _< AViewContainer > &container)
Moves (like via std::move) all children and layout of the specified container to this container.
Definition: AViewContainerBase.cpp:513
int getContentMinimumHeight() override
Definition: AViewContainerBase.cpp:280
glm::ivec2 mExpanding
Expansion coefficient. Hints layout manager how much this AView should be extended relative to other ...
Definition: AView.h:205
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Pointing method press event.
Definition: APointerPressedEvent.h:21
Pointing method press event.
Definition: APointerReleasedEvent.h:19
Axis aligned 2D rectangle.
Definition: ARect.h:24
Pointing method scroll event.
Definition: AScrollEvent.h:20
Definition: Scroll.h:16
Controls how do scrollbars and content appear in AScrollArea. This rule is applicable to AScrollArea ...
Definition: ScrollbarAppearance.h:30
static _< T > manage(T *raw)
Definition: SharedPtrTypes.h:371