From ed67c20cba76f787852f58ca30558c91b6065f67 Mon Sep 17 00:00:00 2001 From: Lukas Cone Date: Thu, 14 Jul 2022 11:38:23 +0200 Subject: [PATCH] fix: User folders didnt load at startup (#578) --- .../source/content/settings_entries.cpp | 72 +++++++++++-------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 48c343d0c..a4f4cb5b6 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -13,6 +13,19 @@ #include +namespace { + static std::vector userFolders; + static void loadUserFoldersFromSetting(nlohmann::json &setting) { + userFolders.clear(); + std::vector paths = setting; + for (const auto &path : paths) { + // JSON reads char8_t as array, char8_t is not supported as of now + std::u8string_view uString(reinterpret_cast(&path.front()), reinterpret_cast(std::next(&path.back()))); + userFolders.emplace_back(uString); + } + } +}; + namespace hex::plugin::builtin { void registerSettings() { @@ -247,7 +260,7 @@ namespace hex::plugin::builtin { for (const auto &[unlocalizedName, visualizer] : visualizers) { if (ImGui::Selectable(LangEntry(unlocalizedName))) { setting = unlocalizedName; - result = true; + result = true; } } @@ -314,34 +327,27 @@ namespace hex::plugin::builtin { ContentRegistry::Settings::add(dirsSetting, dirsSetting, std::vector {}, [](auto name, nlohmann::json &setting) { hex::unused(name); - static size_t currentItemIndex = 0; - static std::vector folders = [&setting]{ - std::vector result; + static size_t currentItemIndex = [&setting] {loadUserFoldersFromSetting(setting); return 0; }(); - std::vector paths = setting; - for (const auto &path : paths) - result.emplace_back(path); - - return result; - }(); + auto saveToSetting = [&setting] { + std::vector folderStrings; + for (const auto &folder : userFolders) { + auto utfString = folder.u8string(); + // JSON stores char8_t as array, char8_t is not supported as of now + folderStrings.emplace_back(reinterpret_cast(&utfString.front()), reinterpret_cast(std::next(&utfString.back()))); + } + setting = folderStrings; + ImHexApi::System::setAdditionalFolderPaths(userFolders); + }; bool result = false; - auto writeSetting = [&setting]{ - std::vector folderStrings; - for (const auto &folder : folders) - folderStrings.push_back(folder.u8string()); - setting = folderStrings; - - ImHexApi::System::setAdditionalFolderPaths(folders); - }; - if (!ImGui::BeginListBox("", ImVec2(-38, -FLT_MIN))) { return false; } else { - for (size_t n = 0; n < folders.size(); n++) { + for (size_t n = 0; n < userFolders.size(); n++) { const bool isSelected = (currentItemIndex == n); - if (ImGui::Selectable(folders.at(n).string().c_str(), isSelected)) { currentItemIndex = n; } + if (ImGui::Selectable(userFolders.at(n).string().c_str(), isSelected)) { currentItemIndex = n; } if (isSelected) { ImGui::SetItemDefaultFocus(); } } ImGui::EndListBox(); @@ -351,12 +357,9 @@ namespace hex::plugin::builtin { if (ImGui::IconButton(ICON_VS_NEW_FOLDER, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) { fs::openFileBrowser(fs::DialogMode::Folder, {}, [&](const std::fs::path &path) { - - if (std::find(folders.begin(), folders.end(), path) == folders.end()) { - folders.emplace_back(path); - - writeSetting(); - + if (std::find(userFolders.begin(), userFolders.end(), path) == userFolders.end()) { + userFolders.emplace_back(path); + saveToSetting(); result = true; } }); @@ -364,10 +367,9 @@ namespace hex::plugin::builtin { ImGui::InfoTooltip("hex.builtin.setting.folders.add_folder"_lang); if (ImGui::IconButton(ICON_VS_REMOVE_CLOSE, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) { - if (!folders.empty()) { - folders.erase(std::next(folders.begin(), currentItemIndex)); - - writeSetting(); + if (!userFolders.empty()) { + userFolders.erase(std::next(userFolders.begin(), currentItemIndex)); + saveToSetting(); result = true; } @@ -480,10 +482,18 @@ namespace hex::plugin::builtin { ImHexApi::System::setTheme(static_cast(theme)); } + static void loadFoldersSettings() { + static const std::string dirsSetting { "hex.builtin.setting.folders" }; + auto dirs = ContentRegistry::Settings::getSetting(dirsSetting, dirsSetting); + loadUserFoldersFromSetting(dirs); + ImHexApi::System::setAdditionalFolderPaths(userFolders); + } + void loadSettings() { loadInterfaceScalingSetting(); loadFontSettings(); loadThemeSettings(); + loadFoldersSettings(); } }