From c9383e5411022e1bcda256d8d317a30bae4de3ea Mon Sep 17 00:00:00 2001 From: Infi Date: Fri, 17 May 2024 07:26:47 +0200 Subject: [PATCH] Don't encode the preview message every frame --- ChatTwo/Ui/ChatLogWindow.cs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/ChatTwo/Ui/ChatLogWindow.cs b/ChatTwo/Ui/ChatLogWindow.cs index 5d2815b..120409b 100644 --- a/ChatTwo/Ui/ChatLogWindow.cs +++ b/ChatTwo/Ui/ChatLogWindow.cs @@ -67,7 +67,9 @@ public sealed class ChatLogWindow : Window private int AutoCompleteSelection; private bool AutoCompleteShouldScroll; + private int LastLength; private float PreviewHeight; + private Message? PreviewMessage; public int CursorPos; @@ -518,16 +520,19 @@ public sealed class ChatLogWindow : Window WasDocked = ImGui.IsWindowDocked(); // We Predraw this once to get the actual height :HideThePain: - // TODO: I hate this predraw thing PreviewHeight = 0; - Message? predrawnMessage = null; if (Plugin.Config.PreviewPosition is PreviewPosition.Inside && !string.IsNullOrEmpty(Chat)) { - var bytes = Encoding.UTF8.GetBytes(Chat.Trim()); - AutoTranslate.ReplaceWithPayload(Plugin.DataManager, ref bytes); + if (PreviewMessage == null || LastLength != Chat.Length) + { + LastLength = Chat.Length; - var chunks = ChunkUtil.ToChunks(SeString.Parse(bytes), ChunkSource.Content, ChatType.Say).ToList(); - predrawnMessage = Message.FakeMessage(chunks, new ChatCode((ushort) XivChatType.Say)); + var bytes = Encoding.UTF8.GetBytes(Chat.Trim()); + AutoTranslate.ReplaceWithPayload(Plugin.DataManager, ref bytes); + + var chunks = ChunkUtil.ToChunks(SeString.Parse(bytes), ChunkSource.Content, ChatType.Say).ToList(); + PreviewMessage = Message.FakeMessage(chunks, new ChatCode((ushort)XivChatType.Say)); + } var pos = ImGui.GetCursorPos(); ImGui.SetCursorPos(new Vector2(-500, -500)); @@ -535,13 +540,18 @@ public sealed class ChatLogWindow : Window using (ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero)) { ImGui.TextUnformatted(Language.Options_Preview_Header); - DrawChunks(predrawnMessage.Content); + DrawChunks(PreviewMessage.Content); } var after = ImGui.GetCursorPosY(); ImGui.SetCursorPos(pos); PreviewHeight = after - before; } + else + { + LastLength = 0; + PreviewMessage = null; + } var currentTab = Plugin.Config.SidebarTabView ? DrawTabSidebar() : DrawTabBar(); @@ -549,12 +559,12 @@ public sealed class ChatLogWindow : Window if (currentTab > -1 && currentTab < Plugin.Config.Tabs.Count) activeTab = Plugin.Config.Tabs[currentTab]; - if (predrawnMessage != null) + if (PreviewMessage != null) { using (ImRaii.PushStyle(ImGuiStyleVar.ItemSpacing, Vector2.Zero)) { ImGui.TextUnformatted("Text Preview:"); - DrawChunks(predrawnMessage.Content); + DrawChunks(PreviewMessage.Content); } }