Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Implements WindowStateStore on top of QWidget::saveGeometry() /
6 : : * restoreGeometry(); the Wayland check lives in the local onWayland().
7 : : */
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 : :
17 : : namespace {
18 : :
19 : : /**
20 : : * @brief Whether the application runs on a Wayland platform plugin, where
21 : : * client-side window placement is ignored by the compositor.
22 : : * @return true for any platform name starting with "wayland".
23 : : */
24 : 58 : auto onWayland() -> bool {
25 : 58 : return QGuiApplication::platformName().startsWith(QLatin1String("wayland"));
26 : : }
27 : :
28 : : } // namespace
29 : :
30 : : namespace WindowStateStore {
31 : :
32 : 194 : auto restore(QWidget &window, const QString &key) -> bool {
33 : 388 : const QByteArray geometry = QtPassSettings::getDialogGeometry(key);
34 [ + + + - : 194 : if (!geometry.isEmpty() && window.restoreGeometry(geometry)) {
+ ]
35 : : return true;
36 : : }
37 : 54 : centreOnCursorScreen(window);
38 : : return false;
39 : : }
40 : :
41 : 43 : void save(const QWidget &window, const QString &key) {
42 : 43 : QtPassSettings::setDialogGeometry(key, window.saveGeometry());
43 : 43 : }
44 : :
45 : 194 : void attach(QDialog &dialog, const QString &key) {
46 : 194 : restore(dialog, key);
47 : 194 : QObject::connect(&dialog, &QDialog::finished, &dialog,
48 : 431 : [&dialog, key] { save(dialog, key); });
49 : 194 : }
50 : :
51 : 58 : void centreOnCursorScreen(QWidget &window) {
52 [ + - ]: 58 : if (onWayland()) {
53 : 0 : return;
54 : : }
55 : 58 : QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
56 [ - + ]: 58 : if (screen == nullptr) {
57 : 0 : screen = QGuiApplication::primaryScreen();
58 : : }
59 [ # # ]: 0 : if (screen == nullptr) {
60 : : return;
61 : : }
62 : 58 : QRect frame = window.frameGeometry();
63 : 58 : frame.moveCenter(screen->availableGeometry().center());
64 : 58 : window.move(frame.topLeft());
65 : : }
66 : :
67 : : } // namespace WindowStateStore
|