build: Move menu items handler into libimhex

This commit is contained in:
WerWolv
2025-02-16 15:48:44 +01:00
parent 67340e1526
commit 879e4c9d58
11 changed files with 12 additions and 16 deletions

View File

@@ -24,7 +24,7 @@
#include <wolv/literals.hpp>
#include <romfs/romfs.hpp>
#include <ui/menu_items.hpp>
#include <hex/helpers/menu_items.hpp>
#include <GLFW/glfw3.h>

View File

@@ -21,7 +21,7 @@
#include <ranges>
#include <unordered_set>
#include <ui/menu_items.hpp>
#include <hex/helpers/menu_items.hpp>
namespace hex::plugin::builtin::recent {

View File

@@ -20,7 +20,7 @@
#include <wolv/utils/string.hpp>
#include <string>
#include <ui/menu_items.hpp>
#include <hex/helpers/menu_items.hpp>
namespace hex::plugin::builtin {

View File

@@ -25,7 +25,7 @@
#include <content/popups/popup_blocking_task.hpp>
#include <content/popups/hex_editor/popup_hex_editor_find.hpp>
#include <pl/patterns/pattern.hpp>
#include <ui/menu_items.hpp>
#include <hex/helpers/menu_items.hpp>
#include <wolv/literals.hpp>
using namespace std::literals::string_literals;

View File

@@ -36,7 +36,7 @@
#include <content/global_actions.hpp>
#include <fonts/fonts.hpp>
#include <ui/menu_items.hpp>
#include <hex/helpers/menu_items.hpp>
namespace hex::plugin::builtin {

View File

@@ -12,7 +12,7 @@
#include <imgui_internal.h>
#include <random>
#include <hex/ui/imgui_imhex_extensions.h>
#include <ui/menu_items.hpp>
#include <hex/helpers/menu_items.hpp>
#include <fonts/vscode_icons.hpp>
#include <hex/api/tutorial_manager.hpp>

View File

@@ -17,11 +17,4 @@ add_imhex_plugin(
LIBRARIES
fonts
LIBRARY_PLUGIN
)
if (APPLE)
target_sources(ui PRIVATE source/ui/macos_menu.m)
find_library(FOUNDATION NAMES Foundation REQUIRED)
find_library(COCOA NAMES Cocoa REQUIRED)
target_link_libraries(ui PUBLIC ${FOUNDATION} ${COCOA})
endif()
)

View File

@@ -1,25 +0,0 @@
#pragma once
#include <hex/api/shortcut_manager.hpp>
namespace hex::menu {
void enableNativeMenuBar(bool enabled);
bool isNativeMenuBarUsed();
bool beginMainMenuBar();
void endMainMenuBar();
bool beginMenu(const char *label, bool enabled = true);
void endMenu();
bool beginMenuEx(const char* label, const char* icon, bool enabled = true);
bool menuItem(const char *label, const Shortcut &shortcut = Shortcut::None, bool selected = false, bool enabled = true);
bool menuItem(const char *label, const Shortcut &shortcut, bool *selected, bool enabled = true);
bool menuItemEx(const char *label, const char *icon, const Shortcut &shortcut = Shortcut::None, bool selected = false, bool enabled = true);
bool menuItemEx(const char *label, const char *icon, const Shortcut &shortcut, bool *selected, bool enabled = true);
void menuSeparator();
}

View File

@@ -1,181 +0,0 @@
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
struct KeyEquivalent {
bool valid;
bool ctrl, opt, cmd, shift;
int key;
};
const static int MenuBegin = 1;
static NSInteger s_currTag = MenuBegin;
static NSInteger s_selectedTag = -1;
@interface MenuItemHandler : NSObject
-(void) OnClick: (id) sender;
@end
@implementation MenuItemHandler
-(void) OnClick: (id) sender {
NSMenuItem* menu_item = sender;
s_selectedTag = menu_item.tag;
}
@end
static NSMenu* s_menuStack[1024];
static int s_menuStackSize = 0;
static MenuItemHandler* s_menuItemHandler;
static bool s_constructingMenu = false;
static bool s_resetNeeded = true;
void macosMenuBarInit(void) {
s_menuStack[0] = NSApp.mainMenu;
s_menuStackSize += 1;
s_menuItemHandler = [[MenuItemHandler alloc] init];
}
void macosClearMenu(void) {
// Remove all items except the Application menu
while (s_menuStack[0].itemArray.count > 2) {
[s_menuStack[0] removeItemAtIndex:1];
}
s_currTag = MenuBegin;
}
bool macosBeginMainMenuBar(void) {
if (s_resetNeeded) {
macosClearMenu();
s_resetNeeded = false;
}
return true;
}
void macosEndMainMenuBar(void) {
s_constructingMenu = false;
}
bool macosBeginMenu(const char* label, bool enabled) {
NSString* title = [NSString stringWithUTF8String:label];
// Search for menu item with the given name
NSInteger menuIndex = [s_menuStack[s_menuStackSize - 1] indexOfItemWithTitle:title];
if (menuIndex == -1) {
// Construct the content of the menu if it doesn't exist yet
s_constructingMenu = true;
NSMenu* newMenu = [[NSMenu alloc] init];
newMenu.autoenablesItems = false;
newMenu.title = title;
NSMenuItem* menuItem = [[NSMenuItem alloc] init];
menuItem.title = title;
[menuItem setSubmenu:newMenu];
// Add the new menu to the end of the list
menuIndex = [s_menuStack[s_menuStackSize - 1] numberOfItems];
if (s_menuStackSize == 1)
menuIndex -= 1;
[s_menuStack[s_menuStackSize - 1] insertItem:menuItem atIndex:menuIndex];
}
NSMenuItem* menuItem = [s_menuStack[s_menuStackSize - 1] itemAtIndex:menuIndex];
if (menuItem != NULL) {
menuItem.enabled = enabled;
s_menuStack[s_menuStackSize] = menuItem.submenu;
s_menuStackSize += 1;
}
return true;
}
void macosEndMenu(void) {
s_menuStack[s_menuStackSize - 1] = NULL;
s_menuStackSize -= 1;
}
bool macosMenuItem(const char* label, struct KeyEquivalent keyEquivalent, bool selected, bool enabled) {
NSString* title = [NSString stringWithUTF8String:label];
if (s_constructingMenu) {
NSMenuItem* menuItem = [[NSMenuItem alloc] init];
menuItem.title = title;
menuItem.action = @selector(OnClick:);
menuItem.target = s_menuItemHandler;
[menuItem setTag:s_currTag];
s_currTag += 1;
// Setup the key equivalent, aka the shortcut
if (keyEquivalent.valid) {
NSInteger flags = 0x00;
if (keyEquivalent.ctrl)
flags |= NSEventModifierFlagControl;
if (keyEquivalent.shift)
flags |= NSEventModifierFlagShift;
if (keyEquivalent.cmd)
flags |= NSEventModifierFlagCommand;
if (keyEquivalent.opt)
flags |= NSEventModifierFlagOption;
[menuItem setKeyEquivalentModifierMask:flags];
menuItem.keyEquivalent = [[NSString alloc] initWithCharacters:(const unichar *)&keyEquivalent.key length:1];
}
[s_menuStack[s_menuStackSize - 1] addItem:menuItem];
}
NSInteger menuIndex = [s_menuStack[s_menuStackSize - 1] indexOfItemWithTitle:title];
NSMenuItem* menuItem = NULL;
if (menuIndex >= 0 && menuIndex < [s_menuStack[s_menuStackSize - 1] numberOfItems]) {
menuItem = [s_menuStack[s_menuStackSize - 1] itemAtIndex:menuIndex];
if (menuItem != NULL) {
if (s_constructingMenu == false) {
if (![title isEqualToString:menuItem.title]) {
s_resetNeeded = true;
}
}
menuItem.enabled = enabled;
menuItem.state = selected ? NSControlStateValueOn : NSControlStateValueOff;
}
if (enabled && menuItem != NULL) {
if ([menuItem tag] == s_selectedTag) {
s_selectedTag = -1;
return true;
}
}
} else {
s_resetNeeded = true;
}
return false;
}
bool macosMenuItemSelect(const char* label, struct KeyEquivalent keyEquivalent, bool* selected, bool enabled) {
if (macosMenuItem(label, keyEquivalent, selected != NULL ? *selected : false, enabled)) {
if (selected != NULL)
*selected = !(*selected);
return true;
}
return false;
}
void macosSeparator(void) {
if (s_constructingMenu) {
NSMenuItem* separator = [NSMenuItem separatorItem];
[s_menuStack[s_menuStackSize - 1] addItem:separator];
}
}

View File

@@ -1,4 +1,4 @@
#include <ui/menu_items.hpp>
#include <../../../../lib/libimhex/include/hex/helpers/menu_items.hpp>
#include <imgui.h>
#include <imgui_internal.h>