Files
2026-04-22 09:01:43 -04:00

61 lines
2.6 KiB
JavaScript

import { app } from "../../scripts/app.js";
app.registerExtension({
name: "TensorRTX.UpscaleLogic",
async beforeRegisterNodeDef(nodeType, nodeData) {
if (nodeData.name === "TensorRTXUpscaleImage") {
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;
// 1. Capture the initial widgets array safely
// We use a property that won't conflict with LiteGraph internals
this._custom_widgets_store = this.widgets ? [...this.widgets] : [];
console.log("[TensorRTXUpscaleImage] Initial widgets captured:", this._custom_widgets_store);
const scaleWidget = this._custom_widgets_store.find(w => w.name === "scale_by");
const widthWidget = this._custom_widgets_store.find(w => w.name === "width");
const heightWidget = this._custom_widgets_store.find(w => w.name === "height");
if (!scaleWidget) {
console.error("[TensorRTXUpscaleImage] Fatal: 'scale_by' widget not found!");
return r;
}
const updateVisibility = () => {
const isCustom = scaleWidget.value === "custom";
console.log(`[TensorRTXUpscaleImage] Updating visibility. Custom mode: ${isCustom}`);
if (widthWidget) widthWidget.hidden = !isCustom;
if (heightWidget) heightWidget.hidden = !isCustom;
// Force the node to recalculate its size
this.setSize(this.computeSize());
this.setDirtyCanvas(true, true);
};
// 2. Redefine 'widgets' to act as a window into our store
Object.defineProperty(this, "widgets", {
get() {
// Return only non-hidden widgets to the renderer
return this._custom_widgets_store.filter(w => !w.hidden);
},
set(newWidgets) {
// In case something else tries to overwrite the widget list
this._custom_widgets_store = newWidgets;
},
configurable: true
});
scaleWidget.callback = () => updateVisibility();
// Initial trigger
setTimeout(() => updateVisibility(), 10);
return r;
};
}
}
});