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 44 : auto onWayland() -> bool {
25 44 : return QGuiApplication::platformName().startsWith(QLatin1String("wayland"));
26 : }
27 :
28 : } // namespace
29 :
30 : namespace WindowStateStore {
31 :
32 139 : auto restore(QWidget &window, const QString &key) -> bool {
33 278 : const QByteArray geometry = QtPassSettings::getDialogGeometry(key);
34 139 : if (!geometry.isEmpty() && window.restoreGeometry(geometry)) {
35 : return true;
36 : }
37 40 : centreOnCursorScreen(window);
38 : return false;
39 : }
40 :
41 32 : void save(const QWidget &window, const QString &key) {
42 32 : QtPassSettings::setDialogGeometry(key, window.saveGeometry());
43 32 : }
44 :
45 139 : void attach(QDialog &dialog, const QString &key) {
46 139 : restore(dialog, key);
47 139 : QObject::connect(&dialog, &QDialog::finished, &dialog,
48 310 : [&dialog, key] { save(dialog, key); });
49 139 : }
50 :
51 44 : void centreOnCursorScreen(QWidget &window) {
52 44 : if (onWayland()) {
53 0 : return;
54 : }
55 44 : QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
56 44 : if (screen == nullptr) {
57 0 : screen = QGuiApplication::primaryScreen();
58 : }
59 0 : if (screen == nullptr) {
60 : return;
61 : }
62 44 : QRect frame = window.frameGeometry();
63 44 : frame.moveCenter(screen->availableGeometry().center());
64 44 : window.move(frame.topLeft());
65 : }
66 :
67 : } // namespace WindowStateStore
|