Optimize tooltip checks

* Instead of traversing tens of thousands of text nodes, only look at elements and their children
* Debounce the checks to happen only every one second
This commit is contained in:
Aarni Koskela 2023-05-24 20:19:16 +03:00
parent a6e653be26
commit d66c64b9d7

View File

@ -116,17 +116,17 @@ var titles = {
"Negative Guidance minimum sigma": "Skip negative prompt for steps where image is already mostly denoised; the higher this value, the more skips there will be; provides increased performance in exchange for minor quality reduction." "Negative Guidance minimum sigma": "Skip negative prompt for steps where image is already mostly denoised; the higher this value, the more skips there will be; provides increased performance in exchange for minor quality reduction."
}; };
function updateTooltipForSpan(span) { function updateTooltip(element) {
if (span.title) return; // already has a title if (element.title) return; // already has a title
let tooltip = localization[titles[span.textContent]] || titles[span.textContent]; let tooltip = localization[titles[element.textContent]] || titles[element.textContent];
if (!tooltip) { if (!tooltip) {
tooltip = localization[titles[span.value]] || titles[span.value]; tooltip = localization[titles[element.value]] || titles[element.value];
} }
if (!tooltip) { if (!tooltip) {
for (const c of span.classList) { for (const c of element.classList) {
if (c in titles) { if (c in titles) {
tooltip = localization[titles[c]] || titles[c]; tooltip = localization[titles[c]] || titles[c];
break; break;
@ -135,34 +135,39 @@ function updateTooltipForSpan(span) {
} }
if (tooltip) { if (tooltip) {
span.title = tooltip; element.title = tooltip;
} }
} }
function updateTooltipForSelect(select) { // Nodes to check for adding tooltips.
if (select.onchange != null) return; const tooltipCheckNodes = new Set();
// Timer for debouncing tooltip check.
let tooltipCheckTimer = null;
select.onchange = function() { function processTooltipCheckNodes() {
select.title = localization[titles[select.value]] || titles[select.value] || ""; for (const node of tooltipCheckNodes) {
}; updateTooltip(node);
}
tooltipCheckNodes.clear();
} }
var observedTooltipElements = {SPAN: 1, BUTTON: 1, SELECT: 1, P: 1}; onUiUpdate(function(mutationRecords) {
for (const record of mutationRecords) {
onUiUpdate(function(m) { for (const node of record.addedNodes) {
m.forEach(function(record) { if (node.nodeType === Node.ELEMENT_NODE && !node.classList.contains("hide")) {
record.addedNodes.forEach(function(node) { if (
if (observedTooltipElements[node.tagName]) { node.tagName === "SPAN" ||
updateTooltipForSpan(node); node.tagName === "BUTTON" ||
node.tagName === "P"
) {
tooltipCheckNodes.add(node);
}
node.querySelectorAll('span, button, p').forEach(n => tooltipCheckNodes.add(n));
} }
if (node.tagName == "SELECT") { }
updateTooltipForSelect(node); }
} if (tooltipCheckNodes.size) {
clearTimeout(tooltipCheckTimer);
if (node.querySelectorAll) { tooltipCheckTimer = setTimeout(processTooltipCheckNodes, 1000);
node.querySelectorAll('span, button, select, p').forEach(updateTooltipForSpan); }
node.querySelectorAll('select').forEach(updateTooltipForSelect);
}
});
});
}); });