From 2fb00faa670009cb5bc57a8b976a1850bb642066 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 21 Jul 2020 19:58:50 +1000 Subject: [PATCH] Qt: Delay settings save by one second --- src/duckstation-qt/qthostinterface.cpp | 34 +++++++++++++++++++++++++- src/duckstation-qt/qthostinterface.h | 7 +++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/duckstation-qt/qthostinterface.cpp b/src/duckstation-qt/qthostinterface.cpp index 264dd1aac..74b784616 100644 --- a/src/duckstation-qt/qthostinterface.cpp +++ b/src/duckstation-qt/qthostinterface.cpp @@ -123,7 +123,8 @@ bool QtHostInterface::parseCommandLineParameters(int argc, char* argv[], return CommonHostInterface::ParseCommandLineParameters(argc, argv, out_boot_params); } -std::string QtHostInterface::GetStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) +std::string QtHostInterface::GetStringSettingValue(const char* section, const char* key, + const char* default_value /*= ""*/) { std::lock_guard guard(m_settings_mutex); return m_settings_interface->GetStringValue(section, key, default_value); @@ -157,24 +158,28 @@ void QtHostInterface::SetBoolSettingValue(const char* section, const char* key, { std::lock_guard guard(m_settings_mutex); m_settings_interface->SetBoolValue(section, key, value); + queueSettingsSave(); } void QtHostInterface::SetIntSettingValue(const char* section, const char* key, int value) { std::lock_guard guard(m_settings_mutex); m_settings_interface->SetIntValue(section, key, value); + queueSettingsSave(); } void QtHostInterface::SetFloatSettingValue(const char* section, const char* key, float value) { std::lock_guard guard(m_settings_mutex); m_settings_interface->SetFloatValue(section, key, value); + queueSettingsSave(); } void QtHostInterface::SetStringSettingValue(const char* section, const char* key, const char* value) { std::lock_guard guard(m_settings_mutex); m_settings_interface->SetStringValue(section, key, value); + queueSettingsSave(); } void QtHostInterface::SetStringListSettingValue(const char* section, const char* key, @@ -182,12 +187,37 @@ void QtHostInterface::SetStringListSettingValue(const char* section, const char* { std::lock_guard guard(m_settings_mutex); m_settings_interface->SetStringList(section, key, values); + queueSettingsSave(); } void QtHostInterface::RemoveSettingValue(const char* section, const char* key) { std::lock_guard guard(m_settings_mutex); m_settings_interface->DeleteValue(section, key); + queueSettingsSave(); +} + +void QtHostInterface::queueSettingsSave() +{ + if (!m_settings_save_timer) + { + m_settings_save_timer = std::make_unique(); + connect(m_settings_save_timer.get(), &QTimer::timeout, this, &QtHostInterface::doSaveSettings); + m_settings_save_timer->setSingleShot(true); + } + else + { + m_settings_save_timer->stop(); + } + + m_settings_save_timer->start(SETTINGS_SAVE_DELAY); +} + +void QtHostInterface::doSaveSettings() +{ + std::lock_guard guard(m_settings_mutex); + m_settings_interface->Save(); + m_settings_save_timer.reset(); } void QtHostInterface::setDefaultSettings() @@ -202,6 +232,8 @@ void QtHostInterface::setDefaultSettings() { std::lock_guard guard(m_settings_mutex); SetDefaultSettings(*m_settings_interface.get()); + m_settings_interface->Save(); + CommonHostInterface::LoadSettings(*m_settings_interface.get()); } diff --git a/src/duckstation-qt/qthostinterface.h b/src/duckstation-qt/qthostinterface.h index c3961b00a..ee7bd6edc 100644 --- a/src/duckstation-qt/qthostinterface.h +++ b/src/duckstation-qt/qthostinterface.h @@ -145,6 +145,7 @@ private Q_SLOTS: void doStopThread(); void onHostDisplayWindowResized(int width, int height); void doBackgroundControllerPoll(); + void doSaveSettings(); protected: bool AcquireHostDisplay() override; @@ -171,7 +172,9 @@ private: enum : u32 { BACKGROUND_CONTROLLER_POLLING_INTERVAL = - 100 /// Interval at which the controllers are polled when the system is not active. + 100, /// Interval at which the controllers are polled when the system is not active. + + SETTINGS_SAVE_DELAY = 1000 }; using InputButtonHandler = std::function; @@ -211,6 +214,7 @@ private: void renderDisplay(); void connectDisplaySignals(QtDisplayWidget* widget); void updateDisplayState(); + void queueSettingsSave(); void wakeThread(); std::unique_ptr m_settings_interface; @@ -224,6 +228,7 @@ private: std::atomic_bool m_shutdown_flag{false}; QTimer* m_background_controller_polling_timer = nullptr; + std::unique_ptr m_settings_save_timer; bool m_is_rendering_to_main = false; bool m_is_fullscreen = false;