From d8fefc947e72c7d0d9b415e9abb68b1a006a60c8 Mon Sep 17 00:00:00 2001 From: dfj Date: Sat, 24 Oct 2026 13:57:09 +0200 Subject: [PATCH] Close the window when Escape is pressed Register the main frame ("AmibisMainFrame") in UISpecialFrames so Escape hides it and suppresses the game menu while it is open, matching the default UI panels. Move the stats-retry timer cleanup into the frame's OnHide script so the close button, Escape, and /amibis all stop the timer. Tests: the mock now registers named frames in _G and runs OnHide scripts. The UI suite simulates FrameXML's CloseSpecialWindows() and asserts the frame hides and the retry timer is cleared. 108 passing. --- AGENTS.md | 1 + Amibis/Tests/README.md | 2 +- Amibis/Tests/test_ui.lua | 26 ++++++++++++++++++++++++++ Amibis/Tests/wow_mocks.lua | 30 ++++++++++++++++++++++++++---- Amibis/UI/ComparisonFrame.lua | 26 ++++++++++++++++++++++---- README.md | 1 + 6 files changed, 77 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 631cde9..bca05e4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -42,4 +42,5 @@ Amibis/ - Max 17 visible rows; rows are clickable (Shift+click = chat link, Ctrl+click = dress-up) - Stats panel is **per-spec**: `SPEC_STAT_PROFILES` in `Amibis.lua` defines an ordered `{ stat, weight }` list for every class/spec (casters see spell damage/hit/crit/haste, melee see strength/AP/hit/expertise/armor pen, tanks see stamina/defense/dodge/parry/block, healers see healing/spirit/mp5). The same weights rank top upgrades, score completion, and pick which two gains are shown in the upgrade line. Keep profiles to 8 rows so the panel fits the frame. - The phase-progress bar always measures the **latest** phase, independent of the selected phase: stats-based once item data is cached, slot-based (prefixed `~`) until then. It refreshes on talent and equipment changes. +- The main frame is registered in `UISpecialFrames`, so Escape closes it (and suppresses the game menu while it is open). `MainFrame`'s `OnHide` clears the stats retry timer, so both the close button and Escape clean up. - Stat values are read via `STAT_MAP` (WoW `GetItemStats` keys → internal stat names); TBC's per-school hit/crit/haste keys are all mapped to one internal stat. diff --git a/Amibis/Tests/README.md b/Amibis/Tests/README.md index a92bc39..8cd658d 100644 --- a/Amibis/Tests/README.md +++ b/Amibis/Tests/README.md @@ -27,7 +27,7 @@ apt-get download lua5.4 && dpkg -x lua5.4_*.deb /tmp/lua && /tmp/lua/usr/bin/lua | `test_stats.lua` | `ScanItemStats`: MAX aggregation across alias keys (spell damage schools, healing, haste/crit/mp5 ratings), single-mapping pass-through, unmapped key filtering, nil handling | | `test_compare.lua` | `CompareGear`: BIS counting, weighted upgrade ranking, positive-diffs-only scoring, per-spec stat profiles (Shadow counts spell damage, Holy counts healing, Warrior counts strength/AP, tank mitigation stats), `GetStatKeysForSpec`, `GetCompletionRatio` | | `test_slash.lua` | `/amibis spec ` parsing: override set/clear, case and multi-word normalization, `spec clear` not treated as a spec name | -| `test_ui.lua` | UI smoke test with mocked frames: the stats panel rebuilds when switching Shadow → Holy (regression: old rows were hidden via a missing `label` field, which errored and left the Shadow rows on screen) | +| `test_ui.lua` | UI smoke tests with mocked frames: the stats panel rebuilds when switching Shadow → Holy (regression: old rows were hidden via a missing `label` field, which errored and left the Shadow rows on screen), and the main frame is registered in `UISpecialFrames` so Escape closes it | ## Layout diff --git a/Amibis/Tests/test_ui.lua b/Amibis/Tests/test_ui.lua index 091da40..850c3d3 100644 --- a/Amibis/Tests/test_ui.lua +++ b/Amibis/Tests/test_ui.lua @@ -53,4 +53,30 @@ describe("UI stat panel", function() expect(holyTexts["+Healing"]).toBeTruthy() expect(holyTexts["+Spell Damage"]).toBeFalsy() end) + + it("closes the window when Escape is pressed", function() + ns.UI.Initialize() + if not ns.UI.IsShown() then + ns.UI.ToggleFrame() + end + expect(ns.UI.IsShown()).toBeTruthy() + + local registered = false + for _, name in ipairs(UISpecialFrames) do + if name == "AmibisMainFrame" then registered = true end + end + expect(registered).toBeTruthy() + + -- Simulate an in-flight stats retry so the OnHide cleanup is exercised. + ns.Amibis._statsRetryTimer = CreateFrame("Frame") + + -- Mirrors FrameXML's CloseSpecialWindows(): hide every shown special frame. + for _, name in ipairs(UISpecialFrames) do + local frame = _G[name] + if frame and frame:IsShown() then frame:Hide() end + end + + expect(ns.UI.IsShown()).toBeFalsy() + expect(ns.Amibis._statsRetryTimer).toBeNil() + end) end) diff --git a/Amibis/Tests/wow_mocks.lua b/Amibis/Tests/wow_mocks.lua index e319303..43baa5b 100644 --- a/Amibis/Tests/wow_mocks.lua +++ b/Amibis/Tests/wow_mocks.lua @@ -71,7 +71,7 @@ local function makeChild() end local function makeFrame() - local frame = { fonts = {}, textures = {}, shown = true, width = 0, height = 0 } + local frame = { fonts = {}, textures = {}, shown = true, width = 0, height = 0, scripts = {} } return setmetatable(frame, { __index = function(self, key) if key == "CreateFontString" then @@ -86,10 +86,28 @@ local function makeFrame() table.insert(self.textures, tx) return tx end + elseif key == "SetScript" then + return function(_, event, handler) + self.scripts[event] = handler + return self + end + elseif key == "GetScript" then + return function(_, event) + return self.scripts[event] + end elseif key == "Show" then - return function() self.shown = true; return self end + return function() + self.shown = true + return self + end elseif key == "Hide" then - return function() self.shown = false; return self end + return function() + local wasShown = self.shown + self.shown = false + local handler = self.scripts["OnHide"] + if wasShown and handler then handler(self) end + return self + end elseif key == "IsShown" then return function() return self.shown end elseif key == "SetSize" then @@ -112,11 +130,15 @@ local function makeFrame() end _G.MockFrames = {} -function CreateFrame() +function CreateFrame(frameType, name, parent, template) local f = makeFrame() + if name then + _G[name] = f + end table.insert(_G.MockFrames, f) return f end _G.GameTooltip = makeFrame() _G.SlashCmdList = {} +_G.UISpecialFrames = { "GameMenuFrame", "StaticPopup1" } diff --git a/Amibis/UI/ComparisonFrame.lua b/Amibis/UI/ComparisonFrame.lua index 8c175be..62397a0 100644 --- a/Amibis/UI/ComparisonFrame.lua +++ b/Amibis/UI/ComparisonFrame.lua @@ -52,6 +52,27 @@ local function CreateMainFrame() self:StopMovingOrSizing() Amibis:SaveFramePosition(self) end) + frame:SetScript("OnHide", function() + if Amibis._statsRetryTimer then + Amibis._statsRetryTimer:Hide() + Amibis._statsRetryTimer = nil + end + end) + + -- Registering with UISpecialFrames makes the Escape key close the window + -- (and suppresses the game menu, like the default UI panels). + if UISpecialFrames then + local registered = false + for _, name in ipairs(UISpecialFrames) do + if name == "AmibisMainFrame" then + registered = true + break + end + end + if not registered then + table.insert(UISpecialFrames, "AmibisMainFrame") + end + end frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0) return frame @@ -584,11 +605,8 @@ function ns.UI.ToggleFrame() end if MainFrame:IsShown() then + -- Hiding runs the frame's OnHide script, which stops the stats retry timer. MainFrame:Hide() - if Amibis._statsRetryTimer then - Amibis._statsRetryTimer:Hide() - Amibis._statsRetryTimer = nil - end else MainFrame:Show() Amibis._statsRetryAttempts = nil diff --git a/README.md b/README.md index 3380c19..7e3e4a1 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ A World of Warcraft: The Burning Crusade Classic addon that compares your equipp - **Shift+Click** a row to link the BIS item in chat - **Ctrl+Click** a row to open the dress-up preview - **Hover** a row to see the item tooltip +- **Press Escape** to close the window (registered in `UISpecialFrames`) - **Click a phase segment** (T4 | T5 | T6 | ZA) to switch the comparison target ## Adding BIS Lists