Rewrite/Test rounding logic of numeric widgets (#2758)

This commit is contained in:
Chenlei Hu
2025-02-27 17:52:16 -05:00
committed by GitHub
parent cb4a5b88fc
commit fc39ce9624
5 changed files with 193 additions and 24 deletions

View File

@@ -1,11 +1,27 @@
import type { LGraphNode } from '@comfyorg/litegraph'
import type { INumericWidget } from '@comfyorg/litegraph/dist/types/widgets'
import _ from 'lodash'
import type { InputSpec } from '@/schemas/nodeDefSchema'
import type { ComfyWidgetConstructor } from '@/scripts/widgets'
import { useSettingStore } from '@/stores/settingStore'
import { getNumberDefaults } from '@/utils/mathUtil'
function onFloatValueChange(this: INumericWidget, v: number) {
this.value = this.options.round
? _.clamp(
Math.round((v + Number.EPSILON) / this.options.round) *
this.options.round,
this.options.min ?? -Infinity,
this.options.max ?? Infinity
)
: v
}
export const _for_testing = {
onFloatValueChange
}
export const useFloatWidget = () => {
const widgetConstructor: ComfyWidgetConstructor = (
node: LGraphNode,
@@ -40,16 +56,7 @@ export const useFloatWidget = () => {
widgetType,
inputName,
val,
function (this: INumericWidget, v: number) {
if (config.round) {
this.value =
Math.round((v + Number.EPSILON) / config.round) * config.round
if (this.value > config.max) this.value = config.max
if (this.value < config.min) this.value = config.min
} else {
this.value = v
}
},
onFloatValueChange,
config
)
}

View File

@@ -10,6 +10,29 @@ import {
import { useSettingStore } from '@/stores/settingStore'
import { getNumberDefaults } from '@/utils/mathUtil'
function onValueChange(this: INumericWidget, v: number) {
// For integers, always round to the nearest step
// step === 0 is invalid, assign 1 if options.step is 0
const step = this.options.step2 || 1
if (step === 1) {
// Simple case: round to nearest integer
this.value = Math.round(v)
} else {
// Round to nearest multiple of step
// First, determine if min value creates an offset
const min = this.options.min ?? 0
const offset = min % step
// Round to nearest step, accounting for offset
this.value = Math.round((v - offset) / step) * step + offset
}
}
export const _for_testing = {
onValueChange
}
export const useIntWidget = () => {
const widgetConstructor: ComfyWidgetConstructor = (
node: LGraphNode,
@@ -35,20 +58,7 @@ export const useIntWidget = () => {
config.precision = 0
const result = {
widget: node.addWidget(
widgetType,
inputName,
val,
function (this: INumericWidget, v: number) {
const s = this.options.step2 || 1
let sh = (this.options.min ?? 0) % s
if (isNaN(sh)) {
sh = 0
}
this.value = Math.round((v - sh) / s) * s + sh
},
config
)
widget: node.addWidget(widgetType, inputName, val, onValueChange, config)
}
if (inputData[1]?.control_after_generate) {

View File

@@ -17,6 +17,10 @@ declare module '@comfyorg/litegraph/dist/types/widgets' {
* - If true or undefined, the value will be included in both the API workflow and graph state
*/
serialize?: boolean
/**
* Rounding value for numeric float widgets.
*/
round?: number
}
interface IBaseWidget {