Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion UnitFrames/Units/Definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,19 @@ function Units:GetSpec(def, frame)
return spec
end

-- Whether the unit button is spawned visible at all (Style.lua ApplyShown).
-- Unit-level: the UnitFrame component and its previews never read it.
Units.SHOWN_OPTION = { key = "shown", type = "toggle", label = "Show frame", default = true,
tooltip = "Turn off to hide this frame entirely. Changes made in combat apply when combat ends." }

-- Frame-free style schema of a unit (cached; schemas are not mutated).
function Units:GetSchema(unit)
self.schemas = self.schemas or {}
if not self.schemas[unit] then
self.schemas[unit] = ns.Schema.Build(ns.Components.UnitFrame.BuildSchema, self:GetSpec(self.definitions[unit]))
local schema = { Units.SHOWN_OPTION }
local style = ns.Schema.Build(ns.Components.UnitFrame.BuildSchema, self:GetSpec(self.definitions[unit]))
for _, opt in ipairs(style) do schema[#schema + 1] = opt end
self.schemas[unit] = schema
end
return self.schemas[unit]
end
Expand Down
20 changes: 20 additions & 0 deletions UnitFrames/Units/Style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ function Units:CopyPlayerSettings(unit)
copy.comboPoints = ns.Util.Copy(ns.Config:GetUnitConfig(unit).comboPoints)
-- The player frame has no threat glow: each frame keeps its own.
copy.threat = ns.Util.Copy(ns.Config:GetUnitConfig(unit).threat)
-- Hiding a frame is per frame, not part of its style.
copy.shown = ns.Config:GetUnitConfig(unit).shown
return ns.Config:UnitStore(unit):Replace(copy)
end

Expand Down Expand Up @@ -256,6 +258,22 @@ oUF:RegisterStyle(Units.STYLE, function(frame, unit)
Units:Bind(frame)
end)

-- Visibility ------------------------------------------------------------------

-- A hidden frame drops oUF's unit watch, so the game no longer shows it. The
-- watch is protected: changes made in combat wait for it to end.
function Units:ApplyShown(unit)
local frame = self.frames[unit]
if not frame then return end
local shown = ns.Config:UnitStore(unit):Resolved().shown
if not shown == not frame:IsEnabled() then return end
if InCombatLockdown() then
ns.AfterCombat:Queue("layout", frame, function() self:ApplyShown(unit) end)
return
end
if shown then frame:Enable() else frame:Disable() end
end

-- Spawning --------------------------------------------------------------------

local function Spawn(factory, unit)
Expand All @@ -264,6 +282,7 @@ local function Spawn(factory, unit)
local name = "UnitFrames" .. unit:gsub("^%l", string.upper)
local frame = factory:Spawn(unit, name)
Units.frames[unit] = frame
Units:ApplyShown(unit)
ns.Perf:End("unit spawn")
ns.Perf:Begin("edit mode registration")
ns.EditMode:Register(frame, unit, "UnitFrames: " .. def.label, def.defaultPosition)
Expand All @@ -280,6 +299,7 @@ end)

ns.Events.RegisterCallback(Units, "UnitConfigChanged", function(_, unit)
Units:Refresh(unit)
Units:ApplyShown(unit)
end)

-- Colours come from the theme too (power colours, health gradient).
Expand Down
15 changes: 15 additions & 0 deletions tests/proof/workbench.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,22 @@ local function LargeSharedDebuffs(ns)
ns.Config:SetThemeValue("auras.spacing", 4)
end

local function HiddenPetLayout()
local ns, workbench = Helpers.LoadWorkbench()
ns.Config:SetUnitValue("pet", "shown", false)
workbench:Open("unit.pet")
workbench:SelectSection("layout")
local content = workbench.inspector.scroll:GetContent()
content:SetParent(UIParent)
content:ClearAllPoints()
content:SetPoint("TOPLEFT")
content:SetSize(420, 330)
workbench.inspector:Refresh()
return content
end

return {
{ name = "workbench-pet-hidden", build = HiddenPetLayout },
{ name = "workbench-global-settings-menu", build = GlobalSettingsMenu },
{ name = "workbench-global-aura-icons", build = GlobalAuraIcons() },
{ name = "workbench-global-aura-icons-changed", build = GlobalAuraIcons(LargeSharedDebuffs) },
Expand Down
58 changes: 58 additions & 0 deletions tests/spec/units/shown_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local Helpers = require("helpers")
local Mock = Helpers.Mock

local function Watched(frame)
return frame:IsEnabled() and true or false
end

describe("Show frame", function()
local ns
before_each(function()
ns = Helpers.LoadAddon(true, true)
end)

after_each(function()
Mock.inCombat = false
end)

it("is offered on every unit's layout page and on by default", function()
for _, unit in ipairs(ns.Units.order) do
local opt = ns.Schema.Find(ns.Units:GetSchema(unit), "shown")
assert.equals("toggle", opt.type, unit)
assert.is_true(opt.default, unit)
assert.is_true(Watched(ns.Units.frames[unit]), unit)
end
end)

it("drops the unit watch when switched off and restores it when switched on", function()
local frame = ns.Units.frames.pet
ns.Config:SetUnitValue("pet", "shown", false)
assert.is_false(Watched(frame))
assert.is_false(frame:IsShown())
assert.is_true(Watched(ns.Units.frames.player))

ns.Config:SetUnitValue("pet", "shown", nil)
assert.is_true(Watched(frame))
end)

it("waits for combat to end", function()
local frame = ns.Units.frames.focus
Mock.inCombat = true
ns.Config:SetUnitValue("focus", "shown", false)
assert.is_true(Watched(frame))

Mock.inCombat = false
Mock.FireEvent("PLAYER_REGEN_ENABLED")
assert.is_false(Watched(frame))
end)

it("keeps each frame's own visibility when copying the player style", function()
ns.Config:SetUnitValue("player", "shown", false)
ns.Config:SetUnitValue("target", "shown", false)
ns.Units:CopyPlayerSettings("focus")
assert.is_nil(ns.Config:GetUnitConfig("focus").shown)
assert.is_true(Watched(ns.Units.frames.focus))
ns.Units:CopyPlayerSettings("target")
assert.is_false(ns.Config:GetUnitConfig("target").shown)
end)
end)
Loading