Skip to content

Foundry Window

R edited this page Sep 25, 2026 · 1 revision

Foundry.Window

Part of the Foundry API reference.

New to WoW addons? Start here →

Foundry.Window attaches drag movement and a persisted position to a frame you already created. You supply the frame and its drag handle; Foundry owns SetMovable, StartMoving/StopMovingOrSizing, SetClampedToScreen, validated restore and save of a table you own, and :Reset. It is not a frame factory, and it does not manage resize, size persistence, scale, or Edit Mode.

API version: Foundry.Window is API version 1. Assert it with F:RequireModule("Window", 1) if your addon needs the module present at a minimum version. (This is the module's version; the library as a whole is F.API_VERSION.)


1. Mission

Most addons with a movable window rewrite the same handful of calls: mark the frame movable, wire a drag handle to StartMoving/StopMovingOrSizing, clamp it to the screen, and save/restore an anchor point in the addon's own saved data, with the same edge cases every time (a first run with nothing saved yet, a corrupted or hand-edited save, an off-screen restore). Foundry.Window encapsulates that pattern into one call: F.Window:Attach(win, config) validates atomically, wires the drag, and applies the saved position (or a default) immediately.

What the module does: marks a frame movable and clamped, wires a caller-owned drag handle, and validates, restores, and saves an anchor point into a table you own. What the module does not do: create the window frame, resize it, persist its size, manage its scale, or integrate with Edit Mode. See §7 for the full list.


2. F.Window:Attach(win, config)

local F = _G.Foundry_1_0

F.Window:Attach(win, {
    handle   = win.TitleBar,               -- required: frame that receives the drag scripts
    geometry = db.profile.window,          -- required: caller-owned table
    default  = {"CENTER", "CENTER", 0, 0}, -- required: point, relPoint, x, y relative to UIParent
})

:Attach returns win on success, so local w = F.Window:Attach(win, cfg) reads naturally. A refused call returns nil and touches nothing.

Config fields

Field Type Required Meaning
handle table (frame) yes Receives the drag scripts. May be win itself (whole-frame drag) or any frame you choose; Foundry does not verify that it descends from win.
geometry table yes Your own table. Foundry reads and writes four keys in it (point, relPoint, x, y) in place, holding the table by reference rather than copying it.
default table yes {point, relPoint, x, y}, relative to UIParent, positional (not named keys). Applied whenever geometry is empty or malformed. Foundry copies this table at :Attach time, so mutating your own default literal afterward does not change what :Reset applies.

Validation order

Validation is atomic: nothing on win or config.handle is touched until every check below passes. Each failure calls F:RaiseDevError(...) with a "Window:Attach: ..." prefix and returns nil (dev build raises, release build prints and refuses).

  1. win: must be a table exposing SetMovable, SetClampedToScreen, StartMoving, StopMovingOrSizing, SetUserPlaced, GetPoint, ClearAllPoints, SetPoint, and GetParent.
  2. win:GetParent() == UIParent: win must be parented directly to UIParent. The save and restore math is only correct for a plain top-level, UIParent-parented frame with exactly one anchor point.
  3. win is not already attached: a second :Attach on the same frame is refused; Foundry.Window does not re-attach.
  4. config: must be a table.
  5. config.handle: must be a table exposing EnableMouse, RegisterForDrag, SetScript, and GetScript.
  6. config.handle's OnDragStart and OnDragStop scripts: both must be nil. Foundry.Window owns those two scripts and refuses to clobber existing ones rather than silently overwriting them.
  7. config.geometry: must be a table.
  8. config.default: must be {point, relPoint, x, y} with point and relPoint from the anchor whitelist (TOPLEFT, TOP, TOPRIGHT, LEFT, CENTER, RIGHT, BOTTOMLEFT, BOTTOM, BOTTOMRIGHT) and x, y as finite numbers (not NaN or infinite).

What Attach does, in order, once validation passes

  1. win:SetMovable(true), then immediately win:SetUserPlaced(false), then win:SetClampedToScreen(true). Attach calls SetUserPlaced(false) immediately so the saved record, not the client's own layout cache, is the position source.
  2. Wires config.handle: EnableMouse(true), RegisterForDrag("LeftButton"), an OnDragStart script that calls win:StartMoving(), and an OnDragStop script (see §4 below).
  3. Records the attachment (frame is the registry key) with a copy of default.
  4. Restores position (see §3 below).

3. Restore rules

Applied once, at :Attach, in this order:

  • Well-formed record (point/relPoint both on the whitelist, x/y both finite numbers): applied as-is.
  • Malformed record (any of the four keys present, but the record fails the well-formed check: an unrecognized point name, a string offset, a missing key, or a non-finite offset from a hand-edited save): the four keys are cleared from your table and default is applied. No error is raised, in either build; a malformed record is disk-editable data, not a programmer mistake.
  • Empty record (none of the four keys present, a fresh install): default is applied.

Applying a position is always win:ClearAllPoints() followed by win:SetPoint(point, UIParent, relPoint, x, y).


4. Drag and save (OnDragStop)

Dragging by the handle calls win:StartMoving() on OnDragStart. On OnDragStop, in order:

  1. win:StopMovingOrSizing().
  2. win:SetUserPlaced(false), immediately after, so the saved record, not the client's own layout cache, is what repositions the frame across sessions.
  3. A save attempt, gated by a precondition re-check:
if win:GetParent() ~= UIParent
   or (the frame's sole anchor's relativeTo is set and isn't UIParent) then
    -- refuse: raise a dev error, write nothing
else
    -- read win:GetPoint(1) and write point/relPoint/x/y into geometry
end

If the frame has been reparented since :Attach, or its sole anchor is not UIParent-relative, the write is refused and F:RaiseDevError fires with:

Window: attached frame's parent or anchor is no longer UIParent-relative; geometry not saved

The same message covers both causes, since they're indistinguishable from inside the save check. Either way, the frame keeps wherever the drag left it; only the write to your geometry table is skipped.

A successful save writes geometry.point, .relPoint, .x, and .y in place. Foundry never assigns a new geometry table, and never touches any other key already in yours.


5. F.Window:Reset(win)

Clears the four geometry keys from your table and re-applies default immediately, with no reload required, so a window dragged somewhere unreachable comes right back. Calling :Reset on a frame that was never attached is refused with F:RaiseDevError("Window:Reset: frame is not attached") and does nothing. :Reset has no return value.


6. Caller obligations

  • Call :Attach after your SavedVariables exist. It applies a position immediately, so call it from your addon's own login hook (OnInitialize, OnLogin, or equivalent), never at file scope.
  • win must be parented directly to UIParent. This is checked at :Attach and re-checked on every save.
  • You create and level the frame and its handle. Foundry never calls CreateFrame. Set the handle's frame level yourself relative to anything else on the window (a close button, for example) that should win clicks over the drag handle.
  • The geometry table is captured by reference, not copied. Foundry writes into the exact table instance you passed at :Attach for the rest of the frame's life. Never replace it (db.window = {} after :Attach silently detaches Foundry from your data); mutate it in place instead. This matters in particular for a profile switch: swap the contents of the table your addon already gave Foundry, not the table reference.
  • Your frame and handle stay insecure. Every call Foundry makes is a plain widget method; nothing here is protected or restricted, so dragging works in combat. Attaching a protected frame is out of contract; Foundry does not check for it.

7. What Foundry.Window does not do

  • Frame factory. You create win and its handle; Foundry never calls CreateFrame.
  • Resize. Foundry.Window only moves a frame; it does not touch its size.
  • Size persistence. Only the four position keys are read or written.
  • Scale management. Foundry.Window never calls SetScale. A restored position is applied in the window's own scaled units at restore time, so a scale changed between save and restore shifts the visual position proportionally. That's expected, not a bug.
  • Edit Mode integration. Foundry.Window is a plain drag-and-persist helper, independent of Blizzard's Edit Mode system.
  • Detach. There is no :Detach in v1. Once attached, a frame stays attached for the addon's session.

8. Worked example

local F = _G.Foundry_1_0

local MyWindow = CreateFrame("Frame", "MyAddonWindow", UIParent, "BackdropTemplate")
MyWindow:SetSize(300, 200)
MyWindow.TitleBar = CreateFrame("Frame", nil, MyWindow)
MyWindow.TitleBar:SetPoint("TOPLEFT")
MyWindow.TitleBar:SetPoint("TOPRIGHT")
MyWindow.TitleBar:SetHeight(24)

local MyModule = {}

function MyModule:OnLogin()
    -- db is your own saved-variables table (Foundry.DB, AceDB, or
    -- hand-rolled). Materialize the geometry table here if this is the
    -- window's first login: an empty table is the "never saved" state, and
    -- Attach applies `default` to it exactly like a fresh install.
    db.profile.window = db.profile.window or {}

    F.Window:Attach(MyWindow, {
        handle   = MyWindow.TitleBar,
        geometry = db.profile.window,
        default  = {"CENTER", "CENTER", 0, 0},
    })
end

-- Somewhere in a slash command or options panel:
function MyModule:ResetWindowPosition()
    F.Window:Reset(MyWindow)
end

Call MyModule:OnLogin() from your addon's own login hook, after db (your saved-variables table) is loaded.


9. Escape hatch

Foundry.Window keeps no controller and hands back nothing beyond win itself on a successful :Attach; there's no separate handle object to reach into. You already hold win and config.handle; call any native method on either at any time. If you find yourself needing Foundry.Window to do something with either frame that it doesn't already do, that's a signal for a feature request rather than a workaround.

Clone this wiki locally