Merge pull request #541 from Yubico/monitor-check

Make sure the application is always visible on a monitor
This commit is contained in:
Dennis Fokin 2020-02-18 02:54:52 -08:00 committed by GitHub
commit 2dda96e4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include <QApplication>
#include <QDesktopWidget>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <stdlib.h>
@ -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));

View File

@ -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() {