File size: 25,252 Bytes
48f38a9
 
 
 
 
 
 
 
 
 
 
 
79e1a03
48f38a9
 
449bc14
48f38a9
 
 
 
 
 
 
 
 
 
 
 
3119b7f
 
 
 
 
 
48f38a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79e1a03
48f38a9
 
449bc14
48f38a9
 
 
 
266f72f
fd0b72b
48f38a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79e1a03
48f38a9
 
449bc14
48f38a9
 
 
 
266f72f
fd0b72b
48f38a9
 
 
 
 
776d09d
48f38a9
f5f0556
3119b7f
166d6be
fd0b72b
48f38a9
 
 
 
3119b7f
 
48f38a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79e1a03
48f38a9
 
449bc14
48f38a9
 
 
 
266f72f
fd0b72b
48f38a9
 
 
 
 
 
 
 
 
 
776d09d
48f38a9
f5f0556
3119b7f
166d6be
fd0b72b
48f38a9
3119b7f
 
48f38a9
 
 
 
 
 
 
 
 
 
 
 
79e1a03
48f38a9
 
449bc14
48f38a9
 
 
 
3119b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79e1a03
3119b7f
 
 
 
 
266f72f
fd0b72b
3119b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5f0556
3119b7f
 
 
79e1a03
3119b7f
166d6be
266f72f
 
fd0b72b
266f72f
3119b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48f38a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3119b7f
48f38a9
 
 
 
f5f0556
48f38a9
 
 
 
 
 
 
 
 
 
 
3119b7f
48f38a9
 
 
 
 
f4dd995
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import gradio as gr
from .chain_handlers import (
    create_lora_event_handlers,
    create_controlnet_event_handlers,
    create_ipadapter_event_handlers,
    create_embedding_event_handlers,
    create_conditioning_event_handlers,
    create_flux1_ipadapter_event_handlers,
    create_sd3_ipadapter_event_handlers,
    create_style_event_handlers,
    create_reference_latent_event_handlers,
    create_hidream_o1_reference_event_handlers,
    create_sensenova_reference_event_handlers,
    create_joyai_reference_event_handlers,
    create_krea2_identity_edit_event_handlers,
    create_krea2_reference_edit_event_handlers,
    create_qwen_image_edit_event_handlers,
    create_reference_image_event_handlers
)
from .change_handlers import (
    make_update_fn,
    make_model_change_fn,
    run_on_load,
    on_aspect_ratio_change
)
from .run_handlers import create_run_event

def attach_event_handlers(ui_components, demo):
    prefixes = ["imagegen", "txt2img", "img2img", "inpaint", "outpaint", "hires_fix"]
    active_prefixes = [p for p in prefixes if f'base_model_{p}' in ui_components]

    for prefix in active_prefixes:
        task_type = prefix if prefix != "imagegen" else None
        type_comp = ui_components.get(f'type_{prefix}')  # for unified imagegen UI

        arch_comp = ui_components.get(f'model_arch_{prefix}')
        cat_comp = ui_components.get(f'model_cat_{prefix}')
        model_comp = ui_components.get(f'base_model_{prefix}')
        aspect_ratio_comp = ui_components.get(f'aspect_ratio_{prefix}') or ui_components.get(f'{prefix}_aspect_ratio_dropdown')
        width_comp = ui_components.get(f'width_{prefix}') or ui_components.get(f'{prefix}_width')
        height_comp = ui_components.get(f'height_{prefix}') or ui_components.get(f'{prefix}_height')
        
        cn_types_list = ui_components.get(f'controlnet_types_{prefix}', [])
        cn_series_list = ui_components.get(f'controlnet_series_{prefix}', [])
        cn_filepaths_list = ui_components.get(f'controlnet_filepaths_{prefix}', [])
        
        lora_accordion = ui_components.get(f'lora_accordion_{prefix}')
        cn_accordion = ui_components.get(f'controlnet_accordion_{prefix}')
        ipa_accordion = ui_components.get(f'ipadapter_accordion_{prefix}')
        sd3_ipa_accordion = ui_components.get(f'sd3_ipadapter_accordion_{prefix}')
        flux1_ipa_accordion = ui_components.get(f'flux1_ipadapter_accordion_{prefix}')
        style_accordion = ui_components.get(f'style_accordion_{prefix}')
        embedding_accordion = ui_components.get(f'embedding_accordion_{prefix}')
        conditioning_accordion = ui_components.get(f'conditioning_accordion_{prefix}')
        ref_latent_accordion = ui_components.get(f'reference_latent_accordion_{prefix}')
        hidream_o1_ref_accordion = ui_components.get(f'hidream_o1_reference_accordion_{prefix}')
        sensenova_ref_accordion = ui_components.get(f'sensenova_reference_accordion_{prefix}')
        joyai_ref_accordion = ui_components.get(f'joyai_reference_accordion_{prefix}')
        krea2_identity_edit_accordion = ui_components.get(f'krea2_identity_edit_accordion_{prefix}')
        krea2_reference_edit_accordion = ui_components.get(f'krea2_reference_edit_accordion_{prefix}')
        qwen_image_edit_accordion = ui_components.get(f'qwen_image_edit_accordion_{prefix}')
        ref_img_accordion = ui_components.get(f'reference_image_accordion_{prefix}')
        pid_accordion = ui_components.get(f'pid_accordion_{prefix}')
        vae_accordion = ui_components.get(f'vae_accordion_{prefix}')
        qwen_pe_accordion = ui_components.get(f'qwen_image_2_1_prompt_enhancer_accordion_{prefix}')
        ming_pe_accordion = ui_components.get(f'ming_image_prompt_enhancer_accordion_{prefix}')
        
        ipa_preset_list = ui_components.get(f'ipadapter_final_preset_{prefix}')
        
        prompt_comp = ui_components.get(f'prompt_{prefix}') or ui_components.get(f'{prefix}_positive_prompt')
        neg_prompt_comp = ui_components.get(f'neg_prompt_{prefix}') or ui_components.get(f'{prefix}_negative_prompt')
        steps_comp = ui_components.get(f'steps_{prefix}') or ui_components.get(f'{prefix}_steps')
        cfg_comp = ui_components.get(f'cfg_{prefix}') or ui_components.get(f'{prefix}_cfg')
        sampler_comp = ui_components.get(f'sampler_{prefix}') or ui_components.get(f'{prefix}_sampler_name')
        scheduler_comp = ui_components.get(f'scheduler_{prefix}') or ui_components.get(f'{prefix}_scheduler')
        
        extra_comps = [prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp, width_comp, height_comp]
        valid_extra_comps = [c for c in extra_comps if c is not None]
        
        if arch_comp and cat_comp and model_comp:
            outputs = [model_comp, cat_comp]
            if aspect_ratio_comp: outputs.append(aspect_ratio_comp)
            outputs.extend(cn_types_list + cn_series_list + cn_filepaths_list)
            if lora_accordion: outputs.append(lora_accordion)
            if cn_accordion: outputs.append(cn_accordion)
            if ipa_accordion: outputs.append(ipa_accordion)
            if sd3_ipa_accordion: outputs.append(sd3_ipa_accordion)
            if flux1_ipa_accordion: outputs.append(flux1_ipa_accordion)
            if style_accordion: outputs.append(style_accordion)
            if embedding_accordion: outputs.append(embedding_accordion)
            if conditioning_accordion: outputs.append(conditioning_accordion)
            if ref_latent_accordion: outputs.append(ref_latent_accordion)
            if hidream_o1_ref_accordion: outputs.append(hidream_o1_ref_accordion)
            if sensenova_ref_accordion: outputs.append(sensenova_ref_accordion)
            if joyai_ref_accordion: outputs.append(joyai_ref_accordion)
            if krea2_identity_edit_accordion: outputs.append(krea2_identity_edit_accordion)
            if krea2_reference_edit_accordion: outputs.append(krea2_reference_edit_accordion)
            if qwen_image_edit_accordion: outputs.append(qwen_image_edit_accordion)
            if ref_img_accordion: outputs.append(ref_img_accordion)
            if pid_accordion: outputs.append(pid_accordion)
            if vae_accordion: outputs.append(vae_accordion)
            if qwen_pe_accordion: outputs.append(qwen_pe_accordion)
            if ming_pe_accordion: outputs.append(ming_pe_accordion)
            if ipa_preset_list: outputs.append(ipa_preset_list)
            
            outputs.extend(valid_extra_comps)
            
            update_fn = make_update_fn(
                model_comp, cat_comp, aspect_ratio_comp, width_comp, height_comp, 
                cn_types_list, cn_series_list, cn_filepaths_list, 
                ipa_preset_list, lora_accordion, cn_accordion, ipa_accordion, sd3_ipa_accordion, flux1_ipa_accordion, style_accordion, embedding_accordion, conditioning_accordion,
                ref_latent_accordion, hidream_o1_ref_accordion, prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp,
                pid_acc=pid_accordion, vae_acc=vae_accordion, joyai_ref_acc=joyai_ref_accordion, krea2_identity_edit_acc=krea2_identity_edit_accordion, krea2_reference_edit_acc=krea2_reference_edit_accordion, qwen_image_edit_acc=qwen_image_edit_accordion, ref_img_acc=ref_img_accordion, sensenova_ref_acc=sensenova_ref_accordion,
                task_type=task_type, type_comp=type_comp, qwen_pe_acc=qwen_pe_accordion, ming_pe_acc=ming_pe_accordion
            )
            inputs = [arch_comp, cat_comp]
            if aspect_ratio_comp:
                inputs.append(aspect_ratio_comp)
            if type_comp:
                inputs.append(type_comp)
            arch_comp.change(fn=update_fn, inputs=inputs, outputs=outputs)
            cat_comp.change(fn=update_fn, inputs=inputs, outputs=outputs)

        if model_comp:
            outputs2 = []
            if arch_comp: outputs2.append(arch_comp)
            if cat_comp: outputs2.append(cat_comp)
            if aspect_ratio_comp: outputs2.append(aspect_ratio_comp)
            outputs2.extend(cn_types_list + cn_series_list + cn_filepaths_list)
            if lora_accordion: outputs2.append(lora_accordion)
            if cn_accordion: outputs2.append(cn_accordion)
            if ipa_accordion: outputs2.append(ipa_accordion)
            if sd3_ipa_accordion: outputs2.append(sd3_ipa_accordion)
            if flux1_ipa_accordion: outputs2.append(flux1_ipa_accordion)
            if style_accordion: outputs2.append(style_accordion)
            if embedding_accordion: outputs2.append(embedding_accordion)
            if conditioning_accordion: outputs2.append(conditioning_accordion)
            if ref_latent_accordion: outputs2.append(ref_latent_accordion)
            if hidream_o1_ref_accordion: outputs2.append(hidream_o1_ref_accordion)
            if sensenova_ref_accordion: outputs2.append(sensenova_ref_accordion)
            if joyai_ref_accordion: outputs2.append(joyai_ref_accordion)
            if krea2_identity_edit_accordion: outputs2.append(krea2_identity_edit_accordion)
            if krea2_reference_edit_accordion: outputs2.append(krea2_reference_edit_accordion)
            if qwen_image_edit_accordion: outputs2.append(qwen_image_edit_accordion)
            if ref_img_accordion: outputs2.append(ref_img_accordion)
            if pid_accordion: outputs2.append(pid_accordion)
            if vae_accordion: outputs2.append(vae_accordion)
            if qwen_pe_accordion: outputs2.append(qwen_pe_accordion)
            if ming_pe_accordion: outputs2.append(ming_pe_accordion)
            if ipa_preset_list: outputs2.append(ipa_preset_list)
            
            outputs2.extend(valid_extra_comps)
            
            if outputs2:
                inputs2 = [model_comp]
                if arch_comp: inputs2.append(arch_comp)
                if cat_comp: inputs2.append(cat_comp)
                if aspect_ratio_comp: inputs2.append(aspect_ratio_comp)
                change_fn = make_model_change_fn(
                    cat_comp, aspect_ratio_comp, width_comp, height_comp, 
                    cn_types_list, cn_series_list, cn_filepaths_list, 
                    arch_comp, ipa_preset_list, lora_accordion, cn_accordion, ipa_accordion, sd3_ipa_accordion, flux1_ipa_accordion, style_accordion, embedding_accordion, conditioning_accordion,
                    ref_latent_accordion, hidream_o1_ref_accordion, prompt_comp, neg_prompt_comp, steps_comp, cfg_comp, sampler_comp, scheduler_comp,
                    pid_acc=pid_accordion, vae_acc=vae_accordion, joyai_ref_acc=joyai_ref_accordion, krea2_identity_edit_acc=krea2_identity_edit_accordion, krea2_reference_edit_acc=krea2_reference_edit_accordion, qwen_image_edit_acc=qwen_image_edit_accordion, ref_img_acc=ref_img_accordion, sensenova_ref_acc=sensenova_ref_accordion,
                    task_type=task_type, type_comp=type_comp, qwen_pe_acc=qwen_pe_accordion, ming_pe_acc=ming_pe_accordion
                )
                if type_comp:
                    inputs2.append(type_comp)
                model_comp.change(fn=change_fn, inputs=inputs2, outputs=outputs2)

        create_lora_event_handlers(prefix, ui_components)
        create_controlnet_event_handlers(prefix, ui_components)
        create_ipadapter_event_handlers(prefix, ui_components)
        create_embedding_event_handlers(prefix, ui_components)
        create_conditioning_event_handlers(prefix, ui_components)
        create_flux1_ipadapter_event_handlers(prefix, ui_components)
        create_sd3_ipadapter_event_handlers(prefix, ui_components)
        create_style_event_handlers(prefix, ui_components)
        create_reference_latent_event_handlers(prefix, ui_components)
        create_hidream_o1_reference_event_handlers(prefix, ui_components)
        create_sensenova_reference_event_handlers(prefix, ui_components)
        create_joyai_reference_event_handlers(prefix, ui_components)
        create_krea2_identity_edit_event_handlers(prefix, ui_components)
        create_krea2_reference_edit_event_handlers(prefix, ui_components)
        create_qwen_image_edit_event_handlers(prefix, ui_components)
        create_reference_image_event_handlers(prefix, ui_components)
        create_run_event(prefix, task_type, ui_components)

    # Dynamic Type toggle event for unified imagegen UI
    if 'type_imagegen' in ui_components:
        def on_type_change(type_val, model_name):
            from core.settings import MODEL_TYPE_MAP, FEATURES_CONFIG, TASK_FEATURES_CONFIG, ARCHITECTURES_CONFIG
            is_txt2img = (type_val == "Txt2Img")
            is_img2img = (type_val == "Img2Img")
            is_inpaint = (type_val == "Inpaint")
            is_outpaint = (type_val == "Outpaint")
            is_hires_fix = (type_val == "Hires. Fix")
            
            denoise_val = 0.7 if is_img2img else (1.0 if is_inpaint else (0.55 if is_hires_fix else 0.7))
            run_text = "Run Inpaint" if is_inpaint else ("Run Outpaint" if is_outpaint else ("Run Hires. Fix" if is_hires_fix else "Run"))
            
            prompt_lines = 6 if is_inpaint else 3
            gallery_cols = 2 if is_txt2img else 1
            gallery_height = 627 if is_txt2img else (505 if is_img2img else (510 if is_inpaint else (685 if is_outpaint else 610)))
            
            updates = {
                ui_components['image_input_col_imagegen']: gr.update(visible=not is_txt2img),
                ui_components['input_image_imagegen']: gr.update(visible=(is_img2img or is_outpaint or is_hires_fix)),
                ui_components['inpaint_box_imagegen']: gr.update(visible=is_inpaint),
                ui_components['prompt_imagegen']: gr.update(lines=prompt_lines),
                ui_components['neg_prompt_imagegen']: gr.update(lines=prompt_lines),
                ui_components['aspect_ratio_row_imagegen']: gr.update(visible=is_txt2img),
                ui_components['width_height_row_imagegen']: gr.update(visible=is_txt2img),
                ui_components['denoise_row_imagegen']: gr.update(visible=(is_img2img or is_inpaint or is_hires_fix)),
                ui_components['denoise_imagegen']: gr.update(value=denoise_val),
                ui_components['grow_mask_by_imagegen']: gr.update(visible=is_inpaint),
                ui_components['outpaint_pads_col_imagegen']: gr.update(visible=is_outpaint),
                ui_components['hires_upscaler_row_imagegen']: gr.update(visible=is_hires_fix),
                ui_components['run_imagegen']: gr.update(value=run_text),
                ui_components['result_imagegen']: gr.update(columns=gallery_cols, height=gallery_height),
            }

            # Re-evaluate feature accordion visibility based on task + architecture intersection
            type_map = {'Txt2Img': 'txt2img', 'Img2Img': 'img2img', 'Inpaint': 'inpaint', 'Outpaint': 'outpaint', 'Hires. Fix': 'hires_fix'}
            current_task = type_map.get(type_val, 'txt2img')
            
            m_type = MODEL_TYPE_MAP.get(model_name, "SDXL") if model_name else "SDXL"
            architectures_dict = ARCHITECTURES_CONFIG.get('architectures', {})
            arch_model_type = architectures_dict.get(m_type, {}).get("model_type", m_type.lower().replace(" ", "").replace(".", ""))
            
            arch_enabled_chains = FEATURES_CONFIG.get(arch_model_type, {}).get('enabled_chains', [])
            task_enabled_chains = TASK_FEATURES_CONFIG.get(current_task, {}).get('enabled_chains', [])
            enabled_chains = [c for c in arch_enabled_chains if c in task_enabled_chains]

            prefix = 'imagegen'
            accordion_chain_map = {
                f'lora_accordion_{prefix}': 'lora',
                f'controlnet_accordion_{prefix}': 'controlnet',
                f'ipadapter_accordion_{prefix}': 'ipadapter',
                f'sd3_ipadapter_accordion_{prefix}': 'sd3_ipadapter',
                f'flux1_ipadapter_accordion_{prefix}': 'flux1_ipadapter',
                f'style_accordion_{prefix}': 'style',
                f'embedding_accordion_{prefix}': 'embedding',
                f'conditioning_accordion_{prefix}': 'conditioning',
                f'reference_latent_accordion_{prefix}': 'reference_latent',
                f'hidream_o1_reference_accordion_{prefix}': 'hidream_o1_reference',
                f'sensenova_reference_accordion_{prefix}': 'sensenova_reference',
                f'joyai_reference_accordion_{prefix}': 'joyai_image',
                f'krea2_identity_edit_accordion_{prefix}': 'krea2_identity_edit',
                f'krea2_reference_edit_accordion_{prefix}': 'krea2_style_reference',
                f'qwen_image_edit_accordion_{prefix}': 'qwen_image_edit',
                f'reference_image_accordion_{prefix}': 'reference_image',
                f'qwen_image_2_1_prompt_enhancer_accordion_{prefix}': 'qwen_image_2_1_prompt_enhancer',
                f'ming_image_prompt_enhancer_accordion_{prefix}': 'ming_image_prompt_enhancer',
                f'pid_accordion_{prefix}': 'pid',
                f'vae_accordion_{prefix}': 'vae',
            }
            for comp_key, chain_name in accordion_chain_map.items():
                comp = ui_components.get(comp_key)
                if comp:
                    updates[comp] = gr.update(visible=(chain_name in enabled_chains))

            return updates

        type_change_outputs = [
            ui_components['image_input_col_imagegen'],
            ui_components['input_image_imagegen'],
            ui_components['inpaint_box_imagegen'],
            ui_components['prompt_imagegen'],
            ui_components['neg_prompt_imagegen'],
            ui_components['aspect_ratio_row_imagegen'],
            ui_components['width_height_row_imagegen'],
            ui_components['denoise_row_imagegen'],
            ui_components['denoise_imagegen'],
            ui_components['grow_mask_by_imagegen'],
            ui_components['outpaint_pads_col_imagegen'],
            ui_components['hires_upscaler_row_imagegen'],
            ui_components['run_imagegen'],
            ui_components['result_imagegen'],
        ]
        # Add feature accordion outputs for task-based visibility
        accordion_keys = [
            'lora_accordion_imagegen', 'controlnet_accordion_imagegen',
            'ipadapter_accordion_imagegen',
            'sd3_ipadapter_accordion_imagegen', 'flux1_ipadapter_accordion_imagegen',
            'style_accordion_imagegen', 'embedding_accordion_imagegen',
            'conditioning_accordion_imagegen', 'reference_latent_accordion_imagegen',
            'hidream_o1_reference_accordion_imagegen', 'sensenova_reference_accordion_imagegen', 'joyai_reference_accordion_imagegen',
            'krea2_identity_edit_accordion_imagegen', 'krea2_reference_edit_accordion_imagegen',
            'qwen_image_edit_accordion_imagegen',
            'reference_image_accordion_imagegen',
            'qwen_image_2_1_prompt_enhancer_accordion_imagegen',
            'ming_image_prompt_enhancer_accordion_imagegen',
            'pid_accordion_imagegen', 'vae_accordion_imagegen',
        ]
        for key in accordion_keys:
            if key in ui_components:
                type_change_outputs.append(ui_components[key])
        ui_components['type_imagegen'].change(
            fn=on_type_change,
            inputs=[ui_components['type_imagegen'], ui_components['base_model_imagegen']],
            outputs=type_change_outputs,
            show_progress=False
        )

    # Fullscreen view toggle for imagegen
    if 'view_mode_imagegen' in ui_components:
        def toggle_imagegen_fullscreen_view(view_mode):
            is_fullscreen = (view_mode == "Fullscreen View")
            other_elements_visible = not is_fullscreen
            editor_height = 800 if is_fullscreen else 272
            
            updates = {
                ui_components['type_imagegen']: gr.update(visible=other_elements_visible),
                ui_components['prompts_col_imagegen']: gr.update(visible=other_elements_visible),
                ui_components['params_and_gallery_row_imagegen']: gr.update(visible=other_elements_visible),
                ui_components['accordion_wrapper_imagegen']: gr.update(visible=other_elements_visible),
                ui_components['input_image_dict_imagegen']: gr.update(height=editor_height),
            }
            
            model_and_run_rows = ui_components.get('model_and_run_row_imagegen', [])
            for row in model_and_run_rows:
                updates[row] = gr.update(visible=other_elements_visible)
            
            return updates
        
        output_components = [ui_components['type_imagegen']]
        model_and_run_rows = ui_components.get('model_and_run_row_imagegen', [])
        if isinstance(model_and_run_rows, list):
            output_components.extend(model_and_run_rows)
        else:
            output_components.append(model_and_run_rows)
        
        output_components.extend([
            ui_components['prompts_col_imagegen'],
            ui_components['params_and_gallery_row_imagegen'], 
            ui_components['accordion_wrapper_imagegen'],
            ui_components['input_image_dict_imagegen']
        ])
        
        ui_components['view_mode_imagegen'].change(
            fn=toggle_imagegen_fullscreen_view, 
            inputs=[ui_components['view_mode_imagegen']], 
            outputs=output_components, 
            show_progress=False
        )

    if 'view_mode_inpaint' in ui_components:
        def toggle_inpaint_fullscreen_view(view_mode):
            is_fullscreen = (view_mode == "Fullscreen View")
            other_elements_visible = not is_fullscreen
            editor_height = 800 if is_fullscreen else 272
            
            updates = {
                ui_components['prompts_column_inpaint']: gr.update(visible=other_elements_visible),
                ui_components['params_and_gallery_row_inpaint']: gr.update(visible=other_elements_visible),
                ui_components['accordion_wrapper_inpaint']: gr.update(visible=other_elements_visible),
                ui_components['input_image_dict_inpaint']: gr.update(height=editor_height),
            }
            
            model_and_run_rows = ui_components.get('model_and_run_row_inpaint', [])
            for row in model_and_run_rows:
                updates[row] = gr.update(visible=other_elements_visible)
            
            return updates
        
        output_components = []
        model_and_run_rows = ui_components.get('model_and_run_row_inpaint', [])
        if isinstance(model_and_run_rows, list):
            output_components.extend(model_and_run_rows)
        else:
            output_components.append(model_and_run_rows)
        
        output_components.extend([
            ui_components['prompts_column_inpaint'],
            ui_components['params_and_gallery_row_inpaint'], 
            ui_components['accordion_wrapper_inpaint'],
            ui_components['input_image_dict_inpaint']
        ])
        
        ui_components['view_mode_inpaint'].change(
            fn=toggle_inpaint_fullscreen_view, 
            inputs=[ui_components['view_mode_inpaint']], 
            outputs=output_components, 
            show_progress=False
        )

    all_load_outputs = []
    for prefix in prefixes:
        if f'controlnet_types_{prefix}' in ui_components:
            all_load_outputs.extend(ui_components[f'controlnet_types_{prefix}'])
            all_load_outputs.extend(ui_components[f'controlnet_series_{prefix}'])
            all_load_outputs.extend(ui_components[f'controlnet_filepaths_{prefix}'])

        if f'ipadapter_final_preset_{prefix}' in ui_components:
            all_load_outputs.extend(ui_components[f'ipadapter_lora_strengths_{prefix}'])
            all_load_outputs.append(ui_components[f'ipadapter_final_preset_{prefix}'])
            all_load_outputs.append(ui_components[f'ipadapter_final_lora_strength_{prefix}'])

    if all_load_outputs:
        demo.load(
            fn=lambda: run_on_load(ui_components),
            outputs=all_load_outputs
        )

    for prefix in prefixes:
        aspect_ratio_dropdown = ui_components.get(f'aspect_ratio_{prefix}') or ui_components.get(f'{prefix}_aspect_ratio_dropdown')
        width_component = ui_components.get(f'width_{prefix}') or ui_components.get(f'{prefix}_width')
        height_component = ui_components.get(f'height_{prefix}') or ui_components.get(f'{prefix}_height')
        model_dropdown = ui_components.get(f'base_model_{prefix}')
        if aspect_ratio_dropdown and width_component and height_component and model_dropdown:
            aspect_ratio_dropdown.change(fn=on_aspect_ratio_change, inputs=[aspect_ratio_dropdown, model_dropdown], outputs=[width_component, height_component], show_progress=False)