1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2025-12-05 21:15:38 -05:00

settings: optimize mem usage

- keep un-loaded unless called upon
This commit is contained in:
bbedward
2025-11-29 18:32:45 -05:00
parent e6c3ae9397
commit b11b375848
8 changed files with 400 additions and 335 deletions

View File

@@ -1,11 +1,9 @@
pragma Singleton
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import Quickshell.Services.Notifications
import Quickshell.Widgets
import qs.Common
import "../Common/markdown2html.js" as Markdown2Html
@@ -28,108 +26,106 @@ Singleton {
property int maxIngressPerSecond: 20
property double _lastIngressSec: 0
property int _ingressCountThisSec: 0
property int maxStoredNotifications: 300
property int maxStoredNotifications: 50
property var _dismissQueue: []
property int _dismissBatchSize: 8
property int _dismissTickMs: 8
property bool _suspendGrouping: false
property var _groupCache: ({
"notifications": [],
"popups": []
})
"notifications": [],
"popups": []
})
property bool _groupsDirty: false
Component.onCompleted: {
_recomputeGroups()
_recomputeGroups();
}
function _nowSec() {
return Date.now() / 1000.0
return Date.now() / 1000.0;
}
function _ingressAllowed(notif) {
const t = _nowSec()
const t = _nowSec();
if (t - _lastIngressSec >= 1.0) {
_lastIngressSec = t
_ingressCountThisSec = 0
_lastIngressSec = t;
_ingressCountThisSec = 0;
}
_ingressCountThisSec += 1
_ingressCountThisSec += 1;
if (notif.urgency === NotificationUrgency.Critical) {
return true
return true;
}
return _ingressCountThisSec <= maxIngressPerSecond
return _ingressCountThisSec <= maxIngressPerSecond;
}
function _enqueuePopup(wrapper) {
if (notificationQueue.length >= maxQueueSize) {
const gk = getGroupKey(wrapper)
let idx = notificationQueue.findIndex(w => w && getGroupKey(w) === gk && w.urgency !== NotificationUrgency.Critical)
const gk = getGroupKey(wrapper);
let idx = notificationQueue.findIndex(w => w && getGroupKey(w) === gk && w.urgency !== NotificationUrgency.Critical);
if (idx === -1) {
idx = notificationQueue.findIndex(w => w && w.urgency !== NotificationUrgency.Critical)
idx = notificationQueue.findIndex(w => w && w.urgency !== NotificationUrgency.Critical);
}
if (idx === -1) {
idx = 0
idx = 0;
}
const victim = notificationQueue[idx]
const victim = notificationQueue[idx];
if (victim) {
victim.popup = false
victim.popup = false;
}
notificationQueue.splice(idx, 1)
notificationQueue.splice(idx, 1);
}
notificationQueue = [...notificationQueue, wrapper]
notificationQueue = [...notificationQueue, wrapper];
}
function _initWrapperPersistence(wrapper) {
const timeoutMs = wrapper.timer ? wrapper.timer.interval : 5000
const isCritical = wrapper.notification && wrapper.notification.urgency === NotificationUrgency.Critical
wrapper.isPersistent = isCritical || (timeoutMs === 0)
const timeoutMs = wrapper.timer ? wrapper.timer.interval : 5000;
const isCritical = wrapper.notification && wrapper.notification.urgency === NotificationUrgency.Critical;
wrapper.isPersistent = isCritical || (timeoutMs === 0);
}
function _trimStored() {
if (notifications.length > maxStoredNotifications) {
const overflow = notifications.length - maxStoredNotifications
const toDrop = []
const overflow = notifications.length - maxStoredNotifications;
const toDrop = [];
for (var i = notifications.length - 1; i >= 0 && toDrop.length < overflow; --i) {
const w = notifications[i]
const w = notifications[i];
if (w && w.notification && w.urgency !== NotificationUrgency.Critical) {
toDrop.push(w)
toDrop.push(w);
}
}
for (var i = notifications.length - 1; i >= 0 && toDrop.length < overflow; --i) {
const w = notifications[i]
const w = notifications[i];
if (w && w.notification && toDrop.indexOf(w) === -1) {
toDrop.push(w)
toDrop.push(w);
}
}
for (const w of toDrop) {
try {
w.notification.dismiss()
} catch (e) {
}
w.notification.dismiss();
} catch (e) {}
}
}
}
function onOverlayOpen() {
popupsDisabled = true
addGate.stop()
addGateBusy = false
popupsDisabled = true;
addGate.stop();
addGateBusy = false;
notificationQueue = []
notificationQueue = [];
for (const w of visibleNotifications) {
if (w) {
w.popup = false
w.popup = false;
}
}
visibleNotifications = []
_recomputeGroupsLater()
visibleNotifications = [];
_recomputeGroupsLater();
}
function onOverlayClose() {
popupsDisabled = false
processQueue()
popupsDisabled = false;
processQueue();
}
Timer {
@@ -138,8 +134,8 @@ Singleton {
running: false
repeat: false
onTriggered: {
addGateBusy = false
processQueue()
addGateBusy = false;
processQueue();
}
}
@@ -150,7 +146,7 @@ Singleton {
running: root.allWrappers.length > 0 || visibleNotifications.length > 0
triggeredOnStart: false
onTriggered: {
root.timeUpdateTick = !root.timeUpdateTick
root.timeUpdateTick = !root.timeUpdateTick;
}
}
@@ -160,23 +156,21 @@ Singleton {
repeat: true
running: false
onTriggered: {
let n = Math.min(_dismissBatchSize, _dismissQueue.length)
let n = Math.min(_dismissBatchSize, _dismissQueue.length);
for (var i = 0; i < n; ++i) {
const w = _dismissQueue.pop()
const w = _dismissQueue.pop();
try {
if (w && w.notification) {
w.notification.dismiss()
w.notification.dismiss();
}
} catch (e) {
}
} catch (e) {}
}
if (_dismissQueue.length === 0) {
dismissPump.stop()
_suspendGrouping = false
bulkDismissing = false
popupsDisabled = false
_recomputeGroupsLater()
dismissPump.stop();
_suspendGrouping = false;
bulkDismissing = false;
popupsDisabled = false;
_recomputeGroupsLater();
}
}
}
@@ -212,52 +206,50 @@ Singleton {
persistenceSupported: true
onNotification: notif => {
notif.tracked = true
notif.tracked = true;
if (!_ingressAllowed(notif)) {
if (notif.urgency !== NotificationUrgency.Critical) {
try {
notif.dismiss()
} catch (e) {
}
return
notif.dismiss();
} catch (e) {}
return;
}
}
if (SettingsData.soundsEnabled && SettingsData.soundNewNotification) {
if (notif.urgency === NotificationUrgency.Critical) {
AudioService.playCriticalNotificationSound()
AudioService.playCriticalNotificationSound();
} else {
AudioService.playNormalNotificationSound()
AudioService.playNormalNotificationSound();
}
}
const shouldShowPopup = !root.popupsDisabled && !SessionData.doNotDisturb
const isTransient = notif.transient
const shouldShowPopup = !root.popupsDisabled && !SessionData.doNotDisturb;
const isTransient = notif.transient;
const wrapper = notifComponent.createObject(root, {
"popup": shouldShowPopup,
"notification": notif
})
"popup": shouldShowPopup,
"notification": notif
});
if (wrapper) {
root.allWrappers.push(wrapper)
root.allWrappers.push(wrapper);
if (!isTransient) {
root.notifications.push(wrapper)
_trimStored()
root.notifications.push(wrapper);
_trimStored();
}
Qt.callLater(() => {
_initWrapperPersistence(wrapper)
})
_initWrapperPersistence(wrapper);
});
if (shouldShowPopup) {
_enqueuePopup(wrapper)
processQueue()
_enqueuePopup(wrapper);
processQueue();
}
}
_recomputeGroupsLater()
_recomputeGroupsLater();
}
}
@@ -271,80 +263,80 @@ Singleton {
onPopupChanged: {
if (!popup) {
removeFromVisibleNotifications(wrapper)
removeFromVisibleNotifications(wrapper);
}
}
readonly property Timer timer: Timer {
interval: {
if (!wrapper.notification) {
return 5000
return 5000;
}
switch (wrapper.notification.urgency) {
case NotificationUrgency.Low:
return SettingsData.notificationTimeoutLow
case NotificationUrgency.Critical:
return SettingsData.notificationTimeoutCritical
default:
return SettingsData.notificationTimeoutNormal
case NotificationUrgency.Low:
return SettingsData.notificationTimeoutLow;
case NotificationUrgency.Critical:
return SettingsData.notificationTimeoutCritical;
default:
return SettingsData.notificationTimeoutNormal;
}
}
repeat: false
running: false
onTriggered: {
if (interval > 0) {
wrapper.popup = false
wrapper.popup = false;
}
}
}
readonly property date time: new Date()
readonly property string timeStr: {
root.timeUpdateTick
root.clockFormatChanged
root.timeUpdateTick;
root.clockFormatChanged;
const now = new Date()
const diff = now.getTime() - time.getTime()
const minutes = Math.floor(diff / 60000)
const hours = Math.floor(minutes / 60)
const now = new Date();
const diff = now.getTime() - time.getTime();
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(minutes / 60);
if (hours < 1) {
if (minutes < 1) {
return "now"
return "now";
}
return `${minutes}m ago`
return `${minutes}m ago`;
}
const nowDate = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const timeDate = new Date(time.getFullYear(), time.getMonth(), time.getDate())
const daysDiff = Math.floor((nowDate - timeDate) / (1000 * 60 * 60 * 24))
const nowDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const timeDate = new Date(time.getFullYear(), time.getMonth(), time.getDate());
const daysDiff = Math.floor((nowDate - timeDate) / (1000 * 60 * 60 * 24));
if (daysDiff === 0) {
return formatTime(time)
return formatTime(time);
}
if (daysDiff === 1) {
return `yesterday, ${formatTime(time)}`
return `yesterday, ${formatTime(time)}`;
}
return `${daysDiff} days ago`
return `${daysDiff} days ago`;
}
function formatTime(date) {
let use24Hour = true
let use24Hour = true;
try {
if (typeof SettingsData !== "undefined" && SettingsData.use24HourClock !== undefined) {
use24Hour = SettingsData.use24HourClock
use24Hour = SettingsData.use24HourClock;
}
} catch (e) {
use24Hour = true
use24Hour = true;
}
if (use24Hour) {
return date.toLocaleTimeString(Qt.locale(), "HH:mm")
return date.toLocaleTimeString(Qt.locale(), "HH:mm");
} else {
return date.toLocaleTimeString(Qt.locale(), "h:mm AP")
return date.toLocaleTimeString(Qt.locale(), "h:mm AP");
}
}
@@ -353,27 +345,27 @@ Singleton {
readonly property string body: notification.body
readonly property string htmlBody: {
if (body && (body.includes('<') && body.includes('>'))) {
return body
return body;
}
return Markdown2Html.markdownToHtml(body)
return Markdown2Html.markdownToHtml(body);
}
readonly property string appIcon: notification.appIcon
readonly property string appName: {
if (notification.appName == "") {
const entry = DesktopEntries.heuristicLookup(notification.desktopEntry)
const entry = DesktopEntries.heuristicLookup(notification.desktopEntry);
if (entry && entry.name) {
return entry.name.toLowerCase()
return entry.name.toLowerCase();
}
}
return notification.appName || "app"
return notification.appName || "app";
}
readonly property string desktopEntry: notification.desktopEntry
readonly property string image: notification.image
readonly property string cleanImage: {
if (!image) {
return ""
return "";
}
return Paths.strip(image)
return Paths.strip(image);
}
readonly property int urgency: notification.urgency
readonly property list<NotificationAction> actions: notification.actions
@@ -382,26 +374,26 @@ Singleton {
target: wrapper.notification.Retainable
function onDropped(): void {
root.allWrappers = root.allWrappers.filter(w => w !== wrapper)
root.notifications = root.notifications.filter(w => w !== wrapper)
root.allWrappers = root.allWrappers.filter(w => w !== wrapper);
root.notifications = root.notifications.filter(w => w !== wrapper);
if (root.bulkDismissing) {
return
return;
}
const groupKey = getGroupKey(wrapper)
const remainingInGroup = root.notifications.filter(n => getGroupKey(n) === groupKey)
const groupKey = getGroupKey(wrapper);
const remainingInGroup = root.notifications.filter(n => getGroupKey(n) === groupKey);
if (remainingInGroup.length <= 1) {
clearGroupExpansionState(groupKey)
clearGroupExpansionState(groupKey);
}
cleanupExpansionStates()
root._recomputeGroupsLater()
cleanupExpansionStates();
root._recomputeGroupsLater();
}
function onAboutToDestroy(): void {
wrapper.destroy()
wrapper.destroy();
}
}
}
@@ -414,146 +406,145 @@ Singleton {
function clearAllPopups() {
for (const w of visibleNotifications) {
if (w) {
w.popup = false
w.popup = false;
}
}
visibleNotifications = []
notificationQueue = []
visibleNotifications = [];
notificationQueue = [];
}
function clearAllNotifications() {
bulkDismissing = true
popupsDisabled = true
addGate.stop()
addGateBusy = false
notificationQueue = []
bulkDismissing = true;
popupsDisabled = true;
addGate.stop();
addGateBusy = false;
notificationQueue = [];
for (const w of allWrappers) {
if (w) {
w.popup = false
w.popup = false;
}
}
visibleNotifications = []
visibleNotifications = [];
_dismissQueue = notifications.slice()
_dismissQueue = notifications.slice();
if (notifications.length) {
notifications = []
notifications = [];
}
expandedGroups = {}
expandedMessages = {}
expandedGroups = {};
expandedMessages = {};
_suspendGrouping = true
_suspendGrouping = true;
if (!dismissPump.running && _dismissQueue.length) {
dismissPump.start()
dismissPump.start();
}
}
function dismissNotification(wrapper) {
if (!wrapper || !wrapper.notification) {
return
return;
}
wrapper.popup = false
wrapper.notification.dismiss()
wrapper.popup = false;
wrapper.notification.dismiss();
}
function disablePopups(disable) {
popupsDisabled = disable
popupsDisabled = disable;
if (disable) {
notificationQueue = []
notificationQueue = [];
for (const notif of visibleNotifications) {
notif.popup = false
notif.popup = false;
}
visibleNotifications = []
visibleNotifications = [];
}
}
function processQueue() {
if (addGateBusy) {
return
return;
}
if (popupsDisabled) {
return
return;
}
if (SessionData.doNotDisturb) {
return
return;
}
if (notificationQueue.length === 0) {
return
return;
}
const activePopupCount = visibleNotifications.filter(n => n && n.popup).length
const activePopupCount = visibleNotifications.filter(n => n && n.popup).length;
if (activePopupCount >= 4) {
return
return;
}
const next = notificationQueue.shift()
const next = notificationQueue.shift();
next.seq = ++seqCounter
visibleNotifications = [...visibleNotifications, next]
next.popup = true
next.seq = ++seqCounter;
visibleNotifications = [...visibleNotifications, next];
next.popup = true;
if (next.timer.interval > 0) {
next.timer.start()
next.timer.start();
}
addGateBusy = true
addGate.restart()
addGateBusy = true;
addGate.restart();
}
function removeFromVisibleNotifications(wrapper) {
visibleNotifications = visibleNotifications.filter(n => n !== wrapper)
processQueue()
visibleNotifications = visibleNotifications.filter(n => n !== wrapper);
processQueue();
}
function releaseWrapper(w) {
visibleNotifications = visibleNotifications.filter(n => n !== w)
notificationQueue = notificationQueue.filter(n => n !== w)
visibleNotifications = visibleNotifications.filter(n => n !== w);
notificationQueue = notificationQueue.filter(n => n !== w);
if (w && w.destroy && !w.isPersistent && notifications.indexOf(w) === -1) {
Qt.callLater(() => {
try {
w.destroy()
} catch (e) {
}
})
try {
w.destroy();
} catch (e) {}
});
}
}
function getGroupKey(wrapper) {
if (wrapper.desktopEntry && wrapper.desktopEntry !== "") {
return wrapper.desktopEntry.toLowerCase()
return wrapper.desktopEntry.toLowerCase();
}
return wrapper.appName.toLowerCase()
return wrapper.appName.toLowerCase();
}
function _recomputeGroups() {
if (_suspendGrouping) {
_groupsDirty = true
return
_groupsDirty = true;
return;
}
_groupCache = {
"notifications": _calcGroupedNotifications(),
"popups": _calcGroupedPopups()
}
_groupsDirty = false
};
_groupsDirty = false;
}
function _recomputeGroupsLater() {
_groupsDirty = true
_groupsDirty = true;
if (!groupsDebounce.running) {
groupsDebounce.start()
groupsDebounce.start();
}
}
function _calcGroupedNotifications() {
const groups = {}
const groups = {};
for (const notif of notifications) {
if (!notif) continue
const groupKey = getGroupKey(notif)
if (!notif)
continue;
const groupKey = getGroupKey(notif);
if (!groups[groupKey]) {
groups[groupKey] = {
"key": groupKey,
@@ -562,34 +553,35 @@ Singleton {
"latestNotification": null,
"count": 0,
"hasInlineReply": false
}
};
}
groups[groupKey].notifications.unshift(notif)
groups[groupKey].latestNotification = groups[groupKey].notifications[0]
groups[groupKey].count = groups[groupKey].notifications.length
groups[groupKey].notifications.unshift(notif);
groups[groupKey].latestNotification = groups[groupKey].notifications[0];
groups[groupKey].count = groups[groupKey].notifications.length;
if (notif.notification.hasInlineReply) {
groups[groupKey].hasInlineReply = true
groups[groupKey].hasInlineReply = true;
}
}
return Object.values(groups).sort((a, b) => {
const aUrgency = a.latestNotification.urgency || NotificationUrgency.Low
const bUrgency = b.latestNotification.urgency || NotificationUrgency.Low
if (aUrgency !== bUrgency) {
return bUrgency - aUrgency
}
return b.latestNotification.time.getTime() - a.latestNotification.time.getTime()
})
const aUrgency = a.latestNotification.urgency || NotificationUrgency.Low;
const bUrgency = b.latestNotification.urgency || NotificationUrgency.Low;
if (aUrgency !== bUrgency) {
return bUrgency - aUrgency;
}
return b.latestNotification.time.getTime() - a.latestNotification.time.getTime();
});
}
function _calcGroupedPopups() {
const groups = {}
const groups = {};
for (const notif of popups) {
if (!notif) continue
const groupKey = getGroupKey(notif)
if (!notif)
continue;
const groupKey = getGroupKey(notif);
if (!groups[groupKey]) {
groups[groupKey] = {
"key": groupKey,
@@ -598,92 +590,92 @@ Singleton {
"latestNotification": null,
"count": 0,
"hasInlineReply": false
}
};
}
groups[groupKey].notifications.unshift(notif)
groups[groupKey].latestNotification = groups[groupKey].notifications[0]
groups[groupKey].count = groups[groupKey].notifications.length
groups[groupKey].notifications.unshift(notif);
groups[groupKey].latestNotification = groups[groupKey].notifications[0];
groups[groupKey].count = groups[groupKey].notifications.length;
if (notif.notification.hasInlineReply) {
groups[groupKey].hasInlineReply = true
groups[groupKey].hasInlineReply = true;
}
}
return Object.values(groups).sort((a, b) => {
return b.latestNotification.time.getTime() - a.latestNotification.time.getTime()
})
return b.latestNotification.time.getTime() - a.latestNotification.time.getTime();
});
}
function toggleGroupExpansion(groupKey) {
let newExpandedGroups = {}
let newExpandedGroups = {};
for (const key in expandedGroups) {
newExpandedGroups[key] = expandedGroups[key]
newExpandedGroups[key] = expandedGroups[key];
}
newExpandedGroups[groupKey] = !newExpandedGroups[groupKey]
expandedGroups = newExpandedGroups
newExpandedGroups[groupKey] = !newExpandedGroups[groupKey];
expandedGroups = newExpandedGroups;
}
function dismissGroup(groupKey) {
const group = groupedNotifications.find(g => g.key === groupKey)
const group = groupedNotifications.find(g => g.key === groupKey);
if (group) {
for (const notif of group.notifications) {
if (notif && notif.notification) {
notif.notification.dismiss()
notif.notification.dismiss();
}
}
} else {
for (const notif of allWrappers) {
if (notif && notif.notification && getGroupKey(notif) === groupKey) {
notif.notification.dismiss()
notif.notification.dismiss();
}
}
}
}
function clearGroupExpansionState(groupKey) {
let newExpandedGroups = {}
let newExpandedGroups = {};
for (const key in expandedGroups) {
if (key !== groupKey && expandedGroups[key]) {
newExpandedGroups[key] = true
newExpandedGroups[key] = true;
}
}
expandedGroups = newExpandedGroups
expandedGroups = newExpandedGroups;
}
function cleanupExpansionStates() {
const currentGroupKeys = new Set(groupedNotifications.map(g => g.key))
const currentMessageIds = new Set()
const currentGroupKeys = new Set(groupedNotifications.map(g => g.key));
const currentMessageIds = new Set();
for (const group of groupedNotifications) {
for (const notif of group.notifications) {
if (notif && notif.notification) {
currentMessageIds.add(notif.notification.id)
currentMessageIds.add(notif.notification.id);
}
}
}
let newExpandedGroups = {}
let newExpandedGroups = {};
for (const key in expandedGroups) {
if (currentGroupKeys.has(key) && expandedGroups[key]) {
newExpandedGroups[key] = true
newExpandedGroups[key] = true;
}
}
expandedGroups = newExpandedGroups
let newExpandedMessages = {}
expandedGroups = newExpandedGroups;
let newExpandedMessages = {};
for (const messageId in expandedMessages) {
if (currentMessageIds.has(messageId) && expandedMessages[messageId]) {
newExpandedMessages[messageId] = true
newExpandedMessages[messageId] = true;
}
}
expandedMessages = newExpandedMessages
expandedMessages = newExpandedMessages;
}
function toggleMessageExpansion(messageId) {
let newExpandedMessages = {}
let newExpandedMessages = {};
for (const key in expandedMessages) {
newExpandedMessages[key] = expandedMessages[key]
newExpandedMessages[key] = expandedMessages[key];
}
newExpandedMessages[messageId] = !newExpandedMessages[messageId]
expandedMessages = newExpandedMessages
newExpandedMessages[messageId] = !newExpandedMessages[messageId];
expandedMessages = newExpandedMessages;
}
Connections {
@@ -692,13 +684,13 @@ Singleton {
if (SessionData.doNotDisturb) {
// Hide all current popups when DND is enabled
for (const notif of visibleNotifications) {
notif.popup = false
notif.popup = false;
}
visibleNotifications = []
notificationQueue = []
visibleNotifications = [];
notificationQueue = [];
} else {
// Re-enable popup processing when DND is disabled
processQueue()
processQueue();
}
}
}
@@ -706,7 +698,7 @@ Singleton {
Connections {
target: typeof SettingsData !== "undefined" ? SettingsData : null
function onUse24HourClockChanged() {
root.clockFormatChanged = !root.clockFormatChanged
root.clockFormatChanged = !root.clockFormatChanged;
}
}
}

View File

@@ -16,6 +16,7 @@ Singleton {
property var systemUpdatePopout: null
property var settingsModal: null
property var settingsModalLoader: null
property var clipboardHistoryModal: null
property var spotlightModal: null
property var powerMenuModal: null
@@ -191,14 +192,50 @@ Singleton {
}
}
property bool _settingsWantsOpen: false
property bool _settingsWantsToggle: false
function openSettings() {
settingsModal?.show();
if (settingsModal) {
settingsModal.show();
} else if (settingsModalLoader) {
_settingsWantsOpen = true;
_settingsWantsToggle = false;
settingsModalLoader.activeAsync = true;
}
}
function closeSettings() {
settingsModal?.close();
}
function toggleSettings() {
if (settingsModal) {
settingsModal.toggle();
} else if (settingsModalLoader) {
_settingsWantsToggle = true;
_settingsWantsOpen = false;
settingsModalLoader.activeAsync = true;
}
}
function unloadSettings() {
if (settingsModalLoader) {
settingsModal = null;
settingsModalLoader.active = false;
}
}
function _onSettingsModalLoaded() {
if (_settingsWantsOpen) {
_settingsWantsOpen = false;
settingsModal?.show();
} else if (_settingsWantsToggle) {
_settingsWantsToggle = false;
settingsModal?.toggle();
}
}
function openClipboardHistory() {
clipboardHistoryModal?.show();
}