notifs: fix null errors

This commit is contained in:
2 * r + 2 * t 2025-05-23 21:58:56 +08:00
parent 44f0b2928e
commit 8bcb349462

View file

@ -13,12 +13,12 @@ StyledRect {
id: root id: root
required property Notifs.Notif modelData required property Notifs.Notif modelData
readonly property bool hasImage: modelData.image.length > 0 readonly property bool hasImage: modelData?.image.length > 0
readonly property bool hasAppIcon: modelData.appIcon.length > 0 readonly property bool hasAppIcon: modelData?.appIcon.length > 0
readonly property int nonAnimHeight: summary.implicitHeight + (root.expanded ? appName.height + body.height + actions.height + actions.anchors.topMargin : bodyPreview.height) + inner.anchors.margins * 2 readonly property int nonAnimHeight: summary.implicitHeight + (root.expanded ? appName.height + body.height + actions.height + actions.anchors.topMargin : bodyPreview.height) + inner.anchors.margins * 2
property bool expanded property bool expanded
color: root.modelData.urgency === NotificationUrgency.Critical ? Colours.palette.m3secondaryContainer : Colours.palette.m3surfaceContainer color: root.modelData?.urgency === NotificationUrgency.Critical ? Colours.palette.m3secondaryContainer : Colours.palette.m3surfaceContainer
radius: Appearance.rounding.normal radius: Appearance.rounding.normal
implicitWidth: NotifsConfig.sizes.width implicitWidth: NotifsConfig.sizes.width
implicitHeight: inner.height implicitHeight: inner.height
@ -43,12 +43,12 @@ StyledRect {
onPressed: event => { onPressed: event => {
startY = event.y; startY = event.y;
if (event.button === Qt.MiddleButton) if (event.button === Qt.MiddleButton)
root.modelData.notification.dismiss(); root.modelData?.notification.dismiss();
} }
onReleased: event => { onReleased: event => {
if (Math.abs(root.x) < NotifsConfig.sizes.width * NotifsConfig.clearThreshold) if (Math.abs(root.x) < NotifsConfig.sizes.width * NotifsConfig.clearThreshold)
root.x = 0; root.x = 0;
else else if (root.modelData)
root.modelData.popup = false; root.modelData.popup = false;
} }
onPositionChanged: event => { onPositionChanged: event => {
@ -62,8 +62,8 @@ StyledRect {
if (!NotifsConfig.actionOnClick || event.button !== Qt.LeftButton) if (!NotifsConfig.actionOnClick || event.button !== Qt.LeftButton)
return; return;
const actions = root.modelData.actions; const actions = root.modelData?.actions;
if (actions.length === 1) if (actions?.length === 1)
actions[0].invoke(); actions[0].invoke();
} }
} }
@ -109,7 +109,7 @@ StyledRect {
Image { Image {
anchors.fill: parent anchors.fill: parent
source: Qt.resolvedUrl(root.modelData.image) source: Qt.resolvedUrl(root.modelData?.image)
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop
cache: false cache: false
asynchronous: true asynchronous: true
@ -130,7 +130,7 @@ StyledRect {
sourceComponent: StyledRect { sourceComponent: StyledRect {
radius: Appearance.rounding.full radius: Appearance.rounding.full
color: root.modelData.urgency === NotificationUrgency.Critical ? Colours.palette.m3error : root.modelData.urgency === NotificationUrgency.Low ? Colours.palette.m3surfaceContainerHighest : Colours.palette.m3tertiaryContainer color: root.modelData?.urgency === NotificationUrgency.Critical ? Colours.palette.m3error : root.modelData?.urgency === NotificationUrgency.Low ? Colours.palette.m3surfaceContainerHighest : Colours.palette.m3tertiaryContainer
implicitWidth: root.hasImage ? NotifsConfig.sizes.badge : NotifsConfig.sizes.image implicitWidth: root.hasImage ? NotifsConfig.sizes.badge : NotifsConfig.sizes.image
implicitHeight: root.hasImage ? NotifsConfig.sizes.badge : NotifsConfig.sizes.image implicitHeight: root.hasImage ? NotifsConfig.sizes.badge : NotifsConfig.sizes.image
@ -141,26 +141,26 @@ StyledRect {
asynchronous: true asynchronous: true
anchors.centerIn: parent anchors.centerIn: parent
visible: !root.modelData.appIcon.includes("symbolic") visible: !root.modelData?.appIcon.includes("symbolic")
width: Math.round(parent.width * 0.6) width: Math.round(parent.width * 0.6)
height: Math.round(parent.width * 0.6) height: Math.round(parent.width * 0.6)
sourceComponent: IconImage { sourceComponent: IconImage {
implicitSize: Math.round(parent.width * 0.6) implicitSize: Math.round(parent.width * 0.6)
source: Quickshell.iconPath(root.modelData.appIcon) source: Quickshell.iconPath(root.modelData?.appIcon)
asynchronous: true asynchronous: true
} }
} }
Loader { Loader {
active: root.modelData.appIcon.includes("symbolic") active: root.modelData?.appIcon.includes("symbolic") ?? false
asynchronous: true asynchronous: true
anchors.fill: icon anchors.fill: icon
sourceComponent: Colouriser { sourceComponent: Colouriser {
source: icon source: icon
colorizationColor: root.modelData.urgency === NotificationUrgency.Critical ? Colours.palette.m3onError : root.modelData.urgency === NotificationUrgency.Low ? Colours.palette.m3onSurface : Colours.palette.m3onTertiaryContainer colorizationColor: root.modelData?.urgency === NotificationUrgency.Critical ? Colours.palette.m3onError : root.modelData?.urgency === NotificationUrgency.Low ? Colours.palette.m3onSurface : Colours.palette.m3onTertiaryContainer
} }
} }
@ -171,7 +171,7 @@ StyledRect {
sourceComponent: MaterialIcon { sourceComponent: MaterialIcon {
text: { text: {
const summary = root.modelData.summary.toLowerCase(); const summary = root.modelData?.summary.toLowerCase() ?? "";
if (summary.includes("reboot")) if (summary.includes("reboot"))
return "restart_alt"; return "restart_alt";
if (summary.includes("recording")) if (summary.includes("recording"))
@ -190,12 +190,12 @@ StyledRect {
return "update"; return "update";
if (summary.startsWith("file")) if (summary.startsWith("file"))
return "folder_copy"; return "folder_copy";
if (root.modelData.urgency === NotificationUrgency.Critical) if (root.modelData?.urgency === NotificationUrgency.Critical)
return "release_alert"; return "release_alert";
return "chat"; return "chat";
} }
color: root.modelData.urgency === NotificationUrgency.Critical ? Colours.palette.m3onError : root.modelData.urgency === NotificationUrgency.Low ? Colours.palette.m3onSurface : Colours.palette.m3onTertiaryContainer color: root.modelData?.urgency === NotificationUrgency.Critical ? Colours.palette.m3onError : root.modelData?.urgency === NotificationUrgency.Low ? Colours.palette.m3onSurface : Colours.palette.m3onTertiaryContainer
font.pointSize: Appearance.font.size.large font.pointSize: Appearance.font.size.large
} }
} }
@ -225,7 +225,7 @@ StyledRect {
TextMetrics { TextMetrics {
id: appNameMetrics id: appNameMetrics
text: root.modelData.appName text: root.modelData?.appName ?? ""
font.family: appName.font.family font.family: appName.font.family
font.pointSize: appName.font.pointSize font.pointSize: appName.font.pointSize
elide: Text.ElideRight elide: Text.ElideRight
@ -278,7 +278,7 @@ StyledRect {
TextMetrics { TextMetrics {
id: summaryMetrics id: summaryMetrics
text: root.modelData.summary text: root.modelData?.summary ?? ""
font.family: summary.font.family font.family: summary.font.family
font.pointSize: summary.font.pointSize font.pointSize: summary.font.pointSize
elide: Text.ElideRight elide: Text.ElideRight
@ -324,7 +324,7 @@ StyledRect {
animate: true animate: true
horizontalAlignment: Text.AlignLeft horizontalAlignment: Text.AlignLeft
text: root.modelData.timeStr text: root.modelData?.timeStr ?? ""
color: Colours.palette.m3onSurfaceVariant color: Colours.palette.m3onSurfaceVariant
font.pointSize: Appearance.font.size.small font.pointSize: Appearance.font.size.small
} }
@ -381,7 +381,7 @@ StyledRect {
TextMetrics { TextMetrics {
id: bodyPreviewMetrics id: bodyPreviewMetrics
text: root.modelData.body text: root.modelData?.body ?? ""
font.family: bodyPreview.font.family font.family: bodyPreview.font.family
font.pointSize: bodyPreview.font.pointSize font.pointSize: bodyPreview.font.pointSize
elide: Text.ElideRight elide: Text.ElideRight
@ -398,7 +398,7 @@ StyledRect {
animate: true animate: true
textFormat: Text.MarkdownText textFormat: Text.MarkdownText
text: root.modelData.body text: root.modelData?.body ?? ""
color: Colours.palette.m3onSurfaceVariant color: Colours.palette.m3onSurfaceVariant
font.pointSize: Appearance.font.size.small font.pointSize: Appearance.font.size.small
wrapMode: Text.WrapAtWordBoundaryOrAnywhere wrapMode: Text.WrapAtWordBoundaryOrAnywhere
@ -426,7 +426,7 @@ StyledRect {
} }
Repeater { Repeater {
model: root.modelData.actions model: root.modelData?.actions ?? 0
delegate: StyledRect { delegate: StyledRect {
id: action id: action
@ -445,7 +445,7 @@ StyledRect {
radius: Appearance.rounding.full radius: Appearance.rounding.full
function onClicked(): void { function onClicked(): void {
action.modelData.invoke(); action.modelData?.invoke();
} }
} }
@ -461,12 +461,12 @@ StyledRect {
TextMetrics { TextMetrics {
id: actionTextMetrics id: actionTextMetrics
text: modelData.text text: modelData?.text
font.family: actionText.font.family font.family: actionText.font.family
font.pointSize: actionText.font.pointSize font.pointSize: actionText.font.pointSize
elide: Text.ElideRight elide: Text.ElideRight
elideWidth: { elideWidth: {
const numActions = root.modelData.actions.length; const numActions = root.modelData?.actions.length;
return (inner.width - actions.spacing * (numActions - 1)) / numActions - Appearance.padding.normal * 2; return (inner.width - actions.spacing * (numActions - 1)) / numActions - Appearance.padding.normal * 2;
} }
} }