File size: 5,987 Bytes
c336648 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
function createToast() {
const toast = document.createElement("div");
toast.id = "toast";
toast.className = "toast";
toast.textContent = "Warning, you are now using the INPAINT model";
document.body.appendChild(toast);
return toast;
}
function createInpaintButton() {
const button = document.createElement("div");
button.className = "inpaint-button";
button.textContent = "🎨";
button.style.fontSize = "14px";
button.style.background = "#1b2636";
button.style.color = "white";
button.style.border = "1px solid white";
button.style.minWidth = "1px";
button.style.display = "none";
button.style.padding = "0px 4px";
button.style.justifyContent = "center";
button.style.alignItems = "center";
button.classList.add("gradio-button", "tool");
// Add tooltip
const tooltipText = "Inpaint model is currently selected";
button.setAttribute("title", tooltipText);
return button;
}
function darkenColor(color, amount) {
const num = parseInt(color.slice(1), 16);
const r = Math.max((num >> 16) - amount, 0);
const g = Math.max(((num >> 8) & 0x00ff) - amount, 0);
const b = Math.max((num & 0x0000ff) - amount, 0);
return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, "0")}`;
}
function getSelectedButtonValue(tabNav) {
const selectedButton = tabNav.querySelector(".selected");
return selectedButton ? selectedButton.textContent.trim() : null;
}
function showToastOrNot(tabName) {
if (tabName === "img2img") {
return true;
}
if (tabName === "Sketch") {
return true;
}
if (tabName === "Batch") {
return true;
}
return false;
}
// Wait until opts loaded
async function waitForOpts() {
for (;;) {
if (window.opts && Object.keys(window.opts).length) {
return window.opts;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
function addHoverStyle(normalColor, hoverColor) {
const style = document.createElement("style");
style.type = "text/css";
style.innerHTML = `
.generate-btn {
background-color: ${normalColor};
}
.generate-btn:hover {
background-color: ${hoverColor};
}
`;
document.head.appendChild(style);
}
function setButtonHoverColor(button, normalColor, hoverColor) {
button.addEventListener("mouseenter", () => {
button.style.background = hoverColor;
});
button.addEventListener("mouseleave", () => {
button.style.background = normalColor;
});
}
function customizeButton(button, normalColor, hoverColor) {
button.style.background = normalColor;
button.style.border = normalColor;
setButtonHoverColor(button, normalColor, hoverColor);
}
function updateButtonColors(buttons, enableBtnColor, normalColor, hoverColor) {
buttons.forEach((button) => {
if (enableBtnColor) {
customizeButton(button, normalColor, hoverColor);
} else {
button.style.background = "";
button.style.border = "";
setButtonHoverColor(button, "", "");
}
});
}
function showToast() {
toast.classList.add("show");
setTimeout(() => {
toast.classList.remove("show");
}, 5000);
}
// The function that will be called when the input value changes
const handleInputChange = (input, inpaintingPattern, inpaintButton) => {
const modelClass = input.value;
if (inpaintingPattern.test(modelClass)) {
inpaintButton.style.display = "flex";
} else {
inpaintButton.style.display = "none";
}
};
onUiLoaded(async () => {
const opts = await waitForOpts();
const preConfig = {
canvas_zoom_inpaint_warning: true,
canvas_zoom_inpaint_label: true,
canvas_zoom_inpaint_change_btn_color: false,
canvas_zoom_inpaint_btn_color: "#C33227",
};
const config = {
...preConfig,
...opts,
};
const txt2imgGenerateBtn = document.querySelector("#txt2img_generate");
const img2imgGenerateBtn = document.querySelector("#img2img_generate");
const input = document.querySelector("#setting_sd_model_checkpoint input");
const quickSettings = document.querySelector("#quicksettings button");
const inpaintButton = createInpaintButton();
const toast = createToast();
const buttons = [txt2imgGenerateBtn, img2imgGenerateBtn];
const enableWarning = config.canvas_zoom_inpaint_warning;
const enableLabel = config.canvas_zoom_inpaint_label;
const enableBtnColor = config.canvas_zoom_inpaint_change_btn_color;
const inpaintingPattern = /(?:inpainting|inpaint)/i;
const normalColor = config.canvas_zoom_inpaint_btn_color;
const hoverColor = darkenColor(normalColor, 20);
if (enableLabel) {
quickSettings.insertAdjacentElement("beforebegin", inpaintButton);
handleInputChange(input, inpaintingPattern, inpaintButton)
}
if (enableBtnColor) {
if (inpaintingPattern.test(input.value)) {
updateButtonColors(buttons, true, normalColor, hoverColor);
}
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
// if (mutation.type === "attributes" && mutation.attributeName === "class") {
setTimeout(() => { // delay check for 3 seconds
// console.log(input.value)
if (enableLabel) {
handleInputChange(input, inpaintingPattern, inpaintButton);
}
if (enableBtnColor) {
const modelClass = input.value;
if (inpaintingPattern.test(modelClass)) {
// Enable button colors
updateButtonColors(buttons, true, normalColor, hoverColor);
} else {
// Disable button colors
updateButtonColors(buttons, false, normalColor, hoverColor);
}
}
}, 3000);
}
// }
});
// Beginning Observation
observer.observe(input, { attributes: true });
txt2imgGenerateBtn.addEventListener("click", () => {
const modelClass = input.value;
if (inpaintingPattern.test(modelClass)) {
if (enableWarning) {
showToast();
}
}
});
});
|