Skip to content

Commit

Permalink
Merge pull request #15002 from light-and-ray/support_resizable_column…
Browse files Browse the repository at this point in the history
…s_for_touch_(tablets)

support resizable columns for touch (tablets)
  • Loading branch information
AUTOMATIC1111 authored Feb 22, 2024
2 parents 3f18a09 + 58985e6 commit 1881972
Showing 1 changed file with 62 additions and 33 deletions.
95 changes: 62 additions & 33 deletions javascript/resizeHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,31 @@
resizeHandle.classList.add('resize-handle');
parent.insertBefore(resizeHandle, rightCol);

resizeHandle.addEventListener('mousedown', (evt) => {
if (evt.button !== 0) 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;
R.screenX = evt.screenX;
['mousedown', 'touchstart'].forEach((eventType) => {
resizeHandle.addEventListener(eventType, (evt) => {
if (eventType.startsWith('mouse')) {
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;
if (eventType.startsWith('mouse')) {
R.screenX = evt.screenX;
} else {
R.screenX = evt.changedTouches[0].screenX;
}
});
});

resizeHandle.addEventListener('dblclick', (evt) => {
Expand All @@ -92,30 +102,49 @@
afterResize(parent);
}

window.addEventListener('mousemove', (evt) => {
if (evt.button !== 0) return;

if (R.tracking) {
evt.preventDefault();
evt.stopPropagation();
['mousemove', 'touchmove'].forEach((eventType) => {
window.addEventListener(eventType, (evt) => {
if (eventType.startsWith('mouse')) {
if (evt.button !== 0) return;
} else {
if (evt.changedTouches.length !== 1) return;
}

const delta = R.screenX - evt.screenX;
const leftColWidth = Math.max(Math.min(R.leftColStartWidth - delta, R.parent.offsetWidth - GRADIO_MIN_WIDTH - PAD), GRADIO_MIN_WIDTH);
setLeftColGridTemplate(R.parent, leftColWidth);
}
if (R.tracking) {
if (eventType.startsWith('mouse')) {
evt.preventDefault();
}
evt.stopPropagation();

let delta = 0;
if (eventType.startsWith('mouse')) {
delta = R.screenX - evt.screenX;
} else {
delta = R.screenX - evt.changedTouches[0].screenX;
}
const leftColWidth = Math.max(Math.min(R.leftColStartWidth - delta, R.parent.offsetWidth - GRADIO_MIN_WIDTH - PAD), GRADIO_MIN_WIDTH);
setLeftColGridTemplate(R.parent, leftColWidth);
}
});
});

window.addEventListener('mouseup', (evt) => {
if (evt.button !== 0) return;
['mouseup', 'touchend'].forEach((eventType) => {
window.addEventListener(eventType, (evt) => {
if (eventType.startsWith('mouse')) {
if (evt.button !== 0) return;
} else {
if (evt.changedTouches.length !== 1) return;
}

if (R.tracking) {
evt.preventDefault();
evt.stopPropagation();
if (R.tracking) {
evt.preventDefault();
evt.stopPropagation();

R.tracking = false;
R.tracking = false;

document.body.classList.remove('resizing');
}
document.body.classList.remove('resizing');
}
});
});


Expand Down

0 comments on commit 1881972

Please sign in to comment.