-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayout.mpp
More file actions
79 lines (69 loc) · 1.97 KB
/
Layout.mpp
File metadata and controls
79 lines (69 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export module CppUtils.Terminal.Layout;
import std;
import CppUtils.Container.Size;
import CppUtils.Terminal.Area;
export namespace CppUtils::Terminal
{
class Layout: public Widget
{
public:
enum class Direction
{
Horizontal,
Vertical
};
virtual ~Layout() = default;
template<std::derived_from<Widget> T>
inline auto addWidget(std::unique_ptr<T> widget) -> T&
{
widget->setWidgetManager(getWidgetManager());
return dynamic_cast<T&>(*m_widgets.emplace_back(std::move(widget)));
}
[[nodiscard]] inline auto getSize() const noexcept -> Container::Size2 final
{
auto totalSize = Container::Size2{0, 0};
for (const auto& widget : m_widgets)
{
if (direction == Direction::Horizontal)
{
totalSize.width() += widget->getSize().width();
totalSize.height() = std::max(totalSize.height(), widget->getSize().height());
}
else
{
totalSize.width() = std::max(totalSize.width(), widget->getSize().width());
totalSize.height() += widget->getSize().height();
}
}
return totalSize;
}
inline auto draw(WritableAreaView& view) noexcept -> void override final
{
auto origin = view.getViewport().getPosition();
auto size = view.getViewport().getSize();
for (const auto& widget : m_widgets)
{
auto widgetSize = widget->getSize();
widgetSize.width() = std::min(widgetSize.width(), size.width());
widgetSize.height() = std::min(widgetSize.height(), size.height());
auto subview = view.getSubView(Viewport{widgetSize, origin});
if (not subview)
continue;
widget->draw(subview.value());
if (direction == Direction::Horizontal and size.width() < widgetSize.width())
{
origin.width() += widgetSize.width();
size.width() -= widgetSize.width();
}
else
{
origin.height() += widgetSize.height();
size.height() -= widgetSize.height();
}
}
}
Direction direction = Direction::Vertical;
private:
std::vector<std::unique_ptr<Widget>> m_widgets;
};
}