Merge pull request #12749 from daswer123/improve_integration

Zoom and pan: Improve integration
This commit is contained in:
AUTOMATIC1111 2023-08-24 09:36:53 +03:00 committed by GitHub
commit 2c570f641c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 11 deletions

View File

@ -48,7 +48,7 @@ onUiLoaded(async() => {
// Wait until opts loaded // Wait until opts loaded
async function waitForOpts() { async function waitForOpts() {
for (;;) { for (; ;) {
if (window.opts && Object.keys(window.opts).length) { if (window.opts && Object.keys(window.opts).length) {
return window.opts; return window.opts;
} }
@ -269,7 +269,7 @@ onUiLoaded(async() => {
input?.addEventListener("input", () => restoreImgRedMask(elements)); input?.addEventListener("input", () => restoreImgRedMask(elements));
} }
function applyZoomAndPan(elemId) { function applyZoomAndPan(elemId, isExtension = true) {
const targetElement = gradioApp().querySelector(elemId); const targetElement = gradioApp().querySelector(elemId);
if (!targetElement) { if (!targetElement) {
@ -381,6 +381,10 @@ onUiLoaded(async() => {
panY: 0 panY: 0
}; };
if (isExtension) {
targetElement.style.overflow = "hidden";
}
fixCanvas(); fixCanvas();
targetElement.style.transform = `scale(${elemData[elemId].zoomLevel}) translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px)`; targetElement.style.transform = `scale(${elemData[elemId].zoomLevel}) translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px)`;
@ -396,8 +400,22 @@ onUiLoaded(async() => {
closeBtn.addEventListener("click", resetZoom); closeBtn.addEventListener("click", resetZoom);
} }
if (canvas && isExtension) {
const parentElement = targetElement.closest('[id^="component-"]');
if (
canvas &&
parseFloat(canvas.style.width) > parentElement.offsetWidth &&
parseFloat(targetElement.style.width) > parentElement.offsetWidth
) {
fitToElement();
return;
}
}
if ( if (
canvas && canvas &&
!isExtension &&
parseFloat(canvas.style.width) > 865 && parseFloat(canvas.style.width) > 865 &&
parseFloat(targetElement.style.width) > 865 parseFloat(targetElement.style.width) > 865
) { ) {
@ -472,6 +490,10 @@ onUiLoaded(async() => {
targetElement.style.transform = `translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px) scale(${newZoomLevel})`; targetElement.style.transform = `translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px) scale(${newZoomLevel})`;
toggleOverlap("on"); toggleOverlap("on");
if (isExtension) {
targetElement.style.overflow = "visible";
}
return newZoomLevel; return newZoomLevel;
} }
@ -494,7 +516,7 @@ onUiLoaded(async() => {
fullScreenMode = false; fullScreenMode = false;
elemData[elemId].zoomLevel = updateZoom( elemData[elemId].zoomLevel = updateZoom(
elemData[elemId].zoomLevel + elemData[elemId].zoomLevel +
(operation === "+" ? delta : -delta), (operation === "+" ? delta : -delta),
zoomPosX - targetElement.getBoundingClientRect().left, zoomPosX - targetElement.getBoundingClientRect().left,
zoomPosY - targetElement.getBoundingClientRect().top zoomPosY - targetElement.getBoundingClientRect().top
); );
@ -511,10 +533,19 @@ onUiLoaded(async() => {
//Reset Zoom //Reset Zoom
targetElement.style.transform = `translate(${0}px, ${0}px) scale(${1})`; targetElement.style.transform = `translate(${0}px, ${0}px) scale(${1})`;
let parentElement;
if (isExtension) {
parentElement = targetElement.closest('[id^="component-"]');
} else {
parentElement = targetElement.parentElement;
}
// Get element and screen dimensions // Get element and screen dimensions
const elementWidth = targetElement.offsetWidth; const elementWidth = targetElement.offsetWidth;
const elementHeight = targetElement.offsetHeight; const elementHeight = targetElement.offsetHeight;
const parentElement = targetElement.parentElement;
const screenWidth = parentElement.clientWidth; const screenWidth = parentElement.clientWidth;
const screenHeight = parentElement.clientHeight; const screenHeight = parentElement.clientHeight;
@ -565,9 +596,14 @@ onUiLoaded(async() => {
`${elemId} canvas[key="interface"]` `${elemId} canvas[key="interface"]`
); );
if (isExtension) {
targetElement.style.overflow = "visible";
}
if (!canvas) return; if (!canvas) return;
if (canvas.offsetWidth > 862) { if (canvas.offsetWidth > 862 || isExtension) {
targetElement.style.width = (canvas.offsetWidth + 2) + "px"; targetElement.style.width = (canvas.offsetWidth + 2) + "px";
} }
@ -677,9 +713,7 @@ onUiLoaded(async() => {
targetElement.isExpanded = false; targetElement.isExpanded = false;
function autoExpand() { function autoExpand() {
const canvas = document.querySelector(`${elemId} canvas[key="interface"]`); const canvas = document.querySelector(`${elemId} canvas[key="interface"]`);
const isMainTab = activeElement === elementIDs.inpaint || activeElement === elementIDs.inpaintSketch || activeElement === elementIDs.sketch; if (canvas) {
if (canvas && isMainTab) {
if (hasHorizontalScrollbar(targetElement) && targetElement.isExpanded === false) { if (hasHorizontalScrollbar(targetElement) && targetElement.isExpanded === false) {
targetElement.style.visibility = "hidden"; targetElement.style.visibility = "hidden";
setTimeout(() => { setTimeout(() => {
@ -818,6 +852,11 @@ onUiLoaded(async() => {
if (isMoving && elemId === activeElement) { if (isMoving && elemId === activeElement) {
updatePanPosition(e.movementX, e.movementY); updatePanPosition(e.movementX, e.movementY);
targetElement.style.pointerEvents = "none"; targetElement.style.pointerEvents = "none";
if (isExtension) {
targetElement.style.overflow = "visible";
}
} else { } else {
targetElement.style.pointerEvents = "auto"; targetElement.style.pointerEvents = "auto";
} }
@ -831,9 +870,9 @@ onUiLoaded(async() => {
gradioApp().addEventListener("mousemove", handleMoveByKey); gradioApp().addEventListener("mousemove", handleMoveByKey);
} }
applyZoomAndPan(elementIDs.sketch); applyZoomAndPan(elementIDs.sketch, false);
applyZoomAndPan(elementIDs.inpaint); applyZoomAndPan(elementIDs.inpaint, false);
applyZoomAndPan(elementIDs.inpaintSketch); applyZoomAndPan(elementIDs.inpaintSketch, false);
// Make the function global so that other extensions can take advantage of this solution // Make the function global so that other extensions can take advantage of this solution
const applyZoomAndPanIntegration = async(id, elementIDs) => { const applyZoomAndPanIntegration = async(id, elementIDs) => {