LCOV - code coverage report
Current view: top level - src/renderer/core/canvas/litegraph - slotCalculations.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 99.0 % 102 101
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { LGraphNode, LGraph } from '@comfyorg/graph-model/entityRegistry'
       2              : /**
       3              :  * Slot Position Calculations
       4              :  *
       5              :  * Centralized utility for calculating input/output slot positions on nodes.
       6              :  * This allows both litegraph nodes and the layout system to use the same
       7              :  * calculation logic while providing their own position data.
       8              :  */
       9              : 
      10              : import type {
      11              :   PointTuple as Point,
      12              :   Point2D as LayoutPoint
      13              : } from '@comfyorg/shared-frontend-utils/geometry'
      14              : import type {
      15              :   INodeInputSlot,
      16              :   INodeOutputSlot
      17              : } from '@comfyorg/graph-model/slots'
      18              : import { litegraph } from '@/lib/litegraph/src/litegraphInstance'
      19              : import { isWidgetInputSlot } from '@/lib/litegraph/src/node/slotUtils'
      20              : import { TitleMode } from '@comfyorg/graph-model/globalEnums'
      21              : import { nodesInRenderOrder } from '@/renderer/core/canvas/litegraph/arrangeForLegacyRender'
      22              : import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
      23              : import type { SlotLayout } from '@comfyorg/graph-model/layout'
      24              : import { pointInBounds } from '@/renderer/core/layout/utils/layoutMath'
      25              : 
      26              : export interface SlotPositionContext {
      27              :   /** Node's X position in graph coordinates */
      28              :   nodeX: number
      29              :   /** Node's Y position in graph coordinates */
      30              :   nodeY: number
      31              :   /** Node's width */
      32              :   nodeWidth: number
      33              :   /** Node's height */
      34              :   nodeHeight: number
      35              :   /** Whether the node is collapsed */
      36              :   collapsed: boolean
      37              :   /** Collapsed width (if applicable) */
      38              :   collapsedWidth?: number
      39              :   /** Node constructor's slot_start_y offset */
      40              :   slotStartY?: number
      41              :   /** Node's input slots */
      42              :   inputs: INodeInputSlot[]
      43              :   /** Node's output slots */
      44              :   outputs: INodeOutputSlot[]
      45              :   /** Node's widgets (for widget slot detection) */
      46              :   widgets?: Array<{ name?: string }>
      47              : }
      48              : 
      49              : /**
      50              :  * Calculate the position of an input slot in graph coordinates
      51              :  * @param context Node context containing position and slot data
      52              :  * @param slot The input slot index
      53              :  * @returns Position of the input slot center in graph coordinates
      54              :  */
      55       359162 : function calculateInputSlotPos(
      56              :   context: SlotPositionContext,
      57              :   slot: number
      58              : ): Point {
      59       359162 :   const input = context.inputs.at(slot)
      60       359162 :   if (!input) return [context.nodeX, context.nodeY]
      61              : 
      62       359162 :   return calculateInputSlotPosFromSlot(context, input)
      63              : }
      64              : 
      65              : /**
      66              :  * Calculate the position of an input slot in graph coordinates
      67              :  * @param context Node context containing position and slot data
      68              :  * @param input The input slot object
      69              :  * @returns Position of the input slot center in graph coordinates
      70              :  */
      71       359216 : export function calculateInputSlotPosFromSlot(
      72              :   context: SlotPositionContext,
      73              :   input: INodeInputSlot
      74              : ): Point {
      75       359216 :   const { nodeX, nodeY, collapsed } = context
      76              : 
      77              :   // Handle collapsed nodes
      78       359216 :   if (collapsed) {
      79              :     const halfTitle = litegraph().NODE_TITLE_HEIGHT * 0.5
      80           36 :     return [nodeX, nodeY - halfTitle]
      81              :   }
      82              : 
      83              :   // Handle hard-coded positions
      84       359180 :   const { pos } = input
      85       359215 :   if (pos) return [nodeX + pos[0], nodeY + pos[1]]
      86              : 
      87              :   // Default vertical slots
      88       194991 :   const offsetX = litegraph().NODE_SLOT_HEIGHT * 0.5
      89       359216 :   const nodeOffsetY = context.slotStartY || 0
      90       359216 :   let slotIndex = -1
      91       359216 :   const inputIndex = context.inputs.indexOf(input)
      92       359216 :   if (inputIndex === -1) return [nodeX, nodeY]
      93       359216 :   if (!input.pos && !(context.widgets?.length && isWidgetInputSlot(input))) {
      94       194944 :     slotIndex = 0
      95       194944 :     for (const [index, slot] of context.inputs.entries()) {
      96       751566 :       if (index >= inputIndex) break
      97       751566 :       if (!slot.pos && !(context.widgets?.length && isWidgetInputSlot(slot))) {
      98       163148 :         slotIndex++
      99              :       }
     100              :     }
     101              :   }
     102       194991 :   const slotY = (slotIndex + 0.7) * litegraph().NODE_SLOT_HEIGHT
     103              : 
     104       194991 :   return [nodeX + offsetX, nodeY + slotY + nodeOffsetY]
     105              : }
     106              : 
     107              : /**
     108              :  * Calculate the position of an output slot in graph coordinates
     109              :  * @param context Node context containing position and slot data
     110              :  * @param slot The output slot index
     111              :  * @returns Position of the output slot center in graph coordinates
     112              :  */
     113       193179 : function calculateOutputSlotPos(
     114              :   context: SlotPositionContext,
     115              :   slot: number
     116              : ): Point {
     117       193179 :   const { nodeX, nodeY, nodeWidth, collapsed, collapsedWidth, outputs } =
     118              :     context
     119              : 
     120              :   // Handle collapsed nodes
     121       193179 :   if (collapsed) {
     122           33 :     const width = collapsedWidth || litegraph().NODE_COLLAPSED_WIDTH
     123           33 :     const halfTitle = litegraph().NODE_TITLE_HEIGHT * 0.5
     124           33 :     return [nodeX + width, nodeY - halfTitle]
     125              :   }
     126              : 
     127       193146 :   const outputSlot = outputs.at(slot)
     128       193178 :   if (!outputSlot) return [nodeX + nodeWidth, nodeY]
     129              : 
     130              :   // Handle hard-coded positions
     131       193146 :   const outputPos = outputSlot.pos
     132       193178 :   if (outputPos) return [nodeX + outputPos[0], nodeY + outputPos[1]]
     133              : 
     134              :   // Default vertical slots
     135       193146 :   const offsetX = litegraph().NODE_SLOT_HEIGHT * 0.5
     136       193179 :   const nodeOffsetY = context.slotStartY || 0
     137              :   const defaultVerticalOutputs = getDefaultVerticalOutputs(context)
     138              :   const slotIndex = defaultVerticalOutputs.indexOf(outputSlot)
     139       193179 :   const slotY = (slotIndex + 0.7) * litegraph().NODE_SLOT_HEIGHT
     140              : 
     141              :   // TODO: Why +1?
     142       193179 :   return [nodeX + nodeWidth + 1 - offsetX, nodeY + slotY + nodeOffsetY]
     143              : }
     144              : 
     145              : /**
     146              :  * Get a slot center from current node and slot geometry.
     147              :  * @param node The LGraphNode
     148              :  * @param slotIndex The slot index
     149              :  * @param isInput Whether this is an input slot
     150              :  * @returns Position of the slot center in graph coordinates
     151              :  */
     152       866476 : export function getSlotPosition(
     153              :   node: LGraphNode,
     154              :   slotIndex: number,
     155              :   isInput: boolean
     156              : ): Point {
     157       866476 :   if (litegraph().vueNodesMode)
     158       314135 :     return getVueSlotPosition(node, slotIndex, isInput)
     159              : 
     160       866476 :   const context: SlotPositionContext = {
     161              :     nodeX: node.pos[0],
     162              :     nodeY: node.pos[1],
     163              :     nodeWidth: node.size[0],
     164              :     nodeHeight: node.size[1],
     165              :     collapsed: node.flags.collapsed || false,
     166              :     collapsedWidth: node._collapsed_width,
     167              :     slotStartY: node.constructor.slot_start_y,
     168              :     inputs: node.inputs,
     169              :     outputs: node.outputs,
     170              :     widgets: node.widgets
     171              :   }
     172              : 
     173       866476 :   return isInput
     174              :     ? calculateInputSlotPos(context, slotIndex)
     175              :     : calculateOutputSlotPos(context, slotIndex)
     176              : }
     177              : 
     178       314135 : function getVueSlotPosition(
     179              :   node: LGraphNode,
     180              :   slotIndex: number,
     181              :   isInput: boolean
     182              : ): Point {
     183       314135 :   const [nodeX, nodeY] = node.pos
     184       314135 :   const offset = getRenderedSlotOffset(node, slotIndex, isInput)
     185       314135 :   if (offset) return [nodeX + offset.x, nodeY + offset.y]
     186              : 
     187              :   const nodeWidth = node.flags.collapsed
     188              :     ? (node._collapsed_width ?? litegraph().NODE_COLLAPSED_WIDTH)
     189              :     : node.renderingSize[0]
     190       314135 :   return calculateVueSlotPosition(
     191              :     node,
     192              :     slotIndex,
     193              :     isInput,
     194              :     nodeX,
     195              :     nodeY,
     196              :     nodeWidth
     197              :   )
     198              : }
     199              : 
     200       314135 : function getRenderedSlotOffset(
     201              :   node: LGraphNode,
     202              :   slotIndex: number,
     203              :   isInput: boolean
     204              : ): LayoutPoint | null {
     205       314135 :   if (!node.graph) return null
     206       314135 :   return layoutStore.getSlotOffset(
     207              :     node.graph.rootGraph.id,
     208              :     node.id,
     209              :     slotIndex,
     210              :     isInput ? 'input' : 'output',
     211              :     node.flags.collapsed ? 'collapsed' : 'expanded'
     212              :   )
     213              : }
     214              : 
     215        77840 : function calculateVueSlotPosition(
     216              :   node: LGraphNode,
     217              :   slotIndex: number,
     218              :   isInput: boolean,
     219              :   nodeX: number,
     220              :   nodeY: number,
     221              :   nodeWidth: number
     222              : ): Point {
     223        77840 :   if (node.flags.collapsed) {
     224            5 :     return [
     225              :       isInput ? nodeX : nodeX + nodeWidth,
     226              :       nodeY - litegraph().NODE_TITLE_HEIGHT * 0.5
     227              :     ]
     228              :   }
     229              : 
     230        77840 :   return isInput
     231              :     ? calculateVueInputSlotPosition(node, slotIndex, nodeX, nodeY)
     232              :     : calculateVueOutputSlotPosition(node, slotIndex, nodeX, nodeY, nodeWidth)
     233              : }
     234              : 
     235        52526 : function calculateVueInputSlotPosition(
     236              :   node: LGraphNode,
     237              :   slotIndex: number,
     238              :   nodeX: number,
     239              :   nodeY: number
     240              : ): Point {
     241        52526 :   const input = node.inputs.at(slotIndex)
     242        52526 :   if (!input) return [nodeX, nodeY]
     243              : 
     244        52526 :   const widgetSlotY = getWidgetSlotY(node, input)
     245        52526 :   if (widgetSlotY !== undefined) {
     246        27295 :     return [nodeX, getVueNodeContentY(node, nodeY) + widgetSlotY]
     247              :   }
     248              : 
     249        25231 :   const renderedIndex = node.inputs
     250              :     .slice(0, slotIndex)
     251              :     .filter((candidate) => !isWidgetInputSlot(candidate)).length
     252        25231 :   return [
     253              :     nodeX,
     254              :     getVueNodeContentY(node, nodeY) + getVueSlotY(node, renderedIndex)
     255              :   ]
     256              : }
     257              : 
     258        52526 : function getWidgetSlotY(
     259              :   node: LGraphNode,
     260              :   input: INodeInputSlot
     261              : ): number | undefined {
     262        52526 :   if (!isWidgetInputSlot(input)) return undefined
     263        27627 :   if (input.pos) return input.pos[1]
     264              : 
     265        52526 :   const widget =
     266              :     input._widget ??
     267              :     node.widgets?.find((candidate) => candidate.name === input.widget.name)
     268        52526 :   return widget && widget.y + litegraph().NODE_SLOT_HEIGHT * 0.5
     269              : }
     270              : 
     271        25309 : function calculateVueOutputSlotPosition(
     272              :   node: LGraphNode,
     273              :   slotIndex: number,
     274              :   nodeX: number,
     275              :   nodeY: number,
     276              :   nodeWidth: number
     277              : ): Point {
     278        25309 :   if (!node.outputs.at(slotIndex)) {
     279            0 :     return [nodeX + nodeWidth, nodeY]
     280              :   }
     281              : 
     282        25309 :   return [
     283              :     nodeX + nodeWidth,
     284              :     getVueNodeContentY(node, nodeY) + getVueSlotY(node, slotIndex)
     285              :   ]
     286              : }
     287              : 
     288        50540 : function getVueSlotY(node: LGraphNode, slotIndex: number): number {
     289        50540 :   return (
     290              :     (slotIndex + (node.type === 'Reroute' ? 0.5 : 0.7)) *
     291              :     litegraph().NODE_SLOT_HEIGHT
     292              :   )
     293              : }
     294              : 
     295        77835 : function getVueNodeContentY(node: LGraphNode, nodeY: number): number {
     296        77835 :   return node.title_mode === TitleMode.NO_TITLE
     297              :     ? nodeY - litegraph().NODE_TITLE_HEIGHT
     298              :     : nodeY
     299              : }
     300              : 
     301         2414 : export function getSlotLayout(
     302              :   node: LGraphNode,
     303              :   slotIndex: number,
     304              :   isInput: boolean
     305              : ): SlotLayout | null {
     306              :   const slot = isInput ? node.inputs.at(slotIndex) : node.outputs.at(slotIndex)
     307         2414 :   if (!slot) return null
     308              : 
     309         2414 :   return createSlotLayout(
     310              :     node,
     311              :     slotIndex,
     312              :     isInput,
     313              :     getSlotPosition(node, slotIndex, isInput)
     314              :   )
     315              : }
     316              : 
     317         2414 : function createSlotLayout(
     318              :   node: LGraphNode,
     319              :   slotIndex: number,
     320              :   isInput: boolean,
     321              :   [x, y]: Point
     322              : ): SlotLayout {
     323         2414 :   const size = litegraph().NODE_SLOT_HEIGHT
     324         2414 :   const half = size * 0.5
     325         2414 :   return {
     326              :     nodeId: node.id,
     327              :     index: slotIndex,
     328              :     type: isInput ? 'input' : 'output',
     329              :     position: { x, y },
     330              :     bounds: { x: x - half, y: y - half, width: size, height: size }
     331              :   }
     332              : }
     333              : 
     334          733 : export function getGraphSlotLayout(
     335              :   graph: LGraph,
     336              :   nodeId: LGraphNode['id'],
     337              :   slotIndex: number,
     338              :   isInput: boolean
     339              : ): SlotLayout | null {
     340          733 :   const node = graph.getNodeById(nodeId)
     341          733 :   return node ? getSlotLayout(node, slotIndex, isInput) : null
     342              : }
     343              : 
     344          180 : export function getSlotLayoutAtPoint(
     345              :   graph: LGraph,
     346              :   point: LayoutPoint,
     347              :   node?: LGraphNode
     348              : ): SlotLayout | null {
     349          180 :   if (node) return getNodeSlotLayoutAtPoint(node, point)
     350          166 :   const nodes = nodesInRenderOrder(graph)
     351          175 :   for (let index = nodes.length - 1; index >= 0; index--) {
     352          383 :     const layout = getNodeSlotLayoutAtPoint(nodes[index], point)
     353          383 :     if (layout) return layout
     354              :   }
     355          166 :   return null
     356              : }
     357              : 
     358          397 : function getNodeSlotLayoutAtPoint(
     359              :   node: LGraphNode,
     360              :   point: LayoutPoint
     361              : ): SlotLayout | null {
     362          397 :   for (const [index] of node.inputs.entries()) {
     363         1213 :     const layout = getSlotLayout(node, index, true)
     364         1213 :     if (layout && pointInBounds(point, layout.bounds)) return layout
     365              :   }
     366          395 :   for (const [index] of node.outputs.entries()) {
     367          485 :     const layout = getSlotLayout(node, index, false)
     368          485 :     if (layout && pointInBounds(point, layout.bounds)) return layout
     369              :   }
     370          395 :   return null
     371              : }
     372              : 
     373              : /**
     374              :  * Get the outputs that are not positioned with absolute coordinates
     375              :  */
     376       193146 : function getDefaultVerticalOutputs(
     377              :   context: SlotPositionContext
     378              : ): INodeOutputSlot[] {
     379       193146 :   return context.outputs.filter((slot) => !slot.pos)
     380              : }
        

Generated by: LCOV version 2.0-1