From 7f4841ff39f2047bb2338cf2a17ce1ee860a2716 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Wed, 21 Jul 2021 13:29:27 +0300 Subject: js: key bindings: ensure priorities for same-key bindings Previously, if a script bound the same key more than once to different functions (without removing the older bindings first), then pressing the key triggered any of the bound functions arbitrarily[1]. Now the newest binding for the same key is always the active one. If the newest binding is then removed - the second-newest will become active, etc. (same mechanism as before, except that the active key was not always the newest). [1] The order was implementation-defined, however `for (name in obj)' happens to iterate. With mujs that's currently alhabetic order, and name is from mp.add_[forced_]key_binding(key, name...) or generated internally if name is not provided. So a binding with higher alphabetic `name' value had priority over lower name value. --- player/javascript/defaults.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 91f1f52517..6a1c0762d4 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -276,9 +276,18 @@ function dispatch_key_binding(name, state, key_name) { var binds_tid = 0; // flush timer id. actual id's are always true-thy mp.flush_key_bindings = function flush_key_bindings() { + function prioritized_inputs(arr) { + return arr.sort(function(a, b) { return a.id > b.id }) + .map(function(bind) { return bind.input }); + } + var def = [], forced = []; - for (var n in binds) // Array.join() will later skip undefined .input - (binds[n].forced ? forced : def).push(binds[n].input); + for (var n in binds) + if (binds[n].input) + (binds[n].forced ? forced : def).push(binds[n]); + // newer bindings for the same key override/hide older ones + def = prioritized_inputs(def); + forced = prioritized_inputs(forced); var sect = "input_" + mp.script_name; mp.commandv("define-section", sect, def.join("\n"), "default"); @@ -306,13 +315,14 @@ function add_binding(forced, key, name, fn, opts) { fn = name; name = false; } - if (!name) - name = "__keybinding" + next_bid++; // new unique binding name var key_data = {forced: forced}; switch (typeof opts) { // merge opts into key_data case "string": key_data[opts] = true; break; case "object": for (var o in opts) key_data[o] = opts[o]; } + key_data.id = next_bid++; + if (!name) + name = "__keybinding" + key_data.id; // new unique binding name if (key_data.complex) { mp.register_script_message(name, function msg_cb() { -- cgit v1.2.3