1
0
mirror of synced 2025-02-07 06:31:24 +01:00

fix: User folders didnt load at startup (#578)

This commit is contained in:
Lukas Cone 2022-07-14 11:38:23 +02:00 committed by GitHub
parent 35c209c791
commit ed67c20cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,19 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
namespace {
static std::vector<std::fs::path> userFolders;
static void loadUserFoldersFromSetting(nlohmann::json &setting) {
userFolders.clear();
std::vector<std::string> 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<const char8_t *>(&path.front()), reinterpret_cast<const char8_t *>(std::next(&path.back())));
userFolders.emplace_back(uString);
}
}
};
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
void registerSettings() { void registerSettings() {
@ -247,7 +260,7 @@ namespace hex::plugin::builtin {
for (const auto &[unlocalizedName, visualizer] : visualizers) { for (const auto &[unlocalizedName, visualizer] : visualizers) {
if (ImGui::Selectable(LangEntry(unlocalizedName))) { if (ImGui::Selectable(LangEntry(unlocalizedName))) {
setting = unlocalizedName; setting = unlocalizedName;
result = true; result = true;
} }
} }
@ -314,34 +327,27 @@ namespace hex::plugin::builtin {
ContentRegistry::Settings::add(dirsSetting, dirsSetting, std::vector<std::string> {}, [](auto name, nlohmann::json &setting) { ContentRegistry::Settings::add(dirsSetting, dirsSetting, std::vector<std::string> {}, [](auto name, nlohmann::json &setting) {
hex::unused(name); hex::unused(name);
static size_t currentItemIndex = 0; static size_t currentItemIndex = [&setting] {loadUserFoldersFromSetting(setting); return 0; }();
static std::vector<std::fs::path> folders = [&setting]{
std::vector<std::fs::path> result;
std::vector<std::u8string> paths = setting; auto saveToSetting = [&setting] {
for (const auto &path : paths) std::vector<std::string> folderStrings;
result.emplace_back(path); for (const auto &folder : userFolders) {
auto utfString = folder.u8string();
return result; // JSON stores char8_t as array, char8_t is not supported as of now
}(); folderStrings.emplace_back(reinterpret_cast<const char *>(&utfString.front()), reinterpret_cast<const char *>(std::next(&utfString.back())));
}
setting = folderStrings;
ImHexApi::System::setAdditionalFolderPaths(userFolders);
};
bool result = false; bool result = false;
auto writeSetting = [&setting]{
std::vector<std::u8string> folderStrings;
for (const auto &folder : folders)
folderStrings.push_back(folder.u8string());
setting = folderStrings;
ImHexApi::System::setAdditionalFolderPaths(folders);
};
if (!ImGui::BeginListBox("", ImVec2(-38, -FLT_MIN))) { if (!ImGui::BeginListBox("", ImVec2(-38, -FLT_MIN))) {
return false; return false;
} else { } else {
for (size_t n = 0; n < folders.size(); n++) { for (size_t n = 0; n < userFolders.size(); n++) {
const bool isSelected = (currentItemIndex == 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(); } if (isSelected) { ImGui::SetItemDefaultFocus(); }
} }
ImGui::EndListBox(); ImGui::EndListBox();
@ -351,12 +357,9 @@ namespace hex::plugin::builtin {
if (ImGui::IconButton(ICON_VS_NEW_FOLDER, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) { if (ImGui::IconButton(ICON_VS_NEW_FOLDER, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) {
fs::openFileBrowser(fs::DialogMode::Folder, {}, [&](const std::fs::path &path) { fs::openFileBrowser(fs::DialogMode::Folder, {}, [&](const std::fs::path &path) {
if (std::find(userFolders.begin(), userFolders.end(), path) == userFolders.end()) {
if (std::find(folders.begin(), folders.end(), path) == folders.end()) { userFolders.emplace_back(path);
folders.emplace_back(path); saveToSetting();
writeSetting();
result = true; result = true;
} }
}); });
@ -364,10 +367,9 @@ namespace hex::plugin::builtin {
ImGui::InfoTooltip("hex.builtin.setting.folders.add_folder"_lang); ImGui::InfoTooltip("hex.builtin.setting.folders.add_folder"_lang);
if (ImGui::IconButton(ICON_VS_REMOVE_CLOSE, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) { if (ImGui::IconButton(ICON_VS_REMOVE_CLOSE, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) {
if (!folders.empty()) { if (!userFolders.empty()) {
folders.erase(std::next(folders.begin(), currentItemIndex)); userFolders.erase(std::next(userFolders.begin(), currentItemIndex));
saveToSetting();
writeSetting();
result = true; result = true;
} }
@ -480,10 +482,18 @@ namespace hex::plugin::builtin {
ImHexApi::System::setTheme(static_cast<ImHexApi::System::Theme>(theme)); ImHexApi::System::setTheme(static_cast<ImHexApi::System::Theme>(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() { void loadSettings() {
loadInterfaceScalingSetting(); loadInterfaceScalingSetting();
loadFontSettings(); loadFontSettings();
loadThemeSettings(); loadThemeSettings();
loadFoldersSettings();
} }
} }