diff --git a/src/apps/deskflow-gui/MainWindow.cpp b/src/apps/deskflow-gui/MainWindow.cpp index 1e53ec02d..422b2b39a 100644 --- a/src/apps/deskflow-gui/MainWindow.cpp +++ b/src/apps/deskflow-gui/MainWindow.cpp @@ -179,36 +179,33 @@ MainWindow::~MainWindow() void MainWindow::restoreWindow() { - const auto &windowSize = m_appConfig.mainWindowSize(); - if (windowSize.has_value()) { - qDebug() << "restoring main window size"; - m_expandedSize = windowSize.value(); - } - - const auto &windowPosition = m_appConfig.mainWindowPosition(); - if (windowPosition.has_value()) { - int x = 0; - int y = 0; - int w = 0; - int h = 0; - for (auto screen : QGuiApplication::screens()) { - auto geo = screen->geometry(); - x = std::min(geo.x(), x); - y = std::min(geo.y(), y); - w = std::max(geo.x() + geo.width(), w); - h = std::max(geo.y() + geo.height(), h); - } - const QSize totalScreenSize(w, h); - const QPoint point = windowPosition.value(); - if (point.x() < totalScreenSize.width() && point.y() < totalScreenSize.height()) { - qDebug() << "restoring main window position"; - move(point); - } - } else { + const auto windowGeometry = Settings::value(Settings::Gui::WindowGeometry).toRect(); + if (!windowGeometry.isValid()) { // center main window in middle of screen const auto screen = QGuiApplication::primaryScreen(); QRect screenGeometry = screen->geometry(); move(screenGeometry.center() - rect().center()); + return; + } + + m_expandedSize = windowGeometry.size(); + + int x = 0; + int y = 0; + int w = 0; + int h = 0; + const auto screens = QGuiApplication::screens(); + for (auto screen : screens) { + auto geo = screen->geometry(); + x = std::min(geo.x(), x); + y = std::min(geo.y(), y); + w = std::max(geo.x() + geo.width(), w); + h = std::max(geo.y() + geo.height(), h); + } + const QRect screensGeometry(x, y, w, h); + if (screensGeometry.contains(windowGeometry)) { + qDebug() << "restoring main window position"; + move(windowGeometry.topLeft()); } } @@ -863,9 +860,7 @@ void MainWindow::closeEvent(QCloseEvent *event) } if (m_saveOnExit) { - m_appConfig.setMainWindowPosition(pos()); - m_appConfig.setMainWindowSize(size()); - m_configScopes.save(); + Settings::setValue(Settings::Gui::WindowGeometry, frameGeometry()); } qDebug() << "quitting application"; event->accept(); diff --git a/src/lib/common/Settings.cpp b/src/lib/common/Settings.cpp index 350527da1..7cd34dbda 100644 --- a/src/lib/common/Settings.cpp +++ b/src/lib/common/Settings.cpp @@ -7,6 +7,7 @@ #include "Settings.h" #include +#include Settings *Settings::instance() { @@ -78,6 +79,9 @@ QVariant Settings::defaultValue(const QString &key) return true; } + if (key == Gui::WindowGeometry) + return QRect(); + return QVariant(); } diff --git a/src/lib/common/Settings.h b/src/lib/common/Settings.h index 7af7602f6..53cde87a2 100644 --- a/src/lib/common/Settings.h +++ b/src/lib/common/Settings.h @@ -43,6 +43,7 @@ public: inline static const auto CloseToTray = QStringLiteral("gui/closeToTray"); inline static const auto LogExpanded = QStringLiteral("gui/logExpanded"); inline static const auto SymbolicTrayIcon = QStringLiteral("gui/symbolicTrayIcon"); + inline static const auto WindowGeometry = QStringLiteral("gui/windowGeometry"); }; static Settings *instance(); @@ -83,6 +84,7 @@ private: , Gui::CloseToTray , Gui::LogExpanded , Gui::SymbolicTrayIcon + , Gui::WindowGeometry }; // clang-format on }; diff --git a/src/lib/gui/config/AppConfig.cpp b/src/lib/gui/config/AppConfig.cpp index 65c0a742f..ed91b4eeb 100644 --- a/src/lib/gui/config/AppConfig.cpp +++ b/src/lib/gui/config/AppConfig.cpp @@ -72,8 +72,8 @@ const char *const AppConfig::m_SettingsName[] = { "", // 36 = serverClientMode, obsolete "enableService", "", // 38 Moved to deskflow settings - "mainWindowSize", - "mainWindowPosition", + "", // 39 window size moved to deskflow settings + "", // 40 window position moved to deskflow settings "", // 41 = Show dev thanks, obsolete "", // 42 show Close Reminder moved to deskflow settings "", // 43 Moved to deskflow settings @@ -135,9 +135,6 @@ void AppConfig::recallFromCurrentScope() m_TlsCertPath = getFromCurrentScope(kTlsCertPath, m_TlsCertPath).toString(); m_TlsKeyLength = getFromCurrentScope(kTlsKeyLength, m_TlsKeyLength).toInt(); m_RequireClientCert = getFromCurrentScope(kRequireClientCert, m_RequireClientCert).toBool(); - m_MainWindowPosition = - getFromCurrentScope(kMainWindowPosition, [](const QVariant &v) { return v.toPoint(); }); - m_MainWindowSize = getFromCurrentScope(kMainWindowSize, [](const QVariant &v) { return v.toSize(); }); } void AppConfig::recallScreenName() @@ -186,8 +183,6 @@ void AppConfig::commit() setInCurrentScope(kLanguageSync, m_LanguageSync); setInCurrentScope(kInvertScrollDirection, m_InvertScrollDirection); setInCurrentScope(kEnableService, m_EnableService); - setInCurrentScope(kMainWindowSize, m_MainWindowSize); - setInCurrentScope(kMainWindowPosition, m_MainWindowPosition); setInCurrentScope(kRequireClientCert, m_RequireClientCert); } @@ -525,16 +520,6 @@ const QString &AppConfig::serverHostname() const return m_ServerHostname; } -std::optional AppConfig::mainWindowSize() const -{ - return m_MainWindowSize; -} - -std::optional AppConfig::mainWindowPosition() const -{ - return m_MainWindowPosition; -} - /////////////////////////////////////////////////////////////////////////////// // End getters /////////////////////////////////////////////////////////////////////////////// @@ -672,16 +657,6 @@ void AppConfig::setRequireClientCerts(bool requireClientCerts) m_RequireClientCert = requireClientCerts; } -void AppConfig::setMainWindowSize(const QSize &size) -{ - m_MainWindowSize = size; -} - -void AppConfig::setMainWindowPosition(const QPoint &position) -{ - m_MainWindowPosition = position; -} - /////////////////////////////////////////////////////////////////////////////// // End setters /////////////////////////////////////////////////////////////////////////////// diff --git a/src/lib/gui/config/AppConfig.h b/src/lib/gui/config/AppConfig.h index 4612957bc..c38fd6df0 100644 --- a/src/lib/gui/config/AppConfig.h +++ b/src/lib/gui/config/AppConfig.h @@ -94,8 +94,8 @@ private: // 36 = server-client-mode, obsolete kEnableService = 37, // 38, close to tray moved to deskflow settings - kMainWindowSize = 39, - kMainWindowPosition = 40, + // 39 window size moved to deskflow settings + // 40 window position moved to deskflow settings // 41 = show dev thanks, obsolete // 42, close reminder moved to deskflow settings // 43 = Enable Update Check, @@ -168,8 +168,6 @@ public: bool serverGroupChecked() const; bool useInternalConfig() const; QString lastVersion() const; - std::optional mainWindowSize() const; - std::optional mainWindowPosition() const; // // Setters (overrides) @@ -202,8 +200,6 @@ public: void setClientGroupChecked(bool); void setServerHostname(const QString &); void setLastVersion(const QString &version); - void setMainWindowSize(const QSize &size); - void setMainWindowPosition(const QPoint &position); /// @brief Sets the user preference to load from SystemScope. /// @param [in] value @@ -299,8 +295,6 @@ private: QString m_ServerHostname = ""; bool m_EnableService = deskflow::gui::kDefaultProcessMode == ProcessMode::kService; int m_TlsKeyLength = deskflow::gui::kDefaultTlsKeyLength; - std::optional m_MainWindowSize; - std::optional m_MainWindowPosition; bool m_LoadFromSystemScope = false; bool m_RequireClientCert = true;