From 1eaac5ccb82e5877081a51fbf693eaa80226edbb Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Wed, 16 Feb 2022 13:28:13 -0500 Subject: [PATCH] fix: handle newlines and close-to-edge wrapping better --- ChatTwo/Util/ImGuiUtil.cs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/ChatTwo/Util/ImGuiUtil.cs b/ChatTwo/Util/ImGuiUtil.cs index 7eea3a7..96a7067 100755 --- a/ChatTwo/Util/ImGuiUtil.cs +++ b/ChatTwo/Util/ImGuiUtil.cs @@ -82,21 +82,31 @@ internal static class ImGuiUtil { // empty string if (text == null) { ImGui.TextUnformatted(""); - ImGui.TextUnformatted(""); - return; + continue; } var widthLeft = ImGui.GetContentRegionAvail().X; var endPrevLine = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, textEnd, widthLeft); if (endPrevLine == null) { - return; + continue; } - Text(text, endPrevLine); + var firstSpace = FindFirstSpace(text, textEnd); + var properBreak = firstSpace <= endPrevLine; + if (properBreak) { + Text(text, endPrevLine); + } else { + ImGui.TextUnformatted(""); + } widthLeft = ImGui.GetContentRegionAvail().X; while (endPrevLine < textEnd) { - text = endPrevLine; + if (properBreak) { + text = endPrevLine; + } + + properBreak = true; + if (*text == ' ') { ++text; } // skip a space at start of line @@ -119,6 +129,16 @@ internal static class ImGuiUtil { } } + private static unsafe byte* FindFirstSpace(byte* text, byte* textEnd) { + for (var i = text; i < textEnd; i++) { + if (char.IsWhiteSpace((char) *i)) { + return i; + } + } + + return textEnd; + } + internal static bool IconButton(FontAwesomeIcon icon, string? id = null, string? tooltip = null) { ImGui.PushFont(UiBuilder.IconFont);