Thread Tools Display Modes
07-11-23, 07:58 PM   #1
Garmsen
A Defias Bandit
Join Date: Jul 2023
Posts: 2
SetText on AceGUI multi line edit box results in no text after AFK

Edit: Sorry if this is in the wrong section, just noticed there's a Lua/XML Help section.

Hi.

I'm a few months into learning Lua and addon development.

My first addon allows players to customize each chat frame.

One of the features allows players to copy messages from a chat frame by duplicating them to an AceGUI multi line edit box when you hold control and left click on a chat tab.

I noticed that if I AFK for about 10 minutes and come back, sometimes no messages show up when I try to open the frame.

Lua Code:
  1. local AceGUI = LibStub("AceGUI-3.0")
  2.  
  3. --- @class Private
  4. local Private = select(2, ...)
  5.  
  6. --- @class CopyModule: AceModule
  7. local CopyModule = Private.Addon:NewModule("Copy")
  8.  
  9. --- @class CopyConstants
  10. local CopyConstants = Private.CopyConstants
  11.  
  12. --- @class ChatFrameUtils
  13. local ChatFrameUtils = Private.ChatFrameUtils
  14.  
  15. --- @class DatabaseUtils
  16. local DatabaseUtils = Private.DatabaseUtils
  17.  
  18. --- @class CopyState
  19. --- @field copyFrame table?
  20. --- @field multiLineEditBox table?
  21. --- @field text string?
  22. local state = {
  23.     copyFrame = nil,
  24.     multiLineEditBox = nil,
  25.     text = "",
  26. }
  27.  
  28. --- Configures the copy frame.
  29. local function configureCopyFrame(copyFrame)
  30.     copyFrame:SetTitle(CopyConstants.FRAME_TITLE)
  31.     copyFrame:SetWidth(CopyConstants.FRAME_WIDTH)
  32.     copyFrame:SetHeight(CopyConstants.FRAME_HEIGHT)
  33.     copyFrame:SetLayout(CopyConstants.FRAME_LAYOUT)
  34.     copyFrame.frame:EnableKeyboard(true)
  35.     copyFrame.frame:SetClampedToScreen(true)
  36.     copyFrame.frame:SetFrameStrata(CopyConstants.FRAME_STRATA)
  37.     copyFrame.frame:SetPropagateKeyboardInput(true)
  38. end
  39.  
  40. --- Creates the copy frame.
  41. local function createCopyFrame()
  42.     local copyFrame = AceGUI:Create("Frame")
  43.  
  44.     configureCopyFrame(copyFrame)
  45.  
  46.     return copyFrame
  47. end
  48.  
  49. --- Handles the text changing in the multi line edit box.
  50. local function onTextChangedCallback(widget)
  51.     local currentText = state.text
  52.     local cursorPosition = widget:GetCursorPosition()
  53.  
  54.     widget:SetText(currentText)
  55.     widget:SetCursorPosition(cursorPosition)
  56. end
  57.  
  58. --- Configures the multi line edit box.
  59. local function configureMultiLineEditBox(multiLineEditBox)
  60.     multiLineEditBox:DisableButton(true)
  61.     multiLineEditBox:SetLabel("")
  62.     multiLineEditBox:SetCallback("OnTextChanged", onTextChangedCallback)
  63. end
  64.  
  65. --- Creates the multi line edit box.
  66. local function createMultiLineEditBox()
  67.     local multiLineEditBox = AceGUI:Create("MultiLineEditBox")
  68.  
  69.     configureMultiLineEditBox(multiLineEditBox)
  70.  
  71.     return multiLineEditBox
  72. end
  73.  
  74. --- Updates the copy frame with the chat frame messages.
  75. --- @param chatFrame table
  76. local function updateCopyFrame(chatFrame)
  77.     state.text = table.concat(ChatFrameUtils:GetChatFrameMessages(chatFrame), "\n")
  78.     state.multiLineEditBox:SetText(state.text)
  79.  
  80.     -- To scroll to the bottom of the multi line edit box, we need to wait.
  81.     C_Timer.After(0, function()
  82.         state.multiLineEditBox:SetCursorPosition(#state.text)
  83.     end)
  84. end
  85.  
  86. --- Closes the copy frame.
  87. local function closeCopyFrame()
  88.     state.copyFrame:Hide()
  89.  
  90.     AceGUI:Release(state.copyFrame)
  91.  
  92.     state.copyFrame = nil
  93. end
  94.  
  95. --- Sets the escape key handler for a frame.
  96. local function setEscapeKeyHandler(frame, editBox)
  97.     frame:SetScript("OnKeyDown", function(self, key)
  98.         if key ~= "ESCAPE" then
  99.             self:SetPropagateKeyboardInput(true)
  100.  
  101.             return
  102.         end
  103.  
  104.         closeCopyFrame()
  105.  
  106.         self:SetPropagateKeyboardInput(false)
  107.     end)
  108. end
  109.  
  110. --- Initializes the copy frame.
  111. local function initializeCopyFrame()
  112.     local copyFrame = createCopyFrame()
  113.     local multiLineEditBox = createMultiLineEditBox()
  114.  
  115.     copyFrame:AddChild(multiLineEditBox)
  116.  
  117.     setEscapeKeyHandler(copyFrame.frame, multiLineEditBox.editBox)
  118.  
  119.     state.copyFrame = copyFrame
  120.     state.multiLineEditBox = multiLineEditBox
  121.     state.text = ""
  122. end
  123.  
  124. --- Shows the copy frame.
  125. --- @param chatFrame table
  126. function CopyModule:ShowCopyFrame(chatFrame)
  127.     if not state.copyFrame then
  128.         initializeCopyFrame()
  129.     end
  130.  
  131.     updateCopyFrame(chatFrame)
  132.  
  133.     state.copyFrame:Show()
  134.     state.copyFrame.frame:SetPropagateKeyboardInput(true)
  135. end

I have tried to debug the problem as shown below. I was able to reproduce the issue of the messages not showing up in the multi line edit box, but they would appear in ChatFrame4 and ChatFrame5.

Lua Code:
  1. --- Updates the copy frame with the chat frame messages.
  2. --- @param chatFrame table
  3. local function updateCopyFrame(chatFrame)
  4.     state.text = table.concat(ChatFrameUtils:GetChatFrameMessages(chatFrame), "\n")
  5.     ChatFrame5:AddMessage(state.text)
  6.     state.multiLineEditBox:SetText(state.text)
  7.  
  8.     for _, msg in ipairs(ChatFrameUtils:GetChatFrameMessages(chatFrame)) do
  9.         ChatFrame4:AddMessage(msg)
  10.     end
  11.  
  12.     C_Timer.After(2, function()
  13.         state.multiLineEditBox:SetText(state.text)
  14.     end)
  15.  
  16.     -- To scroll to the bottom of the multi line edit box, we need to wait.
  17.     C_Timer.After(4, function()
  18.         state.multiLineEditBox:SetCursorPosition(#state.text)
  19.     end)
  20. end

My code is on GitHub as well so it might be easier to look at here: https://github.com/drzewieckinichola...dules/Copy.lua

The files we're probably interested in are Modules/Copy.lua, Hooks/ChatFrame.lua, and I guess Utils/ChatFrame.lua, but I'm assuming the problem is in Modules/Copy.lua.

Does anybody have any idea what I'm doing wrong?

Here are some images as well: https://imgur.com/a/M46PsRp

Thanks in advance!

Last edited by Garmsen : 07-11-23 at 08:14 PM.
  Reply With Quote
08-16-23, 01:08 PM   #2
Garmsen
A Defias Bandit
Join Date: Jul 2023
Posts: 2
I'm providing an update for anyone that runs across this issue in the future.

The issue ended up being due to trying to copy over messages with protected strings (info towards bottom of this page): https://wowpedia.fandom.com/wiki/UI_escape_sequences

An example of a protected string is that message in chat that occurs when a Battle.net friend logs on or off (if you have that option enabled). It was causing all of my copy frame messages to disappear because I am using a single string with line breaks.

Solution:
I couldn't find anybody that was able to figure out what the issue was so I started looking through GitHub repos to see if others were doing something special when copying over chat messages to a frame. Credit to https://github.com/tukui-org/ElvUI as that was the repo I referenced, but there are plenty of other repos with similar functions.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » SetText on AceGUI multi line edit box results in no text after AFK


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off