LCOV - code coverage report
Current view: top level - src/renderer/extensions/propertiesPanel/parameters - SectionWidgets.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 79.0 % 100 79
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : <script setup lang="ts">
       2              : import { watchDebounced } from '@vueuse/core'
       3              : import {
       4              :   computed,
       5              :   inject,
       6              :   onBeforeUnmount,
       7              :   onMounted,
       8              :   provide,
       9              :   ref,
      10              :   shallowRef,
      11              :   watchEffect
      12              : } from 'vue'
      13              : import { useI18n } from 'vue-i18n'
      14              : 
      15              : import Button from '@comfyorg/design-system/button/Button'
      16              : import { resolvePromotedWidgetSource } from '@/core/graph/subgraph/resolvePromotedWidgetSource'
      17              : import type { LGraphGroup, LGraphNode } from '@/lib/litegraph/src/litegraph'
      18              : import { SubgraphNode } from '@/lib/litegraph/src/litegraph'
      19              : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
      20              : import { useTelemetry } from '@comfyorg/telemetry'
      21              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
      22              : import { DraggableList } from '@/scripts/ui/draggableList'
      23              : import { useExecutionErrorStore } from '@/stores/executionErrorStore'
      24              : import { useWidgetValueStore } from '@/stores/widgetValueStore'
      25              : import { usePropertiesPanelStore } from '@/renderer/extensions/propertiesPanel/propertiesPanelStore'
      26              : import { useErrorsTabRequestStore } from '@/platform/workflowErrors/panel/errorsTabRequestStore'
      27              : import { useSettingStore } from '@/platform/settings/settingStore'
      28              : import { cn } from '@comfyorg/tailwind-utils'
      29              : import { useNodeDefStore } from '@/stores/nodeDefStore'
      30              : import { getExecutionIdByNode } from '@/utils/graphTraversalUtil'
      31              : import { getWidgetDefaultValue } from '@/utils/widgetUtil'
      32              : import type { WidgetValue } from '@/utils/widgetUtil'
      33              : 
      34              : import PropertiesAccordionItem from '@comfyorg/design-system/accordion/PropertiesAccordionItem'
      35              : import { HideLayoutFieldKey, WidgetHeightKey } from '@/types/widgetTypes'
      36              : 
      37              : import { GetNodeParentGroupKey } from '@/renderer/extensions/propertiesPanel/parentGroup'
      38              : import WidgetItem from './WidgetItem.vue'
      39              : import { getStableWidgetRenderKey } from '@/core/graph/subgraph/widgetRenderKey'
      40              : 
      41              : const {
      42              :   label,
      43              :   node,
      44              :   widgets: widgetsProp,
      45              :   showLocateButton = false,
      46              :   isDraggable = false,
      47              :   hiddenFavoriteIndicator = false,
      48              :   showNodeName = false,
      49              :   host,
      50              :   enableEmptyState = false,
      51              :   tooltip
      52              : } = defineProps<{
      53              :   label?: string
      54              :   host?: SubgraphNode
      55              :   node?: LGraphNode
      56              :   widgets: { widget: IBaseWidget; node: LGraphNode }[]
      57              :   showLocateButton?: boolean
      58              :   isDraggable?: boolean
      59              :   hiddenFavoriteIndicator?: boolean
      60              :   showNodeName?: boolean
      61              :   /**
      62              :    * Whether to show the empty state slot when there are no widgets.
      63              :    */
      64              :   enableEmptyState?: boolean
      65              :   tooltip?: string
      66              : }>()
      67              : 
      68         2355 : const collapse = defineModel<boolean>('collapse', { default: false })
      69              : 
      70          354 : const emit = defineEmits<{
      71              :   reorder: [event: { fromIndex: number; toIndex: number }]
      72              : }>()
      73              : 
      74          354 : const widgetsContainer = ref<HTMLElement>()
      75          354 : const rootElement = ref<HTMLElement>()
      76              : 
      77          354 : const localWidgets = shallowRef(widgetsProp)
      78          354 : watchEffect(() => (localWidgets.value = widgetsProp))
      79              : 
      80          354 : const draggableList = ref<DraggableList | undefined>()
      81              : 
      82          395 : function setDraggableState() {
      83          395 :   draggableList.value?.dispose()
      84          395 :   draggableList.value = undefined
      85              : 
      86          395 :   if (!isDraggable || collapse.value) return
      87          223 :   const container = widgetsContainer.value
      88          395 :   if (!container?.children?.length) return
      89              : 
      90           16 :   const list = new DraggableList(container, '.draggable-item')
      91              : 
      92           16 :   list.applyNewItemsOrder = function () {
      93            1 :     if (!this.draggableItem) return
      94            1 :     const { items, oldPosition } = this.getReorderedItems(this.draggableItem)
      95              : 
      96            1 :     if (oldPosition === -1) {
      97            0 :       console.error('[SectionWidgets] draggableItem not found in items')
      98            0 :       return
      99              :     }
     100              : 
     101            1 :     const newPosition = items.indexOf(this.draggableItem)
     102              : 
     103            1 :     emit('reorder', { fromIndex: oldPosition, toIndex: newPosition })
     104              :   }
     105              : 
     106           16 :   draggableList.value = list
     107              : }
     108              : 
     109          354 : watchDebounced(
     110              :   [localWidgets, () => isDraggable, collapse],
     111              :   () => setDraggableState(),
     112              :   { debounce: 100 }
     113              : )
     114          354 : onMounted(setDraggableState)
     115          354 : onBeforeUnmount(() => draggableList.value?.dispose())
     116              : 
     117          354 : provide(HideLayoutFieldKey, true)
     118          354 : provide(WidgetHeightKey, 'h-7')
     119              : 
     120          354 : const canvasStore = useCanvasStore()
     121          354 : const executionErrorStore = useExecutionErrorStore()
     122          354 : const propertiesPanelStore = usePropertiesPanelStore()
     123          354 : const errorsTabRequestStore = useErrorsTabRequestStore()
     124          354 : const nodeDefStore = useNodeDefStore()
     125          354 : const { t } = useI18n()
     126              : 
     127          354 : const getNodeParentGroup = inject(GetNodeParentGroupKey, null)
     128              : 
     129          354 : const isEmpty = computed(() => localWidgets.value.length === 0)
     130              : 
     131          354 : const displayLabel = computed(
     132              :   () => label ?? (node ? node.title : t('rightSidePanel.inputs'))
     133              : )
     134              : 
     135          354 : const targetNode = computed<LGraphNode | null>(() => {
     136          355 :   if (node) return node
     137          233 :   if (isEmpty.value) return null
     138              : 
     139           14 :   const firstNodeId = localWidgets.value[0].node.id
     140              :   const allSameNode = localWidgets.value.every(
     141              :     ({ node }) => node.id === firstNodeId
     142              :   )
     143              : 
     144          355 :   return allSameNode ? localWidgets.value[0].node : null
     145              : })
     146              : 
     147          354 : const hasDirectError = computed(() => {
     148           75 :   if (!targetNode.value) return false
     149           75 :   return executionErrorStore.activeGraphErrorNodeIds.has(
     150              :     String(targetNode.value.id)
     151              :   )
     152              : })
     153              : 
     154          354 : const hasContainerInternalError = computed(() => {
     155           75 :   if (!targetNode.value) return false
     156           75 :   if (!(targetNode.value instanceof SubgraphNode)) return false
     157              : 
     158            1 :   return executionErrorStore.isContainerWithInternalError(targetNode.value)
     159              : })
     160              : 
     161          354 : const nodeHasError = computed(() => {
     162          359 :   if (!targetNode.value) return false
     163          217 :   if (canvasStore.selectedItems.length === 1) return false
     164          359 :   return hasDirectError.value || hasContainerInternalError.value
     165              : })
     166              : 
     167          354 : const showSeeError = computed(
     168              :   () =>
     169              :     nodeHasError.value &&
     170              :     useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')
     171              : )
     172              : 
     173          354 : const parentGroup = computed<LGraphGroup | null>(() => {
     174          358 :   if (!targetNode.value || !getNodeParentGroup) return null
     175          189 :   return getNodeParentGroup(targetNode.value)
     176              : })
     177              : 
     178          354 : const canShowLocateButton = computed(
     179              :   () => showLocateButton && targetNode.value !== null
     180              : )
     181              : 
     182            0 : function handleLocateNode() {
     183            0 :   if (!targetNode.value || !canvasStore.canvas) return
     184              : 
     185            0 :   useTelemetry()?.trackUiButtonClicked({
     186              :     button_id: 'right_side_panel_locate_node_clicked',
     187              :     element_group: 'right_side_panel_nodes'
     188              :   })
     189              : 
     190            0 :   const graphNode = canvasStore.canvas.graph?.getNodeById(targetNode.value.id)
     191            0 :   if (graphNode) {
     192            0 :     canvasStore.canvas.animateToBounds(graphNode.boundingRect)
     193              :   }
     194              : }
     195              : 
     196            0 : function navigateToErrorTab() {
     197            0 :   if (!targetNode.value) return
     198            0 :   if (!useSettingStore().get('Comfy.RightSidePanel.ShowErrorsTab')) return
     199            0 :   if (propertiesPanelStore.openPanel('errors')) {
     200            0 :     errorsTabRequestStore.focusNode(String(targetNode.value.id))
     201              :   }
     202              : }
     203              : 
     204            1 : function clearWidgetErrors(
     205              :   widgetNode: LGraphNode,
     206              :   widget: IBaseWidget,
     207              :   value: WidgetValue
     208              : ) {
     209            1 :   const rootGraph = widgetNode.graph?.rootGraph
     210            1 :   if (!rootGraph) return
     211              : 
     212            1 :   const executionId = getExecutionIdByNode(rootGraph, widgetNode)
     213            1 :   if (!executionId) return
     214              : 
     215            1 :   const options = { min: widget.options?.min, max: widget.options?.max }
     216            1 :   const source = resolvePromotedWidgetSource(rootGraph, widgetNode, widget)
     217            1 :   if (source?.sourceExecutionId) {
     218            1 :     executionErrorStore.clearWidgetRelatedErrors(
     219              :       source.sourceExecutionId,
     220              :       source.sourceWidgetName,
     221              :       source.sourceWidgetName,
     222              :       value,
     223              :       {
     224              :         min: source.sourceWidget.options?.min,
     225              :         max: source.sourceWidget.options?.max
     226              :       }
     227              :     )
     228              :   }
     229              : 
     230            1 :   executionErrorStore.clearWidgetRelatedErrors(
     231              :     executionId,
     232              :     widget.name,
     233              :     widget.name,
     234              :     value,
     235              :     options
     236              :   )
     237              : }
     238              : 
     239            1 : function setWidgetValue(
     240              :   widgetNode: LGraphNode,
     241              :   widget: IBaseWidget,
     242              :   value: WidgetValue
     243              : ) {
     244              :   // Store-backed widgets (interior node widgets and promoted subgraph inputs)
     245              :   // are addressed by widgetId; writing there keeps the displayed value in sync.
     246            1 :   if (widget.widgetId) useWidgetValueStore().setValue(widget.widgetId, value)
     247            1 :   widget.value = value
     248            1 :   widget.callback?.(value)
     249            1 :   clearWidgetErrors(widgetNode, widget, value)
     250            1 :   canvasStore.canvas?.setDirty(true, true)
     251              : }
     252              : 
     253            0 : function handleResetAllWidgets() {
     254            0 :   useTelemetry()?.trackUiButtonClicked({
     255              :     button_id: 'right_side_panel_reset_all_parameters_clicked',
     256              :     element_group: 'right_side_panel_nodes'
     257              :   })
     258              : 
     259            0 :   for (const { widget, node: widgetNode } of widgetsProp) {
     260              :     const spec = nodeDefStore.getInputSpecForWidget(widgetNode, widget.name)
     261            0 :     const defaultValue = getWidgetDefaultValue(spec)
     262            0 :     if (defaultValue !== undefined) {
     263            0 :       setWidgetValue(widgetNode, widget, defaultValue)
     264              :     }
     265              :   }
     266              : }
     267              : 
     268            1 : function handleWidgetValueUpdate(
     269              :   widgetNode: LGraphNode,
     270              :   widget: IBaseWidget,
     271              :   newValue: WidgetValue
     272              : ) {
     273            1 :   if (newValue === undefined) return
     274            1 :   setWidgetValue(widgetNode, widget, newValue)
     275              : }
     276              : 
     277            0 : function handleWidgetReset(
     278              :   widgetNode: LGraphNode,
     279              :   widget: IBaseWidget,
     280              :   newValue: WidgetValue
     281              : ) {
     282            0 :   setWidgetValue(widgetNode, widget, newValue)
     283              : }
     284              : 
     285          354 : defineExpose({
     286              :   widgetsContainer,
     287              :   rootElement
     288              : })
     289          354 : </script>
     290              : 
     291              : <template>
     292          405 :   <div ref="rootElement">
     293              :     <PropertiesAccordionItem
     294              :       v-model:collapse="collapse"
     295              :       :enable-empty-state
     296              :       :disabled="isEmpty"
     297              :       :tooltip
     298              :       :size="showSeeError ? 'lg' : 'default'"
     299              :     >
     300              :       <template #label>
     301              :         <div class="flex min-w-0 flex-1 flex-wrap items-center gap-2">
     302              :           <span class="flex min-w-0 flex-1 items-center gap-2">
     303              :             <i
     304              :               v-if="nodeHasError"
     305              :               class="icon-[lucide--octagon-alert] text-destructive"
     306              :             />
     307              :             <span :class="cn('truncate', nodeHasError && 'text-destructive')">
     308              :               <slot name="label">
     309              :                 {{ displayLabel }}
     310              :               </slot>
     311              :             </span>
     312              :             <span
     313              :               v-if="parentGroup"
     314              :               class="min-w-11 flex-1 truncate text-right text-xs text-muted-foreground"
     315              :               :title="parentGroup.title"
     316              :             >
     317              :               {{ parentGroup.title }}
     318              :             </span>
     319              :           </span>
     320              :           <Button
     321              :             v-if="showSeeError"
     322              :             variant="secondary"
     323              :             size="sm"
     324              :             class="h-8 shrink-0 rounded-lg text-sm"
     325              :             @click.stop="navigateToErrorTab"
     326              :           >
     327              :             {{ t('rightSidePanel.seeError') }}
     328              :           </Button>
     329              :           <Button
     330              :             v-if="!isEmpty"
     331              :             variant="muted-textonly"
     332              :             size="icon-sm"
     333              :             class="subbutton size-8 shrink-0 hover:text-foreground"
     334              :             :title="t('rightSidePanel.resetAllParameters')"
     335              :             :aria-label="t('rightSidePanel.resetAllParameters')"
     336              :             @click.stop="handleResetAllWidgets"
     337              :           >
     338              :             <i class="icon-[lucide--rotate-ccw]" />
     339              :           </Button>
     340              :           <Button
     341              :             v-if="canShowLocateButton"
     342              :             variant="muted-textonly"
     343              :             size="icon-sm"
     344              :             class="subbutton mr-3 size-8 shrink-0 hover:text-foreground"
     345              :             :title="t('rightSidePanel.locateNode')"
     346              :             :aria-label="t('rightSidePanel.locateNode')"
     347              :             @click.stop="handleLocateNode"
     348              :           >
     349              :             <i class="icon-[lucide--locate]" />
     350              :           </Button>
     351              :         </div>
     352              :       </template>
     353              : 
     354              :       <template #empty><slot name="empty" /></template>
     355              : 
     356              :       <div
     357              :         ref="widgetsContainer"
     358              :         data-testid="section-widgets-list"
     359              :         :data-draggable-ready="draggableList ? 'true' : undefined"
     360              :         class="relative space-y-2 rounded-lg px-4 pt-1"
     361              :       >
     362          456 :         <WidgetItem
     363              :           v-for="{ widget, node } in localWidgets"
     364              :           :key="getStableWidgetRenderKey(widget)"
     365              :           :widget
     366              :           :node
     367              :           :is-draggable
     368              :           :hidden-favorite-indicator
     369              :           :show-node-name
     370              :           :host
     371              :           @update:widget-value="handleWidgetValueUpdate(node, widget, $event)"
     372              :           @reset-to-default="handleWidgetReset(node, widget, $event)"
     373              :         />
     374              :       </div>
     375              :     </PropertiesAccordionItem>
     376              :   </div>
     377              : </template>
        

Generated by: LCOV version 2.0-1