Line data Source code
1 : import { clamp } from 'es-toolkit/compat'
2 :
3 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
4 : import type { INumericWidget } from '@comfyorg/graph-model/widgets'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import { isFloatInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
7 : import type { InputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
8 : import type { ComfyWidgetConstructorV2 } from '@/types/widgetConstructor'
9 : import { addValueControlWidget } from '@/core/graph/widgets/valueControlWidgets'
10 : import { transformInputSpecV2ToV1 } from '@comfyorg/object-info-parser/migration'
11 :
12 78 : function onFloatValueChange(this: INumericWidget, v: number) {
13 78 : const round = this.options.round
14 78 : if (round) {
15 78 : const precision =
16 : this.options.precision ?? Math.max(0, -Math.floor(Math.log10(round)))
17 78 : const rounded = Math.round(v / round) * round
18 78 : this.value = clamp(
19 : Number(rounded.toFixed(precision)),
20 : this.options.min ?? -Infinity,
21 : this.options.max ?? Infinity
22 : )
23 : } else {
24 0 : this.value = v
25 : }
26 : }
27 :
28 : export const _for_testing = {
29 : onFloatValueChange
30 : }
31 :
32 2356 : export const useFloatWidget = () => {
33 4711 : const widgetConstructor: ComfyWidgetConstructorV2 = (
34 : node: LGraphNode,
35 : inputSpec: InputSpec
36 : ) => {
37 7257 : if (!isFloatInputSpec(inputSpec)) {
38 0 : throw new Error(`Invalid input data: ${inputSpec}`)
39 : }
40 :
41 7257 : const settingStore = useSettingStore()
42 7257 : const sliderEnabled = !settingStore.get('Comfy.DisableSliders')
43 :
44 7257 : const display_type = inputSpec.display
45 7257 : const widgetType =
46 : display_type == 'gradientslider'
47 : ? 'gradientslider'
48 : : sliderEnabled && display_type == 'slider'
49 : ? 'slider'
50 : : display_type == 'knob'
51 : ? 'knob'
52 : : 'number'
53 :
54 7257 : const step = inputSpec.step ?? 0.5
55 7257 : const precision =
56 : settingStore.get('Comfy.FloatRoundingPrecision') ||
57 : Math.max(0, -Math.floor(Math.log10(step)))
58 7257 : const enableRounding = !settingStore.get('Comfy.DisableFloatRounding')
59 :
60 : /** Assertion {@link inputSpec.default} */
61 7257 : const defaultValue = (inputSpec.default as number | undefined) ?? 0
62 7257 : const widget = node.addWidget(
63 : widgetType,
64 : inputSpec.name,
65 : defaultValue,
66 : onFloatValueChange,
67 : {
68 : min: inputSpec.min ?? 0,
69 : max: inputSpec.max ?? 2048,
70 : round:
71 : enableRounding && precision && !inputSpec.round
72 : ? Math.pow(10, -precision)
73 : : (inputSpec.round as number),
74 : /** @deprecated Use step2 instead. The 10x value is a legacy implementation. */
75 : step: step * 10.0,
76 : step2: step,
77 : precision,
78 : ...(inputSpec.gradient_stops
79 : ? { gradient_stops: inputSpec.gradient_stops }
80 : : {})
81 : }
82 : )
83 :
84 7257 : if (inputSpec.control_after_generate) {
85 : const controlWidget = addValueControlWidget(
86 : node,
87 : widget,
88 : 'fixed',
89 : undefined,
90 : undefined,
91 : transformInputSpecV2ToV1(inputSpec)
92 : )
93 0 : widget.linkedWidgets = [controlWidget]
94 : }
95 :
96 7257 : return widget
97 : }
98 :
99 4711 : return widgetConstructor
100 : }
|