Skip to content
Closed
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
110 changes: 110 additions & 0 deletions browser_tests/tests/vueNodes/interactions/node/alignment.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { expect } from '@playwright/test'

import { comfyPageFixture as test } from '../../../../fixtures/ComfyPage'
import type { ComfyPage } from '../../../../fixtures/ComfyPage'

async function getNodeHeader(comfyPage: ComfyPage, title: string) {
const node = comfyPage.vueNodes.getNodeByTitle(title).first()
await expect(node).toBeVisible()
return node.locator('.lg-node-header')
}

async function selectTwoNodes(comfyPage: ComfyPage) {
const checkpointHeader = await getNodeHeader(comfyPage, 'Load Checkpoint')
const ksamplerHeader = await getNodeHeader(comfyPage, 'KSampler')

await checkpointHeader.click()
await ksamplerHeader.click({ modifiers: ['Control'] })
await comfyPage.nextFrame()
}

test.describe('Vue Node Alignment', { tag: '@ui' }, () => {
test.beforeEach(async ({ comfyPage }) => {
await comfyPage.settings.setSetting('Comfy.UseNewMenu', 'Disabled')
await comfyPage.settings.setSetting('Comfy.VueNodes.Enabled', true)
await comfyPage.settings.setSetting('Comfy.Canvas.SelectionToolbox', true)
await comfyPage.workflow.loadWorkflow('default')
await comfyPage.canvasOps.resetView()
await comfyPage.vueNodes.waitForNodes(6)
})

test('snaps a dragged node to another node in Vue nodes mode', async ({
comfyPage
}) => {
await comfyPage.settings.setSetting(
'Comfy.Canvas.AlignNodesWhileDragging',
true
)

const ksamplerNode = comfyPage.vueNodes.getNodeByTitle('KSampler').first()
const checkpointNode = comfyPage.vueNodes
.getNodeByTitle('Load Checkpoint')
.first()
const ksamplerHeader = ksamplerNode.locator('.lg-node-header')

const ksamplerBox = await ksamplerNode.boundingBox()
const checkpointBox = await checkpointNode.boundingBox()
const headerBox = await ksamplerHeader.boundingBox()

if (!ksamplerBox || !checkpointBox || !headerBox) {
throw new Error('Expected Vue node bounding boxes to be available')
}

const dragStart = {
x: headerBox.x + headerBox.width / 2,
y: headerBox.y + headerBox.height / 2
}
const targetLeft = checkpointBox.x + 5
const dragTarget = {
x: dragStart.x + (targetLeft - ksamplerBox.x),
y: dragStart.y
}

await comfyPage.canvasOps.dragAndDrop(dragStart, dragTarget)

await expect
.poll(async () => {
const draggedBox = await ksamplerNode.boundingBox()
return draggedBox ? Math.round(draggedBox.x) : null
})
.toBe(Math.round(checkpointBox.x))
})

test('shows center alignment actions from the multi-node right-click menu', async ({
comfyPage
}) => {
await selectTwoNodes(comfyPage)

const ksamplerHeader = await getNodeHeader(comfyPage, 'KSampler')
await ksamplerHeader.click({ button: 'right' })

const alignMenuItem = comfyPage.page.getByText('Align Selected To', {
exact: true
})
await expect(alignMenuItem).toBeVisible()
await alignMenuItem.hover()

await expect(
comfyPage.page.getByText('Horizontal Center', { exact: true })
).toBeVisible()
await expect(
comfyPage.page.getByText('Vertical Center', { exact: true })
).toBeVisible()
})

test('does not show alignment actions from the selection toolbox More Options menu', async ({
comfyPage
}) => {
await selectTwoNodes(comfyPage)
await expect(comfyPage.selectionToolbox).toBeVisible()

await comfyPage.page.click('[data-testid="more-options-button"]')

await expect(
comfyPage.page.getByText('Rename', { exact: true })
).toBeVisible()
await expect(
comfyPage.page.getByText('Align Selected To', { exact: true })
).not.toBeVisible()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ import Button from '@/components/ui/button/Button.vue'
import { toggleNodeOptions } from '@/composables/graph/useMoreOptionsMenu'

const handleClick = (event: Event) => {
toggleNodeOptions(event)
toggleNodeOptions(event, 'toolbar')
}
</script>
11 changes: 11 additions & 0 deletions src/components/rightSidePanel/settings/TabGlobalSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const snapToGrid = computed({
set: (value) => settingStore.set('pysssss.SnapToGrid', value)
})

const alignNodesWhileDragging = computed({
get: () => settingStore.get('Comfy.Canvas.AlignNodesWhileDragging'),
set: (value) =>
settingStore.set('Comfy.Canvas.AlignNodesWhileDragging', value)
})

// CONNECTION LINKS settings
const linkShape = computed({
get: () => settingStore.get('Comfy.Graph.LinkMarkers'),
Expand Down Expand Up @@ -160,6 +166,11 @@ function openFullSettings() {
:label="t('rightSidePanel.globalSettings.snapNodesToGrid')"
:tooltip="t('settings.pysssss_SnapToGrid.tooltip')"
/>
<FieldSwitch
v-model="alignNodesWhileDragging"
:label="t('rightSidePanel.globalSettings.alignNodesWhileDragging')"
:tooltip="t('settings.Comfy_Canvas_AlignNodesWhileDragging.tooltip')"
/>
</div>
</PropertiesAccordionItem>

Expand Down
14 changes: 9 additions & 5 deletions src/composables/graph/contextMenuConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const CORE_MENU_ITEMS = new Set([
'Convert to Subgraph',
'Frame selection',
'Frame Nodes',
'Align Selected To',
'Distribute Nodes',
'Minimize Node',
'Expand',
'Collapse',
Expand Down Expand Up @@ -229,6 +231,8 @@ const MENU_ORDER: string[] = [
'Convert to Subgraph',
'Frame selection',
'Frame Nodes',
'Align Selected To',
'Distribute Nodes',
'Minimize Node',
'Expand',
'Collapse',
Expand Down Expand Up @@ -301,14 +305,14 @@ export function buildStructuredMenu(options: MenuOption[]): MenuOption[] {
// Section boundaries based on MENU_ORDER indices
// Section 1: 0-2 (Rename, Copy, Duplicate)
// Section 2: 3-8 (Run Branch, Pin, Unpin, Bypass, Remove Bypass, Mute)
// Section 3: 9-15 (Convert to Subgraph, Frame selection, Minimize Node, Expand, Collapse, Resize, Clone)
// Section 4: 16-17 (Node Info, Color)
// Section 5: 18+ (Image operations and fallback items)
// Section 3: 9-17 (Convert to Subgraph ... Clone)
// Section 4: 18-19 (Node Info, Color)
// Section 5: 20+ (Image operations and fallback items)
const getSectionNumber = (index: number): number => {
if (index <= 2) return 1
if (index <= 8) return 2
if (index <= 15) return 3
if (index <= 17) return 4
if (index <= 17) return 3
if (index <= 19) return 4
return 5
}

Expand Down
108 changes: 108 additions & 0 deletions src/composables/graph/useMoreOptionsMenu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { computed, ref } from 'vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import {
showNodeOptions,
toggleNodeOptions,
useMoreOptionsMenu
} from '@/composables/graph/useMoreOptionsMenu'

const selectedItems = ref([{ id: 'node-1' }, { id: 'node-2' }])
const selectedNodes = ref([{ id: 'node-1' }, { id: 'node-2' }])

vi.mock('@/renderer/core/canvas/canvasStore', () => ({
useCanvasStore: () => ({
canvas: null
})
}))

vi.mock('@/utils/litegraphUtil', () => ({
isLGraphGroup: () => false
}))

vi.mock('@/composables/graph/useSelectionState', () => ({
useSelectionState: () => ({
selectedItems: computed(() => selectedItems.value),
selectedNodes: computed(() => selectedNodes.value),
nodeDef: computed(() => null),
showNodeHelp: vi.fn(),
hasSubgraphs: computed(() => false),
hasImageNode: computed(() => false),
hasOutputNodesSelected: computed(() => false),
hasMultipleSelection: computed(() => true),
computeSelectionFlags: () => ({
collapsed: false,
pinned: false,
bypassed: false
})
})
}))

vi.mock('@/composables/graph/useImageMenuOptions', () => ({
useImageMenuOptions: () => ({
getImageMenuOptions: () => []
})
}))

vi.mock('@/composables/graph/useNodeMenuOptions', () => ({
useNodeMenuOptions: () => ({
getNodeInfoOption: () => ({ label: 'Node Info' }),
getNodeVisualOptions: () => [],
getPinOption: () => ({ label: 'Pin' }),
getBypassOption: () => ({ label: 'Bypass' }),
getRunBranchOption: () => ({ label: 'Run Branch' })
})
}))

vi.mock('@/composables/graph/useGroupMenuOptions', () => ({
useGroupMenuOptions: () => ({
getFitGroupToNodesOption: () => ({ label: 'Fit Group to Nodes' }),
getGroupColorOptions: () => ({ label: 'Group Color' }),
getGroupModeOptions: () => []
})
}))

vi.mock('@/composables/graph/useSelectionMenuOptions', () => ({
useSelectionMenuOptions: () => ({
getBasicSelectionOptions: () => [{ label: 'Rename' }],
getMultipleNodesOptions: () => [{ label: 'Frame Nodes' }],
getSubgraphOptions: () => [],
getAlignmentOptions: () => [{ label: 'Align Selected To' }]
})
}))

vi.mock('@/core/graph/subgraph/promotedWidgetTypes', () => ({
isPromotedWidgetView: () => false
}))

vi.mock('@/services/litegraphService', () => ({
getExtraOptionsForWidget: () => []
}))

describe('useMoreOptionsMenu', () => {
beforeEach(() => {
selectedItems.value = [{ id: 'node-1' }, { id: 'node-2' }]
selectedNodes.value = [{ id: 'node-1' }, { id: 'node-2' }]
toggleNodeOptions(new Event('click'), 'toolbar')
})

it('adds alignment options for right-click menus only', () => {
const { menuOptions } = useMoreOptionsMenu()

expect(
menuOptions.value.some((option) => option.label === 'Align Selected To')
).toBe(false)

showNodeOptions(new MouseEvent('contextmenu'))

expect(
menuOptions.value.some((option) => option.label === 'Align Selected To')
).toBe(true)

toggleNodeOptions(new Event('click'), 'toolbar')

expect(
menuOptions.value.some((option) => option.label === 'Align Selected To')
).toBe(false)
})
})
19 changes: 16 additions & 3 deletions src/composables/graph/useMoreOptionsMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,28 @@ export interface SubMenuOption {
disabled?: boolean
}

type NodeOptionsTriggerSource = 'contextmenu' | 'toolbar'

export enum BadgeVariant {
NEW = 'new',
DEPRECATED = 'deprecated'
}

// Global singleton for NodeOptions component reference
let nodeOptionsInstance: null | NodeOptionsInstance = null
const nodeOptionsTriggerSource = ref<NodeOptionsTriggerSource>('toolbar')

const hoveredWidget = ref<[string, NodeId | undefined]>()

/**
* Toggle the node options popover
* @param event - The trigger event
*/
export function toggleNodeOptions(event: Event) {
export function toggleNodeOptions(
event: Event,
triggerSource: NodeOptionsTriggerSource = 'toolbar'
) {
nodeOptionsTriggerSource.value = triggerSource
if (nodeOptionsInstance?.toggle) {
nodeOptionsInstance.toggle(event)
}
Expand All @@ -71,8 +78,10 @@ export function toggleNodeOptions(event: Event) {
export function showNodeOptions(
event: MouseEvent,
widgetName?: string,
nodeId?: NodeId
nodeId?: NodeId,
triggerSource: NodeOptionsTriggerSource = 'contextmenu'
) {
nodeOptionsTriggerSource.value = triggerSource
hoveredWidget.value = widgetName ? [widgetName, nodeId] : undefined
if (nodeOptionsInstance?.show) {
nodeOptionsInstance.show(event)
Expand Down Expand Up @@ -147,7 +156,8 @@ export function useMoreOptionsMenu() {
const {
getBasicSelectionOptions,
getMultipleNodesOptions,
getSubgraphOptions
getSubgraphOptions,
getAlignmentOptions
} = useSelectionMenuOptions()

const hasSubgraphs = hasSubgraphsComputed
Expand Down Expand Up @@ -227,6 +237,9 @@ export function useMoreOptionsMenu() {
)
if (hasMultipleNodes.value) {
options.push(...getMultipleNodesOptions())
if (nodeOptionsTriggerSource.value === 'contextmenu') {
options.push(...getAlignmentOptions(selectedNodes.value.length))
}
}
if (groupContext) {
options.push(getFitGroupToNodesOption(groupContext))
Expand Down
Loading
Loading