feat(wallpaper): add better wallpaper script

This commit is contained in:
2026-07-16 19:12:02 +01:00
parent a1001e521e
commit 901cfeca0d
2 changed files with 76 additions and 109 deletions

View File

@@ -1,91 +1,44 @@
#!/usr/bin/env bash
# Configuration
WALLPAPER_DIR="$HOME/.wallpapers" # Change this to your wallpaper directory
CACHE_DIR="$HOME/.cache/wallpaper-selector"
THUMBNAIL_WIDTH="250" # Size of thumbnails in pixels (16:9)
THUMBNAIL_HEIGHT="141"
# Create cache directory if it doesn't exist
mkdir -p "$CACHE_DIR"
# wallpaper.sh — pick a wallpaper from ~/.wallpapers via wofi, apply with hyprpaper
set -euo pipefail
DIR="${HOME}/.wallpapers"
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/wallpaper-thumbs"
[[ -d "$DIR" ]] || { notify-send "wallpaper.sh" "No $DIR"; exit 1; }
mkdir -p "$CACHE"
# Function to generate thumbnail
generate_thumbnail() {
local input="$1"
local output="$2"
magick "$input" -thumbnail "${THUMBNAIL_WIDTH}x${THUMBNAIL_HEIGHT}^" -gravity center -extent "${THUMBNAIL_WIDTH}x${THUMBNAIL_HEIGHT}" "$output"
}
mapfile -t files < <(find -L "$DIR" -maxdepth 1 -type f \
\( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.webp' \) \
-printf '%f\n' | LC_ALL=C sort)
[[ ${#files[@]} -gt 0 ]] || { notify-send "wallpaper.sh" "No images found"; exit 1; }
# Create shuffle icon thumbnail on the fly
SHUFFLE_ICON="$CACHE_DIR/shuffle_thumbnail.png"
# Create a properly sized shuffle icon thumbnail
# magick -size "${THUMBNAIL_WIDTH}x${THUMBNAIL_HEIGHT}" xc:#1e1e2e \
# "$HOME/Repos/wallpaper-selector/assets/shuffle.png" -resize "120x120" -gravity center -composite \
# "$SHUFFLE_ICON"
magick -size "${THUMBNAIL_WIDTH}x${THUMBNAIL_HEIGHT}" xc:#1e1e2e \
\( "$HOME/Repos/wallpaper-selector/assets/shuffle.png" -resize "80x80" \) \
-gravity center -composite "$SHUFFLE_ICON"
# Build any missing/stale thumbnails, in parallel.
for f in "${files[@]}"; do
src="${DIR}/${f}"
thumb="${CACHE}/${f%.*}.png"
[[ -f "$thumb" && "$thumb" -nt "$src" ]] && continue
magick "$src" -thumbnail 360x360^ -strip "$thumb" &
done
wait
# Generate thumbnails and create menu items
generate_menu() {
# Add random/shuffle option with a name that sorts first (using ! prefix)
echo -en "img:$SHUFFLE_ICON\x00info:!Random Wallpaper\x1fRANDOM\n"
# Then add all wallpapers
for img in "$WALLPAPER_DIR"/*.{jpg,jpeg,png}; do
# Skip if no matches found
[[ -f "$img" ]] || continue
# Generate thumbnail filename
thumbnail="$CACHE_DIR/$(basename "${img%.*}").png"
# Generate thumbnail if it doesn't exist or is older than source
if [[ ! -f "$thumbnail" ]] || [[ "$img" -nt "$thumbnail" ]]; then
generate_thumbnail "$img" "$thumbnail"
fi
# Output menu item (filename and path)
echo -en "img:$thumbnail\x00info:$(basename "$img")\x1f$img\n"
done
}
RANDOM_LABEL="🎲 Random"
menu="img::text:${RANDOM_LABEL}\n"
for f in "${files[@]}"; do
menu+="img:${CACHE}/${f%.*}.png:text:${f}\n"
done
# Use wofi to display grid of wallpapers - IMPORTANT: added --sort-order=default
selected=$(generate_menu | wofi --show dmenu \
--cache-file /dev/null \
--define "image-size=${THUMBNAIL_WIDTH}x${THUMBNAIL_HEIGHT}" \
--columns 3 \
--allow-images \
--insensitive \
--sort-order=default \
--prompt "Select Wallpaper" \
--conf ~/.config/wofi/wallpaper.conf \
)
line=$(echo -en "$menu" | wofi \
--dmenu --show dmenu --allow-images --insensitive \
--define image_size=180 --columns 4 --prompt "Wallpaper" \
--define sort_order=default --cache-file /dev/null \
--style "${HOME}/.config/wofi/wallpaper.css" \
--width 900 --height 620 --define halign=fill --define content_halign=fill)
[[ -n "$line" ]] || exit 0
# Set wallpaper if one was selected
if [ -n "$selected" ]; then
# Remove the img: prefix to get the cached thumbnail path
thumbnail_path="${selected#img:}"
# Check if random wallpaper was selected
if [[ "$thumbnail_path" == "$SHUFFLE_ICON" ]]; then
# Select a random wallpaper from the directory
original_path=$(find "$WALLPAPER_DIR" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) | shuf -n 1)
else
# Get the original filename from the thumbnail path
original_filename=$(basename "${thumbnail_path%.*}")
# Find the corresponding original file in the wallpaper directory
original_path=$(find "$WALLPAPER_DIR" -type f -name "${original_filename}.*" | head -n1)
fi
# Ensure a valid wallpaper was found before proceeding
if [ -n "$original_path" ]; then
hyprctl hyprpaper wallpaper ",$original_path"
# Save the selection for persistence
echo "$original_path" > "$HOME/.cache/current_wallpaper"
# Optional: Notify user
notify-send "Wallpaper" "Wallpaper has been updated" -i "$original_path"
else
notify-send "Wallpaper Error" "Could not find the original wallpaper file."
fi
if [[ "$line" == *"$RANDOM_LABEL"* ]]; then
WALL="${DIR}/${files[RANDOM % ${#files[@]}]}"
else
choice="${line##*:text:}"
WALL="${DIR}/${choice}"
fi
hyprctl hyprpaper wallpaper ",${WALL}"