Move component to under h/w controls

master
Bit9Labs 2023-07-25 15:51:40 -04:00
parent 2b8fadb18b
commit 26d9039dcb
1 changed files with 34 additions and 16 deletions

View File

@ -6,13 +6,6 @@ from modules.ui_components import FormRow, ToolButton
class RatioLock(scripts.Script):
detect_ratio_symbol = '\U0001F50D' #
# Slider controls from A1111 WebUI.
txt2img_w_slider = None
txt2img_h_slider = None
img2img_w_slider = None
img2img_h_slider = None
image_ratios = {
"None": None,
"1:1 - Square": 1.0,
@ -26,6 +19,19 @@ class RatioLock(scripts.Script):
"9:16 - Vertical Video": 9/16,
"21:9 - Ultrawide": 21/9
}
# Slider controls from A1111 WebUI.
txt2img_w_slider = None
txt2img_h_slider = None
img2img_w_slider = None
img2img_h_slider = None
# Our controls
txt2img_dims_ratio = gr.Dropdown(list(image_ratios.keys()), value="None", label="Image Ratio", elem_id="txt2img_dims_ratio")
txt2img_dims_detect = ToolButton(value=detect_ratio_symbol, elem_id="txt2img_dims_detect", label="Detect dims")
img2img_dims_ratio = gr.Dropdown(list(image_ratios.keys()), value="None", label="Image Ratio", elem_id="img2img_dims_ratio")
img2img_dims_detect = ToolButton(value=detect_ratio_symbol, elem_id="img2img_dims_detect", label="Detect dims")
def __init__(self):
self.width = None
self.height = None
@ -46,34 +52,32 @@ class RatioLock(scripts.Script):
# Most UI components can return a value, such as a boolean for a checkbox.
# The returned values are passed to the run method as parameters.
def ui(self, is_img2img):
return
with FormRow():
dims_ratio = gr.Dropdown(list(self.image_ratios.keys()), value="None", label="Image Ratio", elem_id="txt2img_dims_ratio")
dims_detect = ToolButton(value=self.detect_ratio_symbol, elem_id="txt2img_dims_detect", label="Detect dims")
# not sure where else to put this, assumes these items are built first
# based on where scripts display in page
if not is_img2img:
dims_ratio = self.txt2img_dims_ratio
dims_detect = self.txt2img_dims_detect
self.width = self.txt2img_w_slider
self.height = self.txt2img_h_slider
else:
dims_ratio = self.img2img_dims_ratio
dims_detect = self.img2img_dims_detect
self.width = self.img2img_w_slider
self.height = self.img2img_h_slider
width_change_event = self.width.change(fn=self.width_change, inputs=[dims_ratio, self.width, self.height], outputs=[self.height], show_progress=False)
# height_change_event = self.txt2img_h_slider.change(fn=height_change, inputs=[dims_ratio, width, height], outputs=[width], show_progress=False, cancels=[width_change_event])
dims_ratio.change(fn=self.on_dims_ratio, inputs=[dims_ratio, self.width, self.height], outputs=[self.height])
dims_detect.click(fn=self.on_dims_detect, inputs=[self.width, self.height], outputs=[dims_ratio])
return [dims_ratio]
return []
def width_change(self, ratio, width, height):
print("Width change")
ratio = self.image_ratios[ratio]
return round(width/ratio) if ratio else height
def height_change(ratio, width, height):
print("Height change")
ratio = RatioLock.image_ratios[ratio]
return round(height * ratio) if ratio else width
@ -89,6 +93,20 @@ class RatioLock(scripts.Script):
return key
return "None"
@staticmethod
def on_before_component(component, **_kwargs):
elem_id = _kwargs.get('elem_id')
if elem_id == "txt2img_cfg_scale":
with gr.Row():
RatioLock.txt2img_dims_ratio.render()
RatioLock.txt2img_dims_detect.render()
if elem_id == "img2img_cfg_scale":
with gr.Row():
RatioLock.img2img_dims_ratio.render()
RatioLock.img2img_dims_detect.render()
@staticmethod
def on_after_component(component, **_kwargs):
elem_id = getattr(component, "elem_id", None)
@ -109,5 +127,5 @@ class RatioLock(scripts.Script):
RatioLock.img2img_h_slider = component
return
# script_callbacks.on_before_component(RatioLock.on_before_component)
script_callbacks.on_before_component(RatioLock.on_before_component)
script_callbacks.on_after_component(RatioLock.on_after_component)