impr: Only show remaining Open File / New File menu options when they make sense

This commit is contained in:
WerWolv
2025-12-03 19:21:23 +01:00
parent 09006588fc
commit 94b53592d9
5 changed files with 26 additions and 13 deletions

View File

@@ -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<UnlocalizedString> 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<UnlocalizedString> 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
);

View File

@@ -1006,16 +1006,16 @@ namespace hex {
}
}
void addMenuItemSubMenu(std::vector<UnlocalizedString> 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<UnlocalizedString> 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<UnlocalizedString> unlocalizedMainMenuNames, const char *icon, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) {
void addMenuItemSubMenu(std::vector<UnlocalizedString> 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 }
});
}

View File

@@ -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 */

View File

@@ -24,6 +24,7 @@
#include <ranges>
#include <unordered_set>
#include <hex/api/content_registry/views.hpp>
#include <hex/helpers/menu_items.hpp>
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);
}
}

View File

@@ -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<const UnlocalizedString> 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()) {