Repository navigation
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 isF.API_VERSION.)
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.
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.
| 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 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).
-
win: must be a table exposingSetMovable,SetClampedToScreen,StartMoving,StopMovingOrSizing,SetUserPlaced,GetPoint,ClearAllPoints,SetPoint, andGetParent. -
win:GetParent() == UIParent:winmust 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. -
winis not already attached: a second:Attachon the same frame is refused; Foundry.Window does not re-attach. -
config: must be a table. -
config.handle: must be a table exposingEnableMouse,RegisterForDrag,SetScript, andGetScript. -
config.handle'sOnDragStartandOnDragStopscripts: both must be nil. Foundry.Window owns those two scripts and refuses to clobber existing ones rather than silently overwriting them. -
config.geometry: must be a table. -
config.default: must be{point, relPoint, x, y}withpointandrelPointfrom the anchor whitelist (TOPLEFT,TOP,TOPRIGHT,LEFT,CENTER,RIGHT,BOTTOMLEFT,BOTTOM,BOTTOMRIGHT) andx,yas finite numbers (not NaN or infinite).
-
win:SetMovable(true), then immediatelywin:SetUserPlaced(false), thenwin:SetClampedToScreen(true). Attach callsSetUserPlaced(false)immediately so the saved record, not the client's own layout cache, is the position source. - Wires
config.handle:EnableMouse(true),RegisterForDrag("LeftButton"), anOnDragStartscript that callswin:StartMoving(), and anOnDragStopscript (see §4 below). - Records the attachment (frame is the registry key) with a copy of
default. - Restores position (see §3 below).
Applied once, at :Attach, in this order:
-
Well-formed record (
point/relPointboth on the whitelist,x/yboth 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
defaultis 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):
defaultis applied.
Applying a position is always win:ClearAllPoints() followed by
win:SetPoint(point, UIParent, relPoint, x, y).
Dragging by the handle calls win:StartMoving() on OnDragStart. On
OnDragStop, in order:
-
win:StopMovingOrSizing(). -
win:SetUserPlaced(false), immediately after, so the saved record, not the client's own layout cache, is what repositions the frame across sessions. - 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.
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.
-
Call
:Attachafter 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. -
winmust be parented directly to UIParent. This is checked at:Attachand 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
geometrytable is captured by reference, not copied. Foundry writes into the exact table instance you passed at:Attachfor the rest of the frame's life. Never replace it (db.window = {}after:Attachsilently 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.
-
Frame factory. You create
winand its handle; Foundry never callsCreateFrame. - 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
:Detachin v1. Once attached, a frame stays attached for the addon's session.
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)
endCall MyModule:OnLogin() from your addon's own login hook, after db (your
saved-variables table) is loaded.
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.