diff --git a/browser_tests/tests/vueNodes/interactions/node/slotContextMenu.spec.ts b/browser_tests/tests/vueNodes/interactions/node/slotContextMenu.spec.ts new file mode 100644 index 00000000000..d2e1fd8f769 --- /dev/null +++ b/browser_tests/tests/vueNodes/interactions/node/slotContextMenu.spec.ts @@ -0,0 +1,209 @@ +import { expect } from '@playwright/test' + +import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage' + +test.describe('Vue Nodes - Slot Context Menu', () => { + test.beforeEach(async ({ comfyPage }) => { + await comfyPage.settings.setSetting('Comfy.VueNodes.Enabled', true) + await comfyPage.settings.setSetting('Comfy.Graph.CanvasMenu', false) + await comfyPage.setup() + await comfyPage.vueNodes.waitForNodes() + }) + + test('Right-clicking an input slot dot shows the slot context menu', async ({ + comfyPage + }) => { + // KSampler has input slots with connections — find any input slot + const ksamplerNode = comfyPage.vueNodes.getNodeByTitle('KSampler') + const inputSlot = ksamplerNode.locator('.lg-slot--input').first() + await expect(inputSlot).toBeVisible() + + await inputSlot.locator('.slot-dot').click({ button: 'right' }) + + // The custom slot context menu should appear (role="menu" is on the + // teleported menu, not on the PrimeVue node context menu) + const slotMenu = comfyPage.page.locator( + 'div[role="menu"]:not(.p-contextmenu)' + ) + await expect(slotMenu).toBeVisible() + + // Should show "Rename" since standard slots are not nameLocked + await expect( + slotMenu.getByRole('menuitem', { name: 'Rename' }) + ).toBeVisible() + }) + + test('Right-clicking the node body shows the node context menu, not the slot menu', async ({ + comfyPage + }) => { + const ksamplerNode = comfyPage.vueNodes.getNodeByTitle('KSampler') + const nodeBody = ksamplerNode.locator('[data-testid^="node-body-"]') + await expect(nodeBody).toBeVisible() + + await nodeBody.click({ button: 'right' }) + await comfyPage.nextFrame() + + // The PrimeVue node context menu should appear + const nodeMenu = comfyPage.page.locator('.p-contextmenu') + await expect(nodeMenu).toBeVisible() + + // Slot-specific items should NOT be present + await expect( + comfyPage.page.getByRole('menuitem', { name: 'Disconnect Links' }) + ).toBeHidden() + }) + + test('Right-clicking an output slot dot shows the slot context menu', async ({ + comfyPage + }) => { + const checkpointNode = comfyPage.vueNodes.getNodeByTitle('Load Checkpoint') + const outputSlot = checkpointNode.locator('.lg-slot--output').first() + await expect(outputSlot).toBeVisible() + + await outputSlot.locator('.slot-dot').click({ button: 'right' }) + + const slotMenu = comfyPage.page.locator( + 'div[role="menu"]:not(.p-contextmenu)' + ) + await expect(slotMenu).toBeVisible() + + await expect( + slotMenu.getByRole('menuitem', { name: 'Rename' }) + ).toBeVisible() + }) + + test('Slot context menu closes when clicking outside', async ({ + comfyPage + }) => { + const ksamplerNode = comfyPage.vueNodes.getNodeByTitle('KSampler') + const inputSlot = ksamplerNode.locator('.lg-slot--input').first() + await expect(inputSlot).toBeVisible() + + await inputSlot.locator('.slot-dot').click({ button: 'right' }) + + const slotMenu = comfyPage.page.locator( + 'div[role="menu"]:not(.p-contextmenu)' + ) + await expect(slotMenu).toBeVisible() + + // Press Escape to close the menu + await comfyPage.page.keyboard.press('Escape') + + await expect(slotMenu).toBeHidden() + }) + + test('Rename Slot updates the slot label reactively in the DOM', async ({ + comfyPage + }) => { + // Find a KSampler input slot that is renamable (not widget-backed) + const slotInfo = await comfyPage.page.evaluate(() => { + const graph = window.app!.graph! + const node = graph.findNodesByType('KSampler', [])[0] + if (!node?.inputs) return null + const idx = node.inputs.findIndex( + (i) => !i.nameLocked && !('widget' in i && i.widget) + ) + if (idx < 0) return null + return { + index: idx, + originalName: node.inputs[idx].label || node.inputs[idx].name + } + }) + expect(slotInfo).not.toBeNull() + + const ksamplerNode = comfyPage.vueNodes.getNodeByTitle('KSampler') + const inputSlot = ksamplerNode + .locator('.lg-slot--input') + .nth(slotInfo!.index) + await expect(inputSlot).toBeVisible() + + // Right-click to open slot context menu + await inputSlot.locator('.slot-dot').click({ button: 'right' }) + + const slotMenu = comfyPage.page.locator( + 'div[role="menu"]:not(.p-contextmenu)' + ) + await expect(slotMenu).toBeVisible() + + // Click "Rename" + await slotMenu.getByRole('menuitem', { name: 'Rename' }).click() + + // Menu should close, prompt dialog should appear + await expect(slotMenu).toBeHidden() + + const promptInput = comfyPage.page.locator('.prompt-dialog-content input') + await expect(promptInput).toBeVisible() + + // Type a new name and confirm + await promptInput.fill('MyCustomSlot') + await comfyPage.page.locator('.prompt-dialog-content button').click() + + // Wait for dialog to close + await expect(promptInput).toBeHidden() + await comfyPage.nextFrame() + + // The slot label in the DOM should reactively reflect the new name + await expect(inputSlot).toContainText('MyCustomSlot') + + // Verify the underlying graph data also changed + const labelInGraph = await comfyPage.page.evaluate((idx) => { + const graph = window.app!.graph! + const node = graph.findNodesByType('KSampler', [])[0] + return node?.inputs?.[idx]?.label + }, slotInfo!.index) + expect(labelInGraph).toBe('MyCustomSlot') + }) + + test('Slot context menu Disconnect Links removes connections', async ({ + comfyPage + }) => { + // Use page.evaluate to find a KSampler input with an active link + const connectedSlot = await comfyPage.page.evaluate(() => { + const graph = window.app!.graph! + const nodes = graph.findNodesByType('KSampler', []) + const ksamplerNode = nodes.find((n) => + n.inputs?.some((i) => i.link != null) + ) + if (!ksamplerNode) return null + const slotIndex = ksamplerNode.inputs!.findIndex((i) => i.link != null) + return { nodeId: ksamplerNode.id, slotIndex } + }) + expect(connectedSlot).not.toBeNull() + + // Find the corresponding Vue input slot by node ID + const ksamplerNode = comfyPage.page.locator( + `[data-node-id="${connectedSlot!.nodeId}"]` + ) + const inputSlot = ksamplerNode + .locator('.lg-slot--input') + .nth(connectedSlot!.slotIndex) + await expect(inputSlot).toBeVisible() + + await inputSlot.locator('.slot-dot').click({ button: 'right' }) + + const slotMenu = comfyPage.page.locator( + 'div[role="menu"]:not(.p-contextmenu)' + ) + await expect(slotMenu).toBeVisible() + + await expect( + slotMenu.getByRole('menuitem', { name: 'Disconnect Links' }) + ).toBeVisible() + + await slotMenu.getByRole('menuitem', { name: 'Disconnect Links' }).click() + + // Menu should close + await expect(slotMenu).toBeHidden() + + // Verify the link was removed in the graph + const linkRemoved = await comfyPage.page.evaluate( + ({ nodeId, slotIndex }) => { + const graph = window.app!.graph! + const node = graph.getNodeById(nodeId) + return node?.inputs?.[slotIndex]?.link == null + }, + connectedSlot! + ) + expect(linkRemoved).toBe(true) + }) +}) diff --git a/src/components/graph/GraphCanvas.vue b/src/components/graph/GraphCanvas.vue index 967b8f00f32..74497b22289 100644 --- a/src/components/graph/GraphCanvas.vue +++ b/src/components/graph/GraphCanvas.vue @@ -115,6 +115,7 @@ + @@ -154,6 +155,7 @@ import NodeContextMenu from '@/components/graph/NodeContextMenu.vue' import NodeDragPreview from '@/components/graph/NodeDragPreview.vue' import NodeSelectionModeBanner from '@/components/graph/NodeSelectionModeBanner.vue' import SelectionToolbox from '@/components/graph/SelectionToolbox.vue' +import SlotContextMenu from '@/components/graph/SlotContextMenu.vue' import TitleEditor from '@/components/graph/TitleEditor.vue' import NodePropertiesPanel from '@/components/rightSidePanel/RightSidePanel.vue' import { useAgentDockMount } from '@/workbench/extensions/agent/composables/useAgentDockMount' diff --git a/src/components/graph/NodeContextMenu.vue b/src/components/graph/NodeContextMenu.vue index 788b5780f65..a19795575ee 100644 --- a/src/components/graph/NodeContextMenu.vue +++ b/src/components/graph/NodeContextMenu.vue @@ -46,11 +46,12 @@ diff --git a/src/composables/graph/useCanvasAnchoredPosition.ts b/src/composables/graph/useCanvasAnchoredPosition.ts new file mode 100644 index 00000000000..6650da77832 --- /dev/null +++ b/src/composables/graph/useCanvasAnchoredPosition.ts @@ -0,0 +1,77 @@ +import { useElementBounding, useRafFn } from '@vueuse/core' +import type { Ref } from 'vue' +import { ref, watchEffect } from 'vue' + +import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' + +export function useCanvasAnchoredPosition(isOpen: Ref) { + const canvasStore = useCanvasStore() + const lgCanvas = canvasStore.getCanvas() + const { left: canvasLeft, top: canvasTop } = useElementBounding( + lgCanvas.canvas + ) + + const screenPosition = ref({ x: 0, y: 0 }) + const worldPosition = ref({ x: 0, y: 0 }) + + let lastScale = 0 + let lastOffsetX = 0 + let lastOffsetY = 0 + + function anchorToEvent(event: MouseEvent) { + const screenX = event.clientX - canvasLeft.value + const screenY = event.clientY - canvasTop.value + const { scale, offset } = lgCanvas.ds + + worldPosition.value = { + x: screenX / scale - offset[0], + y: screenY / scale - offset[1] + } + + lastScale = scale + lastOffsetX = offset[0] + lastOffsetY = offset[1] + + screenPosition.value = { + x: event.clientX, + y: event.clientY + } + } + + function updateScreenPosition() { + if (!isOpen.value) return + + const { scale, offset } = lgCanvas.ds + if ( + scale === lastScale && + offset[0] === lastOffsetX && + offset[1] === lastOffsetY + ) { + return + } + + lastScale = scale + lastOffsetX = offset[0] + lastOffsetY = offset[1] + + screenPosition.value = { + x: (worldPosition.value.x + offset[0]) * scale + canvasLeft.value, + y: (worldPosition.value.y + offset[1]) * scale + canvasTop.value + } + } + + const { resume: startSync, pause: stopSync } = useRafFn( + updateScreenPosition, + { immediate: false } + ) + + watchEffect(() => { + if (isOpen.value) { + startSync() + } else { + stopSync() + } + }) + + return { screenPosition, anchorToEvent } +} diff --git a/src/locales/en/main.json b/src/locales/en/main.json index f3f89dcb317..e1fd0407c41 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -17,11 +17,13 @@ "decrement": "Decrement", "deleteImage": "Delete image", "deleteAudioFile": "Delete audio file", + "disconnectLinks": "Disconnect Links", "increment": "Increment", "removeImage": "Remove image", "removeVideo": "Remove video", "removeTag": "Remove tag", "remove": "Remove", + "removeSlot": "Remove Slot", "chart": "Chart", "chartLowercase": "chart", "file": "file", diff --git a/src/renderer/extensions/vueNodes/components/InputSlot.vue b/src/renderer/extensions/vueNodes/components/InputSlot.vue index f9f20d65735..68b19a4a480 100644 --- a/src/renderer/extensions/vueNodes/components/InputSlot.vue +++ b/src/renderer/extensions/vueNodes/components/InputSlot.vue @@ -31,6 +31,7 @@ @click="onClick" @dblclick="onDoubleClick" @pointerdown="onPointerDown" + @contextmenu.stop.prevent="onSlotContextMenu" /> @@ -63,6 +64,11 @@ import type { INodeSlot } from '@/lib/litegraph/src/litegraph' import { useSlotLinkDragUIState } from '@/renderer/core/canvas/links/slotLinkDragUIState' import { getSlotKey } from '@/renderer/core/layout/slots/slotIdentifier' import { useNodeTooltips } from '@/renderer/extensions/vueNodes/composables/useNodeTooltips' +import { + hasAnySlotAction, + showSlotMenu +} from '@/renderer/extensions/vueNodes/composables/slotMenuService' +import { useSlotElementTracking } from '@/renderer/extensions/vueNodes/composables/useSlotElementTracking' import { useSlotLinkInteraction } from '@/renderer/extensions/vueNodes/composables/useSlotLinkInteraction' import { cn } from '@comfyorg/tailwind-utils' import type { NodeId } from '@/types/nodeId' @@ -128,4 +134,11 @@ const { onClick, onDoubleClick, onPointerDown } = useSlotLinkInteraction({ index: props.index, type: 'input' }) + +function onSlotContextMenu(event: MouseEvent) { + if (!props.nodeId) return + const ctx = { nodeId: props.nodeId, slotIndex: props.index, isInput: true } + if (!hasAnySlotAction(ctx)) return + showSlotMenu(event, ctx) +} diff --git a/src/renderer/extensions/vueNodes/components/OutputSlot.vue b/src/renderer/extensions/vueNodes/components/OutputSlot.vue index 2e1a8a743a3..f205ee71426 100644 --- a/src/renderer/extensions/vueNodes/components/OutputSlot.vue +++ b/src/renderer/extensions/vueNodes/components/OutputSlot.vue @@ -20,6 +20,7 @@ class="w-3 translate-x-1/2" :slot-data @pointerdown="onPointerDown" + @contextmenu.stop.prevent="onSlotContextMenu" /> @@ -34,6 +35,11 @@ import { RenderShape } from '@/lib/litegraph/src/types/globalEnums' import { useSlotLinkDragUIState } from '@/renderer/core/canvas/links/slotLinkDragUIState' import { getSlotKey } from '@/renderer/core/layout/slots/slotIdentifier' import { useNodeTooltips } from '@/renderer/extensions/vueNodes/composables/useNodeTooltips' +import { + hasAnySlotAction, + showSlotMenu +} from '@/renderer/extensions/vueNodes/composables/slotMenuService' +import { useSlotElementTracking } from '@/renderer/extensions/vueNodes/composables/useSlotElementTracking' import { useSlotLinkInteraction } from '@/renderer/extensions/vueNodes/composables/useSlotLinkInteraction' import { cn } from '@comfyorg/tailwind-utils' import type { NodeId } from '@/types/nodeId' @@ -112,4 +118,11 @@ const { onPointerDown } = useSlotLinkInteraction({ index: props.index, type: 'output' }) + +function onSlotContextMenu(event: MouseEvent) { + if (!props.nodeId) return + const ctx = { nodeId: props.nodeId, slotIndex: props.index, isInput: false } + if (!hasAnySlotAction(ctx)) return + showSlotMenu(event, ctx) +} diff --git a/src/renderer/extensions/vueNodes/composables/slotMenuService.test.ts b/src/renderer/extensions/vueNodes/composables/slotMenuService.test.ts new file mode 100644 index 00000000000..8063ecc6be4 --- /dev/null +++ b/src/renderer/extensions/vueNodes/composables/slotMenuService.test.ts @@ -0,0 +1,468 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const { mockGraph, mockCanvas } = vi.hoisted(() => { + const mockGraph = { + getNodeById: vi.fn(), + beforeChange: vi.fn(), + afterChange: vi.fn(), + trigger: vi.fn() + } + const mockCanvas = { + graph: mockGraph as typeof mockGraph | null, + setDirty: vi.fn() + } + return { mockGraph, mockCanvas } +}) + +vi.mock('@/scripts/app', () => ({ + app: { canvas: mockCanvas } +})) + +import { + canDisconnectSlot, + canRemoveSlot, + canRenameSlot, + disconnectSlotLinks, + hasAnySlotAction, + removeSlot, + renameSlot +} from './slotMenuService' + +describe(canRenameSlot, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('returns false when graph is null', () => { + mockCanvas.graph = null + expect(canRenameSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) + + it('returns false when node not found', () => { + mockGraph.getNodeById.mockReturnValue(null) + expect(canRenameSlot({ nodeId: '99', slotIndex: 0, isInput: true })).toBe( + false + ) + }) + + it('returns true for normal input slot', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image' }], + outputs: [] + }) + expect(canRenameSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + true + ) + }) + + it('returns true for normal output slot', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [{ type: 'IMAGE', links: [], name: 'image' }] + }) + expect(canRenameSlot({ nodeId: '1', slotIndex: 0, isInput: false })).toBe( + true + ) + }) + + it('returns false when slot is nameLocked', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image', nameLocked: true }], + outputs: [] + }) + expect(canRenameSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) + + it('returns false when input slot has a widget', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [ + { type: 'INT', link: 5, name: 'steps', widget: { name: 'steps' } } + ], + outputs: [] + }) + expect(canRenameSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) + + it('returns false when slot index out of range', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [] + }) + expect(canRenameSlot({ nodeId: '1', slotIndex: 5, isInput: true })).toBe( + false + ) + }) +}) + +describe(canDisconnectSlot, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('returns false when graph is null', () => { + mockCanvas.graph = null + expect( + canDisconnectSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + ).toBe(false) + }) + + it('returns true for input with link', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: 42, name: 'image' }], + outputs: [] + }) + expect( + canDisconnectSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + ).toBe(true) + }) + + it('returns false for input without link', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image' }], + outputs: [] + }) + expect( + canDisconnectSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + ).toBe(false) + }) + + it('returns true for output with links', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [{ type: 'IMAGE', links: [1, 2], name: 'image' }] + }) + expect( + canDisconnectSlot({ nodeId: '1', slotIndex: 0, isInput: false }) + ).toBe(true) + }) + + it('returns false for output with empty links array', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [{ type: 'IMAGE', links: [], name: 'image' }] + }) + expect( + canDisconnectSlot({ nodeId: '1', slotIndex: 0, isInput: false }) + ).toBe(false) + }) + + it('returns false for output with null links', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [{ type: 'IMAGE', links: null, name: 'image' }] + }) + expect( + canDisconnectSlot({ nodeId: '1', slotIndex: 0, isInput: false }) + ).toBe(false) + }) +}) + +describe(canRemoveSlot, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('returns false when graph is null', () => { + mockCanvas.graph = null + expect(canRemoveSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) + + it('returns true when removable is true and not locked', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image', removable: true }], + outputs: [] + }) + expect(canRemoveSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + true + ) + }) + + it('returns false when removable is false', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image', removable: false }], + outputs: [] + }) + expect(canRemoveSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) + + it('returns false when locked is true', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [ + { + type: 'IMAGE', + link: null, + name: 'image', + removable: true, + locked: true + } + ], + outputs: [] + }) + expect(canRemoveSlot({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) +}) + +describe(hasAnySlotAction, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('returns true when rename is available', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image' }], + outputs: [] + }) + expect(hasAnySlotAction({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + true + ) + }) + + it('returns true when disconnect is available', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: 42, name: 'image', nameLocked: true }], + outputs: [] + }) + expect(hasAnySlotAction({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + true + ) + }) + + it('returns false when no action available', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [ + { + type: 'IMAGE', + link: null, + name: 'image', + nameLocked: true, + removable: false + } + ], + outputs: [] + }) + expect(hasAnySlotAction({ nodeId: '1', slotIndex: 0, isInput: true })).toBe( + false + ) + }) +}) + +describe(renameSlot, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('does nothing when graph is null', () => { + mockCanvas.graph = null + renameSlot({ nodeId: '1', slotIndex: 0, isInput: true }, 'new') + expect(mockGraph.beforeChange).not.toHaveBeenCalled() + }) + + it('does nothing when nameLocked', () => { + const inputSlot = { + type: 'IMAGE', + link: null, + name: 'image', + label: '', + nameLocked: true + } + mockGraph.getNodeById.mockReturnValue({ + inputs: [inputSlot], + outputs: [], + getInputInfo: () => inputSlot, + getOutputInfo: () => null + }) + + renameSlot({ nodeId: '1', slotIndex: 0, isInput: true }, 'new') + expect(mockGraph.beforeChange).not.toHaveBeenCalled() + expect(inputSlot.label).toBe('') + }) + + it('renames an input slot label', () => { + const inputSlot = { type: 'IMAGE', link: null, name: 'image', label: '' } + mockGraph.getNodeById.mockReturnValue({ + inputs: [inputSlot], + outputs: [] + }) + + renameSlot({ nodeId: '1', slotIndex: 0, isInput: true }, 'my_image') + + expect(inputSlot.label).toBe('my_image') + expect(mockGraph.beforeChange).toHaveBeenCalled() + expect(mockGraph.afterChange).toHaveBeenCalled() + expect(mockCanvas.setDirty).toHaveBeenCalledWith(true, true) + }) + + it('renames an output slot label', () => { + const outputSlot = { + type: 'MODEL', + links: [], + name: 'model', + label: '' + } + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [outputSlot] + }) + + renameSlot({ nodeId: '1', slotIndex: 0, isInput: false }, 'my_model') + + expect(outputSlot.label).toBe('my_model') + expect(mockGraph.beforeChange).toHaveBeenCalled() + expect(mockGraph.afterChange).toHaveBeenCalled() + }) + + it('does nothing when slot not found', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [] + }) + + renameSlot({ nodeId: '1', slotIndex: 0, isInput: true }, 'new') + expect(mockGraph.beforeChange).not.toHaveBeenCalled() + }) +}) + +describe(disconnectSlotLinks, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('does nothing when graph is null', () => { + mockCanvas.graph = null + disconnectSlotLinks({ nodeId: '1', slotIndex: 0, isInput: true }) + expect(mockGraph.beforeChange).not.toHaveBeenCalled() + }) + + it('disconnects an input slot', () => { + const disconnectInput = vi.fn() + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: 42, name: 'image' }], + outputs: [], + disconnectInput + }) + + disconnectSlotLinks({ nodeId: '1', slotIndex: 0, isInput: true }) + + expect(mockGraph.beforeChange).toHaveBeenCalled() + expect(disconnectInput).toHaveBeenCalledWith(0, true) + expect(mockGraph.afterChange).toHaveBeenCalled() + expect(mockCanvas.setDirty).toHaveBeenCalledWith(true, true) + }) + + it('disconnects an output slot', () => { + const disconnectOutput = vi.fn() + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [{ type: 'IMAGE', links: [1, 2], name: 'image' }], + disconnectOutput + }) + + disconnectSlotLinks({ nodeId: '1', slotIndex: 0, isInput: false }) + + expect(mockGraph.beforeChange).toHaveBeenCalled() + expect(disconnectOutput).toHaveBeenCalledWith(0) + expect(mockGraph.afterChange).toHaveBeenCalled() + expect(mockCanvas.setDirty).toHaveBeenCalledWith(true, true) + }) + + it('triggers slot refresh event after disconnect', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: 42, name: 'image' }], + outputs: [], + disconnectInput: vi.fn() + }) + + disconnectSlotLinks({ nodeId: '1', slotIndex: 0, isInput: true }) + + expect(mockGraph.trigger).toHaveBeenCalledWith('node:slot-label:changed', { + nodeId: '1' + }) + }) +}) + +describe(removeSlot, () => { + beforeEach(() => { + vi.clearAllMocks() + mockCanvas.graph = mockGraph + }) + + it('does nothing when graph is null', () => { + mockCanvas.graph = null + removeSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + expect(mockGraph.beforeChange).not.toHaveBeenCalled() + }) + + it('removes an input slot', () => { + const removeInput = vi.fn() + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image', removable: true }], + outputs: [], + removeInput + }) + + removeSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + + expect(mockGraph.beforeChange).toHaveBeenCalled() + expect(removeInput).toHaveBeenCalledWith(0) + expect(mockGraph.afterChange).toHaveBeenCalled() + expect(mockCanvas.setDirty).toHaveBeenCalledWith(true, true) + }) + + it('removes an output slot', () => { + const removeOutput = vi.fn() + mockGraph.getNodeById.mockReturnValue({ + inputs: [], + outputs: [{ type: 'IMAGE', links: [], name: 'image', removable: true }], + removeOutput + }) + + removeSlot({ nodeId: '1', slotIndex: 0, isInput: false }) + + expect(mockGraph.beforeChange).toHaveBeenCalled() + expect(removeOutput).toHaveBeenCalledWith(0) + expect(mockGraph.afterChange).toHaveBeenCalled() + expect(mockCanvas.setDirty).toHaveBeenCalledWith(true, true) + }) + + it('does nothing when not removable', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image', removable: false }], + outputs: [] + }) + + removeSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + expect(mockGraph.beforeChange).not.toHaveBeenCalled() + }) + + it('triggers slot refresh event after remove', () => { + mockGraph.getNodeById.mockReturnValue({ + inputs: [{ type: 'IMAGE', link: null, name: 'image', removable: true }], + outputs: [], + removeInput: vi.fn() + }) + + removeSlot({ nodeId: '1', slotIndex: 0, isInput: true }) + + expect(mockGraph.trigger).toHaveBeenCalledWith('node:slot-label:changed', { + nodeId: '1' + }) + }) +}) diff --git a/src/renderer/extensions/vueNodes/composables/slotMenuService.ts b/src/renderer/extensions/vueNodes/composables/slotMenuService.ts new file mode 100644 index 00000000000..48e435f7314 --- /dev/null +++ b/src/renderer/extensions/vueNodes/composables/slotMenuService.ts @@ -0,0 +1,147 @@ +import type { Ref } from 'vue' + +import type { NodeId } from '@/lib/litegraph/src/LGraphNode' +import { app } from '@/scripts/app' + +export interface SlotMenuContext { + nodeId: NodeId + slotIndex: number + isInput: boolean +} + +interface SlotMenuInstance { + show: (event: MouseEvent, context: SlotMenuContext) => void + hide: () => void + isOpen: Ref +} + +let slotMenuInstance: SlotMenuInstance | null = null + +export function registerSlotMenuInstance( + instance: SlotMenuInstance | null +): void { + slotMenuInstance = instance +} + +export function showSlotMenu( + event: MouseEvent, + context: SlotMenuContext +): void { + slotMenuInstance?.show(event, context) +} + +function getSlotInfo(context: SlotMenuContext) { + const graph = app.canvas?.graph + if (!graph) return null + + const node = graph.getNodeById(context.nodeId) + if (!node) return null + + const slotInfo = context.isInput + ? node.inputs?.[context.slotIndex] + : node.outputs?.[context.slotIndex] + if (!slotInfo) return null + + return { graph, node, slotInfo } +} + +export function canRenameSlot(context: SlotMenuContext): boolean { + const result = getSlotInfo(context) + if (!result) return false + + const { slotInfo } = result + if (slotInfo.nameLocked) return false + if (context.isInput && 'widget' in slotInfo && slotInfo.widget) return false + + return true +} + +export function canDisconnectSlot(context: SlotMenuContext): boolean { + const result = getSlotInfo(context) + if (!result) return false + + const { slotInfo } = result + if (context.isInput) { + return 'link' in slotInfo && slotInfo.link != null + } + return ( + 'links' in slotInfo && + Array.isArray(slotInfo.links) && + slotInfo.links.length > 0 + ) +} + +export function canRemoveSlot(context: SlotMenuContext): boolean { + const result = getSlotInfo(context) + if (!result) return false + + const { slotInfo } = result + return Boolean(slotInfo.removable) && !slotInfo.locked +} + +export function hasAnySlotAction(context: SlotMenuContext): boolean { + return ( + canRenameSlot(context) || + canDisconnectSlot(context) || + canRemoveSlot(context) + ) +} + +function triggerSlotRefresh(context: SlotMenuContext): void { + const graph = app.canvas?.graph + graph?.trigger('node:slot-label:changed', { + nodeId: context.nodeId + }) +} + +export function renameSlot(context: SlotMenuContext, newLabel: string): void { + if (!canRenameSlot(context)) return + + const result = getSlotInfo(context) + if (!result) return + + const { graph, slotInfo } = result + + graph.beforeChange() + slotInfo.label = newLabel + app.canvas?.setDirty(true, true) + graph.afterChange() +} + +export function disconnectSlotLinks(context: SlotMenuContext): void { + if (!canDisconnectSlot(context)) return + + const result = getSlotInfo(context) + if (!result) return + + const { graph, node } = result + + graph.beforeChange() + if (context.isInput) { + node.disconnectInput(context.slotIndex, true) + } else { + node.disconnectOutput(context.slotIndex) + } + graph.afterChange() + app.canvas?.setDirty(true, true) + triggerSlotRefresh(context) +} + +export function removeSlot(context: SlotMenuContext): void { + if (!canRemoveSlot(context)) return + + const result = getSlotInfo(context) + if (!result) return + + const { graph, node } = result + + graph.beforeChange() + if (context.isInput) { + node.removeInput(context.slotIndex) + } else { + node.removeOutput(context.slotIndex) + } + graph.afterChange() + app.canvas?.setDirty(true, true) + triggerSlotRefresh(context) +}