1
0
mirror of synced 2025-01-10 21:41:53 +01:00

impr: Display sub menus for shortcut settings

This commit is contained in:
WerWolv 2024-12-15 11:54:10 +01:00
parent caf8fa8e25
commit ee4bb33b0a
4 changed files with 37 additions and 9 deletions

View File

@ -393,7 +393,7 @@ namespace hex {
using Callback = std::function<void()>;
struct ShortcutEntry {
Shortcut shortcut;
UnlocalizedString unlocalizedName;
std::vector<UnlocalizedString> unlocalizedName;
Callback callback;
};
@ -403,6 +403,7 @@ namespace hex {
* @param unlocalizedName The unlocalized name of the shortcut
* @param callback The callback to call when the shortcut is triggered.
*/
static void addGlobalShortcut(const Shortcut &shortcut, const std::vector<UnlocalizedString> &unlocalizedName, const Callback &callback);
static void addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const Callback &callback);
/**
@ -412,6 +413,7 @@ namespace hex {
* @param unlocalizedName The unlocalized name of the shortcut
* @param callback The callback to call when the shortcut is triggered.
*/
static void addShortcut(View *view, const Shortcut &shortcut, const std::vector<UnlocalizedString> &unlocalizedName, const Callback &callback);
static void addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const Callback &callback);

View File

@ -909,10 +909,16 @@ namespace hex {
if (shortcut != Shortcut::None) {
auto callbackIfEnabled = [enabledCallback, function]{ if (enabledCallback()) { function(); } };
const auto unlocalizedShortcutName =
unlocalizedMainMenuNames.size() == 1 ?
std::vector { unlocalizedMainMenuNames.back() } :
std::vector(unlocalizedMainMenuNames.begin() + 1, unlocalizedMainMenuNames.end());
if (shortcut.isLocal() && view != nullptr)
ShortcutManager::addShortcut(view, shortcut, unlocalizedMainMenuNames.back(), callbackIfEnabled);
ShortcutManager::addShortcut(view, shortcut, unlocalizedShortcutName, callbackIfEnabled);
else
ShortcutManager::addGlobalShortcut(shortcut, unlocalizedMainMenuNames.back(), callbackIfEnabled);
ShortcutManager::addGlobalShortcut(shortcut, unlocalizedShortcutName, callbackIfEnabled);
}
}

View File

@ -16,14 +16,22 @@ namespace hex {
}
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::vector<UnlocalizedString> &unlocalizedName, const std::function<void()> &callback) {
s_globalShortcuts->insert({ shortcut, { shortcut, unlocalizedName, callback } });
}
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
s_globalShortcuts->insert({ shortcut, { shortcut, { unlocalizedName }, callback } });
}
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::vector<UnlocalizedString> &unlocalizedName, const std::function<void()> &callback) {
view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, unlocalizedName, callback } });
}
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, { unlocalizedName }, callback } });
}
static Shortcut getShortcut(bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
Shortcut pressedShortcut;

View File

@ -246,9 +246,12 @@ namespace hex::plugin::builtin {
class KeybindingWidget : public ContentRegistry::Settings::Widgets::Widget {
public:
KeybindingWidget(View *view, const Shortcut &shortcut) : m_view(view), m_shortcut(shortcut), m_drawShortcut(shortcut), m_defaultShortcut(shortcut) {}
KeybindingWidget(View *view, const Shortcut &shortcut, const std::vector<UnlocalizedString> &fullName)
: m_view(view), m_shortcut(shortcut), m_drawShortcut(shortcut), m_defaultShortcut(shortcut), m_fullName(fullName) {}
bool draw(const std::string &name) override {
std::ignore = name;
std::string label;
if (!m_editing)
@ -300,7 +303,15 @@ namespace hex::plugin::builtin {
ImGui::SameLine();
ImGuiExt::TextFormatted("{}", name);
std::string fullName;
for (const auto &part : m_fullName) {
fullName += Lang(part).get();
fullName += " -> ";
}
if (fullName.size() >= 4)
fullName = fullName.substr(0, fullName.size() - 4);
ImGuiExt::TextFormatted("{}", fullName);
ImGui::PopID();
@ -376,6 +387,7 @@ namespace hex::plugin::builtin {
private:
View *m_view = nullptr;
Shortcut m_shortcut, m_drawShortcut, m_defaultShortcut;
std::vector<UnlocalizedString> m_fullName;
bool m_editing = false;
bool m_hasDuplicate = false;
};
@ -934,12 +946,12 @@ namespace hex::plugin::builtin {
{
EventImHexStartupFinished::subscribe([]{
for (const auto &shortcutEntry : ShortcutManager::getGlobalShortcuts()) {
ContentRegistry::Settings::add<KeybindingWidget>("hex.builtin.setting.shortcuts", "hex.builtin.setting.shortcuts.global", shortcutEntry.unlocalizedName, nullptr, shortcutEntry.shortcut);
ContentRegistry::Settings::add<KeybindingWidget>("hex.builtin.setting.shortcuts", "hex.builtin.setting.shortcuts.global", shortcutEntry.unlocalizedName.back(), nullptr, shortcutEntry.shortcut, shortcutEntry.unlocalizedName);
}
for (auto &[viewName, view] : ContentRegistry::Views::impl::getEntries()) {
for (const auto &shortcutEntry : ShortcutManager::getViewShortcuts(view.get())) {
ContentRegistry::Settings::add<KeybindingWidget>("hex.builtin.setting.shortcuts", viewName, shortcutEntry.unlocalizedName, view.get(), shortcutEntry.shortcut);
ContentRegistry::Settings::add<KeybindingWidget>("hex.builtin.setting.shortcuts", viewName, shortcutEntry.unlocalizedName.back(), view.get(), shortcutEntry.shortcut, shortcutEntry.unlocalizedName);
}
}
});