forked from EllesmereGaming/EllesmereUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEUI_UnlockMode_Anchors.lua
More file actions
2039 lines (1931 loc) · 90.4 KB
/
Copy pathEUI_UnlockMode_Anchors.lua
File metadata and controls
2039 lines (1931 loc) · 90.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if EUI_CLIENT_BLOCKED then return end -- pre-12.1 client failsafe (EllesmereUI_ClientGate.lua)
-------------------------------------------------------------------------------
-- EUI_UnlockMode_Anchors.lua
-- Anchor / match engine: anchor and size-match DB helpers, match status
-- API, link pruning and re-keying, NotifyElementResized, OnSizeChanged
-- hooks, combat-parked positioning, fallback anchors.
-- Loaded after EUI_UnlockMode.lua; _unlockCoreInit runs it once with UM.
-------------------------------------------------------------------------------
local _, EUI_NS = ...
EUI_NS = EUI_NS.__euiCoreNS or EUI_NS -- standalone builds: the core's own table (EllesmereUI.lua)
EUI_NS.unlockParts = EUI_NS.unlockParts or {}
EUI_NS.unlockParts.Anchors = function(UM)
local ns, floor, PP, registeredElements = EUI_NS, UM.floor, UM.PP, UM.registeredElements
local movers, pendingPositions, SELECT_ELEMENT_ALPHA, SELECT_ELEMENT_FADE = UM.movers, UM.pendingPositions, UM.SELECT_ELEMENT_ALPHA, UM.SELECT_ELEMENT_FADE
-------------------------------------------------------------------------------
-- Anchor / Match DB helpers -- eui-style: allow comment-budget
-- EllesmereUIDB.unlockAnchors = { [childKey] = { target=key, side="LEFT"|"RIGHT"|"TOP"|"BOTTOM" } }
-- Width/height matches apply immediately into the element's own settings.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Actual grow direction -- never nil; used by position math that needs the
-- true anchor edge.
-------------------------------------------------------------------------------
local function GetBarGrowDirActual(barKey)
if barKey == "EQT_Tracker" then return "DOWN" end
-- Via the owning module's resolver so menu/layout never disagree (clamps to orientation on read, not save).
if barKey == "ERB_TotemBar" then
if EllesmereUI.GetTotemGrowDir then return (EllesmereUI.GetTotemGrowDir()) end
return "RIGHT"
end
if barKey == "EABR_Reminders" then
if EllesmereUI.GetAuraBuffGrowDir then
return EllesmereUI.GetAuraBuffGrowDir()
end
return "CENTER"
end
if barKey:sub(1, 4) == "CDM_" then
local rawKey = barKey:sub(5)
local cdm = EllesmereUI.Lite.GetAddon("EllesmereUICooldownManager", true)
local cdmBars = cdm and cdm.db and cdm.db.profile and cdm.db.profile.cdmBars
if cdmBars and cdmBars.bars then
for _, bar in ipairs(cdmBars.bars) do
if bar.key == rawKey then
return bar.growDirection or "CENTER"
end
end
end
return "CENTER"
else
local eab = EllesmereUI.Lite.GetAddon("EllesmereUIActionBars", true)
local s = eab and eab.db and eab.db.profile and eab.db.profile.bars
and eab.db.profile.bars[barKey]
if s then
return (s.growDirection or "up"):upper()
end
return "CENTER"
end
end
-------------------------------------------------------------------------------
-- Grow direction from the bar's per-profile settings: uppercase string, or nil
-- if default/unset. Action bar default "UP", CDM default nil (centered).
-------------------------------------------------------------------------------
local function GetBarGrowDir(barKey)
if barKey == "EQT_Tracker" then return "DOWN" end
if barKey == "ERB_TotemBar" then
if not EllesmereUI.GetTotemGrowDir then return "RIGHT" end
local g = EllesmereUI.GetTotemGrowDir()
if g == "CENTER" then return nil end -- centered = no direction indicator
return g
end
if barKey == "EABR_Reminders" then
if not EllesmereUI.GetAuraBuffGrowDir then return nil end
local g = EllesmereUI.GetAuraBuffGrowDir()
if g and g ~= "CENTER" then return g end
return nil
end
if barKey:sub(1, 4) == "CDM_" then
local rawKey = barKey:sub(5)
local cdm = EllesmereUI.Lite.GetAddon("EllesmereUICooldownManager", true)
local cdmBars = cdm and cdm.db and cdm.db.profile and cdm.db.profile.cdmBars
if cdmBars and cdmBars.bars then
for _, bar in ipairs(cdmBars.bars) do
if bar.key == rawKey then
local g = bar.growDirection
if g and g ~= "CENTER" then return g end
return nil
end
end
end
return nil
else
local eab = EllesmereUI.Lite.GetAddon("EllesmereUIActionBars", true)
local s = eab and eab.db and eab.db.profile and eab.db.profile.bars
and eab.db.profile.bars[barKey]
if s then
local g = (s.growDirection or "up"):upper()
if g == "CENTER" then return nil end
-- UP is default for horizontal bars (no indicator) but meaningful for vertical (show indicator)
if g == "UP" and (s.orientation or "horizontal") ~= "vertical" then return nil end
return g
end
return nil
end
end
local function GetAnchorDB()
if not EllesmereUIDB then return nil end
if not EllesmereUIDB.unlockAnchors then
EllesmereUIDB.unlockAnchors = {}
end
return EllesmereUIDB.unlockAnchors
end
local function GetAnchorInfo(barKey)
local db = GetAnchorDB()
if not db then return nil end
return db[barKey]
end
-- Re-applies the grow-direction-aware anchor after a resize so the fixed edge stays
-- put. Must be synchronous (no C_Timer.After) to avoid a visible flicker frame.
-- Defined here, after its dependencies, so the closure captures the right upvalues.
function EllesmereUI.RecenterBarAnchor(barKey)
if not UM.isUnlocked then return end
local elem = registeredElements[barKey]
if elem and elem.isAnchored and elem.isAnchored() then return end
local b = UM.GetBarFrame(barKey)
if not b then return end
local s = b:GetEffectiveScale()
local uiS = UIParent:GetEffectiveScale()
local elemScale = s / uiS
local bL = b:GetLeft()
local bT = b:GetTop()
if not bL or not bT then return end
local w = (b:GetWidth() or 0) * elemScale
local h = (b:GetHeight() or 0) * elemScale
if w < 1 or h < 1 then return end
-- Center in UIParent-BOTTOMLEFT space
local uiCX = bL * elemScale + w * 0.5
local uiCY = bT * elemScale - h * 0.5
local growDir = GetBarGrowDirActual(barKey)
local anchor, aX, aY
if growDir == "RIGHT" then
anchor = "LEFT"
aX = bL * elemScale
aY = uiCY
elseif growDir == "LEFT" then
anchor = "RIGHT"
aX = (bL * elemScale) + w
aY = uiCY
elseif growDir == "DOWN" then
anchor = "TOP"
aX = uiCX
aY = bT * elemScale
elseif growDir == "UP" then
anchor = "BOTTOM"
aX = uiCX
aY = bT * elemScale - h
else
anchor = "CENTER"
aX = uiCX
aY = uiCY
end
-- Convert to CENTER-relative (the unlock mode system's standard format)
local uiW, uiH = UIParent:GetSize()
local cRelX = aX - uiW / 2
local cRelY = aY - uiH / 2
-- Snap to the physical pixel grid so subpixel coords never persist into pendingPositions -> CommitPositions -> SavedVariables.
local PPr = PP or (EllesmereUI and EllesmereUI.PP)
if PPr then
if anchor == "CENTER" then
cRelX = PPr.SnapCenterForDim(cRelX, w, uiS)
cRelY = PPr.SnapCenterForDim(cRelY, h, uiS)
else
cRelX = PPr.SnapForES(cRelX, uiS)
cRelY = PPr.SnapForES(cRelY, uiS)
end
end
-- cRelX/cRelY are UIParent units; SetPoint offsets read in the frame's own space
-- (identical unless the element self-scales, see ApplyCenterPosition) -- divide is a no-op for anything unscaled.
local setX, setY = cRelX, cRelY
if elemScale ~= 1 and elemScale > 0 then
setX = cRelX / elemScale
setY = cRelY / elemScale
end
pcall(function()
b:ClearAllPoints()
b:SetPoint(anchor, UIParent, "CENTER", setX, setY)
end)
-- Keep mover's stored center in sync so drag/snap logic stays consistent
local m = movers[barKey]
if m and m._setCenterXY then
m._setCenterXY(uiCX, uiCY - uiH)
end
end
local function SetAnchorInfo(childKey, targetKey, side, offsetX, offsetY)
local db = GetAnchorDB()
if not db then return end
-- Keep an existing fallback only when re-anchoring to the SAME target; a new target invalidates it (fallback belongs to the link, not the child).
local prev = db[childKey]
local fb = prev and prev.target == targetKey and prev.fallback or nil
-- The cross-axis screen edge belongs to the CHILD, not to this link, so it
-- survives a re-anchor -- unless the new target claims its axis (edge target),
-- in which case the two would fight over the same coordinate.
local edge = prev and prev.edge or nil
if edge and edge.key and EllesmereUI._ScreenEdgeAxis
and EllesmereUI._ScreenEdgeAxis(edge.key) == EllesmereUI._ScreenEdgeAxis(targetKey) then
edge = nil
end
db[childKey] = { target = targetKey, side = side, offsetX = offsetX, offsetY = offsetY,
fallback = fb, edge = edge }
-- Link-change stamp: modules with memoized views over the anchor DB (e.g.
-- the tracking bar growth-edge extent watch) re-derive lazily.
EllesmereUI._anchorLinksStamp = (EllesmereUI._anchorLinksStamp or 0) + 1
end
local function ClearAnchorInfo(childKey)
local db = GetAnchorDB()
if not db then return end
db[childKey] = nil
EllesmereUI._anchorLinksStamp = (EllesmereUI._anchorLinksStamp or 0) + 1
end
local function IsAnchored(barKey)
local info = GetAnchorInfo(barKey)
if info ~= nil then return true end
local elem = registeredElements[barKey]
return elem and elem.isAnchored and elem.isAnchored() or false
end
-- Element anchored via a module option (e.g. ERB "Anchor To") with keepMoverWhenAnchored:
-- mover exists but is position-locked (module's anchor owns position) -- drag/nudge/
-- anchor-link disabled, resize and width/height match stay. On ns: deferred body is at the 200-local cap.
function ns.IsMoverPosLocked(barKey)
local elem = registeredElements[barKey]
if not (elem and elem.keepMoverWhenAnchored) then return false end
return elem.isAnchored and elem.isAnchored() or false
end
-- Width/Height match persistent links
local MatchH = {}
function MatchH.GetWidthMatchDB()
if not EllesmereUIDB then return nil end
if not EllesmereUIDB.unlockWidthMatch then
EllesmereUIDB.unlockWidthMatch = {}
end
return EllesmereUIDB.unlockWidthMatch
end
function MatchH.GetHeightMatchDB()
if not EllesmereUIDB then return nil end
if not EllesmereUIDB.unlockHeightMatch then
EllesmereUIDB.unlockHeightMatch = {}
end
return EllesmereUIDB.unlockHeightMatch
end
-- Extra width / height on a match (the unlock cog's Extra Width / Extra Height
-- rows): whole physical pixels at the child's scale, added to the matched size
-- after the target's pad and before the child's own pad comes off. Keyed by the
-- child element like its link ("w" / "h" axis); stored only while non-zero and
-- read only while the child's link exists, so a leftover entry does nothing.
function EllesmereUI.GetMatchExtra(axis, key)
if not EllesmereUIDB or not key then return nil end
local t
if axis == "h" then t = EllesmereUIDB.unlockHeightMatchExtra
else t = EllesmereUIDB.unlockWidthMatchExtra end
local v = t and t[key]
if v and v ~= 0 then return v end
return nil
end
function MatchH.SetMatchExtra(axis, key, px)
if not EllesmereUIDB or not key then return end
local field = "unlockWidthMatchExtra"
if axis == "h" then field = "unlockHeightMatchExtra" end
px = tonumber(px)
if px then px = math.floor(px + 0.5) end
local t = EllesmereUIDB[field]
if not px or px == 0 then
if t then t[key] = nil end
return
end
if not t then t = {}; EllesmereUIDB[field] = t end
t[key] = px
end
function MatchH.GetWidthMatchInfo(barKey)
local db = MatchH.GetWidthMatchDB()
return db and db[barKey] or nil
end
function MatchH.GetHeightMatchInfo(barKey)
local db = MatchH.GetHeightMatchDB()
return db and db[barKey] or nil
end
-- Cycle detect: walk the chain from targetKey; reaching childKey would loop forever.
function MatchH.WouldCreateCycle(db, childKey, targetKey)
local visited = {}
local current = targetKey
while current do
if current == childKey then return true end
if visited[current] then return false end
visited[current] = true
current = db[current]
end
return false
end
-- True when barKey's width/height is driven, via an unbroken chain of match links,
-- by a content-sized bar: a CDM_ bar, or an action bar reached through at least one
-- link (0-hop start mirrors call-site direct-target tests, so a bar matched
-- DIRECTLY to a plain action bar never qualifies). Match DBs key on literal element
-- keys (no alias resolution), as the anchor cascade reads them; visited guards malformed cycles.
function MatchH.ChainDrivenToBar(barKey, axis)
if not barKey then return false end
local db
if axis == "width" then
db = MatchH.GetWidthMatchDB()
elseif axis == "height" then
db = MatchH.GetHeightMatchDB()
else
return false
end
local abKeys = EllesmereUI._abBarKeys
local visited = {}
local current = barKey
while current do
if current:sub(1, 4) == "CDM_" then
return true
end
if abKeys and abKeys[current] and current ~= barKey then
return true
end
if not db or visited[current] then return false end
visited[current] = true
current = db[current]
end
return false
end
function MatchH.SetWidthMatch(childKey, targetKey)
local db = MatchH.GetWidthMatchDB()
if not db then return end
if MatchH.WouldCreateCycle(db, childKey, targetKey) then
-- Break the cycle: clear the link that targetKey has, then set ours
db[targetKey] = nil
MatchH.SetMatchExtra("w", targetKey, nil)
end
-- A new match starts at +0.
if db[childKey] ~= targetKey then MatchH.SetMatchExtra("w", childKey, nil) end
db[childKey] = targetKey
end
function MatchH.SetHeightMatch(childKey, targetKey)
local db = MatchH.GetHeightMatchDB()
if not db then return end
if MatchH.WouldCreateCycle(db, childKey, targetKey) then
db[targetKey] = nil
MatchH.SetMatchExtra("h", targetKey, nil)
end
if db[childKey] ~= targetKey then MatchH.SetMatchExtra("h", childKey, nil) end
db[childKey] = targetKey
end
function MatchH.ClearWidthMatch(childKey)
local db = MatchH.GetWidthMatchDB()
if not db then return end
-- Persist current width so "0 = match parent" defaults don't revert on reload
-- (elem.setWidth saves to its own DB); pixel-snapped like every match apply, else an off-grid raw GetWidth propagates through setters/harvests.
local elem = registeredElements[childKey]
if elem and elem.setWidth then
local frame = UM.GetBarFrame(childKey)
if frame then
local curW = frame:GetWidth()
if curW and curW > 0 then
local PPm = EllesmereUI and EllesmereUI.PP
if PPm and PPm.SnapForES then
curW = PPm.SnapForES(curW, frame:GetEffectiveScale())
end
pcall(elem.setWidth, childKey, curW)
end
end
end
db[childKey] = nil
MatchH.SetMatchExtra("w", childKey, nil)
end
function MatchH.ClearHeightMatch(childKey)
local db = MatchH.GetHeightMatchDB()
if not db then return end
local elem = registeredElements[childKey]
if elem and elem.setHeight then
local frame = UM.GetBarFrame(childKey)
if frame then
local curH = frame:GetHeight()
if curH and curH > 0 then
local PPm = EllesmereUI and EllesmereUI.PP
if PPm and PPm.SnapForES then
curH = PPm.SnapForES(curH, frame:GetEffectiveScale())
end
pcall(elem.setHeight, childKey, curH)
end
end
end
db[childKey] = nil
MatchH.SetMatchExtra("h", childKey, nil)
end
-------------------------------------------------------------------------------
-- Public API: query width/height match state from any addon
-------------------------------------------------------------------------------
function EllesmereUI.GetWidthMatchTarget(barKey)
local db = MatchH.GetWidthMatchDB()
return db and db[barKey] or nil
end
function EllesmereUI.GetHeightMatchTarget(barKey)
local db = MatchH.GetHeightMatchDB()
return db and db[barKey] or nil
end
-- Returns (disabled_fn, tooltip_fn, rawTooltip) for width/height sliders, composing
-- with an optional existing disabled/tooltip so both conditions work.
-- rawTooltip=true tells the widget system to skip DisabledTooltip wrapping.
function EllesmereUI.MatchGuard(barKey, axis, existingDisabled, existingTooltip)
local isWidth = (axis == "Width" or axis == "width")
local getFn = isWidth and EllesmereUI.GetWidthMatchTarget or EllesmereUI.GetHeightMatchTarget
local disabled = function()
if getFn(barKey) then return true end
if existingDisabled then return existingDisabled() end
return false
end
local tooltip = function()
local target = getFn(barKey)
if target then
local name = (EllesmereUI.GetBarLabel and EllesmereUI.GetBarLabel(target)) or target
return axis .. " matched to " .. name .. ". Unmatch in Unlock Mode to edit."
end
if existingTooltip then
return type(existingTooltip) == "function" and existingTooltip() or existingTooltip
end
return ""
end
local rawTooltip = function()
return getFn(barKey) ~= nil
end
return disabled, tooltip, rawTooltip
end
-------------------------------------------------------------------------------
-- PruneStaleLinks -- removes all anchor/match links for a key, as child AND
-- as target. Call on unregister so nothing points to a ghost key.
-------------------------------------------------------------------------------
local function PruneStaleLinks(key)
if not EllesmereUIDB then return end
-- Anchors: key as child
local anchors = EllesmereUIDB.unlockAnchors
if anchors then
anchors[key] = nil
-- key as target -- scan all children
for childKey, info in pairs(anchors) do
if info and info.target == key then
anchors[childKey] = nil
end
end
end
-- Match extras go with the links dropped below: the key's own, and those of
-- the children matched to it (read before those links are gone).
local wx = EllesmereUIDB.unlockWidthMatchExtra
if wx then
local wmL = EllesmereUIDB.unlockWidthMatch
wx[key] = nil
for childKey in pairs(wx) do
if wmL and wmL[childKey] == key then wx[childKey] = nil end
end
end
local hx = EllesmereUIDB.unlockHeightMatchExtra
if hx then
local hmL = EllesmereUIDB.unlockHeightMatch
hx[key] = nil
for childKey in pairs(hx) do
if hmL and hmL[childKey] == key then hx[childKey] = nil end
end
end
-- Width matches: key as child or target
local wm = EllesmereUIDB.unlockWidthMatch
if wm then
wm[key] = nil
for childKey, targetKey in pairs(wm) do
if targetKey == key then wm[childKey] = nil end
end
end
-- Height matches: key as child or target
local hm = EllesmereUIDB.unlockHeightMatch
if hm then
hm[key] = nil
for childKey, targetKey in pairs(hm) do
if targetKey == key then hm[childKey] = nil end
end
end
end
EllesmereUI.PruneStaleLinks = PruneStaleLinks
-- Override UnregisterUnlockElement so cleanup runs on removal (e.g. a deleted CDM bar).
function EllesmereUI:UnregisterUnlockElement(key)
self._unlockRegisteredElements[key] = nil
self._unlockRegistrationDirty = true
PruneStaleLinks(key)
end
-------------------------------------------------------------------------------
-- ShiftIndexedAnchorKeys -- re-keys anchor/size-match links for an index-keyed
-- element family after a slot is removed (deleting Tracking Bar 2 of 4 shifts
-- TBB_3->TBB_2, TBB_4->TBB_3, mirroring the addon's own re-keyed position
-- store). Links pointing AT the removed key are severed (child keeps its
-- stored position); the removed key's own links go with it.
-------------------------------------------------------------------------------
function EllesmereUI.ShiftIndexedAnchorKeys(prefix, removedIdx, oldCount)
if not EllesmereUIDB then return end
local removedKey = prefix .. removedIdx
local plen = #prefix
-- Returns the shifted key for keys above the removed index, else nil.
local function ShiftedKey(key)
if type(key) ~= "string" or key:sub(1, plen) ~= prefix then return nil end
local i = tonumber(key:sub(plen + 1))
if not i or i <= removedIdx or i > oldCount then return nil end
return prefix .. (i - 1)
end
local anchors = EllesmereUIDB.unlockAnchors
if anchors then
-- Sever children anchored to the removed key; retarget higher indexes.
for childKey, info in pairs(anchors) do
if info and info.target == removedKey then
anchors[childKey] = nil
elseif info then
local nt = ShiftedKey(info.target)
if nt then info.target = nt end
end
end
-- Shift child-role keys down one slot.
anchors[removedKey] = nil
for i = removedIdx + 1, oldCount do
local oldK, newK = prefix .. i, prefix .. (i - 1)
anchors[newK] = anchors[oldK]
anchors[oldK] = nil
end
end
local function ShiftMatchStore(store)
if not store then return end
for childKey, targetKey in pairs(store) do
if targetKey == removedKey then
store[childKey] = nil
else
local nt = ShiftedKey(targetKey)
if nt then store[childKey] = nt end
end
end
store[removedKey] = nil
for i = removedIdx + 1, oldCount do
local oldK, newK = prefix .. i, prefix .. (i - 1)
store[newK] = store[oldK]
store[oldK] = nil
end
end
-- Match extras (child key -> px) follow their links: a child whose link is
-- severed (it pointed at the removed key) loses its extra, read before the
-- link stores shift below; the rest re-key like the links (ShiftedKey never
-- retargets a number, so only the child-role keys move).
local function DropSeveredExtras(links, extras)
if not (links and extras) then return end
for childKey, targetKey in pairs(links) do
if targetKey == removedKey then extras[childKey] = nil end
end
end
DropSeveredExtras(EllesmereUIDB.unlockWidthMatch, EllesmereUIDB.unlockWidthMatchExtra)
DropSeveredExtras(EllesmereUIDB.unlockHeightMatch, EllesmereUIDB.unlockHeightMatchExtra)
ShiftMatchStore(EllesmereUIDB.unlockWidthMatchExtra)
ShiftMatchStore(EllesmereUIDB.unlockHeightMatchExtra)
ShiftMatchStore(EllesmereUIDB.unlockWidthMatch)
ShiftMatchStore(EllesmereUIDB.unlockHeightMatch)
end
-- Validate stored relationships against registered elements. Runs on every
-- unlock-mode open: drops links whose endpoint is gone for good, and size
-- matches a noResize endpoint cannot use (see MatchUnusable).
local function ValidateStoredLinks()
if not EllesmereUIDB then return end
local elems = EllesmereUI._unlockRegisteredElements
-- While a spec-override unlock LAYER is live, never prune a missing endpoint:
-- elements may exist for only some specs, and the transition harvest would bank
-- the prune into the layer, destroying data other specs expect.
local activeFn = EllesmereUI.SpecOverrides_UnlockActive
local function OverrideProtected()
return (activeFn and activeFn() ~= nil) and true or false
end
-- Tracking Bar keys are spec-scoped (registry holds only the current spec's
-- bars), so a missing TBB_ key may exist for another spec; never prune over one
-- (bar deletion re-keys/severs via ShiftIndexedAnchorKeys). Global tracking bar
-- groups (TBBG_) live in a per-profile registry too and may come back.
local function MissingForGood(key)
if key ~= nil and elems[key] then return false end
if type(key) == "string" and key:find("^TBB_%d+$") then return false end
if type(key) == "string" and key:find("^TBBG_") then return false end
return true
end
-- A unit frame key missing from the registry is switched off by a unit
-- setting (Frame Source, Enable) or the module being off, never deleted. A
-- link whose CHILD is one stays for when it comes back (nothing places a
-- missing child) as long as its other end is live or another such key (a
-- cast bar and its frame). A live child anchored or matched to one is still
-- freed, so it is never left following a frame that is not there.
local resolveFolder = EllesmereUI.ResolveKeyToFolder
local function ufKey(key)
return resolveFolder ~= nil and resolveFolder(key) == "EllesmereUIUnitFrames"
end
-- WoW Forever never builds the House Favor bar or the Battle Res and
-- Bloodlust icons: a link whose CHILD is one of them stays for the other
-- client on the same terms (its other end live, a unit frame key, or
-- another such key).
local function clientAbsent(key)
return EllesmereUI.IS_FOREVER == true
and (key == "FavorBar" or key == "EUI_BattleRes" or key == "EUI_Bloodlust")
end
local function LinkGone(childKey, targetKey)
local childGone, targetGone = MissingForGood(childKey), MissingForGood(targetKey)
if not (childGone or targetGone) then return false end
if childGone and (ufKey(childKey) or clientAbsent(childKey))
and (not targetGone or ufKey(targetKey) or clientAbsent(targetKey)) then
return false
end
return true
end
local anchors = EllesmereUIDB.unlockAnchors
if anchors then
for childKey, info in pairs(anchors) do
if LinkGone(childKey, info and info.target)
and not OverrideProtected(childKey) then
anchors[childKey] = nil
elseif info and info.edge and info.edge.key and LinkGone(childKey, info.edge.key)
and not OverrideProtected(childKey) then
-- Unknown cross-axis edge (a string from a build without this
-- feature): drop the extra, keep the link itself.
info.edge = nil
end
end
end
-- A size match a noResize endpoint cannot use: a child that cannot be sized
-- (unless it may match as a source) or a target with no size to follow.
-- sizeFixedByLook endpoints are only size-locked by the current look
-- (Blizzard Style unit frames), so their links stay for the other look.
local function MatchUnusable(childKey, targetKey)
local c, t = elems[childKey], elems[targetKey]
if not (c and t) then return false end
if c.noResize and not c.allowMatchSource and not c.sizeFixedByLook then return true end
if t.noResize and not t.sizeFixedByLook then return true end
return false
end
local wm = EllesmereUIDB.unlockWidthMatch
if wm then
for childKey, targetKey in pairs(wm) do
if LinkGone(childKey, targetKey)
and not OverrideProtected(childKey) then
wm[childKey] = nil
elseif MatchUnusable(childKey, targetKey) then
wm[childKey] = nil
end
end
end
local hm = EllesmereUIDB.unlockHeightMatch
if hm then
for childKey, targetKey in pairs(hm) do
if LinkGone(childKey, targetKey)
and not OverrideProtected(childKey) then
hm[childKey] = nil
elseif MatchUnusable(childKey, targetKey) then
hm[childKey] = nil
end
end
end
-- A match extra is read only while its link exists: drop every extra whose
-- link is gone, so orphans never ride into snapshots and profiles.
local wx = EllesmereUIDB.unlockWidthMatchExtra
if wx then
for childKey in pairs(wx) do
if not (wm and wm[childKey]) then wx[childKey] = nil end
end
end
local hx = EllesmereUIDB.unlockHeightMatchExtra
if hx then
for childKey in pairs(hx) do
if not (hm and hm[childKey]) then hx[childKey] = nil end
end
end
end
-- Apply width/height match: sync source size from target. _propagatingMatch
-- prevents re-entrant loops (setWidth -> OnSizeChanged -> NotifyElementResized ->
-- PropagateWidthMatch); exposed so child addons' setWidth can detect it.
local _propagatingMatch = false
EllesmereUI._propagatingMatch = false
function MatchH.ApplyWidthMatch(sourceKey, targetKey)
local targetElem = registeredElements[targetKey]
local targetBar = UM.GetBarFrame(targetKey)
local targetW
if targetElem and targetElem.getSize then
targetW = targetElem.getSize(targetKey)
elseif targetBar then
targetW = targetBar:GetWidth()
end
-- Chrome drawn outside the target's own rect (getMatchPad, e.g. a classic
-- resource bar's frame): the match lines up with what is on screen.
if targetW and targetElem and targetElem.getMatchPad then
local pw = targetElem.getMatchPad(targetKey)
if pw and pw > 0 then targetW = targetW + pw end
end
-- A width that is not a real number (a frame not laid out yet can read NaN)
-- skips the match: WoW Forever errors on dividing one.
if targetW and EllesmereUI.PP.IsNum(targetW) and targetW > 0 then
local rawW, conv = targetW, 1
-- Snap to the physical pixel grid with round-to-nearest: PP.Scale
-- truncates and drops a pixel on float boundary values; SnapForES uses
-- floor(x/px + 0.5), which is safe.
local PPm = EllesmereUI and EllesmereUI.PP
if PPm and PPm.SnapForES and targetBar then
targetW = PPm.SnapForES(targetW, targetBar:GetEffectiveScale())
else
targetW = floor(targetW + 0.5)
end
-- Convert target width to source's coordinate space if scales differ
local sourceBar = UM.GetBarFrame(sourceKey)
if targetBar and sourceBar then
local tES = targetBar:GetEffectiveScale()
local sES = sourceBar:GetEffectiveScale()
if math.abs(tES - sES) > 0.001 then
targetW = targetW * tES / sES
conv = tES / sES
end
end
local sourceElem = registeredElements[sourceKey]
if sourceElem and sourceElem.setWidth then
-- The source's own outside chrome comes off the width it is set
-- to, so what it draws is what matches. Taken off the unsnapped
-- width and snapped once: snapping before the subtraction rounds a
-- half-pixel pad twice, leaving two bars with the same pad a pixel
-- apart.
if sourceElem.getMatchPad then
local pw = sourceElem.getMatchPad(sourceKey)
if pw and pw > 0 then
targetW = rawW * conv - pw
if PPm and PPm.SnapForES and sourceBar then
targetW = PPm.SnapForES(targetW, sourceBar:GetEffectiveScale())
else
targetW = floor(targetW + 0.5)
end
targetW = math.max(1, targetW)
end
end
-- The match's Extra Width (whole pixels at the source's scale). targetW
-- sits on the source's pixel grid here, so whole pixels keep it there
-- with no second rounding. nil = no extra: nothing changes.
local ex = EllesmereUI.GetMatchExtra("w", sourceKey)
if ex then
local es = sourceBar and sourceBar:GetEffectiveScale()
local one = (PPm and PPm.perfect and es and es > 0) and (PPm.perfect / es) or 1
targetW = math.max(1, targetW + ex * one)
end
if UM.isUnlocked then
local sb = UM.GetBarFrame(sourceKey)
local savedAlpha = sb and EllesmereUI._GetFFD(sb).restoreAlpha
if sb and not savedAlpha then EllesmereUI._UnlockSetBarAlpha(sb, 0) end
_propagatingMatch = true; EllesmereUI._propagatingMatch = true
pcall(sourceElem.setWidth, sourceKey, targetW)
_propagatingMatch = false; EllesmereUI._propagatingMatch = false
EllesmereUI.RecenterBarAnchor(sourceKey)
if sb and not savedAlpha then
C_Timer.After(0, function() EllesmereUI._UnlockSetBarAlpha(sb, 1) end)
end
local m = movers[sourceKey]
if m then m:SyncSize() end
else
_propagatingMatch = true; EllesmereUI._propagatingMatch = true
pcall(sourceElem.setWidth, sourceKey, targetW)
_propagatingMatch = false; EllesmereUI._propagatingMatch = false
if sourceElem.loadPosition then
local pos = sourceElem.loadPosition(sourceKey)
if pos and pos.point == "CENTER" and pos.relPoint == "CENTER" then
UM.ApplyCenterPosition(sourceKey, pos)
end
end
end
end
end
end
function MatchH.ApplyHeightMatch(sourceKey, targetKey)
local targetElem = registeredElements[targetKey]
local targetBar = UM.GetBarFrame(targetKey)
local _, targetH
if targetElem and targetElem.getSize then
_, targetH = targetElem.getSize(targetKey)
elseif targetBar then
targetH = targetBar:GetHeight()
end
-- Outside chrome on the target (getMatchPad), as in ApplyWidthMatch.
if targetH and targetElem and targetElem.getMatchPad then
local _, ph = targetElem.getMatchPad(targetKey)
if ph and ph > 0 then targetH = targetH + ph end
end
if targetH and EllesmereUI.PP.IsNum(targetH) and targetH > 0 then
local rawH, conv = targetH, 1
local PPm = EllesmereUI and EllesmereUI.PP
if PPm and PPm.SnapForES and targetBar then
targetH = PPm.SnapForES(targetH, targetBar:GetEffectiveScale())
else
targetH = floor(targetH + 0.5)
end
-- Cross-scale conversion, mirroring ApplyWidthMatch: matched height is a
-- coordinate in the TARGET's effective scale; a source at another scale
-- would persist a physically wrong height into its module config.
local sourceBar = UM.GetBarFrame(sourceKey)
if targetBar and sourceBar then
local tES = targetBar:GetEffectiveScale()
local sES = sourceBar:GetEffectiveScale()
if math.abs(tES - sES) > 0.001 then
targetH = targetH * tES / sES
conv = tES / sES
end
end
local sourceElem = registeredElements[sourceKey]
if sourceElem and sourceElem.setHeight then
-- The source's own outside chrome comes off the unsnapped height,
-- snapped once, as in ApplyWidthMatch.
if sourceElem.getMatchPad then
local _, ph = sourceElem.getMatchPad(sourceKey)
if ph and ph > 0 then
targetH = rawH * conv - ph
if PPm and PPm.SnapForES and sourceBar then
targetH = PPm.SnapForES(targetH, sourceBar:GetEffectiveScale())
else
targetH = floor(targetH + 0.5)
end
targetH = math.max(1, targetH)
end
end
-- The match's Extra Height, as the Extra Width in ApplyWidthMatch.
local ex = EllesmereUI.GetMatchExtra("h", sourceKey)
if ex then
local es = sourceBar and sourceBar:GetEffectiveScale()
local one = (PPm and PPm.perfect and es and es > 0) and (PPm.perfect / es) or 1
targetH = math.max(1, targetH + ex * one)
end
if UM.isUnlocked then
local sb = UM.GetBarFrame(sourceKey)
local savedAlpha = sb and EllesmereUI._GetFFD(sb).restoreAlpha
if sb and not savedAlpha then EllesmereUI._UnlockSetBarAlpha(sb, 0) end
_propagatingMatch = true; EllesmereUI._propagatingMatch = true
pcall(sourceElem.setHeight, sourceKey, targetH)
_propagatingMatch = false; EllesmereUI._propagatingMatch = false
EllesmereUI.RecenterBarAnchor(sourceKey)
if sb and not savedAlpha then
C_Timer.After(0, function() EllesmereUI._UnlockSetBarAlpha(sb, 1) end)
end
local m = movers[sourceKey]
if m then m:SyncSize() end
else
_propagatingMatch = true; EllesmereUI._propagatingMatch = true
pcall(sourceElem.setHeight, sourceKey, targetH)
_propagatingMatch = false; EllesmereUI._propagatingMatch = false
if sourceElem.loadPosition then
local pos = sourceElem.loadPosition(sourceKey)
if pos and pos.point == "CENTER" and pos.relPoint == "CENTER" then
UM.ApplyCenterPosition(sourceKey, pos)
end
end
end
end
end
end
-- Pending anchor propagation keys -- batched into a single deferred frame
UM._pendingAnchorKeys = {}
local _anchorBatchScheduled = false
local function ScheduleAnchorBatch()
if _anchorBatchScheduled then return end
_anchorBatchScheduled = true
C_Timer.After(0, function()
_anchorBatchScheduled = false
if UM.isUnlocked then return end -- unlock mode handles its own saves
local keys = UM._pendingAnchorKeys
UM._pendingAnchorKeys = {}
-- Profile swap: skip AB bars entirely (LayoutBar already positioned them from
-- the new profile; stale resize events would move them wrong). CDM bars pass through (need post-settle).
local abSkip = EllesmereUI._abAnchorSuppressed and EllesmereUI._abBarKeys
for k, axis in pairs(keys) do
if abSkip and abSkip[k] then
-- skip: AB bar during profile swap
else
-- If this element is itself anchored, re-apply its own position
-- first: it resized and must reposition relative to its target.
local anchorDB = GetAnchorDB()
if anchorDB then
local ownInfo = anchorDB[k]
if ownInfo and ownInfo.target then
-- Skip AB growth bars: LayoutBar/applyPos owns their position;
-- reapplying from a stale offset yields the wrong edge and visible drift.
local isAB = EllesmereUI._abBarKeys and EllesmereUI._abBarKeys[k]
if not isAB then
UM.ApplyAnchorPosition(k, ownInfo.target, ownInfo.side)
end
end
-- An alias key (global tracking bar group riding this bar's hooks) may itself be anchored: re-apply on a shared resize.
local aliasK = EllesmereUI._unlockKeyAliases and EllesmereUI._unlockKeyAliases[k]
local aliasInfo = aliasK and anchorDB[aliasK]
if aliasInfo and aliasInfo.target then
UM.ApplyAnchorPosition(aliasK, aliasInfo.target, aliasInfo.side)
end
end
-- Propagate with axis filter (nil = all axes)
local propagateAxis = (axis == "all") and nil or axis
UM.PropagateAnchorChain(k, nil, propagateAxis)
end -- abSkip else
end
-- No persistence here: only Save & Exit (CommitPositions) writes the DB; the chain just repositioned in-place.
wipe(pendingPositions)
end)
end
-------------------------------------------------------------------------------
-- One-time follow-baseline migration ("bless the pin"): anchored grow-direction -- eui-style: allow comment-budget
-- bars saved before follow-baseline capture have a savedEdge but no tgt*
-- baseline, so their follow delta stays 0 and they can't track a resizing
-- target. The baseline can't be reconstructed from ai.offsetX/Y (those drift
-- from layout maintenance and are non-authoritative for grow bars -- why
-- savedEdge is the authority). Instead, once per bar (tgt* presence = the
-- migrated flag) at a quiescent settle, pair the UNTOUCHED savedEdge with the
-- target's live settled geometry: delta is 0 at that instant by construction,
-- so the bar doesn't move, and every future resize/login follows from the
-- blessed pair. Skipped in combat/unlock mode (retries next settle).
-------------------------------------------------------------------------------
do
local function MigrateOne(childKey, info)
local savedEdge
if childKey:sub(1, 4) == "CDM_" then
local t = EllesmereUI._cdmBarPositions
savedEdge = t and t[childKey:sub(5)]
elseif EllesmereUI._abBarKeys and EllesmereUI._abBarKeys[childKey] then
local t = EllesmereUI._abBarPositions
savedEdge = t and t[childKey]
end
if not savedEdge then return end
-- Presence of a baseline is the migrated flag -- never touch again.
if savedEdge.tgtx ~= nil or savedEdge.tgty ~= nil then return end
local growDir = GetBarGrowDirActual(childKey)
if not growDir or growDir == "CENTER" then return end
local targetBar = UM.GetBarFrame(info.target)
if not targetBar or not targetBar:GetLeft() then return end
-- A clamp-held target is not at its saved spot: pairing it would make the
-- child jump once the target is released. Retries next settle.
if EllesmereUI._RectHeldByClamp(info.target, targetBar) then return end
-- UIParent-space target geometry, computed like ApplyAnchorPosition's tL/tR/tT/tB so the baseline is comparable.
local uiS = UIParent:GetEffectiveScale()
local tS = targetBar:GetEffectiveScale()
local tL = (targetBar:GetLeft() or 0) * tS / uiS
local tR = (targetBar:GetRight() or 0) * tS / uiS
local tT = (targetBar:GetTop() or 0) * tS / uiS