From 94b53592d9f4b8c6531d5434492d8ab9c59fa7ab Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 3 Dec 2025 19:21:23 +0100 Subject: [PATCH] impr: Only show remaining Open File / New File menu options when they make sense --- .../hex/api/content_registry/user_interface.hpp | 8 ++++++-- lib/libimhex/source/api/content_registry.cpp | 8 ++++---- plugins/builtin/source/content/main_menu_items.cpp | 10 +++++----- plugins/builtin/source/content/recent.cpp | 3 ++- plugins/builtin/source/content/window_decoration.cpp | 10 +++++++++- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry/user_interface.hpp b/lib/libimhex/include/hex/api/content_registry/user_interface.hpp index 70ef6dcd0..b3e68cbfe 100644 --- a/lib/libimhex/include/hex/api/content_registry/user_interface.hpp +++ b/lib/libimhex/include/hex/api/content_registry/user_interface.hpp @@ -159,13 +159,15 @@ EXPORT_MODULE namespace hex { * @param function The function to call when the entry is clicked * @param enabledCallback The function to call to determine if the entry is enabled * @param view The view to use for the entry. If nullptr, the item will always be visible + * @param showOnWelcomeScreen If this entry should be shown on the welcome screen */ void addMenuItemSubMenu( std::vector unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; }, - View *view = nullptr + View *view = nullptr, + bool showOnWelcomeScreen = false ); /** @@ -176,6 +178,7 @@ EXPORT_MODULE namespace hex { * @param function The function to call when the entry is clicked * @param enabledCallback The function to call to determine if the entry is enabled * @param view The view to use for the entry. If nullptr, the item will always be visible + * @param showOnWelcomeScreen If this entry should be shown on the welcome screen */ void addMenuItemSubMenu( std::vector unlocalizedMainMenuNames, @@ -183,7 +186,8 @@ EXPORT_MODULE namespace hex { u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; }, - View *view = nullptr + View *view = nullptr, + bool showOnWelcomeScreen = false ); diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index c0dd1ea1c..853d03b57 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -1006,16 +1006,16 @@ namespace hex { } } - void addMenuItemSubMenu(std::vector unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) { - addMenuItemSubMenu(std::move(unlocalizedMainMenuNames), "", priority, function, enabledCallback, view); + void addMenuItemSubMenu(std::vector unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view, bool showOnWelcomeScreen) { + addMenuItemSubMenu(std::move(unlocalizedMainMenuNames), "", priority, function, enabledCallback, view, showOnWelcomeScreen); } - void addMenuItemSubMenu(std::vector unlocalizedMainMenuNames, const char *icon, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) { + void addMenuItemSubMenu(std::vector unlocalizedMainMenuNames, const char *icon, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view, bool showOnWelcomeScreen) { log::debug("Added new menu item sub menu to menu {} with priority {}", unlocalizedMainMenuNames[0].get(), priority); unlocalizedMainMenuNames.emplace_back(impl::SubMenuValue); impl::s_menuItems->insert({ - priority, impl::MenuItem { unlocalizedMainMenuNames, icon, Shortcut::None, view, function, enabledCallback, []{ return false; }, -1 } + priority, impl::MenuItem { unlocalizedMainMenuNames, icon, showOnWelcomeScreen ? Shortcut({ ShowOnWelcomeScreen }) : Shortcut::None, view, function, enabledCallback, []{ return false; }, -1 } }); } diff --git a/plugins/builtin/source/content/main_menu_items.cpp b/plugins/builtin/source/content/main_menu_items.cpp index 2c25201cf..88a325ed8 100644 --- a/plugins/builtin/source/content/main_menu_items.cpp +++ b/plugins/builtin/source/content/main_menu_items.cpp @@ -375,13 +375,13 @@ namespace hex::plugin::builtin { ContentRegistry::UserInterface::registerMainMenuItem("hex.builtin.menu.file", 1000); /* Create File */ - ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.create_file" }, ICON_VS_FILE, 1050, CTRLCMD + Keys::N + AllowWhileTyping, [] { + ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.create_file" }, ICON_VS_FILE, 1050, CTRLCMD + Keys::N + AllowWhileTyping + ShowOnWelcomeScreen, [] { auto newProvider = hex::ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true); if (newProvider != nullptr && !newProvider->open()) hex::ImHexApi::Provider::remove(newProvider); else EventProviderOpened::post(newProvider); - }, noRunningTasks); + }, noRunningTasks, ContentRegistry::Views::getViewByName("hex.builtin.view.hex_editor.name")); /* Open File */ ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.open_file" }, ICON_VS_FOLDER_OPENED, 1100, CTRLCMD + Keys::O + AllowWhileTyping + ShowOnWelcomeScreen, [] { @@ -394,10 +394,10 @@ namespace hex::plugin::builtin { if (menu::menuItemEx(Lang(unlocalizedProviderName), icon)) ImHexApi::Provider::createProvider(unlocalizedProviderName); } - }, noRunningTasks); + }, noRunningTasks, ContentRegistry::Views::getViewByName("hex.builtin.view.hex_editor.name"), true); /* Reload Provider */ - ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.reload_provider"}, ICON_VS_REFRESH, 1250, CTRLCMD + Keys::R + AllowWhileTyping, [] { + ContentRegistry::UserInterface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.reload_provider"}, ICON_VS_REFRESH, 1250, CTRLCMD + Keys::R + AllowWhileTyping + ShowOnWelcomeScreen, [] { auto provider = ImHexApi::Provider::get(); provider->close(); @@ -405,7 +405,7 @@ namespace hex::plugin::builtin { ImHexApi::Provider::remove(provider, true); EventDataChanged::post(provider); - }, noRunningTaskAndValidProvider); + }, noRunningTaskAndValidProvider, ContentRegistry::Views::getViewByName("hex.builtin.view.hex_editor.name")); /* Project open / save */ diff --git a/plugins/builtin/source/content/recent.cpp b/plugins/builtin/source/content/recent.cpp index 4eef85078..0d56e5801 100644 --- a/plugins/builtin/source/content/recent.cpp +++ b/plugins/builtin/source/content/recent.cpp @@ -24,6 +24,7 @@ #include #include +#include #include namespace hex::plugin::builtin::recent { @@ -426,6 +427,6 @@ namespace hex::plugin::builtin::recent { } }, [] { return TaskManager::getRunningTaskCount() == 0 && !s_recentEntriesUpdating && !s_recentEntries.empty(); - }); + }, ContentRegistry::Views::getViewByName("hex.builtin.view.hex_editor.name"), true); } } diff --git a/plugins/builtin/source/content/window_decoration.cpp b/plugins/builtin/source/content/window_decoration.cpp index 479cb33fb..f46ae274b 100644 --- a/plugins/builtin/source/content/window_decoration.cpp +++ b/plugins/builtin/source/content/window_decoration.cpp @@ -52,11 +52,14 @@ namespace hex::plugin::builtin { ImGui::GetWindowDrawList()->AddShadowCircle(pos, diameter / 2, ImGui::GetColorU32(ImGuiCol_ButtonActive, 0.8F), diameter / 4, ImVec2()); } + bool allowSeparator = false; void createNestedMenu(std::span menuItems, const char *icon, const Shortcut &shortcut, View *view, const ContentRegistry::UserInterface::impl::MenuCallback &callback, const ContentRegistry::UserInterface::impl::EnabledCallback &enabledCallback, const ContentRegistry::UserInterface::impl::SelectedCallback &selectedCallback) { const auto &name = menuItems.front(); if (name.get() == ContentRegistry::UserInterface::impl::SeparatorValue) { - menu::menuSeparator(); + if (allowSeparator) + menu::menuSeparator(); + allowSeparator = false; return; } @@ -80,10 +83,13 @@ namespace hex::plugin::builtin { bool isSubmenu = (menuItems.begin() + 1)->get() == ContentRegistry::UserInterface::impl::SubMenuValue; if (menu::beginMenuEx(Lang(name), std::next(menuItems.begin())->get() == ContentRegistry::UserInterface::impl::SubMenuValue ? icon : nullptr, isSubmenu ? enabledCallback() : true)) { + allowSeparator = true; createNestedMenu({ std::next(menuItems.begin()), menuItems.end() }, icon, shortcut, view, callback, enabledCallback, selectedCallback); menu::endMenu(); } } + + allowSeparator = true; } void drawFooter(ImDrawList *drawList, ImVec2 dockSpaceSize) { @@ -342,6 +348,8 @@ namespace hex::plugin::builtin { } void populateMenu(const UnlocalizedString &menuName) { + allowSeparator = false; + const auto lastFocusedView = View::getLastFocusedView(); for (auto &[priority, menuItem] : ContentRegistry::UserInterface::impl::getMenuItems()) { if (!menuName.empty()) {