Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/lib/litegraph/src/LGraphCanvas.drawConnections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,10 @@ describe('drawConnections hidden links', () => {

canvas.setGraph(graph)
expect(canvas.dirty_bgcanvas).toBe(true)
const [clientX, clientY] = outputBadgePoint(link)
canvas.processMouseMove(
new PointerEvent('pointermove', { clientX, clientY, isPrimary: false })
)
canvas.drawConnections(createMockCtx())

expect(isLinkRevealed(scope.rootGraphId, link.id)).toBe(false)
Expand All @@ -900,6 +904,25 @@ describe('drawConnections hidden links', () => {
expect(canvas.dirty_bgcanvas).toBe(true)
})

it('rebuilds badge hit areas after a same-graph reset without a reveal', () => {
const link = createHiddenLink()
canvas.drawConnections(createMockCtx())
canvas.dirty_bgcanvas = false

canvas.setGraph(graph)

expect(canvas.dirty_bgcanvas).toBe(true)
expect(
queryLinkBadgeAtPoint(canvas, ...outputBadgePoint(link))
).toBeUndefined()

canvas.drawConnections(createMockCtx())

expect(queryLinkBadgeAtPoint(canvas, ...outputBadgePoint(link))).toBe(
link.id
)
})

it('clears slot-owned reveals and badge hit areas when the graph changes', () => {
const link = createHiddenLink()
canvas.drawConnections(createMockCtx())
Expand Down Expand Up @@ -997,6 +1020,99 @@ describe('drawConnections hidden links', () => {
])
})

it.for([false, true])(
'orders both ends by displayed slot position with reversed creation %s',
(reverseCreation) => {
const source = new LGraphNode('Source')
source.pos = [0, 100]
source.addOutput('upper', 'STRING').pos = [150, 10]
source.addOutput('lower', 'STRING').pos = [150, 20]
graph.add(source)
const target = new LGraphNode('Target')
target.pos = [400, 100]
target.addInput('upper', 'STRING').pos = [0, 10]
target.addInput('lower', 'STRING').pos = [0, 20]
graph.add(target)

const slots = reverseCreation ? [1, 0] : [0, 1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Nit: Keep each table row’s setup explicit. This ternary makes the row compute its own scenario, and the test then loops to hide links. The repository test guidance requires scenario values in the rows and no conditionals or loops in test bodies. Please put the explicit slot order in each row and patch the two links directly, or move that setup into a helper outside the test body. The same new one-layout loops appear in the visibility, context-menu, and badge-layout tests; retrieving the expected layout with assert.exists before drawing would also make an empty map fail at setup.

const links = slots.map((slot) =>
createTestLink(graph, source, slot, target, 1 - slot)
)
const upperOutputLink = links.find((link) => link.origin_slot === 0)
const upperInputLink = links.find((link) => link.target_slot === 0)
if (!upperOutputLink || !upperInputLink)
throw new Error('Missing crossed links')
const scope = graphScopeOf(graph)
const presentationStore = useLinkPresentationStore()
for (const link of links.toReversed()) {
presentationStore.patch(scope, link.id, { hidden: true })
}

canvas.drawConnections(createMockCtx())

expect(queryLinkBadgeAtPoint(canvas, 170, 110)).toBe(upperOutputLink.id)
expect(queryLinkBadgeAtPoint(canvas, 170, 132)).toBe(upperInputLink.id)
expect(queryLinkBadgeAtPoint(canvas, 380, 110)).toBe(upperInputLink.id)
expect(queryLinkBadgeAtPoint(canvas, 380, 132)).toBe(upperOutputLink.id)

canvas.processMouseMove(
new PointerEvent('pointermove', {
clientX: 380,
clientY: 132,
isPrimary: false
})
)
expect(isLinkRevealed(scope.rootGraphId, upperOutputLink.id)).toBe(true)
const renderLink = vi.spyOn(canvas, 'renderLink')
const ctx = createMockCtx()

canvas.drawConnections(ctx)

const inputBadge = vi
.mocked(ctx.roundRect)
.mock.calls.find(
([x, y, width, height]) =>
x < 380 && x + width > 380 && y < 132 && y + height > 132
)
expect(inputBadge).toBeDefined()
expect(renderLink).toHaveBeenCalledOnce()
expect(renderLink.mock.calls[0][2]).toEqual([inputBadge?.[0], 132])
}
)

it('follows widget rows when their input slot indices run in reverse', () => {
const source = new LGraphNode('Source')
source.pos = [0, 100]
source.addOutput('first', 'STRING')
source.addOutput('second', 'STRING')
graph.add(source)
const target = new LGraphNode('Target')
target.pos = [400, 100]
const upperWidget = target.addWidget('text', 'upper', '', null)
upperWidget.computeSize = () => [100, 6]
const lowerWidget = target.addWidget('text', 'lower', '', null)
target.addInput('lower', 'STRING').widget = { name: 'lower' }
target.addInput('upper', 'STRING').widget = { name: 'upper' }
graph.add(target)
const lowerLink = createTestLink(graph, source, 0, target, 0)
const upperLink = createTestLink(graph, source, 1, target, 1)
const presentationStore = useLinkPresentationStore()
for (const link of [lowerLink, upperLink]) {
presentationStore.patch(graphScopeOf(graph), link.id, { hidden: true })
}

const ctx = createMockCtx()
canvas.drawConnections(ctx)

expect(upperWidget.y).toBeLessThan(lowerWidget.y)
const [socketX, socketY] = target.getInputPos(1)
const badgeX = socketX - BADGE_GAP - 4
expect(queryLinkBadgeAtPoint(canvas, badgeX, socketY)).toBe(upperLink.id)
expect(queryLinkBadgeAtPoint(canvas, badgeX, socketY + 22)).toBe(
lowerLink.id
)
})

it('suppresses reroutes until the full routed link is revealed', () => {
const link = createHiddenLink()
const reroute = graph.createReroute([225, 150], link)
Expand Down
22 changes: 12 additions & 10 deletions src/lib/litegraph/src/LGraphCanvas.linkVisibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,18 @@ describe('LGraphCanvas link visibility interactions', () => {
useLinkPresentationStore().patch(graphScopeOf(graph), link.id, {
hidden: true
})
const layout = layoutHiddenLinkBadges(
canvas,
canvas.ctx,
link,
{ hidden: true },
[400, 300],
[700, 300],
'#89A'
)
drawHiddenLinkBadges(canvas.ctx, layout, [0, 0, 800, 600])
const layouts = layoutHiddenLinkBadges(canvas, canvas.ctx, [
{
link,
presentation: { hidden: true },
startPos: [400, 300],
endPos: [700, 300],
color: '#89A'
}
])
for (const layout of layouts.values()) {
drawHiddenLinkBadges(canvas.ctx, layout, [0, 0, 800, 600])
}
const prompt = vi
.spyOn(canvas, 'prompt')
.mockReturnValue(document.createElement('div'))
Expand Down
10 changes: 5 additions & 5 deletions src/lib/litegraph/src/LGraphCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,10 +2110,10 @@ export class LGraphCanvas implements CustomEventDispatcher<LGraphCanvasEventMap>
*/
setGraph(newGraph: LGraph | Subgraph): void {
const { graph } = this
if (graph && clearRootLinkReveals(graphScopeOf(graph).rootGraphId))
this.dirty_bgcanvas = true
if (newGraph === graph) return
if (graph) clearRootLinkReveals(graphScopeOf(graph).rootGraphId)
clearLinkBadgeHitAreas(this)
this.dirty_bgcanvas = true
if (newGraph === graph) return

// Drop any in-flight ghost so listeners don't outlive the graph it belongs to
if (this.state.ghostNodeId != null) this.finalizeGhostPlacement(true)
Expand Down Expand Up @@ -6121,8 +6121,8 @@ export class LGraphCanvas implements CustomEventDispatcher<LGraphCanvasEventMap>
this._renderAllLinkSegments(
ctx,
link,
hiddenLayout?.outputTip ?? startPos,
hiddenLayout?.inputTip ?? endPos,
hiddenLayout?.output.tip ?? startPos,
hiddenLayout?.input.tip ?? endPos,
visibleReroutes,
renderedRerouteSegments,
now,
Expand Down
44 changes: 24 additions & 20 deletions src/lib/litegraph/src/canvas/getCanvasContextMenuTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ describe('getCanvasContextMenuTarget', () => {
it('returns a hidden link hit on its badge', () => {
const link = createLink(5)
hide(link)
const layout = layoutHiddenLinkBadges(
canvas,
canvas.ctx,
link,
{ hidden: true },
[-10, 20],
[200, 20],
'#cab8ff'
)
drawHiddenLinkBadges(canvas.ctx, layout, [0, 0, 800, 600])
const layouts = layoutHiddenLinkBadges(canvas, canvas.ctx, [
{
link,
presentation: { hidden: true },
startPos: [-10, 20],
endPos: [200, 20],
color: '#cab8ff'
}
])
for (const layout of layouts.values()) {
drawHiddenLinkBadges(canvas.ctx, layout, [0, 0, 800, 600])
}
const target = resolve()

expect(target.group).toBe(group)
Expand Down Expand Up @@ -249,16 +251,18 @@ describe('getCanvasContextMenuTarget', () => {
const link = createLink(5)
hide(link)
mockQueryRerouteAtPoint.mockReturnValue({ id: 9 })
const layout = layoutHiddenLinkBadges(
canvas,
canvas.ctx,
link,
{ hidden: true },
[-10, 20],
[200, 20],
'#cab8ff'
)
drawHiddenLinkBadges(canvas.ctx, layout, [0, 0, 800, 600])
const layouts = layoutHiddenLinkBadges(canvas, canvas.ctx, [
{
link,
presentation: { hidden: true },
startPos: [-10, 20],
endPos: [200, 20],
color: '#cab8ff'
}
])
for (const layout of layouts.values()) {
drawHiddenLinkBadges(canvas.ctx, layout, [0, 0, 800, 600])
}

const target = resolve()

Expand Down
44 changes: 14 additions & 30 deletions src/lib/litegraph/src/canvas/linkBadgeRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { LGraph } from '../LGraph'
import type { LLink } from '../LLink'
import type { LinkId } from '@/types/linkId'
import type { LinkBadgeLayout } from './linkBadges'
import type { HiddenLinkBadge } from './linkBadges'
import { graphScopeOf } from '@/types/graphScopeId'
import { compareNodeIds } from '@/types/nodeId'
import { useLinkPresentationStore } from '@/stores/linkPresentationStore'
import { layoutHiddenLinkBadges, queryLinkBadgeAtPoint } from './linkBadges'
import { getLinkEndpointPositions } from './linkGeometry'
Expand Down Expand Up @@ -33,41 +31,27 @@ export function layoutGraphLinkBadges(
graph: LGraph,
linkTypeColors: Readonly<Record<string | number, string>>,
defaultLinkColor: string
): Map<LinkId, LinkBadgeLayout> {
): ReturnType<typeof layoutHiddenLinkBadges> {
const scope = graphScopeOf(graph)
const presentationStore = useLinkPresentationStore()
const hiddenLinks: LLink[] = []
const hiddenLinks: HiddenLinkBadge[] = []
for (const linkId of presentationStore.graphHiddenLinkIds(scope)) {
const link = graph.getLink(linkId)
if (link) hiddenLinks.push(link)
}
hiddenLinks.sort(
(first, second) =>
compareNodeIds(first.origin_id, second.origin_id) ||
first.origin_slot - second.origin_slot ||
first.id - second.id
)
const layouts = new Map<LinkId, LinkBadgeLayout>()
for (const link of hiddenLinks) {
if (!link) continue
const endpoints = getLinkEndpointPositions(graph, link)
const presentation = presentationStore.getPresentation(scope, link.id)
if (!endpoints || !presentation) continue

const [startPos, endPos] = endpoints
layouts.set(
link.id,
layoutHiddenLinkBadges(
host,
ctx,
link,
presentation,
startPos,
endPos,
hiddenLinks.push({
link,
presentation,
startPos: endpoints[0],
endPos: endpoints[1],
color:
(typeof link.color === 'string' && link.color) ||
linkTypeColors[slotTypeKey(link.type)] ||
defaultLinkColor
)
)
linkTypeColors[slotTypeKey(link.type)] ||
defaultLinkColor
})
}
return layouts
return layoutHiddenLinkBadges(host, ctx, hiddenLinks)
}
Loading
Loading