LCOV - code coverage report
Current view: top level - src/renderer/extensions/firstRunTour/tour - cameraFraming.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 12.5 % 40 5
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   Size2D as Viewport,
       3              :   ReadOnlyRect
       4              : } from '@comfyorg/shared-frontend-utils/geometry'
       5              : import { delay } from 'es-toolkit'
       6              : 
       7              : import {
       8              :   CARD_GLIDE_MS,
       9              :   CARD_WIDTH,
      10              :   CURSOR_GAP,
      11              :   topSafeInset
      12              : } from '@/platform/onboarding/coachmarkLayout'
      13              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
      14              : import type { NodeId } from '@comfyorg/graph-model/nodeId'
      15              : 
      16         2355 : const FOCUS_DURATION_MS = 450
      17              : /** Never magnify past this: the aim is a node that reads, not one that dominates. */
      18         2355 : export const MAX_FOCUS_SCALE = 0.65
      19         2355 : const CARD_COLUMN = CARD_WIDTH + CURSOR_GAP * 2
      20              : 
      21              : /**
      22              :  * Fill fraction that frames `bounds` beside the card. The fit centres the node
      23              :  * and so splits the free space evenly, hence room for a card column on each
      24              :  * side, and for the top bar on each end — the canvas runs the full height of
      25              :  * the window, underneath that bar, so a node fitted to the whole of it hides
      26              :  * its own title behind the workflow tabs. Inverts litegraph's fit, which
      27              :  * solves each axis as `(fill * side) / max(bound, 300)` and takes the smaller,
      28              :  * so the binding axis has to be handed the larger fill.
      29              :  */
      30            0 : export function focusFill(bounds: ReadOnlyRect, viewport: Viewport): number {
      31            0 :   const [, , width, height] = bounds
      32            0 :   const sideRoom = viewport.width - CARD_COLUMN * 2
      33            0 :   const usableWidth = sideRoom > 0 ? sideRoom : viewport.width
      34            0 :   const endRoom = viewport.height - topSafeInset() * 2
      35            0 :   const usableHeight = endRoom > 0 ? endRoom : viewport.height
      36            0 :   const scale = Math.min(
      37              :     usableWidth / Math.max(width, 1),
      38              :     usableHeight / Math.max(height, 1),
      39              :     MAX_FOCUS_SCALE
      40              :   )
      41            0 :   return Math.max(
      42              :     (scale * Math.max(width, 300)) / viewport.width,
      43              :     (scale * Math.max(height, 300)) / viewport.height
      44              :   )
      45              : }
      46              : 
      47            0 : function prefersReducedMotion(): boolean {
      48            0 :   return window.matchMedia('(prefers-reduced-motion: reduce)').matches
      49              : }
      50              : 
      51              : /** Re-solves the fill each time: it is a fraction of a viewport that can resize. */
      52            0 : function fitInstantly(bounds: ReadOnlyRect) {
      53            0 :   const canvas = useCanvasStore().canvas
      54            0 :   const viewport = canvas?.canvas.getBoundingClientRect()
      55            0 :   if (!canvas || !viewport?.width || !viewport.height) return
      56            0 :   canvas.ds.fitToBounds(bounds, { zoom: focusFill(bounds, viewport) })
      57            0 :   canvas.setDirty(true, true)
      58              : }
      59              : 
      60         2355 : const SETTLED_PX = 8
      61         2355 : const SETTLED_SCALE = 0.01
      62              : 
      63              : /** The scale litegraph's fit lands on: it takes whichever axis binds first. */
      64            0 : function framedScale(bounds: ReadOnlyRect, viewport: Viewport): number {
      65            0 :   const zoom = focusFill(bounds, viewport)
      66            0 :   return Math.min(
      67              :     (zoom * viewport.width) / Math.max(bounds[2], 300),
      68              :     (zoom * viewport.height) / Math.max(bounds[3], 300)
      69              :   )
      70              : }
      71              : 
      72              : /** Back onto a step already framed would otherwise animate 0px for 750ms. */
      73            0 : function alreadyFramed(
      74              :   bounds: ReadOnlyRect,
      75              :   ds: { offset: ArrayLike<number>; scale: number },
      76              :   viewport: Viewport
      77              : ): boolean {
      78            0 :   const centreX = (bounds[0] + bounds[2] / 2 + ds.offset[0]) * ds.scale
      79            0 :   const centreY = (bounds[1] + bounds[3] / 2 + ds.offset[1]) * ds.scale
      80            0 :   return (
      81              :     Math.abs(ds.scale - framedScale(bounds, viewport)) <= SETTLED_SCALE &&
      82              :     Math.abs(centreX - viewport.width / 2) < SETTLED_PX &&
      83              :     Math.abs(centreY - viewport.height / 2) < SETTLED_PX
      84              :   )
      85              : }
      86              : 
      87              : /**
      88              :  * Brings a node into view for the step that spotlights it. Resolves once the
      89              :  * camera has landed, so an opening tour reveals its card on a still view.
      90              :  *
      91              :  * @param glide Wait out the card's travel to this step first, so only one thing
      92              :  * moves at a time. The tour's first card has nowhere to travel from.
      93              :  */
      94            0 : export async function frameNode(
      95              :   nodeId: NodeId,
      96              :   signal: AbortSignal,
      97              :   { glide = true }: { glide?: boolean } = {}
      98              : ): Promise<void> {
      99            0 :   const canvas = useCanvasStore().canvas
     100            0 :   const node = canvas?.graph?.getNodeById(nodeId)
     101            0 :   const viewport = canvas?.canvas.getBoundingClientRect()
     102            0 :   if (!canvas || !node || !viewport?.width || !viewport.height) return
     103              : 
     104            0 :   const bounds = node.boundingRect
     105              : 
     106            0 :   window.addEventListener('resize', () => fitInstantly(bounds), { signal })
     107              : 
     108            0 :   if (prefersReducedMotion()) return fitInstantly(bounds)
     109            0 :   if (alreadyFramed(bounds, canvas.ds, viewport)) return
     110              : 
     111            0 :   if (glide) await delay(CARD_GLIDE_MS, { signal })
     112            0 :   canvas.animateToBounds(bounds, {
     113              :     zoom: focusFill(bounds, viewport),
     114              :     duration: FOCUS_DURATION_MS
     115              :   })
     116            0 :   await delay(FOCUS_DURATION_MS, { signal })
     117              : }
        

Generated by: LCOV version 2.0-1