From: Kevin Ottens Date: Wed, 20 May 2020 18:03:08 +0000 (+0200) Subject: Get rid of screenIndex X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~222^2^2~160^2~5 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=40824dc42740fd9a6e67f20bbd3dcdc82efbca3c;p=nextcloud-desktop.git Get rid of screenIndex 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 --- diff --git a/src/gui/systray.cpp b/src/gui/systray.cpp index 5db933983..15f18ba66 100644 --- a/src/gui/systray.cpp +++ b/src/gui/systray.cpp @@ -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 diff --git a/src/gui/systray.h b/src/gui/systray.h index bc1923707..634d0a154 100644 --- a/src/gui/systray.h +++ b/src/gui/systray.h @@ -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; diff --git a/src/gui/tray/Window.qml b/src/gui/tray/Window.qml index 505875808..9e11828b8 100644 --- a/src/gui/tray/Window.qml +++ b/src/gui/tray/Window.qml @@ -63,7 +63,7 @@ Window { onShowWindow: { accountMenu.close(); - trayWindow.screen = Qt.application.screens[systrayBackend.screenIndex()]; + trayWindow.screen = systrayBackend.currentScreenVar(); var position = systrayBackend.computeWindowPosition(trayWindow.width, trayWindow.height) trayWindow.x = position.x