I have a UXP panel with a button with
id = "toggleLayerVisibility"
I’m trying to set up this button so that a “mousedown” event will “hide” layer visibility and “mouseup” will “show” layer visibility.
The “mousedown” event does NOT work to “hide” the layer visibility.
The code to do this is as follows:
const exeModal = require('photoshop').core.executeAsModal;
//Hide layer visibility
document.getElementById("toggleLayerVisibility").addEventListener("mousedown", async () => {
try {await exeModal(targetFunction, {"commandName": "Progress...", "interactive": true});}
catch(e) {
if (e.number == 9) {showAlert(tkLang['alertSomeOtherPlugin']);}
else {showAlert(e)}
}
async function targetFunction(executionControl) {
///////////////////// Start modal execution /////////////////////
//Get current "visible" property of the layer
let layerVisibility = await batchPlay([
{ _obj: "get", _target: [ { _property: "visible" }, { "_ref": "layer","_enum": "ordinal","_value": "targetEnum" }, { _ref: "document", "_enum": "ordinal","_value": "targetEnum" } ], _options: { dialogOptions: "dontDisplay" } }
],{}).then(result => {return result[0].visible}, error => {return "no Layer Effects"});
console.log(layerVisibility)
await batchPlay([
//Hide current layer
{ "_obj": "hide", "null": [ { "_ref": "layer", "_enum": "ordinal", "_value": "targetEnum" } ]},
], {}).then(result=>console.log(result), error=>console.log(error));
///////////////////// Stop modal execution /////////////////////
} // end targetFunction()
});
//Show layer visibilty
document.getElementById("toggleLayerVisibility").addEventListener("mouseup", async () => {
try {await exeModal(targetFunction, {"commandName": "Progress...", "interactive": true});}
catch(e) {
if (e.number == 9) {showAlert(tkLang['alertSomeOtherPlugin']);}
else {showAlert(e)}
}
async function targetFunction(executionControl) {
///////////////////// Start modal execution /////////////////////
//Get current "visible" property of the layer
let layerVisibility = await batchPlay([
{ _obj: "get", _target: [ { _property: "visible" }, { "_ref": "layer","_enum": "ordinal","_value": "targetEnum" }, { _ref: "document", "_enum": "ordinal","_value": "targetEnum" } ], _options: { dialogOptions: "dontDisplay" } }
],{}).then(result => {return result[0].visible}, error => {return "no Layer Effects"});
console.log(layerVisibility)
await batchPlay([
//Show current layer
{ "_obj": "show", "null": [ { "_ref": "layer", "_enum": "ordinal", "_value": "targetEnum" } ]},
], {}).then(result=>console.log(result), error=>console.log(error));
///////////////////// Stop modal execution /////////////////////
} // end targetFunction()
});
Only the “mouseup” event works. I logged the current layer visibility as this executed and also the results of the batchPlay promises to try and see where the error was.
The console shows the code executes just fine. In fact, the console indicates that layer visibility is actually toggling between true and false, like it is supposed to do. However, layer visibility in Photoshop never gets turned off. So, there is a mismatch between what’s happening in the console and what’s happening in Photoshop.
I CAN get the “hide” command to work with a “click” event, but not with “mousedown”.
This seems like a bug to me, but always worth checking with others in case I’m doing something wrong.