From 32a1a40f646d286d6c33a6a7def831af49558cf5 Mon Sep 17 00:00:00 2001 From: end-4 <97237370+end-4@users.noreply.github.com> Date: Sat, 25 May 2024 21:55:11 +0700 Subject: [PATCH] notifications: time is "Now" within 1 minute --- .../modules/.commonwidgets/notification.js | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/.config/ags/modules/.commonwidgets/notification.js b/.config/ags/modules/.commonwidgets/notification.js index 5e4074041..121df0b0b 100644 --- a/.config/ags/modules/.commonwidgets/notification.js +++ b/.config/ags/modules/.commonwidgets/notification.js @@ -25,6 +25,19 @@ function exists(widget) { return widget !== null; } +const getFriendlyNotifTimeString = (timeObject) => { + const messageTime = GLib.DateTime.new_from_unix_local(timeObject); + const oneMinuteAgo = GLib.DateTime.new_now_local().add_seconds(-60); + if (messageTime.compare(oneMinuteAgo) > 0) + return 'Now'; + else if (messageTime.get_day_of_year() == GLib.DateTime.new_now_local().get_day_of_year()) + return messageTime.format(userOptions.time.format); + else if (messageTime.get_day_of_year() == GLib.DateTime.new_now_local().get_day_of_year() - 1) + return 'Yesterday'; + else + return messageTime.format(userOptions.time.dateFormat); +} + const NotificationIcon = (notifObject) => { // { appEntry, appIcon, image }, urgency = 'normal' if (notifObject.image) { @@ -218,14 +231,7 @@ export default ({ }), ] }); - let notifTime = ''; - const messageTime = GLib.DateTime.new_from_unix_local(notifObject.time); - if (messageTime.get_day_of_year() == GLib.DateTime.new_now_local().get_day_of_year()) - notifTime = messageTime.format(userOptions.time.format); - else if (messageTime.get_day_of_year() == GLib.DateTime.new_now_local().get_day_of_year() - 1) - notifTime = 'Yesterday'; - else - notifTime = messageTime.format(userOptions.time.dateFormat); + const notifTextSummary = Label({ xalign: 0, className: 'txt-small txt-semibold titlefont', @@ -237,11 +243,15 @@ export default ({ useMarkup: notifObject.summary.startsWith('<'), label: notifObject.summary, }); + const initTimeString = getFriendlyNotifTimeString(notifObject.time); const notifTextBody = Label({ vpack: 'center', justification: 'right', className: 'txt-smaller txt-semibold', - label: notifTime, + label: initTimeString, + setup: initTimeString == 'Now' ? (self) => { + Utils.timeout(60000, () => self.label = getFriendlyNotifTimeString(notifObject.time)) + } : () => {}, }); const notifText = Box({ valign: Gtk.Align.CENTER,