From cc84ab2289381038f483f06963374aa0247f6724 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 2 Sep 2025 20:56:37 +0200 Subject: ... --- .../shell/quickshell-plugins/CMakeLists.txt | 1 + .../gkleen@sif/shell/quickshell-plugins/Chrono.hpp | 3 +- .../shell/quickshell-plugins/FileSelector.cpp | 99 ++++++++++++++++++++++ .../shell/quickshell-plugins/FileSelector.hpp | 52 ++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp create mode 100644 accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.hpp (limited to 'accounts/gkleen@sif/shell/quickshell-plugins') diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/CMakeLists.txt b/accounts/gkleen@sif/shell/quickshell-plugins/CMakeLists.txt index aa363c4c..2123ed35 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/CMakeLists.txt +++ b/accounts/gkleen@sif/shell/quickshell-plugins/CMakeLists.txt @@ -104,6 +104,7 @@ qt6_add_qml_module(customplugin target_sources(customplugin PRIVATE Chrono.cpp Chrono.hpp + FileSelector.cpp FileSelector.hpp ) target_compile_features(customplugin PUBLIC cxx_std_26) diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp index 4d06007d..04080187 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp @@ -1,11 +1,12 @@ #pragma once +#include + #include #include #include #include -#include class Chrono : public QObject { Q_OBJECT; diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp new file mode 100644 index 00000000..3a0537b6 --- /dev/null +++ b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp @@ -0,0 +1,99 @@ +#include "FileSelector.hpp" + +#include +#include +#include +#include + +#include +#include + +namespace fs = std::filesystem; + +FileSelector::FileSelector(QObject* parent): QObject(parent) { + QObject::connect(&this->timer, &QTimer::timeout, this, &FileSelector::onTimeout); +} + +QString FileSelector::directory() const { + return QString::fromStdString(this->mDirectory->string()); +} +void FileSelector::setDirectory(QString directory) { + this->mDirectory = directory.toStdString(); + if (this->mDirectory && this->mEpoch) + this->update(); + emit this->directoryChanged(); +} + +unsigned int FileSelector::epoch() const { + return std::chrono::duration_cast(*this->mEpoch).count(); +} +void FileSelector::setEpoch(unsigned int epoch) { + this->mEpoch = std::chrono::milliseconds{epoch}; + if (this->mDirectory && this->mEpoch) + this->update(); + emit this->epochChanged(); +} + +QString FileSelector::seed() const { + return this->mSeed; +} +void FileSelector::setSeed(QString seed) { + this->mSeed = seed; + emit this->seedChanged(); +} + +QString FileSelector::selected() const { + if (!this->mDirectory || !this->mEpoch) + return QString(); + + std::vector shuffled(this->mFiles.begin(), this->mFiles.end()); + std::sort(shuffled.begin(), shuffled.end()); + + auto currentTime = std::chrono::system_clock::now(); + uint64_t currentEpoch = currentTime.time_since_epoch() / *this->mEpoch; + std::chrono::milliseconds timeInEpoch = std::chrono::duration_cast(currentTime.time_since_epoch()) % *this->mEpoch; + + std::ostringstream seed; + seed << this->mSeed.size() << ";" << this->mSeed.toStdString() << ";"; + seed << *this->mEpoch << ";"; + seed << currentEpoch << ";"; + seed << this->mDirectory->string().size() << ";" << *this->mDirectory << ";"; + seed << this->mFiles.size() << ";"; + for (const fs::path& p: this->mFiles) + seed << p.string().size() << ";" << p << ";"; + + std::vector v; + v.reserve(seed.str().size()); + for (const char& c: seed.str()) + v.push_back(c); + + std::seed_seq engine_seed(v.begin(), v.end()); + std::mt19937 g(engine_seed); + std::shuffle(shuffled.begin(), shuffled.end(), g); + + std::vector::size_type ix = shuffled.size() * timeInEpoch / *this->mEpoch; + return QString::fromStdString((*this->mDirectory / shuffled[ix]).string()); +} + +void FileSelector::onTimeout() { + if (!this->mFiles.size()) + return; + + auto currentTime = std::chrono::system_clock::now(); + uint64_t currentMinorEpoch = currentTime.time_since_epoch() / (*this->mEpoch / this->mFiles.size()); + auto nextTime = std::chrono::time_point((2 * currentMinorEpoch + 3) * (*this->mEpoch / (this->mFiles.size() * 2))); + this->timer.start(std::chrono::duration_cast(nextTime - currentTime).count()); + + emit this->selectedChanged(); +} + +void FileSelector::update() { + this->mFiles = std::set{}; + for (const fs::directory_entry& entry: + fs::recursive_directory_iterator(*this->mDirectory, fs::directory_options::follow_directory_symlink)) + { + this->mFiles.insert(fs::relative(entry, *this->mDirectory)); + } + + this->onTimeout(); +} diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.hpp b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.hpp new file mode 100644 index 00000000..72c4f2a7 --- /dev/null +++ b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +#include + +class FileSelector : public QObject { + Q_OBJECT; + Q_PROPERTY(QString directory READ directory WRITE setDirectory NOTIFY directoryChanged REQUIRED); + Q_PROPERTY(unsigned int epoch READ epoch WRITE setEpoch NOTIFY epochChanged REQUIRED); + Q_PROPERTY(QString seed READ seed WRITE setSeed NOTIFY seedChanged); + Q_PROPERTY(QString selected READ selected NOTIFY selectedChanged); + QML_ELEMENT; + +public: + explicit FileSelector(QObject* parent = nullptr); + + QString directory() const; + void setDirectory(QString directory); + + unsigned int epoch() const; + void setEpoch(unsigned int epoch); + + QString seed() const; + void setSeed(QString seed); + + QString selected() const; + +signals: + void directoryChanged(); + void epochChanged(); + void seedChanged(); + void selectedChanged(); + +private slots: + void onTimeout(); + +private: + std::optional mDirectory; + std::optional mEpoch; + std::set mFiles; + QString mSeed; + QTimer timer; + + void update(); +}; -- cgit v1.2.3