mirror of
https://github.com/noctalia-dev/noctalia-shell.git
synced 2026-05-11 17:08:27 +08:00
feat(notifications): add notification display rules
- block: skips the notification completely - mute: does not play sound (played by noctalia), shows popup, adds to history - hide: no sound, no popup, still adds to history
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
pragma Singleton
|
||||
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import qs.Commons
|
||||
|
||||
Singleton {
|
||||
id: root
|
||||
|
||||
readonly property string rulesFilePath: Settings.configDir + "notification-rules.json"
|
||||
|
||||
property var rules: []
|
||||
|
||||
property FileView rulesFileView: FileView {
|
||||
id: rulesFileView
|
||||
path: root.rulesFilePath
|
||||
watchChanges: true
|
||||
printErrors: false
|
||||
|
||||
adapter: JsonAdapter {
|
||||
id: rulesAdapter
|
||||
property var rules: []
|
||||
}
|
||||
|
||||
onLoaded: {
|
||||
try {
|
||||
const parsed = JSON.parse(rulesFileView.text());
|
||||
root.rules = Array.isArray(parsed.rules) ? parsed.rules : [];
|
||||
} catch (e) {
|
||||
root.rules = [];
|
||||
}
|
||||
}
|
||||
|
||||
onLoadFailed: function (error) {
|
||||
root.rules = [];
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
}
|
||||
|
||||
function save() {
|
||||
Quickshell.execDetached(["mkdir", "-p", Settings.configDir]);
|
||||
rulesAdapter.rules = root.rules;
|
||||
rulesFileView.writeAdapter();
|
||||
}
|
||||
|
||||
function evaluate(appName, summary, body) {
|
||||
const haystack = [appName || "", summary || "", body || ""].join(" ");
|
||||
for (let i = 0; i < root.rules.length; i++) {
|
||||
const r = root.rules[i];
|
||||
const pattern = (r.pattern || "").trim();
|
||||
if (pattern === "")
|
||||
continue;
|
||||
let matched = false;
|
||||
if (pattern.length >= 3 && pattern.startsWith("/") && pattern.endsWith("/")) {
|
||||
try {
|
||||
matched = new RegExp(pattern.slice(1, -1)).test(haystack);
|
||||
} catch (e) {
|
||||
Logger.w("NotificationRulesService", "Invalid regex:", pattern, e);
|
||||
}
|
||||
} else if (pattern.includes("*")) {
|
||||
try {
|
||||
const reStr = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
||||
matched = new RegExp(reStr, "i").test(haystack);
|
||||
} catch (e) {
|
||||
matched = haystack.toLowerCase().includes(pattern.toLowerCase());
|
||||
}
|
||||
} else {
|
||||
matched = haystack.toLowerCase().includes(pattern.toLowerCase());
|
||||
}
|
||||
if (matched) {
|
||||
const a = (r.action || "block").toLowerCase();
|
||||
if (a === "mute" || a === "hide")
|
||||
return a;
|
||||
if (a === "silence")
|
||||
return "hide";
|
||||
return "block";
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user