summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/dms/Services/ToastService.qml
blob: 7684f90b406f09e6c9282cf9da678337bca47d33 (plain)
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
pragma Singleton

pragma ComponentBehavior: Bound

import QtQuick
import Quickshell

Singleton {
    id: root

    readonly property int levelInfo: 0
    readonly property int levelWarn: 1
    readonly property int levelError: 2
    property string currentMessage: ""
    property int currentLevel: levelInfo
    property bool toastVisible: false
    property var toastQueue: []
    property string currentDetails: ""
    property string currentCommand: ""
    property bool hasDetails: false
    property string wallpaperErrorStatus: ""
    property int maxQueueSize: 3
    property var lastErrorTime: ({})
    property int errorThrottleMs: 1000
    property string currentCategory: ""

    function showToast(message, level = levelInfo, details = "", command = "", category = "") {
        const now = Date.now()
        const messageKey = message + level

        if (level === levelError) {
            const lastTime = lastErrorTime[messageKey] || 0
            if (now - lastTime < errorThrottleMs) {
                return
            }
            lastErrorTime[messageKey] = now
        }

        if (category && level === levelError) {
            if (currentCategory === category && toastVisible && currentLevel === levelError) {
                currentMessage = message
                currentDetails = details || ""
                currentCommand = command || ""
                hasDetails = currentDetails.length > 0 || currentCommand.length > 0
                resetToastState()
                if (hasDetails) {
                    toastTimer.interval = 8000
                } else {
                    toastTimer.interval = 5000
                }
                toastTimer.restart()
                return
            }

            toastQueue = toastQueue.filter(t => t.category !== category)
        }

        const isDuplicate = toastQueue.some(toast =>
            toast.message === message && toast.level === level
        )
        if (isDuplicate) {
            return
        }

        if (toastQueue.length >= maxQueueSize) {
            if (level === levelError) {
                toastQueue = toastQueue.filter(t => t.level !== levelError).slice(0, maxQueueSize - 1)
            } else {
                return
            }
        }

        toastQueue.push({
                            "message": message,
                            "level": level,
                            "details": details,
                            "command": command,
                            "category": category
                        })
        if (!toastVisible) {
            processQueue()
        }
    }

    function showInfo(message, details = "", command = "", category = "") {
        showToast(message, levelInfo, details, command, category)
    }

    function showWarning(message, details = "", command = "", category = "") {
        showToast(message, levelWarn, details, command, category)
    }

    function showError(message, details = "", command = "", category = "") {
        showToast(message, levelError, details, command, category)
    }

    function dismissCategory(category) {
        if (!category) {
            return
        }

        if (currentCategory === category && toastVisible) {
            hideToast()
            return
        }

        toastQueue = toastQueue.filter(t => t.category !== category)
    }

    function hideToast() {
        toastVisible = false
        currentMessage = ""
        currentDetails = ""
        currentCommand = ""
        currentCategory = ""
        hasDetails = false
        currentLevel = levelInfo
        toastTimer.stop()
        resetToastState()
        if (toastQueue.length > 0) {
            processQueue()
        }
    }

    function processQueue() {
        if (toastQueue.length === 0) {
            return
        }

        const toast = toastQueue.shift()
        currentMessage = toast.message
        currentLevel = toast.level
        currentDetails = toast.details || ""
        currentCommand = toast.command || ""
        currentCategory = toast.category || ""
        hasDetails = currentDetails.length > 0 || currentCommand.length > 0
        toastVisible = true
        resetToastState()

        if (toast.level === levelError && hasDetails) {
            toastTimer.interval = 8000
            toastTimer.start()
        } else {
            toastTimer.interval = toast.level === levelError ? 5000 : toast.level === levelWarn ? 3000 : 1500
            toastTimer.start()
        }
    }

    signal resetToastState

    function stopTimer() {
        toastTimer.stop()
    }

    function restartTimer() {
        if (hasDetails && currentLevel === levelError) {
            toastTimer.interval = 8000
            toastTimer.restart()
        }
    }

    function clearWallpaperError() {
        wallpaperErrorStatus = ""
    }

    Timer {
        id: toastTimer

        interval: 5000
        running: false
        repeat: false
        onTriggered: hideToast()
    }
}