Search code examples
luaworld-of-warcraft

How to close an AceGUI 3.0 Frame when escape is pressed?


I'm using WowAce's Ace3 AceGUI library for a World of Warcraft AddOn. I'd like my Frame to be closed when escape is pressed, as is conventional in the game.

This is how the Frame is created:

    local frame = AceGUI:Create("Frame")
    frame:SetTitle("Flare")
    frame:SetStatusText("Ready")
    frame:SetCallback("OnClose", function(widget) AceGUI:Release(widget) end)
    frame:SetLayout("List")

Solution

  • As a part of the World of Warcraft API, the UISpecialFrames table is provided as a global variable, and any string in that table will be fetched as key from the global variables table when the escape key is pressed; If that global variable is an open WoW Frame, it will be closed.

    This means you'll have to declare your WoW Frame as a global variable and add the variable's name to the UISpecialFrames table with table.insert. Note that the WoW Frame of an AceGUI Frame is stored under its frame key. Put together in code:

        local frame = AceGUI:Create("Frame")
        frame:SetTitle("Flare")
        frame:SetStatusText("Ready")
        frame:SetCallback("OnClose", function(widget) AceGUI:Release(widget) end)
        frame:SetLayout("List")
    
        -- Add the frame as a global variable under the name `MyGlobalFrameName`
        _G["MyGlobalFrameName"] = frame.frame
        -- Register the global variable `MyGlobalFrameName` as a "special frame"
        -- so that it is closed when the escape key is pressed.
        tinsert(UISpecialFrames, "MyGlobalFrameName")