Skip to content

Commit 6867d84

Browse files
[backport core/1.33] Remove app.graph usage from widgetInput code (#7010)
Backport of #7008 to `core/1.33` Automatically created by backport workflow. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7010-backport-core-1-33-Remove-app-graph-usage-from-widgetInput-code-2b86d73d3650811f9da1c5869f352e10) by [Unito](https://www.unito.io) Co-authored-by: AustinMroz <[email protected]>
1 parent d0bd4c2 commit 6867d84

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

src/extensions/core/widgetInputs.ts

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import type {
77
INodeInputSlot,
88
INodeOutputSlot,
99
ISlotType,
10-
LLink,
11-
Point
10+
LLink
1211
} from '@/lib/litegraph/src/litegraph'
12+
import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot'
1313
import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events'
1414
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
1515
import type { InputSpec } from '@/schemas/nodeDefSchema'
@@ -37,15 +37,15 @@ export class PrimitiveNode extends LGraphNode {
3737
}
3838

3939
override applyToGraph(extraLinks: LLink[] = []) {
40-
if (!this.outputs[0].links?.length) return
40+
if (!this.outputs[0].links?.length || !this.graph) return
4141

4242
const links = [
43-
...this.outputs[0].links.map((l) => app.graph.links[l]),
43+
...this.outputs[0].links.map((l) => this.graph!.links[l]),
4444
...extraLinks
4545
]
4646
let v = this.widgets?.[0].value
4747
if (v && this.properties[replacePropertyName]) {
48-
v = applyTextReplacements(app.graph, v as string)
48+
v = applyTextReplacements(this.graph, v as string)
4949
}
5050

5151
// For each output link copy our value over the original widget value
@@ -331,13 +331,13 @@ export class PrimitiveNode extends LGraphNode {
331331
const config1 = (output.widget?.[GET_CONFIG] as () => InputSpec)?.()
332332
if (!config1) return
333333
const isNumber = config1[0] === 'INT' || config1[0] === 'FLOAT'
334-
if (!isNumber) return
334+
if (!isNumber || !this.graph) return
335335

336336
for (const linkId of links) {
337-
const link = app.graph.links[linkId]
337+
const link = this.graph.links[linkId]
338338
if (!link) continue // Can be null when removing a node
339339

340-
const theirNode = app.graph.getNodeById(link.target_id)
340+
const theirNode = this.graph.getNodeById(link.target_id)
341341
if (!theirNode) continue
342342
const theirInput = theirNode.inputs[link.target_slot]
343343

@@ -441,30 +441,26 @@ function getWidgetType(config: InputSpec) {
441441
return { type }
442442
}
443443

444-
export function setWidgetConfig(
445-
slot: INodeInputSlot | INodeOutputSlot,
446-
config?: InputSpec
447-
) {
444+
export function setWidgetConfig(slot: INodeInputSlot, config?: InputSpec) {
448445
if (!slot.widget) return
449446
if (config) {
450447
slot.widget[GET_CONFIG] = () => config
451448
} else {
452449
delete slot.widget
453450
}
454451

455-
if ('link' in slot) {
456-
const link = app.graph.links[slot.link ?? -1]
457-
if (link) {
458-
const originNode = app.graph.getNodeById(link.origin_id)
459-
if (originNode && isPrimitiveNode(originNode)) {
460-
if (config) {
461-
originNode.recreateWidget()
462-
} else if (!app.configuringGraph) {
463-
originNode.disconnectOutput(0)
464-
originNode.onLastDisconnect()
465-
}
466-
}
467-
}
452+
if (!(slot instanceof NodeSlot)) return
453+
const graph = slot.node.graph
454+
if (!graph) return
455+
const link = graph.links[slot.link ?? -1]
456+
if (!link) return
457+
const originNode = graph.getNodeById(link.origin_id)
458+
if (!originNode || !isPrimitiveNode(originNode)) return
459+
if (config) {
460+
originNode.recreateWidget()
461+
} else if (!app.configuringGraph) {
462+
originNode.disconnectOutput(0)
463+
originNode.onLastDisconnect()
468464
}
469465
}
470466

@@ -555,15 +551,6 @@ app.registerExtension({
555551
}
556552
)
557553

558-
function isNodeAtPos(pos: Point) {
559-
for (const n of app.graph.nodes) {
560-
if (n.pos[0] === pos[0] && n.pos[1] === pos[1]) {
561-
return true
562-
}
563-
}
564-
return false
565-
}
566-
567554
// Double click a widget input to automatically attach a primitive
568555
const origOnInputDblClick = nodeType.prototype.onInputDblClick
569556
nodeType.prototype.onInputDblClick = function (
@@ -589,18 +576,18 @@ app.registerExtension({
589576

590577
// Create a primitive node
591578
const node = LiteGraph.createNode('PrimitiveNode')
592-
if (!node) return r
579+
const graph = app.canvas.graph
580+
if (!node || !graph) return r
593581

594-
this.graph?.add(node)
582+
graph?.add(node)
595583

596584
// Calculate a position that won't directly overlap another node
597585
const pos: [number, number] = [
598586
this.pos[0] - node.size[0] - 30,
599587
this.pos[1]
600588
]
601-
while (isNodeAtPos(pos)) {
589+
while (graph.getNodeOnPos(pos[0], pos[1], graph.nodes))
602590
pos[1] += LiteGraph.NODE_TITLE_HEIGHT
603-
}
604591

605592
node.pos = pos
606593
node.connect(0, this, slot)

0 commit comments

Comments
 (0)