QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
windowstatestore.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#include "windowstatestore.h"
9#include "qtpasssettings.h"
10
11#include <QCursor>
12#include <QDialog>
13#include <QGuiApplication>
14#include <QScreen>
15#include <QWidget>
16
17namespace {
18
24auto onWayland() -> bool {
25 return QGuiApplication::platformName().startsWith(QLatin1String("wayland"));
26}
27
28} // namespace
29
31
32auto restore(QWidget &window, const QString &key) -> bool {
33 const QByteArray geometry = QtPassSettings::getDialogGeometry(key);
34 if (!geometry.isEmpty() && window.restoreGeometry(geometry)) {
35 return true;
36 }
38 return false;
39}
40
41void save(const QWidget &window, const QString &key) {
42 QtPassSettings::setDialogGeometry(key, window.saveGeometry());
43}
44
45void attach(QDialog &dialog, const QString &key) {
46 restore(dialog, key);
47 QObject::connect(&dialog, &QDialog::finished, &dialog,
48 [&dialog, key] { save(dialog, key); });
49}
50
51void centreOnCursorScreen(QWidget &window) {
52 if (onWayland()) {
53 return;
54 }
55 QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
56 if (screen == nullptr) {
57 screen = QGuiApplication::primaryScreen();
58 }
59 if (screen == nullptr) {
60 return;
61 }
62 QRect frame = window.frameGeometry();
63 frame.moveCenter(screen->availableGeometry().center());
64 window.move(frame.topLeft());
65}
66
67} // namespace WindowStateStore
static void setDialogGeometry(const QString &key, const QByteArray &geometry)
Save dialog geometry.
static auto getDialogGeometry(const QString &key, const QByteArray &defaultValue=QVariant().toByteArray()) -> QByteArray
Get saved dialog geometry.
Persist and restore a top-level window's geometry, one way for the main window and every dialog.
void save(const QWidget &window, const QString &key)
Save window's geometry under key.
auto restore(QWidget &window, const QString &key) -> bool
Restore window's geometry saved under key.
void centreOnCursorScreen(QWidget &window)
Move window so its frame is centred on the screen under the mouse pointer (the primary screen when th...
void attach(QDialog &dialog, const QString &key)
restore() now, save() whenever the dialog finishes.
QtPassSettings, the QSettings singleton behind every saved setting, the profiles and the Pass backend...
WindowStateStore: saves and restores top-level window geometry through QtPassSettings,...