mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-08 09:00:05 +00:00
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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user