Skip to content

Commit fe2b5b0

Browse files
authored
feat: toast notification module (#9476)
1 parent 263d2ed commit fe2b5b0

3 files changed

Lines changed: 291 additions & 78 deletions

File tree

src/Classes/TreeTab.lua

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ local s_gsub = string.gsub
1919
local s_byte = string.byte
2020
local dkjson = require "dkjson"
2121

22-
-- Helper function to find toast index by content pattern
23-
-- TODO: remove this when when we can control toast notifications better
24-
local function findToastIndex(pattern)
25-
for i, msg in ipairs(main.toastMessages) do
26-
if msg:match(pattern) then
27-
return i
28-
end
29-
end
30-
return nil
31-
end
32-
3322
local TreeTabClass = newClass("TreeTab", "ControlHost", function(self, build)
3423
self.ControlHost()
3524

@@ -290,7 +279,7 @@ local TreeTabClass = newClass("TreeTab", "ControlHost", function(self, build)
290279
end)
291280
self.controls.powerReportList.shown = false
292281
-- Progress callback from the CalcsTab power builder coroutine
293-
self.powerBuilderToastActive = false
282+
self.powerBuilderToastId = nil
294283
self.lastProgressToastUpdate = 0
295284
self.build.powerBuilderProgressCallback = function(percent)
296285
local now = GetTime()
@@ -301,33 +290,29 @@ local TreeTabClass = newClass("TreeTab", "ControlHost", function(self, build)
301290
local message = percent and string.format("Building Power Report... (%d%%)", percent) or "Building Power Report..."
302291

303292
self.controls.powerReportList.label = message
293+
294+
if self.powerBuilderToastId and ToastNotification:WasDismissed(self.powerBuilderToastId) then
295+
return
296+
end
304297
self.lastProgressToastUpdate = now
305-
local toastIndex = findToastIndex("^Building Power Report")
306-
if toastIndex then
307-
main.toastMessages[toastIndex] = message
298+
299+
if self.powerBuilderToastId and ToastNotification:Exists(self.powerBuilderToastId) then
300+
ToastNotification:Update(self.powerBuilderToastId, message)
308301
else
309-
t_insert(main.toastMessages, message)
310-
self.powerBuilderToastActive = true
302+
self.powerBuilderToastId = ToastNotification:Add(message)
311303
end
312304
end
313305
-- Completion callback from the CalcsTab power builder coroutine
314306
self.build.powerBuilderCallback = function()
315307
local powerStat = self.build.calcsTab.powerStat or data.powerStatList[1]
316308
local report = self:BuildPowerReportList(powerStat)
317309
self.controls.powerReportList:SetReport(powerStat, report)
318-
local toastIndex = findToastIndex("^Building Power Report")
319-
if self.powerBuilderToastActive and toastIndex then
320-
-- Remove the toast from the queue instead of triggering hide animation
321-
-- This prevents issues when the toast is not currently displayed (queued behind another toast)
322-
-- TODO: look into allowing toast notifications to stack and have UUID's we can control them better
323-
if toastIndex == 1 then
324-
main.toastMode = "HIDING"
325-
main.toastStart = GetTime()
326-
else
327-
t_remove(main.toastMessages, toastIndex)
328-
end
310+
311+
if self.powerBuilderToastId then
312+
ToastNotification:ClearDismissed(self.powerBuilderToastId)
313+
ToastNotification:Remove(self.powerBuilderToastId)
314+
self.powerBuilderToastId = nil
329315
end
330-
self.powerBuilderToastActive = false
331316
end
332317

333318
self.controls.specConvertText = new("LabelControl", { "BOTTOMLEFT", self.controls.specSelect, "TOPLEFT" }, { 0, -14, 0, 16 }, "^7This is an older tree version, which may not be fully compatible with the current game version.")
@@ -1029,6 +1014,12 @@ function TreeTabClass:SetPowerCalc(powerStat)
10291014
self.build.calcsTab.powerBuildFlag = true
10301015
self.build.calcsTab.powerStat = powerStat
10311016
self.controls.powerReportList:SetReport(powerStat, nil)
1017+
-- Remove old toast and clear dismissed state so toast can show for new power report
1018+
if self.powerBuilderToastId then
1019+
ToastNotification:ClearDismissed(self.powerBuilderToastId)
1020+
ToastNotification:Remove(self.powerBuilderToastId, true)
1021+
self.powerBuilderToastId = nil
1022+
end
10321023
end
10331024

10341025
function TreeTabClass:BuildPowerReportList(currentStat)

src/Modules/Main.lua

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ LoadModule("Modules/CalcTools")
2323
LoadModule("Modules/PantheonTools")
2424
LoadModule("Modules/BuildSiteTools")
2525

26+
-- Load as global so other modules can access the same instance
27+
ToastNotification = LoadModule("Modules/ToastNotification")
28+
2629
--[[if launch.devMode then
2730
for skillName, skill in pairs(data.enchantments.Helmet) do
2831
for _, mod in ipairs(skill.ENDGAME) do
@@ -234,19 +237,14 @@ function main:Init()
234237
self.controls.devMode.shown = function()
235238
return launch.devMode
236239
end
237-
self.controls.dismissToast = new("ButtonControl", {"BOTTOMLEFT",self.anchorMain,"BOTTOMLEFT"}, {0, function() return -self.mainBarHeight + self.toastHeight end, 80, 20}, "Dismiss", function()
238-
self.toastMode = "HIDING"
239-
self.toastStart = GetTime()
240-
end)
241-
self.controls.dismissToast.shown = function()
242-
return self.toastMode == "SHOWN"
243-
end
244240

245241
self.mainBarHeight = 58
246-
self.toastMessages = { }
242+
243+
-- Initialize toast notification system
244+
ToastNotification:Init(self.anchorMain)
247245

248246
if launch.devMode and GetTime() >= 0 and GetTime() < 15000 then
249-
t_insert(self.toastMessages, [[
247+
ToastNotification:Add([[
250248
^xFF7700Warning: ^7Developer Mode active!
251249
The program is currently running in developer
252250
mode, which is not intended for normal use.
@@ -372,56 +370,23 @@ function main:OnFrame()
372370
self:CallMode("OnFrame", self.inputEvents, self.viewPort)
373371

374372
if launch.updateErrMsg then
375-
t_insert(self.toastMessages, string.format("Update check failed!\n%s", launch.updateErrMsg))
373+
ToastNotification:Add(string.format("Update check failed!\n%s", launch.updateErrMsg))
376374
launch.updateErrMsg = nil
377375
end
378376
if launch.updateAvailable then
379377
if launch.updateAvailable == "none" then
380-
t_insert(self.toastMessages, "No update available\nYou are running the latest version.")
378+
ToastNotification:Add("No update available\nYou are running the latest version.")
381379
launch.updateAvailable = nil
382380
elseif not self.updateAvailableShown then
383-
t_insert(self.toastMessages, "Update Available\nAn update has been downloaded and is ready\nto be applied.")
381+
ToastNotification:Add("Update Available\nAn update has been downloaded and is ready\nto be applied.")
384382
self.updateAvailableShown = true
385383
end
386384
end
387385

388-
-- Run toasts
389-
if self.toastMessages[1] then
390-
if not self.toastMode then
391-
self.toastMode = "SHOWING"
392-
self.toastStart = GetTime()
393-
self.toastHeight = #self.toastMessages[1]:gsub("[^\n]","") * 16 + 20 + 40
394-
end
395-
if self.toastMode == "SHOWING" then
396-
local now = GetTime()
397-
if now >= self.toastStart + 250 then
398-
self.toastMode = "SHOWN"
399-
else
400-
self.mainBarHeight = 58 + self.toastHeight * (now - self.toastStart) / 250
401-
end
402-
end
403-
if self.toastMode == "SHOWN" then
404-
self.mainBarHeight = 58 + self.toastHeight
405-
elseif self.toastMode == "HIDING" then
406-
local now = GetTime()
407-
if now >= self.toastStart + 75 then
408-
self.toastMode = nil
409-
self.mainBarHeight = 58
410-
t_remove(self.toastMessages, 1)
411-
else
412-
self.mainBarHeight = 58 + self.toastHeight * (1 - (now - self.toastStart) / 75)
413-
end
414-
end
415-
if self.toastMode then
416-
SetDrawColor(0.85, 0.85, 0.85)
417-
DrawImage(nil, 0, self.screenH - self.mainBarHeight, 312, self.mainBarHeight)
418-
SetDrawColor(0.1, 0.1, 0.1)
419-
DrawImage(nil, 0, self.screenH - self.mainBarHeight + 4, 308, self.mainBarHeight - 4)
420-
SetDrawColor(1, 1, 1)
421-
DrawString(4, self.screenH - self.mainBarHeight + 8, "LEFT", 20, "VAR", self.toastMessages[1]:gsub("\n.*",""))
422-
DrawString(4, self.screenH - self.mainBarHeight + 28, "LEFT", 16, "VAR", self.toastMessages[1]:gsub("^[^\n]*\n?",""))
423-
end
424-
end
386+
-- Update and render toasts
387+
ToastNotification:UpdateFrame(self.inputEvents, self.viewPort, self.screenH)
388+
local totalToastHeight = ToastNotification:Render()
389+
self.mainBarHeight = 58 + totalToastHeight
425390

426391
-- Draw main controls
427392
SetDrawColor(0.85, 0.85, 0.85)

0 commit comments

Comments
 (0)