diff --git a/main.cpp b/main.cpp index d729549f..1e5652d7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -46,6 +47,21 @@ int main(int argc, char *argv[]) application.setOrganizationName("Yubico"); application.setOrganizationDomain("com.yubico"); + // Get x and y coordinates of all monitors + QVariantList monitorAreas; + for (QScreen* screen : QGuiApplication::screens()) { + QRect monitorArea = screen->geometry(); + + QVariantMap coordinates; + + coordinates.insert("xMin", monitorArea.x()); + coordinates.insert("xMax", monitorArea.x() + monitorArea.width()); + coordinates.insert("yMin", monitorArea.y()); + coordinates.insert("yMax", monitorArea.y() + monitorArea.height()); + + monitorAreas << coordinates; + } + QQuickStyle::setStyle("Material"); QCommandLineParser cliParser; @@ -97,6 +113,7 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("appVersion", APP_VERSION); engine.rootContext()->setContextProperty("ScreenShot", &screenshot); engine.rootContext()->setContextProperty("application", &application); + engine.rootContext()->setContextProperty("monitorAreas", monitorAreas); engine.load(QUrl(url_prefix + main_qml)); diff --git a/qml/main.qml b/qml/main.qml index 6f7fb946..2f470654 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -139,6 +139,30 @@ ApplicationWindow { && (settings.desktopAvailableHeight === Screen.desktopAvailableHeight) app.x = (savedScreenLayout) ? settings.x : Screen.width / 2 - app.width / 2 app.y = (savedScreenLayout) ? settings.y : Screen.height / 2 - app.height / 2 + + function isWindowPositionInsideSomeMonitor() { + for (var i = 0; i < monitorAreas.length; i++) { + var xMin = monitorAreas[i].xMin + var xMax = monitorAreas[i].xMax + var yMin = monitorAreas[i].yMin + var yMax = monitorAreas[i].yMax + + if (app.x > xMin && app.x < xMax) { + if (app.y > yMin && app.y < yMax) { + return true + } + } + } + return false + } + + // If app.x and app.y are outside of the available screen geometry, + // put the app in the middle of the screen. + if (!isWindowPositionInsideSomeMonitor()) { + app.x = Screen.width / 2 - app.width / 2 + app.y = Screen.height / 2 - app.height / 2 + } + } function updateTrayVisibility() {