refactoring -- now pausing of repeating key handling is a keyboard feature...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-03-08 21:07:29 +03:00
parent 6a8c5fb561
commit 852769acea
2 changed files with 54 additions and 29 deletions

View File

@ -673,10 +673,16 @@ var KeyboardActions = actions.Actions({
`,
{ keepDialogTitle: true },
function(key, no_match){
var that = this
// get/set the handler...
var handler = this.__key_press_handler =
this.__key_press_handler
|| keyboard.makeKeyboardHandler(this.keyboard, null, this)
//|| keyboard.makeKeyboardHandler(this.keyboard, null, this)
|| keyboard.makePausableKeyboardHandler(
this.keyboard,
null,
this,
function(){ return that.config['keyboard-repeat-pause-check'] })
// do the call...
return handler(key, no_match)
}],
@ -689,24 +695,6 @@ var KeyboardActions = actions.Actions({
return this.__keyboard_handler ? 'on' : 'off'
}
// repeat stop checker...
var check = (function(){
if(this.config['keyboard-repeat-pause-check'] > 0
&& this.__keyboard_repeat_paused){
var that = this
this.__keyboard_repeat_pause_timeout
&& clearTimeout(this.__keyboard_repeat_pause_timeout)
this.__keyboard_repeat_pause_timeout = setTimeout(function(){
delete that.__keyboard_repeat_paused
delete that.__keyboard_repeat_pause_timeout
}, this.config['keyboard-repeat-pause-check'] || 100)
return false
}
return true
}).bind(this)
var kb = this.keyboard
// start/reset keyboard handling...
@ -725,15 +713,7 @@ var KeyboardActions = actions.Actions({
&& target.off('keydown', this.__keyboard_handler)
// make the base handler...
var handler =
keyboard.stoppableKeyboardRepeat(
this.keyPress.bind(this),
/*/ log unbound keys...
function(evt){
return that.keyPress(evt, function(evt, k){
window.DEBUG && console.log('KEY:', k) }) },
//*/
check)
var handler = this.keyPress.bind(this)
// setup base keyboard for devel, in case something breaks...
// This branch does not drop keys...
@ -771,7 +751,10 @@ var KeyboardActions = actions.Actions({
This is useful for stopping repeating (held down) keys after some
event.`,
function(){ this.__keyboard_repeat_paused = true }],
function(){
this.config['keyboard-repeat-pause-check'] > 0
&& this.keyboard.pauseRepeat
&& this.keyboard.pauseRepeat() }],
})
var Keyboard =

View File

@ -1049,6 +1049,48 @@ function makeKeyboardHandler(keyboard, unhandled, actions){
//---------------------------------------------------------------------
// Pausable base event handler wrapper of Keyboard...
//
// This is the same as .makeKeyboardHandler(..) but adds ability to
// pause repeating key handling...
//
// This will extend the keyboard object by adding:
// .pauseRepeat() - will pause repeating keys...
//
var makePausableKeyboardHandler =
module.makePausableKeyboardHandler =
function makePausableKeyboardHandler(keyboard, unhandled, actions, check_interval){
var kb = keyboard instanceof Keyboard ?
keyboard
//: Keyboard(keyboard, checkGlobalMode)
: Keyboard(keyboard)
kb.pauseRepeat = function(){ this.__repeat_paused = true }
return stoppableKeyboardRepeat(
makeKeyboardHandler(kb, unhandled, actions),
function(){
if(kb.__repeat_paused){
var that = this
kb.__repeat_pause_timeout
&& clearTimeout(kb.__repeat_pause_timeout)
kb.__repeat_pause_timeout = setTimeout(function(){
delete kb.__repeat_paused
delete kb.__repeat_pause_timeout
}, (check_interval instanceof Function ?
check_interval.call(actions)
: (check_interval || 100)))
return false
}
return true
})
}
//---------------------------------------------------------------------
// handler wrappers...