aboutsummaryrefslogtreecommitdiff
path: root/src/gui/kwindowconfig.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/kwindowconfig.cpp')
-rw-r--r--src/gui/kwindowconfig.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/gui/kwindowconfig.cpp b/src/gui/kwindowconfig.cpp
index 2f787a2f..07f1a307 100644
--- a/src/gui/kwindowconfig.cpp
+++ b/src/gui/kwindowconfig.cpp
@@ -7,6 +7,7 @@
#include "kwindowconfig.h"
+#include <QGuiApplication>
#include <QScreen>
#include <QWindow>
@@ -71,3 +72,60 @@ void KWindowConfig::restoreWindowSize(QWindow *window, const KConfigGroup &confi
window->setWindowState(Qt::WindowMaximized);
}
}
+
+void KWindowConfig::saveWindowPosition(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options)
+{
+ // On Wayland, the compositor is solely responsible for window positioning,
+ // So this needs to be a no-op
+ if (!window || QGuiApplication::platformName() == QStringLiteral("wayland")) {
+ return;
+ }
+
+ // Prepend the names of all connected screens so that we save the position
+ // on a per-screen-arrangement basis, since people often like to have
+ // windows positioned differently depending on their screen arrangements
+ QStringList names;
+ const auto screens = QGuiApplication::screens();
+ names.reserve(screens.length());
+ for (auto screen : screens) {
+ names << screen->name();
+ }
+ const QString allScreens = names.join(QStringLiteral(" "));
+ config.writeEntry(allScreens + QStringLiteral(" XPosition"), window->x(), options);
+ config.writeEntry(allScreens + QStringLiteral(" YPosition"), window->y(), options);
+}
+
+void KWindowConfig::restoreWindowPosition(QWindow *window, const KConfigGroup &config)
+{
+ // On Wayland, the compositor is solely responsible for window positioning,
+ // So this needs to be a no-op
+ if (!window || QGuiApplication::platformName() == QStringLiteral("wayland")) {
+ return;
+ }
+
+ const QRect desk = window->screen()->geometry();
+ const bool isMaximized = config.readEntry(QStringLiteral("Window-Maximized %1x%2").arg(desk.height()).arg(desk.width()), false);
+
+ // Don't need to restore position if the window was maximized
+ if (isMaximized) {
+ window->setWindowState(Qt::WindowMaximized);
+ return;
+ }
+
+ QStringList names;
+ const auto screens = QGuiApplication::screens();
+ names.reserve(screens.length());
+ for (auto screen : screens) {
+ names << screen->name();
+ }
+ const QString allScreens = names.join(QStringLiteral(" "));
+ const int xPos = config.readEntry(allScreens + QStringLiteral(" XPosition"), -1);
+ const int yPos = config.readEntry(allScreens + QStringLiteral(" YPosition"), -1);
+
+ if (xPos == -1 || yPos == -1) {
+ return;
+ }
+
+ window->setX(xPos);
+ window->setY(yPos);
+}