Quickshell hands expireTimeout through in ms, so multiplying by 1000 kept a notify-send -t 3000 popup up for about 50 minutes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
283 lines
9.7 KiB
QML
283 lines
9.7 KiB
QML
pragma Singleton
|
|
pragma ComponentBehavior: Bound
|
|
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
import Quickshell.Services.Notifications
|
|
|
|
// The shell's freedesktop notification server. It owns history (the server's
|
|
// tracked notifications), the popup queue and Do Not Disturb. Only one process
|
|
// can own org.freedesktop.Notifications: while another daemon (e.g. SwayNC)
|
|
// holds the name, `active` stays false and the UI says so.
|
|
Singleton {
|
|
id: root
|
|
readonly property var options: ConfigService.ready ? ConfigService.data.notifications : ({
|
|
enabled: false,
|
|
doNotDisturb: false,
|
|
position: "top-right",
|
|
animation: "slide",
|
|
timeout: 5000,
|
|
maxPopups: 3,
|
|
criticalInDnd: true
|
|
})
|
|
readonly property bool enabled: ConfigService.ready && options.enabled
|
|
readonly property bool dnd: enabled && options.doNotDisturb
|
|
readonly property var server: loader.item
|
|
// Who owns the notification bus name: "shell", "swaync", a process name, or "" (unknown/none).
|
|
// A live Control Center/Settings status poll is fresher than the startup check.
|
|
property string checkedOwner: ""
|
|
readonly property string owner: SystemService.state.dnd && SystemService.state.dnd.provider !== undefined ? SystemService.state.dnd.provider : checkedOwner
|
|
readonly property bool active: enabled && owner === "shell"
|
|
readonly property string notice: !enabled ? "Built-in notifications are turned off" : owner === "shell" || owner === "" ? "" : owner + " is handling notifications. Stop it to use glassMxrph notifications."
|
|
|
|
// Newest first.
|
|
readonly property var list: server ? server.trackedNotifications.values.slice().reverse() : []
|
|
readonly property int count: list.length
|
|
// Notifications currently shown as popups, newest first.
|
|
property var popups: []
|
|
// id -> arrival time (ms); reassigned so bindings update.
|
|
property var times: ({})
|
|
property real now: Date.now()
|
|
// Ids whose change signals are connected (not bound; mutated in place).
|
|
property var connected: ({})
|
|
|
|
// Per-app stacks for the Control Center, newest group first.
|
|
readonly property var groups: {
|
|
const order = [], byKey = {};
|
|
for (const n of list) {
|
|
const key = groupKey(n);
|
|
if (!byKey[key]) {
|
|
byKey[key] = {
|
|
key: key,
|
|
name: appLabel(n),
|
|
items: []
|
|
};
|
|
order.push(byKey[key]);
|
|
}
|
|
byKey[key].items.push(n);
|
|
}
|
|
return order;
|
|
}
|
|
|
|
function groupKey(n) {
|
|
return (n.desktopEntry || n.appName || "notifications").toLowerCase();
|
|
}
|
|
function entryFor(n) {
|
|
return (n.desktopEntry ? ApplicationsService.byId(n.desktopEntry) : null) || (n.appName ? ApplicationsService.byId(n.appName) : null);
|
|
}
|
|
function appLabel(n) {
|
|
const entry = n.appName ? null : entryFor(n);
|
|
return n.appName || (entry ? entry.name : "") || "Notification";
|
|
}
|
|
// App icon: the notification's own icon, else its desktop entry's.
|
|
function iconFor(n) {
|
|
if (!n)
|
|
return "";
|
|
const icon = n.appIcon || "";
|
|
if (icon.startsWith("/") || icon.startsWith("file:") || icon.startsWith("image:"))
|
|
return icon.startsWith("/") ? "file://" + icon : icon;
|
|
return (icon ? Quickshell.iconPath(icon, true) : "") || ApplicationsService.icon(entryFor(n));
|
|
}
|
|
function imageFor(n) {
|
|
const image = n ? n.image || "" : "";
|
|
return image.startsWith("/") ? "file://" + image : image;
|
|
}
|
|
function critical(n) {
|
|
return !!n && n.urgency === NotificationUrgency.Critical;
|
|
}
|
|
// Popup lifetime in ms; 0 keeps it until dismissed. Quickshell passes the
|
|
// sender's expire_timeout through in milliseconds (notify-send -t 3000 is 3000).
|
|
function timeoutFor(n) {
|
|
if (critical(n))
|
|
return 0;
|
|
return n.expireTimeout > 0 ? Math.round(Math.max(1000, n.expireTimeout)) : options.timeout;
|
|
}
|
|
function since(n) {
|
|
const at = n ? times[n.id] : undefined;
|
|
if (at === undefined)
|
|
return "";
|
|
const minutes = Math.floor((now - at) / 60000);
|
|
if (minutes < 1)
|
|
return "now";
|
|
if (minutes < 60)
|
|
return minutes + "m ago";
|
|
const hours = Math.floor(minutes / 60);
|
|
if (hours < 24)
|
|
return hours + "h ago";
|
|
return Qt.formatDate(new Date(at), "d MMM");
|
|
}
|
|
// The default action is the notification body; the rest become buttons.
|
|
function defaultAction(n) {
|
|
return n ? n.actions.find(a => a.identifier === "default") || null : null;
|
|
}
|
|
function buttons(n) {
|
|
return n ? n.actions.filter(a => a.identifier !== "default" && a.text) : [];
|
|
}
|
|
|
|
function setDnd(on) {
|
|
return ConfigService.setValue("notifications.doNotDisturb", !!on);
|
|
}
|
|
// Leave the popup stack; the notification stays in the Control Center.
|
|
function hidePopup(n) {
|
|
if (!popups.includes(n))
|
|
return;
|
|
popups = popups.filter(p => p !== n);
|
|
if (n.transient)
|
|
n.expire();
|
|
}
|
|
// Clicking a notification runs its default action; it always leaves view.
|
|
function activate(n) {
|
|
const action = defaultAction(n);
|
|
if (action)
|
|
action.invoke();
|
|
if (!action || !n.resident)
|
|
n.dismiss();
|
|
}
|
|
function invoke(n, action) {
|
|
action.invoke();
|
|
if (!n.resident)
|
|
n.dismiss();
|
|
}
|
|
function dismiss(n) {
|
|
n.dismiss();
|
|
}
|
|
function dismissGroup(key) {
|
|
for (const n of list.filter(n => groupKey(n) === key))
|
|
n.dismiss();
|
|
}
|
|
function clearAll() {
|
|
for (const n of list)
|
|
n.dismiss();
|
|
popups = [];
|
|
}
|
|
function clearPopups() {
|
|
for (const n of popups.slice())
|
|
hidePopup(n);
|
|
}
|
|
|
|
function forget(n) {
|
|
delete connected[n.id];
|
|
popups = popups.filter(p => p !== n);
|
|
const next = Object.assign({}, times);
|
|
delete next[n.id];
|
|
times = next;
|
|
}
|
|
function show(n) {
|
|
// Do Not Disturb keeps everything in history; critical alerts may break through.
|
|
if (dnd && !(critical(n) && options.criticalInDnd)) {
|
|
if (n.transient)
|
|
n.expire();
|
|
return;
|
|
}
|
|
const rest = popups.filter(p => p !== n);
|
|
// Overflow leaves the oldest popups in history rather than queueing them.
|
|
popups = [n].concat(rest).slice(0, options.maxPopups);
|
|
for (const p of rest.slice(options.maxPopups - 1))
|
|
if (p.transient)
|
|
p.expire();
|
|
popupShown(n);
|
|
}
|
|
// Restarts the popup timer of a replaced notification.
|
|
signal popupShown(var notification)
|
|
|
|
function receive(n) {
|
|
n.tracked = true;
|
|
const next = Object.assign({}, times);
|
|
next[n.id] = Date.now();
|
|
times = next;
|
|
now = Date.now();
|
|
if (!connected[n.id]) {
|
|
connected[n.id] = true;
|
|
n.closed.connect(() => root.forget(n));
|
|
// In-place replacements (progress, volume scripts) pop up again.
|
|
const again = () => {
|
|
if (n.tracked)
|
|
root.show(n);
|
|
};
|
|
n.summaryChanged.connect(again);
|
|
n.bodyChanged.connect(again);
|
|
}
|
|
show(n);
|
|
}
|
|
|
|
LazyLoader {
|
|
id: loader
|
|
active: root.enabled
|
|
component: NotificationServer {
|
|
keepOnReload: true
|
|
persistenceSupported: true
|
|
bodySupported: true
|
|
bodyMarkupSupported: true
|
|
bodyHyperlinksSupported: true
|
|
actionsSupported: true
|
|
imageSupported: true
|
|
onNotification: n => root.receive(n)
|
|
}
|
|
}
|
|
onEnabledChanged: if (!enabled)
|
|
popups = []
|
|
// Popups follow DND turning on.
|
|
onDndChanged: if (dnd)
|
|
popups = popups.filter(n => critical(n) && options.criticalInDnd)
|
|
|
|
Timer {
|
|
interval: 30000
|
|
repeat: true
|
|
running: root.count > 0
|
|
onTriggered: root.now = Date.now()
|
|
}
|
|
|
|
// One-shot ownership check after the server registers; Control Center and
|
|
// Settings polls refresh it through SystemService.
|
|
ProviderJob {
|
|
id: check
|
|
helper: "helpers/system.py"
|
|
onFinished: (request, result) => {
|
|
const dnd = result.ok && result.state ? result.state.dnd : null;
|
|
root.checkedOwner = dnd && dnd.provider ? dnd.provider : "";
|
|
}
|
|
}
|
|
Timer {
|
|
running: root.enabled && ConfigService.ready
|
|
interval: 1500
|
|
onTriggered: check.start({
|
|
op: "status",
|
|
features: ["dnd"],
|
|
options: ConfigService.data.system
|
|
})
|
|
}
|
|
|
|
IpcHandler {
|
|
target: "notifications"
|
|
function toggleDnd(): bool {
|
|
root.setDnd(!root.dnd);
|
|
return !root.dnd;
|
|
}
|
|
function setDnd(on: bool): bool {
|
|
return root.setDnd(on);
|
|
}
|
|
// Clears the whole history.
|
|
function clear(): void {
|
|
root.clearAll();
|
|
}
|
|
// Hides the visible popups; they stay in the Control Center.
|
|
function dismiss(): void {
|
|
root.clearPopups();
|
|
}
|
|
function status(): string {
|
|
return JSON.stringify({
|
|
enabled: root.enabled,
|
|
active: root.active,
|
|
owner: root.owner,
|
|
dnd: root.dnd,
|
|
count: root.count,
|
|
popups: root.popups.length,
|
|
groups: root.groups.map(g => ({
|
|
app: g.name,
|
|
count: g.items.length
|
|
}))
|
|
});
|
|
}
|
|
}
|
|
}
|