From 67b2d0c8ab2641bcc0e98719488d562a8d302cfd Mon Sep 17 00:00:00 2001 From: David Rowe Date: Fri, 13 Mar 2015 12:18:49 -0700 Subject: [PATCH] Trim text to fit in window --- examples/users.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/users.js b/examples/users.js index 9604f1c248..dec141127e 100644 --- a/examples/users.js +++ b/examples/users.js @@ -107,6 +107,8 @@ var usersWindow = (function () { myUsername, user, userText, + textWidth, + maxTextWidth, i; myUsername = GlobalServices.username; @@ -118,7 +120,21 @@ var usersWindow = (function () { if (user.location.root) { userText += " @ " + user.location.root.name; } - usersOnline[i].textWidth = Overlays.textSize(windowPane2D, userText).width; + textWidth = Overlays.textSize(windowPane2D, userText).width; + + maxTextWidth = WINDOW_WIDTH_2D - 2 * WINDOW_MARGIN_2D; + if (textWidth > maxTextWidth) { + // Trim and append "..." to fit window width + maxTextWidth = maxTextWidth - Overlays.textSize(windowPane2D, "...").width; + while (textWidth > maxTextWidth) { + userText = userText.slice(0, -1); + textWidth = Overlays.textSize(windowPane2D, userText).width; + } + userText += "..."; + textWidth = Overlays.textSize(windowPane2D, userText).width; + } + + usersOnline[i].textWidth = textWidth; linesOfUsers.push(i); displayText += "\n" + userText; }