diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2025-09-02 20:56:37 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2025-09-02 20:56:37 +0200 |
| commit | cc84ab2289381038f483f06963374aa0247f6724 (patch) | |
| tree | f60dda5bf4efb447a47a5065081ccd9169ccef48 /accounts/gkleen@sif/shell/quickshell-plugins | |
| parent | aff71d6f9f3cc5c9217ee0cb4fa6c66f978e2b36 (diff) | |
| download | nixos-cc84ab2289381038f483f06963374aa0247f6724.tar nixos-cc84ab2289381038f483f06963374aa0247f6724.tar.gz nixos-cc84ab2289381038f483f06963374aa0247f6724.tar.bz2 nixos-cc84ab2289381038f483f06963374aa0247f6724.tar.xz nixos-cc84ab2289381038f483f06963374aa0247f6724.zip | |
...
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins')
4 files changed, 154 insertions, 1 deletions
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 | |||
| 104 | 104 | ||
| 105 | target_sources(customplugin PRIVATE | 105 | target_sources(customplugin PRIVATE |
| 106 | Chrono.cpp Chrono.hpp | 106 | Chrono.cpp Chrono.hpp |
| 107 | FileSelector.cpp FileSelector.hpp | ||
| 107 | ) | 108 | ) |
| 108 | 109 | ||
| 109 | target_compile_features(customplugin PUBLIC cxx_std_26) | 110 | 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 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include <chrono> | ||
| 4 | |||
| 3 | #include <QDateTime> | 5 | #include <QDateTime> |
| 4 | #include <QObject> | 6 | #include <QObject> |
| 5 | #include <QTimer> | 7 | #include <QTimer> |
| 6 | 8 | ||
| 7 | #include <qqmlintegration.h> | 9 | #include <qqmlintegration.h> |
| 8 | #include <chrono> | ||
| 9 | 10 | ||
| 10 | class Chrono : public QObject { | 11 | class Chrono : public QObject { |
| 11 | Q_OBJECT; | 12 | 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 @@ | |||
| 1 | #include "FileSelector.hpp" | ||
| 2 | |||
| 3 | #include <sstream> | ||
| 4 | #include <vector> | ||
| 5 | #include <random> | ||
| 6 | #include <algorithm> | ||
| 7 | |||
| 8 | #include <iostream> | ||
| 9 | #include <format> | ||
| 10 | |||
| 11 | namespace fs = std::filesystem; | ||
| 12 | |||
| 13 | FileSelector::FileSelector(QObject* parent): QObject(parent) { | ||
| 14 | QObject::connect(&this->timer, &QTimer::timeout, this, &FileSelector::onTimeout); | ||
| 15 | } | ||
| 16 | |||
| 17 | QString FileSelector::directory() const { | ||
| 18 | return QString::fromStdString(this->mDirectory->string()); | ||
| 19 | } | ||
| 20 | void FileSelector::setDirectory(QString directory) { | ||
| 21 | this->mDirectory = directory.toStdString(); | ||
| 22 | if (this->mDirectory && this->mEpoch) | ||
| 23 | this->update(); | ||
| 24 | emit this->directoryChanged(); | ||
| 25 | } | ||
| 26 | |||
| 27 | unsigned int FileSelector::epoch() const { | ||
| 28 | return std::chrono::duration_cast<std::chrono::milliseconds>(*this->mEpoch).count(); | ||
| 29 | } | ||
| 30 | void FileSelector::setEpoch(unsigned int epoch) { | ||
| 31 | this->mEpoch = std::chrono::milliseconds{epoch}; | ||
| 32 | if (this->mDirectory && this->mEpoch) | ||
| 33 | this->update(); | ||
| 34 | emit this->epochChanged(); | ||
| 35 | } | ||
| 36 | |||
| 37 | QString FileSelector::seed() const { | ||
| 38 | return this->mSeed; | ||
| 39 | } | ||
| 40 | void FileSelector::setSeed(QString seed) { | ||
| 41 | this->mSeed = seed; | ||
| 42 | emit this->seedChanged(); | ||
| 43 | } | ||
| 44 | |||
| 45 | QString FileSelector::selected() const { | ||
| 46 | if (!this->mDirectory || !this->mEpoch) | ||
| 47 | return QString(); | ||
| 48 | |||
| 49 | std::vector<fs::path> shuffled(this->mFiles.begin(), this->mFiles.end()); | ||
| 50 | std::sort(shuffled.begin(), shuffled.end()); | ||
| 51 | |||
| 52 | auto currentTime = std::chrono::system_clock::now(); | ||
| 53 | uint64_t currentEpoch = currentTime.time_since_epoch() / *this->mEpoch; | ||
| 54 | std::chrono::milliseconds timeInEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime.time_since_epoch()) % *this->mEpoch; | ||
| 55 | |||
| 56 | std::ostringstream seed; | ||
| 57 | seed << this->mSeed.size() << ";" << this->mSeed.toStdString() << ";"; | ||
| 58 | seed << *this->mEpoch << ";"; | ||
| 59 | seed << currentEpoch << ";"; | ||
| 60 | seed << this->mDirectory->string().size() << ";" << *this->mDirectory << ";"; | ||
| 61 | seed << this->mFiles.size() << ";"; | ||
| 62 | for (const fs::path& p: this->mFiles) | ||
| 63 | seed << p.string().size() << ";" << p << ";"; | ||
| 64 | |||
| 65 | std::vector<std::seed_seq::result_type> v; | ||
| 66 | v.reserve(seed.str().size()); | ||
| 67 | for (const char& c: seed.str()) | ||
| 68 | v.push_back(c); | ||
| 69 | |||
| 70 | std::seed_seq engine_seed(v.begin(), v.end()); | ||
| 71 | std::mt19937 g(engine_seed); | ||
| 72 | std::shuffle(shuffled.begin(), shuffled.end(), g); | ||
| 73 | |||
| 74 | std::vector<fs::path>::size_type ix = shuffled.size() * timeInEpoch / *this->mEpoch; | ||
| 75 | return QString::fromStdString((*this->mDirectory / shuffled[ix]).string()); | ||
| 76 | } | ||
| 77 | |||
| 78 | void FileSelector::onTimeout() { | ||
| 79 | if (!this->mFiles.size()) | ||
| 80 | return; | ||
| 81 | |||
| 82 | auto currentTime = std::chrono::system_clock::now(); | ||
| 83 | uint64_t currentMinorEpoch = currentTime.time_since_epoch() / (*this->mEpoch / this->mFiles.size()); | ||
| 84 | auto nextTime = std::chrono::time_point<std::chrono::system_clock>((2 * currentMinorEpoch + 3) * (*this->mEpoch / (this->mFiles.size() * 2))); | ||
| 85 | this->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime).count()); | ||
| 86 | |||
| 87 | emit this->selectedChanged(); | ||
| 88 | } | ||
| 89 | |||
| 90 | void FileSelector::update() { | ||
| 91 | this->mFiles = std::set<fs::path>{}; | ||
| 92 | for (const fs::directory_entry& entry: | ||
| 93 | fs::recursive_directory_iterator(*this->mDirectory, fs::directory_options::follow_directory_symlink)) | ||
| 94 | { | ||
| 95 | this->mFiles.insert(fs::relative(entry, *this->mDirectory)); | ||
| 96 | } | ||
| 97 | |||
| 98 | this->onTimeout(); | ||
| 99 | } | ||
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 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <filesystem> | ||
| 4 | #include <chrono> | ||
| 5 | #include <set> | ||
| 6 | #include <optional> | ||
| 7 | |||
| 8 | #include <QObject> | ||
| 9 | #include <QTimer> | ||
| 10 | |||
| 11 | #include <qqmlintegration.h> | ||
| 12 | |||
| 13 | class FileSelector : public QObject { | ||
| 14 | Q_OBJECT; | ||
| 15 | Q_PROPERTY(QString directory READ directory WRITE setDirectory NOTIFY directoryChanged REQUIRED); | ||
| 16 | Q_PROPERTY(unsigned int epoch READ epoch WRITE setEpoch NOTIFY epochChanged REQUIRED); | ||
| 17 | Q_PROPERTY(QString seed READ seed WRITE setSeed NOTIFY seedChanged); | ||
| 18 | Q_PROPERTY(QString selected READ selected NOTIFY selectedChanged); | ||
| 19 | QML_ELEMENT; | ||
| 20 | |||
| 21 | public: | ||
| 22 | explicit FileSelector(QObject* parent = nullptr); | ||
| 23 | |||
| 24 | QString directory() const; | ||
| 25 | void setDirectory(QString directory); | ||
| 26 | |||
| 27 | unsigned int epoch() const; | ||
| 28 | void setEpoch(unsigned int epoch); | ||
| 29 | |||
| 30 | QString seed() const; | ||
| 31 | void setSeed(QString seed); | ||
| 32 | |||
| 33 | QString selected() const; | ||
| 34 | |||
| 35 | signals: | ||
| 36 | void directoryChanged(); | ||
| 37 | void epochChanged(); | ||
| 38 | void seedChanged(); | ||
| 39 | void selectedChanged(); | ||
| 40 | |||
| 41 | private slots: | ||
| 42 | void onTimeout(); | ||
| 43 | |||
| 44 | private: | ||
| 45 | std::optional<std::filesystem::path> mDirectory; | ||
| 46 | std::optional<std::chrono::milliseconds> mEpoch; | ||
| 47 | std::set<std::filesystem::path> mFiles; | ||
| 48 | QString mSeed; | ||
| 49 | QTimer timer; | ||
| 50 | |||
| 51 | void update(); | ||
| 52 | }; | ||
