From 19daf937167866b74d17841b99cade578b09f54f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tinkl?= Date: Mon, 12 Feb 2024 12:43:14 +0100 Subject: [PATCH] fix: timestamp shows yesterday for a message received 3 days ago - introduce one global update timer, to be used in various components for date/time sensitive updates - this timer runs only when the app is/becomes active - use the timer's `triggered()` signal to update the timestamp label when needed; ie. if it's to display a relative timestamp and it's currently visible Fixes #11460 --- .../include/StatusQ/sharedupdatetimer.h | 17 ++++++++++++++ .../Components/StatusTimeStampLabel.qml | 19 ++++++++++++++- ui/StatusQ/src/plugin.cpp | 4 ++++ ui/StatusQ/src/sharedupdatetimer.cpp | 23 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ui/StatusQ/include/StatusQ/sharedupdatetimer.h create mode 100644 ui/StatusQ/src/sharedupdatetimer.cpp diff --git a/ui/StatusQ/include/StatusQ/sharedupdatetimer.h b/ui/StatusQ/include/StatusQ/sharedupdatetimer.h new file mode 100644 index 00000000000..23dac2b3115 --- /dev/null +++ b/ui/StatusQ/include/StatusQ/sharedupdatetimer.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +class SharedUpdateTimer : public QObject +{ + Q_OBJECT +public: + explicit SharedUpdateTimer(QObject* parent = nullptr); + +signals: + void triggered(); + +private: + QTimer m_timer; +}; diff --git a/ui/StatusQ/src/StatusQ/Components/StatusTimeStampLabel.qml b/ui/StatusQ/src/StatusQ/Components/StatusTimeStampLabel.qml index 9b7ad9cc4bd..23da366aeb4 100644 --- a/ui/StatusQ/src/StatusQ/Components/StatusTimeStampLabel.qml +++ b/ui/StatusQ/src/StatusQ/Components/StatusTimeStampLabel.qml @@ -12,7 +12,24 @@ StatusBaseText { color: Theme.palette.baseColor1 font.pixelSize: 10 visible: !!text - text: showFullTimestamp ? LocaleUtils.formatDateTime(timestamp) : LocaleUtils.formatRelativeTimestamp(timestamp) + text: d.formattedLabel + + QtObject { + id: d + property string formattedLabel: showFullTimestamp ? LocaleUtils.formatDateTime(root.timestamp) : LocaleUtils.formatRelativeTimestamp(root.timestamp) + + readonly property var _conn: Connections { + target: SharedUpdateTimer + enabled: root.visible && root.timestamp && !root.showFullTimestamp + function onTriggered() { + const newFormattedLabel = LocaleUtils.formatRelativeTimestamp(root.timestamp) + if (newFormattedLabel !== d.formattedLabel) { + d.formattedLabel = newFormattedLabel + } + } + } + } + StatusToolTip { id: tooltip visible: hhandler.hovered && !!text diff --git a/ui/StatusQ/src/plugin.cpp b/ui/StatusQ/src/plugin.cpp index 277cede2c94..f9accfe6e29 100644 --- a/ui/StatusQ/src/plugin.cpp +++ b/ui/StatusQ/src/plugin.cpp @@ -14,6 +14,7 @@ #include "StatusQ/permissionutilsinternal.h" #include "StatusQ/rolesrenamingmodel.h" #include "StatusQ/rxvalidator.h" +#include "StatusQ/sharedupdatetimer.h" #include "StatusQ/statussyntaxhighlighter.h" #include "StatusQ/statuswindow.h" #include "StatusQ/stringutilsinternal.h" @@ -57,6 +58,9 @@ class StatusQPlugin : public QQmlExtensionPlugin qmlRegisterType("StatusQ", 0, 1, "WritableProxyModel"); qmlRegisterType("StatusQ", 0, 1, "FormattedDoubleProperty"); + qmlRegisterSingletonType( + "StatusQ.Core", 0, 1, "SharedUpdateTimer", [](QQmlEngine*, QJSEngine*) { return new SharedUpdateTimer; }); + qmlRegisterSingletonType("StatusQ", 0, 1, "QClipboardProxy", &QClipboardProxy::qmlInstance); qmlRegisterSingletonType( diff --git a/ui/StatusQ/src/sharedupdatetimer.cpp b/ui/StatusQ/src/sharedupdatetimer.cpp new file mode 100644 index 00000000000..26e0c2ff236 --- /dev/null +++ b/ui/StatusQ/src/sharedupdatetimer.cpp @@ -0,0 +1,23 @@ +#include "StatusQ/sharedupdatetimer.h" + +#include + +using namespace std::chrono_literals; + +SharedUpdateTimer::SharedUpdateTimer(QObject* parent) + : QObject{parent} +{ + m_timer.setTimerType(Qt::VeryCoarseTimer); + m_timer.setInterval(1s); + + connect(&m_timer, &QTimer::timeout, this, &SharedUpdateTimer::triggered); + connect(qApp, &QGuiApplication::applicationStateChanged, this, [this](Qt::ApplicationState state) { + if (state == Qt::ApplicationActive) { + emit triggered(); // emulate QML Timer's `triggeredOnStart` + m_timer.start(); + } + else { + m_timer.stop(); + } + }); +}