Skip to content

Commit 0ca403a

Browse files
authored
config/ui: add random-shuffle slideshow order (#352)
* feat: Add random-shuffle wallpaper order Add a 'wallpaper#order' value, 'random-shuffle', that reshuffles when the slideshow wraps so each cycle shows every image once in a new order.
1 parent 51e497d commit 0ca403a

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/config/ConfigManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,19 @@ std::vector<CConfigManager::SSetting> CConfigManager::getSettings() {
203203
auto resolvedPaths = RESOLVE_PATH.value();
204204

205205
if (resolvedPaths.size() > 1) {
206-
if (order != "default" && order != "random") {
206+
if (order != "default" && order != "random" && order != "random-shuffle") {
207207
g_logger->log(LOG_WARN, "Invalid order value '{}', falling back to default", order);
208208
order = "default";
209209
}
210210

211-
if (order == "random") {
211+
if (order == "random" || order == "random-shuffle") {
212212
std::random_device rd;
213213
std::mt19937 g(rd());
214214
std::shuffle(resolvedPaths.begin(), resolvedPaths.end(), g);
215215
}
216216
}
217217

218-
result.emplace_back(SSetting{.monitor = std::move(monitor), .fitMode = std::move(fitMode), .paths = std::move(resolvedPaths), .timeout = timeout});
218+
result.emplace_back(SSetting{.monitor = std::move(monitor), .fitMode = std::move(fitMode), .paths = std::move(resolvedPaths), .order = std::move(order), .timeout = timeout});
219219
}
220220

221221
return result;

src/config/ConfigManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CConfigManager {
1616
struct SSetting {
1717
std::string monitor, fitMode;
1818
std::vector<std::string> paths;
19+
std::string order = "default";
1920
int timeout = 0;
2021
uint32_t id = 0;
2122
};

src/ui/UI.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "../ipc/IPC.hpp"
77
#include "../config/WallpaperMatcher.hpp"
88

9+
#include <algorithm>
10+
#include <random>
911
#include <hyprtoolkit/core/Output.hpp>
1012

1113
#include <hyprutils/string/String.hpp>
@@ -24,14 +26,23 @@ static std::string_view pruneDesc(const std::string_view& sv) {
2426

2527
class CWallpaperTarget::CImagesData {
2628
public:
27-
CImagesData(Hyprtoolkit::eImageFitMode fitMode, const std::vector<std::string>& images, const int timeout = 0) :
28-
fitMode(fitMode), images(images), timeout(timeout > 0 ? timeout : 30) {}
29+
CImagesData(Hyprtoolkit::eImageFitMode fitMode, std::vector<std::string> images, const int timeout = 0, std::string order = "default") :
30+
fitMode(fitMode), images(std::move(images)), order(std::move(order)), timeout(timeout > 0 ? timeout : 30) {}
2931

3032
const Hyprtoolkit::eImageFitMode fitMode;
31-
const std::vector<std::string> images;
33+
std::vector<std::string> images;
34+
const std::string order;
3235
const int timeout;
3336

3437
std::string nextImage() {
38+
if (order == "random-shuffle" && current + 1 >= images.size()) {
39+
std::random_device rd;
40+
std::mt19937 g(rd());
41+
std::shuffle(images.begin(), images.end(), g);
42+
current = 0;
43+
return images[current];
44+
}
45+
3546
current = (current + 1) % images.size();
3647
return images[current];
3748
}
@@ -41,7 +52,7 @@ class CWallpaperTarget::CImagesData {
4152
};
4253

4354
CWallpaperTarget::CWallpaperTarget(SP<Hyprtoolkit::IBackend> backend, SP<Hyprtoolkit::IOutput> output, const std::vector<std::string>& path, Hyprtoolkit::eImageFitMode fitMode,
44-
const int timeout) : m_monitorName(output->port()), m_backend(backend) {
55+
const int timeout, const std::string& order) : m_monitorName(output->port()), m_backend(backend) {
4556
static const auto SPLASH_REPLY = HyprlandSocket::getFromSocket("/splash");
4657

4758
static const auto PENABLESPLASH = Hyprlang::CSimpleConfigValue<Hyprlang::INT>(g_config->hyprlang(), "splash");
@@ -79,7 +90,7 @@ CWallpaperTarget::CWallpaperTarget(SP<Hyprtoolkit::IBackend> backend, SP<Hyprtoo
7990
m_image->setPositionFlag(Hyprtoolkit::IElement::HT_POSITION_FLAG_CENTER, true);
8091

8192
if (path.size() > 1) {
82-
m_imagesData = makeUnique<CImagesData>(fitMode, path, timeout);
93+
m_imagesData = makeUnique<CImagesData>(fitMode, std::vector<std::string>(path), timeout, order);
8394
m_timer =
8495
m_backend->addTimer(std::chrono::milliseconds(std::chrono::seconds(m_imagesData->timeout)), [this](ASP<Hyprtoolkit::CTimer> self, void*) { onRepeatTimer(); }, nullptr);
8596
}
@@ -228,7 +239,7 @@ void CUI::targetChanged(const SP<Hyprtoolkit::IOutput>& mon) {
228239

229240
std::erase_if(m_targets, [&mon](const auto& e) { return e->m_monitorName == mon->port(); });
230241

231-
m_targets.emplace_back(makeShared<CWallpaperTarget>(m_backend, mon, TARGET->get().paths, toFitMode(TARGET->get().fitMode), TARGET->get().timeout));
242+
m_targets.emplace_back(makeShared<CWallpaperTarget>(m_backend, mon, TARGET->get().paths, toFitMode(TARGET->get().fitMode), TARGET->get().timeout, TARGET->get().order));
232243
}
233244

234245
const std::vector<SP<CWallpaperTarget>>& CUI::targets() {

src/ui/UI.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class CWallpaperTarget {
1818
public:
1919
CWallpaperTarget(SP<Hyprtoolkit::IBackend> backend, SP<Hyprtoolkit::IOutput> output, const std::vector<std::string>& path,
20-
Hyprtoolkit::eImageFitMode fitMode = Hyprtoolkit::IMAGE_FIT_MODE_COVER, const int timeout = 0);
20+
Hyprtoolkit::eImageFitMode fitMode = Hyprtoolkit::IMAGE_FIT_MODE_COVER, const int timeout = 0, const std::string& order = "default");
2121
~CWallpaperTarget();
2222

2323
CWallpaperTarget(const CWallpaperTarget&) = delete;

0 commit comments

Comments
 (0)