Skip to content

Commit

Permalink
Add resize-handle extension
Browse files Browse the repository at this point in the history
  • Loading branch information
catboxanon committed Aug 20, 2023
1 parent 9d2299e commit 2f06f96
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
106 changes: 106 additions & 0 deletions extensions-builtin/resize-handle/javascript/resize-handle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
onUiLoaded(async() => {
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;

const leftCols = [
gradioApp().querySelector('#txt2img_settings'),
gradioApp().querySelector('#img2img_settings'),
];

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 setup() {
for (const leftCol of leftCols) {
const parent = leftCol.parentElement;
const rightCol = parent.lastElementChild;

Check failure on line 47 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Trailing spaces not allowed
if (!displayResizeHandle(parent)) {
return

Check failure on line 49 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Missing semicolon
};

Check failure on line 50 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Unnecessary semicolon

Check failure on line 51 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Trailing spaces not allowed
parent.style.display = 'grid';
parent.style.gap = '0';
parent.style.gridTemplateColumns = GRID_TEMPLATE_COLUMNS;

Check failure on line 55 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Trailing spaces not allowed
const resizeHandle = document.createElement('div');
resizeHandle.classList.add('resize-handle');
parent.insertBefore(resizeHandle, rightCol);

Check failure on line 59 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Trailing spaces not allowed
resizeHandle.addEventListener('mousedown', (evt) => {
R.tracking = true;
R.parent = parent;
R.parentWidth = parent.offsetWidth;
R.handle = resizeHandle;
R.leftCol = leftCol;
R.leftColStartWidth = leftCol.offsetWidth;
R.screenX = evt.screenX;
});
}
}

window.addEventListener('mousemove', (evt) => {
if (R.tracking) {
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);
}
});

window.addEventListener('mouseup', () => { R.tracking = false; });

Check failure on line 80 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Statement inside of curly braces should be on next line

Check failure on line 80 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Closing curly brace should be on the same line as opening curly brace or on the line after the previous block

window.addEventListener('resize', () => {
clearTimeout(resizeTimer);

resizeTimer = setTimeout(() => {
for (const leftCol of leftCols) {
const parent = leftCol.parentElement;

Check failure on line 88 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Trailing spaces not allowed
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]);

Check failure on line 93 in extensions-builtin/resize-handle/javascript/resize-handle.js

View workflow job for this annotation

GitHub Actions / eslint

Trailing spaces not allowed
const ratio = newParentWidth / oldParentWidth;

const newWidthL = Math.max(Math.floor(ratio * widthL), GRADIO_MIN_WIDTH);
setLeftColGridTemplate(parent, newWidthL);

R.parentWidth = newParentWidth;
}
}
}, DEBOUNCE_TIME)
});

setup();
});
10 changes: 10 additions & 0 deletions extensions-builtin/resize-handle/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.resize-handle{
cursor: col-resize;
grid-column: 2 / 3;
min-width: 8px !important;
max-width: 8px !important;
height: 100%;
border-left: 1px dashed var(--border-color-primary);
user-select: none;
margin-left: 8px;
}

0 comments on commit 2f06f96

Please sign in to comment.