window-basic-main-scene-collections.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <filesystem>
  15. #include <string>
  16. #include <obs.hpp>
  17. #include <util/util.hpp>
  18. #include <QMessageBox>
  19. #include <QVariant>
  20. #include <QFileDialog>
  21. #include <QStandardPaths>
  22. #include <qt-wrappers.hpp>
  23. #include "item-widget-helpers.hpp"
  24. #include "window-basic-main.hpp"
  25. #include "window-importer.hpp"
  26. #include "window-namedialog.hpp"
  27. // MARK: Constant Expressions
  28. constexpr std::string_view OBSSceneCollectionPath = "/obs-studio/basic/scenes/";
  29. // MARK: - Main Scene Collection Management Functions
  30. void OBSBasic::SetupNewSceneCollection(const std::string &collectionName)
  31. {
  32. const OBSSceneCollection &newCollection = CreateSceneCollection(collectionName);
  33. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  34. ActivateSceneCollection(newCollection);
  35. blog(LOG_INFO, "Created scene collection '%s' (clean, %s)", newCollection.name.c_str(),
  36. newCollection.fileName.c_str());
  37. blog(LOG_INFO, "------------------------------------------------");
  38. }
  39. void OBSBasic::SetupDuplicateSceneCollection(const std::string &collectionName)
  40. {
  41. const OBSSceneCollection &newCollection = CreateSceneCollection(collectionName);
  42. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  43. SaveProjectNow();
  44. const auto copyOptions = std::filesystem::copy_options::overwrite_existing;
  45. try {
  46. std::filesystem::copy(currentCollection.collectionFile, newCollection.collectionFile, copyOptions);
  47. } catch (const std::filesystem::filesystem_error &error) {
  48. blog(LOG_DEBUG, "%s", error.what());
  49. throw std::logic_error("Failed to copy file for cloned scene collection: " + newCollection.name);
  50. }
  51. OBSDataAutoRelease collection = obs_data_create_from_json_file(newCollection.collectionFile.u8string().c_str());
  52. obs_data_set_string(collection, "name", newCollection.name.c_str());
  53. OBSDataArrayAutoRelease sources = obs_data_get_array(collection, "sources");
  54. if (sources) {
  55. obs_data_erase(collection, "sources");
  56. obs_data_array_enum(
  57. sources,
  58. [](obs_data_t *data, void *) -> void {
  59. const char *uuid = os_generate_uuid();
  60. obs_data_set_string(data, "uuid", uuid);
  61. bfree((void *)uuid);
  62. },
  63. nullptr);
  64. obs_data_set_array(collection, "sources", sources);
  65. }
  66. obs_data_save_json_safe(collection, newCollection.collectionFile.u8string().c_str(), "tmp", nullptr);
  67. ActivateSceneCollection(newCollection);
  68. blog(LOG_INFO, "Created scene collection '%s' (duplicate, %s)", newCollection.name.c_str(),
  69. newCollection.fileName.c_str());
  70. blog(LOG_INFO, "------------------------------------------------");
  71. }
  72. void OBSBasic::SetupRenameSceneCollection(const std::string &collectionName)
  73. {
  74. const OBSSceneCollection &newCollection = CreateSceneCollection(collectionName);
  75. const OBSSceneCollection currentCollection = GetCurrentSceneCollection();
  76. SaveProjectNow();
  77. const auto copyOptions = std::filesystem::copy_options::overwrite_existing;
  78. try {
  79. std::filesystem::copy(currentCollection.collectionFile, newCollection.collectionFile, copyOptions);
  80. } catch (const std::filesystem::filesystem_error &error) {
  81. blog(LOG_DEBUG, "%s", error.what());
  82. throw std::logic_error("Failed to copy file for scene collection: " + currentCollection.name);
  83. }
  84. collections.erase(currentCollection.name);
  85. OBSDataAutoRelease collection = obs_data_create_from_json_file(newCollection.collectionFile.u8string().c_str());
  86. obs_data_set_string(collection, "name", newCollection.name.c_str());
  87. obs_data_save_json_safe(collection, newCollection.collectionFile.u8string().c_str(), "tmp", nullptr);
  88. ActivateSceneCollection(newCollection);
  89. RemoveSceneCollection(currentCollection);
  90. blog(LOG_INFO, "Renamed scene collection '%s' to '%s' (%s)", currentCollection.name.c_str(),
  91. newCollection.name.c_str(), newCollection.fileName.c_str());
  92. blog(LOG_INFO, "------------------------------------------------");
  93. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_RENAMED);
  94. }
  95. // MARK: - Scene Collection File Management Functions
  96. const OBSSceneCollection &OBSBasic::CreateSceneCollection(const std::string &collectionName)
  97. {
  98. if (const auto &foundCollection = GetSceneCollectionByName(collectionName)) {
  99. throw std::invalid_argument("Scene collection already exists: " + collectionName);
  100. }
  101. std::string fileName;
  102. if (!GetFileSafeName(collectionName.c_str(), fileName)) {
  103. throw std::invalid_argument("Failed to create safe directory for new scene collection: " +
  104. collectionName);
  105. }
  106. std::string collectionFile;
  107. collectionFile.reserve(App()->userScenesLocation.u8string().size() + OBSSceneCollectionPath.size() +
  108. fileName.size());
  109. collectionFile.append(App()->userScenesLocation.u8string()).append(OBSSceneCollectionPath).append(fileName);
  110. if (!GetClosestUnusedFileName(collectionFile, "json")) {
  111. throw std::invalid_argument("Failed to get closest file name for new scene collection: " + fileName);
  112. }
  113. const std::filesystem::path collectionFilePath = std::filesystem::u8path(collectionFile);
  114. auto [iterator, success] = collections.try_emplace(
  115. collectionName,
  116. OBSSceneCollection{collectionName, collectionFilePath.filename().u8string(), collectionFilePath});
  117. return iterator->second;
  118. }
  119. void OBSBasic::RemoveSceneCollection(OBSSceneCollection collection)
  120. {
  121. try {
  122. std::filesystem::remove(collection.collectionFile);
  123. } catch (const std::filesystem::filesystem_error &error) {
  124. blog(LOG_DEBUG, "%s", error.what());
  125. throw std::logic_error("Failed to remove scene collection file: " + collection.fileName);
  126. }
  127. blog(LOG_INFO, "Removed scene collection '%s' (%s)", collection.name.c_str(), collection.fileName.c_str());
  128. blog(LOG_INFO, "------------------------------------------------");
  129. }
  130. // MARK: - Scene Collection UI Handling Functions
  131. bool OBSBasic::CreateNewSceneCollection(const QString &name)
  132. {
  133. try {
  134. SetupNewSceneCollection(name.toStdString());
  135. return true;
  136. } catch (const std::invalid_argument &error) {
  137. blog(LOG_ERROR, "%s", error.what());
  138. return false;
  139. } catch (const std::logic_error &error) {
  140. blog(LOG_ERROR, "%s", error.what());
  141. return false;
  142. }
  143. }
  144. bool OBSBasic::CreateDuplicateSceneCollection(const QString &name)
  145. {
  146. try {
  147. SetupDuplicateSceneCollection(name.toStdString());
  148. return true;
  149. } catch (const std::invalid_argument &error) {
  150. blog(LOG_ERROR, "%s", error.what());
  151. return false;
  152. } catch (const std::logic_error &error) {
  153. blog(LOG_ERROR, "%s", error.what());
  154. return false;
  155. }
  156. }
  157. void OBSBasic::DeleteSceneCollection(const QString &name)
  158. {
  159. const std::string_view currentCollectionName{
  160. config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  161. if (currentCollectionName == name.toStdString()) {
  162. on_actionRemoveSceneCollection_triggered();
  163. return;
  164. }
  165. OBSSceneCollection currentCollection = GetCurrentSceneCollection();
  166. RemoveSceneCollection(currentCollection);
  167. collections.erase(name.toStdString());
  168. RefreshSceneCollections();
  169. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  170. }
  171. void OBSBasic::ChangeSceneCollection()
  172. {
  173. QAction *action = reinterpret_cast<QAction *>(sender());
  174. if (!action) {
  175. return;
  176. }
  177. const std::string_view currentCollectionName{
  178. config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  179. const std::string selectedCollectionName{action->text().toStdString()};
  180. if (currentCollectionName == selectedCollectionName) {
  181. action->setChecked(true);
  182. return;
  183. }
  184. const std::optional<OBSSceneCollection> foundCollection = GetSceneCollectionByName(selectedCollectionName);
  185. if (!foundCollection) {
  186. const std::string errorMessage{"Selected scene collection not found: "};
  187. throw std::invalid_argument(errorMessage + currentCollectionName.data());
  188. }
  189. const OBSSceneCollection &selectedCollection = foundCollection.value();
  190. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  191. ActivateSceneCollection(selectedCollection);
  192. blog(LOG_INFO, "Switched to scene collection '%s' (%s)", selectedCollection.name.c_str(),
  193. selectedCollection.fileName.c_str());
  194. blog(LOG_INFO, "------------------------------------------------");
  195. }
  196. void OBSBasic::RefreshSceneCollections(bool refreshCache)
  197. {
  198. std::string_view currentCollectionName{config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  199. QList<QAction *> menuActions = ui->sceneCollectionMenu->actions();
  200. for (auto &action : menuActions) {
  201. QVariant variant = action->property("file_name");
  202. if (variant.typeName() != nullptr) {
  203. delete action;
  204. }
  205. }
  206. if (refreshCache) {
  207. RefreshSceneCollectionCache();
  208. }
  209. size_t numAddedCollections = 0;
  210. for (auto &[collectionName, collection] : collections) {
  211. QAction *action = new QAction(QString().fromStdString(collectionName), this);
  212. action->setProperty("file_name", QString().fromStdString(collection.fileName));
  213. connect(action, &QAction::triggered, this, &OBSBasic::ChangeSceneCollection);
  214. action->setCheckable(true);
  215. action->setChecked(collectionName == currentCollectionName);
  216. ui->sceneCollectionMenu->addAction(action);
  217. numAddedCollections += 1;
  218. }
  219. ui->actionRemoveSceneCollection->setEnabled(numAddedCollections > 1);
  220. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  221. main->ui->actionPasteFilters->setEnabled(false);
  222. main->ui->actionPasteRef->setEnabled(false);
  223. main->ui->actionPasteDup->setEnabled(false);
  224. }
  225. // MARK: - Scene Collection Cache Functions
  226. void OBSBasic::RefreshSceneCollectionCache()
  227. {
  228. OBSSceneCollectionCache foundCollections{};
  229. const std::filesystem::path collectionsPath =
  230. App()->userScenesLocation / std::filesystem::u8path(OBSSceneCollectionPath.substr(1));
  231. if (!std::filesystem::exists(collectionsPath)) {
  232. blog(LOG_WARNING, "Failed to get scene collections config path");
  233. return;
  234. }
  235. for (const auto &entry : std::filesystem::directory_iterator(collectionsPath)) {
  236. if (entry.is_directory()) {
  237. continue;
  238. }
  239. if (entry.path().extension().u8string() != ".json") {
  240. continue;
  241. }
  242. OBSDataAutoRelease collectionData =
  243. obs_data_create_from_json_file_safe(entry.path().u8string().c_str(), "bak");
  244. std::string candidateName;
  245. const char *collectionName = obs_data_get_string(collectionData, "name");
  246. if (!collectionName) {
  247. candidateName = entry.path().filename().u8string();
  248. } else {
  249. candidateName = collectionName;
  250. }
  251. foundCollections.try_emplace(candidateName,
  252. OBSSceneCollection{candidateName, entry.path().filename().u8string(),
  253. entry.path()});
  254. }
  255. collections.swap(foundCollections);
  256. }
  257. const OBSSceneCollection &OBSBasic::GetCurrentSceneCollection() const
  258. {
  259. std::string currentCollectionName{config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  260. if (currentCollectionName.empty()) {
  261. throw std::invalid_argument("No valid scene collection name in configuration Basic->SceneCollection");
  262. }
  263. const auto &foundCollection = collections.find(currentCollectionName);
  264. if (foundCollection != collections.end()) {
  265. return foundCollection->second;
  266. } else {
  267. throw std::invalid_argument("Scene collection not found in collection list: " + currentCollectionName);
  268. }
  269. }
  270. std::optional<OBSSceneCollection> OBSBasic::GetSceneCollectionByName(const std::string &collectionName) const
  271. {
  272. auto foundCollection = collections.find(collectionName);
  273. if (foundCollection == collections.end()) {
  274. return {};
  275. } else {
  276. return foundCollection->second;
  277. }
  278. }
  279. std::optional<OBSSceneCollection> OBSBasic::GetSceneCollectionByFileName(const std::string &fileName) const
  280. {
  281. for (auto &[iterator, collection] : collections) {
  282. if (collection.fileName == fileName) {
  283. return collection;
  284. }
  285. }
  286. return {};
  287. }
  288. // MARK: - Qt Slot Functions
  289. void OBSBasic::on_actionNewSceneCollection_triggered()
  290. {
  291. const OBSPromptCallback sceneCollectionCallback = [this](const OBSPromptResult &result) {
  292. if (GetSceneCollectionByName(result.promptValue)) {
  293. return false;
  294. }
  295. return true;
  296. };
  297. const OBSPromptRequest request{Str("Basic.Main.AddSceneCollection.Title"),
  298. Str("Basic.Main.AddSceneCollection.Text")};
  299. OBSPromptResult result = PromptForName(request, sceneCollectionCallback);
  300. if (!result.success) {
  301. return;
  302. }
  303. try {
  304. SetupNewSceneCollection(result.promptValue);
  305. } catch (const std::invalid_argument &error) {
  306. blog(LOG_ERROR, "%s", error.what());
  307. } catch (const std::logic_error &error) {
  308. blog(LOG_ERROR, "%s", error.what());
  309. }
  310. }
  311. void OBSBasic::on_actionDupSceneCollection_triggered()
  312. {
  313. const OBSPromptCallback sceneCollectionCallback = [this](const OBSPromptResult &result) {
  314. if (GetSceneCollectionByName(result.promptValue)) {
  315. return false;
  316. }
  317. return true;
  318. };
  319. const OBSPromptRequest request{Str("Basic.Main.AddSceneCollection.Title"),
  320. Str("Basic.Main.AddSceneCollection.Text")};
  321. OBSPromptResult result = PromptForName(request, sceneCollectionCallback);
  322. if (!result.success) {
  323. return;
  324. }
  325. try {
  326. SetupDuplicateSceneCollection(result.promptValue);
  327. } catch (const std::invalid_argument &error) {
  328. blog(LOG_ERROR, "%s", error.what());
  329. } catch (const std::logic_error &error) {
  330. blog(LOG_ERROR, "%s", error.what());
  331. }
  332. }
  333. void OBSBasic::on_actionRenameSceneCollection_triggered()
  334. {
  335. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  336. const OBSPromptCallback sceneCollectionCallback = [this](const OBSPromptResult &result) {
  337. if (GetSceneCollectionByName(result.promptValue)) {
  338. return false;
  339. }
  340. return true;
  341. };
  342. const OBSPromptRequest request{Str("Basic.Main.RenameSceneCollection.Title"),
  343. Str("Basic.Main.AddSceneCollection.Text"), currentCollection.name};
  344. OBSPromptResult result = PromptForName(request, sceneCollectionCallback);
  345. if (!result.success) {
  346. return;
  347. }
  348. try {
  349. SetupRenameSceneCollection(result.promptValue);
  350. } catch (const std::invalid_argument &error) {
  351. blog(LOG_ERROR, "%s", error.what());
  352. } catch (const std::logic_error &error) {
  353. blog(LOG_ERROR, "%s", error.what());
  354. }
  355. }
  356. void OBSBasic::on_actionRemoveSceneCollection_triggered(bool skipConfirmation)
  357. {
  358. if (collections.size() < 2) {
  359. return;
  360. }
  361. OBSSceneCollection currentCollection;
  362. try {
  363. currentCollection = GetCurrentSceneCollection();
  364. if (!skipConfirmation) {
  365. const QString confirmationText =
  366. QTStr("ConfirmRemove.Text").arg(QString::fromStdString(currentCollection.name));
  367. const QMessageBox::StandardButton button =
  368. OBSMessageBox::question(this, QTStr("ConfirmRemove.Title"), confirmationText);
  369. if (button == QMessageBox::No) {
  370. return;
  371. }
  372. }
  373. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  374. collections.erase(currentCollection.name);
  375. } catch (const std::invalid_argument &error) {
  376. blog(LOG_ERROR, "%s", error.what());
  377. } catch (const std::logic_error &error) {
  378. blog(LOG_ERROR, "%s", error.what());
  379. }
  380. const OBSSceneCollection &newCollection = collections.begin()->second;
  381. ActivateSceneCollection(newCollection);
  382. RemoveSceneCollection(currentCollection);
  383. blog(LOG_INFO, "Switched to scene collection '%s' (%s)", newCollection.name.c_str(),
  384. newCollection.fileName.c_str());
  385. blog(LOG_INFO, "------------------------------------------------");
  386. }
  387. void OBSBasic::on_actionImportSceneCollection_triggered()
  388. {
  389. OBSImporter imp(this);
  390. imp.exec();
  391. RefreshSceneCollections(true);
  392. }
  393. void OBSBasic::on_actionExportSceneCollection_triggered()
  394. {
  395. SaveProjectNow();
  396. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  397. const QString home = QDir::homePath();
  398. const QString destinationFileName = SaveFile(this, QTStr("Basic.MainMenu.SceneCollection.Export"),
  399. home + "/" + currentCollection.fileName.c_str(),
  400. "JSON Files (*.json)");
  401. if (!destinationFileName.isEmpty() && !destinationFileName.isNull()) {
  402. const std::filesystem::path sourceFile = currentCollection.collectionFile;
  403. const std::filesystem::path destinationFile =
  404. std::filesystem::u8path(destinationFileName.toStdString());
  405. OBSDataAutoRelease collection = obs_data_create_from_json_file(sourceFile.u8string().c_str());
  406. OBSDataArrayAutoRelease sources = obs_data_get_array(collection, "sources");
  407. if (!sources) {
  408. blog(LOG_WARNING, "No sources in exported scene collection");
  409. return;
  410. }
  411. obs_data_erase(collection, "sources");
  412. using OBSDataVector = std::vector<OBSData>;
  413. OBSDataVector sourceItems;
  414. obs_data_array_enum(
  415. sources,
  416. [](obs_data_t *data, void *vector) -> void {
  417. OBSDataVector &sourceItems{*static_cast<OBSDataVector *>(vector)};
  418. sourceItems.push_back(data);
  419. },
  420. &sourceItems);
  421. std::sort(sourceItems.begin(), sourceItems.end(), [](const OBSData &a, const OBSData &b) {
  422. return astrcmpi(obs_data_get_string(a, "name"), obs_data_get_string(b, "name")) < 0;
  423. });
  424. OBSDataArrayAutoRelease newSources = obs_data_array_create();
  425. for (auto &item : sourceItems) {
  426. obs_data_array_push_back(newSources, item);
  427. }
  428. obs_data_set_array(collection, "sources", newSources);
  429. obs_data_save_json_pretty_safe(collection, destinationFile.u8string().c_str(), "tmp", "bak");
  430. }
  431. }
  432. void OBSBasic::on_actionRemigrateSceneCollection_triggered()
  433. {
  434. if (Active()) {
  435. OBSMessageBox::warning(this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  436. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.Active"));
  437. return;
  438. }
  439. OBSDataAutoRelease priv = obs_get_private_data();
  440. if (!usingAbsoluteCoordinates && !migrationBaseResolution) {
  441. OBSMessageBox::warning(
  442. this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  443. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.UnknownBaseResolution"));
  444. return;
  445. }
  446. obs_video_info ovi;
  447. obs_get_video_info(&ovi);
  448. if (!usingAbsoluteCoordinates && migrationBaseResolution->first == ovi.base_width &&
  449. migrationBaseResolution->second == ovi.base_height) {
  450. OBSMessageBox::warning(
  451. this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  452. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.BaseResolutionMatches"));
  453. return;
  454. }
  455. const OBSSceneCollection &currentCollection = GetCurrentSceneCollection();
  456. QString name = QString::fromStdString(currentCollection.name);
  457. QString message =
  458. QTStr("Basic.Main.RemigrateSceneCollection.Text").arg(name).arg(ovi.base_width).arg(ovi.base_height);
  459. auto answer = OBSMessageBox::question(this, QTStr("Basic.Main.RemigrateSceneCollection.Title"), message);
  460. if (answer == QMessageBox::No)
  461. return;
  462. lastOutputResolution = {ovi.base_width, ovi.base_height};
  463. if (!usingAbsoluteCoordinates) {
  464. /* Temporarily change resolution to migration resolution */
  465. ovi.base_width = migrationBaseResolution->first;
  466. ovi.base_height = migrationBaseResolution->second;
  467. if (obs_reset_video(&ovi) != OBS_VIDEO_SUCCESS) {
  468. OBSMessageBox::critical(
  469. this, QTStr("Basic.Main.RemigrateSceneCollection.Title"),
  470. QTStr("Basic.Main.RemigrateSceneCollection.CannotMigrate.FailedVideoReset"));
  471. return;
  472. }
  473. }
  474. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
  475. /* Save and immediately reload to (re-)run migrations. */
  476. SaveProjectNow();
  477. /* Reset video if we potentially changed to a temporary resolution */
  478. if (!usingAbsoluteCoordinates) {
  479. ResetVideo();
  480. }
  481. ActivateSceneCollection(currentCollection);
  482. }
  483. // MARK: - Scene Collection Management Helper Functions
  484. void OBSBasic::ActivateSceneCollection(const OBSSceneCollection &collection)
  485. {
  486. const std::string currentCollectionName{config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  487. if (auto foundCollection = GetSceneCollectionByName(currentCollectionName)) {
  488. if (collection.name != foundCollection.value().name) {
  489. SaveProjectNow();
  490. }
  491. }
  492. config_set_string(App()->GetUserConfig(), "Basic", "SceneCollection", collection.name.c_str());
  493. config_set_string(App()->GetUserConfig(), "Basic", "SceneCollectionFile", collection.fileName.c_str());
  494. Load(collection.collectionFile.u8string().c_str());
  495. RefreshSceneCollections();
  496. UpdateTitleBar();
  497. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  498. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  499. }