Created
June 29, 2023 11:25
-
-
Save indiscripts/f1bf14b34d642ec92079b3499030b4c2 to your computer and use it in GitHub Desktop.
Illustrates the distinctive priority of USER vs. PROGRAMMATIC events in ScriptUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function testEventsPriority( w,g) | |
//---------------------------------- | |
// ScriptUI CS/CC - Modal dialog. | |
// Illustrates the distinctive priority of USER vs. PROGRAMMATIC events. | |
{ | |
// Global mousedown/mouseup listener. | |
const ON_MOUSE = function(ev, t) | |
{ | |
// this :: Window | |
if( (ev.target.properties||0).NoEvent ) return; // Used for the [Clear] action. | |
t = ev.timeStamp; | |
t = t.getHours()+':'+t.getMinutes()+':'+t.getSeconds()+'.'+('00'+t.getMilliseconds()).slice(-3); | |
this.info.text += [ev.target,ev.type,t].join(' \u2022 ')+'\r'; | |
}; | |
w = new Window('dialog',"Events Priority"); | |
( w.info = w.add('panel').add('statictext',void 0,"Click anywhere... then click [Test]\r\r",{multiline:true}) ) | |
.preferredSize = [500,400]; | |
// Buttons: [Test] and [Clear] | |
g = w.add('group', void 0, {NoEvent:1}); | |
g.add('button', void 0, 'Test').onClick = function( wg,i,ev) | |
{ | |
wg = this.window.info; | |
wg.text = "Try to click the panel while the countdown is running...\r\r"; | |
for( i=10 ; 0 < i ; i-- ) | |
{ | |
if( 5===i ) // One internal mouse event is created at i==5 | |
{ | |
wg.text += "[" + i + "] -> Now dispatching a fake mouse event\r"; | |
(ev=ScriptUI.events.createEvent('MouseEvent')).initMouseEvent('mousedown'); | |
this.window.dispatchEvent(ev); // Triggered and handled right now, within the present loop! | |
} | |
else | |
{ | |
wg.text += '[' + i + ']\r'; | |
} | |
for( t=Date.now() ; Date.now()-t < 500 ; ); // Sleep 500ms | |
} | |
wg.text += '-----------------\r'; | |
}; | |
g.add('button', void 0, 'Clear',{NoEvent:1}).onClick = function() | |
{ | |
this.window.info.text=''; | |
}; | |
// Attach the global listener to mousedown|mouseup | |
w.addEventListener('mousedown', ON_MOUSE); | |
w.addEventListener('mouseup', ON_MOUSE); | |
w.show(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment