AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
ASplitterHelper.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 <AUI/Util/ALayoutDirection.h>
16#include <AUI/View/AView.h>
17
19 friend class ASplitter;
20public:
21 struct Item {
22 _<AView> view;
23 AOptional<int> overridedSize;
24 };
25
26 ASplitterHelper() = default;
27 ASplitterHelper(ALayoutDirection direction) : mDirection(direction) {}
28
29 void setDirection(ALayoutDirection direction) {
30 mDirection = direction;
31 }
32
33 void beginDrag(const glm::ivec2& mousePos);
34 bool mouseDrag(const glm::ivec2& mousePos);
35 void endDrag() {
36 mDraggingDividerIndex = -1;
37 }
38
39 [[nodiscard]]
40 const AVector<Item>& items() const { return mItems; }
41
42 void setItems(AVector<Item> items) {
43 mItems = std::move(items);
44 }
45
46 void setItems(const AVector<_<AView>>& items) {
47 mItems = items.map([](const _<AView>& view) { return Item { .view = view }; });
48 }
49
50 [[nodiscard]]
51 bool isDragging() const {
52 return mDraggingDividerIndex != -1;
53 }
54
55 bool isDraggingArea(glm::ivec2 position);
56
66 int reclaimSpace(int space, size_t dividerIndex);
67
76 int reclaimSpace(int space) {
77 return reclaimSpace(space, mItems.size() - 1);
78 }
79
80private:
81 ALayoutDirection mDirection; // will be initialized in the Builder
82 size_t mDraggingDividerIndex = -1;
83 int mDragOffset; // may be uninitialized
84 AVector<Item> mItems;
85
86 float getTotalOccupiedSizeOf(const _<AView>& view) {
87 return mDirection == ALayoutDirection::VERTICAL ? view->getTotalOccupiedHeight() : view->getTotalOccupiedWidth();
88 }
89
90 template<typename T>
91 [[nodiscard]]
92 T& getAxisValue(glm::tvec2<T>& v) {
93 return aui::layout_direction::getAxisValue(mDirection, v);
94 }
95
96 template<typename T>
97 [[nodiscard]]
98 T getAxisValue(const glm::tvec2<T>& v) {
99 return aui::layout_direction::getAxisValue(mDirection, v);
100 }
101
102};
103
104
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Definition: ASplitterHelper.h:18
int reclaimSpace(int space)
Reclaims space via the last divider.
Definition: ASplitterHelper.h:76
int reclaimSpace(int space, size_t dividerIndex)
Reclaims space via specified divider index.
Definition: ASplitterHelper.cpp:135
A resizeable horizontal or vertical layout.
Definition: ASplitter.h:46
A std::vector with AUI extensions.
Definition: AVector.h:38
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Definition: ASplitterHelper.h:21