diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 02bd242b4..fa9351fd0 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -12,6 +12,7 @@ #include #include #include +#include using ImGuiID = unsigned int; struct ImVec2; @@ -158,6 +159,9 @@ namespace hex { void setProgramArguments(int argc, char **argv, char **envp); void setBorderlessWindowMode(bool enabled); + + void setCustomFontPath(const std::fs::path &path); + void setFontSize(float size); } struct ProgramArguments { @@ -166,6 +170,12 @@ namespace hex { char **envp; }; + enum class Theme { + Dark = 1, + Light = 2, + Classic = 3 + }; + const ProgramArguments &getProgramArguments(); float getTargetFPS(); @@ -181,6 +191,14 @@ namespace hex { std::map &getInitArguments(); + const std::fs::path &getCustomFontPath(); + float getFontSize(); + + void setTheme(Theme theme); + Theme getTheme(); + + void enableSystemThemeDetection(bool enabled); + bool usesSystemThemeDetection(); } } diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 139414665..1ce08779e 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -1,13 +1,16 @@ #include +#include #include #include -#include +#include #include #include +#include + namespace hex { namespace ImHexApi::Common { @@ -315,6 +318,16 @@ namespace hex { s_borderlessWindowMode = enabled; } + static std::fs::path s_customFontPath; + void setCustomFontPath(const std::fs::path &path) { + s_customFontPath = path; + } + + static float s_fontSize; + void setFontSize(float size) { + s_fontSize = size; + } + } @@ -360,6 +373,39 @@ namespace hex { return initArgs; } + + const std::fs::path &getCustomFontPath() { + return impl::s_customFontPath; + } + + float getFontSize() { + return impl::s_fontSize; + } + + + static Theme s_theme; + static bool s_systemThemeDetection; + + void setTheme(Theme theme) { + s_theme = theme; + + EventManager::post(); + } + + Theme getTheme() { + return s_theme; + } + + + void enableSystemThemeDetection(bool enabled) { + s_systemThemeDetection = enabled; + + EventManager::post(); + } + + bool usesSystemThemeDetection() { + return s_systemThemeDetection; + } } } diff --git a/main/source/init/splash_window.cpp b/main/source/init/splash_window.cpp index 4926dd3a1..da5a6e339 100644 --- a/main/source/init/splash_window.cpp +++ b/main/source/init/splash_window.cpp @@ -185,6 +185,8 @@ namespace hex::init { } ImHexApi::System::impl::setGlobalScale(meanScale); + } else { + ImHexApi::System::impl::setGlobalScale(1.0); } this->m_window = glfwCreateWindow(640_scaled, 400_scaled, "Starting ImHex...", nullptr, nullptr); diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 04e2dd7df..901868af0 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -93,23 +93,6 @@ namespace hex::init { auto fonts = IM_NEW(ImFontAtlas)(); ImFontConfig cfg = {}; - std::fs::path fontFile = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_path", ""); - if (!fs::exists(fontFile)) - fontFile.clear(); - - // If no custom font has been specified, search for a file called "font.ttf" in one of the resource folders - if (fontFile.empty()) { - for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Resources)) { - auto path = dir / "font.ttf"; - if (fs::exists(path)) { - log::info("Loading custom front from {}", path.string()); - - fontFile = path; - break; - } - } - } - ImVector ranges; { ImFontGlyphRangesBuilder glyphRangesBuilder; @@ -135,7 +118,8 @@ namespace hex::init { 0x0100, 0xFFF0, 0 }; - float fontSize = 13.0F * ImHexApi::System::getGlobalScale(); + auto fontFile = ImHexApi::System::getCustomFontPath(); + float fontSize = ImHexApi::System::getFontSize(); if (fontFile.empty()) { // Load default font if no custom one has been specified @@ -147,8 +131,6 @@ namespace hex::init { } else { // Load custom font - fontSize = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13) * ImHexApi::System::getGlobalScale(); - cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true; cfg.SizePixels = fontSize; @@ -303,28 +285,6 @@ namespace hex::init { return false; } - float interfaceScaling = 1.0F; - switch (ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling", 0)) { - default: - case 0: - // Native scaling - break; - case 1: - interfaceScaling = 0.5F; - break; - case 2: - interfaceScaling = 1.0F; - break; - case 3: - interfaceScaling = 1.5F; - break; - case 4: - interfaceScaling = 2.0F; - break; - } - - ImHexApi::System::impl::setGlobalScale(interfaceScaling); - return true; } diff --git a/main/source/window/linux_window.cpp b/main/source/window/linux_window.cpp index ba58d0981..231c12791 100644 --- a/main/source/window/linux_window.cpp +++ b/main/source/window/linux_window.cpp @@ -21,7 +21,7 @@ namespace hex { } void Window::setupNativeWindow() { - bool themeFollowSystem = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color") == 0; + bool themeFollowSystem = ImHexApi::System::usesSystemThemeDetection(); EventManager::subscribe(this, [themeFollowSystem] { if (!themeFollowSystem) return; diff --git a/main/source/window/macos_window.cpp b/main/source/window/macos_window.cpp index efcf4968c..c8678b1e5 100644 --- a/main/source/window/macos_window.cpp +++ b/main/source/window/macos_window.cpp @@ -20,7 +20,7 @@ namespace hex { } void Window::setupNativeWindow() { - bool themeFollowSystem = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color") == 0; + bool themeFollowSystem = ImHexApi::System::usesSystemThemeDetection(); EventManager::subscribe(this, [themeFollowSystem] { if (!themeFollowSystem) return; diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index deab7f3a2..5246a150c 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -1,9 +1,11 @@ #include +#include #include #include #include #include +#include #include diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 49ed0d788..c2b349033 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -1,13 +1,15 @@ +#include #include #include - #include +#include +#include +#include + #include #include #include -#include -#include #include @@ -53,6 +55,11 @@ namespace hex::plugin::builtin { if (ImGui::Combo(name.data(), &selection, themes, IM_ARRAYSIZE(themes))) { setting = selection; + + ImHexApi::System::enableSystemThemeDetection(selection == 0); + if (selection != 0) + ImHexApi::System::setTheme(static_cast(selection)); + return true; } @@ -392,4 +399,71 @@ namespace hex::plugin::builtin { false); } + + static void loadInterfaceScalingSetting() { + float interfaceScaling = 1.0F; + switch (ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.scaling", 0)) { + default: + case 0: + // Native scaling + break; + case 1: + interfaceScaling = 0.5F; + break; + case 2: + interfaceScaling = 1.0F; + break; + case 3: + interfaceScaling = 1.5F; + break; + case 4: + interfaceScaling = 2.0F; + break; + } + + ImHexApi::System::impl::setGlobalScale(interfaceScaling); + } + + static void loadFontSettings() { + std::fs::path fontFile = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_path", ""); + if (!fs::exists(fontFile)) + fontFile.clear(); + + // If no custom font has been specified, search for a file called "font.ttf" in one of the resource folders + if (fontFile.empty()) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Resources)) { + auto path = dir / "font.ttf"; + if (fs::exists(path)) { + log::info("Loading custom front from {}", path.string()); + + fontFile = path; + break; + } + } + } + + // If a custom font has been loaded now, also load the font size + float fontSize = 13.0F * ImHexApi::System::getGlobalScale(); + if (!fontFile.empty()) { + ImHexApi::System::impl::setCustomFontPath(fontFile); + + fontSize = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_size", 13) * ImHexApi::System::getGlobalScale(); + } + + ImHexApi::System::impl::setFontSize(fontSize); + } + + static void loadThemeSettings() { + auto theme = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", static_cast(ImHexApi::System::Theme::Dark)); + + ImHexApi::System::enableSystemThemeDetection(theme == 0); + ImHexApi::System::setTheme(static_cast(theme)); + } + + void loadSettings() { + loadInterfaceScalingSetting(); + loadFontSettings(); + loadThemeSettings(); + } + } diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index fa17a1cce..e76660a1e 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -1,19 +1,15 @@ #include +#include #include #include #include -#include - -#include #include #include #include #include -#include - namespace hex::plugin::builtin { static void drawGlobalPopups() { diff --git a/plugins/builtin/source/plugin_builtin.cpp b/plugins/builtin/source/plugin_builtin.cpp index 40cade687..bd067799b 100644 --- a/plugins/builtin/source/plugin_builtin.cpp +++ b/plugins/builtin/source/plugin_builtin.cpp @@ -10,6 +10,7 @@ namespace hex::plugin::builtin { void registerPatternLanguagePragmas(); void registerCommandPaletteCommands(); void registerSettings(); + void loadSettings(); void registerDataProcessorNodes(); void registerHashes(); void registerProviders(); @@ -51,6 +52,7 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") { registerPatternLanguagePragmas(); registerCommandPaletteCommands(); registerSettings(); + loadSettings(); registerDataProcessorNodes(); registerHashes(); registerProviders();