From df06dd49c571e00828859a7571d3a3939cd67cfa Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 16 Feb 2021 23:42:35 +0100 Subject: [PATCH] Added better settings API that handles errors better This fixes #161 --- .../include/hex/api/content_registry.hpp | 1 + .../libimhex/source/api/content_registry.cpp | 9 +++++ source/views/view_data_processor.cpp | 30 ++++++++-------- source/views/view_pattern.cpp | 26 +++++++------- source/window.cpp | 35 +++++++++++-------- 5 files changed, 60 insertions(+), 41 deletions(-) diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index 887b7362c..8ca508b7e 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -51,6 +51,7 @@ namespace hex { static std::vector read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector& defaultValue = { }); static std::map>& getEntries(); + static std::optional getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName); static nlohmann::json& getSettingsData(); }; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 4f12c226b..b80da2b4b 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -109,6 +109,15 @@ namespace hex { return SharedData::settingsEntries; } + std::optional ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) { + auto &settings = getSettingsData(); + + if (!settings.contains(unlocalizedCategory)) return { }; + if (!settings[unlocalizedCategory.data()].contains(unlocalizedName)) return { }; + + return settings[unlocalizedCategory.data()][unlocalizedName.data()]; + } + nlohmann::json& ContentRegistry::Settings::getSettingsData() { return SharedData::settingsJson; } diff --git a/source/views/view_data_processor.cpp b/source/views/view_data_processor.cpp index 1c19d0b57..48ea345bd 100644 --- a/source/views/view_data_processor.cpp +++ b/source/views/view_data_processor.cpp @@ -18,22 +18,24 @@ namespace hex { } View::subscribeEvent(Events::SettingsChanged, [](auto) { - int theme = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.color"]; + auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); - switch (theme) { - default: - case 0: /* Dark theme */ - imnodes::StyleColorsDark(); - break; - case 1: /* Light theme */ - imnodes::StyleColorsLight(); - break; - case 2: /* Classic theme */ - imnodes::StyleColorsClassic(); - break; + if (theme.has_value()) { + switch (static_cast(theme.value())) { + default: + case 0: /* Dark theme */ + imnodes::StyleColorsDark(); + break; + case 1: /* Light theme */ + imnodes::StyleColorsLight(); + break; + case 2: /* Classic theme */ + imnodes::StyleColorsClassic(); + break; + } + + imnodes::GetStyle().flags = imnodes::StyleFlags(imnodes::StyleFlags_NodeOutline | imnodes::StyleFlags_GridLines); } - - imnodes::GetStyle().flags = imnodes::StyleFlags(imnodes::StyleFlags_NodeOutline | imnodes::StyleFlags_GridLines); }); View::subscribeEvent(Events::FileLoaded, [this](auto) { diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index f918b2980..ad0553a53 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -173,19 +173,21 @@ namespace hex { { View::subscribeEvent(Events::SettingsChanged, [this](auto) { - int theme = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.color"]; + auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); - switch (theme) { - default: - case 0: /* Dark theme */ - this->m_textEditor.SetPalette(TextEditor::GetDarkPalette()); - break; - case 1: /* Light theme */ - this->m_textEditor.SetPalette(TextEditor::GetLightPalette()); - break; - case 2: /* Classic theme */ - this->m_textEditor.SetPalette(TextEditor::GetRetroBluePalette()); - break; + if (theme.has_value()) { + switch (static_cast(theme.value())) { + default: + case 0: /* Dark theme */ + this->m_textEditor.SetPalette(TextEditor::GetDarkPalette()); + break; + case 1: /* Light theme */ + this->m_textEditor.SetPalette(TextEditor::GetLightPalette()); + break; + case 2: /* Classic theme */ + this->m_textEditor.SetPalette(TextEditor::GetRetroBluePalette()); + break; + } } }); diff --git a/source/window.cpp b/source/window.cpp index 49266c029..bc3c351fd 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -55,25 +55,30 @@ namespace hex { EventManager::subscribe(Events::SettingsChanged, this, [](auto) -> std::any { { - int theme = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.color"]; - switch (theme) { - default: - case 0: /* Dark theme */ - ImGui::StyleColorsDark(); - break; - case 1: /* Light theme */ - ImGui::StyleColorsLight(); - break; - case 2: /* Classic theme */ - ImGui::StyleColorsClassic(); - break; + auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); + + if (theme.has_value()) { + switch (static_cast(theme.value())) { + default: + case 0: /* Dark theme */ + ImGui::StyleColorsDark(); + break; + case 1: /* Light theme */ + ImGui::StyleColorsLight(); + break; + case 2: /* Classic theme */ + ImGui::StyleColorsClassic(); + break; + } + ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; } - ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; } { - std::string language = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.language"]; - LangEntry::loadLanguage(language); + auto language = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.language"); + + if (language.has_value()) + LangEntry::loadLanguage(static_cast(language.value())); } return { };