LCOV - code coverage report
Current view: top level - src/core/graph/widgets - valueControlWidgets.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 93.3 % 60 56
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { t } from '@/i18n'
       2              : import { isComboWidget } from '@/lib/litegraph/src/litegraph'
       3              : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
       4              : import type {
       5              :   IBaseWidget,
       6              :   IComboWidget,
       7              :   IStringWidget
       8              : } from '@comfyorg/graph-model/widgets'
       9              : import { useSettingStore } from '@/platform/settings/settingStore'
      10              : import type { InputSpec } from '@comfyorg/object-info-parser'
      11              : import { CONTROL_OPTIONS } from '@/types/simplifiedWidget'
      12              : 
      13              : import { IS_CONTROL_WIDGET } from './controlWidgetMarker'
      14              : import { isValueControlMode, nextValueForLinkedTarget } from './valueControl'
      15              : 
      16         3759 : function controlValueRunBefore() {
      17         3759 :   return useSettingStore().get('Comfy.WidgetControlMode') === 'before'
      18              : }
      19              : 
      20         3599 : export function updateControlWidgetLabel(widget: IBaseWidget) {
      21         3599 :   if (controlValueRunBefore()) {
      22            0 :     widget.label = t('g.control_before_generate')
      23              :   } else {
      24         3599 :     widget.label = t('g.control_after_generate')
      25              :   }
      26              : }
      27              : 
      28         2356 : const HAS_EXECUTED = Symbol()
      29              : 
      30              : /**
      31              :  * `control_after_generate` is either a group-node widget name override or a
      32              :  * control mode that core's `io.ControlAfterGenerate` enum serialises into
      33              :  * `object_info`. Only a non-blank string that is not a mode is a name.
      34              :  */
      35        10657 : function controlAfterGenerateNameOverride(value: unknown): string | undefined {
      36        10657 :   if (typeof value !== 'string' || value.trim() === '') return undefined
      37        10657 :   return isValueControlMode(value) ? undefined : value
      38              : }
      39              : 
      40         3527 : export function addValueControlWidget(
      41              :   node: LGraphNode,
      42              :   targetWidget: IBaseWidget,
      43              :   defaultValue?: string,
      44              :   _values?: unknown,
      45              :   widgetName?: string,
      46              :   inputData?: InputSpec
      47              : ): IComboWidget {
      48         3527 :   const name =
      49              :     controlAfterGenerateNameOverride(inputData?.[1]?.control_after_generate) ??
      50              :     widgetName
      51              :   const widgets = addValueControlWidgets(
      52              :     node,
      53              :     targetWidget,
      54              :     defaultValue ?? 'randomize',
      55              :     {
      56              :       addFilterList: false,
      57              :       controlAfterGenerateName: name
      58              :     },
      59              :     inputData
      60              :   )
      61         3527 :   return widgets[0]
      62              : }
      63              : 
      64              : interface ValueControlWidgetOptions {
      65              :   addFilterList?: boolean
      66              :   controlAfterGenerateName?: string
      67              :   controlFilterListName?: string
      68              : }
      69              : 
      70         3567 : export function addValueControlWidgets(
      71              :   node: LGraphNode,
      72              :   targetWidget: IBaseWidget,
      73              :   defaultValue?: string,
      74              :   options: ValueControlWidgetOptions = {},
      75              :   inputData?: InputSpec
      76              : ): [IComboWidget, ...IStringWidget[]] {
      77              :   // `useIntWidget` forwards a group-node name override here as the mode.
      78         3567 :   const specNameOverride = controlAfterGenerateNameOverride(
      79              :     inputData?.[1]?.control_after_generate
      80              :   )
      81         3567 :   if (!defaultValue || defaultValue === specNameOverride) {
      82            4 :     defaultValue = 'randomize'
      83              :   }
      84              : 
      85         3567 :   const getName = (
      86              :     defaultName: 'control_after_generate' | 'control_filter_list',
      87              :     optionName: 'controlAfterGenerateName' | 'controlFilterListName'
      88              :   ) => {
      89         3599 :     const nameOverride = options[optionName]
      90         3599 :     if (nameOverride) return nameOverride
      91         3599 :     const inputOptions = inputData?.[1]
      92         3599 :     const specValue = inputOptions?.[defaultName]
      93         3599 :     const defaultNameOverride =
      94              :       defaultName === 'control_after_generate'
      95              :         ? controlAfterGenerateNameOverride(specValue)
      96              :         : typeof specValue === 'string'
      97              :           ? specValue
      98              :           : undefined
      99         3599 :     if (defaultNameOverride !== undefined) return defaultNameOverride
     100         3599 :     if (inputOptions?.control_prefix) {
     101            0 :       return inputOptions.control_prefix + ' ' + defaultName
     102              :     }
     103         3595 :     return defaultName
     104              :   }
     105              : 
     106         3567 :   const valueControl = node.addWidget(
     107              :     'combo',
     108              :     getName('control_after_generate', 'controlAfterGenerateName'),
     109              :     defaultValue,
     110              :     function () {},
     111              :     {
     112              :       values: [...CONTROL_OPTIONS],
     113              :       serialize: false, // Don't include this in prompt.
     114              :       surfaces: { canvas: 'shown', vueNode: 'never', panel: 'never' }
     115              :     }
     116              :   ) as IComboWidget
     117              : 
     118         3567 :   valueControl.tooltip =
     119              :     'Allows the linked widget to be changed automatically, for example randomizing the noise seed.'
     120         3567 :   valueControl[IS_CONTROL_WIDGET] = true
     121         3567 :   updateControlWidgetLabel(valueControl)
     122         3567 :   Object.defineProperty(valueControl, 'disabled', {
     123              :     get: () => targetWidget.computedDisabled
     124              :   })
     125         3567 :   const widgets: [IComboWidget, ...IStringWidget[]] = [valueControl]
     126              : 
     127         3567 :   const isCombo = isComboWidget(targetWidget)
     128         3567 :   let comboFilter: IStringWidget
     129         3567 :   if (isCombo) {
     130           32 :     const values = valueControl.options.values
     131           32 :     if (Array.isArray(values)) values.push('increment-wrap')
     132              :   }
     133         3567 :   if (isCombo && options.addFilterList !== false) {
     134           32 :     comboFilter = node.addWidget(
     135              :       'string',
     136              :       getName('control_filter_list', 'controlFilterListName'),
     137              :       '',
     138              :       function () {},
     139              :       {
     140              :         serialize: false // Don't include this in prompt.
     141              :       }
     142              :     ) as IStringWidget
     143           32 :     updateControlWidgetLabel(comboFilter)
     144           32 :     comboFilter.tooltip =
     145              :       "Allows for filtering the list of values when changing the value via the control generate mode. Allows for RegEx matches in the format /abc/ to only filter to values containing 'abc'."
     146           32 :     Object.defineProperty(comboFilter, 'disabled', {
     147              :       get: () => targetWidget.computedDisabled
     148              :     })
     149              : 
     150           32 :     widgets.push(comboFilter)
     151              :   }
     152              : 
     153           65 :   function applyWidgetControl() {
     154           65 :     if (
     155              :       node.inputs.some(
     156              :         (input, index) =>
     157              :           input.widget?.name === targetWidget.name &&
     158              :           node.isInputConnected(index)
     159              :       )
     160              :     )
     161            4 :       return
     162              : 
     163           61 :     const next = nextValueForLinkedTarget({
     164              :       target: targetWidget,
     165              :       linkedWidgets: targetWidget.linkedWidgets,
     166              :       nodeId: node.id
     167              :     })
     168           65 :     if (next === undefined) return
     169              : 
     170           47 :     targetWidget.value = next
     171           65 :     targetWidget.callback?.(next)
     172              :   }
     173              : 
     174         3567 :   valueControl.beforeQueued = () => {
     175           95 :     if (controlValueRunBefore()) {
     176              :       // Don't run on first execution
     177            0 :       if (valueControl[HAS_EXECUTED]) {
     178            0 :         applyWidgetControl()
     179              :       }
     180              :     }
     181           95 :     valueControl[HAS_EXECUTED] = true
     182              :   }
     183              : 
     184         3567 :   valueControl.afterQueued = () => {
     185           65 :     if (!controlValueRunBefore()) {
     186           65 :       applyWidgetControl()
     187              :     }
     188              :   }
     189              : 
     190         3567 :   return widgets
     191              : }
        

Generated by: LCOV version 2.0-1