Skip to content

Commit

Permalink
[Refactor] Move Comfy.WidgetControlMode to coreSettings (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei authored Dec 28, 2024
1 parent 74361ee commit feabd3f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
18 changes: 18 additions & 0 deletions src/components/graph/GraphCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { useCommandStore } from '@/stores/commandStore'
import { useWorkflowService } from '@/services/workflowService'
import { useColorPaletteStore } from '@/stores/workspace/colorPaletteStore'
import { useColorPaletteService } from '@/services/colorPaletteService'
import { IS_CONTROL_WIDGET, updateControlWidgetLabel } from '@/scripts/widgets'
const emit = defineEmits(['ready'])
const canvasRef = ref<HTMLCanvasElement | null>(null)
Expand Down Expand Up @@ -195,6 +196,23 @@ watchEffect(() => {
LiteGraph.alwaysSnapToGrid = settingStore.get('pysssss.SnapToGrid')
})
watch(settingStore.get('Comfy.WidgetControlMode'), () => {
for (const n of comfyApp.graph.nodes) {
if (!n.widgets) continue
for (const w of n.widgets) {
if (w[IS_CONTROL_WIDGET]) {
updateControlWidgetLabel(w)
if (w.linkedWidgets) {
for (const l of w.linkedWidgets) {
updateControlWidgetLabel(l)
}
}
}
}
}
comfyApp.graph.setDirtyCanvas(true)
})
watchEffect(() => {
if (!canvasStore.canvas) return
Expand Down
11 changes: 11 additions & 0 deletions src/constants/coreSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,5 +679,16 @@ export const CORE_SETTINGS: SettingParams[] = [
type: 'hidden',
defaultValue: {} as ColorPalettes,
versionModified: '1.6.7'
},
{
id: 'Comfy.WidgetControlMode',
category: ['Comfy', 'Node Widget', 'WidgetControlMode'],
name: 'Widget control mode',
tooltip:
'Controls when widget values are updated (randomize/increment/decrement), either before the prompt is queued or after.',
type: 'combo',
defaultValue: 'after',
options: ['before', 'after'],
versionModified: '1.6.10'
}
]
7 changes: 1 addition & 6 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// @ts-strict-ignore
import {
type ComfyWidgetConstructor,
ComfyWidgets,
initWidgets
} from './widgets'
import { type ComfyWidgetConstructor, ComfyWidgets } from './widgets'
import { ComfyUI, $el } from './ui'
import { api, type ComfyApi } from './api'
import { defaultGraph } from './defaultGraph'
Expand Down Expand Up @@ -1048,7 +1044,6 @@ export class ComfyApp {

await useExtensionService().invokeExtensionsAsync('init')
await this.registerNodes()
initWidgets(this)

// Load previous workflow
let restored = false
Expand Down
43 changes: 8 additions & 35 deletions src/scripts/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ export type ComfyWidgetConstructor = (
widgetName?: string
) => { widget: IWidget; minWidth?: number; minHeight?: number }

let controlValueRunBefore = false
function controlValueRunBefore() {
return useSettingStore().get('Comfy.WidgetControlMode') === 'before'
}

export function updateControlWidgetLabel(widget) {
let replacement = 'after'
let find = 'before'
if (controlValueRunBefore) {
if (controlValueRunBefore()) {
;[find, replacement] = [replacement, find]
}
widget.label = (widget.label ?? widget.name).replace(find, replacement)
}

const IS_CONTROL_WIDGET = Symbol()
export const IS_CONTROL_WIDGET = Symbol()
const HAS_EXECUTED = Symbol()

function getNumberDefaults(
Expand Down Expand Up @@ -252,7 +255,7 @@ export function addValueControlWidgets(
}

valueControl.beforeQueued = () => {
if (controlValueRunBefore) {
if (controlValueRunBefore()) {
// Don't run on first execution
if (valueControl[HAS_EXECUTED]) {
applyWidgetControl()
Expand All @@ -262,7 +265,7 @@ export function addValueControlWidgets(
}

valueControl.afterQueued = () => {
if (!controlValueRunBefore) {
if (!controlValueRunBefore()) {
applyWidgetControl()
}
}
Expand Down Expand Up @@ -465,36 +468,6 @@ function isSlider(display, app) {
return display === 'slider' ? 'slider' : 'number'
}

export function initWidgets(app) {
app.ui.settings.addSetting({
id: 'Comfy.WidgetControlMode',
category: ['Comfy', 'Node Widget', 'WidgetControlMode'],
name: 'Widget control mode',
tooltip:
'Controls when widget values are updated (randomize/increment/decrement), either before the prompt is queued or after.',
type: 'combo',
defaultValue: 'after',
options: ['before', 'after'],
onChange(value) {
controlValueRunBefore = value === 'before'
for (const n of app.graph.nodes) {
if (!n.widgets) continue
for (const w of n.widgets) {
if (w[IS_CONTROL_WIDGET]) {
updateControlWidgetLabel(w)
if (w.linkedWidgets) {
for (const l of w.linkedWidgets) {
updateControlWidgetLabel(l)
}
}
}
}
}
app.graph.setDirtyCanvas(true)
}
})
}

export const ComfyWidgets: Record<string, ComfyWidgetConstructor> = {
'INT:seed': seedWidget,
'INT:noise_seed': seedWidget,
Expand Down

0 comments on commit feabd3f

Please sign in to comment.