From d47536007d1f4b435f6541fc589264c1a3686bd9 Mon Sep 17 00:00:00 2001 From: bepis Date: Wed, 30 Sep 2026 22:38:45 +1000 Subject: [PATCH] Code cleanup and reducing CPU usage --- QuestExpandAll.lua | 184 ++++++++++++++++++++------------------------- QuestExpandAll.toc | 2 +- 2 files changed, 84 insertions(+), 102 deletions(-) diff --git a/QuestExpandAll.lua b/QuestExpandAll.lua index 95f9e94..625e6ea 100755 --- a/QuestExpandAll.lua +++ b/QuestExpandAll.lua @@ -2,127 +2,109 @@ -- Interface: 16001 local addon = CreateFrame("Frame") +local button -- set in CreateExpandButton -------------------------------------------------- -- Helpers -------------------------------------------------- local function AreAllHeadersExpanded() + for i = 1, C_QuestLog.GetNumQuestLogEntries() do + local info = C_QuestLog.GetInfo(i) + if info and info.isHeader and info.isCollapsed then + return false + end + end + return true +end -local numEntries = C_QuestLog.GetNumQuestLogEntries() +local function UpdateButton() + if not button then return end --------------------------------------------------- --- Resize search box to fit button --------------------------------------------------- - -local function ResizeSearchBox() - local sb = QuestScrollFrame and QuestScrollFrame.SearchBox - if sb then - sb:SetWidth(150) + local text = AreAllHeadersExpanded() and "- All" or "+ All" + if button:GetText() ~= text then + button:SetText(text) end end -if QuestScrollFrame and QuestScrollFrame.SearchBox then - QuestScrollFrame.SearchBox:HookScript("OnShow", ResizeSearchBox) +local searchBoxHooked = false + +local function ResizeSearchBox() + local sb = QuestScrollFrame and QuestScrollFrame.SearchBox + if not sb then return end + + if sb:GetWidth() ~= 150 then + sb:SetWidth(150) + end + + if not searchBoxHooked then + searchBoxHooked = true + sb:HookScript("OnShow", ResizeSearchBox) + end end -ResizeSearchBox() -for i = 1, numEntries do - local info = C_QuestLog.GetInfo(i) +local function UpdateButton() + if not button then return end - if info and info.isHeader and info.isCollapsed then - return false - end - end + ResizeSearchBox() - return true - end + local text = AreAllHeadersExpanded() and "- All" or "+ All" + if button:GetText() ~= text then + button:SetText(text) + end +end - local function UpdateButton(button) +-------------------------------------------------- +-- Create Button +-------------------------------------------------- + +local function CreateExpandButton() + if button then return end + + local parent = QuestMapFrame and QuestMapFrame.QuestsFrame + if not parent then return end + + button = CreateFrame("Button", "QuestExpandAllButton", parent, "UIPanelButtonTemplate") + button:SetSize(50, 20) + + if QuestLogQuestCount then + button:SetPoint("RIGHT", QuestLogQuestCount, "RIGHT", 54, 0) + else + button:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -10, 24) + end + + button:SetScript("OnClick", function() if AreAllHeadersExpanded() then - button:SetText("- All") - else - button:SetText("+ All") - end - end + CollapseQuestHeader(0) + else + ExpandQuestHeader(0) + end + end) - -------------------------------------------------- - -- Create Button - -------------------------------------------------- + button:SetScript("OnEnter", function(self) + GameTooltip:SetOwner(self, "ANCHOR_RIGHT") + GameTooltip:SetText("Quest Headers") + GameTooltip:AddLine("Left click to toggle all zone headers.", 1, 1, 1, true) + GameTooltip:Show() + end) - local function CreateExpandButton() + button:SetScript("OnLeave", GameTooltip_Hide) - if QuestExpandAllButton then - return - end + UpdateButton() +end - -- Wait until the Quest Map exists - if not QuestMapFrame or not QuestMapFrame.QuestsFrame then - return - end +-------------------------------------------------- +-- Events +-------------------------------------------------- - local parent = QuestMapFrame.QuestsFrame +addon:RegisterEvent("PLAYER_LOGIN") +addon:RegisterEvent("QUEST_LOG_UPDATE") - local button = CreateFrame( - "Button", - "QuestExpandAllButton", - parent, - "UIPanelButtonTemplate" - ) - - button:SetSize(50,20) - button:SetPoint("RIGHT", QuestLogQuestCount, "RIGHT", 54, 0) - button:SetScript("OnClick", function(self) - - if AreAllHeadersExpanded() then - CollapseQuestHeader(0) - else - ExpandQuestHeader(0) - end - - C_Timer.After(0, function() - UpdateButton(self) - end) - - end) - - button:SetScript("OnEnter", function(self) - - GameTooltip:SetOwner(self, "ANCHOR_RIGHT") - GameTooltip:AddLine("Quest Headers") - GameTooltip:AddLine(" ") - GameTooltip:AddLine("Left Click to toggle all zone headers.",1,1,1,true) - GameTooltip:Show() - - end) - - button:SetScript("OnLeave", function() - GameTooltip:Hide() - end) - - UpdateButton(button) - - end - - -------------------------------------------------- - -- Events - -------------------------------------------------- - - addon:RegisterEvent("PLAYER_LOGIN") - addon:RegisterEvent("QUEST_LOG_UPDATE") - - addon:SetScript("OnEvent", function(self, event) - - if event == "PLAYER_LOGIN" then - - CreateExpandButton() - - elseif event == "QUEST_LOG_UPDATE" then - - if QuestExpandAllButton then - UpdateButton(QuestExpandAllButton) - end - - end - - end) +addon:SetScript("OnEvent", function(self, event) + if event == "PLAYER_LOGIN" then + self:UnregisterEvent("PLAYER_LOGIN") + CreateExpandButton() + else + UpdateButton() + end +end) \ No newline at end of file diff --git a/QuestExpandAll.toc b/QuestExpandAll.toc index 7108827..e622ad3 100755 --- a/QuestExpandAll.toc +++ b/QuestExpandAll.toc @@ -2,6 +2,6 @@ ## Title: Quest Expand All ## Notes: Adds an Expand All button to the quest log. ## Author: Drink_Bepis -## Version: 0.1 +## Version: 0.2 QuestExpandAll.lua