diff --git a/README.md b/README.md index 648611e..ef142e3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/__init__.py b/__init__.py index cf000d9..b141280 100755 --- a/__init__.py +++ b/__init__.py @@ -1,6 +1,8 @@ from .inpaint_cropandstitch import InpaintCrop from .inpaint_cropandstitch import InpaintStitch +WEB_DIRECTORY = "js" + NODE_CLASS_MAPPINGS = { "InpaintCrop": InpaintCrop, "InpaintStitch": InpaintStitch diff --git a/js/showcontrol.js b/js/showcontrol.js new file mode 100644 index 0000000..38d6e7c --- /dev/null +++ b/js/showcontrol.js @@ -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); + } + }); + } + } +});