Repository navigation
Expand file tree
/
Copy pathProfiles.lua
More file actions
205 lines (184 loc) · 7.51 KB
/
Copy pathProfiles.lua
File metadata and controls
205 lines (184 loc) · 7.51 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
local _, ns = ...
local L = ns.L
-- Options > AddOns > Clean Tooltips > Profiles: the active profile, the default for new
-- characters, new, copy, delete and reset, each asking first through the game's popups.
-- ns.Options.BuildProfiles(canvas) the page, built on first show (OptionsWidgets.lua)
local Kit = ns.OptionsKit
local LEFT, TOP, ROW, LABEL_WIDTH = Kit.LEFT, Kit.TOP, Kit.ROW, Kit.LABEL_WIDTH
local RefuseInCombat, NewPage, Dropdown = Kit.RefuseInCombat, Kit.NewPage, Kit.Dropdown
local POPUP_PREFIX = "CLEANTOOLTIPS_"
local NAME_LETTERS = 32
-- Profiles -------------------------------------------------------------------------------------------------------------
-- A canvas page like the others, with CT's own dropdowns and buttons. Not a list page of the game's Settings controls:
-- the panel's search reads every list page's settings, and reading CT's taints the rest of the search, so the game
-- then refuses the Social page's protected Discord call while it draws the results (ADDON_ACTION_FORBIDDEN).
-- Canvas pages aren't searched.
local profilesPage
local function RefreshProfiles()
if profilesPage then profilesPage:Refresh() end
end
local function Trim(text)
return (tostring(text or ""):gsub("^%s+", ""):gsub("%s+$", ""))
end
local function ProfileNames(skipCurrent)
local names = {}
if not ns.db then return names end
local current = ns.db:GetCurrentProfile()
for _, name in ipairs((ns.db:GetProfiles({}))) do
if not (skipCurrent and name == current) then names[#names + 1] = name end
end
table.sort(names)
return names
end
local function ProfileExists(name)
for _, existing in ipairs(ProfileNames()) do
if existing == name then return true end
end
return false
end
-- The profile names for a dropdown, as { value, text }; `extra` first when it isn't a profile yet.
local function ProfileChoices(skipCurrent, extra)
return function()
local list, seen = {}, {}
if extra then
list[1] = { value = extra, text = extra }
seen[extra] = true
end
for _, name in ipairs(ProfileNames(skipCurrent)) do
if not seen[name] then list[#list + 1] = { value = name, text = name } end
end
return list
end
end
local PROFILE_DROPDOWN_WIDTH = 200
local BUTTON_WIDTH = 160
local LABEL_GAP = 16 -- between the longest label and the controls
-- Where the page's dropdowns and buttons start: past its longest label (user, 19 Nov: "Default for new characters" ran
-- under its dropdown at the other pages' LABEL_WIDTH), and never nearer than that. Labels are never secret.
local function LabelWidth(page)
local measure = page:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
measure:Hide()
local widest = 0
for _, key in ipairs({ "PROFILE_ACTIVE", "PROFILE_DEFAULT", "PROFILE_COPY", "PROFILE_DELETE" }) do
measure:SetText(L[key])
widest = math.max(widest, ns.Readable(measure:GetUnboundedStringWidth()) or 0)
end
measure:SetText("")
return math.max(LABEL_WIDTH, math.ceil(widest) + LABEL_GAP)
end
local function ProfileButton(page, text, x, y, click)
local button = CreateFrame("Button", nil, page, "UIPanelButtonTemplate")
button:SetText(text)
button:SetSize(BUTTON_WIDTH, 22)
button:SetPoint("TOPLEFT", x, y)
button:SetScript("OnClick", click)
return button
end
-- A dropdown that picks a profile to act on (copy from, delete): it always reads as a prompt, asks to confirm, and is
-- greyed while there's no other profile.
local function PickerDropdown(page, labelText, popup, y, labelWidth)
local dropdown = Dropdown(page, labelText, PROFILE_DROPDOWN_WIDTH, ProfileChoices(true),
function() return nil end,
function(value)
if RefuseInCombat() then return end
StaticPopup_Show(POPUP_PREFIX .. popup, value, ns.db and ns.db:GetCurrentProfile() or "", value)
end, LEFT, y, labelWidth)
dropdown:SetDefaultText(L.PROFILE_CHOOSE)
table.insert(page.refreshers, function() dropdown:SetEnabled(#ProfileNames(true) > 0) end)
return dropdown
end
function ns.Options.BuildProfiles(canvas)
local page = NewPage(L.PAGE_PROFILES, canvas)
local y = TOP
local labelWidth = LabelWidth(page)
page.Active = Dropdown(page, L.PROFILE_ACTIVE, PROFILE_DROPDOWN_WIDTH, ProfileChoices(false),
function() return ns.db and ns.db:GetCurrentProfile() end,
function(value)
if ns.db and not RefuseInCombat() then ns.db:SetProfile(value) end
RefreshProfiles()
end, LEFT, y, labelWidth)
y = y - ROW - 8
-- The default for new characters may name a profile that doesn't exist yet (AceDB makes it on first use).
page.Default = Dropdown(page, L.PROFILE_DEFAULT, PROFILE_DROPDOWN_WIDTH,
function() return ProfileChoices(false, ns.global and ns.global.defaultProfile or "Default")() end,
function() return ns.global and ns.global.defaultProfile or "Default" end,
function(value)
if ns.global then ns.global.defaultProfile = value end
RefreshProfiles()
end, LEFT, y, labelWidth)
y = y - ROW - 16
page.New = ProfileButton(page, L.PROFILE_NEW, LEFT + labelWidth, y, function()
if not RefuseInCombat() then StaticPopup_Show(POPUP_PREFIX .. "PROFILE_NEW") end
end)
y = y - ROW - 8
page.Copy = PickerDropdown(page, L.PROFILE_COPY, "PROFILE_COPY", y, labelWidth)
y = y - ROW - 8
page.Delete = PickerDropdown(page, L.PROFILE_DELETE, "PROFILE_DELETE", y, labelWidth)
y = y - ROW - 16
page.Reset = ProfileButton(page, L.PROFILE_RESET, LEFT + labelWidth, y, function()
if not RefuseInCombat() and ns.db then
StaticPopup_Show(POPUP_PREFIX .. "PROFILE_RESET", ns.db:GetCurrentProfile())
end
end)
-- Defaults on this page would be Reset Profile, which asks first: the button is there for that.
function page:OnDefault() end
profilesPage = page
return page
end
local function Popup(key, info)
info.timeout = 0
info.whileDead = true
info.hideOnEscape = true
StaticPopupDialogs[POPUP_PREFIX .. key] = info
end
Popup("PROFILE_NEW", {
text = L.PROFILE_NEW_TEXT,
button1 = ACCEPT, button2 = CANCEL,
hasEditBox = true, maxLetters = NAME_LETTERS,
OnShow = function(dialog) dialog:GetButton1():Disable() end,
EditBoxOnTextChanged = function(editBox) StaticPopup_StandardNonEmptyTextHandler(editBox) end,
EditBoxOnEnterPressed = function(editBox)
local button = editBox:GetParent():GetButton1()
if button:IsEnabled() then button:Click() end
end,
OnAccept = function(dialog)
if RefuseInCombat() or not ns.db then return end
local name = Trim(dialog:GetEditBox():GetText())
if name == "" then return true end
if ProfileExists(name) then
ns.Print(L.PROFILE_EXISTS:format(name))
return true
end
ns.db:SetProfile(name)
RefreshProfiles()
end,
})
Popup("PROFILE_COPY", {
text = L.PROFILE_COPY_CONFIRM, -- %s the profile copied from, %s the current one
button1 = YES, button2 = NO, showAlert = true,
OnAccept = function(_, source)
if RefuseInCombat() or not ns.db or not ProfileExists(source) then return end
if source ~= ns.db:GetCurrentProfile() then ns.db:CopyProfile(source) end
end,
})
Popup("PROFILE_DELETE", {
text = L.PROFILE_DELETE_CONFIRM, -- %s the profile
button1 = YES, button2 = NO, showAlert = true,
OnAccept = function(_, name)
if RefuseInCombat() or not ns.db or not ProfileExists(name) then return end
if name == ns.db:GetCurrentProfile() then return end
ns.db:DeleteProfile(name)
if ns.global and ns.global.defaultProfile == name then ns.global.defaultProfile = "Default" end
RefreshProfiles()
end,
})
Popup("PROFILE_RESET", {
text = L.PROFILE_RESET_CONFIRM, -- %s the profile
button1 = YES, button2 = NO, showAlert = true,
OnAccept = function()
if not RefuseInCombat() and ns.db then ns.db:ResetProfile() end
end,
})
ns.Listen("SETTINGS_CHANGED", function(what)
if what == nil then RefreshProfiles() end
end)