From 26c76d184fafac03dc245e0e8fc6fc09dd73d032 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 5 Apr 2024 19:21:56 +1000 Subject: [PATCH] Qt: Fix toolbar position not saving --- src/duckstation-qt/mainwindow.cpp | 83 +++++++++++++++++++++++++------ src/duckstation-qt/mainwindow.h | 4 +- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/duckstation-qt/mainwindow.cpp b/src/duckstation-qt/mainwindow.cpp index 3d146b786..698e58e22 100644 --- a/src/duckstation-qt/mainwindow.cpp +++ b/src/duckstation-qt/mainwindow.cpp @@ -162,7 +162,7 @@ void MainWindow::initialize() setupAdditionalUi(); connectSignals(); - restoreGeometryFromConfig(); + restoreStateFromConfig(); switchToGameListView(); updateWindowTitle(); @@ -2462,29 +2462,76 @@ void MainWindow::onSettingsResetToDefault(bool system, bool controller) updateMenuSelectedTheme(); } -void MainWindow::saveGeometryToConfig() +void MainWindow::saveStateToConfig() { - const QByteArray geometry = saveGeometry(); - const QByteArray geometry_b64 = geometry.toBase64(); - const std::string old_geometry_b64 = Host::GetBaseStringSettingValue("UI", "MainWindowGeometry"); + if (!isVisible() || ((windowState() & Qt::WindowFullScreen) != Qt::WindowNoState)) + return; + + bool changed = false; + + const QByteArray geometry(saveGeometry()); + const QByteArray geometry_b64(geometry.toBase64()); + const std::string old_geometry_b64(Host::GetBaseStringSettingValue("UI", "MainWindowGeometry")); if (old_geometry_b64 != geometry_b64.constData()) { Host::SetBaseStringSettingValue("UI", "MainWindowGeometry", geometry_b64.constData()); - Host::CommitBaseSettingChanges(); + changed = true; } + + const QByteArray state(saveState()); + const QByteArray state_b64(state.toBase64()); + const std::string old_state_b64(Host::GetBaseStringSettingValue("UI", "MainWindowState")); + if (old_state_b64 != state_b64.constData()) + { + Host::SetBaseStringSettingValue("UI", "MainWindowState", state_b64.constData()); + changed = true; + } + + if (changed) + Host::CommitBaseSettingChanges(); } -void MainWindow::restoreGeometryFromConfig() +void MainWindow::restoreStateFromConfig() { - const std::string geometry_b64 = Host::GetBaseStringSettingValue("UI", "MainWindowGeometry"); - const QByteArray geometry = QByteArray::fromBase64(QByteArray::fromStdString(geometry_b64)); - if (!geometry.isEmpty()) - restoreGeometry(geometry); + { + const std::string geometry_b64 = Host::GetBaseStringSettingValue("UI", "MainWindowGeometry"); + const QByteArray geometry = QByteArray::fromBase64(QByteArray::fromStdString(geometry_b64)); + if (!geometry.isEmpty()) + restoreGeometry(geometry); + } + + { + const std::string state_b64 = Host::GetBaseStringSettingValue("UI", "MainWindowState"); + const QByteArray state = QByteArray::fromBase64(QByteArray::fromStdString(state_b64)); + if (!state.isEmpty()) + { + restoreState(state); + + // make sure we're not loading a dodgy config which had fullscreen set... + setWindowState(windowState() & ~(Qt::WindowFullScreen | Qt::WindowActive)); + } + + { + QSignalBlocker sb(m_ui.actionViewToolbar); + m_ui.actionViewToolbar->setChecked(!m_ui.toolBar->isHidden()); + } + { + QSignalBlocker sb(m_ui.actionViewStatusBar); + m_ui.actionViewStatusBar->setChecked(!m_ui.statusBar->isHidden()); + } + } } void MainWindow::saveDisplayWindowGeometryToConfig() { - const QByteArray geometry = getDisplayContainer()->saveGeometry(); + QWidget* container = getDisplayContainer(); + if (container->windowState() & Qt::WindowFullScreen) + { + // if we somehow ended up here, don't save the fullscreen state to the config + return; + } + + const QByteArray geometry = container->saveGeometry(); const QByteArray geometry_b64 = geometry.toBase64(); const std::string old_geometry_b64 = Host::GetBaseStringSettingValue("UI", "DisplayWindowGeometry"); if (old_geometry_b64 != geometry_b64.constData()) @@ -2500,9 +2547,17 @@ void MainWindow::restoreDisplayWindowGeometryFromConfig() const QByteArray geometry = QByteArray::fromBase64(QByteArray::fromStdString(geometry_b64)); QWidget* container = getDisplayContainer(); if (!geometry.isEmpty()) + { container->restoreGeometry(geometry); + + // make sure we're not loading a dodgy config which had fullscreen set... + container->setWindowState(container->windowState() & ~(Qt::WindowFullScreen | Qt::WindowActive)); + } else + { + // default size container->resize(640, 480); + } } SettingsWindow* MainWindow::getSettingsDialog() @@ -2640,7 +2695,7 @@ void MainWindow::closeEvent(QCloseEvent* event) // If there's no VM, we can just exit as normal. if (!s_system_valid || !m_display_created) { - saveGeometryToConfig(); + saveStateToConfig(); if (m_display_created) g_emu_thread->stopFullscreenUI(); destroySubWindows(); @@ -2657,7 +2712,7 @@ void MainWindow::closeEvent(QCloseEvent* event) return; // Application will be exited in VM stopped handler. - saveGeometryToConfig(); + saveStateToConfig(); m_is_closing = true; } diff --git a/src/duckstation-qt/mainwindow.h b/src/duckstation-qt/mainwindow.h index 732f855c8..8bb399092 100644 --- a/src/duckstation-qt/mainwindow.h +++ b/src/duckstation-qt/mainwindow.h @@ -224,8 +224,8 @@ private: void switchToGameListView(); void switchToEmulationView(); - void saveGeometryToConfig(); - void restoreGeometryFromConfig(); + void saveStateToConfig(); + void restoreStateFromConfig(); void saveDisplayWindowGeometryToConfig(); void restoreDisplayWindowGeometryFromConfig(); void createDisplayWidget(bool fullscreen, bool render_to_main, bool use_main_window_pos);