From aa0760fcb99f894d56c0a409c0c83734dc96c844 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 30 Aug 2018 16:07:11 +0300 Subject: [PATCH] added debounce to toggleSingleImage when triggered from the keyboard... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/keyboard.js | 58 +++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/ui (gen4)/features/keyboard.js b/ui (gen4)/features/keyboard.js index 5aaafde4..11ff37eb 100755 --- a/ui (gen4)/features/keyboard.js +++ b/ui (gen4)/features/keyboard.js @@ -256,7 +256,8 @@ module.GLOBAL_KEYBOARD = { // modes... - Enter: 'toggleSingleImage', + //Enter: 'toggleSingleImage', + Enter: 'debounce: 500 "toggleSingleImage" -- Toggle single image mode (debounced)', S: 'slideshowDialog', @@ -487,6 +488,11 @@ var KeyboardActions = actions.Actions({ // The amount of keyboard "quiet" time to wait for when // .pauseKeyboardRepeat(..) is called... 'keyboard-repeat-pause-check': 100, + + + // A timeout to wait between calls to actions triggered via + // .debounce(..) + 'debounce-action-timeout': 200, }, get keybindings(){ @@ -832,6 +838,56 @@ var KeyboardActions = actions.Actions({ this.config['keyboard-repeat-pause-check'] > 0 && this.keyboard.pauseRepeat && this.keyboard.pauseRepeat() }], + + debounce: ['- Interface/', + core.doc`Debounce action call... + + .debounce(action, ...) + .debounce(timeout, action, ...) + .debounce(timeout, tag, action, ...) + + NOTE: when using a tag, it must not resolve to and action, i.e. + this[tag] must not be callable... + NOTE: this ignores action return value and returns this... + `, + function(...args){ + // parse the args... + var timeout = typeof(args[0]) == typeof(123) ? + args.shift() + : (this.config['debounce-action-timeout'] || 200) + // NOTE: this[tag] must not be callable, otherwise we treat it + // as an action... + var tag = this[args[0]] instanceof Function ? + args[0] + : args.shift() + var action = args.shift() + + var attr = '__debounce_'+ tag + + // repeated call... + if(this[attr]){ + this[attr +'_retriggered'] = true + + // setup and first call... + } else { + // NOTE: we are ignoring the return value here so as to + // make the first and repeated call uniform... + this[action](...args) + + this[attr] = setTimeout(function(){ + delete this[attr] + + // retrigger... + if(this[attr +'_retriggered']){ + delete this[attr +'_retriggered'] + + tag == action ? + this.debounce(timeout, action, ...args) + : this.debounce(timeout, tag, action, ...args) + } + }.bind(this), timeout) + } + }], }) var Keyboard =