From 1fdf49fe85c6ec28a55bf8bb6d53b7ba023c636a Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 18 Jul 2024 12:46:01 -0700 Subject: [PATCH] Truncate overflowing combo widget text (#17) * Truncate overflowing combo widget text * Avoid uneccessary calls to * Use unicode ellipsis * Only measure first 3 chars --- src/litegraph.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/litegraph.js b/src/litegraph.js index 23c524dc7..c0e20e526 100755 --- a/src/litegraph.js +++ b/src/litegraph.js @@ -9958,6 +9958,30 @@ LGraphNode.prototype.executeAction = function(action) if(values && values.constructor !== Array) v = values[ w.value ]; } + const labelWidth = ctx.measureText(w.label || w.name).width + margin * 2; + const inputWidth = widget_width - margin * 4 + const availableWidth = inputWidth - labelWidth + const textWidth = ctx.measureText(v).width; + if (textWidth > availableWidth) { + const ELLIPSIS = "\u2026"; + const ellipsisWidth = ctx.measureText(ELLIPSIS).width; + const charWidthAvg = ctx.measureText("a").width; + if (availableWidth <= ellipsisWidth) { + v = "\u2024"; // One dot leader + } else { + const overflowWidth = (textWidth + ellipsisWidth) - availableWidth; + // Only first 3 characters need to be measured precisely + if ( overflowWidth + charWidthAvg * 3 > availableWidth) { + const preciseRange = availableWidth + charWidthAvg * 3; + const preTruncateCt = Math.floor((preciseRange - ellipsisWidth) / charWidthAvg); + v = v.substr(0, preTruncateCt) + } + while (ctx.measureText(v).width + ellipsisWidth > availableWidth) { + v = v.substr(0, v.length - 1); + } + v += ELLIPSIS; + } + } ctx.fillText( v, widget_width - margin * 2 - 20,