Use cursor position to position popup window

This commit is contained in:
Stephen Shelton 2020-01-08 11:36:24 -08:00
parent 5e02c9fc48
commit b144c1d5a6
3 changed files with 40 additions and 37 deletions

View file

@ -32,46 +32,40 @@ ApplicationWindow {
function display() {
if (platformDetails.isLinux()) {
// TODO: this allegedly returns 0,0 for x,y in some cases (e.g. some Linux WMs)
// so for now, we just hope the WM can do something intelligent for us
// (e.g. place the window near the mouse pointer)
var rect = platformDetails.getAbsoluteCursorPosition();
console.log("mouse cursor at: "+ rect.x +", "+ rect.y);
} else if (systray.geometry) {
// if Qt / our OS will at least give us systray geometry, use it to set the
// position of our systray icon in its own screen coordinates
const rect = systray.geometry;
console.log("icon at: "+ rect.x + ", "+ rect.y);
console.log("screen: "+ Screen.width +", "+ Screen.height);
// attempt to determine what quadrant the systray is positioned in
// and anchor the window to the opposite corner of the systray icon
// TODO: this technique is flawed -- it often leaves the window partially obscured
// by the taskbar (esp. when the taskbar is oriented horizontally)
var right = false;
if ((rect.x / Screen.width) >= 0.5) {
right = true;
}
var bottom = false;
if ((rect.y / Screen.height) >= 0.5) {
bottom = true;
}
// on the right side, shift left by (window width)
// on the left side, shift right by (systray icon width)
// on the bottom side, shift up by (window height)
// on the top side, shift down by systray icon height)
var winX = (right ? (rect.x - window.width) : (rect.x + rect.width));
var winY = (bottom ? (rect.y - window.height) : (rect.y + rect.height));
window.x = winX;
window.y = winY;
console.log("updated window popup position: "+ window.x + ", "+ window.y);
// attempt to determine what quadrant the systray is positioned in
// and anchor the window to the opposite corner of the systray icon
// TODO: this technique is flawed -- it often leaves the window partially obscured
// by the taskbar (esp. when the taskbar is oriented horizontally)
var right = false;
if ((rect.x / Screen.width) >= 0.5) {
right = true;
}
var bottom = false;
if ((rect.y / Screen.height) >= 0.5) {
bottom = true;
}
console.log("right: "+ right);
console.log("bottom: "+ bottom);
console.log("window size: "+ window.width +"x"+ window.height);
// on the right side, shift left by (window width)
// on the left side, shift right by (systray icon width)
// on the bottom side, shift up by (window height)
// on the top side, shift down by systray icon height)
var winX = (right ? (rect.x - window.width) : rect.x);
var winY = (bottom ? (rect.y - window.height) : rect.y);
window.x = winX;
window.y = winY;
console.log("updated window popup position: "+ window.x + ", "+ window.y);
window.show();
window.raise();
window.requestActivate();

View file

@ -1,5 +1,8 @@
#include "PlatformDetails.hpp"
#include <QPoint>
#include <QCursor>
// PlatformDetails::isWindows
Q_INVOKABLE bool PlatformDetails::isWindows() {
#if defined(Q_OS_WINDOWS)
@ -58,3 +61,6 @@ Q_INVOKABLE bool PlatformDetails::isLokinetRunning() {
return (status == LokinetProcessManager::ProcessStatus::Running);
}
Q_INVOKABLE QPoint PlatformDetails::getAbsoluteCursorPosition() {
return QCursor::pos();
}

View file

@ -2,6 +2,7 @@
#define __LOKI_PLATFORM_DETAILS_HPP__
#include <QObject>
#include <QPoint>
#include "process/LokinetProcessManager.hpp"
@ -28,6 +29,8 @@ public:
Q_INVOKABLE static bool forciblyStopLokinetProcess();
Q_INVOKABLE static bool managedStopLokinetProcess();
Q_INVOKABLE static bool isLokinetRunning();
Q_INVOKABLE static QPoint getAbsoluteCursorPosition();
};
#endif // __LOKI_PLATFORM_DETAILS_HPP__