From 4a22c4829330c9ff347e171e0286d65630714365 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 3 Jun 2018 12:06:57 -0700 Subject: [PATCH] Implemented Hotkeys::Set() and related functionality. --- src/musikcube/app/layout/HotkeysLayout.cpp | 24 +++++++++++++++----- src/musikcube/app/layout/HotkeysLayout.h | 2 ++ src/musikcube/app/util/Hotkeys.cpp | 26 +++++++++++++++------- src/musikcube/data/locales/en_US.json | 2 +- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/musikcube/app/layout/HotkeysLayout.cpp b/src/musikcube/app/layout/HotkeysLayout.cpp index e20ab52f7..20c22e3dc 100644 --- a/src/musikcube/app/layout/HotkeysLayout.cpp +++ b/src/musikcube/app/layout/HotkeysLayout.cpp @@ -67,7 +67,7 @@ static void confirmResetHotkeys() { App::Overlays().Push(dialog); } -static void checkConflictAndSave(Hotkeys::Id id, const std::string& key) { +static void checkConflictAndSave(Hotkeys::Id id, const std::string& key, std::function cb) { const std::string existing = Hotkeys::Existing(key); if (existing == Hotkeys::Name(id)) { @@ -89,14 +89,20 @@ static void checkConflictAndSave(Hotkeys::Id id, const std::string& key) { "KEY_ENTER", "ENTER", _TSTR("button_yes"), - [id, key](const std::string& str) { + [id, key, cb](const std::string& str) { Hotkeys::Set(id, key); + if (cb) { + cb(); + } }); App::Overlays().Push(dialog); } else { Hotkeys::Set(id, key); + if (cb) { + cb(); + } } } @@ -122,16 +128,24 @@ HotkeysLayout::HotkeysLayout() { HotkeysLayout::~HotkeysLayout() { } -void HotkeysLayout::OnEntryActivated(cursespp::ListWindow* w, size_t index) { +void HotkeysLayout::OnEntryActivated(cursespp::ListWindow* list, size_t index) { Hotkeys::Id id = static_cast(index); + auto shortcuts = this->shortcuts; - ReassignHotkeyOverlay::Show(id, [id](std::string key) { - checkConflictAndSave(id, key); + ReassignHotkeyOverlay::Show(id, [this, list, id](std::string key) { + checkConflictAndSave(id, key, [this, list]() { + list->OnAdapterChanged(); + this->SetShortcutsWindow(this->shortcuts); + }); }); } void HotkeysLayout::SetShortcutsWindow(ShortcutsWindow* shortcuts) { + this->shortcuts = shortcuts; + if (shortcuts) { + shortcuts->RemoveAll(); + shortcuts->AddShortcut("M-r", _TSTR("hotkeys_reset_defaults")); shortcuts->AddShortcut(Hotkeys::Get(Hotkeys::NavigateLibrary), _TSTR("shortcuts_library")); shortcuts->AddShortcut(Hotkeys::Get(Hotkeys::NavigateSettings), _TSTR("shortcuts_settings")); diff --git a/src/musikcube/app/layout/HotkeysLayout.h b/src/musikcube/app/layout/HotkeysLayout.h index 9b521ade4..47ab22acd 100644 --- a/src/musikcube/app/layout/HotkeysLayout.h +++ b/src/musikcube/app/layout/HotkeysLayout.h @@ -36,6 +36,7 @@ #include #include +#include #include #include "ITopLevelLayout.h" @@ -67,6 +68,7 @@ namespace musik { void OnEntryActivated(cursespp::ListWindow* w, size_t index); std::shared_ptr listWindow; + cursespp::ShortcutsWindow* shortcuts; }; } } \ No newline at end of file diff --git a/src/musikcube/app/util/Hotkeys.cpp b/src/musikcube/app/util/Hotkeys.cpp index fa1d8b773..9544e2eb6 100755 --- a/src/musikcube/app/util/Hotkeys.cpp +++ b/src/musikcube/app/util/Hotkeys.cpp @@ -272,14 +272,13 @@ std::string find(Id id, T& map) { } template -void findMany(const std::string& kn, T& map, std::vector& target) { - for (int i = 0; i < Id::COUNT; i++) { - Id id = static_cast(i); - std::string thisKn = find(id, map); - if (thisKn == kn) { - target.push_back(thisKn); +Hotkeys::Id find(const std::string& kn, T& map) { + for (auto it : map) { + if (it.second == kn) { + return it.first; } } + return Hotkeys::COUNT; } std::string Hotkeys::Default(Id id) { @@ -298,7 +297,8 @@ std::string Hotkeys::Get(Id id) { } void Hotkeys::Set(Id id, const std::string& kn) { - /* CAL TODO */ + customIdToKey[id] = kn; + savePreferences(); } void Hotkeys::Reset() { @@ -308,7 +308,17 @@ void Hotkeys::Reset() { } std::string Hotkeys::Existing(const std::string& kn) { - return "TODO!!"; + auto id = find(kn, customIdToKey); + if (id == Hotkeys::COUNT) { + id = find(kn, ID_TO_DEFAULT); + if (customIdToKey.find(id) != customIdToKey.end()) { + /* we found a default key for this one, but that default + binding has already been overridden! ensure we return + that it's available. */ + id = Hotkeys::COUNT; + } + } + return id != Hotkeys::COUNT ? Name(id) : ""; } std::string Hotkeys::Name(Id id) { diff --git a/src/musikcube/data/locales/en_US.json b/src/musikcube/data/locales/en_US.json index 7f1bcd77b..284868606 100644 --- a/src/musikcube/data/locales/en_US.json +++ b/src/musikcube/data/locales/en_US.json @@ -43,7 +43,7 @@ "hotkeys_reset_all_title": "reset hotkeys", "hotkeys_reset_all_message": "are you sure you want to reset all hotkeys to their default values?\n\nexisting custom hotkeys will be forgotten!", "hotkeys_conflict_title": "hotkey conflict", - "hotkeys_conflict_message": "the hotkey '{{hotkey}}' conflicts with '{{existing}}'.\n\nare you sure you want to assign this binding?", + "hotkeys_conflict_message": "the hotkey '{{hotkey}}' *may* conflict with '{{existing}}', depending on context.\n\nare you sure you want to assign this binding?", "settings_space_to_add": "browse (SPACE to add)", "settings_backspace_to_remove": "indexed paths (BACKSPACE to remove)",