From 53ff9f5a0cc68bdef348b585120914476a9df848 Mon Sep 17 00:00:00 2001 From: Mike Skec Date: Sun, 21 Feb 2021 09:59:01 +1100 Subject: Fix window geometry/state not being saved in some environments The window state and geometry are now saved either after app.exec() finished, or if closeEvent is emitted on the MainWindow. This allows the window geometry/state restoration to be a lot more reliable. E.g in my configuration (Fluxbox) - if I closed the window using my own defined keybinding, the state would not get saved --- src/kristall.hpp | 2 ++ src/main.cpp | 21 +++++++++++++++------ src/mainwindow.cpp | 7 +++++++ src/mainwindow.hpp | 2 ++ 4 files changed, 26 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/kristall.hpp b/src/kristall.hpp index b692e75..8ae8cb2 100644 --- a/src/kristall.hpp +++ b/src/kristall.hpp @@ -152,6 +152,8 @@ namespace kristall void setUiDensity(UIDensity density, bool previewing); + void saveWindowState(); + extern QString default_font_family, default_font_family_fixed; } diff --git a/src/main.cpp b/src/main.cpp index d54ae22..f969834 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,6 +37,7 @@ QString toFingerprintString(QSslCertificate const & certificate) static QSettings * app_settings_ptr; static QApplication * app; static MainWindow * main_window = nullptr; +static bool closing_state_saved = false; #define SSTR(X) STR(X) #define STR(X) #X @@ -317,12 +318,8 @@ int main(int argc, char *argv[]) int exit_code = app.exec(); - app_settings.beginGroup("Window State"); - app_settings.setValue("geometry", w.saveGeometry()); - app_settings.setValue("state", w.saveState()); - app_settings.endGroup(); - - kristall::saveSettings(); + if (!closing_state_saved) + kristall::saveWindowState(); return exit_code; } @@ -561,3 +558,15 @@ void kristall::setUiDensity(UIDensity density, bool previewing) assert(main_window != nullptr); main_window->setUiDensity(density, previewing); } + +void kristall::saveWindowState() +{ + closing_state_saved = true; + + app_settings_ptr->beginGroup("Window State"); + app_settings_ptr->setValue("geometry", main_window->saveGeometry()); + app_settings_ptr->setValue("state", main_window->saveState()); + app_settings_ptr->endGroup(); + + kristall::saveSettings(); +} diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 64f5fab..8594f7a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -296,6 +296,13 @@ void MainWindow::mousePressEvent(QMouseEvent *event) } } +void MainWindow::closeEvent(QCloseEvent *event) +{ + kristall::saveWindowState(); + + event->accept(); +} + void MainWindow::on_browser_tabs_currentChanged(int index) { if(index >= 0) { diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp index a252daa..7113587 100644 --- a/src/mainwindow.hpp +++ b/src/mainwindow.hpp @@ -47,6 +47,8 @@ public: void mousePressEvent(QMouseEvent *event) override; + void closeEvent(QCloseEvent *event) override; + private slots: void on_browser_tabs_currentChanged(int index); -- cgit v1.2.3