Something went wrong. Try again.
Something went wrong. Try again.
12 kB · 373 lines
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374import QtQuickimport Quickshellimport Quickshell.Ioimport Quickshell.Services.Notificationsimport "NotificationLogic.js" as Logic
Scope { id: root
property var ipc: null property string focusedScreen: "" property string centerScreen: "" property bool centerOpen: false property int historyLimit: 100 property int popupLimit: 4 readonly property var entries: JSON.parse(state.entriesJson) readonly property alias doNotDisturb: state.dnd readonly property alias alwaysOnTop: state.alwaysOnTop readonly property var history: entries.filter(entry => !entry.transient) readonly property var popups: doNotDisturb || centerOpen ? [] : entries.filter(entry => entry.popup).slice(0, popupLimit) readonly property int unreadCount: history.filter(entry => entry.unread).length readonly property string stateDirectory: (Quickshell.env("XDG_STATE_HOME") || Quickshell.env("HOME") + "/.local/state") + "/quickshell" property bool directoryReady: false property bool dndTouched: false property bool alwaysOnTopTouched: false property string persistenceError: ""
// Only plain data enters models and persistence. Live QObjects stay in this // JS map, never in ListModel roles (which can retain dangling C++ pointers). property var liveRefs: ({}) property var remaining: ({}) property var hovered: ({})
PersistentProperties { id: state reloadableId: "main-notifications" // QJSValue arrays belong to their engine and cannot cross a reload. // Persist JSON, then parse it in the current engine instead. property string entriesJson: "[]" property bool dnd: false property bool alwaysOnTop: false property string session: String(Date.now()) property int serial: 0 property bool initialized: false }
function setEntries(value) { state.entriesJson = JSON.stringify(value); }
function toggleCenter(screenName) { if (!centerOpen || (screenName && screenName !== centerScreen)) { centerScreen = screenName || focusedScreen; centerOpen = true; } else { centerOpen = false; } }
function setDnd(enabled) { dndTouched = true; state.dnd = enabled; if (enabled) dismissPopups(); saveSoon(); }
function setAlwaysOnTop(enabled) { alwaysOnTopTouched = true; state.alwaysOnTop = enabled; saveSoon(); }
function markRead() { for (const entry of entries.filter(item => item.transient)) hidePopup(entry.uid); setEntries(entries.map(entry => Object.assign({}, entry, { unread: false, popup: false }))); }
Connections { target: root.ipc function onPopupRequested(name) { if (name === "notifications") root.toggleCenter(""); } }
onCenterOpenChanged: { if (centerOpen) markRead(); } onEntriesChanged: saveSoon()
function entryFor(uid) { return entries.find(entry => entry.uid === uid); }
function patch(uid, changes) { setEntries(entries.map(entry => entry.uid === uid ? Object.assign({}, entry, changes) : entry)); }
function receive(notification) { notification.tracked = true; const previous = notification.lastGeneration ? entries.find(entry => entry.session === state.session && entry.sourceId === notification.id && entry.live) : null; const uid = previous ? previous.uid : state.session + "-" + (++state.serial); liveRefs[uid] = notification; const refresh = function () { root.refreshNotification(uid); }; for (const name of ["summaryChanged", "bodyChanged", "appNameChanged", "appIconChanged", "imageChanged", "urgencyChanged", "expireTimeoutChanged", "actionsChanged", "hasInlineReplyChanged", "inlineReplyPlaceholderChanged", "transientChanged"]) notification[name].connect(refresh); notification.closed.connect(function () { root.closed(uid); }); if (previous) { if (previous.popup && previous.timeout > 0) remaining[uid] = previous.timeout; refreshNotification(uid, true); return; } const entry = Logic.snapshot(notification, uid, state.session, Date.now()); entry.unread = !centerOpen; entry.popup = !centerOpen && !doNotDisturb; entry.screen = focusedScreen; if (entry.popup && entry.timeout > 0) remaining[uid] = entry.timeout; setEntries([entry].concat(entries)); trim(); // Transient feedback suppressed by DND must not become hidden history. if (entry.transient && !entry.popup) dismiss(uid); }
function refreshNotification(uid, restoring) { const notification = liveRefs[uid]; const previous = entryFor(uid); if (!notification || !previous) return; const entry = Logic.snapshot(notification, uid, state.session, previous.timestamp); entry.unread = restoring ? previous.unread : !centerOpen; entry.popup = !centerOpen && !doNotDisturb && (!restoring || previous.popup); entry.screen = previous.screen; if (!restoring && entry.popup && entry.timeout > 0) remaining[uid] = entry.timeout; patch(uid, entry); trim(); // Replacements can become transient or lose their popup under DND. if (entry.transient && !entry.popup) dismiss(uid); }
function closed(uid) { delete liveRefs[uid]; delete remaining[uid]; delete hovered[uid]; const entry = entryFor(uid); if (!entry) return; if (entry.transient) setEntries(entries.filter(item => item.uid !== uid)); else patch(uid, { live: false, actions: [], inlineReply: false, popup: false, image: "" }); }
function hidePopup(uid) { const entry = entryFor(uid); delete remaining[uid]; delete hovered[uid]; if (entry && entry.transient) { const notification = liveRefs[uid]; if (notification) notification.expire(); else dismiss(uid); } else { patch(uid, { popup: false }); } }
function dismiss(uid) { const notification = liveRefs[uid]; // Remove before closing: the synchronous closed signal must not revive it. setEntries(entries.filter(entry => entry.uid !== uid)); delete liveRefs[uid]; delete remaining[uid]; delete hovered[uid]; if (notification) notification.dismiss(); }
function clear() { for (const entry of entries.slice()) dismiss(entry.uid); }
function dismissPopups() { // Include all marked popups even when DND already hides the model. for (const entry of entries.filter(item => item.popup)) hidePopup(entry.uid); }
function trim() { const excess = history.slice(historyLimit); for (const entry of excess) dismiss(entry.uid); const overflow = entries.filter(entry => entry.popup).slice(popupLimit); for (const entry of overflow) hidePopup(entry.uid); }
function invoke(uid, actionId) { const notification = liveRefs[uid]; if (!notification) return false; // Actions can destroy their notification. Never touch the QObject // after invoking the action. const action = notification.actions.find(item => item.identifier === actionId); if (!action) return false; patch(uid, { popup: false, unread: false }); action.invoke(); return true; }
function reply(uid, text) { const notification = liveRefs[uid]; if (!notification || !notification.hasInlineReply || !text.trim()) return false; patch(uid, { popup: false, unread: false }); notification.sendInlineReply(text); return true; }
function saveSoon() { if (state.initialized && directoryReady) saveTimer.restart(); }
function load(raw) { if (state.initialized) return; try { const saved = Logic.parseState(raw, historyLimit); const seen = new Set(entries.map(entry => entry.uid)); setEntries(entries.concat(saved.entries.filter(entry => !seen.has(entry.uid)))); if (!dndTouched) { state.dnd = saved.dnd; if (saved.dnd) dismissPopups(); } if (!alwaysOnTopTouched) state.alwaysOnTop = saved.alwaysOnTop; trim(); } catch (error) { persistenceError = "Could not read saved notification history"; console.warn(persistenceError, error); } state.initialized = true; }
Process { command: ["mkdir", "-m", "700", "-p", root.stateDirectory] running: true onExited: code => { root.directoryReady = code === 0; if (code !== 0) root.persistenceError = "Could not create notification state directory"; } }
FileView { id: stateFile path: root.directoryReady ? root.stateDirectory + "/notifications.json" : "" atomicWrites: true printErrors: false onLoaded: root.load(text()) onLoadFailed: root.load("") onSaveFailed: { root.persistenceError = "Could not save notification history"; console.warn(root.persistenceError); } }
Timer { id: saveTimer interval: 200 onTriggered: stateFile.setText(JSON.stringify({ version: 1, dnd: state.dnd, alwaysOnTop: state.alwaysOnTop, entries: Logic.persistentEntries(root.entries, root.historyLimit) }) + "\n") }
// One timer for the entire service, and no work when there are no timed // popups. Hover pauses the countdown, including while entering a reply. Timer { interval: 100 repeat: true running: root.popups.some(entry => entry.timeout > 0) onTriggered: { for (const entry of root.popups) { if (entry.timeout <= 0 || root.hovered[entry.uid]) continue; root.remaining[entry.uid] = (root.remaining[entry.uid] || entry.timeout) - interval; if (root.remaining[entry.uid] <= 0) root.hidePopup(entry.uid); } } }
NotificationServer { keepOnReload: true actionsSupported: true imageSupported: true persistenceSupported: true inlineReplySupported: true // Render plain text deliberately; no remote HTML images or link fetching. bodyMarkupSupported: false bodyHyperlinksSupported: false onNotification: notification => root.receive(notification) }
IpcHandler { target: "notifications" function toggle(): void { root.toggleCenter(""); } function close(): void { root.centerOpen = false; } function toggleDnd(): bool { root.setDnd(!root.doNotDisturb); return root.doNotDisturb; } function setDnd(enabled: bool): void { root.setDnd(enabled); } function clear(): void { root.clear(); } function dismissAll(): void { root.dismissPopups(); } function getState(): string { return JSON.stringify({ dnd: root.doNotDisturb, open: root.centerOpen, unread: root.unreadCount, count: root.history.length, popups: root.popups.length, persistenceError: root.persistenceError }); } }}