diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 0264c692a..afdd28870 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -796,6 +796,7 @@ EXPORT_MODULE namespace hex { using EnabledCallback = std::function; using SelectedCallback = std::function; using ClickCallback = std::function; + using ToggleCallback = std::function; struct MainMenuItem { UnlocalizedString unlocalizedName; @@ -824,6 +825,13 @@ EXPORT_MODULE namespace hex { ClickCallback callback; }; + struct WelcomeScreenQuickSettingsToggle { + std::string onIcon, offIcon; + UnlocalizedString unlocalizedTooltip; + ToggleCallback callback; + mutable bool state; + }; + constexpr static auto SeparatorValue = "$SEPARATOR$"; constexpr static auto SubMenuValue = "$SUBMENU$"; @@ -838,6 +846,7 @@ EXPORT_MODULE namespace hex { const std::vector& getToolbarItems(); const std::vector& getSidebarItems(); const std::vector& getTitlebarButtons(); + const std::vector& getWelcomeScreenQuickSettingsToggles(); } @@ -1007,6 +1016,36 @@ EXPORT_MODULE namespace hex { const impl::ClickCallback &function ); + /** + * @brief Adds a new welcome screen quick settings toggle + * @param icon The icon to use for the button + * @param unlocalizedTooltip The unlocalized tooltip to use for the button + * @param defaultState The default state of the toggle + * @param function The function to call when the button is clicked + */ + void addWelcomeScreenQuickSettingsToggle( + const std::string &icon, + const UnlocalizedString &unlocalizedTooltip, + bool defaultState, + const impl::ToggleCallback &function + ); + + /** + * @brief Adds a new welcome screen quick settings toggle + * @param onIcon The icon to use for the button when it's on + * @param offIcon The icon to use for the button when it's off + * @param unlocalizedTooltip The unlocalized tooltip to use for the button + * @param defaultState The default state of the toggle + * @param function The function to call when the button is clicked + */ + void addWelcomeScreenQuickSettingsToggle( + const std::string &onIcon, + const std::string &offIcon, + const UnlocalizedString &unlocalizedTooltip, + bool defaultState, + const impl::ToggleCallback &function + ); + } /* Provider Registry. Allows adding new data providers to be created from the UI */ diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index c580e8d84..961ed8462 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -948,6 +948,11 @@ namespace hex { return *s_titlebarButtons; } + static AutoReset> s_welcomeScreenQuickSettingsToggles; + const std::vector& getWelcomeScreenQuickSettingsToggles() { + return *s_welcomeScreenQuickSettingsToggles; + } + } void registerMainMenuItem(const UnlocalizedString &unlocalizedName, u32 priority) { @@ -1070,6 +1075,15 @@ namespace hex { impl::s_titlebarButtons->push_back({ icon, unlocalizedTooltip, function }); } + void addWelcomeScreenQuickSettingsToggle(const std::string &icon, const UnlocalizedString &unlocalizedTooltip, bool defaultState, const impl::ToggleCallback &function) { + impl::s_welcomeScreenQuickSettingsToggles->push_back({ icon, icon, unlocalizedTooltip, function, defaultState }); + } + + void addWelcomeScreenQuickSettingsToggle(const std::string &onIcon, const std::string &offIcon, const UnlocalizedString &unlocalizedTooltip, bool defaultState, const impl::ToggleCallback &function) { + impl::s_welcomeScreenQuickSettingsToggles->push_back({ onIcon, offIcon, unlocalizedTooltip, function, defaultState }); + + } + } namespace ContentRegistry::Provider { diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index 6a982d138..f8396d298 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -519,11 +519,19 @@ namespace hex::plugin::builtin { ImGui::SetCursorScreenPos(ImGui::GetWindowPos() + ImGui::GetWindowSize() - windowSize - ImGui::GetStyle().WindowPadding); ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg)); if (ImGuiExt::BeginSubWindow("hex.builtin.welcome.header.quick_settings"_lang, nullptr, windowSize, ImGuiChildFlags_AutoResizeY)) { - if (ImGuiExt::DimmedIconToggle(ICON_VS_COMPASS_ACTIVE, ICON_VS_COMPASS, &s_simplifiedWelcomeScreen)) { - ContentRegistry::Settings::write("hex.builtin.setting.interface", "hex.builtin.setting.interface.simplified_welcome_screen", s_simplifiedWelcomeScreen); - WorkspaceManager::switchWorkspace(s_simplifiedWelcomeScreen ? "Minimal" : "Default"); + u32 id = 0; + for (auto &[onIcon, offIcon, unlocalizedTooltip, toggleCallback, state] : ContentRegistry::Interface::impl::getWelcomeScreenQuickSettingsToggles()) { + ImGui::PushID(id + 1); + if (ImGuiExt::DimmedIconToggle(onIcon.c_str(), offIcon.c_str(), &state)) { + toggleCallback(state); + ContentRegistry::Settings::write("hex.builtin.settings.quick_settings", unlocalizedTooltip, state); + } + if (id % 5 > 0) + ImGui::SameLine(); + ImGui::SetItemTooltip("%s", Lang(unlocalizedTooltip).get()); + ImGui::PopID(); + id += 1; } - ImGui::SetItemTooltip("%s", "hex.builtin.welcome.quick_settings.simplified"_lang.get()); } ImGuiExt::EndSubWindow(); @@ -634,6 +642,31 @@ namespace hex::plugin::builtin { ImHexApi::System::setTargetFPS(static_cast(value.get(14))); }); + + ContentRegistry::Interface::addWelcomeScreenQuickSettingsToggle(ICON_VS_COMPASS_ACTIVE, ICON_VS_COMPASS, "hex.builtin.welcome.quick_settings.simplified", false, [](bool state) { + s_simplifiedWelcomeScreen = state; + WorkspaceManager::switchWorkspace(s_simplifiedWelcomeScreen ? "Minimal" : "Default"); + }); + + EventImHexStartupFinished::subscribe([]() { + for (const auto &quickSetting : ContentRegistry::Interface::impl::getWelcomeScreenQuickSettingsToggles()) { + auto &setting = quickSetting.unlocalizedTooltip; + ContentRegistry::Settings::onChange("hex.builtin.settings.quick_settings", setting, [setting](const ContentRegistry::Settings::SettingsValue &value) { + for (auto &[onIcon, offIcon, unlocalizedTooltip, toggleCallback, state] : ContentRegistry::Interface::impl::getWelcomeScreenQuickSettingsToggles()) { + if (unlocalizedTooltip == setting) { + state = value.get(state); + toggleCallback(state); + break; + } + } + }); + + bool state = ContentRegistry::Settings::read("hex.builtin.settings.quick_settings", quickSetting.unlocalizedTooltip, quickSetting.state); + quickSetting.state = state; + quickSetting.callback(state); + } + }); + static auto updateTextures = [](float scale) { auto changeTexture = [&](const std::string &path) { return ImGuiExt::Texture::fromImage(romfs::get(path).span(), ImGuiExt::Texture::Filter::Nearest);