Auto-show/hide needed or unneeded fields

This commit is contained in:
Luis Quesada
2024-05-15 21:42:10 +02:00
parent 705be5ea77
commit 914bfd46d9
3 changed files with 93 additions and 1 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ If you want to inpaint with SDXL, use forced size = 1024.
# Acknowledgements
This repository uses some code from comfy_extras (https://github.com/comfyanonymous/ComfyUI) and from KJNodes (https://github.com/kijai/ComfyUI-KJNodes), both licensed under GNU GENERAL PUBLIC LICENSE Version 3.
This repository uses some code from comfy_extras (https://github.com/comfyanonymous/ComfyUI), KJNodes (https://github.com/kijai/ComfyUI-KJNodes), and Efficiency Nodes (https://github.com/LucianoCirino/efficiency-nodes-comfyui), all of them licensed under GNU GENERAL PUBLIC LICENSE Version 3.
# License
GNU GENERAL PUBLIC LICENSE Version 3, see [LICENSE](LICENSE)
+2
View File
@@ -1,6 +1,8 @@
from .inpaint_cropandstitch import InpaintCrop
from .inpaint_cropandstitch import InpaintStitch
WEB_DIRECTORY = "js"
NODE_CLASS_MAPPINGS = {
"InpaintCrop": InpaintCrop,
"InpaintStitch": InpaintStitch
+90
View File
@@ -0,0 +1,90 @@
import { app } from "../../scripts/app.js";
// Some fragments of this code are from https://github.com/LucianoCirino/efficiency-nodes-comfyui
function inpaintCropHandler(node) {
if (node.type != "InpaintCrop") {
return;
}
toggleWidget(node, findWidgetByName(node, "force_size"));
toggleWidget(node, findWidgetByName(node, "rescale_factor"));
toggleWidget(node, findWidgetByName(node, "padding"));
if (findWidgetByName(node, "mode").value == "free size") {
toggleWidget(node, findWidgetByName(node, "rescale_factor"), true);
toggleWidget(node, findWidgetByName(node, "padding"), true);
}
else if (findWidgetByName(node, "mode").value == "forced size") {
toggleWidget(node, findWidgetByName(node, "force_size"), true);
}
return;
}
let origProps = {};
const findWidgetByName = (node, name) => {
return node.widgets ? node.widgets.find((w) => w.name === name) : null;
};
const doesInputWithNameExist = (node, name) => {
return node.inputs ? node.inputs.some((input) => input.name === name) : false;
};
const HIDDEN_TAG = "tschide";
// Toggle Widget + change size
function toggleWidget(node, widget, show = false, suffix = "") {
if (!widget || doesInputWithNameExist(node, widget.name)) return;
// Store the original properties of the widget if not already stored
if (!origProps[widget.name]) {
origProps[widget.name] = { origType: widget.type, origComputeSize: widget.computeSize };
}
const origSize = node.size;
// Set the widget type and computeSize based on the show flag
widget.type = show ? origProps[widget.name].origType : HIDDEN_TAG + suffix;
widget.computeSize = show ? origProps[widget.name].origComputeSize : () => [0, -4];
// Recursively handle linked widgets if they exist
widget.linkedWidgets?.forEach(w => toggleWidget(node, w, ":" + widget.name, show));
// Calculate the new height for the node based on its computeSize method
const newHeight = node.computeSize()[1];
node.setSize([node.size[0], newHeight]);
}
app.registerExtension({
name: "inpaint-cropandstitch.showcontrol",
nodeCreated(node) {
for (const w of node.widgets || []) {
let widgetValue = w.value;
// Store the original descriptor if it exists
let originalDescriptor = Object.getOwnPropertyDescriptor(w, 'value');
inpaintCropHandler(node);
Object.defineProperty(w, 'value', {
get() {
// If there's an original getter, use it. Otherwise, return widgetValue.
let valueToReturn = originalDescriptor && originalDescriptor.get
? originalDescriptor.get.call(w)
: widgetValue;
return valueToReturn;
},
set(newVal) {
// If there's an original setter, use it. Otherwise, set widgetValue.
if (originalDescriptor && originalDescriptor.set) {
originalDescriptor.set.call(w, newVal);
} else {
widgetValue = newVal;
}
inpaintCropHandler(node);
}
});
}
}
});