From d121c92fbb19f0029a2075280b20794e9d2d606f Mon Sep 17 00:00:00 2001 From: gxalpha Date: Wed, 18 Aug 2021 16:21:00 +0200 Subject: [PATCH] UI: Add undo/redo for "Add existing source" Adds an undo/redo action for the "Add existing source", which previously was missing. --- UI/window-basic-source-select.cpp | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/UI/window-basic-source-select.cpp b/UI/window-basic-source-select.cpp index 6026ae772..e219778ad 100644 --- a/UI/window-basic-source-select.cpp +++ b/UI/window-basic-source-select.cpp @@ -241,8 +241,51 @@ void OBSBasicSourceSelect::on_buttonBox_accepted() if (!item) return; - AddExisting(QT_TO_UTF8(item->text()), visible, false, nullptr, + QString source_name = item->text(); + AddExisting(QT_TO_UTF8(source_name), visible, false, nullptr, nullptr); + + OBSBasic *main = + reinterpret_cast(App()->GetMainWindow()); + const char *scene_name = + obs_source_get_name(main->GetCurrentSceneSource()); + + auto undo = [scene_name, main](const std::string &data) { + UNUSED_PARAMETER(data); + obs_source_t *scene_source = + obs_get_source_by_name(scene_name); + main->SetCurrentScene(scene_source, true); + obs_source_release(scene_source); + + obs_scene_t *scene = obs_get_scene_by_name(scene_name); + OBSSceneItem item; + auto cb = [](obs_scene_t *scene, + obs_sceneitem_t *sceneitem, void *data) { + UNUSED_PARAMETER(scene); + OBSSceneItem &last = + *reinterpret_cast(data); + last = sceneitem; + return true; + }; + obs_scene_enum_items(scene, cb, &item); + + obs_sceneitem_remove(item); + obs_scene_release(scene); + }; + + auto redo = [scene_name, main, source_name, + visible](const std::string &data) { + UNUSED_PARAMETER(data); + obs_source_t *scene_source = + obs_get_source_by_name(scene_name); + main->SetCurrentScene(scene_source, true); + obs_source_release(scene_source); + AddExisting(QT_TO_UTF8(source_name), visible, false, + nullptr, nullptr); + }; + + undo_s.add_action(QTStr("Undo.Add").arg(source_name), undo, + redo, "", ""); } else { if (ui->sourceName->text().isEmpty()) { OBSMessageBox::warning(this,