Don't use DIALOG or TOOLTIP strata for things that are not dialogs or tooltips unless you have a very good reason. For all other cases, you should just parent frames appropriately, and they will automatically appear on the correct strata. If you "need" strange parenting for some reason, put both x and y on the same strata and use y:SetFrameLevel(x:GetFrameLevel() + 1) to fix the layering.
For boolean values, you can just do "if x then y else z end" instead of "if x == true then y elseif x == false then z end".
You initially set the button to 20% alpha. When it's clicked, you set the alpha to 100% when you hide the frame, but when you show the frame again you don't set the button back to 20% alpha. I'm assuming this is why you added the OnEnter and OnLeave scripts, but you can get rid of those if you just do btn:SetAlpha(0.2) when you do f:Show() in the OnClick script.
If that function is intended to be in the global namespace -- eg. not local, and not a table member like MyAddon:collapse_container() -- you should give it a more descriptive name that clearly identifies which addon it belongs to, like MyAddon_CollapseContainer.
You should add a check to prevent the function from creating frames for the same object more than once.
Finally, your code will be a lot easier to read and maintain if you use consistent indentation, spacing between comma-separated items, line breaks, and consistent naming practices (variables are lowercase, function and object names are camelcase, etc.).
Code:
function MyAddon_CollapseContainer(name, attach, state, fwidth, fheight, bwidth, bheight, point, relpoint)
if not attach.containerFrame then
local f = CreateFrame("Frame", name.."_Frame", attach)
f:SetPoint("CENTER", attach, "CENTER")
f:SetSize(100, 100)
attach.containerFrame = f
f:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
})
f:SetBackdropColor(0, 0, 0, 1)
f:EnableMouse(true)
f:SetMovable(true)
f:SetClampedToScreen(true)
f:RegisterForDrag("LeftButton")
f:SetScript("OnDragStart", f.StartMoving)
f:SetScript("OnDragStop", f.StopMovingOrSizing)
local btn = CreateFrame("Button", name.."_Button", attach)
btn:SetFrameLevel(f:GetFrameLevel() + 1)
btn:SetPoint("TOPLEFT", f, -25, 0)
btn:SetSize(28, 28)
btn:SetAlpha(0.2)
attach.containerButton = btn
btn:SetNormalTexture("Interface\\CHATFRAME\\UI-ChatIcon-Minimize-Up")
btn:SetPushedTexture("Interface\\CHATFRAME\\UI-ChatIcon-Minimize-Down")
btn.state = state
btn:RegisterForClicks("LeftButtonUp")
btn:SetScript("OnClick", function(self, mouseButtonClicked)
if self.state then
--show f, lock btn to 1 alpha
self.state = false
f:Hide()
btn:SetAlpha(1)
else
--hide f, keep showing btn
self.state = true
f:Show()
btn:SetAlpha(0.2)
end
end)
end
if state then
attach.containerFrame:Show()
else
attach.containerFrame:Hide()
end
end
Also, I don't see anything in your code that's actually attaching the object to be contained to the container, so when you move/collapse the container, the object itself is unaffected. Have you actually tried to use this code, or are you just throwing out a drycoded idea?