| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | source "$(dirname "$0")/_lib.sh" |
| 6 | |
| 7 | state="${XDG_STATE_HOME:-$HOME/.local/state}/dotfiles/wallpaper" |
| 8 | mkdir -p "$(dirname "$state")" |
| 9 | fallback_color='#1e1e2e' |
| 10 | |
| 11 | dirs=("${WALLPAPER_DIR:-$HOME/Pictures/Wallpapers}") |
| 12 | repo_walls="$(dirname "$(readlink -f "$HOME/.config/sway")")/../wallpapers" |
| 13 | [[ -d $repo_walls ]] && dirs+=("$(readlink -f "$repo_walls")") |
| 14 | |
| 15 | list_walls() { |
| 16 | find "${dirs[@]}" -maxdepth 2 -type f \ |
| 17 | \( -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.webp' \) 2>/dev/null |
| 18 | } |
| 19 | |
| 20 | apply() { |
| 21 | local img=$1 |
| 22 | pkill -x swaybg 2>/dev/null |
| 23 | if [[ -f $img ]]; then |
| 24 | echo "$img" > "$state" |
| 25 | swaybg -m fill -i "$img" >/dev/null 2>&1 & |
| 26 | else |
| 27 | swaybg -c "$fallback_color" >/dev/null 2>&1 & |
| 28 | fi |
| 29 | disown |
| 30 | } |
| 31 | |
| 32 | case ${1:-restore} in |
| 33 | restore) |
| 34 | img=$(cat "$state" 2>/dev/null) |
| 35 | [[ -f $img ]] || img=$(list_walls | head -n1) |
| 36 | apply "$img" ;; |
| 37 | random) |
| 38 | img=$(list_walls | shuf -n1) |
| 39 | apply "$img" |
| 40 | [[ -n $img ]] && notify wall preferences-desktop-wallpaper "Wallpaper" "$(basename "$img")" ;; |
| 41 | pick) |
| 42 | img=$(list_walls | sort | menu "Wallpaper" --width 700 --height 400) || exit 0 |
| 43 | apply "$img" ;; |
| 44 | set) |
| 45 | apply "$(readlink -f "$2")" ;; |
| 46 | *) echo "usage: $0 restore|random|pick|set <file>"; exit 1 ;; |
| 47 | esac |