anufrievroman / waypaper

GUI wallpaper manager for Wayland and Xorg Linux systems
https://anufrievroman.gitbook.io/waypaper
GNU General Public License v3.0
355 stars 21 forks source link

Feature: Prevent random selection of already applied wallpaper until no more are left #78

Open jude7733 opened 3 weeks ago

jude7733 commented 3 weeks ago

Issue

Currently, Waypaper selects random wallpapers from the configured directories. However, it does not keep track of previously used wallpapers. This can lead to the same wallpapers being used repeatedly before all available wallpapers have been cycled through. This issue proposes adding a feature to Waypaper to avoid using already selected random wallpapers until no more unique wallpapers are left in the configured directories. This will ensure that all available wallpapers are used before repeating any.

Proposed Solution

Actual Behavior

I used to do this with a bash script before using waypaper

#!/bin/bash

# Define the directory containing wallpapers
directory=~/Pictures/wallpapers
# Define a file to keep track of used wallpapers
used_wallpapers_file=~/Pictures/wallpapers/.used_wallpapers.txt

# Create the used wallpapers file if it doesn't exist
if [ ! -f "$used_wallpapers_file" ]; then
    touch "$used_wallpapers_file"
fi

# Check if the directory exists
if [ -d "$directory" ]; then
    # Get a list of all wallpapers
    all_wallpapers=($(ls $directory/*.jpg $directory/*.png))

    # Filter out wallpapers that have already been used
    available_wallpapers=()
    for wallpaper in "${all_wallpapers[@]}"; do
        if ! grep -qx "$wallpaper" "$used_wallpapers_file"; then
            available_wallpapers+=("$wallpaper")
        fi
    done

    # If there are available wallpapers, select one randomly
    if [ ${#available_wallpapers[@]} -gt 0 ]; then
        random_background=$(printf "%s\n" "${available_wallpapers[@]}" | shuf -n 1)

        # Unload and set the new wallpaper
        # hyprctl hyprpaper unload all && sleep 1 &&
        # hyprctl hyprpaper preload "$random_background" &&
        # hyprctl hyprpaper wallpaper "VGA-1, $random_background" &&
        echo "Setting wallpaper to $random_background" 
        swww img "$random_background" --transition-type outer &
        wal -i "$random_background"
        pkill waybar && waybar &

        # Add the selected wallpaper to the used file
        echo "$random_background" >> "$used_wallpapers_file"
    else
        # Reset the used wallpapers file
        > "$used_wallpapers_file"
    fi
else
    echo "Directory $directory does not exist."
fi
anufrievroman commented 3 weeks ago

Hi, thank you for the suggestion. Currently, I don't know if I'll have time to work on that soon, but if someone wants to implement this, I suppose we could maintain a file used_wallpapers of already used wallpaper somewhere next to all the cache for example, and when we do get_random_file() function from common.py module, and inside it, we get image_paths variable, we should contrast it to that list from used_wallpapers, and then return random from the list of what remains. If nothing remains, reset used_wallpapers and return a random choice from image_paths.