Make the reference point independent of the window
authorKevin Ottens <kevin.ottens@nextcloud.com>
Wed, 20 May 2020 19:36:08 +0000 (21:36 +0200)
committerKevin Ottens (Rebase PR Action) <er-vin@users.noreply.github.com>
Mon, 15 Jun 2020 12:32:25 +0000 (12:32 +0000)
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 <kevin.ottens@nextcloud.com>
src/gui/systray.cpp
src/gui/systray.h

index 15f18ba66afd2d1ec791cea97285300924a35a0d..5bc9e1479eff083045829dcb64737611c78e1644 100644 (file)
@@ -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
index 634d0a154ce8a7d99467ff94d6147ec91d510faa..feb0771aafa2dd9ddba0bd7bfcd589795ad72a15 100644 (file)
@@ -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;