Skip to content

Commit

Permalink
Proper(-ish) automatic "new" state for settings
Browse files Browse the repository at this point in the history
Further improves p3lim-wow/QuickQuest#99
  • Loading branch information
p3lim committed Nov 1, 2024
1 parent a9c89fc commit 378fbcd
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions modules/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,37 @@ local function registerSetting(category, savedvariable, info)
return
end

if info.new and not _G[savedvariable][info.key .. '_seen'] then
EventRegistry:RegisterCallback('Settings.CategoryChanged', function(_, cat)
if cat == category then
_G[savedvariable][info.key .. '_seen'] = true
end
end)

-- possibly tainty, and not that clean (it adds new tag to the category list too)
if info.firstInstall then
-- we don't want to add "new" tags to a freshly installed addon
_G[savedvariable][info.key .. '_seen'] = true
elseif not _G[savedvariable][info.key .. '_seen'] then
-- add new tag to the settings panel until it's been observed by the player
-- possibly tainty, definitely ugly
local version = GetBuildInfo()
if not NewSettings[version] then
NewSettings[version] = {}
end

table.insert(NewSettings[version], uniqueKey)

-- remove once seen
EventRegistry:RegisterCallback('Settings.CategoryChanged', function(_, cat)
if cat == category and not _G[savedvariable][info.key .. '_seen'] then
_G[savedvariable][info.key .. '_seen'] = true

local settingIndex
for index, key in next, NewSettings[version] do
if key == uniqueKey then
settingIndex = index
break
end
end

if settingIndex then
table.remove(NewSettings[version], settingIndex)
end
end
end)
end

-- callback when settings change something
Expand All @@ -216,12 +233,18 @@ local function registerSettings(savedvariable, settings)
local category = Settings.RegisterVerticalLayoutCategory(categoryName)
Settings.RegisterAddOnCategory(category)

local firstInstall
if not _G[savedvariable] then
-- for some dumb reason RegisterAddOnSetting doesn't initialize the savedvariables table
_G[savedvariable] = {}
firstInstall = true
end

for _, setting in next, settings do
if firstInstall then
info.firstInstall = true

Check warning on line 245 in modules/settings.lua

View workflow job for this annotation

GitHub Actions / lint

mutating non-standard global variable 'info'
end

registerSetting(category, savedvariable, setting)
end

Expand Down Expand Up @@ -265,7 +288,6 @@ namespace:RegisterSettings('MyAddOnDB', {
title = 'My Toggle',
tooltip = 'Longer description of the toggle in a tooltip',
default = false,
new = false,
},
{
key = 'mySlider',
Expand All @@ -277,7 +299,6 @@ namespace:RegisterSettings('MyAddOnDB', {
maxValue = 1.0,
valueStep = 0.01,
valueFormat = formatter, -- callback function or a string for string.format
new = true,
},
{
key = 'myMenu',
Expand All @@ -290,15 +311,13 @@ namespace:RegisterSettings('MyAddOnDB', {
{value = key2, label = 'Second option'},
{value = key3, label = 'Third option'},
},
new = false,
},
{
key = 'myColor',
type = 'color',
title = 'My Color',
tooltip = 'Longer description of the color in a tooltip',
default = 'ff00ff', -- either "RRGGBB" or "AARRGGBB" format
new = false,
}
})
```
Expand Down

0 comments on commit 378fbcd

Please sign in to comment.