MCPcopy Create free account
hub / github.com/Comfy-Org/litegraph.js / addWidget

Method addWidget

src/LGraphNode.ts:1760–1809  ·  view source on GitHub ↗

* Defines a widget inside the node, it will be rendered on top of the node, you can control lots of properties * @param type the widget type * @param name the text to show on the widget * @param value the default value * @param callback function to call when it changes (optionally, it ca

(
    type: Type,
    name: string,
    value: TValue,
    callback: IBaseWidget["callback"] | string | null,
    options?: IWidgetOptions | string,
  )

Source from the content-addressed store, hash-verified

1758 * @returns the created widget object
1759 */
1760 addWidget<Type extends TWidgetType, TValue extends WidgetTypeMap[Type]["value"]>(
1761 type: Type,
1762 name: string,
1763 value: TValue,
1764 callback: IBaseWidget["callback"] | string | null,
1765 options?: IWidgetOptions | string,
1766 ): WidgetTypeMap[Type] | IBaseWidget {
1767 this.widgets ||= []
1768
1769 if (!options && callback && typeof callback === "object") {
1770 options = callback
1771 callback = null
1772 }
1773
1774 // options can be the property name
1775 options ||= {}
1776 if (typeof options === "string")
1777 options = { property: options }
1778
1779 // callback can be the property name
1780 if (callback && typeof callback === "string") {
1781 options.property = callback
1782 callback = null
1783 }
1784
1785 const w: IBaseWidget & { type: Type } = {
1786 // @ts-expect-error
1787 type: type.toLowerCase(),
1788 name: name,
1789 value: value,
1790 callback: typeof callback !== "function" ? undefined : callback,
1791 options,
1792 y: 0,
1793 }
1794
1795 if (w.options.y !== undefined) {
1796 w.y = w.options.y
1797 }
1798
1799 if (!callback && !w.options.callback && !w.options.property) {
1800 console.warn("LiteGraph addWidget(...) without a callback or property assigned")
1801 }
1802 if (type == "combo" && !w.options.values) {
1803 throw "LiteGraph addWidget('combo',...) requires to pass values in options: { values:['red','blue'] }"
1804 }
1805
1806 const widget = this.addCustomWidget(w)
1807 this.expandToFitContent()
1808 return widget
1809 }
1810
1811 addCustomWidget<TPlainWidget extends IBaseWidget>(
1812 custom_widget: TPlainWidget,

Callers 2

LGraphNode.test.tsFile · 0.80
inner_refreshMethod · 0.80

Calls 2

addCustomWidgetMethod · 0.95
expandToFitContentMethod · 0.95

Tested by

no test coverage detected