Skip to content

Commit 8a14fc3

Browse files
committed
Replace strsplit with directly setting tables, fixes #284
1 parent 2413178 commit 8a14fc3

File tree

3 files changed

+50
-70
lines changed

3 files changed

+50
-70
lines changed

Clicked/Core/CommandProcessor.lua

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ local function EnsureMacroFrameHandler()
5555
macroFrameHandler:Hide()
5656

5757
-- set required data first
58-
macroFrameHandler:SetAttribute("clicked-keybinds", "")
59-
macroFrameHandler:SetAttribute("clicked-identifiers", "")
58+
Addon:SetupRestrictedEnvironmentVariables(macroFrameHandler, {})
6059

6160
-- register OnShow and OnHide handlers to ensure bindings are registered
6261
macroFrameHandler:SetAttribute("_onshow", [[
@@ -90,33 +89,19 @@ local function EnsureMacroFrameHandler()
9089
return
9190
end
9291
93-
local keybinds = self:GetAttribute("clicked-keybinds")
94-
local identifiers = self:GetAttribute("clicked-identifiers")
92+
for i = 1, table.maxn(keybinds) do
93+
local keybind = keybinds[i]
94+
local identifier = identifiers[i]
9595
96-
if strlen(keybinds) > 0 then
97-
keybinds = table.new(strsplit("\001", keybinds))
98-
identifiers = table.new(strsplit("\001", identifiers))
99-
100-
for i = 1, table.maxn(keybinds) do
101-
local keybind = keybinds[i]
102-
local identifier = identifiers[i]
103-
104-
self:SetBindingClick(true, keybind, self, identifier)
105-
end
96+
self:SetBindingClick(true, keybind, self, identifier)
10697
end
10798
]])
10899

109100
-- unregister a binding
110101
macroFrameHandler:SetAttribute("clicked-clear-bindings", [[
111-
local keybinds = self:GetAttribute("clicked-keybinds")
112-
113-
if strlen(keybinds) > 0 then
114-
keybinds = table.new(strsplit("\001", keybinds))
115-
116-
for i = 1, table.maxn(keybinds) do
117-
local keybind = keybinds[i]
118-
self:ClearBinding(keybind)
119-
end
102+
for i = 1, table.maxn(keybinds) do
103+
local keybind = keybinds[i]
104+
self:ClearBinding(keybind)
120105
end
121106
]])
122107

@@ -140,19 +125,7 @@ end
140125
--- @param keybinds Keybind[]
141126
--- @param attributes string[]
142127
function Addon:UpdateMacroFrameHandler(keybinds, attributes)
143-
local split = {
144-
keybinds = {},
145-
identifiers = {}
146-
}
147-
148-
for _, keybind in ipairs(keybinds) do
149-
table.insert(split.keybinds, keybind.key)
150-
table.insert(split.identifiers, keybind.identifier)
151-
end
152-
153-
macroFrameHandler:SetAttribute("clicked-keybinds", table.concat(split.keybinds, "\001"))
154-
macroFrameHandler:SetAttribute("clicked-identifiers", table.concat(split.identifiers, "\001"))
155-
128+
Addon:SetupRestrictedEnvironmentVariables(macroFrameHandler, keybinds)
156129
Addon:SetPendingFrameAttributes(macroFrameHandler, attributes)
157130
Addon:ApplyAttributesToFrame(macroFrameHandler)
158131
end

Clicked/Core/Utils.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,3 +1426,35 @@ function Addon:CharAt(str, index)
14261426

14271427
return string.sub(str, index, index)
14281428
end
1429+
1430+
--- Set the `keybinds` and `identifiers` variables within the restricted environment of the frame.
1431+
---
1432+
--- @param frame Frame
1433+
--- @param keybinds Keybind[]
1434+
function Addon:SetupRestrictedEnvironmentVariables(frame, keybinds)
1435+
local keys = {}
1436+
local identifiers = {}
1437+
1438+
for _, keybind in ipairs(keybinds) do
1439+
table.insert(keys, keybind.key)
1440+
table.insert(identifiers, keybind.identifier)
1441+
end
1442+
1443+
--- @type string
1444+
local command
1445+
1446+
if #keybinds == 0 then
1447+
command = [[
1448+
keybinds = table.new()
1449+
identifiers = table.new()
1450+
]]
1451+
else
1452+
command = string.format([[
1453+
keybinds = table.new('%s')
1454+
identifiers = table.new('%s')
1455+
]], table.concat(keys, ", "), table.concat(identifiers, ", "))
1456+
end
1457+
1458+
--- @diagnostic disable-next-line: undefined-field
1459+
frame:Execute(command)
1460+
end

Clicked/UnitFrames/ClickCastHeader.lua

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ function Addon:RegisterClickCastHeader()
4444
ClickCastHeader = CreateFrame("Frame", "ClickCastHeader", UIParent, "SecureHandlerBaseTemplate,SecureHandlerAttributeTemplate")
4545
Addon.ClickCastHeader = ClickCastHeader
4646

47-
ClickCastHeader:SetAttribute("clicked-keybinds", "")
48-
ClickCastHeader:SetAttribute("clicked-identifiers", "")
47+
Addon:SetupRestrictedEnvironmentVariables(ClickCastHeader, {})
4948

5049
ClickCastHeader:SetAttribute("setup-keybinds", [[
5150
if currentClickcastButton ~= nil then
@@ -54,33 +53,20 @@ function Addon:RegisterClickCastHeader()
5453
5554
currentClickcastButton = self
5655
57-
local keybinds = control:GetAttribute("clicked-keybinds")
58-
local identifiers = control:GetAttribute("clicked-identifiers")
5956
local button = self:GetAttribute("clicked-sidecar") or self
6057
61-
if strlen(keybinds) > 0 then
62-
keybinds = table.new(strsplit("\001", keybinds))
63-
identifiers = table.new(strsplit("\001", identifiers))
58+
for i = 1, table.maxn(keybinds) do
59+
local keybind = keybinds[i]
60+
local identifier = identifiers[i]
6461
65-
for i = 1, table.maxn(keybinds) do
66-
local keybind = keybinds[i]
67-
local identifier = identifiers[i]
68-
69-
self:SetBindingClick(true, keybind, button, identifier)
70-
end
62+
self:SetBindingClick(true, keybind, button, identifier)
7163
end
7264
]])
7365

7466
ClickCastHeader:SetAttribute("clear-keybinds", [[
75-
local keybinds = control:GetAttribute("clicked-keybinds")
76-
77-
if strlen(keybinds) > 0 then
78-
keybinds = table.new(strsplit("\001", keybinds))
79-
80-
for i = 1, table.maxn(keybinds) do
81-
local keybind = keybinds[i]
82-
self:ClearBinding(keybind)
83-
end
67+
for i = 1, table.maxn(keybinds) do
68+
local keybind = keybinds[i]
69+
self:ClearBinding(keybind)
8470
end
8571
8672
currentClickcastButton = nil
@@ -164,18 +150,7 @@ function Addon:UpdateClickCastHeader(keybinds)
164150
return
165151
end
166152

167-
local split = {
168-
keybinds = {},
169-
identifiers = {}
170-
}
171-
172-
for _, keybind in ipairs(keybinds) do
173-
table.insert(split.keybinds, keybind.key)
174-
table.insert(split.identifiers, keybind.identifier)
175-
end
176-
177-
Addon.ClickCastHeader:SetAttribute("clicked-keybinds", table.concat(split.keybinds, "\001"))
178-
Addon.ClickCastHeader:SetAttribute("clicked-identifiers", table.concat(split.identifiers, "\001"))
153+
Addon:SetupRestrictedEnvironmentVariables(Addon.ClickCastHeader, keybinds)
179154

180155
Addon.ClickCastHeader:Execute([[
181156
local button = currentClickcastButton

0 commit comments

Comments
 (0)