Fix wrong window position on some linux DEs - worked around invalid geometry() return...
authorDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Tue, 21 Jan 2020 08:55:26 +0000 (09:55 +0100)
committerDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Tue, 21 Jan 2020 08:55:26 +0000 (09:55 +0100)
Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
src/gui/systray.cpp

index 6134adceec74314e983577fe2464862223817358..3527793c64ea9015134a1b627eec28c8a16bedc9 100644 (file)
@@ -18,6 +18,7 @@
 #include "config.h"
 #include "tray/UserModel.h"
 
+#include <QCursor>
 #include <QDesktopServices>
 #include <QGuiApplication>
 #include <QQmlComponent>
@@ -146,12 +147,21 @@ int Systray::calcTrayWindowX()
     int screenHeight = trayScreen->geometry().height();
     int availableWidth = trayScreen->availableGeometry().width();
     int availableHeight = trayScreen->availableGeometry().height();
-    QPoint topRightDpiAware = this->geometry().topRight() / trayScreen->devicePixelRatio();
-    QPoint topLeftDpiAware = this->geometry().topLeft() / trayScreen->devicePixelRatio();
 
-    // get coordinates from top center point of tray icon
+    QPoint topRightDpiAware = QPoint();
+    QPoint topLeftDpiAware = QPoint();
+    if (this->geometry().left() == 0 || this->geometry().top() == 0) {
+        // tray geometry is invalid - QT bug on some linux desktop environments
+        // Use mouse position instead. Cringy, but should work for now
+        topRightDpiAware = QCursor::pos() / trayScreen->devicePixelRatio();
+        topLeftDpiAware = QCursor::pos() / trayScreen->devicePixelRatio();
+    } else {
+        topRightDpiAware = this->geometry().topRight() / trayScreen->devicePixelRatio();
+        topLeftDpiAware = this->geometry().topLeft() / trayScreen->devicePixelRatio();
+    }
+
+    // get x coordinate from top center point of tray icon
     int trayIconTopCenterX = (topRightDpiAware - ((topRightDpiAware - topLeftDpiAware) * 0.5)).x();
-    int trayIconTopCenterY = (topRightDpiAware - ((topRightDpiAware - topLeftDpiAware) * 0.5)).y();
 
     if (availableHeight < screenHeight) {
         // taskbar is on top or bottom
@@ -182,11 +192,19 @@ int Systray::calcTrayWindowY()
     int screenWidth = trayScreen->geometry().width();
     int screenHeight = trayScreen->geometry().height();
     int availableHeight = trayScreen->availableGeometry().height();
-    QPoint topRightDpiAware = this->geometry().topRight() / trayScreen->devicePixelRatio();
-    QPoint topLeftDpiAware = this->geometry().topLeft() / trayScreen->devicePixelRatio();
 
-    // get coordinates from top center point of tray icon
-    int trayIconTopCenterX = (topRightDpiAware - ((topRightDpiAware - topLeftDpiAware) * 0.5)).x();
+    QPoint topRightDpiAware = QPoint();
+    QPoint topLeftDpiAware = QPoint();
+    if (this->geometry().left() == 0 || this->geometry().top() == 0) {
+        // tray geometry is invalid - QT bug on some linux desktop environments
+        // Use mouse position instead. Cringy, but should work for now
+        topRightDpiAware = QCursor::pos() / trayScreen->devicePixelRatio();
+        topLeftDpiAware = QCursor::pos() / trayScreen->devicePixelRatio();
+    } else {
+        topRightDpiAware = this->geometry().topRight() / trayScreen->devicePixelRatio();
+        topLeftDpiAware = this->geometry().topLeft() / trayScreen->devicePixelRatio();
+    }
+    // get y coordinate from top center point of tray icon
     int trayIconTopCenterY = (topRightDpiAware - ((topRightDpiAware - topLeftDpiAware) * 0.5)).y();
 
     if (availableHeight < screenHeight) {