Get rid of screenIndex
authorKevin Ottens <kevin.ottens@nextcloud.com>
Wed, 20 May 2020 18:03:08 +0000 (20:03 +0200)
committerKevin Ottens (Rebase PR Action) <er-vin@users.noreply.github.com>
Mon, 15 Jun 2020 12:32:25 +0000 (12:32 +0000)
This was leading to the same logic being duplicated several times. It's
fine to return the QScreen* on the QML side directly but wrapped into a
QVariant.

Signed-off-by: Kevin Ottens <kevin.ottens@nextcloud.com>
src/gui/systray.cpp
src/gui/systray.h
src/gui/tray/Window.qml

index 5db933983fed9d15b82aec289be4de6a1a65d297..15f18ba66afd2d1ec791cea97285300924a35a0d 100644 (file)
@@ -158,16 +158,9 @@ QScreen *Systray::currentScreen() const
     return nullptr;
 }
 
-/// Return the current screen index based on cursor position
-int Systray::screenIndex() const
+QVariant Systray::currentScreenVar() const
 {
-    auto qPos = QCursor::pos();
-    for (int i = 0; i < QGuiApplication::screens().count(); i++) {
-        if (QGuiApplication::screens().at(i)->geometry().contains(qPos)) {
-            return i;
-        }
-    }
-    return 0;
+    return QVariant::fromValue(currentScreen());
 }
 
 Systray::TaskBarPosition Systray::taskbarOrientation() const
@@ -195,17 +188,13 @@ Systray::TaskBarPosition Systray::taskbarOrientation() const
     }
 // Probably Linux
 #else
-    auto currentScreen = screenIndex();
-    auto screenWidth = QGuiApplication::screens().at(currentScreen)->geometry().width();
-    auto screenHeight = QGuiApplication::screens().at(currentScreen)->geometry().height();
-    auto virtualY = QGuiApplication::screens().at(currentScreen)->virtualGeometry().y();
-    auto virtualX = QGuiApplication::screens().at(currentScreen)->virtualGeometry().x();
-    QPoint trayIconCenter = calcTrayIconCenter();
-
-    auto distBottom = screenHeight - (trayIconCenter.y() - virtualY);
-    auto distRight = screenWidth - (trayIconCenter.x() - virtualX);
-    auto distLeft = trayIconCenter.x() - virtualX;
-    auto distTop = trayIconCenter.y() - virtualY;
+    const auto screenRect = currentScreenRect();
+    const auto trayIconCenter = calcTrayIconCenter();
+
+    auto distBottom = screenRect.bottom() - trayIconCenter.y();
+    auto distRight = screenRect.right() - trayIconCenter.x();
+    auto distLeft = trayIconCenter.x() - screenRect.left();
+    auto distTop = trayIconCenter.y() - screenRect.top();
 
     if (distBottom < distRight && distBottom < distTop && distBottom < distLeft) {
         return TaskBarPosition::Bottom;
@@ -225,7 +214,7 @@ QRect Systray::taskbarGeometry() const
 #if defined(Q_OS_WIN)
     QRect tbRect = Utility::getTaskbarDimensions();
     //QML side expects effective pixels, convert taskbar dimensions if necessary
-    auto pixelRatio = QGuiApplication::screens().at(screenIndex())->devicePixelRatio();
+    auto pixelRatio = currentScreen()->devicePixelRatio();
     if (pixelRatio != 1) {
         tbRect.setHeight(tbRect.height() / pixelRatio);
         tbRect.setWidth(tbRect.width() / pixelRatio);
@@ -233,14 +222,14 @@ QRect Systray::taskbarGeometry() const
     return tbRect;
 #elif defined(Q_OS_MACOS)
     // Finder bar is always 22px height on macOS (when treating as effective pixels)
-    auto screenWidth = QGuiApplication::screens().at(screenIndex())->geometry().width();
+    auto screenWidth = currentScreenRect().width();
     return QRect(0, 0, screenWidth, 22);
 #else
     if (taskbarOrientation() == TaskBarPosition::Bottom || taskbarOrientation() == TaskBarPosition::Top) {
-        auto screenWidth = QGuiApplication::screens().at(screenIndex())->geometry().width();
+        auto screenWidth = currentScreenRect().width();
         return QRect(0, 0, screenWidth, 32);
     } else {
-        auto screenHeight = QGuiApplication::screens().at(screenIndex())->geometry().height();
+        auto screenHeight = currentScreenRect().height();
         return QRect(0, 0, 32, screenHeight);
     }
 #endif
index bc19237071b8b86a174a389a67da646b2ec81426..634d0a154ce8a7d99467ff94d6147ec91d510faa 100644 (file)
@@ -58,7 +58,7 @@ public:
     Q_INVOKABLE bool syncIsPaused();
     Q_INVOKABLE void setOpened();
     Q_INVOKABLE void setClosed();
-    Q_INVOKABLE int screenIndex() const;
+    Q_INVOKABLE QVariant currentScreenVar() const;
     Q_INVOKABLE QPoint calcTrayIconCenter() const;
     Q_INVOKABLE TaskBarPosition taskbarOrientation() const;
     Q_INVOKABLE QRect taskbarGeometry() const;
index 505875808509cb153d215a67bdeff19cf62b37c3..9e11828b8a042082c8a58d970f887d7d4541303a 100644 (file)
@@ -63,7 +63,7 @@ Window {
         onShowWindow: {\r
             accountMenu.close();\r
 \r
-            trayWindow.screen = Qt.application.screens[systrayBackend.screenIndex()];\r
+            trayWindow.screen = systrayBackend.currentScreenVar();\r
 \r
             var position = systrayBackend.computeWindowPosition(trayWindow.width, trayWindow.height)\r
             trayWindow.x = position.x\r