Add reset preset button (#1986)
* Add reset preset button * fix processor param compare * fix reset button vis after reset * nitspull/1989/head
parent
bbabb39c6a
commit
d5137f063e
|
|
@ -9,6 +9,7 @@
|
||||||
'⇄': 'Mirror webcam',
|
'⇄': 'Mirror webcam',
|
||||||
'💾': 'Save preset',
|
'💾': 'Save preset',
|
||||||
'🗑️': 'Delete preset',
|
'🗑️': 'Delete preset',
|
||||||
|
'↩': 'Reset preset',
|
||||||
};
|
};
|
||||||
|
|
||||||
onUiUpdate(function () {
|
onUiUpdate(function () {
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ from scripts import external_code
|
||||||
save_symbol = "\U0001f4be" # 💾
|
save_symbol = "\U0001f4be" # 💾
|
||||||
delete_symbol = "\U0001f5d1\ufe0f" # 🗑️
|
delete_symbol = "\U0001f5d1\ufe0f" # 🗑️
|
||||||
refresh_symbol = "\U0001f504" # 🔄
|
refresh_symbol = "\U0001f504" # 🔄
|
||||||
|
reset_symbol = "\U000021A9" # ↩
|
||||||
|
|
||||||
NEW_PRESET = "New Preset"
|
NEW_PRESET = "New Preset"
|
||||||
|
|
||||||
|
|
@ -52,16 +53,6 @@ class ControlNetPresetUI(object):
|
||||||
presets = load_presets(preset_directory)
|
presets = load_presets(preset_directory)
|
||||||
|
|
||||||
def __init__(self, id_prefix: str):
|
def __init__(self, id_prefix: str):
|
||||||
self.dropdown = None
|
|
||||||
self.save_button = None
|
|
||||||
self.delete_button = None
|
|
||||||
self.refresh_button = None
|
|
||||||
self.preset_name = None
|
|
||||||
self.confirm_preset_name = None
|
|
||||||
self.name_dialog = None
|
|
||||||
self.render(id_prefix)
|
|
||||||
|
|
||||||
def render(self, id_prefix: str):
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
self.dropdown = gr.Dropdown(
|
self.dropdown = gr.Dropdown(
|
||||||
label="Presets",
|
label="Presets",
|
||||||
|
|
@ -70,6 +61,12 @@ class ControlNetPresetUI(object):
|
||||||
choices=ControlNetPresetUI.dropdown_choices(),
|
choices=ControlNetPresetUI.dropdown_choices(),
|
||||||
value=NEW_PRESET,
|
value=NEW_PRESET,
|
||||||
)
|
)
|
||||||
|
self.reset_button = ToolButton(
|
||||||
|
value=reset_symbol,
|
||||||
|
elem_classes=["cnet-preset-reset"],
|
||||||
|
tooltip="Reset preset",
|
||||||
|
visible=False,
|
||||||
|
)
|
||||||
self.save_button = ToolButton(
|
self.save_button = ToolButton(
|
||||||
value=save_symbol,
|
value=save_symbol,
|
||||||
elem_classes=["cnet-preset-save"],
|
elem_classes=["cnet-preset-save"],
|
||||||
|
|
@ -114,49 +111,59 @@ class ControlNetPresetUI(object):
|
||||||
# 5th update will be updating slider values
|
# 5th update will be updating slider values
|
||||||
# TODO(huchenlei): This is exetremely hacky, need to find a better way
|
# TODO(huchenlei): This is exetremely hacky, need to find a better way
|
||||||
# to achieve the functionality.
|
# to achieve the functionality.
|
||||||
self.dropdown.change(
|
for element, action in (
|
||||||
|
(self.dropdown, "change"),
|
||||||
|
(self.reset_button, "click"),
|
||||||
|
):
|
||||||
|
getattr(element, action)(
|
||||||
fn=ControlNetPresetUI.apply_preset,
|
fn=ControlNetPresetUI.apply_preset,
|
||||||
inputs=[self.dropdown],
|
inputs=[self.dropdown],
|
||||||
outputs=[self.delete_button, control_type, *ui_states],
|
outputs=[self.delete_button, control_type, *ui_states],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
).then(
|
).then(
|
||||||
fn=ControlNetPresetUI.apply_preset,
|
fn=ControlNetPresetUI.apply_preset,
|
||||||
inputs=[self.dropdown],
|
inputs=[self.dropdown],
|
||||||
outputs=[self.delete_button, control_type, *ui_states],
|
outputs=[self.delete_button, control_type, *ui_states],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
).then(
|
).then(
|
||||||
fn=ControlNetPresetUI.apply_preset,
|
fn=ControlNetPresetUI.apply_preset,
|
||||||
inputs=[self.dropdown],
|
inputs=[self.dropdown],
|
||||||
outputs=[self.delete_button, control_type, *ui_states],
|
outputs=[self.delete_button, control_type, *ui_states],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
).then(
|
).then(
|
||||||
fn=ControlNetPresetUI.apply_preset,
|
fn=ControlNetPresetUI.apply_preset,
|
||||||
inputs=[self.dropdown],
|
inputs=[self.dropdown],
|
||||||
outputs=[self.delete_button, control_type, *ui_states],
|
outputs=[self.delete_button, control_type, *ui_states],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
).then(
|
).then(
|
||||||
fn=ControlNetPresetUI.apply_preset,
|
fn=ControlNetPresetUI.apply_preset,
|
||||||
inputs=[self.dropdown],
|
inputs=[self.dropdown],
|
||||||
outputs=[self.delete_button, control_type, *ui_states],
|
outputs=[self.delete_button, control_type, *ui_states],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
|
).then(
|
||||||
|
fn=lambda: gr.update(visible=False),
|
||||||
|
inputs=None,
|
||||||
|
outputs=[self.reset_button],
|
||||||
)
|
)
|
||||||
|
|
||||||
def save_preset(name: str, *ui_states):
|
def save_preset(name: str, *ui_states):
|
||||||
if name == NEW_PRESET:
|
if name == NEW_PRESET:
|
||||||
return gr.update(visible=True), gr.update()
|
return gr.update(visible=True), gr.update(), gr.update()
|
||||||
|
|
||||||
ControlNetPresetUI.save_preset(
|
ControlNetPresetUI.save_preset(
|
||||||
name, external_code.ControlNetUnit(*ui_states)
|
name, external_code.ControlNetUnit(*ui_states)
|
||||||
)
|
)
|
||||||
return gr.update(), gr.update(
|
return (
|
||||||
choices=ControlNetPresetUI.dropdown_choices(), value=name
|
gr.update(), # name dialog
|
||||||
|
gr.update(choices=ControlNetPresetUI.dropdown_choices(), value=name),
|
||||||
|
gr.update(visible=False), # Reset button
|
||||||
)
|
)
|
||||||
|
|
||||||
self.save_button.click(
|
self.save_button.click(
|
||||||
fn=save_preset,
|
fn=save_preset,
|
||||||
inputs=[self.dropdown, *ui_states],
|
inputs=[self.dropdown, *ui_states],
|
||||||
outputs=[self.name_dialog, self.dropdown],
|
outputs=[self.name_dialog, self.dropdown, self.reset_button],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
).then(
|
).then(
|
||||||
fn=None,
|
fn=None,
|
||||||
_js=f"""
|
_js=f"""
|
||||||
|
|
@ -172,13 +179,13 @@ class ControlNetPresetUI(object):
|
||||||
return gr.Dropdown.update(
|
return gr.Dropdown.update(
|
||||||
choices=ControlNetPresetUI.dropdown_choices(),
|
choices=ControlNetPresetUI.dropdown_choices(),
|
||||||
value=NEW_PRESET,
|
value=NEW_PRESET,
|
||||||
)
|
), gr.update(visible=False)
|
||||||
|
|
||||||
self.delete_button.click(
|
self.delete_button.click(
|
||||||
fn=delete_preset,
|
fn=delete_preset,
|
||||||
inputs=[self.dropdown],
|
inputs=[self.dropdown],
|
||||||
outputs=[self.dropdown],
|
outputs=[self.dropdown, self.reset_button],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
)
|
)
|
||||||
|
|
||||||
self.name_dialog.visible = False
|
self.name_dialog.visible = False
|
||||||
|
|
@ -199,14 +206,43 @@ class ControlNetPresetUI(object):
|
||||||
fn=save_new_preset,
|
fn=save_new_preset,
|
||||||
inputs=[self.preset_name, *ui_states],
|
inputs=[self.preset_name, *ui_states],
|
||||||
outputs=[self.name_dialog, self.dropdown],
|
outputs=[self.name_dialog, self.dropdown],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
).then(fn=None, _js="closePopup")
|
).then(fn=None, _js="closePopup")
|
||||||
|
|
||||||
self.refresh_button.click(
|
self.refresh_button.click(
|
||||||
fn=ControlNetPresetUI.refresh_preset,
|
fn=ControlNetPresetUI.refresh_preset,
|
||||||
inputs=None,
|
inputs=None,
|
||||||
outputs=[self.dropdown],
|
outputs=[self.dropdown],
|
||||||
show_progress=False,
|
show_progress="hidden",
|
||||||
|
)
|
||||||
|
|
||||||
|
def update_reset_button(preset_name: str, *ui_states):
|
||||||
|
if preset_name == NEW_PRESET:
|
||||||
|
return gr.update(visible=False)
|
||||||
|
|
||||||
|
infotext = ControlNetPresetUI.presets[preset_name]
|
||||||
|
preset_unit = parse_unit(infotext)
|
||||||
|
current_unit = external_code.ControlNetUnit(*ui_states)
|
||||||
|
preset_unit.image = None
|
||||||
|
current_unit.image = None
|
||||||
|
|
||||||
|
# Do not compare module param that are not used in preset.
|
||||||
|
for module_param in ("processor_res", "threshold_a", "threshold_b"):
|
||||||
|
if getattr(preset_unit, module_param) == -1:
|
||||||
|
setattr(current_unit, module_param, -1)
|
||||||
|
|
||||||
|
return gr.update(visible=vars(current_unit) != vars(preset_unit))
|
||||||
|
|
||||||
|
for ui_state in ui_states:
|
||||||
|
for action in ("edit", "click", "change", "clear", "release"):
|
||||||
|
if action == "release" and not isinstance(ui_state, gr.Slider):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if hasattr(ui_state, action):
|
||||||
|
getattr(ui_state, action)(
|
||||||
|
fn=update_reset_button,
|
||||||
|
inputs=[self.dropdown, *ui_states],
|
||||||
|
outputs=[self.reset_button],
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue