Proper prefix matching in hotkeys?

develop
Robob27 2023-02-02 21:35:46 -05:00
parent 14f1e4b52f
commit 1c08b56a89
1 changed files with 9 additions and 10 deletions

@ -26,7 +26,6 @@ static const string INVOKE_HOTKEYS_COMMAND = "hotkeys";
static const std::string MENU_SCREEN_FOCUS_STRING = "dfhack/lua/hotkeys/menu"; static const std::string MENU_SCREEN_FOCUS_STRING = "dfhack/lua/hotkeys/menu";
static bool valid = false; // whether the following two vars contain valid data static bool valid = false; // whether the following two vars contain valid data
static string current_focus;
static map<string, string> current_bindings; static map<string, string> current_bindings;
static vector<string> sorted_keys; static vector<string> sorted_keys;
@ -38,14 +37,13 @@ static bool can_invoke(const string &cmdline, df::viewscreen *screen) {
} }
static int cleanupHotkeys(lua_State *) { static int cleanupHotkeys(lua_State *) {
DEBUG(log).print("cleaning up old stub keybindings for %s\n", current_focus.c_str()); DEBUG(log).print("cleaning up old stub keybindings for:\n %s\n", join_strings("\n", Gui::getFocusStrings(Gui::getCurViewscreen(true))).c_str());
std::for_each(sorted_keys.begin(), sorted_keys.end(), [](const string &sym) { std::for_each(sorted_keys.begin(), sorted_keys.end(), [](const string &sym) {
string keyspec = sym + "@" + MENU_SCREEN_FOCUS_STRING; string keyspec = sym + "@" + MENU_SCREEN_FOCUS_STRING;
DEBUG(log).print("clearing keybinding: %s\n", keyspec.c_str()); DEBUG(log).print("clearing keybinding: %s\n", keyspec.c_str());
Core::getInstance().ClearKeyBindings(keyspec); Core::getInstance().ClearKeyBindings(keyspec);
}); });
valid = false; valid = false;
current_focus = "";
sorted_keys.clear(); sorted_keys.clear();
current_bindings.clear(); current_bindings.clear();
return 0; return 0;
@ -89,8 +87,7 @@ static void find_active_keybindings(df::viewscreen *screen, bool filtermenu) {
} }
valid_keys.push_back("`"); valid_keys.push_back("`");
vector<string> focusStrings = Gui::getFocusStrings(screen);
current_focus = Gui::getFocusStrings(screen)[0];
for (int shifted = 0; shifted < 2; shifted++) { for (int shifted = 0; shifted < 2; shifted++) {
for (int alt = 0; alt < 2; alt++) { for (int alt = 0; alt < 2; alt++) {
for (int ctrl = 0; ctrl < 2; ctrl++) { for (int ctrl = 0; ctrl < 2; ctrl++) {
@ -112,7 +109,8 @@ static void find_active_keybindings(df::viewscreen *screen, bool filtermenu) {
vector<string> tokens; vector<string> tokens;
split_string(&tokens, *invoke_cmd, ":"); split_string(&tokens, *invoke_cmd, ":");
string focus = tokens[0].substr(1); string focus = tokens[0].substr(1);
if (prefix_matches(focus, current_focus)) { for(string focusString : focusStrings) {
if (prefix_matches(toLower(focus), toLower(focusString))) {
auto cmdline = trim(tokens[1]); auto cmdline = trim(tokens[1]);
add_binding_if_valid(sym, cmdline, screen, filtermenu); add_binding_if_valid(sym, cmdline, screen, filtermenu);
} }
@ -122,6 +120,7 @@ static void find_active_keybindings(df::viewscreen *screen, bool filtermenu) {
} }
} }
} }
}
valid = true; valid = true;
} }
@ -145,8 +144,8 @@ static void list(color_ostream &out) {
if (!valid) if (!valid)
find_active_keybindings(Gui::getCurViewscreen(true), false); find_active_keybindings(Gui::getCurViewscreen(true), false);
out.print("Valid keybindings for the current screen (%s)\n", out.print("Valid keybindings for the current focus:\n %s\n",
current_focus.c_str()); join_strings("\n", Gui::getFocusStrings(Gui::getCurViewscreen(true))).c_str());
std::for_each(sorted_keys.begin(), sorted_keys.end(), [&](const string &sym) { std::for_each(sorted_keys.begin(), sorted_keys.end(), [&](const string &sym) {
out.print("%s: %s\n", sym.c_str(), current_bindings[sym].c_str()); out.print("%s: %s\n", sym.c_str(), current_bindings[sym].c_str());
}); });