More simplifications, also transfer of more tray positioning logic to C++ backend.
authorDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Wed, 20 May 2020 08:12:55 +0000 (10:12 +0200)
committerKevin Ottens (Rebase PR Action) <er-vin@users.noreply.github.com>
Mon, 15 Jun 2020 12:32:25 +0000 (12:32 +0000)
Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
src/gui/systray.cpp
src/gui/systray.h
src/gui/tray/Window.qml

index 98d55c1f968ba50e4d57898670060b50c9c11a42..88d6812a49cd9dc674d197c3d41f275f0da16d82 100644 (file)
@@ -157,7 +157,7 @@ int Systray::screenIndex()
     return 0;
 }
 
-int Systray::taskbarOrientation()
+Systray::TaskBarPosition Systray::taskbarOrientation()
 {
 // macOS: Always on top
 #if defined(Q_OS_MACOS)
@@ -180,18 +180,56 @@ int Systray::taskbarOrientation()
     default:
         return TaskBarPosition::Bottom;
     }
-// Else (generally linux DEs): fallback to cursor position nearest edge logic
+// Probably Linux
 #else
-    return 0;
+    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;
+
+    if (distBottom < distRight && distBottom < distTop && distBottom < distLeft) {
+        return TaskBarPosition::Bottom;
+    } else if (distLeft < distTop && distLeft < distRight && distLeft < distBottom) {
+        return TaskBarPosition::Left;
+    } else if (distTop < distRight && distTop < distBottom && distTop < distLeft) {
+        return TaskBarPosition::Top;
+    } else {
+        return TaskBarPosition::Right;
+    }
 #endif
 }
 
+// TODO: Get real taskbar dimensions Linux as well
 QRect Systray::taskbarRect()
 {
 #if defined(Q_OS_WIN)
-    return Utility::getTaskbarDimensions();
+    QRect tbRect = Utility::getTaskbarDimensions();
+    //QML side expects effective pixels, convert taskbar dimensions if necessary
+    auto pixelRatio = QGuiApplication::screens().at(screenIndex())->devicePixelRatio();
+    if (pixelRatio != 1) {
+        tbRect.setHeight(tbRect.height() / pixelRatio);
+        tbRect.setWidth(tbRect.width() / pixelRatio);
+    }
+    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();
+    return QRect(0, 0, screenWidth, 22);
 #else
-    return QRect(0, 0, 0, 32);
+    if (taskbarOrientation() == TaskBarPosition::Bottom || taskbarOrientation() == TaskBarPosition::Top) {
+        auto screenWidth = QGuiApplication::screens().at(screenIndex())->geometry().width();
+        return QRect(0, 0, screenWidth, 32);
+    } else {
+        auto screenHeight = QGuiApplication::screens().at(screenIndex())->geometry().height();
+        return QRect(0, 0, 32, screenHeight);
+    }
 #endif
 }
 
index ad92c7a54c66864bcc5356a3a193bc9395b7e6d1..5dd26ad043acc9d8519ebf95406ee16290ff6acb 100644 (file)
@@ -33,13 +33,6 @@ namespace Ui {
     class Systray;
 }
 
-enum TaskBarPosition {
-    Bottom = 0,
-    Left,
-    Top,
-    Right
-};
-
 /**
  * @brief The Systray class
  * @ingroup gui
@@ -52,6 +45,9 @@ public:
     static Systray *instance();
     virtual ~Systray() {};
 
+    enum class TaskBarPosition { Bottom, Left, Top, Right };
+    Q_ENUM(TaskBarPosition);
+
     void create();
     void showMessage(const QString &title, const QString &message, MessageIcon icon = Information);
     void setToolTip(const QString &tip);
@@ -63,7 +59,7 @@ public:
     Q_INVOKABLE void setClosed();
     Q_INVOKABLE int screenIndex();
     Q_INVOKABLE QPoint calcTrayIconCenter();
-    Q_INVOKABLE int taskbarOrientation();
+    Q_INVOKABLE TaskBarPosition taskbarOrientation();
     Q_INVOKABLE QRect taskbarRect();
 
 signals:
index 1ae7973dbf7d4ca6a556bd47b065e9ff884147a9..77c097ba679d02d745bcf0012490161273a578db 100644 (file)
@@ -23,36 +23,8 @@ Window {
         trayWindow.requestActivate();\r
         var trayWindowX;\r
         var trayWindowY;\r
-        var taskbarHeight;\r
-        var taskbarWidth;\r
-        var tbOrientation;\r
-        if (Qt.platform.os === "linux") {\r
-            var distBottom = Screen.height - (trayIconCenter.y - Screen.virtualY);\r
-            var distRight = Screen.width - (trayIconCenter.x - Screen.virtualX);\r
-            var distLeft = trayIconCenter.x - Screen.virtualX;\r
-            var distTop = trayIconCenter.y - Screen.virtualY;\r
-            if (distBottom < distRight && distBottom < distTop && distBottom < distLeft) {\r
-                tbOrientation = 0;\r
-            } else if (distLeft < distTop && distLeft < distRight && distLeft < distBottom) {\r
-                tbOrientation = 1;\r
-            } else if (distTop < distRight && distTop < distBottom && distTop < distLeft) {\r
-                tbOrientation = 2;\r
-            } else {\r
-                tbOrientation = 3;\r
-            }\r
-        } else {\r
-            tbOrientation = systrayBackend.taskbarOrientation();\r
-        }\r
-        if (Qt.platform.os === "osx") {\r
-            taskbarHeight = 22;\r
-            taskbarWidth = Screen.width;\r
-        } else if (Qt.platform.os === "linux") {\r
-            taskbarHeight = (tbOrientation === 0 || tbOrientation === 2) ? 32 : Screen.height;\r
-            taskbarWidth = (tbOrientation === 0 || tbOrientation === 2) ? Screen.width : 32;\r
-        } else {\r
-            taskbarHeight = systrayBackend.taskbarRect().height;\r
-            taskbarWidth = systrayBackend.taskbarRect().width;\r
-        }\r
+        var tbOrientation = systrayBackend.taskbarOrientation();\r
+        var taskbarRect = systrayBackend.taskbarRect();\r
 \r
         switch(tbOrientation) {\r
             // Platform separation here: Windows and macOS draw coordinates have to be given in screen-coordinates\r
@@ -60,43 +32,43 @@ Window {
             case 0:\r
                 console.debug("Taskbar is on the bottom.");\r
                 trayWindowX = trayIconCenter.x - trayWindow.width / 2;\r
-                trayWindowY = (Qt.platform.os !== "linux") ? (Screen.height - taskbarHeight - trayWindow.height - 4)\r
-                                                           : (Screen.height + Screen.virtualY - taskbarHeight - trayWindow.height - 4);\r
+                trayWindowY = (Qt.platform.os !== "linux") ? (Screen.height - taskbarRect.height - trayWindow.height - 4)\r
+                                                           : (Screen.height + Screen.virtualY - taskbarRect.height - trayWindow.height - 4);\r
                 break;\r
             case 1:\r
                 console.debug("Taskbar is on the left.");\r
-                trayWindowX = (Qt.platform.os !== "linux") ? (taskbarWidth + 4)\r
-                                                           : (Screen.virtualX + taskbarWidth + 4);\r
+                trayWindowX = (Qt.platform.os !== "linux") ? (taskbarRect.width + 4)\r
+                                                           : (Screen.virtualX + taskbarRect.width + 4);\r
                 trayWindowY = trayIconCenter.y;\r
                 break;\r
             case 2:\r
                 console.debug("Taskbar is on the top.");\r
                 trayWindowX = trayIconCenter.x - trayWindow.width / 2;\r
-                trayWindowY = Screen.virtualY + taskbarHeight + 4;\r
+                trayWindowY = Screen.virtualY + taskbarRect.height + 4;\r
                 break;\r
             case 3:\r
                 console.debug("Taskbar is on the right.");\r
-                trayWindowX = (Qt.platform.os !== "linux") ? (Screen.width - taskbarWidth - trayWindow.width - 4)\r
-                                                           : (Screen.width + Screen.virtualX - taskbarWidth - trayWindow.width - 4);\r
+                trayWindowX = (Qt.platform.os !== "linux") ? (Screen.width - taskbarRect.width - trayWindow.width - 4)\r
+                                                           : (Screen.width + Screen.virtualX - taskbarRect.width - trayWindow.width - 4);\r
                 trayWindowY = trayIconCenter.y;\r
                 break;\r
         }\r
 \r
-        console.debug("Screen.height:",Screen.height);\r
-        console.debug("Screen.desktopAvailableHeight:",Screen.desktopAvailableHeight);\r
-        console.debug("Screen.virtualY:",Screen.virtualY);\r
-        console.debug("Screen.width:",Screen.width);\r
-        console.debug("Screen.desktopAvailableWidth:",Screen.desktopAvailableWidth);\r
-        console.debug("Screen.virtualX:",Screen.virtualX);\r
-        console.debug("Taskbar height:",taskbarHeight);\r
-        console.debug("Taskbar width:",taskbarWidth);\r
+        console.debug("Screen.height:", Screen.height);\r
+        console.debug("Screen.desktopAvailableHeight:", Screen.desktopAvailableHeight);\r
+        console.debug("Screen.virtualY:", Screen.virtualY);\r
+        console.debug("Screen.width:", Screen.width);\r
+        console.debug("Screen.desktopAvailableWidth:", Screen.desktopAvailableWidth);\r
+        console.debug("Screen.virtualX:", Screen.virtualX);\r
+        console.debug("Taskbar height:", taskbarRect.height);\r
+        console.debug("Taskbar width:", taskbarRect.width);\r
 \r
         if (Screen.width <= trayWindowX + trayWindow.width) {\r
             console.debug("Out-of-screen condition on the right detected. Adjusting window position.");\r
             if (Qt.platform.os !== "linux") {\r
                 trayWindowX = Screen.width - trayWindow.width - 4;\r
             } else {\r
-                trayWindowX = Screen.width + Screen.virtualX - trayWindow.width - 4 - (tbOrientation === 3 ? taskbarWidth : 0);\r
+                trayWindowX = Screen.width + Screen.virtualX - trayWindow.width - 4 - (tbOrientation === 3 ? taskbarRect.width : 0);\r
             }\r
         }\r
         if (trayWindowX <= Screen.x && Qt.platform.os !== "linux") {\r
@@ -105,7 +77,7 @@ Window {
         }\r
         if (trayWindowX <= Screen.virtualX && Qt.platform.os === "linux") {\r
            console.debug("Out-of-screen condition on the left detected. Adjusting window position.");\r
-           trayWindowX = Screen.virtualX + 4 + (tbOrientation === 1 ? taskbarWidth : 0)\r
+           trayWindowX = Screen.virtualX + 4 + (tbOrientation === 1 ? taskbarRect.width : 0)\r
         }\r
         if (trayWindowY <= Screen.y && Qt.platform.os !== "linux") {\r
             console.debug("Out-of-screen condition on the top detected. Adjusting window position.");\r
@@ -113,7 +85,7 @@ Window {
         }\r
         if (trayWindowY <= Screen.virtualY && Qt.platform.os === "linux") {\r
             console.debug("Out-of-screen condition on the top detected. Adjusting window position.");\r
-            trayWindowY = Screen.virtualY + 4 + (tbOrientation === 2 ? taskbarHeight : 0);\r
+            trayWindowY = Screen.virtualY + 4 + (tbOrientation === 2 ? taskbarRect.height : 0);\r
         }\r
         if (Screen.height <= trayWindowY - Screen.virtualY + trayWindow.height) {\r
             console.debug("Out-of-screen condition on the bottom detected. Adjusting window position.");\r