3#include "qml_material/control/popup.hpp"
10struct PopupPlacement {
12 Popup::Placement placement;
13 QQuickItem::TransformOrigin origin;
16inline std::optional<PopupPlacement> placePopup(
const QRectF& anchor,
const QSizeF& request,
17 const QRectF& bounds, Popup::Placement preferred,
18 Popup::AnchorAlignment alignment, qreal gap,
20 for (
const auto value : { anchor.x(),
31 if (! std::isfinite(value))
return {};
32 if (bounds.isEmpty() || request.isEmpty() || anchor.width() < 0 || anchor.height() < 0)
34 gap = std::max<qreal>(0, gap);
35 const bool vertical = preferred == Popup::Below || preferred == Popup::Above;
36 auto opposite = [](Popup::Placement 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;
45 auto leftSide = [mirrored](Popup::Placement p) {
46 return (p == Popup::Leading) != mirrored;
48 auto space = [&](Popup::Placement p) {
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();
55 available = anchor.left() - gap - bounds.left();
57 available = bounds.right() - anchor.right() - gap;
58 return std::clamp(available, qreal(0), vertical ? bounds.height() : bounds.width());
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()));
68 size.setHeight(std::min(size.height(), space(resolved)));
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;
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;
86 offset = std::clamp(offset, low, high - cross);
90 resolved == Popup::Below ? anchor.bottom() + gap
91 : anchor.top() - gap - size.height() };
93 point = { leftSide(resolved) ? anchor.left() - gap - size.width() : anchor.right() + gap,
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;
101 const QQuickItem::TransformOrigin top[] = { QQuickItem::TopLeft,
103 QQuickItem::TopRight };
104 const QQuickItem::TransformOrigin bottom[] = { QQuickItem::BottomLeft,
106 QQuickItem::BottomRight };
107 origin = resolved == Popup::Below ? top[crossOrigin] : bottom[crossOrigin];
109 const QQuickItem::TransformOrigin left[] = { QQuickItem::TopLeft,
111 QQuickItem::BottomLeft };
112 const QQuickItem::TransformOrigin right[] = { QQuickItem::TopRight,
114 QQuickItem::BottomRight };
115 origin = leftSide(resolved) ? right[crossOrigin] : left[crossOrigin];
117 return PopupPlacement { QRectF(point, size), resolved, origin };