LCOV - code coverage report
Current view: top level - src/platform/theme - applyTheme.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 100.0 % 50 50
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Theme } from '@comfyorg/theme-core/resolveTheme'
       2              : import { usesThemeAliases } from '@comfyorg/theme-core/resolveTheme'
       3              : import {
       4              :   comfyBaseSchema,
       5              :   themeSchema
       6              : } from '@comfyorg/theme-core/themePresetSchema'
       7              : 
       8              : import { LGraphCanvas, LiteGraph } from '@/lib/litegraph/src/litegraph'
       9              : 
      10         2356 : const THEME_ALIASED_LEGACY_KEYS: ReadonlySet<string> = new Set([
      11              :   'fg-color',
      12              :   'bg-color',
      13              :   'comfy-menu-bg',
      14              :   'comfy-menu-secondary-bg',
      15              :   'descrip-text',
      16              :   'drag-text',
      17              :   'error-text',
      18              :   'tr-even-bg-color',
      19              :   'tr-odd-bg-color',
      20              :   'content-bg',
      21              :   'content-fg',
      22              :   'content-hover-bg',
      23              :   'content-hover-fg'
      24              : ])
      25              : 
      26              : type ThemeTarget = {
      27              :   style: CSSStyleDeclaration
      28              :   canvas?: LGraphCanvas
      29              :   dataTypes?: Iterable<string>
      30              :   backgroundImage?: string
      31              : }
      32              : 
      33         2386 : function applyCssTokens(theme: Theme, style: CSSStyleDeclaration) {
      34         2386 :   for (const key of themeSchema.keyof().options) {
      35        59650 :     const value = theme.cssTokens[key]
      36        59650 :     if (value) style.setProperty(`--${key}`, value)
      37        14310 :     else style.removeProperty(`--${key}`)
      38              :   }
      39         2386 :   style.setProperty(
      40              :     '--node-widget-gap',
      41              :     `${theme.preset.colors.litegraph_base.NODE_WIDGET_GAP}px`
      42              :   )
      43         2386 :   style.colorScheme = theme.colorScheme
      44              : }
      45              : 
      46         2386 : function applyLegacyCssVars(theme: Theme, target: ThemeTarget) {
      47         2386 :   for (const key of comfyBaseSchema.keyof().options) {
      48        64422 :     if (key === 'bg-img') continue
      49        62036 :     const value = theme.legacyCss[key]
      50        64422 :     if (
      51              :       value === undefined ||
      52              :       (usesThemeAliases(theme.id) && THEME_ALIASED_LEGACY_KEYS.has(key))
      53              :     ) {
      54        52343 :       target.style.removeProperty(`--${key}`)
      55              :     } else {
      56         9693 :       target.style.setProperty(`--${key}`, value)
      57              :     }
      58              :   }
      59         2386 :   if (target.backgroundImage) {
      60            6 :     target.style.setProperty('--bg-img', `url('${target.backgroundImage}')`)
      61              :   } else {
      62         2380 :     target.style.removeProperty('--bg-img')
      63              :   }
      64              : }
      65              : 
      66         2385 : function applyLiteGraphGlobals(theme: Theme) {
      67         2385 :   const values = theme.preset.colors.litegraph_base
      68         2385 :   if (typeof values.NODE_DEFAULT_SHAPE === 'string') {
      69            2 :     console.warn(
      70              :       `litegraph_base.NODE_DEFAULT_SHAPE only accepts [${[
      71              :         LiteGraph.BOX_SHAPE,
      72              :         LiteGraph.ROUND_SHAPE,
      73              :         LiteGraph.CARD_SHAPE
      74              :       ].join(', ')}] but got ${values.NODE_DEFAULT_SHAPE}`
      75              :     )
      76              :   }
      77         2385 :   Object.assign(LiteGraph, {
      78              :     ...values,
      79              :     NODE_DEFAULT_SHAPE:
      80              :       typeof values.NODE_DEFAULT_SHAPE === 'string'
      81              :         ? LiteGraph.ROUND_SHAPE
      82              :         : values.NODE_DEFAULT_SHAPE
      83              :   })
      84              : }
      85              : 
      86         2385 : function applyCanvasColors(
      87              :   theme: Theme,
      88              :   canvas: LGraphCanvas,
      89              :   backgroundImage?: string
      90              : ) {
      91         2385 :   canvas.node_title_color = theme.canvas.nodeTitleColor
      92         2385 :   canvas.default_link_color = theme.canvas.linkColor
      93         2385 :   if (backgroundImage) {
      94            6 :     canvas.clear_background_color = 'transparent'
      95              :   } else {
      96         2379 :     canvas.background_image = theme.canvas.backgroundImage
      97         2379 :     canvas.clear_background_color = theme.canvas.clearBackgroundColor
      98              :   }
      99         2385 :   canvas._pattern = undefined
     100              : }
     101              : 
     102         2386 : function applyDataTypeColors(theme: Theme, target: ThemeTarget) {
     103         2386 :   const colors: Record<string, string> = theme.dataTypeColors
     104         2386 :   const types = Array.from(target.dataTypes ?? Object.keys(colors))
     105         2386 :   for (const type of types) {
     106       280898 :     const value = colors[type]
     107       280898 :     if (value) target.style.setProperty(`--color-datatype-${type}`, value)
     108       245179 :     else target.style.removeProperty(`--color-datatype-${type}`)
     109              :   }
     110         2386 :   if (target.canvas) {
     111         2385 :     const reset = Object.fromEntries(types.map((type) => [type, '']))
     112         2385 :     Object.assign(target.canvas.default_connection_color_byType, reset, colors)
     113         2385 :     Object.assign(LGraphCanvas.link_type_colors, reset, colors)
     114              :   }
     115              : }
     116              : 
     117         2386 : export function applyTheme(theme: Theme, target: ThemeTarget) {
     118         2386 :   if (target.canvas) {
     119         2385 :     applyLiteGraphGlobals(theme)
     120         2385 :     applyCanvasColors(theme, target.canvas, target.backgroundImage)
     121              :   }
     122         2386 :   applyCssTokens(theme, target.style)
     123         2386 :   applyDataTypeColors(theme, target)
     124         2386 :   applyLegacyCssVars(theme, target)
     125         2386 :   target.canvas?.setDirty(true, true)
     126              : }
        

Generated by: LCOV version 2.0-1