diff options
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins')
5 files changed, 176 insertions, 28 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.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp index 929b7be6..22b3469b 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.cpp | |||
@@ -42,9 +42,11 @@ void Chrono::update() { | |||
42 | } | 42 | } |
43 | 43 | ||
44 | void Chrono::setTime(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { | 44 | void Chrono::setTime(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { |
45 | using namespace std::chrono_literals; | ||
46 | |||
45 | auto currentTime = std::chrono::system_clock::now(); | 47 | auto currentTime = std::chrono::system_clock::now(); |
46 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); | 48 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); |
47 | this->currentTime = abs(offset.count()) < 500 ? targetTime : currentTime; | 49 | this->currentTime = abs(offset) < 500ms ? targetTime : currentTime; |
48 | 50 | ||
49 | switch (this->mPrecision) { | 51 | switch (this->mPrecision) { |
50 | case Chrono::Hours: this->currentTime = std::chrono::time_point_cast<std::chrono::hours>(this->currentTime); | 52 | case Chrono::Hours: this->currentTime = std::chrono::time_point_cast<std::chrono::hours>(this->currentTime); |
@@ -56,33 +58,26 @@ void Chrono::setTime(const std::chrono::time_point<std::chrono::system_clock>& t | |||
56 | } | 58 | } |
57 | 59 | ||
58 | void Chrono::schedule(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { | 60 | void Chrono::schedule(const std::chrono::time_point<std::chrono::system_clock>& targetTime) { |
61 | using namespace std::chrono_literals; | ||
62 | |||
59 | auto currentTime = std::chrono::system_clock::now(); | 63 | auto currentTime = std::chrono::system_clock::now(); |
60 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); | 64 | auto offset = std::chrono::duration_cast<std::chrono::milliseconds>(targetTime - currentTime); |
61 | auto nextTime = abs(offset.count()) < 500 ? targetTime : currentTime; | 65 | auto nextTime = abs(offset) < 500ms ? targetTime : currentTime; |
62 | |||
63 | { | ||
64 | using namespace std::chrono_literals; | ||
65 | 66 | ||
66 | switch (this->mPrecision) { | 67 | switch (this->mPrecision) { |
67 | case Chrono::Hours: nextTime = std::chrono::time_point_cast<std::chrono::hours>(nextTime) + 1h; | 68 | case Chrono::Hours: nextTime = std::chrono::time_point_cast<std::chrono::hours>(nextTime) + 1h; |
68 | case Chrono::Minutes: nextTime = std::chrono::time_point_cast<std::chrono::minutes>(nextTime) + 1min; | 69 | case Chrono::Minutes: nextTime = std::chrono::time_point_cast<std::chrono::minutes>(nextTime) + 1min; |
69 | case Chrono::Seconds: nextTime = std::chrono::time_point_cast<std::chrono::seconds>(nextTime) + 1s; | 70 | case Chrono::Seconds: nextTime = std::chrono::time_point_cast<std::chrono::seconds>(nextTime) + 1s; |
70 | } | ||
71 | } | 71 | } |
72 | 72 | ||
73 | this->targetTime = nextTime; | 73 | this->targetTime = nextTime; |
74 | auto delay = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime); | 74 | this->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime)); |
75 | this->timer.start(delay); | ||
76 | } | 75 | } |
77 | 76 | ||
78 | QString Chrono::format() const { return this->mFormat; } | 77 | QString Chrono::format(const QString& fmt) const { |
79 | void Chrono::setFormat(QString format) { | 78 | return QString::fromStdString(std::format(std::runtime_format(fmt.toStdString()), std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::time_point_cast<std::chrono::seconds>(this->currentTime)))); |
80 | if (format == this->mFormat) return; | ||
81 | this->mFormat = format; | ||
82 | emit this->formatChanged(); | ||
83 | this->update(); | ||
84 | } | 79 | } |
85 | 80 | ||
86 | QString Chrono::date() const { | 81 | QDateTime Chrono::date() const { |
87 | return QString::fromStdString(std::format(std::runtime_format(this->mFormat.toStdString()), std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::time_point_cast<std::chrono::seconds>(this->currentTime)))); | 82 | return QDateTime::fromStdTimePoint(std::chrono::time_point_cast<std::chrono::milliseconds>(this->currentTime)); |
88 | } | 83 | } |
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp index 788fa88e..04080187 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp | |||
@@ -1,17 +1,18 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include <chrono> | ||
4 | |||
5 | #include <QDateTime> | ||
3 | #include <QObject> | 6 | #include <QObject> |
4 | #include <QTimer> | 7 | #include <QTimer> |
5 | 8 | ||
6 | #include <qqmlintegration.h> | 9 | #include <qqmlintegration.h> |
7 | #include <chrono> | ||
8 | 10 | ||
9 | class Chrono : public QObject { | 11 | class Chrono : public QObject { |
10 | Q_OBJECT; | 12 | Q_OBJECT; |
11 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged); | 13 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged); |
12 | Q_PROPERTY(Chrono::Precision precision READ precision WRITE setPrecision NOTIFY precisionChanged); | 14 | Q_PROPERTY(Chrono::Precision precision READ precision WRITE setPrecision NOTIFY precisionChanged); |
13 | Q_PROPERTY(QString format READ format WRITE setFormat NOTIFY formatChanged); | 15 | Q_PROPERTY(QDateTime date READ date NOTIFY dateChanged) |
14 | Q_PROPERTY(QString date READ date NOTIFY dateChanged); | ||
15 | QML_ELEMENT; | 16 | QML_ELEMENT; |
16 | 17 | ||
17 | public: | 18 | public: |
@@ -30,15 +31,13 @@ public: | |||
30 | Chrono::Precision precision() const; | 31 | Chrono::Precision precision() const; |
31 | void setPrecision(Chrono::Precision precision); | 32 | void setPrecision(Chrono::Precision precision); |
32 | 33 | ||
33 | QString format() const; | 34 | Q_INVOKABLE QString format(const QString& fmt) const; |
34 | void setFormat (QString format); | ||
35 | 35 | ||
36 | QString date() const; | 36 | QDateTime date() const; |
37 | 37 | ||
38 | signals: | 38 | signals: |
39 | void enabledChanged(); | 39 | void enabledChanged(); |
40 | void precisionChanged(); | 40 | void precisionChanged(); |
41 | void formatChanged(); | ||
42 | void dateChanged(); | 41 | void dateChanged(); |
43 | 42 | ||
44 | private slots: | 43 | private slots: |
@@ -47,7 +46,6 @@ private slots: | |||
47 | private: | 46 | private: |
48 | bool mEnabled = true; | 47 | bool mEnabled = true; |
49 | Chrono::Precision mPrecision = Chrono::Seconds; | 48 | Chrono::Precision mPrecision = Chrono::Seconds; |
50 | QString mFormat = "{:%c}"; | ||
51 | QTimer timer; | 49 | QTimer timer; |
52 | std::chrono::time_point<std::chrono::system_clock> currentTime, targetTime; | 50 | std::chrono::time_point<std::chrono::system_clock> currentTime, targetTime; |
53 | 51 | ||
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..d7051d2a --- /dev/null +++ b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp | |||
@@ -0,0 +1,102 @@ | |||
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 | this->timer.setTimerType(Qt::PreciseTimer); | ||
16 | } | ||
17 | |||
18 | QString FileSelector::directory() const { | ||
19 | return QString::fromStdString(this->mDirectory->string()); | ||
20 | } | ||
21 | void FileSelector::setDirectory(QString directory) { | ||
22 | this->mDirectory = directory.toStdString(); | ||
23 | if (this->mDirectory && this->mEpoch) | ||
24 | this->update(); | ||
25 | emit this->directoryChanged(); | ||
26 | } | ||
27 | |||
28 | unsigned int FileSelector::epoch() const { | ||
29 | return std::chrono::duration_cast<std::chrono::milliseconds>(*this->mEpoch).count(); | ||
30 | } | ||
31 | void FileSelector::setEpoch(unsigned int epoch) { | ||
32 | this->mEpoch = std::chrono::milliseconds{epoch}; | ||
33 | if (this->mDirectory && this->mEpoch) | ||
34 | this->update(); | ||
35 | emit this->epochChanged(); | ||
36 | } | ||
37 | |||
38 | QString FileSelector::seed() const { | ||
39 | return this->mSeed; | ||
40 | } | ||
41 | void FileSelector::setSeed(QString seed) { | ||
42 | this->mSeed = seed; | ||
43 | emit this->seedChanged(); | ||
44 | if (this->mDirectory && this->mEpoch) | ||
45 | emit this->selectedChanged(); | ||
46 | } | ||
47 | |||
48 | QString FileSelector::selected() const { | ||
49 | if (!this->mDirectory || !this->mEpoch) | ||
50 | return QString(); | ||
51 | |||
52 | std::vector<fs::path> shuffled(this->mFiles.begin(), this->mFiles.end()); | ||
53 | std::sort(shuffled.begin(), shuffled.end()); | ||
54 | |||
55 | auto currentTime = std::chrono::system_clock::now(); | ||
56 | uint64_t currentEpoch = currentTime.time_since_epoch() / *this->mEpoch; | ||
57 | std::chrono::milliseconds timeInEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime.time_since_epoch()) % *this->mEpoch; | ||
58 | |||
59 | std::ostringstream seed; | ||
60 | seed << this->mSeed.size() << ";" << this->mSeed.toStdString() << ";"; | ||
61 | seed << *this->mEpoch << ";"; | ||
62 | seed << currentEpoch << ";"; | ||
63 | seed << this->mDirectory->string().size() << ";" << *this->mDirectory << ";"; | ||
64 | seed << this->mFiles.size() << ";"; | ||
65 | for (const fs::path& p: this->mFiles) | ||
66 | seed << p.string().size() << ";" << p << ";"; | ||
67 | |||
68 | std::vector<std::seed_seq::result_type> v; | ||
69 | v.reserve(seed.str().size()); | ||
70 | for (const char& c: seed.str()) | ||
71 | v.push_back(c); | ||
72 | |||
73 | std::seed_seq engine_seed(v.begin(), v.end()); | ||
74 | std::mt19937 g(engine_seed); | ||
75 | std::shuffle(shuffled.begin(), shuffled.end(), g); | ||
76 | |||
77 | std::vector<fs::path>::size_type ix = shuffled.size() * timeInEpoch / *this->mEpoch; | ||
78 | return QString::fromStdString((*this->mDirectory / shuffled[ix]).string()); | ||
79 | } | ||
80 | |||
81 | void FileSelector::onTimeout() { | ||
82 | if (!this->mFiles.size()) | ||
83 | return; | ||
84 | |||
85 | auto currentTime = std::chrono::system_clock::now(); | ||
86 | uint64_t currentMinorEpoch = currentTime.time_since_epoch() / (*this->mEpoch / this->mFiles.size()); | ||
87 | auto nextTime = std::chrono::time_point<std::chrono::system_clock>((currentMinorEpoch + 1) * (*this->mEpoch / this->mFiles.size())); | ||
88 | this->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime)); | ||
89 | |||
90 | emit this->selectedChanged(); | ||
91 | } | ||
92 | |||
93 | void FileSelector::update() { | ||
94 | this->mFiles = std::set<fs::path>{}; | ||
95 | for (const fs::directory_entry& entry: | ||
96 | fs::recursive_directory_iterator(*this->mDirectory, fs::directory_options::follow_directory_symlink)) | ||
97 | { | ||
98 | this->mFiles.insert(fs::relative(entry, *this->mDirectory)); | ||
99 | } | ||
100 | |||
101 | this->onTimeout(); | ||
102 | } | ||
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 | }; | ||