QmlMaterial 0.1.0
Loading...
Searching...
No Matches
popup_placement.hpp
1#pragma once
2
3#include "qml_material/control/popup.hpp"
4#include <algorithm>
5#include <cmath>
6#include <optional>
7
8namespace qml_material
9{
10struct PopupPlacement {
11 QRectF rect;
12 Popup::Placement placement;
13 QQuickItem::TransformOrigin origin;
14};
15
16inline std::optional<PopupPlacement> placePopup(const QRectF& anchor, const QSizeF& request,
17 const QRectF& bounds, Popup::Placement preferred,
18 Popup::AnchorAlignment alignment, qreal gap,
19 bool mirrored) {
20 for (const auto value : { anchor.x(),
21 anchor.y(),
22 anchor.width(),
23 anchor.height(),
24 request.width(),
25 request.height(),
26 bounds.x(),
27 bounds.y(),
28 bounds.width(),
29 bounds.height(),
30 gap })
31 if (! std::isfinite(value)) return {};
32 if (bounds.isEmpty() || request.isEmpty() || anchor.width() < 0 || anchor.height() < 0)
33 return {};
34 gap = std::max<qreal>(0, gap);
35 const bool vertical = preferred == Popup::Below || preferred == Popup::Above;
36 auto opposite = [](Popup::Placement p) {
37 switch (p) {
38 case Popup::Below: return Popup::Above;
39 case Popup::Above: return Popup::Below;
40 case Popup::Leading: return Popup::Trailing;
41 case Popup::Trailing: return Popup::Leading;
42 }
43 return Popup::Below;
44 };
45 auto leftSide = [mirrored](Popup::Placement p) {
46 return (p == Popup::Leading) != mirrored;
47 };
48 auto space = [&](Popup::Placement p) {
49 qreal available;
50 if (p == Popup::Below)
51 available = bounds.bottom() - anchor.bottom() - gap;
52 else if (p == Popup::Above)
53 available = anchor.top() - gap - bounds.top();
54 else if (leftSide(p))
55 available = anchor.left() - gap - bounds.left();
56 else
57 available = bounds.right() - anchor.right() - gap;
58 return std::clamp(available, qreal(0), vertical ? bounds.height() : bounds.width());
59 };
60 auto resolved = preferred;
61 const auto other = opposite(preferred);
62 const auto extent = vertical ? request.height() : request.width();
63 if (extent > space(preferred) && space(other) > space(preferred)) resolved = other;
64 if (space(resolved) <= 0) return {};
65 QSizeF size(std::min(request.width(), bounds.width()),
66 std::min(request.height(), bounds.height()));
67 if (vertical)
68 size.setHeight(std::min(size.height(), space(resolved)));
69 else
70 size.setWidth(std::min(size.width(), space(resolved)));
71 const qreal low = vertical ? bounds.left() : bounds.top();
72 const qreal high = vertical ? bounds.right() : bounds.bottom();
73 const qreal cross = vertical ? size.width() : size.height();
74 const qreal start = vertical ? anchor.left() : anchor.top();
75 const qreal end = vertical ? anchor.right() : anchor.bottom();
76 auto align = [&](Popup::AnchorAlignment a) {
77 if (a == Popup::Center) return (start + end - cross) / 2;
78 const bool atEnd = (a == Popup::End) != (vertical && mirrored);
79 return atEnd ? end - cross : start;
80 };
81 qreal offset = align(alignment);
82 if (alignment != Popup::Center && (offset < low || offset + cross > high)) {
83 const auto alternate = align(alignment == Popup::Start ? Popup::End : Popup::Start);
84 if (alternate >= low && alternate + cross <= high) offset = alternate;
85 }
86 offset = std::clamp(offset, low, high - cross);
87 QPointF point;
88 if (vertical)
89 point = { offset,
90 resolved == Popup::Below ? anchor.bottom() + gap
91 : anchor.top() - gap - size.height() };
92 else
93 point = { leftSide(resolved) ? anchor.left() - gap - size.width() : anchor.right() + gap,
94 offset };
95 point.setX(std::clamp(point.x(), bounds.left(), bounds.right() - size.width()));
96 point.setY(std::clamp(point.y(), bounds.top(), bounds.bottom() - size.height()));
97 const qreal center = vertical ? anchor.center().x() : anchor.center().y();
98 const int crossOrigin = std::clamp(qRound(2 * (center - offset) / cross), 0, 2);
99 QQuickItem::TransformOrigin origin;
100 if (vertical) {
101 const QQuickItem::TransformOrigin top[] = { QQuickItem::TopLeft,
102 QQuickItem::Top,
103 QQuickItem::TopRight };
104 const QQuickItem::TransformOrigin bottom[] = { QQuickItem::BottomLeft,
105 QQuickItem::Bottom,
106 QQuickItem::BottomRight };
107 origin = resolved == Popup::Below ? top[crossOrigin] : bottom[crossOrigin];
108 } else {
109 const QQuickItem::TransformOrigin left[] = { QQuickItem::TopLeft,
110 QQuickItem::Left,
111 QQuickItem::BottomLeft };
112 const QQuickItem::TransformOrigin right[] = { QQuickItem::TopRight,
113 QQuickItem::Right,
114 QQuickItem::BottomRight };
115 origin = leftSide(resolved) ? right[crossOrigin] : left[crossOrigin];
116 }
117 return PopupPlacement { QRectF(point, size), resolved, origin };
118}
119} // namespace qml_material