Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add directory wallpaper support (and subsequently shuffling) and deferred preloading #237

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

y0usaf
Copy link

@y0usaf y0usaf commented Feb 4, 2025

Hi all :)
This PR adds two new features:

  1. Directory Wallpaper Support
  • Users can now specify a directory path in the wallpaper config
  • Hyprpaper will randomly select a valid image from the directory
  • Avoids selecting the same image consecutively when possible
  1. Deferred Preloading
  • New defer_preload config option to reduce memory usage
  • Wallpapers are loaded on-demand instead of all at startup
  • Useful for systems with limited memory

Additional improvements:

  • Better path handling with tilde expansion
  • Updated README with new features

Feel free to leave feedback or lampoon as it's obviously not a small change, I saw #194 and wanted to do something about it. I'll make revisions as per feedback, candidly I'm unfamiliar with the Hypr suite on the dev end (but am a very grateful user!) 🙇

- Add support for selecting random wallpapers from directories
- Add defer_preload option to reduce memory usage
- Update README with new features
- Improve path handling with tilde expansion
@y0usaf y0usaf marked this pull request as ready for review February 4, 2025 14:47
- Replace <random> with helpers/RandomGenerator.hpp for consistent random number generation
- Improve buffer target handling by using path directly instead of dereferencing wallpaper target
- Simplify lambda capture in ensurePoolBuffersPresent() by using reference capture
@y0usaf y0usaf changed the title feat: add directory wallpaper support and deferred preloading feat: add directory wallpaper support (and subsequently shuffling) and deferred preloading Feb 4, 2025
Introduces a new ImagePicker helper that consolidates duplicate wallpaper
selection logic from Hyprpaper.cpp and ConfigManager.cpp into a single
reusable function.

- Add new ImagePicker.{hpp,cpp} with getRandomImageFromDirectory() helper
- Function handles directory scanning, file extension validation, and random selection
- Supports excluding current wallpaper to avoid duplicate selections
- Updates both wallpaper loading code paths to use the new helper
- Uses std::shuffle instead of random index for better randomization

This change improves code maintainability by removing duplicate logic and
centralizing image selection behavior in a single location.
std::string getRandomImageFromDirectory(const std::string &dirPath, const std::string &ignore) {
std::vector<std::filesystem::path> images;
for (const auto &entry : std::filesystem::directory_iterator(dirPath)) {
if (entry.is_regular_file()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer guards

if (!entry.isregular())
    continue;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bf38a16

🙇

for (const auto &entry : std::filesystem::directory_iterator(dirPath)) {
if (entry.is_regular_file()) {
auto ext = entry.path().extension().string();
if ((ext == ".png" || ext == ".jpg" || ext == ".jpeg" ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a compromise... and a poor one imo. hyprgraphics uses libmagic to try to load images.

Maybe you want to add a fn to hyprgraphics that says if an image is loadable by it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this, candidly I implemented it due to me just wanting to get my fork working and it's been fine as-is, but I recognize that we need something more robust.

auto ext = entry.path().extension().string();
if ((ext == ".png" || ext == ".jpg" || ext == ".jpeg" ||
ext == ".webp" || ext == ".jxl")) {
if (ignore.empty() || entry.path().string() != ignore) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no {}

return "";
}

std::shuffle(images.begin(), images.end(), CRandomGenerator::get().getGenerator());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? Why not just images.at(randomInt)

}

if (!g_pHyprpaper->isPreloaded(WALLPAPER)) {
if (std::any_cast<Hyprlang::INT>(g_pConfigManager->config->getConfigValue("defer_preload"))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static auto PRENDERSPLASH = Hyprlang::CSimpleConfigValue<Hyprlang::INT>(g_pConfigManager->config.get(), "splash");

Flattened nested conditionals into guard clauses for better readability.
@GscheadaHamme
Copy link

A great addition would be the option to set the currently loaded wallpaper as "default wallpaper", so that the change is persistent even after restart.

@y0usaf
Copy link
Author

y0usaf commented Feb 9, 2025

I'm happy to take a stab at that! Bear with me as I'm travelling etc. soon but I hope to pick up the requisite knowledge needed to get this through the door in that time.

A great addition would be the option to set the currently loaded wallpaper as "default wallpaper", so that the change is persistent even after restart.

On a personal level I'm fine with having more potential configurations, I'll leave it to the Hypr maintainers if they have opposing opinions 🫡

@GscheadaHamme
Copy link

On a personal level I'm fine with having more potential configurations, I'll leave it to the Hypr maintainers if they have opposing opinions 🫡

You're right, I'm sorry for hijacking this thread. I'll post a feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants