2023-05-27 22:56:48 +00:00
// Helper functions
// Get active tab
function getActiveTab ( elements , all = false ) {
2023-05-28 17:22:35 +00:00
const tabs = elements . img2imgTabs . querySelectorAll ( "button" ) ;
2023-05-27 22:56:48 +00:00
2023-05-28 17:22:35 +00:00
if ( all ) return tabs ;
2023-05-27 22:56:48 +00:00
2023-05-28 17:22:35 +00:00
for ( let tab of tabs ) {
if ( tab . classList . contains ( "selected" ) ) {
return tab ;
}
2023-05-27 22:56:48 +00:00
}
}
2023-06-03 22:18:27 +00:00
// Get tab ID
2023-06-04 00:04:46 +00:00
function getTabId ( elements , elementIDs ) {
2023-06-03 22:18:27 +00:00
const activeTab = getActiveTab ( elements ) ;
const tabIdLookup = {
2023-06-04 00:04:46 +00:00
"Sketch" : elementIDs . sketch ,
"Inpaint sketch" : elementIDs . inpaintSketch ,
"Inpaint" : elementIDs . inpaint
2023-06-03 22:18:27 +00:00
} ;
return tabIdLookup [ activeTab . innerText ] ;
2023-06-04 00:04:46 +00:00
}
2023-06-03 22:18:27 +00:00
2023-06-04 00:04:46 +00:00
// Get Active main tab to prevent "Undo" on text2img from being disabled
function getActiveMainTab ( ) {
const selectedTab = gradioApp ( ) . querySelector (
"#tabs .tab-nav button.selected"
) ;
2023-06-03 22:18:27 +00:00
return selectedTab ;
2023-06-04 00:04:46 +00:00
}
2023-06-03 22:18:27 +00:00
2023-06-01 22:04:17 +00:00
// Wait until opts loaded
async function waitForOpts ( ) {
return new Promise ( resolve => {
const checkInterval = setInterval ( ( ) => {
if ( window . opts && Object . keys ( window . opts ) . length !== 0 ) {
clearInterval ( checkInterval ) ;
resolve ( window . opts ) ;
}
} , 100 ) ;
} ) ;
}
// Check is hotkey valid
function isSingleLetter ( value ) {
return (
typeof value === "string" && value . length === 1 && /[a-z]/i . test ( value )
) ;
}
// Create hotkeyConfig from opts
function createHotkeyConfig ( defaultHotkeysConfig , hotkeysConfigOpts ) {
const result = { } ;
2023-06-01 22:26:25 +00:00
const usedKeys = new Set ( ) ;
2023-06-01 22:04:17 +00:00
for ( const key in defaultHotkeysConfig ) {
2023-06-02 16:10:28 +00:00
if ( typeof hotkeysConfigOpts [ key ] === "boolean" ) {
result [ key ] = hotkeysConfigOpts [ key ] ;
continue ;
}
if (
hotkeysConfigOpts [ key ] &&
isSingleLetter ( hotkeysConfigOpts [ key ] ) &&
! usedKeys . has ( hotkeysConfigOpts [ key ] . toUpperCase ( ) )
) {
2023-06-01 22:26:25 +00:00
// If the property passed the test and has not yet been used, add 'Key' before it and save it
2023-06-02 16:10:28 +00:00
result [ key ] = "Key" + hotkeysConfigOpts [ key ] . toUpperCase ( ) ;
2023-06-01 22:26:25 +00:00
usedKeys . add ( hotkeysConfigOpts [ key ] . toUpperCase ( ) ) ;
2023-06-01 22:04:17 +00:00
} else {
2023-06-01 22:26:25 +00:00
// If the property does not pass the test or has already been used, we keep the default value
2023-06-02 16:10:28 +00:00
console . error (
2023-06-02 23:24:57 +00:00
` Hotkey: ${ hotkeysConfigOpts [ key ] } for ${ key } is repeated and conflicts with another hotkey or is not 1 letter. The default hotkey is used: ${ defaultHotkeysConfig [ key ] [ 3 ] } `
2023-06-02 16:10:28 +00:00
) ;
2023-06-01 22:04:17 +00:00
result [ key ] = defaultHotkeysConfig [ key ] ;
}
}
2023-06-01 22:26:25 +00:00
2023-06-01 22:04:17 +00:00
return result ;
}
2023-06-04 00:04:46 +00:00
/ * *
* The restoreImgRedMask function displays a red mask around an image to indicate the aspect ratio .
* If the image display property is set to 'none' , the mask breaks . To fix this , the function
* temporarily sets the display property to 'block' and then hides the mask again after 300 milliseconds
* to avoid breaking the canvas . Additionally , the function adjusts the mask to work correctly on
* very long images .
* /
function restoreImgRedMask ( elements , elementIDs ) {
const mainTabId = getTabId ( elements , elementIDs ) ;
if ( ! mainTabId ) return ;
const mainTab = gradioApp ( ) . querySelector ( mainTabId ) ;
const img = mainTab . querySelector ( "img" ) ;
const imageARPreview = gradioApp ( ) . querySelector ( "#imageARPreview" ) ;
if ( ! img || ! imageARPreview ) return ;
imageARPreview . style . transform = "" ;
if ( parseFloat ( mainTab . style . width ) > 865 ) {
const transformValues = mainTab . style . transform
. match ( /[-+]?[0-9]*\.?[0-9]+/g )
. map ( Number ) ;
const [ posX , posY , zoom ] = transformValues ;
imageARPreview . style . transformOrigin = "0 0" ;
imageARPreview . style . transform = ` scale( ${ zoom } ) ` ;
}
if ( img . style . display !== "none" ) return ;
img . style . display = "block" ;
setTimeout ( ( ) => {
img . style . display = "none" ;
} , 300 ) ;
}
2023-06-01 22:04:17 +00:00
// Main
2023-05-28 17:22:35 +00:00
onUiLoaded ( async ( ) => {
2023-06-01 22:04:17 +00:00
const hotkeysConfigOpts = await waitForOpts ( ) ;
// Default config
const defaultHotkeysConfig = {
canvas _hotkey _reset : "KeyR" ,
canvas _hotkey _fullscreen : "KeyS" ,
canvas _hotkey _move : "KeyF" ,
2023-06-02 16:10:28 +00:00
canvas _hotkey _overlap : "KeyO" ,
2023-06-02 23:18:49 +00:00
canvas _show _tooltip : true ,
canvas _swap _controls : false
2023-05-28 17:22:35 +00:00
} ;
2023-06-02 23:18:49 +00:00
// swap the actions for ctr + wheel and shift + wheel
2023-06-01 22:04:17 +00:00
const hotkeysConfig = createHotkeyConfig (
defaultHotkeysConfig ,
hotkeysConfigOpts
) ;
2023-05-28 17:22:35 +00:00
let isMoving = false ;
let mouseX , mouseY ;
2023-06-04 00:04:46 +00:00
let activeElement ;
2023-05-28 17:22:35 +00:00
const elementIDs = {
sketch : "#img2img_sketch" ,
inpaint : "#img2maskimg" ,
inpaintSketch : "#inpaint_sketch" ,
2023-06-03 22:18:27 +00:00
img2imgTabs : "#mode_img2img .tab-nav" ,
2023-06-04 00:04:46 +00:00
rangeGroup : "#img2img_column_size"
2023-05-28 17:22:35 +00:00
} ;
async function getElements ( ) {
const elements = await Promise . all (
2023-06-04 00:04:46 +00:00
Object . values ( elementIDs ) . map ( id => gradioApp ( ) . querySelector ( id ) )
2023-05-28 17:22:35 +00:00
) ;
return Object . fromEntries (
Object . keys ( elementIDs ) . map ( ( key , index ) => [ key , elements [ index ] ] )
) ;
2023-05-27 22:56:48 +00:00
}
2023-05-28 17:22:35 +00:00
const elements = await getElements ( ) ;
2023-06-04 00:04:46 +00:00
const elemData = { } ;
// Apply functionality to the range inputs. Restore redmask and correct for long images.
const rangeInputs = elements . rangeGroup ? elements . rangeGroup . querySelectorAll ( "input" ) :
[
gradioApp ( ) . querySelector ( "#img2img_width input[type='range']" ) ,
gradioApp ( ) . querySelector ( "#img2img_height input[type='range']" )
] ;
rangeInputs . forEach ( input => {
if ( input ) {
input . addEventListener ( "input" , ( ) => restoreImgRedMask ( elements , elementIDs ) ) ;
}
} ) ;
2023-06-03 22:18:27 +00:00
2023-06-03 16:24:05 +00:00
function applyZoomAndPan ( elemId ) {
const targetElement = gradioApp ( ) . querySelector ( elemId ) ;
if ( ! targetElement ) {
console . log ( "Element not found" ) ;
return ;
}
2023-05-28 17:22:35 +00:00
targetElement . style . transformOrigin = "0 0" ;
2023-06-04 00:04:46 +00:00
elemData [ elemId ] = {
zoom : 1 ,
panX : 0 ,
panY : 0
} ;
2023-05-28 17:22:35 +00:00
let fullScreenMode = false ;
2023-05-27 19:54:45 +00:00
2023-05-31 20:02:49 +00:00
// Create tooltip
2023-06-02 16:10:28 +00:00
function createTooltip ( ) {
const toolTipElemnt =
targetElement . querySelector ( ".image-container" ) ;
const tooltip = document . createElement ( "div" ) ;
tooltip . className = "tooltip" ;
// Creating an item of information
const info = document . createElement ( "i" ) ;
info . className = "tooltip-info" ;
info . textContent = "" ;
// Create a container for the contents of the tooltip
const tooltipContent = document . createElement ( "div" ) ;
tooltipContent . className = "tooltip-content" ;
2023-06-04 00:04:46 +00:00
// Add info about hotkeys
2023-06-02 23:18:49 +00:00
const zoomKey = hotkeysConfig . canvas _swap _controls ? "Ctrl" : "Shift" ;
const adjustKey = hotkeysConfig . canvas _swap _controls ? "Shift" : "Ctrl" ;
2023-06-02 16:10:28 +00:00
const hotkeys = [
2023-06-02 23:18:49 +00:00
{ key : ` ${ zoomKey } + wheel ` , action : "Zoom canvas" } ,
{ key : ` ${ adjustKey } + wheel ` , action : "Adjust brush size" } ,
2023-06-02 16:10:28 +00:00
{
2023-06-04 00:04:46 +00:00
key : hotkeysConfig . canvas _hotkey _reset . charAt ( hotkeysConfig . canvas _hotkey _reset . length - 1 ) ,
2023-06-02 16:10:28 +00:00
action : "Reset zoom"
} ,
{
2023-06-04 00:04:46 +00:00
key : hotkeysConfig . canvas _hotkey _fullscreen . charAt ( hotkeysConfig . canvas _hotkey _fullscreen . length - 1 ) ,
2023-06-02 16:10:28 +00:00
action : "Fullscreen mode"
} ,
{
2023-06-04 00:04:46 +00:00
key : hotkeysConfig . canvas _hotkey _move . charAt ( hotkeysConfig . canvas _hotkey _move . length - 1 ) ,
2023-06-02 16:10:28 +00:00
action : "Move canvas"
}
] ;
hotkeys . forEach ( function ( hotkey ) {
const p = document . createElement ( "p" ) ;
p . innerHTML =
"<b>" + hotkey . key + "</b>" + " - " + hotkey . action ;
tooltipContent . appendChild ( p ) ;
} ) ;
// Add information and content elements to the tooltip element
tooltip . appendChild ( info ) ;
tooltip . appendChild ( tooltipContent ) ;
// Add a hint element to the target element
toolTipElemnt . appendChild ( tooltip ) ;
}
//Show tool tip if setting enable
if ( hotkeysConfig . canvas _show _tooltip ) {
createTooltip ( ) ;
}
2023-05-31 20:02:49 +00:00
2023-05-28 17:22:35 +00:00
// In the course of research, it was found that the tag img is very harmful when zooming and creates white canvases. This hack allows you to almost never think about this problem, it has no effect on webui.
function fixCanvas ( ) {
const activeTab = getActiveTab ( elements ) . textContent . trim ( ) ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
if ( activeTab !== "img2img" ) {
const img = targetElement . querySelector ( ` ${ elemId } img ` ) ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
if ( img && img . style . display !== "none" ) {
img . style . display = "none" ;
img . style . visibility = "hidden" ;
}
}
2023-05-27 19:54:45 +00:00
}
2023-05-28 17:22:35 +00:00
// Reset the zoom level and pan position of the target element to their initial values
function resetZoom ( ) {
2023-06-04 00:04:46 +00:00
elemData [ elemId ] = {
zoomLevel : 1 ,
panX : 0 ,
panY : 0
} ;
2023-05-28 17:22:35 +00:00
fixCanvas ( ) ;
2023-06-04 00:04:46 +00:00
targetElement . style . transform = ` scale( ${ elemData [ elemId ] . zoomLevel } ) translate( ${ elemData [ elemId ] . panX } px, ${ elemData [ elemId ] . panY } px) ` ;
2023-05-28 17:22:35 +00:00
2023-05-28 17:32:21 +00:00
const canvas = gradioApp ( ) . querySelector (
2023-05-28 17:22:35 +00:00
` ${ elemId } canvas[key="interface"] `
) ;
toggleOverlap ( "off" ) ;
fullScreenMode = false ;
if (
canvas &&
parseFloat ( canvas . style . width ) > 865 &&
parseFloat ( targetElement . style . width ) > 865
) {
fitToElement ( ) ;
return ;
}
targetElement . style . width = "" ;
if ( canvas ) {
targetElement . style . height = canvas . style . height ;
}
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Toggle the zIndex of the target element between two values, allowing it to overlap or be overlapped by other elements
function toggleOverlap ( forced = "" ) {
const zIndex1 = "0" ;
const zIndex2 = "998" ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
targetElement . style . zIndex =
targetElement . style . zIndex !== zIndex2 ? zIndex2 : zIndex1 ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
if ( forced === "off" ) {
targetElement . style . zIndex = zIndex1 ;
} else if ( forced === "on" ) {
targetElement . style . zIndex = zIndex2 ;
}
2023-05-27 19:54:45 +00:00
}
2023-05-28 17:22:35 +00:00
// Adjust the brush size based on the deltaY value from a mouse wheel event
function adjustBrushSize (
elemId ,
deltaY ,
withoutValue = false ,
percentage = 5
) {
const input =
2023-05-28 17:32:21 +00:00
gradioApp ( ) . querySelector (
2023-05-28 17:22:35 +00:00
` ${ elemId } input[aria-label='Brush radius'] `
) ||
2023-05-28 17:32:21 +00:00
gradioApp ( ) . querySelector (
2023-05-28 17:22:35 +00:00
` ${ elemId } button[aria-label="Use brush"] `
) ;
if ( input ) {
input . click ( ) ;
if ( ! withoutValue ) {
const maxValue =
parseFloat ( input . getAttribute ( "max" ) ) || 100 ;
const changeAmount = maxValue * ( percentage / 100 ) ;
const newValue =
parseFloat ( input . value ) +
( deltaY > 0 ? - changeAmount : changeAmount ) ;
input . value = Math . min ( Math . max ( newValue , 0 ) , maxValue ) ;
input . dispatchEvent ( new Event ( "change" ) ) ;
}
}
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Reset zoom when uploading a new image
2023-05-28 17:32:21 +00:00
const fileInput = gradioApp ( ) . querySelector (
2023-05-28 17:22:35 +00:00
` ${ elemId } input[type="file"][accept="image/*"].svelte-116rqfv `
2023-05-27 19:54:45 +00:00
) ;
2023-05-28 17:22:35 +00:00
fileInput . addEventListener ( "click" , resetZoom ) ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Update the zoom level and pan position of the target element based on the values of the zoomLevel, panX and panY variables
function updateZoom ( newZoomLevel , mouseX , mouseY ) {
newZoomLevel = Math . max ( 0.5 , Math . min ( newZoomLevel , 15 ) ) ;
2023-06-04 00:04:46 +00:00
elemData [ elemId ] . panX +=
mouseX - ( mouseX * newZoomLevel ) / elemData [ elemId ] . zoomLevel ;
elemData [ elemId ] . panY +=
mouseY - ( mouseY * newZoomLevel ) / elemData [ elemId ] . zoomLevel ;
2023-05-27 22:31:23 +00:00
2023-05-28 17:22:35 +00:00
targetElement . style . transformOrigin = "0 0" ;
2023-06-04 00:04:46 +00:00
targetElement . style . transform = ` translate( ${ elemData [ elemId ] . panX } px, ${ elemData [ elemId ] . panY } px) scale( ${ newZoomLevel } ) ` ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
toggleOverlap ( "on" ) ;
return newZoomLevel ;
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Change the zoom level based on user interaction
function changeZoomLevel ( operation , e ) {
2023-06-02 23:18:49 +00:00
if (
( ! hotkeysConfig . canvas _swap _controls && e . shiftKey ) ||
( hotkeysConfig . canvas _swap _controls && e . ctrlKey )
) {
2023-05-28 17:22:35 +00:00
e . preventDefault ( ) ;
let zoomPosX , zoomPosY ;
let delta = 0.2 ;
2023-06-04 00:04:46 +00:00
if ( elemData [ elemId ] . zoomLevel > 7 ) {
2023-05-28 17:22:35 +00:00
delta = 0.9 ;
2023-06-04 00:04:46 +00:00
} else if ( elemData [ elemId ] . zoomLevel > 2 ) {
2023-05-28 17:22:35 +00:00
delta = 0.6 ;
}
zoomPosX = e . clientX ;
zoomPosY = e . clientY ;
fullScreenMode = false ;
2023-06-04 00:04:46 +00:00
elemData [ elemId ] . zoomLevel = updateZoom (
elemData [ elemId ] . zoomLevel +
( operation === "+" ? delta : - delta ) ,
2023-05-28 17:22:35 +00:00
zoomPosX - targetElement . getBoundingClientRect ( ) . left ,
zoomPosY - targetElement . getBoundingClientRect ( ) . top
) ;
}
}
/ * *
* This function fits the target element to the screen by calculating
* the required scale and offsets . It also updates the global variables
* zoomLevel , panX , and panY to reflect the new state .
* /
function fitToElement ( ) {
//Reset Zoom
targetElement . style . transform = ` translate( ${ 0 } px, ${ 0 } px) scale( ${ 1 } ) ` ;
// Get element and screen dimensions
const elementWidth = targetElement . offsetWidth ;
const elementHeight = targetElement . offsetHeight ;
const parentElement = targetElement . parentElement ;
const screenWidth = parentElement . clientWidth ;
const screenHeight = parentElement . clientHeight ;
// Get element's coordinates relative to the parent element
const elementRect = targetElement . getBoundingClientRect ( ) ;
const parentRect = parentElement . getBoundingClientRect ( ) ;
const elementX = elementRect . x - parentRect . x ;
// Calculate scale and offsets
const scaleX = screenWidth / elementWidth ;
const scaleY = screenHeight / elementHeight ;
const scale = Math . min ( scaleX , scaleY ) ;
const transformOrigin =
window . getComputedStyle ( targetElement ) . transformOrigin ;
const [ originX , originY ] = transformOrigin . split ( " " ) ;
const originXValue = parseFloat ( originX ) ;
const originYValue = parseFloat ( originY ) ;
const offsetX =
( screenWidth - elementWidth * scale ) / 2 -
originXValue * ( 1 - scale ) ;
const offsetY =
( screenHeight - elementHeight * scale ) / 2.5 -
originYValue * ( 1 - scale ) ;
// Apply scale and offsets to the element
targetElement . style . transform = ` translate( ${ offsetX } px, ${ offsetY } px) scale( ${ scale } ) ` ;
// Update global variables
2023-06-04 00:04:46 +00:00
elemData [ elemId ] . zoomLevel = scale ;
elemData [ elemId ] . panX = offsetX ;
elemData [ elemId ] . panY = offsetY ;
2023-05-28 17:22:35 +00:00
fullScreenMode = false ;
toggleOverlap ( "off" ) ;
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
/ * *
* This function fits the target element to the screen by calculating
* the required scale and offsets . It also updates the global variables
* zoomLevel , panX , and panY to reflect the new state .
* /
// Fullscreen mode
function fitToScreen ( ) {
2023-05-28 17:32:21 +00:00
const canvas = gradioApp ( ) . querySelector (
2023-05-28 17:22:35 +00:00
` ${ elemId } canvas[key="interface"] `
) ;
if ( ! canvas ) return ;
if ( canvas . offsetWidth > 862 ) {
targetElement . style . width = canvas . offsetWidth + "px" ;
}
if ( fullScreenMode ) {
resetZoom ( ) ;
fullScreenMode = false ;
return ;
}
//Reset Zoom
targetElement . style . transform = ` translate( ${ 0 } px, ${ 0 } px) scale( ${ 1 } ) ` ;
2023-05-30 13:35:52 +00:00
// Get scrollbar width to right-align the image
2023-05-31 20:02:49 +00:00
const scrollbarWidth =
window . innerWidth - document . documentElement . clientWidth ;
2023-05-30 13:35:52 +00:00
2023-05-28 17:22:35 +00:00
// Get element and screen dimensions
const elementWidth = targetElement . offsetWidth ;
const elementHeight = targetElement . offsetHeight ;
2023-05-30 13:35:52 +00:00
const screenWidth = window . innerWidth - scrollbarWidth ;
2023-05-28 17:22:35 +00:00
const screenHeight = window . innerHeight ;
// Get element's coordinates relative to the page
const elementRect = targetElement . getBoundingClientRect ( ) ;
const elementY = elementRect . y ;
const elementX = elementRect . x ;
// Calculate scale and offsets
const scaleX = screenWidth / elementWidth ;
const scaleY = screenHeight / elementHeight ;
const scale = Math . min ( scaleX , scaleY ) ;
// Get the current transformOrigin
const computedStyle = window . getComputedStyle ( targetElement ) ;
const transformOrigin = computedStyle . transformOrigin ;
const [ originX , originY ] = transformOrigin . split ( " " ) ;
const originXValue = parseFloat ( originX ) ;
const originYValue = parseFloat ( originY ) ;
// Calculate offsets with respect to the transformOrigin
const offsetX =
( screenWidth - elementWidth * scale ) / 2 -
elementX -
originXValue * ( 1 - scale ) ;
const offsetY =
( screenHeight - elementHeight * scale ) / 2 -
elementY -
originYValue * ( 1 - scale ) ;
// Apply scale and offsets to the element
targetElement . style . transform = ` translate( ${ offsetX } px, ${ offsetY } px) scale( ${ scale } ) ` ;
// Update global variables
2023-06-04 00:04:46 +00:00
elemData [ elemId ] . zoomLevel = scale ;
elemData [ elemId ] . panX = offsetX ;
elemData [ elemId ] . panY = offsetY ;
2023-05-28 17:22:35 +00:00
fullScreenMode = true ;
toggleOverlap ( "on" ) ;
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Handle keydown events
function handleKeyDown ( event ) {
const hotkeyActions = {
2023-06-01 22:04:17 +00:00
[ hotkeysConfig . canvas _hotkey _reset ] : resetZoom ,
[ hotkeysConfig . canvas _hotkey _overlap ] : toggleOverlap ,
[ hotkeysConfig . canvas _hotkey _fullscreen ] : fitToScreen
2023-05-28 17:22:35 +00:00
} ;
const action = hotkeyActions [ event . code ] ;
if ( action ) {
event . preventDefault ( ) ;
action ( event ) ;
}
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Get Mouse position
function getMousePosition ( e ) {
mouseX = e . offsetX ;
mouseY = e . offsetY ;
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
targetElement . addEventListener ( "mousemove" , getMousePosition ) ;
// Handle events only inside the targetElement
let isKeyDownHandlerAttached = false ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
function handleMouseMove ( ) {
if ( ! isKeyDownHandlerAttached ) {
document . addEventListener ( "keydown" , handleKeyDown ) ;
isKeyDownHandlerAttached = true ;
2023-06-04 00:04:46 +00:00
activeElement = elemId ;
2023-05-28 17:22:35 +00:00
}
2023-05-27 19:54:45 +00:00
}
2023-05-28 17:22:35 +00:00
function handleMouseLeave ( ) {
if ( isKeyDownHandlerAttached ) {
document . removeEventListener ( "keydown" , handleKeyDown ) ;
isKeyDownHandlerAttached = false ;
2023-06-04 00:04:46 +00:00
activeElement = null ;
2023-05-28 17:22:35 +00:00
}
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Add mouse event handlers
targetElement . addEventListener ( "mousemove" , handleMouseMove ) ;
targetElement . addEventListener ( "mouseleave" , handleMouseLeave ) ;
// Reset zoom when click on another tab
elements . img2imgTabs . addEventListener ( "click" , resetZoom ) ;
elements . img2imgTabs . addEventListener ( "click" , ( ) => {
// targetElement.style.width = "";
if ( parseInt ( targetElement . style . width ) > 865 ) {
setTimeout ( fitToElement , 0 ) ;
}
} ) ;
targetElement . addEventListener ( "wheel" , e => {
// change zoom level
const operation = e . deltaY > 0 ? "-" : "+" ;
changeZoomLevel ( operation , e ) ;
// Handle brush size adjustment with ctrl key pressed
2023-06-02 23:18:49 +00:00
if (
( hotkeysConfig . canvas _swap _controls && e . shiftKey ) ||
( ! hotkeysConfig . canvas _swap _controls &&
( e . ctrlKey || e . metaKey ) )
) {
2023-05-28 17:22:35 +00:00
e . preventDefault ( ) ;
// Increase or decrease brush size based on scroll direction
adjustBrushSize ( elemId , e . deltaY ) ;
}
} ) ;
2023-05-31 20:02:49 +00:00
// Handle the move event for pan functionality. Updates the panX and panY variables and applies the new transform to the target element.
2023-05-28 17:22:35 +00:00
function handleMoveKeyDown ( e ) {
2023-06-01 22:04:17 +00:00
if ( e . code === hotkeysConfig . canvas _hotkey _move ) {
2023-05-31 20:02:49 +00:00
if ( ! e . ctrlKey && ! e . metaKey && isKeyDownHandlerAttached ) {
e . preventDefault ( ) ;
document . activeElement . blur ( ) ;
2023-05-28 17:22:35 +00:00
isMoving = true ;
}
}
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
function handleMoveKeyUp ( e ) {
2023-06-01 22:04:17 +00:00
if ( e . code === hotkeysConfig . canvas _hotkey _move ) {
2023-05-28 17:22:35 +00:00
isMoving = false ;
}
}
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
document . addEventListener ( "keydown" , handleMoveKeyDown ) ;
document . addEventListener ( "keyup" , handleMoveKeyUp ) ;
2023-05-27 19:54:45 +00:00
2023-05-28 17:22:35 +00:00
// Detect zoom level and update the pan speed.
function updatePanPosition ( movementX , movementY ) {
2023-06-04 00:04:46 +00:00
let panSpeed = 2 ;
2023-05-27 19:54:45 +00:00
2023-06-04 00:04:46 +00:00
if ( elemData [ elemId ] . zoomLevel > 8 ) {
panSpeed = 3.5 ;
2023-05-28 17:22:35 +00:00
}
2023-05-27 19:54:45 +00:00
2023-06-04 00:04:46 +00:00
elemData [ elemId ] . panX =
elemData [ elemId ] . panX + movementX * panSpeed ;
elemData [ elemId ] . panY =
elemData [ elemId ] . panY + movementY * panSpeed ;
2023-05-27 19:54:45 +00:00
2023-06-04 00:04:46 +00:00
targetElement . style . transform = ` translate( ${ elemData [ elemId ] . panX } px, ${ elemData [ elemId ] . panY } px) scale( ${ elemData [ elemId ] . zoomLevel } ) ` ;
2023-05-28 17:22:35 +00:00
toggleOverlap ( "on" ) ;
}
function handleMoveByKey ( e ) {
2023-06-04 00:04:46 +00:00
if ( isMoving && elemId === activeElement ) {
2023-05-28 17:22:35 +00:00
updatePanPosition ( e . movementX , e . movementY ) ;
targetElement . style . pointerEvents = "none" ;
} else {
targetElement . style . pointerEvents = "auto" ;
}
}
2023-05-31 20:02:49 +00:00
// Prevents sticking to the mouse
window . onblur = function ( ) {
isMoving = false ;
} ;
2023-05-28 17:32:21 +00:00
gradioApp ( ) . addEventListener ( "mousemove" , handleMoveByKey ) ;
2023-05-28 17:22:35 +00:00
}
2023-05-27 19:54:45 +00:00
2023-06-03 16:24:05 +00:00
applyZoomAndPan ( elementIDs . sketch ) ;
applyZoomAndPan ( elementIDs . inpaint ) ;
applyZoomAndPan ( elementIDs . inpaintSketch ) ;
// Make the function global so that other extensions can take advantage of this solution
window . applyZoomAndPan = applyZoomAndPan ;
2023-05-27 19:54:45 +00:00
} ) ;