From c3a8a171734bfeced58f4611365e85a6daed7db9 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 29 Aug 2025 23:06:55 +0200 Subject: ... --- .../gkleen@sif/shell/quickshell-plugins/Chrono.cpp | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp') diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp new file mode 100644 index 00000000..929b7be6 --- /dev/null +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp @@ -0,0 +1,88 @@ +#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) { + auto currentTime = std::chrono::system_clock::now(); + auto offset = std::chrono::duration_cast(targetTime - currentTime); + this->currentTime = abs(offset.count()) < 500 ? 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) { + auto currentTime = std::chrono::system_clock::now(); + auto offset = std::chrono::duration_cast(targetTime - currentTime); + auto nextTime = abs(offset.count()) < 500 ? targetTime : currentTime; + + { + using namespace std::chrono_literals; + + 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; + auto delay = std::chrono::duration_cast(nextTime - currentTime); + this->timer.start(delay); +} + +QString Chrono::format() const { return this->mFormat; } +void Chrono::setFormat(QString format) { + if (format == this->mFormat) return; + this->mFormat = format; + emit this->formatChanged(); + this->update(); +} + +QString Chrono::date() const { + return QString::fromStdString(std::format(std::runtime_format(this->mFormat.toStdString()), std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::time_point_cast(this->currentTime)))); +} -- cgit v1.2.3