Implemented using one-time idle observer (i.e. setTimeout), to avoid additional function call(back) every time the event loop enters idle. The lua mp.flush_key_bindings() is also available, also undocumented.
Showing the changes to player/javascript (1 of 1 file). Show all
Unified · Split
| @@ -205,7 +205,8 @@ function dispatch_key_binding(name, state, key_name) { · 204 unmodified lines | |||
| 205 | cb(state, key_name); | 205 | cb(state, key_name); |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | function update_input_sections() { | 208 | var binds_tid = 0; // flush timer id. actual id's are always true-thy |
| 209 | mp.flush_key_bindings = function flush_key_bindings() { | ||
| 209 | var def = [], forced = []; | 210 | var def = [], forced = []; |
| 210 | for (var n in binds) // Array.join() will later skip undefined .input | 211 | for (var n in binds) // Array.join() will later skip undefined .input |
| 211 | (binds[n].forced ? forced : def).push(binds[n].input); | 212 | (binds[n].forced ? forced : def).push(binds[n].input); |
| @@ -217,6 +218,14 @@ function update_input_sections() { · 5 unmodified lines | |||
| 217 | sect = "input_forced_" + mp.script_name; | 218 | sect = "input_forced_" + mp.script_name; |
| 218 | mp.commandv("define-section", sect, forced.join("\n"), "force"); | 219 | mp.commandv("define-section", sect, forced.join("\n"), "force"); |
| 219 | mp.commandv("enable-section", sect, "allow-hide-cursor+allow-vo-dragging"); | 220 | mp.commandv("enable-section", sect, "allow-hide-cursor+allow-vo-dragging"); |
| 221 | |||
| 222 | clearTimeout(binds_tid); // cancel future flush if called directly | ||
| 223 | binds_tid = 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | function sched_bindings_flush() { | ||
| 227 | if (!binds_tid) | ||
| 228 | binds_tid = setTimeout(mp.flush_key_bindings, 0); // fires on idle | ||
| 220 | } | 229 | } |
| 221 | 230 | ||
| 222 | // name/opts maybe omitted. opts: object with optional bool members: repeatable, | 231 | // name/opts maybe omitted. opts: object with optional bool members: repeatable, |
| @@ -263,7 +272,7 @@ function add_binding(forced, key, name, fn, opts) { · 40 unmodified lines | |||
| 263 | if (key) | 272 | if (key) |
| 264 | key_data.input = key + " script-binding " + mp.script_name + "/" + name; | 273 | key_data.input = key + " script-binding " + mp.script_name + "/" + name; |
| 265 | binds[name] = key_data; // used by user and/or our (key) script-binding | 274 | binds[name] = key_data; // used by user and/or our (key) script-binding |
| 266 | update_input_sections(); | 275 | sched_bindings_flush(); |
| 267 | } | 276 | } |
| 268 | 277 | ||
| 269 | mp.add_key_binding = add_binding.bind(null, false); | 278 | mp.add_key_binding = add_binding.bind(null, false); |
| @@ -272,7 +281,7 @@ mp.add_forced_key_binding = add_binding.bind(null, true); · 2 unmodified lines | |||
| 272 | mp.remove_key_binding = function(name) { | 281 | mp.remove_key_binding = function(name) { |
| 273 | mp.unregister_script_message(name); | 282 | mp.unregister_script_message(name); |
| 274 | delete binds[name]; | 283 | delete binds[name]; |
| 275 | update_input_sections(); | 284 | sched_bindings_flush(); |
| 276 | } | 285 | } |
| 277 | 286 | ||
| 278 | /********************************************************************** | 287 | /********************************************************************** |