Skip to content

Commit df595ae

Browse files
committed
make resize handle available to extensions
1 parent b4d21e7 commit df595ae

File tree

7 files changed

+139
-120
lines changed

7 files changed

+139
-120
lines changed

Diff for: .eslintrc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ module.exports = {
9090
// localStorage.js
9191
localSet: "readonly",
9292
localGet: "readonly",
93-
localRemove: "readonly"
93+
localRemove: "readonly",
94+
// resizeHandle.js
95+
setupResizeHandle: "writable"
9496
}
9597
};

Diff for: extensions-builtin/resize-handle/javascript/resize-handle.js

-106
This file was deleted.

Diff for: extensions-builtin/resize-handle/style.css

-10
This file was deleted.

Diff for: javascript/resizeHandle.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
(function() {
2+
const GRADIO_MIN_WIDTH = 320;
3+
const GRID_TEMPLATE_COLUMNS = '1fr 16px 1fr';
4+
const PAD = 16;
5+
const DEBOUNCE_TIME = 100;
6+
7+
const R = {
8+
tracking: false,
9+
parent: null,
10+
parentWidth: null,
11+
leftCol: null,
12+
leftColStartWidth: null,
13+
screenX: null,
14+
};
15+
16+
let resizeTimer;
17+
let parents = [];
18+
19+
function setLeftColGridTemplate(el, width) {
20+
el.style.gridTemplateColumns = `${width}px 16px 1fr`;
21+
}
22+
23+
function displayResizeHandle(parent) {
24+
if (window.innerWidth < GRADIO_MIN_WIDTH * 2 + PAD * 4) {
25+
parent.style.display = 'flex';
26+
if (R.handle != null) {
27+
R.handle.style.opacity = '0';
28+
}
29+
return false;
30+
} else {
31+
parent.style.display = 'grid';
32+
if (R.handle != null) {
33+
R.handle.style.opacity = '100';
34+
}
35+
return true;
36+
}
37+
}
38+
39+
function afterResize(parent) {
40+
if (displayResizeHandle(parent) && parent.style.gridTemplateColumns != GRID_TEMPLATE_COLUMNS) {
41+
const oldParentWidth = R.parentWidth;
42+
const newParentWidth = parent.offsetWidth;
43+
const widthL = parseInt(parent.style.gridTemplateColumns.split(' ')[0]);
44+
45+
const ratio = newParentWidth / oldParentWidth;
46+
47+
const newWidthL = Math.max(Math.floor(ratio * widthL), GRADIO_MIN_WIDTH);
48+
setLeftColGridTemplate(parent, newWidthL);
49+
50+
R.parentWidth = newParentWidth;
51+
}
52+
}
53+
54+
function setup(parent) {
55+
const leftCol = parent.firstElementChild;
56+
const rightCol = parent.lastElementChild;
57+
58+
parents.push(parent);
59+
60+
parent.style.display = 'grid';
61+
parent.style.gap = '0';
62+
parent.style.gridTemplateColumns = GRID_TEMPLATE_COLUMNS;
63+
64+
const resizeHandle = document.createElement('div');
65+
resizeHandle.classList.add('resize-handle');
66+
parent.insertBefore(resizeHandle, rightCol);
67+
68+
resizeHandle.addEventListener('mousedown', (evt) => {
69+
R.tracking = true;
70+
R.parent = parent;
71+
R.parentWidth = parent.offsetWidth;
72+
R.handle = resizeHandle;
73+
R.leftCol = leftCol;
74+
R.leftColStartWidth = leftCol.offsetWidth;
75+
R.screenX = evt.screenX;
76+
});
77+
78+
afterResize(parent);
79+
}
80+
81+
window.addEventListener('mousemove', (evt) => {
82+
if (R.tracking) {
83+
const delta = R.screenX - evt.screenX;
84+
const leftColWidth = Math.max(Math.min(R.leftColStartWidth - delta, R.parent.offsetWidth - GRADIO_MIN_WIDTH - PAD), GRADIO_MIN_WIDTH);
85+
setLeftColGridTemplate(R.parent, leftColWidth);
86+
}
87+
});
88+
89+
window.addEventListener('mouseup', () => R.tracking = false);
90+
91+
92+
window.addEventListener('resize', () => {
93+
clearTimeout(resizeTimer);
94+
95+
resizeTimer = setTimeout(function() {
96+
for (const parent of parents) {
97+
afterResize(parent);
98+
}
99+
}, DEBOUNCE_TIME);
100+
});
101+
102+
setupResizeHandle = setup;
103+
})();
104+
105+
onUiLoaded(function() {
106+
for (var elem of gradioApp().querySelectorAll('.resize-handle-row')) {
107+
setupResizeHandle(elem);
108+
}
109+
});

Diff for: modules/ui.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from modules import gradio_extensons # noqa: F401
1515
from modules import sd_hijack, sd_models, script_callbacks, ui_extensions, deepbooru, extra_networks, ui_common, ui_postprocessing, progress, ui_loadsave, shared_items, ui_settings, timer, sysinfo, ui_checkpoint_merger, ui_prompt_styles, scripts, sd_samplers, processing, ui_extra_networks
16-
from modules.ui_components import FormRow, FormGroup, ToolButton, FormHTML, InputAccordion
16+
from modules.ui_components import FormRow, FormGroup, ToolButton, FormHTML, InputAccordion, ResizeHandleRow
1717
from modules.paths import script_path
1818
from modules.ui_common import create_refresh_button
1919
from modules.ui_gradio_extensions import reload_javascript
@@ -333,7 +333,7 @@ def create_ui():
333333
extra_tabs = gr.Tabs(elem_id="txt2img_extra_tabs")
334334
extra_tabs.__enter__()
335335

336-
with gr.Tab("Generation", id="txt2img_generation") as txt2img_generation_tab, gr.Row(equal_height=False):
336+
with gr.Tab("Generation", id="txt2img_generation") as txt2img_generation_tab, ResizeHandleRow(equal_height=False):
337337
with gr.Column(variant='compact', elem_id="txt2img_settings"):
338338
scripts.scripts_txt2img.prepare_ui()
339339

@@ -549,7 +549,7 @@ def create_ui():
549549
extra_tabs = gr.Tabs(elem_id="img2img_extra_tabs")
550550
extra_tabs.__enter__()
551551

552-
with gr.Tab("Generation", id="img2img_generation") as img2img_generation_tab, FormRow(equal_height=False):
552+
with gr.Tab("Generation", id="img2img_generation") as img2img_generation_tab, ResizeHandleRow(equal_height=False):
553553
with gr.Column(variant='compact', elem_id="img2img_settings"):
554554
copy_image_buttons = []
555555
copy_image_destinations = {}

Diff for: modules/ui_components.py

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ def get_block_name(self):
2020
return "button"
2121

2222

23+
class ResizeHandleRow(gr.Row):
24+
"""Same as gr.Row but fits inside gradio forms"""
25+
26+
def __init__(self, **kwargs):
27+
super().__init__(**kwargs)
28+
29+
self.elem_classes.append("resize-handle-row")
30+
31+
def get_block_name(self):
32+
return "row"
33+
34+
2335
class FormRow(FormComponent, gr.Row):
2436
"""Same as gr.Row but fits inside gradio forms"""
2537

Diff for: style.css

+12
Original file line numberDiff line numberDiff line change
@@ -1055,3 +1055,15 @@ div.accordions > div.input-accordion.input-accordion-open{
10551055
position: sticky;
10561056
top: 0.5em;
10571057
}
1058+
1059+
1060+
.resize-handle{
1061+
cursor: col-resize;
1062+
grid-column: 2 / 3;
1063+
min-width: 8px !important;
1064+
max-width: 8px !important;
1065+
height: 100%;
1066+
border-left: 1px dashed var(--border-color-primary);
1067+
user-select: none;
1068+
margin-left: 8px;
1069+
}

0 commit comments

Comments
 (0)