mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-26 09:44:06 +00:00
[Refactor] useIntWidget composable (#2507)
This commit is contained in:
49
src/composables/widgets/useIntWidget.ts
Normal file
49
src/composables/widgets/useIntWidget.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import type { LGraphNode } from '@comfyorg/litegraph'
|
||||
import type { INumericWidget } from '@comfyorg/litegraph/dist/types/widgets'
|
||||
|
||||
import type { ComfyWidgetConstructor } from '@/scripts/widgets'
|
||||
import { useSettingStore } from '@/stores/settingStore'
|
||||
import { InputSpec } from '@/types/apiTypes'
|
||||
import { getNumberDefaults } from '@/utils/mathUtil'
|
||||
|
||||
export const useIntWidget = () => {
|
||||
const widgetConstructor: ComfyWidgetConstructor = (
|
||||
node: LGraphNode,
|
||||
inputName: string,
|
||||
inputData: InputSpec
|
||||
) => {
|
||||
const settingStore = useSettingStore()
|
||||
const sliderEnabled = !settingStore.get('Comfy.DisableSliders')
|
||||
const inputOptions = inputData[1]
|
||||
const widgetType = sliderEnabled
|
||||
? inputOptions.display === 'slider'
|
||||
? 'slider'
|
||||
: 'number'
|
||||
: 'number'
|
||||
|
||||
const { val, config } = getNumberDefaults(inputOptions, {
|
||||
defaultStep: 1,
|
||||
precision: 0,
|
||||
enableRounding: true
|
||||
})
|
||||
config.precision = 0
|
||||
|
||||
return {
|
||||
widget: node.addWidget(
|
||||
widgetType,
|
||||
inputName,
|
||||
val,
|
||||
function (this: INumericWidget, v: number) {
|
||||
const s = (this.options.step ?? 1) / 10
|
||||
let sh = (this.options.min ?? 0) % s
|
||||
if (isNaN(sh)) {
|
||||
sh = 0
|
||||
}
|
||||
this.value = Math.round((v - sh) / s) * s + sh
|
||||
},
|
||||
config
|
||||
)
|
||||
}
|
||||
}
|
||||
return widgetConstructor
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import TiptapStarterKit from '@tiptap/starter-kit'
|
||||
import { Markdown as TiptapMarkdown } from 'tiptap-markdown'
|
||||
|
||||
import { useFloatWidget } from '@/composables/widgets/useFloatWidget'
|
||||
import { useIntWidget } from '@/composables/widgets/useIntWidget'
|
||||
import { useRemoteWidget } from '@/composables/widgets/useRemoteWidget'
|
||||
import { useStringWidget } from '@/composables/widgets/useStringWidget'
|
||||
import { useSettingStore } from '@/stores/settingStore'
|
||||
@@ -436,9 +437,7 @@ export const ComfyWidgets: Record<string, ComfyWidgetConstructor> = {
|
||||
'INT:seed': seedWidget,
|
||||
'INT:noise_seed': seedWidget,
|
||||
FLOAT: useFloatWidget(),
|
||||
INT(node, inputName, inputData: InputSpec, app) {
|
||||
return createIntWidget(node, inputName, inputData, app)
|
||||
},
|
||||
INT: useIntWidget(),
|
||||
BOOLEAN(node, inputName, inputData) {
|
||||
let defaultVal = false
|
||||
let options = {}
|
||||
|
||||
Reference in New Issue
Block a user