refactor: move windowgeometry to Settings

newkey: gui/windowGeometry <= General/mainWindowSize and General/mainWindowPosition
mainWindowSize and Position remove from `AppConfig`
This commit is contained in:
sithlord48
2025-03-05 00:28:05 -05:00
committed by Nick Bolton
parent 1e46bd2727
commit 4c6195cc5d
5 changed files with 34 additions and 64 deletions

View File

@ -179,36 +179,33 @@ MainWindow::~MainWindow()
void MainWindow::restoreWindow() void MainWindow::restoreWindow()
{ {
const auto &windowSize = m_appConfig.mainWindowSize(); const auto windowGeometry = Settings::value(Settings::Gui::WindowGeometry).toRect();
if (windowSize.has_value()) { if (!windowGeometry.isValid()) {
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 {
// center main window in middle of screen // center main window in middle of screen
const auto screen = QGuiApplication::primaryScreen(); const auto screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry(); QRect screenGeometry = screen->geometry();
move(screenGeometry.center() - rect().center()); 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) { if (m_saveOnExit) {
m_appConfig.setMainWindowPosition(pos()); Settings::setValue(Settings::Gui::WindowGeometry, frameGeometry());
m_appConfig.setMainWindowSize(size());
m_configScopes.save();
} }
qDebug() << "quitting application"; qDebug() << "quitting application";
event->accept(); event->accept();

View File

@ -7,6 +7,7 @@
#include "Settings.h" #include "Settings.h"
#include <QFile> #include <QFile>
#include <QRect>
Settings *Settings::instance() Settings *Settings::instance()
{ {
@ -78,6 +79,9 @@ QVariant Settings::defaultValue(const QString &key)
return true; return true;
} }
if (key == Gui::WindowGeometry)
return QRect();
return QVariant(); return QVariant();
} }

View File

@ -43,6 +43,7 @@ public:
inline static const auto CloseToTray = QStringLiteral("gui/closeToTray"); inline static const auto CloseToTray = QStringLiteral("gui/closeToTray");
inline static const auto LogExpanded = QStringLiteral("gui/logExpanded"); inline static const auto LogExpanded = QStringLiteral("gui/logExpanded");
inline static const auto SymbolicTrayIcon = QStringLiteral("gui/symbolicTrayIcon"); inline static const auto SymbolicTrayIcon = QStringLiteral("gui/symbolicTrayIcon");
inline static const auto WindowGeometry = QStringLiteral("gui/windowGeometry");
}; };
static Settings *instance(); static Settings *instance();
@ -83,6 +84,7 @@ private:
, Gui::CloseToTray , Gui::CloseToTray
, Gui::LogExpanded , Gui::LogExpanded
, Gui::SymbolicTrayIcon , Gui::SymbolicTrayIcon
, Gui::WindowGeometry
}; };
// clang-format on // clang-format on
}; };

View File

@ -72,8 +72,8 @@ const char *const AppConfig::m_SettingsName[] = {
"", // 36 = serverClientMode, obsolete "", // 36 = serverClientMode, obsolete
"enableService", "enableService",
"", // 38 Moved to deskflow settings "", // 38 Moved to deskflow settings
"mainWindowSize", "", // 39 window size moved to deskflow settings
"mainWindowPosition", "", // 40 window position moved to deskflow settings
"", // 41 = Show dev thanks, obsolete "", // 41 = Show dev thanks, obsolete
"", // 42 show Close Reminder moved to deskflow settings "", // 42 show Close Reminder moved to deskflow settings
"", // 43 Moved to deskflow settings "", // 43 Moved to deskflow settings
@ -135,9 +135,6 @@ void AppConfig::recallFromCurrentScope()
m_TlsCertPath = getFromCurrentScope(kTlsCertPath, m_TlsCertPath).toString(); m_TlsCertPath = getFromCurrentScope(kTlsCertPath, m_TlsCertPath).toString();
m_TlsKeyLength = getFromCurrentScope(kTlsKeyLength, m_TlsKeyLength).toInt(); m_TlsKeyLength = getFromCurrentScope(kTlsKeyLength, m_TlsKeyLength).toInt();
m_RequireClientCert = getFromCurrentScope(kRequireClientCert, m_RequireClientCert).toBool(); m_RequireClientCert = getFromCurrentScope(kRequireClientCert, m_RequireClientCert).toBool();
m_MainWindowPosition =
getFromCurrentScope<QPoint>(kMainWindowPosition, [](const QVariant &v) { return v.toPoint(); });
m_MainWindowSize = getFromCurrentScope<QSize>(kMainWindowSize, [](const QVariant &v) { return v.toSize(); });
} }
void AppConfig::recallScreenName() void AppConfig::recallScreenName()
@ -186,8 +183,6 @@ void AppConfig::commit()
setInCurrentScope(kLanguageSync, m_LanguageSync); setInCurrentScope(kLanguageSync, m_LanguageSync);
setInCurrentScope(kInvertScrollDirection, m_InvertScrollDirection); setInCurrentScope(kInvertScrollDirection, m_InvertScrollDirection);
setInCurrentScope(kEnableService, m_EnableService); setInCurrentScope(kEnableService, m_EnableService);
setInCurrentScope(kMainWindowSize, m_MainWindowSize);
setInCurrentScope(kMainWindowPosition, m_MainWindowPosition);
setInCurrentScope(kRequireClientCert, m_RequireClientCert); setInCurrentScope(kRequireClientCert, m_RequireClientCert);
} }
@ -525,16 +520,6 @@ const QString &AppConfig::serverHostname() const
return m_ServerHostname; return m_ServerHostname;
} }
std::optional<QSize> AppConfig::mainWindowSize() const
{
return m_MainWindowSize;
}
std::optional<QPoint> AppConfig::mainWindowPosition() const
{
return m_MainWindowPosition;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// End getters // End getters
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -672,16 +657,6 @@ void AppConfig::setRequireClientCerts(bool requireClientCerts)
m_RequireClientCert = requireClientCerts; m_RequireClientCert = requireClientCerts;
} }
void AppConfig::setMainWindowSize(const QSize &size)
{
m_MainWindowSize = size;
}
void AppConfig::setMainWindowPosition(const QPoint &position)
{
m_MainWindowPosition = position;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// End setters // End setters
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -94,8 +94,8 @@ private:
// 36 = server-client-mode, obsolete // 36 = server-client-mode, obsolete
kEnableService = 37, kEnableService = 37,
// 38, close to tray moved to deskflow settings // 38, close to tray moved to deskflow settings
kMainWindowSize = 39, // 39 window size moved to deskflow settings
kMainWindowPosition = 40, // 40 window position moved to deskflow settings
// 41 = show dev thanks, obsolete // 41 = show dev thanks, obsolete
// 42, close reminder moved to deskflow settings // 42, close reminder moved to deskflow settings
// 43 = Enable Update Check, // 43 = Enable Update Check,
@ -168,8 +168,6 @@ public:
bool serverGroupChecked() const; bool serverGroupChecked() const;
bool useInternalConfig() const; bool useInternalConfig() const;
QString lastVersion() const; QString lastVersion() const;
std::optional<QSize> mainWindowSize() const;
std::optional<QPoint> mainWindowPosition() const;
// //
// Setters (overrides) // Setters (overrides)
@ -202,8 +200,6 @@ public:
void setClientGroupChecked(bool); void setClientGroupChecked(bool);
void setServerHostname(const QString &); void setServerHostname(const QString &);
void setLastVersion(const QString &version); void setLastVersion(const QString &version);
void setMainWindowSize(const QSize &size);
void setMainWindowPosition(const QPoint &position);
/// @brief Sets the user preference to load from SystemScope. /// @brief Sets the user preference to load from SystemScope.
/// @param [in] value /// @param [in] value
@ -299,8 +295,6 @@ private:
QString m_ServerHostname = ""; QString m_ServerHostname = "";
bool m_EnableService = deskflow::gui::kDefaultProcessMode == ProcessMode::kService; bool m_EnableService = deskflow::gui::kDefaultProcessMode == ProcessMode::kService;
int m_TlsKeyLength = deskflow::gui::kDefaultTlsKeyLength; int m_TlsKeyLength = deskflow::gui::kDefaultTlsKeyLength;
std::optional<QSize> m_MainWindowSize;
std::optional<QPoint> m_MainWindowPosition;
bool m_LoadFromSystemScope = false; bool m_LoadFromSystemScope = false;
bool m_RequireClientCert = true; bool m_RequireClientCert = true;