Skip to content

Commit

Permalink
Options: Make Dropping and Deleting use a corotinue
Browse files Browse the repository at this point in the history
We should be using more corotinues to make these mass edits safer in the
presence of wow's time limits on processing.

Although the user better not interact with the interface while the
coroutine runs.

Fixes: #4578
  • Loading branch information
InfusOnWoW committed Sep 10, 2023
1 parent 7a16831 commit dd41acc
Showing 1 changed file with 108 additions and 58 deletions.
166 changes: 108 additions & 58 deletions WeakAurasOptions/WeakAurasOptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -440,30 +440,7 @@ StaticPopupDialogs["WEAKAURAS_CONFIRM_DELETE"] = {
button2 = L["Cancel"],
OnAccept = function(self)
if self.data then
local suspended = OptionsPrivate.Private.PauseAllDynamicGroups()
OptionsPrivate.massDelete = true
for _, auraData in pairs(self.data.toDelete) do
WeakAuras.Delete(auraData)
end
OptionsPrivate.massDelete = false

if self.data.parents then
for id in pairs(self.data.parents) do
local parentData = WeakAuras.GetData(id)
local parentButton = OptionsPrivate.GetDisplayButton(id)
WeakAuras.UpdateGroupOrders(parentData)
if(#parentData.controlledChildren == 0) then
parentButton:DisableExpand()
else
parentButton:EnableExpand()
end
parentButton:SetNormalTooltip()
WeakAuras.Add(parentData)
WeakAuras.ClearAndUpdateOptions(parentData.id)
end
end
OptionsPrivate.Private.ResumeAllDynamicGroups(suspended)
OptionsPrivate.SortDisplayButtons(nil, true)
OptionsPrivate.DeleteAuras(self.data.toDelete, self.data.parents)
end
end,
OnCancel = function(self)
Expand Down Expand Up @@ -772,6 +749,54 @@ local function LayoutDisplayButtons(msg)
OptionsPrivate.Private.dynFrame:AddAction("LayoutDisplayButtons1", co1);
end

function OptionsPrivate.DeleteAuras(auras, parents)
local func1 = function()
frame:SetLoadProgressVisible(true)
local num = 0
local total = 0
for _, auraData in pairs(auras) do
total = total +1
end

frame.loadProgress:SetText(L["Deleting auras: "]..num.."/"..total)

local suspended = OptionsPrivate.Private.PauseAllDynamicGroups()
OptionsPrivate.massDelete = true
for _, auraData in pairs(auras) do
WeakAuras.Delete(auraData)
num = num +1
frame.loadProgress:SetText(L["Deleting auras: "]..num.."/"..total)
coroutine.yield()
end
OptionsPrivate.massDelete = false

if parents then
for id in pairs(parents) do
local parentData = WeakAuras.GetData(id)
local parentButton = OptionsPrivate.GetDisplayButton(id)
WeakAuras.UpdateGroupOrders(parentData)
if(#parentData.controlledChildren == 0) then
parentButton:DisableExpand()
else
parentButton:EnableExpand()
end
parentButton:SetNormalTooltip()
WeakAuras.Add(parentData)
WeakAuras.ClearAndUpdateOptions(parentData.id)
frame.loadProgress:SetText(L["Finishing..."])
coroutine.yield()
end
end
OptionsPrivate.Private.ResumeAllDynamicGroups(suspended)
OptionsPrivate.SortDisplayButtons(nil, true)

frame:SetLoadProgressVisible(false)
end

local co1 = coroutine.create(func1)
OptionsPrivate.Private.dynFrame:AddAction("Deleting Auras", co1)
end

function WeakAuras.ShowOptions(msg)
local firstLoad = not(frame);
OptionsPrivate.Private.Pause();
Expand Down Expand Up @@ -1420,50 +1445,75 @@ end
function OptionsPrivate.Drop(mainAura, target, action, area)
WeakAuras_DropDownMenu:Hide()

local mode = ""
if (frame.pickedDisplay == tempGroup and #tempGroup.controlledChildren > 0) then
mode = "MULTI"
elseif mainAura.controlledChildren then
mode = "GROUP"
else
mode = "SINGLE"
end
local func1 = function()
frame:SetLoadProgressVisible(true)

local buttonsToSort = {}
local total = 0
local num = 0
for id, button in pairs(displayButtons) do
if button:IsDragging() then
total = total + 1
end
end
frame.loadProgress:SetText(L["Moving auras: "]..num.."/"..total)

for id, button in pairs(displayButtons) do
if button:IsDragging() then
tinsert(buttonsToSort, button)
local mode = ""
if (frame.pickedDisplay == tempGroup and #tempGroup.controlledChildren > 0) then
mode = "MULTI"
elseif mainAura.controlledChildren then
mode = "GROUP"
else
button:Drop(mode, mainAura, target, action);
mode = "SINGLE"
end
end

if mode == "MULTI" then
-- If we are dragging and dropping multiple auras at once, the order in which we drop is important
-- We want to preserve the top-down order
-- Depending on how exactly we find the insert position, we need to use the right order of insertions
if area == "GROUP" then
table.sort(buttonsToSort, CompareButtonOrderReverse)
elseif area == "BEFORE" then
table.sort(buttonsToSort, CompareButtonOrder)
else -- After
table.sort(buttonsToSort, CompareButtonOrderReverse)
local buttonsToSort = {}

for id, button in pairs(displayButtons) do
if button:IsDragging() then
tinsert(buttonsToSort, button)
num = num + 1
frame.loadProgress:SetText(L["Preparing auras: "]..num.."/"..total)
else
button:Drop(mode, mainAura, target, action);
end
coroutine.yield()
end

num = 0
frame.loadProgress:SetText(L["Moving auras: "]..num.."/"..total)
if mode == "MULTI" then
-- If we are dragging and dropping multiple auras at once, the order in which we drop is important
-- We want to preserve the top-down order
-- Depending on how exactly we find the insert position, we need to use the right order of insertions
if area == "GROUP" then
table.sort(buttonsToSort, CompareButtonOrderReverse)
elseif area == "BEFORE" then
table.sort(buttonsToSort, CompareButtonOrder)
else -- After
table.sort(buttonsToSort, CompareButtonOrderReverse)
end
end
end

for _, button in ipairs(buttonsToSort) do
button:Drop(mode, mainAura, target, action)
end
for _, button in ipairs(buttonsToSort) do
button:Drop(mode, mainAura, target, action)
num = num + 1
frame.loadProgress:SetText(L["Moving auras: "]..num.."/"..total)
coroutine.yield()
end

-- Update offset, this is a bit wasteful to do for every aura
-- But we also need to update the offset if a parent was dragged
for _, button in pairs(displayButtons) do
button:UpdateOffset();
-- Update offset, this is a bit wasteful to do for every aura
-- But we also need to update the offset if a parent was dragged
for _, button in pairs(displayButtons) do
button:UpdateOffset();
end
coroutine.yield()
frame:SetLoadProgressVisible(false)
OptionsPrivate.SortDisplayButtons()
OptionsPrivate.UpdateButtonsScroll()
end

OptionsPrivate.SortDisplayButtons()
OptionsPrivate.UpdateButtonsScroll()
local co1 = coroutine.create(func1)
OptionsPrivate.Private.dynFrame:AddAction("Dropping Auras", co1)
end

function OptionsPrivate.StartDrag(mainAura)
Expand Down

0 comments on commit dd41acc

Please sign in to comment.