LCOV - code coverage report
Current view: top level - src/renderer/extensions/minimap/composables - useMinimapViewport.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 98.2 % 55 54
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useRafFn } from '@vueuse/core'
       2              : import { computed, ref } from 'vue'
       3              : import type { Ref } from 'vue'
       4              : 
       5              : import type { LGraph } from '@/lib/litegraph/src/litegraph'
       6              : import {
       7              :   calculateMinimapScale,
       8              :   enforceMinimumBounds
       9              : } from '@/renderer/core/spatial/boundsCalculator'
      10              : import { MinimapDataSource } from '@/renderer/extensions/minimap/data/MinimapDataSource'
      11              : 
      12              : import type {
      13              :   SizedExtents as MinimapBounds,
      14              :   Bounds as ViewportTransform
      15              : } from '@comfyorg/shared-frontend-utils/geometry'
      16              : import type { MinimapCanvas } from '@/renderer/extensions/minimap/types'
      17              : 
      18         2102 : export function useMinimapViewport(
      19              :   canvas: Ref<MinimapCanvas | null>,
      20              :   graph: Ref<LGraph | null>,
      21              :   width: number,
      22              :   height: number
      23              : ) {
      24         2102 :   const bounds = ref<MinimapBounds>({
      25              :     minX: 0,
      26              :     minY: 0,
      27              :     maxX: 0,
      28              :     maxY: 0,
      29              :     width: 0,
      30              :     height: 0
      31              :   })
      32              : 
      33         2102 :   const scale = ref(1)
      34         2102 :   const viewportTransform = ref<ViewportTransform>({
      35              :     x: 0,
      36              :     y: 0,
      37              :     width: 0,
      38              :     height: 0
      39              :   })
      40              : 
      41         2102 :   const canvasDimensions = ref({
      42              :     width: 0,
      43              :     height: 0
      44              :   })
      45              : 
      46         2102 :   const updateCanvasDimensions = () => {
      47         2196 :     const c = canvas.value
      48         2196 :     if (!c) return
      49              : 
      50         2196 :     const canvasEl = c.canvas
      51         2196 :     const dpr = c.dpr
      52              : 
      53         2196 :     canvasDimensions.value = {
      54              :       width: canvasEl.clientWidth || canvasEl.width / dpr,
      55              :       height: canvasEl.clientHeight || canvasEl.height / dpr
      56              :     }
      57              :   }
      58              : 
      59         2102 :   const calculateGraphBounds = (): MinimapBounds => {
      60        10144 :     const dataSource = new MinimapDataSource(graph.value)
      61              : 
      62        10144 :     if (!dataSource.hasData()) {
      63         2362 :       return { minX: 0, minY: 0, maxX: 100, maxY: 100, width: 100, height: 100 }
      64              :     }
      65              : 
      66         7782 :     const sourceBounds = dataSource.getBounds()
      67         7782 :     return enforceMinimumBounds(sourceBounds)
      68              :   }
      69              : 
      70         2102 :   const calculateScale = () => {
      71        10144 :     return calculateMinimapScale(bounds.value, width, height)
      72              :   }
      73              : 
      74         2102 :   const updateViewport = () => {
      75       114719 :     const c = canvas.value
      76       114719 :     if (!c) return
      77              : 
      78       114719 :     if (
      79              :       canvasDimensions.value.width === 0 ||
      80              :       canvasDimensions.value.height === 0
      81              :     ) {
      82           26 :       updateCanvasDimensions()
      83              :     }
      84              : 
      85       114719 :     const ds = c.ds
      86              : 
      87       114719 :     const viewportWidth = canvasDimensions.value.width / ds.scale
      88       114719 :     const viewportHeight = canvasDimensions.value.height / ds.scale
      89              : 
      90       114719 :     const worldX = -ds.offset[0]
      91       114719 :     const worldY = -ds.offset[1]
      92              : 
      93       114719 :     const centerOffsetX = (width - bounds.value.width * scale.value) / 2
      94       114719 :     const centerOffsetY = (height - bounds.value.height * scale.value) / 2
      95              : 
      96       114719 :     const x = (worldX - bounds.value.minX) * scale.value + centerOffsetX
      97       114719 :     const y = (worldY - bounds.value.minY) * scale.value + centerOffsetY
      98       114719 :     const w = viewportWidth * scale.value
      99       114719 :     const h = viewportHeight * scale.value
     100              : 
     101       114719 :     const curr = viewportTransform.value
     102       114719 :     if (curr.x === x && curr.y === y && curr.width === w && curr.height === h)
     103       104573 :       return
     104              : 
     105        10146 :     viewportTransform.value = { x, y, width: w, height: h }
     106              :   }
     107              : 
     108         2102 :   const updateBounds = () => {
     109        10144 :     bounds.value = calculateGraphBounds()
     110        10144 :     scale.value = calculateScale()
     111              :   }
     112              : 
     113         2102 :   const centerViewOn = (worldX: number, worldY: number) => {
     114            8 :     const c = canvas.value
     115            8 :     if (!c) return
     116              : 
     117            8 :     if (
     118              :       canvasDimensions.value.width === 0 ||
     119              :       canvasDimensions.value.height === 0
     120              :     ) {
     121            0 :       updateCanvasDimensions()
     122              :     }
     123              : 
     124            8 :     const ds = c.ds
     125              : 
     126            8 :     const viewportWidth = canvasDimensions.value.width / ds.scale
     127            8 :     const viewportHeight = canvasDimensions.value.height / ds.scale
     128              : 
     129            8 :     ds.offset[0] = -(worldX - viewportWidth / 2)
     130            8 :     ds.offset[1] = -(worldY - viewportHeight / 2)
     131              : 
     132            8 :     c.setDirty(true, true)
     133              :   }
     134         2102 :   const { resume: startViewportSync, pause: stopViewportSync } =
     135              :     useRafFn(updateViewport)
     136              : 
     137         2102 :   return {
     138              :     bounds: computed(() => bounds.value),
     139              :     scale: computed(() => scale.value),
     140              :     viewportTransform: computed(() => viewportTransform.value),
     141              :     canvasDimensions: computed(() => canvasDimensions.value),
     142              :     updateCanvasDimensions,
     143              :     updateViewport,
     144              :     updateBounds,
     145              :     centerViewOn,
     146              :     startViewportSync,
     147              :     stopViewportSync
     148              :   }
     149              : }
        

Generated by: LCOV version 2.0-1