Skip to content

Commit

Permalink
Merge pull request #6353 from nextcloud/bugfix/window-pos-compute
Browse files Browse the repository at this point in the history
Simplify window positioning compute, more flexibly handle different available geometries
  • Loading branch information
mgallien authored Jan 29, 2024
2 parents 120d67c + b1a50f4 commit b977472
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 49 deletions.
68 changes: 20 additions & 48 deletions src/gui/systray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ void Systray::positionWindowAtScreenCenter(QQuickWindow *window) const
if(!useNormalWindow()) {
window->setScreen(currentScreen());
const QPoint windowAdjustment(window->geometry().width() / 2, window->geometry().height() / 2);
const auto position = currentScreen()->virtualGeometry().center() - windowAdjustment;
const auto position = currentScreen()->availableGeometry().center() - windowAdjustment;
window->setPosition(position);
}
}
Expand Down Expand Up @@ -696,72 +696,50 @@ Systray::TaskBarPosition Systray::taskbarOrientation() const
#endif
}

// TODO: Get real taskbar dimensions Linux as well
QRect Systray::taskbarGeometry() const
QRect Systray::currentScreenRect() const
{
#if defined(Q_OS_WIN)
QRect tbRect = Utility::getTaskbarDimensions();
//QML side expects effective pixels, convert taskbar dimensions if necessary
auto pixelRatio = currentScreen()->devicePixelRatio();
if (pixelRatio != 1) {
tbRect.setHeight(tbRect.height() / pixelRatio);
tbRect.setWidth(tbRect.width() / pixelRatio);
}
return tbRect;
#elif defined(Q_OS_MACOS)
const auto screenWidth = currentScreenRect().width();
const auto statusBarHeight = static_cast<int>(OCC::menuBarThickness());
return {0, 0, screenWidth, statusBarHeight};
#else
if (taskbarOrientation() == TaskBarPosition::Bottom || taskbarOrientation() == TaskBarPosition::Top) {
auto screenWidth = currentScreenRect().width();
return {0, 0, screenWidth, 32};
} else {
auto screenHeight = currentScreenRect().height();
return {0, 0, 32, screenHeight};
}
#endif
const auto screen = currentScreen();
Q_ASSERT(screen);
return screen->geometry();
}

QRect Systray::currentScreenRect() const
QRect Systray::currentAvailableScreenRect() const
{
const auto screen = currentScreen();
Q_ASSERT(screen);
return screen->geometry();
return screen->availableGeometry();
}

QPoint Systray::computeWindowReferencePoint() const
{
constexpr auto spacing = 4;
const auto trayIconCenter = calcTrayIconCenter();
const auto taskbarRect = taskbarGeometry();
const auto taskbarScreenEdge = taskbarOrientation();
const auto screenRect = currentScreenRect();
const auto screenRect = currentAvailableScreenRect();

qCDebug(lcSystray) << "screenRect:" << screenRect;
qCDebug(lcSystray) << "taskbarRect:" << taskbarRect;
qCDebug(lcSystray) << "taskbarScreenEdge:" << taskbarScreenEdge;
qCDebug(lcSystray) << "trayIconCenter:" << trayIconCenter;

switch(taskbarScreenEdge) {
case TaskBarPosition::Bottom:
return {
trayIconCenter.x(),
screenRect.bottom() - taskbarRect.height() - spacing
screenRect.bottom() - spacing
};
case TaskBarPosition::Left:
return {
screenRect.left() + taskbarRect.width() + spacing,
screenRect.left() + spacing,
trayIconCenter.y()
};
case TaskBarPosition::Top:
return {
trayIconCenter.x(),
screenRect.top() + taskbarRect.height() + spacing
screenRect.top() + spacing
};
case TaskBarPosition::Right:
return {
screenRect.right() - taskbarRect.width() - spacing,
screenRect.right() - spacing,
trayIconCenter.y()
};
}
Expand All @@ -772,51 +750,45 @@ QPoint Systray::computeNotificationReferencePoint(int spacing, NotificationPosit
{
auto trayIconCenter = calcTrayIconCenter();
auto taskbarScreenEdge = taskbarOrientation();
auto taskbarRect = taskbarGeometry();
const auto screenRect = currentScreenRect();
const auto screenRect = currentAvailableScreenRect();

if(position == NotificationPosition::TopLeft) {
taskbarScreenEdge = TaskBarPosition::Top;
trayIconCenter = QPoint(0, 0);
taskbarRect = QRect(0, 0, screenRect.width(), 32);
} else if(position == NotificationPosition::TopRight) {
taskbarScreenEdge = TaskBarPosition::Top;
trayIconCenter = QPoint(screenRect.width(), 0);
taskbarRect = QRect(0, 0, screenRect.width(), 32);
} else if(position == NotificationPosition::BottomLeft) {
taskbarScreenEdge = TaskBarPosition::Bottom;
trayIconCenter = QPoint(0, screenRect.height());
taskbarRect = QRect(0, 0, screenRect.width(), 32);
} else if(position == NotificationPosition::BottomRight) {
taskbarScreenEdge = TaskBarPosition::Bottom;
trayIconCenter = QPoint(screenRect.width(), screenRect.height());
taskbarRect = QRect(0, 0, screenRect.width(), 32);
}

qCDebug(lcSystray) << "screenRect:" << screenRect;
qCDebug(lcSystray) << "taskbarRect:" << taskbarRect;
qCDebug(lcSystray) << "taskbarScreenEdge:" << taskbarScreenEdge;
qCDebug(lcSystray) << "trayIconCenter:" << trayIconCenter;

switch(taskbarScreenEdge) {
case TaskBarPosition::Bottom:
return {
trayIconCenter.x() < screenRect.center().x() ? screenRect.left() + spacing : screenRect.right() - spacing,
screenRect.bottom() - taskbarRect.height() - spacing
screenRect.bottom() - spacing
};
case TaskBarPosition::Left:
return {
screenRect.left() + taskbarRect.width() + spacing,
screenRect.left() + spacing,
trayIconCenter.y() < screenRect.center().y() ? screenRect.top() + spacing : screenRect.bottom() - spacing
};
case TaskBarPosition::Top:
return {
trayIconCenter.x() < screenRect.center().x() ? screenRect.left() + spacing : screenRect.right() - spacing,
screenRect.top() + taskbarRect.height() + spacing
screenRect.top() + spacing
};
case TaskBarPosition::Right:
return {
screenRect.right() - taskbarRect.width() - spacing,
screenRect.right() - spacing,
trayIconCenter.y() < screenRect.center().y() ? screenRect.top() + spacing : screenRect.bottom() - spacing
};
}
Expand Down Expand Up @@ -925,11 +897,11 @@ QPoint Systray::computeNotificationPosition(int width, int height, int spacing,

QPoint Systray::calcTrayIconCenter() const
{
if(geometry().isValid()) {
const auto geo = geometry();
if(geo.isValid()) {
// QSystemTrayIcon::geometry() is broken for ages on most Linux DEs (invalid geometry returned)
// thus we can use this only for Windows and macOS
auto trayIconCenter = geometry().center();
return trayIconCenter;
return geo.center();
}

// On Linux, fall back to mouse position (assuming tray icon is activated by mouse click)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ private slots:

[[nodiscard]] QScreen *currentScreen() const;
[[nodiscard]] QRect currentScreenRect() const;
[[nodiscard]] QRect currentAvailableScreenRect() const;
[[nodiscard]] QPoint computeWindowReferencePoint() const;
[[nodiscard]] QPoint computeNotificationReferencePoint(int spacing = 20, NotificationPosition position = NotificationPosition::Default) const;
[[nodiscard]] QPoint calcTrayIconCenter() const;
[[nodiscard]] TaskBarPosition taskbarOrientation() const;
[[nodiscard]] QRect taskbarGeometry() const;
[[nodiscard]] QRect computeWindowRect(int spacing, const QPoint &topLeft, const QPoint &bottomRight) const;
[[nodiscard]] QPoint computeWindowPosition(int width, int height) const;
[[nodiscard]] QPoint computeNotificationPosition(int width, int height, int spacing = 20, NotificationPosition position = NotificationPosition::Default) const;
Expand Down

0 comments on commit b977472

Please sign in to comment.