feat: add notification application rules
This commit is contained in:
@@ -37,6 +37,31 @@ Singleton {
|
||||
// Cleared when the notification centre is opened. The bar binds to this.
|
||||
property int unreadCount: 0
|
||||
|
||||
// Kept separate from the persisted map so this version can safely run
|
||||
// before the matching schema entry lands. A later accepted write folds the
|
||||
// complete map into DesktopPreferences and clears this fallback.
|
||||
property var fallbackAppRules: ({})
|
||||
|
||||
// Display metadata is intentionally session-only. The durable shape stays
|
||||
// just the per-application rule map, while a fresh notification gives the
|
||||
// settings page a human-readable name straight away.
|
||||
property var rememberedApplications: ({})
|
||||
|
||||
readonly property var persistedAppRules: {
|
||||
const stored = DesktopPreferences.get("notificationAppRules");
|
||||
return stored && typeof stored === "object" && !Array.isArray(stored) ? stored : {};
|
||||
}
|
||||
|
||||
readonly property var appRules: Object.assign({}, root.persistedAppRules, root.fallbackAppRules)
|
||||
|
||||
readonly property var applications: {
|
||||
const remembered = root.rememberedApplications;
|
||||
return Object.keys(root.appRules).map(appId => ({
|
||||
id: appId,
|
||||
name: remembered[appId]?.name || appId
|
||||
})).sort((a, b) => a.name.localeCompare(b.name));
|
||||
}
|
||||
|
||||
// Arrival times, keyed by notification id — the protocol carries no
|
||||
// timestamp. Deliberately formatted once at arrival rather than shown as
|
||||
// "5 minutes ago", which would need a clock ticking behind every card.
|
||||
@@ -49,6 +74,70 @@ Singleton {
|
||||
|
||||
readonly property bool hasNotifications: root.history.length > 0
|
||||
|
||||
function notificationAppId(notification: Notification): string {
|
||||
const desktopEntry = String(notification.desktopEntry ?? "").trim();
|
||||
return desktopEntry || String(notification.appName ?? "").trim() || "Notifications";
|
||||
}
|
||||
|
||||
function normalizedAppRule(rule: var): var {
|
||||
const source = rule && typeof rule === "object" && !Array.isArray(rule) ? rule : {};
|
||||
return {
|
||||
enabled: source.enabled !== false,
|
||||
showOnLockScreen: source.showOnLockScreen !== false,
|
||||
showContentOnLockScreen: source.showContentOnLockScreen !== false
|
||||
};
|
||||
}
|
||||
|
||||
function appRule(appId: string): var {
|
||||
return root.normalizedAppRule(root.appRules[appId]);
|
||||
}
|
||||
|
||||
function setAppRule(appId: string, patch: var): bool {
|
||||
if (!appId)
|
||||
return false;
|
||||
|
||||
const current = root.appRule(appId);
|
||||
const next = {};
|
||||
for (const knownAppId of Object.keys(root.appRules))
|
||||
next[knownAppId] = root.appRule(knownAppId);
|
||||
next[appId] = {
|
||||
enabled: patch.enabled === undefined ? current.enabled : patch.enabled === true,
|
||||
showOnLockScreen: patch.showOnLockScreen === undefined ? current.showOnLockScreen : patch.showOnLockScreen === true,
|
||||
showContentOnLockScreen: patch.showContentOnLockScreen === undefined ? current.showContentOnLockScreen : patch.showContentOnLockScreen === true
|
||||
};
|
||||
|
||||
if (DesktopPreferences.set("notificationAppRules", next))
|
||||
root.fallbackAppRules = {};
|
||||
else
|
||||
root.fallbackAppRules = next;
|
||||
return true;
|
||||
}
|
||||
|
||||
function rememberApplication(notification: Notification): string {
|
||||
const appId = root.notificationAppId(notification);
|
||||
const next = Object.assign({}, root.rememberedApplications);
|
||||
next[appId] = {
|
||||
name: String(notification.appName ?? "").trim() || appId
|
||||
};
|
||||
root.rememberedApplications = next;
|
||||
|
||||
if (root.appRules[appId] === undefined)
|
||||
root.setAppRule(appId, {});
|
||||
return appId;
|
||||
}
|
||||
|
||||
// These policy getters deliberately accept Notification objects, so a lock
|
||||
// screen can use the same source of truth without duplicating app matching.
|
||||
function shouldShowOnLockScreen(notification: Notification): bool {
|
||||
const rule = root.appRule(root.notificationAppId(notification));
|
||||
return rule.enabled && rule.showOnLockScreen;
|
||||
}
|
||||
|
||||
function shouldShowContentOnLockScreen(notification: Notification): bool {
|
||||
const rule = root.appRule(root.notificationAppId(notification));
|
||||
return rule.enabled && rule.showOnLockScreen && rule.showContentOnLockScreen;
|
||||
}
|
||||
|
||||
// history grouped by app, in most-recent-app-first order — the shape
|
||||
// NotificationCenter.qml renders directly.
|
||||
readonly property var groups: {
|
||||
@@ -92,6 +181,10 @@ Singleton {
|
||||
if (notification.lastGeneration)
|
||||
return;
|
||||
|
||||
const appId = root.rememberApplication(notification);
|
||||
if (!root.appRule(appId).enabled)
|
||||
return;
|
||||
|
||||
// Without this the object is destroyed the instant this returns.
|
||||
notification.tracked = true;
|
||||
root.arrivals[notification.id] = new Date();
|
||||
|
||||
Reference in New Issue
Block a user