LCOV - code coverage report
Current view: top level - packages/mask-editor/src - maskEditorStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 75.2 % 125 94
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, reactive, ref, watch, shallowRef } from 'vue'
       3              : import { clamp } from 'es-toolkit/compat'
       4              : import type { TgpuRoot } from 'typegpu'
       5              : 
       6              : import {
       7              :   BrushShape,
       8              :   ColorComparisonMethod,
       9              :   MaskBlendMode,
      10              :   Tools
      11              : } from './types'
      12              : import type { Brush, ImageLayer } from './types'
      13              : import type {
      14              :   Point2D as Offset,
      15              :   Point2D as Point
      16              : } from '@comfyorg/shared-frontend-utils/geometry'
      17              : import { useCanvasHistory } from './useCanvasHistory'
      18              : 
      19         2355 : export const useMaskEditorStore = defineStore('maskEditor', () => {
      20         2355 :   const brushSettings = ref<Brush>({
      21              :     type: BrushShape.Arc,
      22              :     size: 10,
      23              :     opacity: 0.7,
      24              :     hardness: 1,
      25              :     stepSize: 10
      26              :   })
      27              : 
      28         2355 :   const maskBlendMode = ref<MaskBlendMode>(MaskBlendMode.Black)
      29         2355 :   const activeLayer = ref<ImageLayer>('mask')
      30         2355 :   const rgbColor = ref<string>('#FF0000')
      31              : 
      32         2355 :   const currentTool = ref<Tools>(Tools.MaskPen)
      33         2355 :   const isAdjustingBrush = ref<boolean>(false)
      34              : 
      35         2355 :   const paintBucketTolerance = ref<number>(5)
      36         2355 :   const fillOpacity = ref<number>(100)
      37              : 
      38         2355 :   const colorSelectTolerance = ref<number>(20)
      39         2355 :   const colorSelectLivePreview = ref<boolean>(false)
      40         2355 :   const colorComparisonMethod = ref<ColorComparisonMethod>(
      41              :     ColorComparisonMethod.Simple
      42              :   )
      43         2355 :   const applyWholeImage = ref<boolean>(false)
      44         2355 :   const maskBoundary = ref<boolean>(false)
      45         2355 :   const maskTolerance = ref<number>(0)
      46         2355 :   const selectionOpacity = ref<number>(100)
      47              : 
      48         2355 :   const zoomRatio = ref<number>(1)
      49         2355 :   const displayZoomRatio = ref<number>(1)
      50         2355 :   const panOffset = ref<Offset>({ x: 0, y: 0 })
      51         2355 :   const cursorPoint = ref<Point>({ x: 0, y: 0 })
      52         2355 :   const resetZoomTrigger = ref<number>(0)
      53         2355 :   const clearTrigger = ref<number>(0)
      54              : 
      55         2355 :   const maskCanvas = ref<HTMLCanvasElement | null>(null)
      56         2355 :   const maskCtx = ref<CanvasRenderingContext2D | null>(null)
      57         2355 :   const rgbCanvas = ref<HTMLCanvasElement | null>(null)
      58         2355 :   const rgbCtx = ref<CanvasRenderingContext2D | null>(null)
      59         2355 :   const imgCanvas = ref<HTMLCanvasElement | null>(null)
      60         2355 :   const imgCtx = ref<CanvasRenderingContext2D | null>(null)
      61         2355 :   const canvasContainer = ref<HTMLElement | null>(null)
      62         2355 :   const canvasBackground = ref<HTMLElement | null>(null)
      63         2355 :   const pointerZone = ref<HTMLElement | null>(null)
      64         2355 :   const image = ref<HTMLImageElement | null>(null)
      65              : 
      66         2355 :   const maskOpacity = ref<number>(0.8)
      67              : 
      68         2355 :   const brushVisible = ref<boolean>(true)
      69         2355 :   const isPanning = ref<boolean>(false)
      70         2355 :   const brushPreviewGradientVisible = ref<boolean>(false)
      71              : 
      72         2355 :   const canvasHistory = useCanvasHistory(
      73              :     reactive({ maskCanvas, maskCtx, rgbCanvas, rgbCtx, imgCanvas, imgCtx }),
      74              :     20
      75              :   )
      76              : 
      77         2355 :   const tgpuRoot = shallowRef<TgpuRoot | null>(null)
      78              : 
      79         2355 :   const colorInput = ref<HTMLInputElement | null>(null)
      80              : 
      81              :   // GPU texture recreation signals
      82              :   /**
      83              :    * GPU texture data must use ArrayBuffer (not SharedArrayBuffer) for compatibility
      84              :    * with WebGPU's device.queue.writeTexture API. SharedArrayBuffer is not accepted
      85              :    * by the WebGPU specification and will cause runtime errors.
      86              :    *
      87              :    * @see https://gpuweb.github.io/gpuweb/#dom-gpuqueue-writetexture
      88              :    */
      89              :   type GPUCompatibleArray = Uint8ClampedArray & { buffer: ArrayBuffer }
      90         2355 :   const gpuTexturesNeedRecreation = ref<boolean>(false)
      91         2355 :   const gpuTextureWidth = ref<number>(0)
      92         2355 :   const gpuTextureHeight = ref<number>(0)
      93         2355 :   const pendingGPUMaskData = ref<GPUCompatibleArray | null>(null)
      94         2355 :   const pendingGPURgbData = ref<GPUCompatibleArray | null>(null)
      95              : 
      96         2355 :   watch(maskCanvas, (canvas) => {
      97           29 :     if (canvas) {
      98           29 :       maskCtx.value = canvas.getContext('2d', { willReadFrequently: true })
      99              :     }
     100              :   })
     101              : 
     102         2355 :   watch(rgbCanvas, (canvas) => {
     103           29 :     if (canvas) {
     104           29 :       rgbCtx.value = canvas.getContext('2d', { willReadFrequently: true })
     105              :     }
     106              :   })
     107              : 
     108         2355 :   watch(imgCanvas, (canvas) => {
     109           29 :     if (canvas) {
     110           29 :       imgCtx.value = canvas.getContext('2d', { willReadFrequently: true })
     111              :     }
     112              :   })
     113              : 
     114         2355 :   const canUndo = computed(() => {
     115            0 :     return canvasHistory.canUndo.value
     116              :   })
     117              : 
     118         2355 :   const canRedo = computed(() => {
     119            0 :     return canvasHistory.canRedo.value
     120              :   })
     121              : 
     122         2355 :   const maskColor = computed(() => {
     123           28 :     switch (maskBlendMode.value) {
     124              :       case MaskBlendMode.Black:
     125           28 :         return { r: 0, g: 0, b: 0 }
     126              :       case MaskBlendMode.White:
     127            0 :         return { r: 255, g: 255, b: 255 }
     128              :       case MaskBlendMode.Negative:
     129            0 :         return { r: 255, g: 255, b: 255 }
     130              :       default:
     131            0 :         return { r: 0, g: 0, b: 0 }
     132              :     }
     133              :   })
     134              : 
     135            3 :   function setBrushSize(size: number): void {
     136            3 :     brushSettings.value.size = clamp(size, 1, 250)
     137              :   }
     138              : 
     139            0 :   function setBrushOpacity(opacity: number): void {
     140            0 :     brushSettings.value.opacity = clamp(opacity, 0, 1)
     141              :   }
     142              : 
     143            0 :   function setBrushHardness(hardness: number): void {
     144            0 :     brushSettings.value.hardness = clamp(hardness, 0, 1)
     145              :   }
     146              : 
     147            0 :   function setBrushStepSize(step: number): void {
     148            0 :     brushSettings.value.stepSize = clamp(step, 1, 100)
     149              :   }
     150              : 
     151            0 :   function resetBrushToDefault(): void {
     152            0 :     brushSettings.value.type = BrushShape.Arc
     153            0 :     brushSettings.value.size = 20
     154            0 :     brushSettings.value.opacity = 1
     155            0 :     brushSettings.value.hardness = 1
     156            0 :     brushSettings.value.stepSize = 5
     157              :   }
     158              : 
     159            0 :   function setPaintBucketTolerance(tolerance: number): void {
     160            0 :     paintBucketTolerance.value = clamp(tolerance, 0, 255)
     161              :   }
     162              : 
     163            0 :   function setFillOpacity(opacity: number): void {
     164            0 :     fillOpacity.value = clamp(opacity, 0, 100)
     165              :   }
     166              : 
     167            0 :   function setColorSelectTolerance(tolerance: number): void {
     168            0 :     colorSelectTolerance.value = clamp(tolerance, 0, 255)
     169              :   }
     170              : 
     171            0 :   function setMaskTolerance(tolerance: number): void {
     172            0 :     maskTolerance.value = clamp(tolerance, 0, 255)
     173              :   }
     174              : 
     175            0 :   function setSelectionOpacity(opacity: number): void {
     176            0 :     selectionOpacity.value = clamp(opacity, 0, 100)
     177              :   }
     178              : 
     179           70 :   function setZoomRatio(ratio: number): void {
     180           70 :     zoomRatio.value = Math.max(0.1, Math.min(10, ratio))
     181              :   }
     182              : 
     183           70 :   function setPanOffset(offset: Offset): void {
     184           70 :     panOffset.value = { ...offset }
     185              :   }
     186              : 
     187          230 :   function setCursorPoint(point: Point): void {
     188          230 :     cursorPoint.value = { ...point }
     189              :   }
     190              : 
     191            0 :   function resetZoom(): void {
     192            0 :     resetZoomTrigger.value++
     193              :   }
     194              : 
     195            1 :   function triggerClear(): void {
     196            1 :     clearTrigger.value++
     197              :   }
     198              : 
     199            0 :   function setMaskOpacity(opacity: number): void {
     200            0 :     maskOpacity.value = clamp(opacity, 0, 1)
     201              :   }
     202              : 
     203            7 :   function resetState(): void {
     204            7 :     brushSettings.value = {
     205              :       type: BrushShape.Arc,
     206              :       size: 10,
     207              :       opacity: 0.7,
     208              :       hardness: 1,
     209              :       stepSize: 5
     210              :     }
     211            7 :     maskBlendMode.value = MaskBlendMode.Black
     212            7 :     activeLayer.value = 'mask'
     213            7 :     rgbColor.value = '#FF0000'
     214            7 :     currentTool.value = Tools.MaskPen
     215            7 :     isAdjustingBrush.value = false
     216            7 :     paintBucketTolerance.value = 5
     217            7 :     fillOpacity.value = 100
     218            7 :     colorSelectTolerance.value = 20
     219            7 :     colorSelectLivePreview.value = false
     220            7 :     colorComparisonMethod.value = ColorComparisonMethod.Simple
     221            7 :     applyWholeImage.value = false
     222            7 :     maskBoundary.value = false
     223            7 :     maskTolerance.value = 0
     224            7 :     selectionOpacity.value = 100
     225            7 :     zoomRatio.value = 1
     226            7 :     panOffset.value = { x: 0, y: 0 }
     227            7 :     cursorPoint.value = { x: 0, y: 0 }
     228            7 :     maskOpacity.value = 0.8
     229              : 
     230              :     // Reset GPU recreation flags
     231            7 :     gpuTexturesNeedRecreation.value = false
     232            7 :     gpuTextureWidth.value = 0
     233            7 :     gpuTextureHeight.value = 0
     234            7 :     pendingGPUMaskData.value = null
     235            7 :     pendingGPURgbData.value = null
     236              :   }
     237              : 
     238         2355 :   return {
     239              :     brushSettings,
     240              :     maskBlendMode,
     241              :     activeLayer,
     242              :     rgbColor,
     243              :     currentTool,
     244              :     isAdjustingBrush,
     245              :     paintBucketTolerance,
     246              :     fillOpacity,
     247              :     colorSelectTolerance,
     248              :     colorSelectLivePreview,
     249              :     colorComparisonMethod,
     250              :     applyWholeImage,
     251              :     maskBoundary,
     252              :     maskTolerance,
     253              :     selectionOpacity,
     254              :     zoomRatio,
     255              :     displayZoomRatio,
     256              :     panOffset,
     257              :     cursorPoint,
     258              :     resetZoomTrigger,
     259              :     maskCanvas,
     260              :     maskCtx,
     261              :     rgbCanvas,
     262              :     rgbCtx,
     263              :     imgCanvas,
     264              :     imgCtx,
     265              :     canvasContainer,
     266              :     canvasBackground,
     267              :     pointerZone,
     268              :     image,
     269              :     maskOpacity,
     270              :     canUndo,
     271              :     canRedo,
     272              :     maskColor,
     273              : 
     274              :     brushVisible,
     275              :     isPanning,
     276              :     brushPreviewGradientVisible,
     277              : 
     278              :     canvasHistory,
     279              : 
     280              :     tgpuRoot,
     281              : 
     282              :     // GPU texture recreation signals
     283              :     gpuTexturesNeedRecreation,
     284              :     gpuTextureWidth,
     285              :     gpuTextureHeight,
     286              :     pendingGPUMaskData,
     287              :     pendingGPURgbData,
     288              : 
     289              :     colorInput,
     290              : 
     291              :     setBrushSize,
     292              :     setBrushOpacity,
     293              :     setBrushHardness,
     294              :     setBrushStepSize,
     295              :     resetBrushToDefault,
     296              :     setPaintBucketTolerance,
     297              :     setFillOpacity,
     298              :     setColorSelectTolerance,
     299              :     setMaskTolerance,
     300              :     setSelectionOpacity,
     301              :     setZoomRatio,
     302              :     setPanOffset,
     303              :     setCursorPoint,
     304              :     resetZoom,
     305              :     triggerClear,
     306              :     clearTrigger,
     307              :     setMaskOpacity,
     308              :     resetState
     309              :   }
     310              : })
        

Generated by: LCOV version 2.0-1