1
0
mirror of synced 2024-11-12 02:00:52 +01:00

impr: Make sure themes are added correctly when downloaded from the store

This commit is contained in:
WerWolv 2023-03-23 20:35:16 +01:00
parent eb2ed6852c
commit 635173e55a
9 changed files with 47 additions and 40 deletions

View File

@ -9,7 +9,7 @@
#include <nlohmann/json_fwd.hpp>
#include <imgui.h>
namespace hex::api {
namespace hex {
/**
* @brief The Theme Manager takes care of loading and applying themes

View File

@ -6,7 +6,7 @@
#include <nlohmann/json.hpp>
namespace hex::api {
namespace hex {
std::map<std::string, nlohmann::json> ThemeManager::s_themes;
std::map<std::string, ThemeManager::ThemeHandler> ThemeManager::s_themeHandlers;

View File

@ -290,7 +290,7 @@ namespace hex::init {
ContentRegistry::FileHandler::impl::getEntries().clear();
ContentRegistry::Hashes::impl::getHashes().clear();
api::ThemeManager::reset();
ThemeManager::reset();
{
auto &visualizers = ContentRegistry::HexEditor::impl::getVisualizers();

View File

@ -116,12 +116,12 @@ namespace hex::plugin::builtin {
ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", "Dark", [](auto name, nlohmann::json &setting) {
static auto selection = static_cast<std::string>(setting);
const auto themeNames = hex::api::ThemeManager::getThemeNames();
const auto themeNames = ThemeManager::getThemeNames();
bool changed = false;
if (ImGui::BeginCombo(name.data(), selection.c_str())) {
if (ImGui::Selectable(api::ThemeManager::NativeTheme, selection == api::ThemeManager::NativeTheme)) {
selection = api::ThemeManager::NativeTheme;
if (ImGui::Selectable(ThemeManager::NativeTheme, selection == ThemeManager::NativeTheme)) {
selection = ThemeManager::NativeTheme;
setting = selection;
ImHexApi::System::enableSystemThemeDetection(true);
changed = true;
@ -132,7 +132,7 @@ namespace hex::plugin::builtin {
selection = themeName;
setting = selection;
ImHexApi::System::enableSystemThemeDetection(false);
api::ThemeManager::changeTheme(selection);
ThemeManager::changeTheme(selection);
changed = true;
}
}
@ -613,13 +613,13 @@ namespace hex::plugin::builtin {
}
static void loadThemeSettings() {
auto theme = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme);
auto theme = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", ThemeManager::NativeTheme);
if (theme == api::ThemeManager::NativeTheme)
if (theme == ThemeManager::NativeTheme)
ImHexApi::System::enableSystemThemeDetection(true);
else {
ImHexApi::System::enableSystemThemeDetection(false);
api::ThemeManager::changeTheme(theme);
ThemeManager::changeTheme(theme);
}
}

View File

@ -20,7 +20,7 @@ namespace hex::plugin::builtin {
void registerThemeHandlers() {
EventManager::subscribe<RequestInitThemeHandlers>([]() {
{
const static api::ThemeManager::ColorMap ImGuiColorMap = {
const static ThemeManager::ColorMap ImGuiColorMap = {
{ "text", ImGuiCol_Text },
{ "text-disabled", ImGuiCol_TextDisabled },
{ "window-background", ImGuiCol_WindowBg },
@ -78,7 +78,7 @@ namespace hex::plugin::builtin {
{ "modal-window-dim-background", ImGuiCol_ModalWindowDimBg }
};
api::ThemeManager::addThemeHandler("imgui", ImGuiColorMap,
ThemeManager::addThemeHandler("imgui", ImGuiColorMap,
[](u32 colorId) -> ImColor {
return ImGui::GetStyle().Colors[colorId];
},
@ -89,7 +89,7 @@ namespace hex::plugin::builtin {
}
{
const static api::ThemeManager::ColorMap ImPlotColorMap = {
const static ThemeManager::ColorMap ImPlotColorMap = {
{ "line", ImPlotCol_Line },
{ "fill", ImPlotCol_Fill },
{ "marker-outline", ImPlotCol_MarkerOutline },
@ -113,7 +113,7 @@ namespace hex::plugin::builtin {
{ "crosshairs", ImPlotCol_Crosshairs }
};
api::ThemeManager::addThemeHandler("implot", ImPlotColorMap,
ThemeManager::addThemeHandler("implot", ImPlotColorMap,
[](u32 colorId) -> ImColor {
return ImPlot::GetStyle().Colors[colorId];
},
@ -124,7 +124,7 @@ namespace hex::plugin::builtin {
}
{
const static api::ThemeManager::ColorMap ImNodesColorMap = {
const static ThemeManager::ColorMap ImNodesColorMap = {
{ "node-background", ImNodesCol_NodeBackground },
{ "node-background-hovered", ImNodesCol_NodeBackgroundHovered },
{ "node-background-selected", ImNodesCol_NodeBackgroundSelected },
@ -156,7 +156,7 @@ namespace hex::plugin::builtin {
{ "mini-map-canvas-outline", ImNodesCol_MiniMapCanvasOutline },
};
api::ThemeManager::addThemeHandler("imnodes", ImNodesColorMap,
ThemeManager::addThemeHandler("imnodes", ImNodesColorMap,
[](u32 colorId) -> ImColor {
return ImNodes::GetStyle().Colors[colorId];
},
@ -167,7 +167,7 @@ namespace hex::plugin::builtin {
}
{
const static api::ThemeManager::ColorMap ImHexColorMap = {
const static ThemeManager::ColorMap ImHexColorMap = {
{ "desc-button", ImGuiCustomCol_DescButton },
{ "desc-button-hovered", ImGuiCustomCol_DescButtonHovered },
{ "desc-button-active", ImGuiCustomCol_DescButtonActive },
@ -181,7 +181,7 @@ namespace hex::plugin::builtin {
{ "highlight", ImGuiCustomCol_Highlight }
};
api::ThemeManager::addThemeHandler("imhex", ImHexColorMap,
ThemeManager::addThemeHandler("imhex", ImHexColorMap,
[](u32 colorId) -> ImColor {
return static_cast<ImGui::ImHexCustomData *>(GImGui->IO.UserData)->Colors[colorId];
@ -193,7 +193,7 @@ namespace hex::plugin::builtin {
}
{
const static api::ThemeManager::ColorMap TextEditorColorMap = {
const static ThemeManager::ColorMap TextEditorColorMap = {
{ "default", u32(TextEditor::PaletteIndex::Default) },
{ "keyword", u32(TextEditor::PaletteIndex::Keyword) },
{ "number", u32(TextEditor::PaletteIndex::Number) },
@ -217,7 +217,7 @@ namespace hex::plugin::builtin {
{ "current-line-edge", u32(TextEditor::PaletteIndex::CurrentLineEdge) }
};
api::ThemeManager::addThemeHandler("text-editor", TextEditorColorMap,
ThemeManager::addThemeHandler("text-editor", TextEditorColorMap,
[](u32 colorId) -> ImColor {
return TextEditor::GetPalette()[colorId];
},
@ -235,7 +235,7 @@ namespace hex::plugin::builtin {
EventManager::subscribe<RequestInitThemeHandlers>([]() {
{
auto &style = ImGui::GetStyle();
const static api::ThemeManager::StyleMap ImGuiStyleMap = {
const static ThemeManager::StyleMap ImGuiStyleMap = {
{ "alpha", { &style.Alpha, 0.001F, 1.0F, false } },
{ "disabled-alpha", { &style.DisabledAlpha, 0.0F, 1.0F, false } },
{ "window-padding", { &style.WindowPadding, 0.0F, 20.0F, true } },
@ -263,12 +263,12 @@ namespace hex::plugin::builtin {
{ "selectable-text-align", { &style.SelectableTextAlign, 0.0F, 1.0F, false } },
};
api::ThemeManager::addStyleHandler("imgui", ImGuiStyleMap);
ThemeManager::addStyleHandler("imgui", ImGuiStyleMap);
}
{
auto &style = ImPlot::GetStyle();
const static api::ThemeManager::StyleMap ImPlotStyleMap = {
const static ThemeManager::StyleMap ImPlotStyleMap = {
{ "line-weight", { &style.LineWeight, 0.0F, 5.0F, true } },
{ "marker-size", { &style.MarkerSize, 2.0F, 10.0F, true } },
{ "marker-weight", { &style.MarkerWeight, 0.0F, 5.0F, true } },
@ -297,12 +297,12 @@ namespace hex::plugin::builtin {
{ "plot-min-size", { &style.PlotMinSize, 0.0F, 300.0F, true } },
};
api::ThemeManager::addStyleHandler("implot", ImPlotStyleMap);
ThemeManager::addStyleHandler("implot", ImPlotStyleMap);
}
{
auto &style = ImNodes::GetStyle();
const static api::ThemeManager::StyleMap ImNodesStyleMap = {
const static ThemeManager::StyleMap ImNodesStyleMap = {
{ "grid-spacing", { &style.GridSpacing, 0.0F, 100.0F, true } },
{ "node-corner-rounding", { &style.NodeCornerRounding, 0.0F, 12.0F, true } },
{ "node-padding", { &style.NodePadding, 0.0F, 20.0F, true } },
@ -320,7 +320,7 @@ namespace hex::plugin::builtin {
{ "mini-map-offset", { &style.MiniMapOffset, -10.0F, 10.0F, true } },
};
api::ThemeManager::addStyleHandler("imnodes", ImNodesStyleMap);
ThemeManager::addStyleHandler("imnodes", ImNodesStyleMap);
}
});
}
@ -328,14 +328,14 @@ namespace hex::plugin::builtin {
void registerThemes() {
// Load built-in themes
for (const auto &theme : romfs::list("themes")) {
api::ThemeManager::addTheme(std::string(romfs::get(theme).string()));
ThemeManager::addTheme(std::string(romfs::get(theme).string()));
}
// Load user themes
for (const auto &themeFolder : fs::getDefaultPaths(fs::ImHexPath::Themes)) {
for (const auto &theme : std::fs::directory_iterator(themeFolder)) {
if (theme.is_regular_file())
api::ThemeManager::addTheme(wolv::io::File(theme.path(), wolv::io::File::Mode::Read).readString());
ThemeManager::addTheme(wolv::io::File(theme.path(), wolv::io::File::Mode::Read).readString());
}
}
}

View File

@ -1,4 +1,5 @@
#include "content/views/view_store.hpp"
#include "hex/api/theme_manager.hpp"
#include <hex/api/content_registry.hpp>
@ -141,11 +142,17 @@ namespace hex::plugin::builtin {
if (ImGui::BeginTabBar("storeTabs")) {
drawTab("hex.builtin.view.store.tab.patterns"_lang, fs::ImHexPath::Patterns, this->m_patterns);
drawTab("hex.builtin.view.store.tab.libraries"_lang, fs::ImHexPath::PatternsInclude, this->m_includes);
drawTab("hex.builtin.view.store.tab.magics"_lang, fs::ImHexPath::Magic, this->m_magics, magic::compile);
drawTab("hex.builtin.view.store.tab.magics"_lang, fs::ImHexPath::Magic, this->m_magics, []{
magic::compile();
});
drawTab("hex.builtin.view.store.tab.nodes"_lang, fs::ImHexPath::Nodes, this->m_nodes);
drawTab("hex.builtin.view.store.tab.encodings"_lang, fs::ImHexPath::Encodings, this->m_encodings);
drawTab("hex.builtin.view.store.tab.constants"_lang, fs::ImHexPath::Constants, this->m_constants);
drawTab("hex.builtin.view.store.tab.themes"_lang, fs::ImHexPath::Themes, this->m_themes);
drawTab("hex.builtin.view.store.tab.themes"_lang, fs::ImHexPath::Themes, this->m_themes, [this]{
auto themeFile = wolv::io::File(this->m_downloadPath, wolv::io::File::Mode::Read);
ThemeManager::addTheme(themeFile.readString());
});
drawTab("hex.builtin.view.store.tab.yara"_lang, fs::ImHexPath::Yara, this->m_yara);
ImGui::EndTabBar();

View File

@ -18,7 +18,7 @@ namespace hex::plugin::builtin {
ImGui::Header("hex.builtin.view.theme_manager.colors"_lang, true);
ImGui::PushID(1);
const auto &themeHandlers = api::ThemeManager::getThemeHandlers();
const auto &themeHandlers = ThemeManager::getThemeHandlers();
for (auto &[name, handler] : themeHandlers) {
if (ImGui::CollapsingHeader(name.c_str())) {
for (auto &[colorName, colorId] : handler.colorMap) {
@ -37,7 +37,7 @@ namespace hex::plugin::builtin {
ImGui::Header("hex.builtin.view.theme_manager.styles"_lang);
ImGui::PushID(2);
for (auto &[name, handler] : api::ThemeManager::getStyleHandlers()) {
for (auto &[name, handler] : ThemeManager::getStyleHandlers()) {
if (ImGui::CollapsingHeader(name.c_str())) {
for (auto &[styleName, style] : handler.styleMap) {
auto &[value, min, max, needsScaling] = style;
@ -55,7 +55,7 @@ namespace hex::plugin::builtin {
ImGui::InputTextIcon("hex.builtin.view.theme_manager.export.name"_lang, ICON_VS_SYMBOL_KEY, this->m_themeName);
if (ImGui::Button("hex.builtin.view.theme_manager.save_theme"_lang, ImVec2(ImGui::GetContentRegionAvail().x, 0))) {
fs::openFileBrowser(fs::DialogMode::Save, { { "ImHex Theme", "json" } }, [this](const std::fs::path &path){
auto json = api::ThemeManager::exportCurrentTheme(this->m_themeName);
auto json = ThemeManager::exportCurrentTheme(this->m_themeName);
wolv::io::File outputFile(path, wolv::io::File::Mode::Create);
outputFile.writeString(json.dump(4));

View File

@ -442,9 +442,9 @@ namespace hex::plugin::builtin {
(void)EventManager::subscribe<EventSettingsChanged>([]() {
{
auto theme = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme);
auto theme = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", ThemeManager::NativeTheme);
if (theme != api::ThemeManager::NativeTheme) {
if (theme != ThemeManager::NativeTheme) {
static std::string lastTheme;
if (theme != lastTheme) {
@ -474,9 +474,9 @@ namespace hex::plugin::builtin {
return ImGui::Texture(reinterpret_cast<const ImU8*>(textureData.data()), textureData.size());
};
api::ThemeManager::changeTheme(theme);
s_bannerTexture = changeTexture(hex::format("banner{}.png", api::ThemeManager::getThemeImagePostfix()));
s_backdropTexture = changeTexture(hex::format("backdrop{}.png", api::ThemeManager::getThemeImagePostfix()));
ThemeManager::changeTheme(theme);
s_bannerTexture = changeTexture(hex::format("banner{}.png", ThemeManager::getThemeImagePostfix()));
s_backdropTexture = changeTexture(hex::format("backdrop{}.png", ThemeManager::getThemeImagePostfix()));
if (!s_bannerTexture.isValid()) {
log::error("Failed to load banner texture!");

View File

@ -22,7 +22,7 @@ namespace hex::plugin::windows {
static void detectSystemTheme() {
// Setup system theme change detector
EventManager::subscribe<EventOSThemeChanged>([] {
bool themeFollowSystem = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme) == api::ThemeManager::NativeTheme;
bool themeFollowSystem = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", ThemeManager::NativeTheme) == ThemeManager::NativeTheme;
if (!themeFollowSystem)
return;
@ -43,7 +43,7 @@ static void detectSystemTheme() {
});
EventManager::subscribe<EventWindowInitialized>([=] {
bool themeFollowSystem = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme) == api::ThemeManager::NativeTheme;
bool themeFollowSystem = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", ThemeManager::NativeTheme) == ThemeManager::NativeTheme;
if (themeFollowSystem)
EventManager::post<EventOSThemeChanged>();