1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-03 20:32:07 -04:00

notifications: try to prevent zombies better, markdown, re-org

This commit is contained in:
bbedward
2025-07-26 16:08:58 -04:00
parent 484a947127
commit 01a94a17de
22 changed files with 684 additions and 350 deletions

View File

@@ -11,7 +11,7 @@ Rectangle {
property var notificationGroup
property bool expanded: NotificationService.expandedGroups[notificationGroup?.key] || false
property bool descriptionExpanded: false
property bool descriptionExpanded: NotificationService.expandedMessages[notificationGroup?.latestNotification?.notification?.id + "_desc"] || false
property bool userInitiatedExpansion: false
width: parent ? parent.width : 400
@@ -75,7 +75,8 @@ Rectangle {
border.color: "transparent"
border.width: 0
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
anchors.top: parent.top
anchors.topMargin: 18
IconImage {
anchors.fill: parent
@@ -178,7 +179,7 @@ Rectangle {
Text {
id: descriptionText
property string fullText: notificationGroup?.latestNotification?.body || ""
property string fullText: notificationGroup?.latestNotification?.htmlBody || ""
property bool hasMoreText: truncated
text: fullText
@@ -189,16 +190,33 @@ Rectangle {
maximumLineCount: descriptionExpanded ? -1 : 2
wrapMode: Text.WordWrap
visible: text.length > 0
textFormat: Text.PlainText
linkColor: Theme.primary
onLinkActivated: Qt.openUrlExternally(link)
MouseArea {
anchors.fill: parent
cursorShape: (parent.hasMoreText || descriptionExpanded) ? Qt.PointingHandCursor : Qt.ArrowCursor
enabled: parent.hasMoreText || descriptionExpanded
onClicked: {
descriptionExpanded = !descriptionExpanded;
cursorShape: parent.hoveredLink ? Qt.PointingHandCursor :
(parent.hasMoreText || descriptionExpanded) ? Qt.PointingHandCursor :
Qt.ArrowCursor
onClicked: mouse => {
if (!parent.hoveredLink && (parent.hasMoreText || descriptionExpanded)) {
const messageId = notificationGroup?.latestNotification?.notification?.id + "_desc";
NotificationService.toggleMessageExpansion(messageId);
}
}
propagateComposedEvents: true
onPressed: mouse => {
if (parent.hoveredLink) {
mouse.accepted = false;
}
}
onReleased: mouse => {
if (parent.hoveredLink) {
mouse.accepted = false;
}
}
z: 1
}
}
}
@@ -305,7 +323,8 @@ Rectangle {
height: 32
radius: 16
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
anchors.top: parent.top
anchors.topMargin: 32
color: Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.1)
border.color: Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.2)
border.width: 1
@@ -384,7 +403,7 @@ Rectangle {
id: bodyText
property bool hasMoreText: truncated
text: modelData?.body || ""
text: modelData?.htmlBody || ""
color: Theme.surfaceVariantText
font.pixelSize: Theme.fontSizeSmall
width: parent.width
@@ -392,17 +411,31 @@ Rectangle {
maximumLineCount: messageExpanded ? -1 : 2
wrapMode: Text.WordWrap
visible: text.length > 0
textFormat: Text.PlainText
linkColor: Theme.primary
onLinkActivated: Qt.openUrlExternally(link)
MouseArea {
anchors.fill: parent
enabled: bodyText.hasMoreText || messageExpanded
cursorShape: bodyText.hasMoreText || messageExpanded ? Qt.PointingHandCursor : Qt.ArrowCursor
onClicked: {
if (bodyText.hasMoreText || messageExpanded) {
cursorShape: parent.hoveredLink ? Qt.PointingHandCursor :
(bodyText.hasMoreText || messageExpanded) ? Qt.PointingHandCursor :
Qt.ArrowCursor
onClicked: mouse => {
if (!parent.hoveredLink && (bodyText.hasMoreText || messageExpanded)) {
NotificationService.toggleMessageExpansion(modelData?.notification?.id || "");
}
}
propagateComposedEvents: true
onPressed: mouse => {
if (parent.hoveredLink) {
mouse.accepted = false;
}
}
onReleased: mouse => {
if (parent.hoveredLink) {
mouse.accepted = false;
}
}
}
}
}

View File

@@ -10,16 +10,30 @@ Item {
width: parent.width
height: 32
Text {
text: "Notifications"
font.pixelSize: Theme.fontSizeLarge
color: Theme.surfaceText
font.weight: Font.Medium
Row {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
spacing: Theme.spacingXS
Text {
text: "Notifications"
font.pixelSize: Theme.fontSizeLarge
color: Theme.surfaceText
font.weight: Font.Medium
anchors.verticalCenter: parent.verticalCenter
}
DankActionButton {
iconName: Prefs.doNotDisturb ? "notifications_off" : "notifications"
iconColor: Prefs.doNotDisturb ? Theme.error : Theme.surfaceText
buttonSize: 28
anchors.verticalCenter: parent.verticalCenter
onClicked: Prefs.setDoNotDisturb(!Prefs.doNotDisturb)
}
}
Rectangle {
id: clearAllButton
width: 120
height: 28
radius: Theme.cornerRadiusLarge

View File

@@ -1,139 +0,0 @@
import QtQuick
import Quickshell
import qs.Common
import qs.Services
QtObject {
id: manager
property int topMargin: 0
property int baseNotificationHeight: 120
property int maxTargetNotifications: 3
property var popupWindows: [] // strong refs to windows (live until exitFinished)
// Factory
property Component popupComponent: Component {
NotificationPopup {
onEntered: manager._onPopupEntered(this)
onExitFinished: manager._onPopupExitFinished(this)
}
}
property Connections notificationConnections: Connections {
target: NotificationService
function onVisibleNotificationsChanged() {
manager._sync(NotificationService.visibleNotifications);
}
}
function _hasWindowFor(w) {
return popupWindows.some(p => p && p.notificationData === w);
}
function _sync(newWrappers) {
for (let w of newWrappers) {
if (!_hasWindowFor(w)) insertNewestAtTop(w);
}
for (let p of popupWindows.slice()) {
if (p && p.notificationData && newWrappers.indexOf(p.notificationData) === -1 && !p.exiting) {
p.notificationData.removedByLimit = true;
p.notificationData.popup = false;
}
}
}
// Insert newest at top
function insertNewestAtTop(wrapper) {
// Shift live, non-exiting windows down *now*
for (let p of popupWindows) {
if (!p) continue;
if (p.exiting) continue;
// Guard: skip if p is already being destroyed
if (p.status === Component.Null) continue;
p.screenY = p.screenY + baseNotificationHeight;
}
// Create the new top window at fixed Y
const notificationId = wrapper && wrapper.notification ? wrapper.notification.id : "";
const win = popupComponent.createObject(null, { notificationData: wrapper, notificationId: notificationId, screenY: topMargin });
if (!win) {
console.warn("Popup create failed");
return;
}
popupWindows.push(win);
_maybeStartOverflow();
}
// Overflow: keep one extra (slot #4), then ask bottom to exit gracefully
function _active() {
return popupWindows.filter(p => p && p.notificationData && p.notificationData.popup);
}
function _bottom() {
let b = null, maxY = -1;
for (let p of _active()) {
if (p.exiting) continue;
if (p.screenY > maxY) {
maxY = p.screenY;
b = p;
}
}
return b;
}
function _maybeStartOverflow() {
if (_active().length <= maxTargetNotifications + 1) return;
const b = _bottom();
if (b && !b.exiting) {
// Tell the popup to animate out (don't destroy here)
b.notificationData.removedByLimit = true;
b.notificationData.popup = false;
}
}
// After entrance, you may kick overflow (optional)
function _onPopupEntered(p) {
_maybeStartOverflow();
}
// Primary cleanup path (after the popup finishes its exit)
function _onPopupExitFinished(p) {
const i = popupWindows.indexOf(p);
if (i !== -1) {
popupWindows.splice(i,1);
popupWindows = popupWindows.slice();
}
if (NotificationService.releaseWrapper)
NotificationService.releaseWrapper(p.notificationData);
// Finally destroy the window object
p.destroy();
// Compact survivors (only live, non-exiting)
const survivors = _active().filter(s => !s.exiting)
.sort((a,b) => a.screenY - b.screenY);
for (let k = 0; k < survivors.length; ++k)
survivors[k].screenY = topMargin + k * baseNotificationHeight;
_maybeStartOverflow();
}
// Optional sweeper (dev only): catch any stranded windows every 2s
property Timer sweeper: Timer {
interval: 2000
running: true
repeat: true
onTriggered: {
for (let p of popupWindows.slice()) {
if (!p) continue;
if (!p.visible && !p.notificationData) {
const i = popupWindows.indexOf(p);
if (i !== -1) {
popupWindows.splice(i,1);
popupWindows = popupWindows.slice();
}
}
}
}
}
}

View File

@@ -13,7 +13,9 @@ PanelWindow {
required property var notificationData
required property string notificationId
visible: true
readonly property bool hasValidData: notificationData && notificationData.notification
visible: hasValidData
WlrLayershell.layer: WlrLayershell.Overlay
WlrLayershell.exclusiveZone: -1
WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
@@ -31,14 +33,11 @@ PanelWindow {
right: 12
}
// Manager drives vertical stacking with this proxy:
property int screenY: 0
onScreenYChanged: margins.top = Theme.barHeight + 4 + screenY
// Disable vertical tween while exiting so there is never diagonal motion
Behavior on screenY {
id: screenYAnim
enabled: !exiting
enabled: !exiting && !_isDestroying
NumberAnimation {
duration: Anims.durShort
easing.type: Easing.BezierSpline
@@ -46,19 +45,25 @@ PanelWindow {
}
}
// State
property bool exiting: false
property bool _isDestroying: false
property bool _finalized: false
signal entered()
signal exitFinished()
onHasValidDataChanged: {
if (!hasValidData && !exiting && !_isDestroying) {
console.warn("NotificationPopup: Data became invalid, forcing exit");
forceExit();
}
}
Item {
id: content
anchors.fill: parent
visible: win.hasValidData
// We animate a Translate so anchors never override horizontal motion
transform: Translate { id: tx; x: Anims.slidePx } // start off-screen right
transform: Translate { id: tx; x: Anims.slidePx }
// Optional: layer while animating for smoothness
layer.enabled: (enterX.running || exitAnim.running)
layer.smooth: true
@@ -144,7 +149,6 @@ PanelWindow {
Rectangle {
id: iconContainer
readonly property bool hasNotificationImage: notificationData && notificationData.image && notificationData.image !== ""
property alias iconImage: iconImage
width: 55
height: 55
@@ -242,7 +246,7 @@ PanelWindow {
}
Text {
text: notificationData ? (notificationData.body || "") : ""
text: notificationData ? (notificationData.htmlBody || "") : ""
color: Theme.surfaceVariantText
font.pixelSize: Theme.fontSizeSmall
width: parent.width
@@ -250,7 +254,13 @@ PanelWindow {
maximumLineCount: 2
wrapMode: Text.WordWrap
visible: text.length > 0
textFormat: Text.PlainText
linkColor: Theme.primary
onLinkActivated: Qt.openUrlExternally(link)
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.NoButton
cursorShape: parent.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
}
}
}
}
@@ -270,7 +280,7 @@ PanelWindow {
buttonSize: 28
z: 15
onClicked: {
if (notificationData)
if (notificationData && !win.exiting)
notificationData.popup = false;
}
}
@@ -315,7 +325,7 @@ PanelWindow {
if (modelData && modelData.invoke) {
modelData.invoke();
}
if (notificationData) {
if (notificationData && !win.exiting) {
notificationData.popup = false;
}
}
@@ -355,7 +365,7 @@ PanelWindow {
onEntered: dismissButton.isHovered = true
onExited: dismissButton.isHovered = false
onClicked: {
if (notificationData) {
if (notificationData && !win.exiting) {
NotificationService.dismissNotification(notificationData);
}
}
@@ -378,24 +388,22 @@ PanelWindow {
notificationData.timer.restart();
}
onClicked: {
if (notificationData)
if (notificationData && !win.exiting)
notificationData.popup = false;
}
}
}
}
// Entrance: slide in from right using slowed Anims curves
NumberAnimation {
id: enterX
target: tx; property: "x"; from: Anims.slidePx; to: 0
duration: Anims.durMed
easing.type: Easing.BezierSpline
easing.bezierCurve: Anims.emphasizedDecel
onStopped: if (!win.exiting && Math.abs(tx.x) < 0.5) win.entered();
onStopped: if (!win.exiting && !win._isDestroying && Math.abs(tx.x) < 0.5) win.entered();
}
// Exit: slide out to right + fade using slowed Anims curves
ParallelAnimation {
id: exitAnim
PropertyAnimation {
@@ -419,52 +427,94 @@ PanelWindow {
onStopped: finalizeExit("animStopped")
}
// Start entrance one tick after create (so it always animates)
Component.onCompleted: Qt.callLater(() => enterX.restart())
Component.onCompleted: {
if (hasValidData) {
Qt.callLater(() => enterX.restart())
} else {
console.warn("NotificationPopup created with invalid data");
forceExit();
}
}
// Safe connection to wrapper: disable automatically when wrapper is null
Connections {
id: wrapperConn
target: win.notificationData || null
ignoreUnknownSignals: true
enabled: !win._isDestroying
function onPopupChanged() {
if (!win.notificationData) return; // guard
if (!win.notificationData || win._isDestroying) return;
if (!win.notificationData.popup && !win.exiting) {
// Freeze vertical and start exit
win.exiting = true; // disables screenY Behavior
exitAnim.restart();
exitWatchdog.restart(); // safety net
if (NotificationService.removeFromVisibleNotifications)
NotificationService.removeFromVisibleNotifications(win.notificationData);
startExit();
}
}
}
onNotificationDataChanged: wrapperConn.target = win.notificationData || null
onNotificationDataChanged: {
if (!_isDestroying) {
wrapperConn.target = win.notificationData || null;
}
}
// Timer to start on entrance
Timer {
id: enterDelay
interval: 160
repeat: false
onTriggered: {
if (notificationData && notificationData.timer)
if (notificationData && notificationData.timer && !exiting && !_isDestroying)
notificationData.timer.start();
}
}
// Start timer after entrance animation
onEntered: enterDelay.start()
onEntered: {
if (!_isDestroying) enterDelay.start();
}
function startExit() {
if (exiting || _isDestroying) return;
exiting = true;
exitAnim.restart();
exitWatchdog.restart();
if (NotificationService.removeFromVisibleNotifications) {
NotificationService.removeFromVisibleNotifications(win.notificationData);
}
}
function forceExit() {
if (_isDestroying) return;
_isDestroying = true;
exiting = true;
visible = false;
exitWatchdog.stop();
finalizeExit("forced");
}
// Idempotent finalizer so we never "half-exit"
property bool _finalized: false
function finalizeExit(reason) {
if (_finalized) return;
_finalized = true;
_isDestroying = true;
exitWatchdog.stop();
win.exitFinished(); // manager will destroy the window
wrapperConn.enabled = false;
wrapperConn.target = null;
win.exitFinished();
}
Timer {
id: exitWatchdog
interval: 600
repeat: false
onTriggered: finalizeExit("watchdog")
}
Timer { id: exitWatchdog; interval: 600; repeat: false; onTriggered: finalizeExit("watchdog") }
// If the popup is torn down unexpectedly, don't leave dangling timers
Component.onDestruction: { exitWatchdog.stop(); }
Component.onDestruction: {
_isDestroying = true;
exitWatchdog.stop();
if (notificationData && notificationData.timer) {
notificationData.timer.stop();
}
}
}

View File

@@ -0,0 +1,288 @@
import QtQuick
import Quickshell
import qs.Common
import qs.Services
QtObject {
id: manager
property int topMargin: 0
property int baseNotificationHeight: 120
property int maxTargetNotifications: 3
property var popupWindows: [] // strong refs to windows (live until exitFinished)
// Track destroying windows to prevent duplicate cleanup
property var destroyingWindows: new Set()
// Factory
property Component popupComponent: Component {
NotificationPopup {
onEntered: manager._onPopupEntered(this)
onExitFinished: manager._onPopupExitFinished(this)
}
}
property Connections notificationConnections: Connections {
target: NotificationService
function onVisibleNotificationsChanged() {
manager._sync(NotificationService.visibleNotifications);
}
}
function _hasWindowFor(w) {
return popupWindows.some(p => {
// More robust check for valid windows
return p &&
p.notificationData === w &&
!p._isDestroying &&
p.status !== Component.Null;
});
}
function _isValidWindow(p) {
return p &&
p.status !== Component.Null &&
!p._isDestroying &&
p.hasValidData;
}
function _sync(newWrappers) {
// Add new notifications
for (let w of newWrappers) {
if (!_hasWindowFor(w)) {
insertNewestAtTop(w);
}
}
// Remove old notifications
for (let p of popupWindows.slice()) {
if (!_isValidWindow(p)) continue;
if (p.notificationData && newWrappers.indexOf(p.notificationData) === -1 && !p.exiting) {
p.notificationData.removedByLimit = true;
p.notificationData.popup = false;
}
}
}
// Insert newest at top
function insertNewestAtTop(wrapper) {
if (!wrapper) {
console.warn("insertNewestAtTop: wrapper is null");
return;
}
// Shift live, non-exiting windows down *now*
for (let p of popupWindows) {
if (!_isValidWindow(p)) continue;
if (p.exiting) continue;
p.screenY = p.screenY + baseNotificationHeight;
}
// Create the new top window at fixed Y
const notificationId = wrapper && wrapper.notification ? wrapper.notification.id : "";
const win = popupComponent.createObject(null, {
notificationData: wrapper,
notificationId: notificationId,
screenY: topMargin
});
if (!win) {
console.warn("Popup create failed");
return;
}
// Validate the window was created properly
if (!win.hasValidData) {
console.warn("Popup created with invalid data, destroying");
win.destroy();
return;
}
popupWindows.push(win);
// Start sweeper if it's not running
if (!sweeper.running) {
sweeper.start();
}
_maybeStartOverflow();
}
// Overflow: keep one extra (slot #4), then ask bottom to exit gracefully
function _active() {
return popupWindows.filter(p => {
return _isValidWindow(p) &&
p.notificationData &&
p.notificationData.popup &&
!p.exiting;
});
}
function _bottom() {
let b = null, maxY = -1;
for (let p of _active()) {
if (p.screenY > maxY) {
maxY = p.screenY;
b = p;
}
}
return b;
}
function _maybeStartOverflow() {
const activeWindows = _active();
if (activeWindows.length <= maxTargetNotifications + 1) return;
const b = _bottom();
if (b && !b.exiting) {
// Tell the popup to animate out (don't destroy here)
b.notificationData.removedByLimit = true;
b.notificationData.popup = false;
}
}
// After entrance, you may kick overflow (optional)
function _onPopupEntered(p) {
if (_isValidWindow(p)) {
_maybeStartOverflow();
}
}
// Primary cleanup path (after the popup finishes its exit)
function _onPopupExitFinished(p) {
if (!p) return;
// Prevent duplicate cleanup
const windowId = p.toString();
if (destroyingWindows.has(windowId)) {
return;
}
destroyingWindows.add(windowId);
// Remove from popupWindows
const i = popupWindows.indexOf(p);
if (i !== -1) {
popupWindows.splice(i, 1);
popupWindows = popupWindows.slice();
}
// Release the wrapper
if (NotificationService.releaseWrapper && p.notificationData) {
NotificationService.releaseWrapper(p.notificationData);
}
// Schedule destruction
Qt.callLater(() => {
if (p && p.destroy) {
try {
p.destroy();
} catch (e) {
console.warn("Error destroying popup:", e);
}
}
// Clean up tracking after a delay
Qt.callLater(() => {
destroyingWindows.delete(windowId);
});
});
// Compact survivors (only live, non-exiting)
const survivors = _active().sort((a, b) => a.screenY - b.screenY);
for (let k = 0; k < survivors.length; ++k) {
survivors[k].screenY = topMargin + k * baseNotificationHeight;
}
_maybeStartOverflow();
}
// Smart sweeper that only runs when needed
property Timer sweeper: Timer {
interval: 2000
running: false // Not running by default
repeat: true
onTriggered: {
let toRemove = [];
for (let p of popupWindows) {
if (!p) {
toRemove.push(p);
continue;
}
// Check for various zombie conditions
const isZombie =
p.status === Component.Null ||
(!p.visible && !p.exiting) ||
(!p.notificationData && !p._isDestroying) ||
(!p.hasValidData && !p._isDestroying);
if (isZombie) {
console.warn("Sweeper found zombie window, cleaning up");
toRemove.push(p);
// Try to force cleanup
if (p.forceExit) {
p.forceExit();
} else if (p.destroy) {
try {
p.destroy();
} catch (e) {
console.warn("Error destroying zombie:", e);
}
}
}
}
// Remove all zombies from array
if (toRemove.length > 0) {
for (let zombie of toRemove) {
const i = popupWindows.indexOf(zombie);
if (i !== -1) {
popupWindows.splice(i, 1);
}
}
popupWindows = popupWindows.slice();
// Recompact after cleanup
const survivors = _active().sort((a, b) => a.screenY - b.screenY);
for (let k = 0; k < survivors.length; ++k) {
survivors[k].screenY = topMargin + k * baseNotificationHeight;
}
}
// Stop the timer if no windows remain
if (popupWindows.length === 0) {
sweeper.stop();
}
}
}
// Watch for changes to popup windows array
onPopupWindowsChanged: {
if (popupWindows.length > 0 && !sweeper.running) {
sweeper.start();
} else if (popupWindows.length === 0 && sweeper.running) {
sweeper.stop();
}
}
// Emergency cleanup function
function cleanupAllWindows() {
sweeper.stop();
for (let p of popupWindows.slice()) {
if (p) {
try {
if (p.forceExit) p.forceExit();
else if (p.destroy) p.destroy();
} catch (e) {
console.warn("Error during emergency cleanup:", e);
}
}
}
popupWindows = [];
destroyingWindows.clear();
}
}