From: Kevin Ottens Date: Wed, 20 May 2020 19:36:08 +0000 (+0200) Subject: Make the reference point independent of the window X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~222^2^2~160^2~4 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=5848a1575b40075c6df4474727cf61934fb9d62b;p=nextcloud-desktop.git Make the reference point independent of the window This leads to simplifying the computation code quite a bit as well. Indeed we're separating concern between what is window size dependent or not and that shows. Signed-off-by: Kevin Ottens --- diff --git a/src/gui/systray.cpp b/src/gui/systray.cpp index 15f18ba66..5bc9e1479 100644 --- a/src/gui/systray.cpp +++ b/src/gui/systray.cpp @@ -242,8 +242,9 @@ QRect Systray::currentScreenRect() const return rect.translated(screen->virtualGeometry().topLeft()); } -QPoint Systray::computeWindowReferencePoint(int width, int height) const +QPoint Systray::computeWindowReferencePoint() const { + constexpr auto spacing = 4; const auto trayIconCenter = calcTrayIconCenter(); const auto taskbarRect = taskbarGeometry(); const auto taskbarScreenEdge = taskbarOrientation(); @@ -252,22 +253,22 @@ QPoint Systray::computeWindowReferencePoint(int width, int height) const switch(taskbarScreenEdge) { case TaskBarPosition::Bottom: return { - trayIconCenter.x() - width / 2, - screenRect.bottom() - taskbarRect.height() - height - 4 + trayIconCenter.x(), + screenRect.bottom() - taskbarRect.height() - spacing }; case TaskBarPosition::Left: return { - screenRect.left() + taskbarRect.width() + 4, + screenRect.left() + taskbarRect.width() + spacing, trayIconCenter.y() }; case TaskBarPosition::Top: return { - trayIconCenter.x() - width / 2, - screenRect.top() + taskbarRect.height() + 4 + trayIconCenter.x(), + screenRect.top() + taskbarRect.height() + spacing }; case TaskBarPosition::Right: return { - screenRect.right() - taskbarRect.width() - width - 4, + screenRect.right() - taskbarRect.width() - spacing, trayIconCenter.y() }; } @@ -276,38 +277,44 @@ QPoint Systray::computeWindowReferencePoint(int width, int height) const QPoint Systray::computeWindowPosition(int width, int height) const { - auto referencePoint = computeWindowReferencePoint(width, height); + const auto referencePoint = computeWindowReferencePoint(); const auto taskbarScreenEdge = taskbarOrientation(); - const auto taskbarRect = taskbarGeometry(); const auto screenRect = currentScreenRect(); - if (screenRect.right() <= referencePoint.x() + width) { - referencePoint.rx() = screenRect.right() - width - 4; -#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) - referencePoint.rx() -= taskbarScreenEdge == TaskBarPosition::Right ? taskbarRect.width() : 0; -#endif - } - - if (referencePoint.x() <= screenRect.left()) { - referencePoint.rx() = screenRect.left() + 4; -#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) - referencePoint.rx() += taskbarScreenEdge == TaskBarPosition::Left ? taskbarRect.width() : 0; -#endif - } - - if (referencePoint.y() <= screenRect.top()) { - referencePoint.ry() = screenRect.top() + 4; + const auto topLeft = [=]() { + switch(taskbarScreenEdge) { + case TaskBarPosition::Bottom: + return referencePoint - QPoint(width / 2, height); + case TaskBarPosition::Left: + return referencePoint; + case TaskBarPosition::Top: + return referencePoint - QPoint(width / 2, 0); + case TaskBarPosition::Right: + return referencePoint - QPoint(width, 0); + } + Q_UNREACHABLE(); + }(); + const auto bottomRight = topLeft + QPoint(width, height); + const auto windowRect = [=]() { + const auto rect = QRect(topLeft, bottomRight); + auto offset = QPoint(); + + if (rect.left() < screenRect.left()) { + offset.setX(screenRect.left() - rect.left() + 4); + } else if (rect.right() > screenRect.right()) { + offset.setX(screenRect.right() - rect.right() - 4); + } -#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) - referencePoint.ry() += taskbarScreenEdge == TaskBarPosition::Top ? taskbarRect.height() : 0; -#endif - } - if (screenRect.bottom() <= referencePoint.y() + height) { - referencePoint.ry() = screenRect.bottom() - height - 4; - } + if (rect.top() < screenRect.top()) { + offset.setY(screenRect.top() - rect.top() + 4); + } else if (rect.bottom() > screenRect.bottom()) { + offset.setY(screenRect.bottom() - rect.bottom() - 4); + } - return referencePoint; + return rect.translated(offset); + }(); + return windowRect.topLeft(); } QPoint Systray::calcTrayIconCenter() const diff --git a/src/gui/systray.h b/src/gui/systray.h index 634d0a154..feb0771aa 100644 --- a/src/gui/systray.h +++ b/src/gui/systray.h @@ -85,7 +85,7 @@ private: QScreen *currentScreen() const; QRect currentScreenRect() const; - QPoint computeWindowReferencePoint(int width, int height) const; + QPoint computeWindowReferencePoint() const; bool _isOpen = false; bool _syncIsPaused = false;