2023-08-21 05:48:22 +00:00
|
|
|
(function() {
|
|
|
|
const GRADIO_MIN_WIDTH = 320;
|
|
|
|
const GRID_TEMPLATE_COLUMNS = '1fr 16px 1fr';
|
|
|
|
const PAD = 16;
|
|
|
|
const DEBOUNCE_TIME = 100;
|
|
|
|
|
|
|
|
const R = {
|
|
|
|
tracking: false,
|
|
|
|
parent: null,
|
|
|
|
parentWidth: null,
|
|
|
|
leftCol: null,
|
|
|
|
leftColStartWidth: null,
|
|
|
|
screenX: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
let resizeTimer;
|
|
|
|
let parents = [];
|
|
|
|
|
|
|
|
function setLeftColGridTemplate(el, width) {
|
|
|
|
el.style.gridTemplateColumns = `${width}px 16px 1fr`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayResizeHandle(parent) {
|
|
|
|
if (window.innerWidth < GRADIO_MIN_WIDTH * 2 + PAD * 4) {
|
|
|
|
parent.style.display = 'flex';
|
|
|
|
if (R.handle != null) {
|
|
|
|
R.handle.style.opacity = '0';
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
parent.style.display = 'grid';
|
|
|
|
if (R.handle != null) {
|
|
|
|
R.handle.style.opacity = '100';
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterResize(parent) {
|
|
|
|
if (displayResizeHandle(parent) && parent.style.gridTemplateColumns != GRID_TEMPLATE_COLUMNS) {
|
|
|
|
const oldParentWidth = R.parentWidth;
|
|
|
|
const newParentWidth = parent.offsetWidth;
|
|
|
|
const widthL = parseInt(parent.style.gridTemplateColumns.split(' ')[0]);
|
|
|
|
|
|
|
|
const ratio = newParentWidth / oldParentWidth;
|
|
|
|
|
|
|
|
const newWidthL = Math.max(Math.floor(ratio * widthL), GRADIO_MIN_WIDTH);
|
|
|
|
setLeftColGridTemplate(parent, newWidthL);
|
|
|
|
|
|
|
|
R.parentWidth = newParentWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup(parent) {
|
|
|
|
const leftCol = parent.firstElementChild;
|
|
|
|
const rightCol = parent.lastElementChild;
|
|
|
|
|
|
|
|
parents.push(parent);
|
|
|
|
|
|
|
|
parent.style.display = 'grid';
|
|
|
|
parent.style.gap = '0';
|
|
|
|
parent.style.gridTemplateColumns = GRID_TEMPLATE_COLUMNS;
|
|
|
|
|
|
|
|
const resizeHandle = document.createElement('div');
|
|
|
|
resizeHandle.classList.add('resize-handle');
|
|
|
|
parent.insertBefore(resizeHandle, rightCol);
|
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
['mousedown', 'touchstart'].forEach((eventType) => {
|
|
|
|
resizeHandle.addEventListener(eventType, (evt) => {
|
2024-02-22 13:16:16 +00:00
|
|
|
if (eventType.startsWith('mouse')) {
|
2024-02-22 13:04:56 +00:00
|
|
|
if (evt.button !== 0) return;
|
|
|
|
} else {
|
|
|
|
if (evt.changedTouches.length !== 1) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
|
|
|
|
document.body.classList.add('resizing');
|
|
|
|
|
|
|
|
R.tracking = true;
|
|
|
|
R.parent = parent;
|
|
|
|
R.parentWidth = parent.offsetWidth;
|
|
|
|
R.handle = resizeHandle;
|
|
|
|
R.leftCol = leftCol;
|
|
|
|
R.leftColStartWidth = leftCol.offsetWidth;
|
2024-02-22 13:16:16 +00:00
|
|
|
if (eventType.startsWith('mouse')) {
|
2024-02-22 13:04:56 +00:00
|
|
|
R.screenX = evt.screenX;
|
|
|
|
} else {
|
|
|
|
R.screenX = evt.changedTouches[0].screenX;
|
|
|
|
}
|
|
|
|
});
|
2023-08-21 05:48:22 +00:00
|
|
|
});
|
|
|
|
|
2023-08-22 07:45:34 +00:00
|
|
|
resizeHandle.addEventListener('dblclick', (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
|
|
|
|
parent.style.gridTemplateColumns = GRID_TEMPLATE_COLUMNS;
|
|
|
|
});
|
2023-08-21 16:40:27 +00:00
|
|
|
|
2023-08-21 05:48:22 +00:00
|
|
|
afterResize(parent);
|
|
|
|
}
|
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
['mousemove', 'touchmove'].forEach((eventType) => {
|
|
|
|
window.addEventListener(eventType, (evt) => {
|
2024-02-22 13:16:16 +00:00
|
|
|
if (eventType.startsWith('mouse')) {
|
2024-02-22 13:04:56 +00:00
|
|
|
if (evt.button !== 0) return;
|
|
|
|
} else {
|
|
|
|
if (evt.changedTouches.length !== 1) return;
|
|
|
|
}
|
2023-08-22 07:45:34 +00:00
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
if (R.tracking) {
|
2024-02-22 13:16:16 +00:00
|
|
|
if (eventType.startsWith('mouse')) {
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
2024-02-22 13:04:56 +00:00
|
|
|
evt.stopPropagation();
|
2024-02-22 13:22:00 +00:00
|
|
|
|
2024-02-22 13:16:16 +00:00
|
|
|
let delta = 0;
|
|
|
|
if (eventType.startsWith('mouse')) {
|
|
|
|
delta = R.screenX - evt.screenX;
|
2024-02-22 13:04:56 +00:00
|
|
|
} else {
|
2024-02-22 13:16:16 +00:00
|
|
|
delta = R.screenX - evt.changedTouches[0].screenX;
|
2024-02-22 13:04:56 +00:00
|
|
|
}
|
|
|
|
const leftColWidth = Math.max(Math.min(R.leftColStartWidth - delta, R.parent.offsetWidth - GRADIO_MIN_WIDTH - PAD), GRADIO_MIN_WIDTH);
|
|
|
|
setLeftColGridTemplate(R.parent, leftColWidth);
|
|
|
|
}
|
|
|
|
});
|
2023-08-21 05:48:22 +00:00
|
|
|
});
|
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
['mouseup', 'touchend'].forEach((eventType) => {
|
|
|
|
window.addEventListener(eventType, (evt) => {
|
2024-02-22 13:16:16 +00:00
|
|
|
if (eventType.startsWith('mouse')) {
|
2024-02-22 13:04:56 +00:00
|
|
|
if (evt.button !== 0) return;
|
|
|
|
} else {
|
|
|
|
if (evt.changedTouches.length !== 1) return;
|
|
|
|
}
|
2023-08-22 08:19:26 +00:00
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
if (R.tracking) {
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
2023-08-22 07:45:34 +00:00
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
R.tracking = false;
|
2023-08-22 07:45:34 +00:00
|
|
|
|
2024-02-22 13:04:56 +00:00
|
|
|
document.body.classList.remove('resizing');
|
|
|
|
}
|
|
|
|
});
|
2023-08-22 07:45:34 +00:00
|
|
|
});
|
2023-08-21 05:48:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
clearTimeout(resizeTimer);
|
|
|
|
|
|
|
|
resizeTimer = setTimeout(function() {
|
|
|
|
for (const parent of parents) {
|
|
|
|
afterResize(parent);
|
|
|
|
}
|
|
|
|
}, DEBOUNCE_TIME);
|
|
|
|
});
|
|
|
|
|
|
|
|
setupResizeHandle = setup;
|
|
|
|
})();
|
|
|
|
|
|
|
|
onUiLoaded(function() {
|
|
|
|
for (var elem of gradioApp().querySelectorAll('.resize-handle-row')) {
|
2023-08-26 19:56:17 +00:00
|
|
|
if (!elem.querySelector('.resize-handle')) {
|
|
|
|
setupResizeHandle(elem);
|
|
|
|
}
|
2023-08-21 05:48:22 +00:00
|
|
|
}
|
|
|
|
});
|