summaryrefslogtreecommitdiffstats
path: root/home-configuration/config/quickshell/modules/notifications/NotificationFocus.qml
blob: ce59b2abe7ed59706c5390ff27ba1c1bfc00da7b (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
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
import QtQuick
import Quickshell
import Quickshell.Io

Item {
    id: root

    property string pendingApp: ""

    function normalize(value) {
        if (!value)
            return ""

        var result = String(value).toLowerCase().trim()

        result = result.replace(/^org\./, "")
        result = result.replace(/^com\./, "")
        result = result.replace(/^net\./, "")
        result = result.replace(/^app\./, "")

        result = result.replace(/\.desktop$/, "")
        result = result.replace(/\.desktop$/i, "")

        result = result.replace(/[^a-z0-9]+/g, "")

        return result
    }

    function candidates(value) {
        var result = []
        var raw = String(value || "").toLowerCase().trim()

        function add(candidate) {
            if (!candidate)
                return

            candidate = normalize(candidate)

            if (candidate && result.indexOf(candidate) === -1)
                result.push(candidate)
        }

        add(raw)

        if (raw.indexOf(".") !== -1) {
            var parts = raw.split(".")

            add(parts[parts.length - 1])

            for (var i = 0; i < parts.length; i++)
                add(parts[i])
        }

        if (raw.indexOf("/") !== -1)
            add(raw.split("/").pop())

        if (raw.indexOf("-") !== -1) {
            var dashParts = raw.split("-")

            for (var j = 0; j < dashParts.length; j++)
                add(dashParts[j])
        }

        if (raw.indexOf("_") !== -1) {
            var underscoreParts = raw.split("_")

            for (var k = 0; k < underscoreParts.length; k++)
                add(underscoreParts[k])
        }

        return result
    }

    function matchScore(notificationApp, windowAppId, windowTitle) {
        var notificationCandidates = candidates(notificationApp)
        var windowCandidates = candidates(windowAppId)

        var best = 0

        for (var i = 0; i < notificationCandidates.length; i++) {
            var notificationCandidate = notificationCandidates[i]

            for (var j = 0; j < windowCandidates.length; j++) {
                var windowCandidate = windowCandidates[j]

                if (notificationCandidate === windowCandidate)
                    best = Math.max(best, 100)

                else if (
                    notificationCandidate.length >= 4 &&
                    windowCandidate.length >= 4 &&
                    (
                        notificationCandidate.indexOf(windowCandidate) !== -1 ||
                        windowCandidate.indexOf(notificationCandidate) !== -1
                    )
                ) {
                    best = Math.max(best, 60)
                }
            }
        }

        var titleCandidates = candidates(windowTitle)

        for (var n = 0; n < notificationCandidates.length; n++) {
            for (var t = 0; t < titleCandidates.length; t++) {
                if (
                    notificationCandidates[n].length >= 4 &&
                    notificationCandidates[n] === titleCandidates[t]
                ) {
                    best = Math.max(best, 30)
                }
            }
        }

        return best
    }

    Process {
        id: windowsProcess

        command: ["niri", "msg", "-j", "windows"]

        stdout: StdioCollector {
            id: windowsOutput

            onStreamFinished: {
                var windows

                try {
                    windows = JSON.parse(this.text)
                } catch (error) {
                    console.error(
                        "Failed to parse Niri windows:",
                        error
                    )

                    root.pendingApp = ""
                    return
                }

                var notificationApp = root.pendingApp
                root.pendingApp = ""

                if (!notificationApp) {
                    console.warn("Notification focus: empty app name")
                    return
                }

                if (!Array.isArray(windows)) {
                    console.error(
                        "Niri windows response is not an array"
                    )
                    return
                }

                var bestWindow = null
                var bestScore = 0

                for (var i = 0; i < windows.length; i++) {
                    var window = windows[i]

                    if (!window || window.id === undefined)
                        continue

                    var appId = window.app_id || ""
                    var title = window.title || ""

                    var score = matchScore(
                        notificationApp,
                        appId,
                        title
                    )

                    if (score > bestScore) {
                        bestScore = score
                        bestWindow = window
                    }
                }

                if (!bestWindow) {
                    console.log(
                        "No matching Niri window found:",
                        notificationApp
                    )
                    return
                }

                console.log(
                    "FOCUSING WINDOW:",
                    bestWindow.id,
                    "notification app:",
                    notificationApp,
                    "app_id:",
                    bestWindow.app_id,
                    "title:",
                    bestWindow.title,
                    "score:",
                    bestScore
                )

                focusProcess.exec([
                    "niri",
                    "msg",
                    "action",
                    "focus-window",
                    "--id",
                    String(bestWindow.id)
                ])
            }
        }
    }

    Process {
        id: focusProcess
    }

    function focusSource(notification) {
        if (!notification)
            return

        var app = notification.appName || ""

        if (!app) {
            console.warn(
                "Notification has no application name"
            )
            return
        }

        root.pendingApp = app

        windowsProcess.exec([
            "niri",
            "msg",
            "-j",
            "windows"
        ])
    }
}