AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AScrollbar.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//
13// Created by alex2 on 06.12.2020.
14//
15
16#pragma once
17
18#include <AUI/Util/ALayoutDirection.h>
19#include <AUI/Common/ATimer.h>
20#include "AViewContainer.h"
21#include "ASpacerExpanding.h"
22
23class API_AUI_VIEWS AScrollbar;
24
25class AScrollbarButton: public AView {
26public:
28
29 }
30};
31class AScrollbarHandle: public AView {friend class API_AUI_VIEWS AScrollbar;
32private:
33 int mScrollOffset = 0;
34 bool mDragging = false;
35
36public:
37 void setSize(glm::ivec2 size) override;
38 void onPointerMove(glm::vec2 pos, const APointerMoveEvent& event) override;
39 void onPointerPressed(const APointerPressedEvent& event) override;
40 void onPointerReleased(const APointerReleasedEvent& event) override;
41 void setOverridenSize(int overridenSize) {
42 mOverridenSize = overridenSize;
43 }
44
45private:
46 explicit AScrollbarHandle(AScrollbar& scrollbar) : mScrollbar(scrollbar) {}
47
48 AScrollbar& mScrollbar;
49 int mOverridenSize;
50};
51
53public:
55
56 }
57
58 int getMinimumWidth() override {
59 return 0;
60 }
61
62 int getMinimumHeight() override {
63 return 0;
64 }
65};
66
73class API_AUI_VIEWS AScrollbar: public AViewContainerBase {
74 friend class AScrollbarHandle;
75public:
76
77 explicit AScrollbar(ALayoutDirection direction = ALayoutDirection::VERTICAL);
78
79 [[nodiscard]] int getCurrentScroll() const {
80 return mCurrentScroll;
81 }
82
83 void setScrollDimensions(size_t viewportSize, size_t fullSize);
84 void updateScrollHandleSize();
85 void setScroll(int scroll);
86
87 void scroll(int delta) noexcept {
88 setScroll(mCurrentScroll + delta);
89 }
90
91 void onScroll(float& delta);
92
93 void onScroll(const AScrollEvent& event) override;
94
102 void setStickToEnd(bool stickToEnd) {
103 if (stickToEnd) {
104 mStickToEnd = StickToEnd{
105 .locked = true,
106 };
107 scrollToEnd();
108 } else {
109 mStickToEnd.reset();
110 }
111 }
112
113 void setAppearance(ass::ScrollbarAppearance::AxisValue appearance) {
114 mAppearance = appearance;
115 }
116
117 void scrollToStart() {
118 setScroll(0);
119 }
120
121 void scrollToEnd();
122
123 float getAvailableSpaceForSpacer();
124
125 void updateScrollHandleOffset(int max);
126
127 void onPointerPressed(const APointerPressedEvent& event) override;
128
129 void setSize(glm::ivec2 size) override;
130
131 static const _<ATimer>& buttonTimer();
132
133
140 std::size_t getMaxScroll() const noexcept {
141 if (mFullSize <= mViewportSize) {
142 return 0;
143 }
144
145 return mFullSize - mViewportSize;
146 }
147
148signals:
149
151
152 emits<int> updatedMaxScroll;
153
154 emits<> triggeredManually;
155
156protected:
157 ALayoutDirection mDirection;
158 _<ASpacerExpanding> mOffsetSpacer;
159 _<AScrollbarHandle> mHandle;
160 _<AScrollbarButton> mForwardButton;
161 _<AScrollbarButton> mBackwardButton;
162
163 size_t mViewportSize = 0, mFullSize = 0;
164 int mCurrentScroll = 0;
165
166 void setOffset(size_t o);
167
168 void scrollForward();
169 void scrollBackward();
170
171 void handleScrollbar(int s);
172
173
174private:
175 struct StickToEnd {
179 bool locked = true;
180 };
188 AOptional<StickToEnd> mStickToEnd;
190};
191
192
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Definition: AScrollbar.h:25
Definition: AScrollbar.h:31
void onPointerReleased(const APointerReleasedEvent &event) override
Called on pointer (mouse) released event.
Definition: AScrollbar.cpp:278
void onPointerPressed(const APointerPressedEvent &event) override
Called on pointer (mouse) released event.
Definition: AScrollbar.cpp:263
void onPointerMove(glm::vec2 pos, const APointerMoveEvent &event) override
Handles pointer hover events.
Definition: AScrollbar.cpp:249
Definition: AScrollbar.h:52
A single scrollbar.
Definition: AScrollbar.h:73
void setStickToEnd(bool stickToEnd)
Set stick to end.
Definition: AScrollbar.h:102
std::size_t getMaxScroll() const noexcept
Definition: AScrollbar.h:140
Expanding view which is useful in UI building.
Definition: ASpacerExpanding.h:28
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
void onScroll(const AScrollEvent &event) override
Definition: AViewContainerBase.cpp:359
Base class of all UI objects.
Definition: AView.h:77
emits< glm::ivec2 > scrolled
Scroll event.
Definition: AView.h:1103
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Pointing method move event.
Definition: APointerMoveEvent.h:21
Pointing method press event.
Definition: APointerPressedEvent.h:21
Pointing method press event.
Definition: APointerReleasedEvent.h:19
Pointing method scroll event.
Definition: AScrollEvent.h:20
Definition: Scroll.h:16
AxisValue
Per-axis behaviour enum.
Definition: ScrollbarAppearance.h:35
@ ON_DEMAND
Scrollbar appears only if content is overflowing.
Definition: ScrollbarAppearance.h:44