Implemented Hotkeys::Set() and related functionality.

This commit is contained in:
casey langen 2018-06-03 12:06:57 -07:00
parent e36fa357b9
commit 4a22c48293
4 changed files with 40 additions and 14 deletions

View File

@ -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<void()> 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<Hotkeys::Id>(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"));

View File

@ -36,6 +36,7 @@
#include <cursespp/LayoutBase.h>
#include <cursespp/ListWindow.h>
#include <cursespp/ShortcutsWindow.h>
#include <vector>
#include "ITopLevelLayout.h"
@ -67,6 +68,7 @@ namespace musik {
void OnEntryActivated(cursespp::ListWindow* w, size_t index);
std::shared_ptr<cursespp::ListWindow> listWindow;
cursespp::ShortcutsWindow* shortcuts;
};
}
}

View File

@ -272,14 +272,13 @@ std::string find(Id id, T& map) {
}
template <typename T>
void findMany(const std::string& kn, T& map, std::vector<std::string>& target) {
for (int i = 0; i < Id::COUNT; i++) {
Id id = static_cast<Id>(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) {

View File

@ -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)",