1
1
import { api } from "./api.js"
2
2
import "./domWidget.js" ;
3
3
4
+ let controlValueRunBefore = false ;
5
+ export function updateControlWidgetLabel ( widget ) {
6
+ let replacement = "after" ;
7
+ let find = "before" ;
8
+ if ( controlValueRunBefore ) {
9
+ [ find , replacement ] = [ replacement , find ]
10
+ }
11
+ widget . label = ( widget . label ?? widget . name ) . replace ( find , replacement ) ;
12
+ }
13
+
14
+ const IS_CONTROL_WIDGET = Symbol ( ) ;
15
+ const HAS_EXECUTED = Symbol ( ) ;
16
+
4
17
function getNumberDefaults ( inputData , defaultStep , precision , enable_rounding ) {
5
18
let defaultVal = inputData [ 1 ] [ "default" ] ;
6
19
let { min, max, step, round} = inputData [ 1 ] ;
@@ -62,6 +75,8 @@ export function addValueControlWidgets(node, targetWidget, defaultValue = "rando
62
75
serialize : false , // Don't include this in prompt.
63
76
}
64
77
) ;
78
+ valueControl [ IS_CONTROL_WIDGET ] = true ;
79
+ updateControlWidgetLabel ( valueControl ) ;
65
80
widgets . push ( valueControl ) ;
66
81
67
82
const isCombo = targetWidget . type === "combo" ;
@@ -76,10 +91,12 @@ export function addValueControlWidgets(node, targetWidget, defaultValue = "rando
76
91
serialize : false , // Don't include this in prompt.
77
92
}
78
93
) ;
94
+ updateControlWidgetLabel ( comboFilter ) ;
95
+
79
96
widgets . push ( comboFilter ) ;
80
97
}
81
98
82
- valueControl . afterQueued = ( ) => {
99
+ const applyWidgetControl = ( ) => {
83
100
var v = valueControl . value ;
84
101
85
102
if ( isCombo && v !== "fixed" ) {
@@ -159,6 +176,23 @@ export function addValueControlWidgets(node, targetWidget, defaultValue = "rando
159
176
targetWidget . callback ( targetWidget . value ) ;
160
177
}
161
178
} ;
179
+
180
+ valueControl . beforeQueued = ( ) => {
181
+ if ( controlValueRunBefore ) {
182
+ // Don't run on first execution
183
+ if ( valueControl [ HAS_EXECUTED ] ) {
184
+ applyWidgetControl ( ) ;
185
+ }
186
+ }
187
+ valueControl [ HAS_EXECUTED ] = true ;
188
+ } ;
189
+
190
+ valueControl . afterQueued = ( ) => {
191
+ if ( ! controlValueRunBefore ) {
192
+ applyWidgetControl ( ) ;
193
+ }
194
+ } ;
195
+
162
196
return widgets ;
163
197
} ;
164
198
@@ -224,6 +258,34 @@ function isSlider(display, app) {
224
258
return ( display === "slider" ) ? "slider" : "number"
225
259
}
226
260
261
+ export function initWidgets ( app ) {
262
+ app . ui . settings . addSetting ( {
263
+ id : "Comfy.WidgetControlMode" ,
264
+ name : "Widget Value Control Mode" ,
265
+ type : "combo" ,
266
+ defaultValue : "after" ,
267
+ options : [ "before" , "after" ] ,
268
+ tooltip : "Controls when widget values are updated (randomize/increment/decrement), either before the prompt is queued or after." ,
269
+ onChange ( value ) {
270
+ controlValueRunBefore = value === "before" ;
271
+ for ( const n of app . graph . _nodes ) {
272
+ if ( ! n . widgets ) continue ;
273
+ for ( const w of n . widgets ) {
274
+ if ( w [ IS_CONTROL_WIDGET ] ) {
275
+ updateControlWidgetLabel ( w ) ;
276
+ if ( w . linkedWidgets ) {
277
+ for ( const l of w . linkedWidgets ) {
278
+ updateControlWidgetLabel ( l ) ;
279
+ }
280
+ }
281
+ }
282
+ }
283
+ }
284
+ app . graph . setDirtyCanvas ( true ) ;
285
+ } ,
286
+ } ) ;
287
+ }
288
+
227
289
export const ComfyWidgets = {
228
290
"INT:seed" : seedWidget ,
229
291
"INT:noise_seed" : seedWidget ,
0 commit comments