LCOV - code coverage report
Current view: top level - src/renderer/core/canvas - canvasStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 78.2 % 87 68
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useEventListener, whenever } from '@vueuse/core'
       2              : import { defineStore } from 'pinia'
       3              : import { computed, ref, shallowRef } from 'vue'
       4              : 
       5              : import { useToast } from '@/components/ui/toast/toastStore'
       6              : import { useAppMode } from '@/composables/useAppMode'
       7              : import { visibleCanvasViewport } from '@/composables/canvas/visibleCanvasViewport'
       8              : 
       9              : import type { PointTuple as Point } from '@comfyorg/shared-frontend-utils/geometry'
      10              : import type { Positionable } from '@comfyorg/graph-model/interfaces'
      11              : import type {
      12              :   LGraph,
      13              :   LGraphCanvas,
      14              :   LGraphGroup,
      15              :   LGraphNode,
      16              :   SubgraphNode
      17              : } from '@/lib/litegraph/src/litegraph'
      18              : import { resolveSelectable } from '@/renderer/core/canvas/litegraph/selectionAdapter'
      19              : import { useSelectionStore } from '@/core/selection/selectionStore'
      20              : import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
      21              : import { LayoutSource } from '@/renderer/core/layout/types'
      22              : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
      23              : import type { NodeId } from '@comfyorg/graph-model/nodeId'
      24              : import { isLGraphNode } from '@/utils/litegraphUtil'
      25              : import { createPositionBounds } from '@/utils/positionBounds'
      26              : 
      27         2356 : let promoteRecommendedWidgets: (subgraphNode: SubgraphNode) => void
      28              : 
      29              : /** Called by `promotionUtils` when it loads; it imports this store. */
      30         2356 : export function providePromoteRecommendedWidgets(
      31              :   promote: (subgraphNode: SubgraphNode) => void
      32              : ): void {
      33         2356 :   promoteRecommendedWidgets = promote
      34              : }
      35              : 
      36         2356 : export const useTitleEditorStore = defineStore('titleEditor', () => {
      37              :   const titleEditorTarget = shallowRef<LGraphNode | LGraphGroup | null>(null)
      38              : 
      39         2355 :   return {
      40              :     titleEditorTarget
      41              :   }
      42              : })
      43              : 
      44         2356 : export const useCanvasStore = defineStore('canvas', () => {
      45              :   /**
      46              :    * The LGraphCanvas instance.
      47              :    *
      48              :    * The root LGraphCanvas object is a shallow ref.
      49              :    */
      50         2355 :   const canvas = shallowRef<LGraphCanvas | null>(null)
      51         2355 :   const selectionStore = useSelectionStore()
      52              : 
      53              :   // Reactive scale percentage that syncs with app.canvas.ds.scale
      54         2355 :   const appScalePercentage = ref(100)
      55         2355 :   const updateAppScalePercentage = (scale: number) => {
      56          119 :     appScalePercentage.value = Math.round(scale * 100)
      57              :   }
      58              : 
      59         2355 :   const { isAppMode, setMode } = useAppMode()
      60         2355 :   const linearMode = computed({
      61              :     get: () => isAppMode.value,
      62              :     set: (val: boolean) => {
      63          155 :       setMode(val ? 'app' : 'graph')
      64              :     }
      65              :   })
      66         2355 :   const isReadOnly = ref(false)
      67              : 
      68              :   // Set up scale synchronization when canvas is available
      69         2355 :   let originalOnChanged: ((scale: number, offset: Point) => void) | undefined =
      70              :     undefined
      71         2355 :   const initScaleSync = () => {
      72           68 :     const ds = canvas.value?.ds
      73           68 :     if (!ds) return
      74              : 
      75           68 :     originalOnChanged = ds.onChanged
      76           68 :     updateAppScalePercentage(ds.scale)
      77              : 
      78           68 :     ds.onChanged = () => {
      79           51 :       if (ds.scale) {
      80           51 :         updateAppScalePercentage(ds.scale)
      81              :       }
      82           51 :       originalOnChanged?.(ds.scale, ds.offset)
      83              :     }
      84              :   }
      85              : 
      86         2355 :   const cleanupScaleSync = () => {
      87            1 :     const ds = canvas.value?.ds
      88            1 :     if (!ds) return
      89            1 :     ds.onChanged = originalOnChanged
      90            1 :     originalOnChanged = undefined
      91              :   }
      92              : 
      93         2355 :   const getCanvas = () => {
      94        22596 :     if (!canvas.value) throw new Error('getCanvas: canvas is null')
      95        22596 :     return canvas.value
      96              :   }
      97              : 
      98              :   /**
      99              :    * Sets the canvas zoom level from a percentage value
     100              :    * @param percentage - Zoom percentage value (1-1000, where 1000 = 1000% zoom)
     101              :    */
     102         2355 :   const setAppZoomFromPercentage = (percentage: number) => {
     103            0 :     const currentCanvas = canvas.value
     104            0 :     if (!currentCanvas || percentage <= 0) return
     105              : 
     106              :     // Convert percentage to scale (1000% = 10.0 scale)
     107            0 :     const newScale = percentage / 100
     108            0 :     const { ds } = currentCanvas
     109            0 :     const { element } = ds
     110              : 
     111            0 :     ds.changeScale(newScale, [element.width / 2, element.height / 2])
     112            0 :     currentCanvas.setDirty(true, true)
     113              : 
     114              :     // Update reactive value immediately for UI consistency
     115            0 :     updateAppScalePercentage(newScale)
     116              :   }
     117              : 
     118         2355 :   const currentGraph = shallowRef<LGraph | null>(null)
     119         2355 :   const rootGraphId = computed(() => currentGraph.value?.rootGraph.id)
     120         2355 :   const isInSubgraph = ref(false)
     121         2355 :   const isGhostPlacing = ref(false)
     122         2355 :   const isPickingNodes = ref(false)
     123         2355 :   const toast = useToast()
     124              : 
     125            0 :   function startNodePicking(): void {
     126            0 :     const currentCanvas = canvas.value
     127            0 :     if (!currentCanvas || isPickingNodes.value) return
     128            0 :     isPickingNodes.value = true
     129            0 :     toast.held = true
     130            0 :     const selected = [...currentCanvas.selectedItems]
     131            0 :     const bounds = createPositionBounds(
     132              :       selected.length ? selected : (currentCanvas.graph?.nodes ?? []),
     133              :       40
     134              :     )
     135            0 :     if (bounds) {
     136            0 :       currentCanvas.animateToBounds(bounds, {
     137              :         viewport: visibleCanvasViewport(currentCanvas)
     138              :       })
     139              :     }
     140              :   }
     141              : 
     142         6347 :   function stopNodePicking(): undefined {
     143         6347 :     if (!isPickingNodes.value) return
     144            0 :     isPickingNodes.value = false
     145            0 :     toast.held = false
     146         6347 :     canvas.value?.deselectAll()
     147              :   }
     148              : 
     149              :   /** The selected items of the on-screen graph, derived from the selection store. */
     150         2355 :   const selectedItems = computed<Positionable[]>(() => {
     151        13918 :     const graph = currentGraph.value
     152        13918 :     if (!graph) return []
     153        11563 :     return selectionStore
     154              :       .selectedKeys(graphScopeOf(graph))
     155              :       .flatMap((key) => resolveSelectable(graph, key) ?? [])
     156              :   })
     157              : 
     158         2355 :   const selectedNodeIds = computed<Set<NodeId>>(
     159              :     () =>
     160              :       new Set(selectedItems.value.filter(isLGraphNode).map((item) => item.id))
     161              :   )
     162              : 
     163         2355 :   whenever(
     164              :     () => canvas.value,
     165              :     (newCanvas) => {
     166         2355 :       currentGraph.value = newCanvas.graph
     167              :       // Scoped to the on-screen graph: selection only holds items from it,
     168              :       // so removals in other graphs can't affect the live selection.
     169         2355 :       useEventListener(
     170              :         () => currentGraph.value?.events,
     171              :         'node:before-removed',
     172              :         (e: CustomEvent<{ node: LGraphNode }>) => {
     173        10088 :           newCanvas.deselect(e.detail.node, { notify: false })
     174              :         }
     175              :       )
     176              : 
     177         2355 :       isReadOnly.value = newCanvas.read_only
     178              : 
     179         2355 :       useEventListener(
     180              :         newCanvas.canvas,
     181              :         'litegraph:read-only-changed',
     182              :         (event: CustomEvent<{ readOnly: boolean }>) => {
     183          126 :           isReadOnly.value = event.detail.readOnly
     184              :         }
     185              :       )
     186              : 
     187         2355 :       useEventListener(
     188              :         newCanvas.canvas,
     189              :         'litegraph:set-graph',
     190              :         (event: CustomEvent<{ newGraph?: LGraph; oldGraph: LGraph }>) => {
     191          216 :           const newGraph = event.detail.newGraph ?? newCanvas.graph // TODO: Ambiguous Graph
     192          216 :           currentGraph.value = newGraph
     193          216 :           isInSubgraph.value = Boolean(newCanvas.subgraph)
     194              :         }
     195              :       )
     196              : 
     197         2355 :       useEventListener(newCanvas.canvas, 'subgraph-opened', () => {
     198          126 :         isInSubgraph.value = true
     199              :       })
     200              : 
     201         2355 :       useEventListener(
     202              :         newCanvas.canvas,
     203              :         'subgraph-converted',
     204              :         (e: CustomEvent<{ subgraphNode: SubgraphNode }>) =>
     205              :           promoteRecommendedWidgets(e.detail.subgraphNode)
     206              :       )
     207              : 
     208         2355 :       useEventListener(
     209              :         newCanvas.canvas,
     210              :         'litegraph:ghost-placement',
     211              :         (e: CustomEvent<{ active: boolean; nodeId: NodeId }>) => {
     212          154 :           isGhostPlacing.value = e.detail.active
     213          154 :           const graph = currentGraph.value
     214          154 :           if (e.detail.active && graph) {
     215              :             const mutations = useLayoutMutations(LayoutSource.Canvas)
     216           81 :             mutations.setNodeOrder(graph, e.detail.nodeId, 'front')
     217              :           }
     218              :         }
     219              :       )
     220              :     },
     221              :     { immediate: true }
     222              :   )
     223              : 
     224         2355 :   return {
     225              :     canvas,
     226              :     selectedItems,
     227              :     selectedNodeIds,
     228              :     appScalePercentage,
     229              :     linearMode,
     230              :     isReadOnly,
     231              :     getCanvas,
     232              :     setAppZoomFromPercentage,
     233              :     initScaleSync,
     234              :     cleanupScaleSync,
     235              :     currentGraph,
     236              :     rootGraphId,
     237              :     isInSubgraph,
     238              :     isGhostPlacing,
     239              :     isPickingNodes,
     240              :     startNodePicking,
     241              :     stopNodePicking
     242              :   }
     243              : })
        

Generated by: LCOV version 2.0-1