Skip to content

Commit 3634689

Browse files
committed
quickshell/notifications: add timestamp
disabled for now, to be used with a panel (TODO)
1 parent 8b26f21 commit 3634689

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

home/services/quickshell/notifications/NotificationBox.qml

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ WrapperMouseArea {
1313
id: root
1414

1515
acceptedButtons: Qt.AllButtons
16+
hoverEnabled: true
1617

1718
property Notification n
19+
property real timestamp
20+
property real elapsed: Date.now()
1821
property string image: (n.image == "" && n.appIcon != "") ? n.appIcon : n.image
1922
property bool hasAppIcon: !(n.image == "" && n.appIcon != "")
2023
property int indexPopup: -1
2124
property int indexAll: -1
2225
property real iconSize: 48
23-
property bool expanded: false
2426

25-
function getImage(image: string): string {
26-
if (image.search(/:\/\//) != -1)
27-
return Qt.resolvedUrl(image);
28-
return Quickshell.iconPath(image);
29-
}
27+
property bool showTime: false
28+
property bool expanded: false
3029

3130
onClicked: mouse => {
3231
if (mouse.button == Qt.LeftButton && root.n.actions != []) {
@@ -41,6 +40,17 @@ WrapperMouseArea {
4140
}
4241
}
4342

43+
ElapsedTimer {
44+
id: elapsedTimer
45+
}
46+
47+
Timer {
48+
running: root.showTime
49+
interval: 1000
50+
repeat: true
51+
onTriggered: root.elapsed = elapsedTimer.elapsed()
52+
}
53+
4454
Rectangle {
4555
implicitWidth: 360
4656
implicitHeight: mainLayout.implicitHeight
@@ -76,7 +86,7 @@ WrapperMouseArea {
7686

7787
IconImage {
7888
implicitSize: coverItem.height
79-
source: root.getImage(root.image)
89+
source: Utils.getImage(root.image)
8090
}
8191
}
8292

@@ -95,7 +105,7 @@ WrapperMouseArea {
95105

96106
IconImage {
97107
implicitSize: 16
98-
source: root.getImage(root.n.appIcon)
108+
source: Utils.getImage(root.n.appIcon)
99109
}
100110
}
101111
}
@@ -108,11 +118,21 @@ WrapperMouseArea {
108118
Layout.leftMargin: coverItem.visible ? 4 : 12
109119
spacing: 4
110120

111-
Text {
121+
RowLayout {
112122
Layout.maximumWidth: contentLayout.width - buttonLayout.width
113-
text: root.n.summary
114-
elide: Text.ElideRight
115-
font.weight: Font.Bold
123+
124+
Text {
125+
Layout.alignment: Qt.AlignLeft
126+
text: root.n.summary
127+
elide: Text.ElideRight
128+
font.weight: Font.Bold
129+
}
130+
131+
Text {
132+
visible: root.showTime
133+
Layout.alignment: Qt.AlignRight
134+
text: Utils.humanTime(root.timestamp, root.elapsed)
135+
}
116136
}
117137

118138
Text {
@@ -170,6 +190,7 @@ WrapperMouseArea {
170190

171191
RowLayout {
172192
id: buttonLayout
193+
visible: root.containsMouse
173194
implicitHeight: 16
174195

175196
anchors {

home/services/quickshell/notifications/NotificationOverlay.qml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ PanelWindow {
3737
id: notifBox
3838
required property int index
3939
n: NotificationState.popupNotifs[index]
40+
timestamp: Date.now()
4041
indexPopup: index
4142

4243
Timer {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pragma Singleton
2+
3+
import QtQml
4+
import Quickshell
5+
6+
Singleton {
7+
function getImage(image: string): string {
8+
if (image.search(/:\/\//) != -1)
9+
return Qt.resolvedUrl(image);
10+
return Quickshell.iconPath(image);
11+
}
12+
13+
function humanTime(timestamp: int, elapsed: int): string {
14+
const MINUTE = 60;
15+
const HOUR = 60 * MINUTE;
16+
const DAY = 24 * HOUR;
17+
18+
const diff = elapsed - timestamp;
19+
20+
if (diff < 15) {
21+
return "now";
22+
} else if (diff < MINUTE) {
23+
return "seconds ago";
24+
} else if (diff < HOUR) {
25+
return `${diff}m ago`;
26+
} else if (diff < DAY) {
27+
return `${Math.round(diff / HOUR)}h ago`;
28+
} else if (diff < 2 * DAY) {
29+
return "yesterday";
30+
} else {
31+
return `${Math.round(diff / DAY)} days ago`;
32+
}
33+
}
34+
}

home/services/quickshell/utils/qmldir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ singleton Config 1.0 Config.qml
33
singleton MprisState 1.0 MprisState.qml
44
singleton NotificationState 1.0 NotificationState.qml
55
singleton ResourcesState 1.0 ResourcesState.qml
6+
singleton Utils 1.0 Utils.qml

0 commit comments

Comments
 (0)