-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibToast-1.0.lua
executable file
·907 lines (739 loc) · 29.1 KB
/
LibToast-1.0.lua
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
-----------------------------------------------------------------------
-- Upvalued Lua API.
-----------------------------------------------------------------------
local _G = getfenv(0)
-- Functions
local error = _G.error
local pairs = _G.pairs
-- Libraries
local table = _G.table
-----------------------------------------------------------------------
-- Library namespace.
-----------------------------------------------------------------------
local LibStub = _G.LibStub
local MAJOR = "LibToast-1.0"
_G.assert(LibStub, MAJOR .. " requires LibStub")
local MINOR = 15 -- Should be manually increased
local LibToast, previousMinorVersion = LibStub:NewLibrary(MAJOR, MINOR)
if not LibToast then
return
end -- No upgrade needed
-----------------------------------------------------------------------
-- Migrations.
-----------------------------------------------------------------------
LibToast.active_toasts = LibToast.active_toasts or {}
LibToast.queued_toasts = LibToast.queued_toasts or {}
LibToast.templates = LibToast.templates or {}
LibToast.unique_templates = LibToast.unique_templates or {}
LibToast.button_heap = LibToast.button_heap or {}
LibToast.toast_heap = LibToast.toast_heap or {}
LibToast.addon_names = LibToast.addon_names or {}
LibToast.registered_sink = LibToast.registered_sink
LibToast.sink_icons = LibToast.sink_icons or {}
LibToast.sink_template = LibToast.sink_template or {} -- Cheating here, since users can only use strings.
LibToast.sink_titles = LibToast.sink_titles or {}
-----------------------------------------------------------------------
-- Variables.
-----------------------------------------------------------------------
local CurrentToast
local IsInternalCall
local CallingObject
-----------------------------------------------------------------------
-- Constants.
-----------------------------------------------------------------------
local ActiveToasts = LibToast.active_toasts
local QueuedToasts = LibToast.queued_toasts
local ToastHeap = LibToast.toast_heap
local ButtonHeap = LibToast.button_heap
local ToastProxy = {}
local METHOD_USAGE_FORMAT = MAJOR .. ":%s() - %s."
local MAX_ACTIVE_TOASTS = 10
local DEFAULT_FADE_HOLD_TIME = 5
local DEFAULT_FADE_IN_TIME = 0.5
local DEFAULT_FADE_OUT_TIME = 1.2
local DEFAULT_TOAST_WIDTH = 250
local DEFAULT_TOAST_HEIGHT = 50
local DEFAULT_GLOW_WIDTH = 252
local DEFAULT_GLOW_HEIGHT = 56
local DEFAULT_ICON_SIZE = 30
local DEFAULT_OS_SPAWN_POINT = _G.IsMacClient() and "TOPRIGHT" or "BOTTOMRIGHT"
local DEFAULT_TOAST_BACKDROP = {
bgFile = [[Interface\FriendsFrame\UI-Toast-Background]],
edgeFile = [[Interface\FriendsFrame\UI-Toast-Border]],
tile = true,
tileSize = 12,
edgeSize = 12,
insets = {
left = 5,
right = 5,
top = 5,
bottom = 5,
},
}
local DEFAULT_BACKGROUND_COLORS = {
r = 0,
g = 0,
b = 0,
}
local DEFAULT_TITLE_COLORS = {
r = 0.510,
g = 0.773,
b = 1,
}
local DEFAULT_TEXT_COLORS = {
r = 0.486,
g = 0.518,
b = 0.541
}
local TOAST_BUTTONS = {
primary_button = true,
secondary_button = true,
tertiary_button = true,
}
local TOAST_BUTTON_HEIGHT = 18
local POINT_TRANSLATION = {
CENTER = DEFAULT_OS_SPAWN_POINT,
BOTTOM = "BOTTOMRIGHT",
BOTTOMLEFT = "BOTTOMLEFT",
BOTTOMRIGHT = "BOTTOMRIGHT",
LEFT = "TOPLEFT",
RIGHT = "TOPRIGHT",
TOP = "TOPRIGHT",
TOPLEFT = "TOPLEFT",
TOPRIGHT = "TOPRIGHT",
}
local SIBLING_ANCHORS = {
TOPRIGHT = "BOTTOMRIGHT",
TOPLEFT = "BOTTOMLEFT",
BOTTOMRIGHT = "TOPRIGHT",
BOTTOMLEFT = "TOPLEFT",
}
local OFFSET_X = {
TOPRIGHT = -20,
TOPLEFT = 20,
BOTTOMRIGHT = -20,
BOTTOMLEFT = 20,
}
local OFFSET_Y = {
TOPRIGHT = -30,
TOPLEFT = -30,
BOTTOMRIGHT = 30,
BOTTOMLEFT = 30,
}
local SIBLING_OFFSET_Y = {
TOPRIGHT = -10,
TOPLEFT = -10,
BOTTOMRIGHT = 10,
BOTTOMLEFT = 10,
}
local L_TOAST = "Toast"
local L_TOAST_DESC = "Shows messages in a toast window."
local LOCALE = _G.GetLocale()
if LOCALE == "esMX" or LOCALE == "esES" then
L_TOAST = "Información emergente"
L_TOAST_DESC = "Muestra mensajes de información en una ventana emergente"
elseif LOCALE == "frFR" then
L_TOAST_DESC = "Montrer les messages dans une fenêtre \"toast\"."
elseif LOCALE == "deDE" then
L_TOAST_DESC = "Zeigt Nachrichten in einem Toast-Fenster"
elseif LOCALE == "itIT" then
-- Nothing yet.
elseif LOCALE == "koKR" then
-- Nothing yet.
elseif LOCALE == "ptBR" then
L_TOAST = "Brinde"
L_TOAST_DESC = "Mostrar mensagems em uma janela externa"
elseif LOCALE == "ruRU" then
L_TOAST = "Всплывающее"
L_TOAST_DESC = "Показывать сообщения во всплывающем окне"
elseif LOCALE == "zhCN" then
L_TOAST = "弹出窗口"
L_TOAST_DESC = "在弹出窗口显示信息。"
elseif LOCALE == "zhTW" then
L_TOAST = "彈出視窗"
L_TOAST_DESC = "在彈出視窗顯示訊息。"
end
-----------------------------------------------------------------------
-- Variables.
-----------------------------------------------------------------------
local QueuedAddOnName
-----------------------------------------------------------------------
-- Settings functions.
-----------------------------------------------------------------------
local function ToastSpawnPoint()
return _G.Toaster and _G.Toaster:SpawnPoint() or DEFAULT_OS_SPAWN_POINT
end
local function ToastOffsetX()
return (_G.Toaster and _G.Toaster.SpawnOffsetX) and _G.Toaster:SpawnOffsetX() or nil
end
local function ToastOffsetY()
return (_G.Toaster and _G.Toaster.SpawnOffsetY) and _G.Toaster:SpawnOffsetY() or nil
end
local function ToastTitleColors(urgencyLevel)
if _G.Toaster then
return _G.Toaster:TitleColors(urgencyLevel)
else
return DEFAULT_TITLE_COLORS.r, DEFAULT_TITLE_COLORS.g, DEFAULT_TITLE_COLORS.b
end
end
local function ToastTextColors(urgencyLevel)
if _G.Toaster then
return _G.Toaster:TextColors(urgencyLevel)
else
return DEFAULT_TEXT_COLORS.r, DEFAULT_TEXT_COLORS.g, DEFAULT_TEXT_COLORS.b
end
end
local function ToastBackgroundColors(urgencyLevel)
if _G.Toaster then
return _G.Toaster:BackgroundColors(urgencyLevel)
else
return DEFAULT_BACKGROUND_COLORS.r, DEFAULT_BACKGROUND_COLORS.g, DEFAULT_BACKGROUND_COLORS.b
end
end
local function ToastDuration(addonName)
return _G.Toaster and _G.Toaster:Duration(addonName) or DEFAULT_FADE_HOLD_TIME
end
local function ToastOpacity(addonName)
return _G.Toaster and _G.Toaster:Opacity(addonName) or 0.75
end
local function ToastHasFloatingIcon(addonName)
return _G.Toaster and _G.Toaster:FloatingIcon(addonName)
end
local function ToastsAreSuppressed(addonName)
return _G.Toaster and (_G.Toaster:HideToasts() or _G.Toaster:HideToastsFromSource(addonName))
end
local function ToastsAreMuted(addonName)
return _G.Toaster and (_G.Toaster:MuteToasts() or _G.Toaster:MuteToastsFromSource(addonName))
end
-----------------------------------------------------------------------
-- Helper functions.
-----------------------------------------------------------------------
local function AnimationHideParent(animation)
animation:GetParent():Hide()
end
local function GetEffectiveSpawnPoint(frame)
local x, y = frame:GetCenter()
if not x or not y then
return DEFAULT_OS_SPAWN_POINT
end
local horizontalName = (x > _G.UIParent:GetWidth() * 2 / 3) and "RIGHT" or (x < _G.UIParent:GetWidth() / 3) and "LEFT" or ""
local verticalName = (y > _G.UIParent:GetHeight() / 2) and "TOP" or "BOTTOM"
return verticalName .. horizontalName
end
local function GetCallingObject()
return CallingObject
end
local function StringValue(input)
local inputType = _G.type(input)
if inputType == "function" then
local output = input()
if _G.type(output) ~= "string" or output == "" then
return
end
return output
elseif inputType == "string" then
return input
end
end
if not LibToast.templates[LibToast.sink_template] then
LibToast.templates[LibToast.sink_template] = function(toast, text, _, _, _, _, _, _, _, _, iconTexture)
local callingObject = GetCallingObject()
toast:SetTitle(StringValue(LibToast.sink_titles[callingObject]))
toast:SetText(text)
toast:SetIconTexture(iconTexture or StringValue(LibToast.sink_icons[callingObject]))
end
end
local function _positionToastIcon(toast)
toast.icon:ClearAllPoints()
if ToastHasFloatingIcon(toast.addonName) then
local lowercasedPointName = POINT_TRANSLATION[GetEffectiveSpawnPoint(toast)]:lower()
if lowercasedPointName:find("right") then
toast.icon:SetPoint("TOPRIGHT", toast, "TOPLEFT", -5, -10)
elseif lowercasedPointName:find("left") then
toast.icon:SetPoint("TOPLEFT", toast, "TOPRIGHT", 5, -10)
end
else
toast.icon:SetPoint("TOPLEFT", toast, "TOPLEFT", 10, -10)
end
end
local function _reclaimButton(button)
button:Hide()
button:ClearAllPoints()
button:SetParent(nil)
button:SetText(nil)
table.insert(ButtonHeap, button)
end
local function _reclaimToast(toast)
for buttonName in pairs(TOAST_BUTTONS) do
local button = toast[buttonName]
if button then
toast[buttonName] = nil
_reclaimButton(button)
end
end
toast.is_persistent = nil
toast.templateName = nil
toast.payload = nil
toast.sound_file = nil
toast:Hide()
table.insert(ToastHeap, toast)
local removalIndex
for index = 1, #ActiveToasts do
if ActiveToasts[index] == toast then
removalIndex = index
break
end
end
if removalIndex then
table.remove(ActiveToasts, removalIndex):ClearAllPoints()
end
local spawnPoint = ToastSpawnPoint()
local offsetX = ToastOffsetX() or OFFSET_X[spawnPoint]
local offsetY = ToastOffsetY() or OFFSET_Y[spawnPoint]
for index = 1, #ActiveToasts do
local indexedToast = ActiveToasts[index]
indexedToast:ClearAllPoints()
_positionToastIcon(indexedToast)
if index == 1 then
indexedToast:SetPoint(spawnPoint, _G.UIParent, spawnPoint, offsetX, offsetY)
else
spawnPoint = POINT_TRANSLATION[GetEffectiveSpawnPoint(ActiveToasts[1])]
indexedToast:SetPoint(spawnPoint, ActiveToasts[index - 1], SIBLING_ANCHORS[spawnPoint], 0, SIBLING_OFFSET_Y[spawnPoint])
end
end
local toastData = table.remove(QueuedToasts, 1)
if toastData and toastData.addonName and _G.type(toastData.template) == "string" and toastData.template ~= "" then
QueuedAddOnName = toastData.addonName
LibToast:Spawn(toastData.template, _G.unpack(toastData.payload))
end
end
local function AnimationDismissToast(animation)
_reclaimToast(animation.toast)
end
local function Focus_OnEnter(frame, motion)
local toast = frame.toast
toast.dismiss_button:Show()
if not toast.is_persistent then
toast.waitAndAnimateOut:Stop()
toast.waitAndAnimateOut.animateOut:SetStartDelay(1)
end
end
local function Focus_OnLeave(frame, motion)
local toast = frame.toast
if not toast.dismiss_button:IsMouseOver() then
toast.dismiss_button:Hide()
end
if not toast.is_persistent then
toast.waitAndAnimateOut:Play()
end
end
local function _dismissToast(frame, button, down)
_reclaimToast(frame:GetParent())
end
local function _acquireToast(addonName)
local toast = table.remove(ToastHeap)
if not toast then
toast = _G.CreateFrame("Button", nil, _G.UIParent, _G.BackdropTemplateMixin and "BackdropTemplate")
toast:SetFrameStrata("DIALOG")
toast:Hide()
local icon = toast:CreateTexture(nil, "BORDER")
icon:SetSize(DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE)
toast.icon = icon
local title = toast:CreateFontString(nil, "BORDER", "FriendsFont_Normal")
title:SetJustifyH("LEFT")
title:SetJustifyV("MIDDLE")
title:SetWordWrap(true)
title:SetPoint("TOPLEFT", toast, "TOPLEFT", 44, -10)
title:SetPoint("RIGHT", toast, "RIGHT", -20, 10)
toast.title = title
local focus = _G.CreateFrame("Frame", nil, toast)
focus:SetAllPoints(toast)
focus:SetScript("OnEnter", Focus_OnEnter)
focus:SetScript("OnLeave", Focus_OnLeave)
focus:SetScript("OnShow", Focus_OnLeave)
focus.toast = toast
local dismissButton = _G.CreateFrame("Button", nil, toast)
dismissButton:SetSize(18, 18)
dismissButton:SetPoint("TOPRIGHT", toast, "TOPRIGHT", -4, -4)
dismissButton:SetFrameStrata("DIALOG")
dismissButton:SetFrameLevel(toast:GetFrameLevel() + 2)
dismissButton:SetNormalTexture([[Interface\FriendsFrame\UI-Toast-CloseButton-Up]])
dismissButton:SetPushedTexture([[Interface\FriendsFrame\UI-Toast-CloseButton-Down]])
dismissButton:SetHighlightTexture([[Interface\FriendsFrame\UI-Toast-CloseButton-Highlight]])
dismissButton:Hide()
dismissButton:SetScript("OnClick", _dismissToast)
toast.dismiss_button = dismissButton
local text = toast:CreateFontString(nil, "BORDER", "FriendsFont_Normal")
text:SetJustifyH("LEFT")
text:SetJustifyV("MIDDLE")
text:SetWordWrap(true)
text:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
toast.text = text
local toastAnimateIn = toast:CreateAnimationGroup()
toast.animateIn = toastAnimateIn
local toastAnimateInFirst = toastAnimateIn:CreateAnimation("Alpha")
toastAnimateInFirst:SetOrder(1)
toastAnimateInFirst:SetFromAlpha(1)
toastAnimateInFirst:SetToAlpha(0)
toastAnimateInFirst:SetDuration(0)
local toastAnimateInSecond = toastAnimateIn:CreateAnimation("Alpha")
toastAnimateInSecond:SetOrder(2)
toastAnimateInSecond:SetFromAlpha(0)
toastAnimateInSecond:SetToAlpha(1)
toastAnimateInSecond:SetDuration(0.2)
local toastWaitAndAnimateOut = toast:CreateAnimationGroup()
toast.waitAndAnimateOut = toastWaitAndAnimateOut
local toastAnimateOut = toastWaitAndAnimateOut:CreateAnimation("Alpha")
toastAnimateOut:SetStartDelay(DEFAULT_FADE_HOLD_TIME)
toastAnimateOut:SetFromAlpha(1)
toastAnimateOut:SetToAlpha(0)
toastAnimateOut:SetDuration(DEFAULT_FADE_OUT_TIME)
toastAnimateOut:SetScript("OnFinished", AnimationDismissToast)
toastAnimateOut.toast = toast
toastWaitAndAnimateOut.animateOut = toastAnimateOut
local glowFrame = _G.CreateFrame("Frame", nil, toast)
glowFrame:SetAllPoints(toast)
toast.glowFrame = glowFrame
local glowTexture = glowFrame:CreateTexture(nil, "OVERLAY")
glowTexture:SetSize(DEFAULT_GLOW_WIDTH, DEFAULT_GLOW_HEIGHT)
glowTexture:SetPoint("TOPLEFT", -1, 3)
glowTexture:SetPoint("BOTTOMRIGHT", 1, -3)
glowTexture:SetTexture([[Interface\FriendsFrame\UI-Toast-Flair]])
glowTexture:SetBlendMode("ADD")
glowTexture:Hide()
glowFrame.glow = glowTexture
local glowAnimateIn = glowTexture:CreateAnimationGroup()
glowAnimateIn:SetScript("OnFinished", AnimationHideParent)
glowTexture.animateIn = glowAnimateIn
local glowAnimateInFirst = glowAnimateIn:CreateAnimation("Alpha")
glowAnimateInFirst:SetOrder(1)
glowAnimateInFirst:SetFromAlpha(0)
glowAnimateInFirst:SetToAlpha(1)
glowAnimateInFirst:SetDuration(0.2)
local glowAnimateInSecond = glowAnimateIn:CreateAnimation("Alpha")
glowAnimateInSecond:SetOrder(2)
glowAnimateInSecond:SetFromAlpha(1)
glowAnimateInSecond:SetToAlpha(0)
glowAnimateInSecond:SetDuration(0.5)
end
toast:SetSize(DEFAULT_TOAST_WIDTH, DEFAULT_TOAST_HEIGHT)
toast:SetBackdrop(DEFAULT_TOAST_BACKDROP)
toast:SetBackdropBorderColor(1, 1, 1)
if _G.Toaster then
local iconSize = _G.Toaster:IconSize(addonName)
toast.icon:SetSize(iconSize, iconSize)
end
return toast
end
-----------------------------------------------------------------------
-- Library methods.
-----------------------------------------------------------------------
function LibToast:Register(templateName, constructor, isUnique)
local isLib = (self == LibToast)
if _G.type(templateName) ~= "string" or templateName == "" then
error(METHOD_USAGE_FORMAT:format(isLib and "Register" or "RegisterToast", "templateName must be a non-empty string"), 2)
end
if _G.type(constructor) ~= "function" then
error(METHOD_USAGE_FORMAT:format(isLib and "Register" or "RegisterToast", "constructor must be a function"), 2)
end
LibToast.templates[templateName] = constructor
LibToast.unique_templates[templateName] = isUnique or nil
end
function LibToast:Spawn(templateName, ...)
local isLib = (self == LibToast)
if not templateName or (not IsInternalCall and (_G.type(templateName) ~= "string" or templateName == "")) then
error(METHOD_USAGE_FORMAT:format(isLib and "Spawn" or "SpawnToast", "templateName must be a non-empty string"), 2)
end
if not LibToast.templates[templateName] then
error(METHOD_USAGE_FORMAT:format(isLib and "Spawn" or "SpawnToast", ("\"%s\" does not match a registered template"):format(templateName)), 2)
end
local addonName
if QueuedAddOnName then
addonName = QueuedAddOnName
QueuedAddOnName = nil
elseif isLib then
addonName = _G.select(3, ([[\]]):split(_G.debugstack(2)))
else
addonName = LibToast.addon_names[self] or _G.UNKNOWN
end
if ToastsAreSuppressed(addonName) then
return
end
if LibToast.unique_templates[templateName] then
for index = 1, #ActiveToasts do
if ActiveToasts[index].templateName == templateName then
return
end
end
end
if #ActiveToasts >= MAX_ACTIVE_TOASTS then
table.insert(QueuedToasts, {
addonName = addonName,
template = templateName,
payload = { ... }
})
return
end
CurrentToast = _acquireToast(addonName)
CurrentToast.templateName = templateName
CurrentToast.addonName = addonName
-----------------------------------------------------------------------
-- Reset defaults.
-----------------------------------------------------------------------
CurrentToast.title:SetText(nil)
CurrentToast.text:SetText(nil)
CurrentToast.icon:SetTexture(nil)
CurrentToast.icon:SetTexCoord(0, 1, 0, 1)
-----------------------------------------------------------------------
-- Run constructor.
-----------------------------------------------------------------------
CallingObject = self
LibToast.templates[templateName](ToastProxy, ...)
if not CurrentToast.title:GetText() and not CurrentToast.text:GetText() and not CurrentToast.icon:GetTexture() then
_reclaimToast(CurrentToast)
return
end
-----------------------------------------------------------------------
-- Finalize layout.
-----------------------------------------------------------------------
local urgency = CurrentToast.urgency_level
CurrentToast.title:SetTextColor(ToastTitleColors(urgency))
CurrentToast.text:SetTextColor(ToastTextColors(urgency))
local opacity = ToastOpacity(addonName)
local r, g, b = ToastBackgroundColors(urgency)
CurrentToast:SetBackdropColor(r, g, b, opacity)
r, g, b = CurrentToast:GetBackdropBorderColor()
CurrentToast:SetBackdropBorderColor(r, g, b, opacity)
if ToastHasFloatingIcon(addonName) or not CurrentToast.icon:GetTexture() then
CurrentToast.title:SetPoint("TOPLEFT", CurrentToast, "TOPLEFT", 10, -10)
else
CurrentToast.title:SetPoint("TOPLEFT", CurrentToast, "TOPLEFT", CurrentToast.icon:GetWidth() + 15, -10)
end
if CurrentToast.title:GetText() then
CurrentToast.title:SetWidth(CurrentToast:GetWidth() - CurrentToast.icon:GetWidth() - 20)
CurrentToast.title:Show()
else
CurrentToast.title:Hide()
end
if CurrentToast.text:GetText() then
CurrentToast.text:SetWidth(CurrentToast:GetWidth() - CurrentToast.icon:GetWidth() - 20)
CurrentToast.text:Show()
else
CurrentToast.text:Hide()
end
local buttonHeight = (CurrentToast.primary_button or CurrentToast.secondary_button or CurrentToast.tertiary_button) and TOAST_BUTTON_HEIGHT or 0
CurrentToast:SetHeight(CurrentToast.text:GetStringHeight() + CurrentToast.title:GetStringHeight() + buttonHeight + 25)
-----------------------------------------------------------------------
-- Anchor and spawn.
-----------------------------------------------------------------------
local spawnPoint = ToastSpawnPoint()
local offsetX = ToastOffsetX() or OFFSET_X[spawnPoint]
local offsetY = ToastOffsetY() or OFFSET_Y[spawnPoint]
if #ActiveToasts > 0 then
spawnPoint = POINT_TRANSLATION[GetEffectiveSpawnPoint(ActiveToasts[1])]
CurrentToast:SetPoint(spawnPoint, ActiveToasts[#ActiveToasts], SIBLING_ANCHORS[spawnPoint], 0, SIBLING_OFFSET_Y[spawnPoint])
else
CurrentToast:SetPoint(spawnPoint, _G.UIParent, spawnPoint, offsetX, offsetY)
end
ActiveToasts[#ActiveToasts + 1] = CurrentToast
_positionToastIcon(CurrentToast)
if CurrentToast.sound_file and not ToastsAreMuted(addonName) then
_G.PlaySoundFile(CurrentToast.sound_file)
end
CurrentToast:Show()
CurrentToast.animateIn:Play()
CurrentToast.glowFrame.glow:Show()
CurrentToast.glowFrame.glow.animateIn:Play()
CurrentToast.waitAndAnimateOut:Stop() -- Stop prior fade attempt.
if not CurrentToast.is_persistent then
if CurrentToast:IsMouseOver() then
CurrentToast.waitAndAnimateOut.animateOut:SetStartDelay(1)
else
CurrentToast.waitAndAnimateOut.animateOut:SetStartDelay(ToastDuration(addonName))
CurrentToast.waitAndAnimateOut:Play()
end
end
end
function LibToast:DefineSink(displayName, texturePath)
local isLib = (self == LibToast)
local texturePathType = _G.type(texturePath)
local displayNameType = _G.type(displayName)
if texturePath and (texturePathType ~= "function" and (texturePathType ~= "string" or texturePath == "")) then
error(METHOD_USAGE_FORMAT:format(isLib and "DefineSink" or "DefineSinkToast", "texturePath must be a non-empty string, a function that returns one, or nil"), 2)
end
if displayName and (displayNameType ~= "function" and (displayNameType ~= "string" or displayName == "")) then
error(METHOD_USAGE_FORMAT:format(isLib and "DefineSink" or "DefineSinkToast", "displayName must be a non-empty string, a function that returns one, or nil"), 2)
end
local addonName = _G.select(3, ([[\]]):split(_G.debugstack(2)))
LibToast.addon_names[self] = addonName or _G.UNKNOWN
LibToast.sink_icons[self] = texturePath
LibToast.sink_titles[self] = displayName
if not LibToast.registered_sink then
local LibSink = LibStub("LibSink-2.0")
if not LibSink then
return
end
LibSink:RegisterSink("LibToast-1.0", L_TOAST, L_TOAST_DESC, function(caller, text, ...)
IsInternalCall = true
local func
if caller.SpawnToast then
func = caller.SpawnToast
else
caller = LibToast
func = LibToast.Spawn
end
func(caller, LibToast.sink_template, text, ...)
IsInternalCall = nil
end)
LibToast.registered_sink = true
end
end
-----------------------------------------------------------------------
-- Proxy methods.
-----------------------------------------------------------------------
local TOAST_URGENCIES = {
very_low = true,
moderate = true,
normal = true,
high = true,
emergency = true,
}
function ToastProxy:SetUrgencyLevel(urgencyLevel)
urgencyLevel = urgencyLevel:gsub(" ", "_"):lower()
if not TOAST_URGENCIES[urgencyLevel] then
error(("\"%s\" is not a valid toast urgency level"):format(urgencyLevel), 2)
end
CurrentToast.urgency_level = urgencyLevel
end
function ToastProxy:UrgencyLevel()
return CurrentToast.urgency_level
end
function ToastProxy:SetTitle(title)
CurrentToast.title:SetText(title)
end
function ToastProxy:SetFormattedTitle(title, ...)
CurrentToast.title:SetFormattedText(title, ...)
end
function ToastProxy:SetText(text)
CurrentToast.text:SetText(text)
end
function ToastProxy:SetFormattedText(text, ...)
CurrentToast.text:SetFormattedText(text, ...)
end
function ToastProxy:SetIconAtlas(...)
CurrentToast.icon:SetAtlas(...)
end
function ToastProxy:SetIconTexture(texture)
CurrentToast.icon:SetTexture(texture)
end
function ToastProxy:SetIconTexCoord(...)
CurrentToast.icon:SetTexCoord(...)
end
local _initializedToastButton
do
local BUTTON_NAME_FORMAT = "LibToast_Button%d"
local button_count = 0
local function _buttonCallbackHandler(button, mouseButtonName, isPress)
button.handler(button.id, mouseButtonName, isPress, button.toast.payload)
_reclaimToast(button.toast)
end
local function _acquireToastButton(toast)
local button = table.remove(ButtonHeap)
if not button then
button_count = button_count + 1
button = _G.CreateFrame("Button", BUTTON_NAME_FORMAT:format(button_count), toast, "UIMenuButtonStretchTemplate")
button:SetHeight(TOAST_BUTTON_HEIGHT)
button:SetFrameStrata("DIALOG")
button:SetScript("OnClick", _buttonCallbackHandler)
local fontString = button:GetFontString()
fontString:SetJustifyH("CENTER")
fontString:SetJustifyV("CENTER")
end
button:SetParent(toast)
button:SetFrameLevel(toast:GetFrameLevel() + 2)
return button
end
function _initializedToastButton(buttonID, label, handler)
if not label or not handler then
error("label and handler are required", 3)
return
end
local button = CurrentToast[buttonID]
if not button then
button = _acquireToastButton(CurrentToast)
CurrentToast[buttonID] = button
end
button.id = buttonID:gsub("_button", "")
button.handler = handler
button.toast = CurrentToast
button:Show()
button:SetText(label)
button:SetWidth(button:GetFontString():GetStringWidth() + 15)
return button
end
end -- do-block
function ToastProxy:SetPrimaryCallback(label, handler)
local button = _initializedToastButton("primary_button", label, handler)
button:SetPoint("BOTTOMLEFT", CurrentToast, "BOTTOMLEFT", 3, 4)
button:SetPoint("BOTTOMRIGHT", CurrentToast, "BOTTOMRIGHT", -3, 4)
CurrentToast:SetHeight(CurrentToast:GetHeight() + button:GetHeight() + 5)
if button:GetWidth() > CurrentToast:GetWidth() then
CurrentToast:SetWidth(button:GetWidth() + 5)
end
end
function ToastProxy:SetSecondaryCallback(label, handler)
if not CurrentToast.primary_button then
error("primary button must be defined first", 2)
end
CurrentToast.primary_button:ClearAllPoints()
CurrentToast.primary_button:SetPoint("BOTTOMLEFT", CurrentToast, "BOTTOMLEFT", 3, 4)
local button = _initializedToastButton("secondary_button", label, handler)
button:SetPoint("BOTTOMRIGHT", CurrentToast, "BOTTOMRIGHT", -3, 4)
if button:GetWidth() + CurrentToast.primary_button:GetWidth() > CurrentToast:GetWidth() then
CurrentToast:SetWidth(button:GetWidth() + CurrentToast.primary_button:GetWidth() + 5)
end
end
function ToastProxy:SetTertiaryCallback(label, handler)
if not CurrentToast.primary_button or not CurrentToast.secondary_button then
error("primary and secondary buttons must be defined first", 2)
end
CurrentToast.secondary_button:ClearAllPoints()
CurrentToast.secondary_button:SetPoint("LEFT", CurrentToast.primary_button, "RIGHT", 0, 0)
local button = _initializedToastButton("tertiary_button", label, handler)
button:SetPoint("LEFT", CurrentToast.secondary_button, "RIGHT", 0, 0)
if button:GetWidth() + CurrentToast.primary_button:GetWidth() + CurrentToast.secondary_button:GetWidth() > CurrentToast:GetWidth() then
CurrentToast:SetWidth(button:GetWidth() + CurrentToast.primary_button:GetWidth() + CurrentToast.secondary_button:GetWidth() + 5)
end
end
function ToastProxy:SetPayload(...)
CurrentToast.payload = { ... }
end
function ToastProxy:Payload()
return _G.unpack(CurrentToast.payload)
end
function ToastProxy:MakePersistent()
CurrentToast.is_persistent = true
end
function ToastProxy:SetSoundFile(filePath)
CurrentToast.sound_file = filePath
end
-----------------------------------------------------------------------
-- Embed handling.
-----------------------------------------------------------------------
LibToast.embeds = LibToast.embeds or {}
local mixins = {
"DefineSink",
"Register",
"Spawn",
}
function LibToast:Embed(target)
LibToast.embeds[target] = true
for index = 1, #mixins do
local method = mixins[index]
target[method .. "Toast"] = LibToast[method]
end
return target
end
for addon in pairs(LibToast.embeds) do
LibToast:Embed(addon)
end