From dede2bb8cbe9340ac339fb42e44744237d31a2ca Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Thu, 10 Sep 2026 21:09:26 +0200 Subject: [PATCH] Refactor QueueUITask helper --- lib/queue/action-queue.cpp | 14 ++++++------- lib/utils/temp-variable.cpp | 10 ++++----- lib/utils/ui-helpers.cpp | 4 ++-- lib/utils/ui-helpers.hpp | 21 ++++++++++++++++++- lib/variables/variable.cpp | 14 ++++++------- .../base/macro-action-scene-collection.cpp | 16 +------------- plugins/base/macro-action-source.cpp | 20 +++--------------- plugins/twitch/twitch-tab.cpp | 11 +++------- tests/stubs/ui-helpers.cpp | 2 +- 9 files changed, 48 insertions(+), 64 deletions(-) diff --git a/lib/queue/action-queue.cpp b/lib/queue/action-queue.cpp index ab0b2cea..cd7f1037 100644 --- a/lib/queue/action-queue.cpp +++ b/lib/queue/action-queue.cpp @@ -420,11 +420,10 @@ static bool queueWithNameExists(const std::string &name) return !GetWeakActionQueueByName(name).expired(); } -static void signalImportedQueues(void *varsPtr) +static void +signalImportedQueues(const std::vector> &queues) { - auto queues = std::unique_ptr>>( - static_cast> *>(varsPtr)); - for (const auto &queue : *queues) { + for (const auto &queue : queues) { ActionQueueSignalManager::Instance()->Add( QString::fromStdString(queue->Name())); } @@ -436,7 +435,7 @@ void ImportQueues(obs_data_t *data) obs_data_get_array(data, "actionQueues"); size_t count = obs_data_array_count(array); - auto importedQueues = new std::vector>; + std::vector> importedQueues; for (size_t i = 0; i < count; i++) { OBSDataAutoRelease arrayElement = obs_data_array_item(array, i); @@ -446,10 +445,11 @@ void ImportQueues(obs_data_t *data) continue; } queues.emplace_back(queue); - importedQueues->emplace_back(queue); + importedQueues.emplace_back(queue); } - QueueUITask(signalImportedQueues, importedQueues); + QueueUITask( + [importedQueues]() { signalImportedQueues(importedQueues); }); } std::weak_ptr GetWeakActionQueueByName(const std::string &name) diff --git a/lib/utils/temp-variable.cpp b/lib/utils/temp-variable.cpp index 0dc3dc60..ec86fc9f 100644 --- a/lib/utils/temp-variable.cpp +++ b/lib/utils/temp-variable.cpp @@ -734,12 +734,10 @@ TempVarSignalManager *TempVarSignalManager::Instance() void NotifyUIAboutTempVarChange(MacroSegment *segment) { IncrementTempVarInUseGeneration(); - QueueUITask( - [](void *segment) { - TempVarSignalManager::Instance()->SegmentTempVarsChanged( - (MacroSegment *)segment); - }, - segment); + QueueUITask([segment]() { + TempVarSignalManager::Instance()->SegmentTempVarsChanged( + segment); + }); } TempVarOutputMappingsWidget::TempVarOutputMappingsWidget(QWidget *parent) diff --git a/lib/utils/ui-helpers.cpp b/lib/utils/ui-helpers.cpp index c3a04c3d..0dea03b0 100644 --- a/lib/utils/ui-helpers.cpp +++ b/lib/utils/ui-helpers.cpp @@ -184,9 +184,9 @@ std::string GetThemeTypeName() #endif } -void QueueUITask(void (*task)(void *param), void *param) +void QueueUITaskRaw(void (*task)(void *param), void *param, bool wait) { - obs_queue_task(OBS_TASK_UI, task, param, false); + obs_queue_task(OBS_TASK_UI, task, param, wait); } bool IsCursorInWidgetArea(QWidget *widget) diff --git a/lib/utils/ui-helpers.hpp b/lib/utils/ui-helpers.hpp index 463e824f..9e5d4679 100644 --- a/lib/utils/ui-helpers.hpp +++ b/lib/utils/ui-helpers.hpp @@ -6,7 +6,10 @@ #include #include +#include #include +#include +#include class QAbstractButton; class QComboBox; @@ -38,7 +41,23 @@ EXPORT void DisplayTrayMessage(const QString &title, const QString &msg, EXPORT std::string GetThemeTypeName(); EXPORT QWidget *GetSettingsWindow(); -EXPORT void QueueUITask(void (*task)(void *param), void *param); +EXPORT void QueueUITaskRaw(void (*task)(void *param), void *param, + bool wait = false); + +// Runs func on the main/UI thread; blocks if wait is true. +template void QueueUITask(F &&func, bool wait = false) +{ + using FnType = std::decay_t; + auto *heapFunc = new FnType(std::forward(func)); + + QueueUITaskRaw( + [](void *param) { + std::unique_ptr fn( + static_cast(param)); + (*fn)(); + }, + heapFunc, wait); +} bool IsCursorInWidgetArea(QWidget *widget); diff --git a/lib/variables/variable.cpp b/lib/variables/variable.cpp index 94bb1b05..f20e09c0 100644 --- a/lib/variables/variable.cpp +++ b/lib/variables/variable.cpp @@ -504,11 +504,10 @@ void LoadVariables(obs_data_t *obj) } } -static void signalImportedVariables(void *varsPtr) +static void +signalImportedVariables(const std::vector> &vars) { - auto vars = std::unique_ptr>>( - static_cast> *>(varsPtr)); - for (const auto &var : *vars) { + for (const auto &var : vars) { VariableSignalManager::Instance()->Add( QString::fromStdString(var->Name())); } @@ -519,7 +518,7 @@ void ImportVariables(obs_data_t *data) OBSDataArrayAutoRelease array = obs_data_get_array(data, "variables"); size_t count = obs_data_array_count(array); - auto importedVars = new std::vector>; + std::vector> importedVars; for (size_t i = 0; i < count; i++) { OBSDataAutoRelease arrayElement = obs_data_array_item(array, i); @@ -531,10 +530,11 @@ void ImportVariables(obs_data_t *data) } GetVariables().emplace_back(var); - importedVars->emplace_back(var); + importedVars.emplace_back(var); } - QueueUITask(signalImportedVariables, importedVars); + QueueUITask( + [importedVars]() { signalImportedVariables(importedVars); }); } std::chrono::high_resolution_clock::time_point GetLastVariableChangeTime() diff --git a/plugins/base/macro-action-scene-collection.cpp b/plugins/base/macro-action-scene-collection.cpp index c9c69cf7..b5dc6076 100644 --- a/plugins/base/macro-action-scene-collection.cpp +++ b/plugins/base/macro-action-scene-collection.cpp @@ -17,20 +17,6 @@ bool MacroActionSceneCollection::_registered = MacroActionFactory::Register( MacroActionSceneCollectionEdit::Create, "AdvSceneSwitcher.action.sceneCollection"}); -template void QueueUITaskLambda(F &&func) -{ - using FnType = std::decay_t; - auto *heapFunc = new FnType(std::forward(func)); - - QueueUITask( - [](void *param) { - std::unique_ptr fn( - static_cast(param)); - (*fn)(); - }, - heapFunc); -} - bool MacroActionSceneCollection::PerformAction() { // Changing the scene collection will also reload the settings of the @@ -41,7 +27,7 @@ bool MacroActionSceneCollection::PerformAction() } const auto collectionName = _sceneCollection; - QueueUITaskLambda([collectionName]() { + QueueUITask([collectionName]() { obs_frontend_set_current_scene_collection( collectionName.c_str()); }); diff --git a/plugins/base/macro-action-source.cpp b/plugins/base/macro-action-source.cpp index b0afc8b5..e213e64e 100644 --- a/plugins/base/macro-action-source.cpp +++ b/plugins/base/macro-action-source.cpp @@ -185,20 +185,6 @@ static void closeSourceDialog(obs_source_t *source, bool accept, } } -template void QueueUITaskLambda(F &&func) -{ - using FnType = std::decay_t; - auto *heapFunc = new FnType(std::forward(func)); - - QueueUITask( - [](void *param) { - std::unique_ptr fn( - static_cast(param)); - (*fn)(); - }, - heapFunc); -} - bool MacroActionSource::PerformAction() { OBSSource s = obs_weak_source_get_source(_source.GetSource()); @@ -271,17 +257,17 @@ bool MacroActionSource::PerformAction() break; } - QueueUITaskLambda([&]() { + QueueUITask([&]() { closeSourceDialog(s, true, "OBSBasicInteraction"); }); break; case Action::CLOSE_FILTER_DIALOG: - QueueUITaskLambda([&]() { + QueueUITask([&]() { closeSourceDialog(s, _acceptDialog, "OBSBasicFilters"); }); break; case Action::CLOSE_PROPERTIES_DIALOG: - QueueUITaskLambda([&]() { + QueueUITask([&]() { closeSourceDialog(s, _acceptDialog, "OBSBasicProperties"); }); diff --git a/plugins/twitch/twitch-tab.cpp b/plugins/twitch/twitch-tab.cpp index c7fa8653..593f453b 100644 --- a/plugins/twitch/twitch-tab.cpp +++ b/plugins/twitch/twitch-tab.cpp @@ -31,14 +31,9 @@ static bool setup() static const auto showInvalidWarnings = [](void *) { const auto invalidTokens = getInvalidTokens(); for (const auto &token : invalidTokens) { - QueueUITask( - [](void *tokenPtr) { - auto tokenName = static_cast( - tokenPtr); - InvalidTokenDialog::ShowWarning( - *tokenName); - }, - (void *)&token); + QueueUITask([token]() { + InvalidTokenDialog::ShowWarning(token); + }); } }; diff --git a/tests/stubs/ui-helpers.cpp b/tests/stubs/ui-helpers.cpp index fa67dcc0..99655b88 100644 --- a/tests/stubs/ui-helpers.cpp +++ b/tests/stubs/ui-helpers.cpp @@ -40,7 +40,7 @@ std::string GetThemeTypeName() return "Dark"; } -void QueueUITask(void (*task)(void *param), void *) {} +void QueueUITaskRaw(void (*task)(void *param), void *, bool) {} QWidget *GetSettingsWindow() {