Implemented platform agnostic tray window positioning logic
authorDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Mon, 30 Dec 2019 13:14:28 +0000 (14:14 +0100)
committerDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Mon, 30 Dec 2019 13:14:28 +0000 (14:14 +0100)
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 97f51370625042ff423c3fc22e7e93caffe0eff2..520450dd98297e7e1cff460b35b108f625120b09 100644 (file)
 #include "config.h"
 #include "tray/UserModel.h"
 
+#include <QGuiApplication>
 #include <QQmlComponent>
 #include <QQmlEngine>
+#include <QScreen>
 
 #ifdef USE_FDO_NOTIFICATIONS
 #include <QDBusConnection>
@@ -97,4 +99,60 @@ void Systray::setToolTip(const QString &tip)
     QSystemTrayIcon::setToolTip(tr("%1: %2").arg(Theme::instance()->appNameGUI(), tip));
 }
 
+int Systray::calcTrayWindowX()
+{
+    QScreen* trayScreen = QGuiApplication::screenAt(this->geometry().topRight());
+    // get coordinates from top center point of tray icon
+    int trayIconTopCenterX = (this->geometry().topRight() - ((this->geometry().topRight() - this->geometry().topLeft()) * 0.5)).x();
+    int trayIconTopCenterY = (this->geometry().topRight() - ((this->geometry().topRight() - this->geometry().topLeft()) * 0.5)).y();
+
+    if ( (trayScreen->geometry().width() - trayIconTopCenterX) < (trayScreen->geometry().width() * 0.5) ) {
+        // tray icon is on right side of the screen
+        if ( ((trayScreen->geometry().width() - trayIconTopCenterX) < trayScreen->geometry().height() - trayIconTopCenterY)
+            && ((trayScreen->geometry().width() - trayIconTopCenterX) < trayIconTopCenterY) ) {
+            // taskbar is on the right
+            return trayScreen->availableSize().width() - 400 - 6;
+        } else {
+            // taskbar is on the bottom or top
+            if (trayIconTopCenterX - (400 * 0.5) < 0) {
+                return 6;
+            } else if (trayIconTopCenterX - (400 * 0.5) > trayScreen->geometry().width()) {
+                return trayScreen->geometry().width() - 406;
+            } else {
+                return trayIconTopCenterX - (400 * 0.5);
+            }
+        }
+    } else {
+        // tray icon is on left side of the screen
+        return (trayScreen->geometry().width() - trayScreen->availableGeometry().width()) + 6;
+    }
+}
+int Systray::calcTrayWindowY()
+{
+    QScreen *trayScreen = QGuiApplication::screenAt(this->geometry().topRight());
+    // get coordinates from top center point of tray icon
+    int trayIconTopCenterX = (this->geometry().topRight() - ((this->geometry().topRight() - this->geometry().topLeft()) * 0.5)).x();
+    int trayIconTopCenterY = (this->geometry().topRight() - ((this->geometry().topRight() - this->geometry().topLeft()) * 0.5)).y();
+
+    if ( (trayScreen->geometry().height() - trayIconTopCenterY) < (trayScreen->geometry().height() * 0.5) ) {
+        // tray icon is on bottom side of the screen
+        if ( ((trayScreen->geometry().height() - trayIconTopCenterY) < trayScreen->geometry().width() - trayIconTopCenterX )
+            && ((trayScreen->geometry().height() - trayIconTopCenterY) < trayIconTopCenterX) ) {
+            // taskbar is on the bottom
+            return trayScreen->availableSize().height() - 500 - 6;
+        } else {
+            // taskbar is on the right or left
+            if (trayIconTopCenterY - 500 > 0) {
+                return trayIconTopCenterY - 500;
+            } else {
+                return 6;
+            }
+        }
+    } else {
+        // tray icon is on the top
+        return (trayScreen->geometry().height() - trayScreen->availableGeometry().height()) + 6;
+    }
+}
+
+
 } // namespace OCC
index dde45be1a9d621632b5f14fbbbb18da964259fef..f99f024e673eeba50526d4148f1fae106b51a844 100644 (file)
@@ -48,6 +48,9 @@ public:
     void showMessage(const QString &title, const QString &message, MessageIcon icon = Information, int millisecondsTimeoutHint = 10000);
     void setToolTip(const QString &tip);
 
+    Q_INVOKABLE int calcTrayWindowX();
+    Q_INVOKABLE int calcTrayWindowY();
+
 signals:
     void currentUserChanged();
 
index 12eb10e7fa21004132b3bf1a1a94ee67abc92ae1..b01df1cf585e6b2acdbbd061f1df75f6f594f07a 100644 (file)
@@ -20,14 +20,6 @@ Window {
         }\r
     }\r
 \r
-    Component.onCompleted: {\r
-        /* desktopAvailableWidth and Height doesn't include the system tray bar\r
-               but breaks application anyway on windows when using multi monitor setup,\r
-               will look for a better solution later, for now just get this thing complete */\r
-        //setX(Screen.desktopAvailableWidth - width);\r
-        //setY(Screen.desktopAvailableHeight + height);\r
-    }\r
-\r
     Connections {\r
         target: userModelBackend\r
         onRefreshCurrentUserGui: {\r
@@ -45,6 +37,8 @@ Window {
         onShowWindow: {\r
             trayWindow.show();\r
             trayWindow.requestActivate();\r
+            trayWindow.setX( systrayBackend.calcTrayWindowX());\r
+            trayWindow.setY( systrayBackend.calcTrayWindowY());\r
         }\r
         onHideWindow: {\r
             trayWindow.hide();\r