Truncate overflowing combo widget text (#17)

* Truncate overflowing combo widget text

* Avoid uneccessary calls to

* Use unicode ellipsis

* Only measure first 3 chars
This commit is contained in:
bymyself
2024-07-18 12:46:01 -07:00
committed by GitHub
parent ac8cb33ac1
commit 1fdf49fe85

View File

@@ -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,