From ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 23 Sep 2026 21:08:35 +0200 Subject: ... --- .../shell/quickshell-plugins/Chrono.cpp | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 accounts/gkleen@skadhi/shell/quickshell-plugins/Chrono.cpp (limited to 'accounts/gkleen@skadhi/shell/quickshell-plugins/Chrono.cpp') diff --git a/accounts/gkleen@skadhi/shell/quickshell-plugins/Chrono.cpp b/accounts/gkleen@skadhi/shell/quickshell-plugins/Chrono.cpp new file mode 100644 index 00000000..22b3469b --- /dev/null +++ b/accounts/gkleen@skadhi/shell/quickshell-plugins/Chrono.cpp @@ -0,0 +1,83 @@ +#include "Chrono.hpp" + +#include +#include +#include + +Chrono::Chrono(QObject* parent): QObject(parent) { + QObject::connect(&this->timer, &QTimer::timeout, this, &Chrono::onTimeout); + this->update(); +} + +bool Chrono::enabled() const { return this->mEnabled; } + +void Chrono::setEnabled(bool enabled) { + if (enabled == this->mEnabled) return; + this->mEnabled = enabled; + emit this->enabledChanged(); + this->update(); +} + +Chrono::Precision Chrono::precision() const { return this->mPrecision; } + +void Chrono::setPrecision(Chrono::Precision precision) { + if (precision == this->mPrecision) return; + this->mPrecision = precision; + emit this->precisionChanged(); + this->update(); +} + +void Chrono::onTimeout() { + this->setTime(this->targetTime); + this->schedule(this->targetTime); +} + +void Chrono::update() { + if (this->mEnabled) { + this->setTime(std::chrono::time_point()); + this->schedule(std::chrono::time_point()); + } else { + this->timer.stop(); + } +} + +void Chrono::setTime(const std::chrono::time_point& targetTime) { + using namespace std::chrono_literals; + + auto currentTime = std::chrono::system_clock::now(); + auto offset = std::chrono::duration_cast(targetTime - currentTime); + this->currentTime = abs(offset) < 500ms ? targetTime : currentTime; + + switch (this->mPrecision) { + case Chrono::Hours: this->currentTime = std::chrono::time_point_cast(this->currentTime); + case Chrono::Minutes: this->currentTime = std::chrono::time_point_cast(this->currentTime); + case Chrono::Seconds: this->currentTime = std::chrono::time_point_cast(this->currentTime); + } + + emit this->dateChanged(); +} + +void Chrono::schedule(const std::chrono::time_point& targetTime) { + using namespace std::chrono_literals; + + auto currentTime = std::chrono::system_clock::now(); + auto offset = std::chrono::duration_cast(targetTime - currentTime); + auto nextTime = abs(offset) < 500ms ? targetTime : currentTime; + + switch (this->mPrecision) { + case Chrono::Hours: nextTime = std::chrono::time_point_cast(nextTime) + 1h; + case Chrono::Minutes: nextTime = std::chrono::time_point_cast(nextTime) + 1min; + case Chrono::Seconds: nextTime = std::chrono::time_point_cast(nextTime) + 1s; + } + + this->targetTime = nextTime; + this->timer.start(std::chrono::duration_cast(nextTime - currentTime)); +} + +QString Chrono::format(const QString& fmt) const { + return QString::fromStdString(std::format(std::runtime_format(fmt.toStdString()), std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::time_point_cast(this->currentTime)))); +} + +QDateTime Chrono::date() const { + return QDateTime::fromStdTimePoint(std::chrono::time_point_cast(this->currentTime)); +} -- cgit v1.2.3