2023-08-21 05:48:22 +00:00
|
|
|
(function() {
|
|
|
|
const GRADIO_MIN_WIDTH = 320;
|
|
|
|
const PAD = 16;
|
2024-03-21 20:03:27 +00:00
|
|
|
const DEBOUNCE_TIME_MS = 250;
|
|
|
|
const DOUBLE_TAP_DELAY_MS = 250;
|
2023-08-21 05:48:22 +00:00
|
|
|
|
|
|
|
const R = {
|
|
|
|
tracking: false,
|
|
|
|
parent: null,
|
|
|
|
parentWidth: null,
|
|
|
|
leftCol: null,
|
|
|
|
leftColStartWidth: null,
|
|
|
|
screenX: null,
|
2024-02-29 11:40:15 +00:00
|
|
|
lastTapTime: null,
|
2023-08-21 05:48:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let resizeTimer;
|
|
|
|
let parents = [];
|
|
|
|
|
|
|
|
function setLeftColGridTemplate(el, width) {
|
2024-03-15 18:31:58 +00:00
|
|
|
el.style.gridTemplateColumns = `${width}px ${PAD}px 1fr`;
|
2023-08-21 05:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function displayResizeHandle(parent) {
|
2024-02-27 19:31:47 +00:00
|
|
|
if (!parent.needHideOnMoblie) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-21 05:48:22 +00:00
|
|
|
if (window.innerWidth < GRADIO_MIN_WIDTH * 2 + PAD * 4) {
|
|
|
|
parent.style.display = 'flex';
|
2024-02-26 09:37:29 +00:00
|
|
|
parent.resizeHandle.style.display = "none";
|
2023-08-21 05:48:22 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
parent.style.display = 'grid';
|
2024-02-26 09:37:29 +00:00
|
|
|
parent.resizeHandle.style.display = "block";
|
2023-08-21 05:48:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterResize(parent) {
|
2024-02-22 22:20:42 +00:00
|
|
|
if (displayResizeHandle(parent) && parent.style.gridTemplateColumns != parent.style.originalGridTemplateColumns) {
|
2023-08-21 05:48:22 +00:00
|
|
|
const oldParentWidth = R.parentWidth;
|
|
|
|
const newParentWidth = parent.offsetWidth;
|
|
|
|
const widthL = parseInt(parent.style.gridTemplateColumns.split(' ')[0]);
|
|
|
|
|
|
|
|
const ratio = newParentWidth / oldParentWidth;
|
|
|
|
|
2024-02-27 14:31:36 +00:00
|
|
|
const newWidthL = Math.max(Math.floor(ratio * widthL), parent.minLeftColWidth);
|
2023-08-21 05:48:22 +00:00
|
|
|
setLeftColGridTemplate(parent, newWidthL);
|
|
|
|
|
|
|
|
R.parentWidth = newParentWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup(parent) {
|
2024-02-29 11:40:15 +00:00
|
|
|
|
|
|
|
function onDoubleClick(evt) {
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
|
|
|
|
parent.style.gridTemplateColumns = parent.style.originalGridTemplateColumns;
|
2024-03-15 18:31:58 +00:00
|
|
|
|
|
|
|
// Fire custom event to modify left column width externally.
|
|
|
|
parent.dispatchEvent(
|
|
|
|
new CustomEvent("resizeHandleDblClick", {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
setLeftColGridTemplate: setLeftColGridTemplate,
|
|
|
|
pad: PAD,
|
|
|
|
minLeftColWidth: parent.minLeftColWidth,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2024-02-29 11:40:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 05:48:22 +00:00
|
|
|
const leftCol = parent.firstElementChild;
|
|
|
|
const rightCol = parent.lastElementChild;
|
|
|
|
|
|
|
|
parents.push(parent);
|
|
|
|
|
|
|
|
parent.style.display = 'grid';
|
|
|
|
parent.style.gap = '0';
|
2024-02-27 14:31:36 +00:00
|
|
|
let leftColTemplate = "";
|
|
|
|
if (parent.children[0].style.flexGrow) {
|
|
|
|
leftColTemplate = `${parent.children[0].style.flexGrow}fr`;
|
|
|
|
parent.minLeftColWidth = GRADIO_MIN_WIDTH;
|
2024-02-27 19:31:47 +00:00
|
|
|
parent.minRightColWidth = GRADIO_MIN_WIDTH;
|
|
|
|
parent.needHideOnMoblie = true;
|
2024-02-27 14:31:36 +00:00
|
|
|
} else {
|
|
|
|
leftColTemplate = parent.children[0].style.flexBasis;
|
2024-02-29 12:02:21 +00:00
|
|
|
parent.minLeftColWidth = parent.children[0].style.flexBasis.slice(0, -2) / 2;
|
2024-02-27 19:31:47 +00:00
|
|
|
parent.minRightColWidth = 0;
|
|
|
|
parent.needHideOnMoblie = false;
|
2024-02-27 14:31:36 +00:00
|
|
|
}
|
2024-03-08 05:13:02 +00:00
|
|
|
|
|
|
|
if (!leftColTemplate) {
|
|
|
|
leftColTemplate = '1fr';
|
|
|
|
}
|
|
|
|
|
2024-02-27 14:31:36 +00:00
|
|
|
const gridTemplateColumns = `${leftColTemplate} ${PAD}px ${parent.children[1].style.flexGrow}fr`;
|
2024-02-22 22:20:42 +00:00
|
|
|
parent.style.gridTemplateColumns = gridTemplateColumns;
|
|
|
|
parent.style.originalGridTemplateColumns = gridTemplateColumns;
|
2023-08-21 05:48:22 +00:00
|
|
|
|
|
|
|
const resizeHandle = document.createElement('div');
|
|
|
|
resizeHandle.classList.add('resize-handle');
|
|
|
|
parent.insertBefore(resizeHandle, rightCol);
|
2024-02-26 09:37:29 +00:00
|
|
|
parent.resizeHandle = resizeHandle;
|
2023-08-21 05:48:22 +00:00
|
|
|
|
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;
|
2024-02-29 11:40:15 +00:00
|
|
|
|
|
|
|
const currentTime = new Date().getTime();
|
2024-03-21 20:03:27 +00:00
|
|
|
if (R.lastTapTime && currentTime - R.lastTapTime <= DOUBLE_TAP_DELAY_MS) {
|
2024-02-29 11:40:15 +00:00
|
|
|
onDoubleClick(evt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
R.lastTapTime = currentTime;
|
2024-02-22 13:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
|
|
|
|
document.body.classList.add('resizing');
|
|
|
|
|
|
|
|
R.tracking = true;
|
|
|
|
R.parent = parent;
|
|
|
|
R.parentWidth = parent.offsetWidth;
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2024-02-29 11:40:15 +00:00
|
|
|
resizeHandle.addEventListener('dblclick', onDoubleClick);
|
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
|
|
|
}
|
2024-02-27 19:31:47 +00:00
|
|
|
const leftColWidth = Math.max(Math.min(R.leftColStartWidth - delta, R.parent.offsetWidth - R.parent.minRightColWidth - PAD), R.parent.minLeftColWidth);
|
2024-02-22 13:04:56 +00:00
|
|
|
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');
|
2024-03-13 21:11:44 +00:00
|
|
|
|
|
|
|
// Fire a custom event at end of resizing.
|
|
|
|
R.parent.dispatchEvent(
|
|
|
|
new CustomEvent("resizeHandleResized", {
|
|
|
|
bubbles: true,
|
|
|
|
}),
|
|
|
|
);
|
2024-02-22 13:04:56 +00:00
|
|
|
}
|
|
|
|
});
|
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);
|
|
|
|
}
|
2024-03-21 20:03:27 +00:00
|
|
|
}, DEBOUNCE_TIME_MS);
|
2023-08-21 05:48:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setupResizeHandle = setup;
|
|
|
|
})();
|
|
|
|
|
2024-02-27 14:31:36 +00:00
|
|
|
|
2024-02-27 19:17:52 +00:00
|
|
|
function setupAllResizeHandles() {
|
2023-08-21 05:48:22 +00:00
|
|
|
for (var elem of gradioApp().querySelectorAll('.resize-handle-row')) {
|
2024-02-27 14:31:36 +00:00
|
|
|
if (!elem.querySelector('.resize-handle') && !elem.children[0].classList.contains("hidden")) {
|
2023-08-26 19:56:17 +00:00
|
|
|
setupResizeHandle(elem);
|
|
|
|
}
|
2023-08-21 05:48:22 +00:00
|
|
|
}
|
2024-02-27 14:38:38 +00:00
|
|
|
}
|
2024-02-27 19:17:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
onUiLoaded(setupAllResizeHandles);
|
|
|
|
|