1
0
mirror of synced 2024-11-28 09:30:51 +01:00

fix: All menu item shortcuts being global

This commit is contained in:
WerWolv 2023-03-20 15:12:12 +01:00
parent 6e23560e80
commit a4dfaba03f
6 changed files with 35 additions and 22 deletions

View File

@ -337,8 +337,7 @@ namespace hex {
} }
void registerMainMenuItem(const std::string &unlocalizedName, u32 priority); void registerMainMenuItem(const std::string &unlocalizedName, u32 priority);
void addMenuItem(const std::string &unlocalizedMainMenuNames, u32 priority, const impl::DrawCallback &function); void addMenuItem(const std::vector<std::string> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; }, View *view = nullptr);
void addMenuItem(const std::vector<std::string> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; });
void addMenuItemSubMenu(std::vector<std::string> unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; }); void addMenuItemSubMenu(std::vector<std::string> unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; });
void addMenuItemSeparator(std::vector<std::string> unlocalizedMainMenuNames, u32 priority); void addMenuItemSeparator(std::vector<std::string> unlocalizedMainMenuNames, u32 priority);

View File

@ -143,10 +143,11 @@ namespace hex {
}; };
constexpr static auto CTRL = Key(static_cast<Keys>(0x1000'0000)); constexpr static auto CTRL = Key(static_cast<Keys>(0x0100'0000));
constexpr static auto ALT = Key(static_cast<Keys>(0x2000'0000)); constexpr static auto ALT = Key(static_cast<Keys>(0x0200'0000));
constexpr static auto SHIFT = Key(static_cast<Keys>(0x4000'0000)); constexpr static auto SHIFT = Key(static_cast<Keys>(0x0400'0000));
constexpr static auto SUPER = Key(static_cast<Keys>(0x8000'0000)); constexpr static auto SUPER = Key(static_cast<Keys>(0x0800'0000));
constexpr static auto CurrentView = Key(static_cast<Keys>(0x1000'0000));
#if defined (OS_MACOS) #if defined (OS_MACOS)
constexpr static auto CTRLCMD = SUPER; constexpr static auto CTRLCMD = SUPER;
@ -182,6 +183,10 @@ namespace hex {
return this->m_keys == other.m_keys; return this->m_keys == other.m_keys;
} }
bool isLocal() const {
return this->m_keys.contains(CurrentView);
}
std::string toString() const { std::string toString() const {
std::string result; std::string result;
@ -216,6 +221,7 @@ namespace hex {
result += SUPER_NAME; result += SUPER_NAME;
result += Concatination; result += Concatination;
} }
keys.erase(CurrentView);
for (const auto &key : keys) { for (const auto &key : keys) {
switch (Keys(key.getKeyCode())) { switch (Keys(key.getKeyCode())) {

View File

@ -486,14 +486,17 @@ namespace hex {
getMainMenuItems().insert({ priority, { unlocalizedName } }); getMainMenuItems().insert({ priority, { unlocalizedName } });
} }
void addMenuItem(const std::vector<std::string> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback) { void addMenuItem(const std::vector<std::string> &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) {
log::debug("Added new menu item to menu {} with priority {}", wolv::util::combineStrings(unlocalizedMainMenuNames, " -> "), priority); log::debug("Added new menu item to menu {} with priority {}", wolv::util::combineStrings(unlocalizedMainMenuNames, " -> "), priority);
getMenuItems().insert({ getMenuItems().insert({
priority, { unlocalizedMainMenuNames, shortcut, function, enabledCallback } priority, { unlocalizedMainMenuNames, shortcut, function, enabledCallback }
}); });
ShortcutManager::addGlobalShortcut(shortcut, function); if (shortcut.isLocal() && view != nullptr)
ShortcutManager::addShortcut(view, shortcut, function);
else
ShortcutManager::addGlobalShortcut(shortcut, function);
} }
void addMenuItemSubMenu(std::vector<std::string> unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback) { void addMenuItemSubMenu(std::vector<std::string> unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback) {

View File

@ -377,7 +377,7 @@ namespace hex::plugin::builtin {
void ViewBookmarks::registerMenuItems() { void ViewBookmarks::registerMenuItems() {
/* Create bookmark */ /* Create bookmark */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.menu.edit.bookmark.create" }, 1900, CTRL + Keys::B, [&] { ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.menu.edit.bookmark.create" }, 1900, CTRLCMD + Keys::B, [&] {
auto selection = ImHexApi::HexEditor::getSelection(); auto selection = ImHexApi::HexEditor::getSelection();
ImHexApi::Bookmarks::add(selection->getStartAddress(), selection->getSize(), {}, {}); ImHexApi::Bookmarks::add(selection->getStartAddress(), selection->getSize(), {}, {});
}, []{ return ImHexApi::Provider::isValid() && ImHexApi::HexEditor::isSelectionValid(); }); }, []{ return ImHexApi::Provider::isValid() && ImHexApi::HexEditor::isSelectionValid(); });

View File

@ -9,7 +9,7 @@ namespace hex::plugin::builtin {
ViewCommandPalette::ViewCommandPalette() : View("hex.builtin.view.command_palette.name") { ViewCommandPalette::ViewCommandPalette() : View("hex.builtin.view.command_palette.name") {
this->m_commandBuffer = std::vector<char>(1024, 0x00); this->m_commandBuffer = std::vector<char>(1024, 0x00);
ShortcutManager::addGlobalShortcut(CTRL + SHIFT + Keys::P, [this] { ShortcutManager::addGlobalShortcut(CTRLCMD + SHIFT + Keys::P, [this] {
EventManager::post<RequestOpenPopup>("hex.builtin.view.command_palette.name"_lang); EventManager::post<RequestOpenPopup>("hex.builtin.view.command_palette.name"_lang);
this->m_commandPaletteOpen = true; this->m_commandPaletteOpen = true;
this->m_justOpened = true; this->m_justOpened = true;

View File

@ -960,7 +960,7 @@ namespace hex::plugin::builtin {
/* Save */ /* Save */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.view.hex_editor.menu.file.save"_lang }, 1350, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.view.hex_editor.menu.file.save"_lang }, 1350,
CTRL + Keys::S, CTRLCMD + Keys::S,
save, save,
[] { [] {
auto provider = ImHexApi::Provider::get(); auto provider = ImHexApi::Provider::get();
@ -971,7 +971,7 @@ namespace hex::plugin::builtin {
/* Save As */ /* Save As */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.view.hex_editor.menu.file.save_as"_lang }, 1375, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.view.hex_editor.menu.file.save_as"_lang }, 1375,
CTRL + SHIFT + Keys::S, CTRLCMD + SHIFT + Keys::S,
saveAs, saveAs,
[] { [] {
auto provider = ImHexApi::Provider::get(); auto provider = ImHexApi::Provider::get();
@ -1033,23 +1033,25 @@ namespace hex::plugin::builtin {
/* Copy */ /* Copy */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.copy" }, 1150, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.copy" }, 1150,
CTRLCMD + Keys::C, CurrentView + CTRLCMD + Keys::C,
[] { [] {
auto selection = ImHexApi::HexEditor::getSelection(); auto selection = ImHexApi::HexEditor::getSelection();
if (selection.has_value() && selection != Region::Invalid()) if (selection.has_value() && selection != Region::Invalid())
copyBytes(*selection); copyBytes(*selection);
}, },
ImHexApi::HexEditor::isSelectionValid); ImHexApi::HexEditor::isSelectionValid,
this);
/* Copy As */ /* Copy As */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.copy_as", "hex.builtin.view.hex_editor.copy.ascii" }, 1200, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.copy_as", "hex.builtin.view.hex_editor.copy.ascii" }, 1200,
CTRLCMD + SHIFT + Keys::C, CurrentView + CTRLCMD + SHIFT + Keys::C,
[] { [] {
auto selection = ImHexApi::HexEditor::getSelection(); auto selection = ImHexApi::HexEditor::getSelection();
if (selection.has_value() && selection != Region::Invalid()) if (selection.has_value() && selection != Region::Invalid())
copyString(*selection); copyString(*selection);
}, },
ImHexApi::HexEditor::isSelectionValid); ImHexApi::HexEditor::isSelectionValid,
this);
/* Copy address */ /* Copy address */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.copy_as", "hex.builtin.view.hex_editor.copy.address" }, 1250, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.copy_as", "hex.builtin.view.hex_editor.copy.address" }, 1250,
@ -1095,26 +1097,29 @@ namespace hex::plugin::builtin {
}); });
/* Paste */ /* Paste */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste" }, 1450, CTRL + Keys::V, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste" }, 1450, CurrentView + CTRLCMD + Keys::V,
[] { [] {
pasteBytes(*ImHexApi::HexEditor::getSelection(), true); pasteBytes(*ImHexApi::HexEditor::getSelection(), true);
}, },
ImHexApi::HexEditor::isSelectionValid); ImHexApi::HexEditor::isSelectionValid,
this);
/* Paste All */ /* Paste All */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste_all" }, 1500, CTRL + SHIFT + Keys::V, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.paste_all" }, 1500, CurrentView + CTRLCMD + SHIFT + Keys::V,
[] { [] {
pasteBytes(*ImHexApi::HexEditor::getSelection(), false); pasteBytes(*ImHexApi::HexEditor::getSelection(), false);
}, },
ImHexApi::HexEditor::isSelectionValid); ImHexApi::HexEditor::isSelectionValid,
this);
/* Select All */ /* Select All */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.select_all" }, 1550, CTRL + Keys::A, ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.select_all" }, 1550, CurrentView + CTRLCMD + Keys::A,
[] { [] {
auto provider = ImHexApi::Provider::get(); auto provider = ImHexApi::Provider::get();
ImHexApi::HexEditor::setSelection(provider->getBaseAddress(), provider->getActualSize()); ImHexApi::HexEditor::setSelection(provider->getBaseAddress(), provider->getActualSize());
}, },
ImHexApi::HexEditor::isSelectionValid); ImHexApi::HexEditor::isSelectionValid,
this);
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.edit" }, 1600); ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.edit" }, 1600);