From 33e971cc097f9f3dcbc7d0a50cf9a570ec3d61df Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 26 Jan 2024 02:13:56 +0200 Subject: [PATCH] wayland: Move popupOffset() back to xdgshell.cpp The InputPanelV1Window doesn't actually need popupOffset(). Its popup positioning is not data driven like xdg-positioner. It will be simpler and more readable to compute the desired popup geometry explicitly. --- src/inputpanelv1window.cpp | 13 ++++++---- src/utils/common.cpp | 52 ------------------------------------- src/utils/common.h | 1 - src/wayland/xdgshell.cpp | 53 +++++++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/inputpanelv1window.cpp b/src/inputpanelv1window.cpp index b7b4d42f1b..d7e96a7cc0 100644 --- a/src/inputpanelv1window.cpp +++ b/src/inputpanelv1window.cpp @@ -126,9 +126,10 @@ void InputPanelV1Window::reposition() m_windowGeometry = QRectF(QPointF(0, 0), surface()->size()); - // Reuse the similar logic like xdg popup - QRectF popupRect(popupOffset(cursorRectangle, Qt::BottomEdge | Qt::LeftEdge, Qt::RightEdge | Qt::BottomEdge, m_windowGeometry.size()), m_windowGeometry.size()); - + QRectF popupRect(cursorRectangle.left(), + cursorRectangle.top() + cursorRectangle.height(), + m_windowGeometry.width(), + m_windowGeometry.height()); if (popupRect.left() < screen.left()) { popupRect.moveLeft(screen.left()); } @@ -136,8 +137,10 @@ void InputPanelV1Window::reposition() popupRect.moveRight(screen.right()); } if (popupRect.top() < screen.top() || popupRect.bottom() > screen.bottom()) { - auto flippedPopupRect = - QRectF(popupOffset(cursorRectangle, Qt::TopEdge | Qt::LeftEdge, Qt::RightEdge | Qt::TopEdge, m_windowGeometry.size()), m_windowGeometry.size()); + const QRectF flippedPopupRect(cursorRectangle.left(), + cursorRectangle.top() - m_windowGeometry.height(), + m_windowGeometry.width(), + m_windowGeometry.height()); // if it still doesn't fit we should continue with the unflipped version if (flippedPopupRect.top() >= screen.top() && flippedPopupRect.bottom() <= screen.bottom()) { diff --git a/src/utils/common.cpp b/src/utils/common.cpp index df9d83d6b4..3375fca4ee 100644 --- a/src/utils/common.cpp +++ b/src/utils/common.cpp @@ -191,58 +191,6 @@ Qt::KeyboardModifiers x11ToQtKeyboardModifiers(int state) return ret; } -QPointF popupOffset(const QRectF &anchorRect, const Qt::Edges anchorEdge, const Qt::Edges gravity, const QSizeF popupSize) -{ - QPointF anchorPoint; - switch (anchorEdge & (Qt::LeftEdge | Qt::RightEdge)) { - case Qt::LeftEdge: - anchorPoint.setX(anchorRect.x()); - break; - case Qt::RightEdge: - anchorPoint.setX(anchorRect.x() + anchorRect.width()); - break; - default: - anchorPoint.setX(qRound(anchorRect.x() + anchorRect.width() / 2.0)); - } - switch (anchorEdge & (Qt::TopEdge | Qt::BottomEdge)) { - case Qt::TopEdge: - anchorPoint.setY(anchorRect.y()); - break; - case Qt::BottomEdge: - anchorPoint.setY(anchorRect.y() + anchorRect.height()); - break; - default: - anchorPoint.setY(qRound(anchorRect.y() + anchorRect.height() / 2.0)); - } - - // calculate where the top left point of the popup will end up with the applied gravity - // gravity indicates direction. i.e if gravitating towards the top the popup's bottom edge - // will next to the anchor point - QPointF popupPosAdjust; - switch (gravity & (Qt::LeftEdge | Qt::RightEdge)) { - case Qt::LeftEdge: - popupPosAdjust.setX(-popupSize.width()); - break; - case Qt::RightEdge: - popupPosAdjust.setX(0); - break; - default: - popupPosAdjust.setX(qRound(-popupSize.width() / 2.0)); - } - switch (gravity & (Qt::TopEdge | Qt::BottomEdge)) { - case Qt::TopEdge: - popupPosAdjust.setY(-popupSize.height()); - break; - case Qt::BottomEdge: - popupPosAdjust.setY(0); - break; - default: - popupPosAdjust.setY(qRound(-popupSize.height() / 2.0)); - } - - return anchorPoint + popupPosAdjust; -} - QRectF gravitateGeometry(const QRectF &rect, const QRectF &bounds, Gravity gravity) { QRectF geometry = rect; diff --git a/src/utils/common.h b/src/utils/common.h index 2844ea74d3..5250f4a159 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -90,7 +90,6 @@ Qt::MouseButton KWIN_EXPORT x11ToQtMouseButton(int button); Qt::MouseButtons KWIN_EXPORT x11ToQtMouseButtons(int state); Qt::KeyboardModifiers KWIN_EXPORT x11ToQtKeyboardModifiers(int state); -KWIN_EXPORT QPointF popupOffset(const QRectF &anchorRect, const Qt::Edges anchorEdge, const Qt::Edges gravity, const QSizeF popupSize); KWIN_EXPORT QRectF gravitateGeometry(const QRectF &rect, const QRectF &bounds, Gravity gravity); } // namespace diff --git a/src/wayland/xdgshell.cpp b/src/wayland/xdgshell.cpp index 928ac642f7..5b11315cc0 100644 --- a/src/wayland/xdgshell.cpp +++ b/src/wayland/xdgshell.cpp @@ -10,7 +10,6 @@ #include "display.h" #include "output.h" #include "seat.h" -#include "utils/common.h" #include "utils/resource.h" #include @@ -1049,6 +1048,58 @@ bool XdgPositioner::isReactive() const return d->isReactive; } +static QPointF popupOffset(const QRectF &anchorRect, const Qt::Edges anchorEdge, const Qt::Edges gravity, const QSizeF popupSize) +{ + QPointF anchorPoint; + switch (anchorEdge & (Qt::LeftEdge | Qt::RightEdge)) { + case Qt::LeftEdge: + anchorPoint.setX(anchorRect.x()); + break; + case Qt::RightEdge: + anchorPoint.setX(anchorRect.x() + anchorRect.width()); + break; + default: + anchorPoint.setX(qRound(anchorRect.x() + anchorRect.width() / 2.0)); + } + switch (anchorEdge & (Qt::TopEdge | Qt::BottomEdge)) { + case Qt::TopEdge: + anchorPoint.setY(anchorRect.y()); + break; + case Qt::BottomEdge: + anchorPoint.setY(anchorRect.y() + anchorRect.height()); + break; + default: + anchorPoint.setY(qRound(anchorRect.y() + anchorRect.height() / 2.0)); + } + + // calculate where the top left point of the popup will end up with the applied gravity + // gravity indicates direction. i.e if gravitating towards the top the popup's bottom edge + // will next to the anchor point + QPointF popupPosAdjust; + switch (gravity & (Qt::LeftEdge | Qt::RightEdge)) { + case Qt::LeftEdge: + popupPosAdjust.setX(-popupSize.width()); + break; + case Qt::RightEdge: + popupPosAdjust.setX(0); + break; + default: + popupPosAdjust.setX(qRound(-popupSize.width() / 2.0)); + } + switch (gravity & (Qt::TopEdge | Qt::BottomEdge)) { + case Qt::TopEdge: + popupPosAdjust.setY(-popupSize.height()); + break; + case Qt::BottomEdge: + popupPosAdjust.setY(0); + break; + default: + popupPosAdjust.setY(qRound(-popupSize.height() / 2.0)); + } + + return anchorPoint + popupPosAdjust; +} + QRectF XdgPositioner::placement(const QRectF &bounds) const { // returns if a target is within the supplied bounds, optional edges argument states which side to check