From 7201d940a4fe664beb9662fadbeade4ee1d788f7 Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:27:48 -0500 Subject: [PATCH] Improve frontend responsiveness for some buttons --- javascript/ui.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ modules/ui.py | 10 ++++++---- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/javascript/ui.js b/javascript/ui.js index 4a440193..5311e7bc 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -361,3 +361,51 @@ function selectCheckpoint(name){ desiredCheckpointName = name; gradioApp().getElementById('change_checkpoint').click() } + +function setRandomSeed(target_interface) { + let seed = gradioApp().querySelector(`#${target_interface}_seed input`); + if (!seed) { + return []; + } + seed.value = "-1"; + seed.dispatchEvent(new Event("input")); + return []; +} + +function setRandomSubseed(target_interface) { + let subseed = gradioApp().querySelector(`#${target_interface}_subseed input`); + if (!subseed) { + return []; + } + subseed.value = "-1"; + subseed.dispatchEvent(new Event("input")); + return []; +} + +function switchWidthHeightTxt2Img() { + let width = gradioApp().querySelector("#txt2img_width input[type=number]"); + let height = gradioApp().querySelector("#txt2img_height input[type=number]"); + if (!width || !height) { + return []; + } + let tmp = width.value; + width.value = height.value; + height.value = tmp; + width.dispatchEvent(new Event("input")); + height.dispatchEvent(new Event("input")); + return []; +} + +function switchWidthHeightImg2Img() { + let width = gradioApp().querySelector("#img2img_width input[type=number]"); + let height = gradioApp().querySelector("#img2img_height input[type=number]"); + if (!width || !height) { + return []; + } + let tmp = width.value; + width.value = height.value; + height.value = tmp; + width.dispatchEvent(new Event("input")); + height.dispatchEvent(new Event("input")); + return []; +} diff --git a/modules/ui.py b/modules/ui.py index 627fbe0b..5c693b7a 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -192,8 +192,9 @@ def create_seed_inputs(target_interface): seed_resize_from_w = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize seed from width", value=0, elem_id=target_interface + '_seed_resize_from_w') seed_resize_from_h = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize seed from height", value=0, elem_id=target_interface + '_seed_resize_from_h') - random_seed.click(fn=lambda: -1, show_progress=False, inputs=[], outputs=[seed]) - random_subseed.click(fn=lambda: -1, show_progress=False, inputs=[], outputs=[subseed]) + target_interface_state = gr.Textbox(target_interface, visible=False) + random_seed.click(fn=None, _js="setRandomSeed", show_progress=False, inputs=[target_interface_state], outputs=[]) + random_subseed.click(fn=None, _js="setRandomSubseed", show_progress=False, inputs=[target_interface_state], outputs=[]) def change_visibility(show): return {comp: gr_show(show) for comp in seed_extras} @@ -576,7 +577,7 @@ def create_ui(): txt2img_prompt.submit(**txt2img_args) submit.click(**txt2img_args) - res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) + res_switch_btn.click(fn=None, _js="switchWidthHeightTxt2Img", inputs=None, outputs=None, show_progress=False) txt_prompt_img.change( fn=modules.images.image_data, @@ -896,7 +897,8 @@ def create_ui(): img2img_prompt.submit(**img2img_args) submit.click(**img2img_args) - res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False) + + res_switch_btn.click(fn=None, _js="switchWidthHeightImg2Img", inputs=None, outputs=None, show_progress=False) img2img_interrogate.click( fn=lambda *args: process_interrogate(interrogate, *args),