-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUtils.lua
More file actions
389 lines (324 loc) · 13.4 KB
/
Utils.lua
File metadata and controls
389 lines (324 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
-- Utils.lua
-- Utilities, Link formatting
MessageBox.URLFuncs = {
["WWW"] = function(a1,a2,a3) return MessageBox:FormatLink(MessageBox.URLPattern.WWW.fm,a1,a2,a3) end,
["PROTOCOL"] = function(a1,a2) return MessageBox:FormatLink(MessageBox.URLPattern.PROTOCOL.fm,a1,a2) end,
["EMAIL"] = function(a1,a2,a3,a4) return MessageBox:FormatLink(MessageBox.URLPattern.EMAIL.fm,a1,a2,a3,a4) end,
["IP"] = function(a1,a2,a3,a4) return MessageBox:FormatLink(MessageBox.URLPattern.IP.fm,a1,a2,a3,a4) end,
}
function MessageBox:FormatLink(formatter, a1, a2, a3, a4, a5)
if not (formatter and a1) then return end
local newtext = string.format(formatter, a1, a2, a3, a4, a5)
local invalidtld
for _, arg in pairs({a5,a4,a3,a2,a1}) do
if arg and string.find(arg, "(%.%.)$") then
invalidtld = true
break
end
end
if (invalidtld) then return newtext end
return " |cff00ccff|Hurl:" .. newtext .. "|h[" .. newtext .. "]|h|r "
end
function MessageBox:HandleLink(text)
if type(text) ~= "string" then return text or "" end
local URLPattern = self.URLPattern
local URLFuncs = self.URLFuncs
text = string.gsub(text, URLPattern.WWW.rx, URLFuncs.WWW)
text = string.gsub(text, URLPattern.PROTOCOL.rx, URLFuncs.PROTOCOL)
text = string.gsub(text, URLPattern.EMAIL.rx, URLFuncs.EMAIL)
text = string.gsub(text, URLPattern.IP.rx, URLFuncs.IP)
return text
end
function MessageBox:SkinScrollbar(frame)
if not frame then return end
local sb
if frame:GetObjectType() == "Slider" then
sb = frame
else
sb = getglobal(frame:GetName().."ScrollBar")
end
if not sb then return end
local name = sb:GetName()
local buttons = {
getglobal(name.."ScrollUpButton"),
getglobal(name.."ScrollDownButton"),
}
local backgroundRegions = {
getglobal(name.."Top"),
getglobal(name.."Middle"),
getglobal(name.."Bottom"),
}
local thumb = getglobal(name.."ThumbTexture")
if MessageBox.settings.modernTheme then
for _, btn in ipairs(buttons) do
if btn then btn:Hide() end
end
for _, region in ipairs(backgroundRegions) do
if region then region:Hide() end
end
if thumb then
thumb:SetTexture(MessageBox.textures.white8x8)
local r, g, b, a = 0.8, 0.8, 0.8, 1
if MessageBox.settings.highlightColor then
r, g, b, a = unpack(MessageBox.settings.highlightColor)
end
thumb:SetVertexColor(r, g, b, a)
thumb:SetWidth(4)
end
else
for _, btn in ipairs(buttons) do
if btn then btn:Show() end
end
for _, region in ipairs(backgroundRegions) do
if region then region:Show() end
end
if thumb then
thumb:SetTexture(MessageBox.textures.scrollKnob)
thumb:SetVertexColor(1, 1, 1, 1)
thumb:SetWidth(18)
end
end
end
function MessageBox:IsFriend(name)
if MessageBox.friendSet then
return MessageBox.friendSet[string.lower(name)] or false
end
for i = 1, GetNumFriends() do
local fName = GetFriendInfo(i)
if fName and string.lower(fName) == string.lower(name) then
return true
end
end
return false
end
function MessageBox:IsPlayerOnline(playerName)
if MessageBox.onlineStatus then
local status = MessageBox.onlineStatus[string.lower(playerName)]
if status ~= nil then return status end
end
return nil
end
function MessageBox:CalculateDisplayLimit(historyFrame)
if not historyFrame then return 50 end
local frameHeight = historyFrame:GetHeight()
if not frameHeight or frameHeight <= 0 then return 50 end
local fontSize = self.settings.chatFontSize or self.defaultSettings.chatFontSize
local lineHeight = fontSize + 2
local visibleLines = math.ceil(frameHeight / lineHeight)
-- Buffer for wrapped lines, date headers, and the unread separator
local buffer = 10
return visibleLines + buffer
end
function MessageBox:RenderMessages(historyFrame, contact, anchorIndex, unreadCount)
if not historyFrame or not contact then return end
local c = self.conversations[contact]
if not c or not c.messages then
historyFrame:Clear()
return
end
local totalMessages = self:GetCount(c)
if totalMessages == 0 then
historyFrame:Clear()
return
end
if anchorIndex > totalMessages then anchorIndex = totalMessages end
if anchorIndex < 1 then anchorIndex = 1 end
local displayLimit = self:CalculateDisplayLimit(historyFrame)
local startIndex = anchorIndex - displayLimit
if startIndex < 1 then startIndex = 1 end
local splitIndex = 0
if unreadCount and unreadCount > 0 then
splitIndex = totalMessages - unreadCount + 1
end
-- Search highlight state
local searchTerm = nil
local currentMatchMsgIndex = 0
if self.chatSearchActive and self.chatSearchTerm and self.chatSearchTerm ~= "" then
searchTerm = string.lower(self.chatSearchTerm)
if self.chatSearchResults and self.chatSearchCurrentIndex > 0
and self.chatSearchCurrentIndex <= table.getn(self.chatSearchResults) then
currentMatchMsgIndex = self.chatSearchResults[self.chatSearchCurrentIndex]
end
end
historyFrame:Clear()
local lastMessageDate = nil
local timeFmt = self.settings.use12HourFormat and "%I:%M %p" or "%H:%M"
local timeFmtKey = self.settings.use12HourFormat and "12h" or "24h"
if not c._fmtCache then c._fmtCache = {} end
if not c._fmtTimeFmt then c._fmtTimeFmt = "" end
if c._fmtTimeFmt ~= timeFmtKey then
c._fmtCache = {}
c._fmtTimeFmt = timeFmtKey
end
local fmtCache = c._fmtCache
for i = startIndex, anchorIndex do
if i == splitIndex then
historyFrame:AddMessage("|cff444444———|r |cffffffffNew Messages|r |cff444444———|r")
end
local msg = c.messages[i]
local timeVal = c.times[i]
local isOutgoing = c.outgoing[i]
local isSystem = c.system[i]
local formattedMessage
if type(timeVal) == "number" then
local currentMessageDate = date("%Y%m%d", timeVal)
if lastMessageDate ~= currentMessageDate then
if lastMessageDate ~= nil then historyFrame:AddMessage(" ") end
local dateText = "|cff666666— " .. date("%A, %B %d", timeVal) .. " —|r"
historyFrame:AddMessage(dateText)
lastMessageDate = currentMessageDate
end
end
local cached = fmtCache[i]
if cached and not searchTerm then
formattedMessage = cached
else
local timeString
if type(timeVal) == "number" then
timeString = "|cff808080[" .. date(timeFmt, timeVal) .. "]|r"
else
timeString = "|cff808080[" .. tostring(timeVal) .. "]|r"
end
if isSystem then
formattedMessage = string.format("%s %s%s|r", timeString, "|cffffcc00", msg)
else
local cleanMessage = self:HandleLink(msg)
-- Apply search highlighting to the message text
if searchTerm and string.find(string.lower(msg), searchTerm, 1, true) then
local highlightColor = (i == currentMatchMsgIndex) and "|cffFF8800" or "|cffFFFF00"
cleanMessage = self:HighlightSearchTerm(cleanMessage, searchTerm, highlightColor)
end
local nameColor = isOutgoing and "|cff8080ff" or "|cffff80ff"
local displayName = isOutgoing and "You" or contact
formattedMessage = string.format("%s %s%s:|r %s%s|r", timeString, nameColor, displayName, "|cffffffff", cleanMessage)
end
if not searchTerm then
fmtCache[i] = formattedMessage
end
end
historyFrame:AddMessage(formattedMessage)
end
historyFrame:ScrollToBottom()
end
function MessageBox:HighlightSearchTerm(text, searchTerm, highlightColor)
if not text or not searchTerm or searchTerm == "" then return text end
local parts = {}
local partCount = 0
local pos = 1
local textLen = string.len(text)
local termLen = string.len(searchTerm)
while pos <= textLen do
if string.sub(text, pos, pos) == "|" then
local nextChar = string.sub(text, pos + 1, pos + 1)
if nextChar == "c" then
partCount = partCount + 1
parts[partCount] = string.sub(text, pos, pos + 9)
pos = pos + 10
elseif nextChar == "r" then
partCount = partCount + 1
parts[partCount] = "|r"
pos = pos + 2
elseif nextChar == "H" or nextChar == "h" then
local endPos = string.find(text, "|", pos + 1)
if endPos then
partCount = partCount + 1
parts[partCount] = string.sub(text, pos, endPos + 1)
pos = endPos + 2
else
partCount = partCount + 1
parts[partCount] = string.sub(text, pos)
break
end
else
partCount = partCount + 1
parts[partCount] = string.sub(text, pos, pos)
pos = pos + 1
end
else
local chunk = string.sub(text, pos, pos + termLen - 1)
if string.lower(chunk) == searchTerm then
partCount = partCount + 1
parts[partCount] = highlightColor .. chunk .. "|r|cffffffff"
pos = pos + termLen
else
local batchStart = pos
pos = pos + 1
while pos <= textLen do
local ch = string.sub(text, pos, pos)
if ch == "|" then break end
local ahead = string.sub(text, pos, pos + termLen - 1)
if string.lower(ahead) == searchTerm then break end
pos = pos + 1
end
partCount = partCount + 1
parts[partCount] = string.sub(text, batchStart, pos - 1)
end
end
end
return table.concat(parts)
end
function MessageBox:SearchConversation(contact, searchTerm)
local results = {}
if not contact or not searchTerm or searchTerm == "" then return results end
local c = self.conversations[contact]
if not c or not c.messages then return results end
local lowerTerm = string.lower(searchTerm)
local totalMessages = self:GetCount(c)
for i = 1, totalMessages do
if not c.system[i] then
local msg = c.messages[i]
if msg and string.find(string.lower(msg), lowerTerm, 1, true) then
table.insert(results, i)
end
end
end
return results
end
function MessageBox:ChatSearchNavigate(delta)
if not self.chatSearchActive then return end
local count = table.getn(self.chatSearchResults)
if count == 0 then return end
self.chatSearchCurrentIndex = self.chatSearchCurrentIndex + delta
if self.chatSearchCurrentIndex > count then
self.chatSearchCurrentIndex = 1
elseif self.chatSearchCurrentIndex < 1 then
self.chatSearchCurrentIndex = count
end
-- Jump scrollbar to the matched message
local msgIndex = self.chatSearchResults[self.chatSearchCurrentIndex]
if self.chatScrollBar then
self.chatScrollBar.isUpdating = true
self.chatScrollBar:SetMinMaxValues(1, self:GetCount(self.conversations[self.selectedContact]))
self.chatScrollBar:SetValue(msgIndex)
self.chatScrollBar.isUpdating = false
end
self:UpdateChatHistory()
self:UpdateSearchCountLabel()
end
function MessageBox:UpdateSearchCountLabel()
if not self.searchCountText then return end
local count = table.getn(self.chatSearchResults)
if not self.chatSearchActive or self.chatSearchTerm == "" then
self.searchCountText:SetText("")
elseif count == 0 then
self.searchCountText:SetText("|cffff4444No matches|r")
else
self.searchCountText:SetText(self.chatSearchCurrentIndex .. "/" .. count)
end
end
function MessageBox:CloseSearchBar()
self.chatSearchActive = false
self.chatSearchTerm = ""
self.chatSearchResults = {}
self.chatSearchCurrentIndex = 0
if self.searchBarFrame then
self.searchBarFrame:Hide()
end
if self.chatHistory and self.chatHeader then
self.chatHistory:SetPoint("TOPLEFT", self.chatHeader, "BOTTOMLEFT", 8, -10)
end
if self.selectedContact and self.conversations[self.selectedContact] then
self.conversations[self.selectedContact]._fmtCache = nil
end
self:UpdateChatHistory()
end