From 8397af5c9ba16ba453f13c92395dd312b3b3b327 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 23 Jan 2022 20:45:51 +0100 Subject: [PATCH] api: Make main menus use the same priority API --- .../include/hex/api/content_registry.hpp | 5 ++-- .../include/hex/helpers/shared_data.hpp | 2 +- lib/libimhex/source/api/content_registry.cpp | 6 ++--- lib/libimhex/source/helpers/shared_data.cpp | 2 +- main/source/window/window.cpp | 5 ++-- .../source/content/main_menu_items.cpp | 26 ++++++++++--------- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 68abfb3be..e7d792224 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -229,7 +229,6 @@ namespace hex { struct MainMenuItem { std::string unlocalizedName; - DrawCallback callback; }; struct MenuItem { @@ -246,7 +245,7 @@ namespace hex { u32 getDockSpaceId(); - void registerMainMenuItem(const std::string &unlocalizedName, const impl::DrawCallback &function = []{}); + void registerMainMenuItem(const std::string &unlocalizedName, u32 priority); void addMenuItem(const std::string &unlocalizedMainMenuName, u32 priority, const impl::DrawCallback &function); void addWelcomeScreenEntry(const impl::DrawCallback &function); @@ -256,7 +255,7 @@ namespace hex { void addLayout(const std::string &unlocalizedName, const impl::LayoutFunction &function); - std::vector& getMainMenuItems(); + std::multimap& getMainMenuItems(); std::multimap& getMenuItems(); std::vector& getWelcomeScreenEntries(); diff --git a/lib/libimhex/include/hex/helpers/shared_data.hpp b/lib/libimhex/include/hex/helpers/shared_data.hpp index fa082a4d5..83dc41663 100644 --- a/lib/libimhex/include/hex/helpers/shared_data.hpp +++ b/lib/libimhex/include/hex/helpers/shared_data.hpp @@ -77,7 +77,7 @@ namespace hex { static ImGuiID dockSpaceId; - static std::vector mainMenuItems; + static std::multimap mainMenuItems; static std::multimap menuItems; static std::vector welcomeScreenEntries; static std::vector footerItems; diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 4db8b5de5..eb10ea9d3 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -294,10 +294,10 @@ namespace hex { return SharedData::dockSpaceId; } - void ContentRegistry::Interface::registerMainMenuItem(const std::string &unlocalizedName, const impl::DrawCallback &function) { + void ContentRegistry::Interface::registerMainMenuItem(const std::string &unlocalizedName, u32 priority) { log::info("Registered new main menu item: {}", unlocalizedName); - getMainMenuItems().push_back({ unlocalizedName, function }); + getMainMenuItems().insert({ priority, { unlocalizedName } }); } void ContentRegistry::Interface::addMenuItem(const std::string &unlocalizedMainMenuName, u32 priority, const impl::DrawCallback &function) { @@ -329,7 +329,7 @@ namespace hex { } - std::vector& ContentRegistry::Interface::getMainMenuItems() { + std::multimap& ContentRegistry::Interface::getMainMenuItems() { return SharedData::mainMenuItems; } std::multimap& ContentRegistry::Interface::getMenuItems() { diff --git a/lib/libimhex/source/helpers/shared_data.cpp b/lib/libimhex/source/helpers/shared_data.cpp index 82bc44d9d..39ed6b8ff 100644 --- a/lib/libimhex/source/helpers/shared_data.cpp +++ b/lib/libimhex/source/helpers/shared_data.cpp @@ -27,7 +27,7 @@ namespace hex { ImGuiID SharedData::dockSpaceId; - std::vector SharedData::mainMenuItems; + std::multimap SharedData::mainMenuItems; std::multimap SharedData::menuItems; std::vector SharedData::welcomeScreenEntries; diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index b5c46c9b4..83b88c173 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -431,9 +431,8 @@ namespace hex { ImGui::SetCursorPosX(5); ImGui::Image(this->m_logoTexture, ImVec2(menuBarHeight, menuBarHeight)); - for (const auto &[name, function] : ContentRegistry::Interface::getMainMenuItems()) { - if (ImGui::BeginMenu(LangEntry(name))) { - function(); + for (const auto &[priority, menuItem] : ContentRegistry::Interface::getMainMenuItems()) { + if (ImGui::BeginMenu(LangEntry(menuItem.unlocalizedName))) { ImGui::EndMenu(); } } diff --git a/plugins/builtin/source/content/main_menu_items.cpp b/plugins/builtin/source/content/main_menu_items.cpp index 439dc04ec..62c448338 100644 --- a/plugins/builtin/source/content/main_menu_items.cpp +++ b/plugins/builtin/source/content/main_menu_items.cpp @@ -11,22 +11,26 @@ namespace hex::plugin::builtin { void registerMainMenuEntries() { - ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.file"); - ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.edit"); + ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.file", 1000); + ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.edit", 2000); + ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.view", 3000); + ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.layout", 4000); + ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.help", 5000); - ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.view", [] { + ContentRegistry::Interface::addMenuItem("hex.builtin.menu.view", 1000, []{ for (auto &[name, view] : ContentRegistry::Views::getEntries()) { if (view->hasViewMenuItemEntry()) ImGui::MenuItem(LangEntry(view->getUnlocalizedName()), "", &view->getWindowOpenState()); } - - #if defined(DEBUG) - ImGui::Separator(); - ImGui::MenuItem("hex.builtin.menu.view.demo"_lang, "", &g_demoWindowOpen); - #endif }); - ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.layout", [] { + #if defined(DEBUG) + ContentRegistry::Interface::addMenuItem("hex.builtin.menu.view", 2000, []{ + ImGui::MenuItem("hex.builtin.menu.view.demo"_lang, "", &g_demoWindowOpen); + }); + #endif + + ContentRegistry::Interface::addMenuItem("hex.builtin.menu.layout", 1000, [] { for (auto &[layoutName, func] : ContentRegistry::Interface::getLayouts()) { if (ImGui::MenuItem(LangEntry(layoutName), "", false, ImHexApi::Provider::isValid())) { auto dock = ContentRegistry::Interface::getDockSpaceId(); @@ -43,15 +47,13 @@ namespace hex::plugin::builtin { } }); + (void) EventManager::subscribe([]{ if (g_demoWindowOpen) { ImGui::ShowDemoWindow(&g_demoWindowOpen); ImPlot::ShowDemoWindow(&g_demoWindowOpen); } }); - - ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.help"); - } } \ No newline at end of file