File size: 12,538 Bytes
7312c42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { useEffect, useMemo, useRef, useState } from 'react'
import type { ComfyApp } from '@comfyorg/comfyui-frontend-types'
import { Eye, EyeOff } from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
  useSamplingPreviewFrames,
  useSamplingPreviewPlayback,
  type SamplingPreviewFrame,
} from '@/hooks/use-sampling-preview-progress'
import { LocaleContext, useT } from '@/lib/i18n'

export const SAMPLING_PREVIEW_EVENT = 'easy_media.sampling_preview'
const EXECUTION_START_EVENT = 'execution_start'

export interface SamplingPreviewPayload {
  node_id: string
  display_node_id?: string
  prompt_id: string | null
  image: string
  images?: string[]
  mime: string
  step: number
  total: number
  fps: number | null
  frame_count: number
  segment_index: number
  sampling_pass: string
}

interface SamplingPreviewWidgetProps {
  app: ComfyApp
  nodeId: string | number | (() => string | number)
  executionRevision?: number
  onVisibilityChange?: (visible: boolean) => void
  interactive?: boolean
  visibilityControlOnly?: boolean
}

function terminalNodeId(value: string | number | undefined) {
  return value === undefined ? '' : String(value).split(':').at(-1)?.split('.').at(-1)
}

function eventTargetsNode(
  payload: SamplingPreviewPayload,
  nodeId: string | number | (() => string | number),
) {
  const currentNodeId = typeof nodeId === 'function' ? nodeId() : nodeId
  return [payload.display_node_id, payload.node_id]
    .some((candidate) => terminalNodeId(candidate) === String(currentNodeId))
}

function previewBlob(image: string, mime: string) {
  const binary = atob(image)
  const bytes = new Uint8Array(binary.length)
  for (let index = 0; index < binary.length; index += 1) {
    bytes[index] = binary.charCodeAt(index)
  }
  return new Blob([bytes], { type: mime || 'image/jpeg' })
}

function PreviewCanvas({ frame, label }: { frame: SamplingPreviewFrame, label: string }) {
  const canvasRef = useRef<HTMLCanvasElement>(null)

  useEffect(() => {
    const canvas = canvasRef.current
    if (!canvas) return
    canvas.width = frame.bitmap.width
    canvas.height = frame.bitmap.height
    const context = canvas.getContext('2d')
    if (!context) {
      console.error('[SamplingPreviewWidget] 2D canvas context is unavailable')
      return
    }
    context.drawImage(frame.bitmap, 0, 0)
  }, [frame])

  return (
    <canvas
      ref={canvasRef}
      role="img"
      aria-label={label}
      className="absolute inset-0 h-full w-full object-contain"
    />
  )
}

function SamplingPreviewWidgetInner({
  app,
  nodeId,
  executionRevision = 0,
  onVisibilityChange,
  interactive = true,
  visibilityControlOnly = false,
}: SamplingPreviewWidgetProps) {
  const t = useT()
  const [preview, setPreview] = useState<SamplingPreviewPayload | null>(null)
  const [pausedPreview, setPausedPreview] = useState<SamplingPreviewPayload | null>(null)
  const [previewUrl, setPreviewUrl] = useState('')
  const [legacyPreviewUrls, setLegacyPreviewUrls] = useState<string[]>([])
  const [isPlaying, setIsPlaying] = useState(true)
  const [isPreviewVisible, setIsPreviewVisible] = useState(true)
  const [frozenFallbackUrl, setFrozenFallbackUrl] = useState('')
  const fallbackImageRef = useRef<HTMLImageElement>(null)
  const frozenFallbackCanvasRef = useRef<HTMLCanvasElement>(null)
  const activePromptIdRef = useRef<string | null>(null)
  const playbackInteractive = interactive && !visibilityControlOnly
  const visibilityInteractive = interactive || visibilityControlOnly
  const displayedPreview = isPlaying ? preview : (pausedPreview ?? preview)

  useEffect(() => {
    const handleExecutionStart = (event: CustomEvent<unknown>) => {
      const detail = event.detail
      activePromptIdRef.current = detail && typeof detail === 'object'
        && 'prompt_id' in detail && typeof detail.prompt_id === 'string'
        ? detail.prompt_id
        : null
    }
    const handlePreview = (event: CustomEvent<unknown>) => {
      const payload = event.detail as SamplingPreviewPayload | undefined
      if (!payload?.image || !eventTargetsNode(payload, nodeId)) return
      if (
        activePromptIdRef.current !== null
        && payload.prompt_id !== activePromptIdRef.current
      ) return
      setPreview(payload)
    }
    app.api.addCustomEventListener(EXECUTION_START_EVENT, handleExecutionStart)
    app.api.addCustomEventListener(SAMPLING_PREVIEW_EVENT, handlePreview)
    return () => {
      app.api.removeCustomEventListener(EXECUTION_START_EVENT, handleExecutionStart)
      app.api.removeCustomEventListener(SAMPLING_PREVIEW_EVENT, handlePreview)
    }
  }, [app.api, nodeId])

  useEffect(() => {
    setPreview(null)
    setPausedPreview(null)
    setIsPlaying(true)
    setFrozenFallbackUrl('')
  }, [executionRevision])

  useEffect(() => {
    onVisibilityChange?.(preview !== null)
  }, [onVisibilityChange, preview])

  const previewData = useMemo(
    () => displayedPreview ? previewBlob(displayedPreview.image, displayedPreview.mime) : null,
    [displayedPreview],
  )

  useEffect(() => {
    if (!previewData) {
      setPreviewUrl('')
      return
    }
    const nextUrl = URL.createObjectURL(previewData)
    setPreviewUrl(nextUrl)
    return () => URL.revokeObjectURL(nextUrl)
  }, [previewData])

  useEffect(() => {
    const legacyImages = displayedPreview?.images ?? []
    if (legacyImages.length === 0) {
      setLegacyPreviewUrls([])
      return
    }
    const nextUrls = legacyImages.map((image) => URL.createObjectURL(
      previewBlob(image, 'image/jpeg'),
    ))
    setLegacyPreviewUrls(nextUrls)
    return () => nextUrls.forEach((url) => URL.revokeObjectURL(url))
  }, [displayedPreview])

  const decodedFrames = useSamplingPreviewFrames(
    previewData,
    displayedPreview?.image,
    playbackInteractive,
  )
  const playbackFrameCount = legacyPreviewUrls.length
    || displayedPreview?.frame_count
    || decodedFrames.length
  const playbackRevision = displayedPreview
    ? `${displayedPreview.segment_index}:${displayedPreview.sampling_pass}:${displayedPreview.frame_count}:${displayedPreview.fps}`
    : undefined

  const { frameIndex, progress: playbackProgress } = useSamplingPreviewPlayback(
    playbackRevision,
    playbackFrameCount,
    displayedPreview?.fps ?? null,
    isPlaying,
  )

  if (!displayedPreview) return null
  const passLabel = t(`samplingPreview.${displayedPreview.sampling_pass}`)
  const decodedFrameIndex = decodedFrames.length <= 1 || playbackFrameCount <= 1
    ? 0
    : Math.min(
      decodedFrames.length - 1,
      Math.floor(frameIndex / playbackFrameCount * decodedFrames.length),
    )
  const decodedFrame = decodedFrames[decodedFrameIndex]
  const legacyPreviewUrl = legacyPreviewUrls[
    Math.min(frameIndex, legacyPreviewUrls.length - 1)
  ]
  const usesAnimatedImageFallback = (
    displayedPreview.mime === 'image/webp'
    && legacyPreviewUrls.length === 0
    && decodedFrame === undefined
  )
  const freezeAnimatedFallback = () => {
    if (!usesAnimatedImageFallback) return
    const image = fallbackImageRef.current
    const canvas = frozenFallbackCanvasRef.current
    if (!image?.complete || image.naturalWidth <= 0 || image.naturalHeight <= 0 || !canvas) return
    canvas.width = image.naturalWidth
    canvas.height = image.naturalHeight
    const context = canvas.getContext('2d')
    if (!context) {
      console.error('[SamplingPreviewWidget] 2D canvas context is unavailable')
      return
    }
    context.drawImage(image, 0, 0)
    setFrozenFallbackUrl(previewUrl)
  }
  const togglePlayback = () => {
    if (isPlaying) {
      freezeAnimatedFallback()
      setPausedPreview(preview)
      setIsPlaying(false)
    } else {
      setPausedPreview(null)
      setIsPlaying(true)
    }
  }
  const playbackLabel = t(isPlaying ? 'samplingPreview.pausePreview' : 'samplingPreview.resumePreview')
  const metaLabel = t('samplingPreview.meta', {
    segment: displayedPreview.segment_index + 1,
    pass: passLabel,
    step: displayedPreview.step,
    total: displayedPreview.total,
  })
  const progressBar = (
    <div
      role="progressbar"
      aria-label={t('samplingPreview.playbackProgress')}
      aria-valuemin={0}
      aria-valuemax={100}
      aria-valuenow={Math.round(playbackProgress)}
      className="h-1.5 w-full overflow-hidden rounded-full bg-muted"
    >
      <div
        className="h-full rounded-full bg-primary"
        style={{ width: `${playbackProgress}%` }}
      />
    </div>
  )

  return (
    <div className="relative flex h-full w-full flex-col overflow-hidden rounded-lg border border-border bg-black">
      {isPreviewVisible ? (
        playbackInteractive ? (
          <Button
            type="button"
            variant="ghost"
            aria-label={playbackLabel}
            onClick={togglePlayback}
            className="relative min-h-0 flex-1 rounded-none p-0 hover:bg-black"
          >
            {legacyPreviewUrl ? (
              <img
                src={legacyPreviewUrl}
                alt={t('samplingPreview.title')}
                draggable={false}
                className="absolute inset-0 h-full w-full object-contain"
              />
            ) : decodedFrame ? (
              <PreviewCanvas
                frame={decodedFrame}
                label={t('samplingPreview.title')}
              />
            ) : previewUrl ? (
              <>
                <img
                  ref={fallbackImageRef}
                  src={previewUrl}
                  alt={t('samplingPreview.title')}
                  draggable={false}
                  onLoad={() => {
                    if (!isPlaying && !frozenFallbackUrl) freezeAnimatedFallback()
                  }}
                  className="absolute inset-0 h-full w-full object-contain"
                />
                <canvas
                  ref={frozenFallbackCanvasRef}
                  data-testid="sampling-preview-frozen-frame"
                  aria-hidden="true"
                  hidden={isPlaying || !frozenFallbackUrl}
                  className="absolute inset-0 h-full w-full object-contain"
                />
              </>
            ) : null}
          </Button>
        ) : (
          <div className="relative min-h-0 flex-1">
            {legacyPreviewUrl ? (
              <img
                src={legacyPreviewUrl}
                alt={t('samplingPreview.title')}
                draggable={false}
                className="absolute inset-0 h-full w-full object-contain"
              />
            ) : previewUrl ? (
              <img
                src={previewUrl}
                alt={t('samplingPreview.title')}
                draggable={false}
                className="absolute inset-0 h-full w-full object-contain"
              />
            ) : null}
          </div>
        )
      ) : (
        <div
          data-testid="sampling-preview-placeholder"
          aria-hidden="true"
          className="min-h-0 flex-1 bg-black"
        />
      )}
      <div className="shrink-0 border-t border-border bg-background px-2 py-1 gap-0">
        <div className="flex items-center justify-between text-[10px] text-muted-foreground">
          <span>{metaLabel}</span>
          {visibilityInteractive ? (
            <Button
              type="button"
              variant="ghost"
              size="icon"
              aria-label={t(
                isPreviewVisible
                  ? 'samplingPreview.hidePreview'
                  : 'samplingPreview.showPreview',
              )}
              onClick={() => setIsPreviewVisible((visible) => !visible)}
              className="pointer-events-auto h-6 w-6"
            >
              {isPreviewVisible ? <EyeOff /> : <Eye />}
            </Button>
          ) : null}
        </div>
        {playbackInteractive ? (
          <Button
            type="button"
            variant="ghost"
            aria-label={playbackLabel}
            onClick={togglePlayback}
            className="h-3 w-full rounded-full p-0 hover:bg-muted"
          >
            {progressBar}
          </Button>
        ) : (
          <div className="h-3 w-full rounded-full p-0">{progressBar}</div>
        )}
      </div>
    </div>
  )
}

export function SamplingPreviewWidget(props: SamplingPreviewWidgetProps) {
  const locale = props.app?.ui?.settings?.settingsValues?.['Comfy.Locale']
  return (
    <LocaleContext.Provider value={locale}>
      <SamplingPreviewWidgetInner {...props} />
    </LocaleContext.Provider>
  )
}