fix: handle newlines and close-to-edge wrapping better

This commit is contained in:
Anna 2022-02-16 13:28:13 -05:00
parent 2d49efb4af
commit 1eaac5ccb8
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
1 changed files with 25 additions and 5 deletions

View File

@ -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);