ScriptUI Photoshop Slider - lag vs. latency
Hello all,
I have the following script, running on Mac, with ESTK.
As you can see, it's just a simple slider, in a Dialog window, that talks to Photoshop and change a layer opacity value.
I have made the passed value from the Dialog Window as fast as possible (I think), i.e. the value of the slider changed by the user gets almost instantly, no lag (minimal that is) to Photoshop.
However, and this is where my question rests : if the user starts to click on the Slider repeatedly, one can notice that the slider gets "unavailable" for a short period of time. I think this might have something to do with WaitForRedraw ? Is there anyway that we could bypass this "latency", by somehow making ScriptUI slider as instant as possible ?
Thank you !
#target Photoshop
function SnpCreateSlider ()
{
this.windowRef = null;
}
cTID = function (s) {
return app.charIDToTypeID(s);
};
sTID = function (s) {
return app.stringIDToTypeID(s);
};
SnpCreateSlider.prototype.run = function() {
var retval = true;
// Create a palette-type window (a modeless or floating dialog),
var win = new Window("dialog", "SnpCreateSlider", [150, 150, 600, 300]);
this.windowRef = win;
var CurrentV = 100;
// Add a panel to contain the components
win.pnl = win.add("panel", [10, 10, 440, 100], "Move slider around");
// Add some labels that describe the state of the slider
win.pnl.minLbl = win.pnl.add("statictext", [20, 47, 35, 60], "0");
win.pnl.curLbl = win.pnl.add("statictext", [115, 47, 170, 60], "50");
win.pnl.maxLbl = win.pnl.add("statictext", [210, 47, 250, 60], "100");
win.pnl.displayTextLbl = win.pnl.add("statictext", [290, 20, 380, 40], "Current value:");
win.pnl.displayLbl = win.pnl.add("edittext", [385, 20, 425, 40], CurrentV);
// Add a slider control
win.pnl.sliderCtrl = win.pnl.add("slider", [20, 20, 230, 45], CurrentV, 0, 100);
// SHORTCUTS
var myVal = win.pnl.displayLbl;
var mySlider = win.pnl.sliderCtrl;
// Add buttons
win.refreshButton = win.add("button", [125, 110, 125, 140], "Refresh");
win.resetButton = win.add("button", [25, 110, 125, 140], "Reset");
win.doneButton = win.add("button",[320, 110, 420, 140] , "Done");
var sliderChangeCallback = function (event) {
var myValue = myVal.text = Math.round(mySlider.value);
win.pnl.enabled = false; // DISABLE GUI
myRoutine (myValue);
win.pnl.enabled = true; // ENABLE GUI
}
var sliderChangingCallback = function (event) {
myVal.text = Math.round(mySlider.value);
}
// EVENT LISTENERS
mySlider.addEventListener ('change', sliderChangeCallback, false);
mySlider.addEventListener ('changing', sliderChangingCallback,false);
mySlider.addEventListener ('mouseup', waitForRedraw);
// MY ROUTINE
var myRoutine = function (vv) {
myVal.text = vv;
// TALKS TO PHOTOSHOP
app.activeDocument.activeLayer.opacity = vv;
}
function waitForRedraw () {
var d;
d = new ActionDescriptor();
d.putEnumerated(stringIDToTypeID('state'), stringIDToTypeID('state'), stringIDToTypeID('redrawComplete'));
executeAction(stringIDToTypeID('wait'), d, DialogModes.NO);
}
function _waitForRedraw () {
var desc = new ActionDescriptor();
desc.putEnumerated(cTID("Stte"), cTID("Stte"), cTID("RdCm"));
executeAction(cTID("Wait"), desc, DialogModes.NO);
}
function __waitForRedraw () {
var eventWait = charIDToTypeID("Wait");
var enumRedrawComplete = charIDToTypeID("RdCm") ;
var typeState = charIDToTypeID("Stte");
var keyState = charIDToTypeID("Stte");
var desc = new ActionDescriptor();
desc.putEnumerated(keyState, typeState, enumRedrawComplete);
executeAction(eventWait, desc, DialogModes.NO);
}
win.refreshButton.onClick = waitForRedraw;
win.onMove = waitForRedraw;
// Define behavior for the "Exit" button
win.doneButton.onClick = function ()
{
win.close();
};
// Define behavior for the "Reset" button
win.resetButton.onClick = function()
{
win.pnl.sliderCtrl.value = 100;
win.pnl.curLbl.text = 100;
win.pnl.displayLbl.text= 100;
}
// Display the window
win.center();
win.show();
return retval;
}
if(typeof(SnpCreateSlider_unitTest) == "undefined") {
new SnpCreateSlider().run();
}
P.S :
To show the latency "issue" here, replace :
mySlider.addEventListener ('mouseup', showLatency);
function showLatency () {
win.pnl.enabled = false; // DISABLE GUI
waitForRedraw () ;
win.pnl.enabled = true; // ENABLE GUI
}