summaryrefslogtreecommitdiffstats
path: root/roles/hyprtheme/files/scripts/theme-bg-next
blob: e0edb2a33f9e890d3784463046d3ff843c434bb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash

# Cycles through the background images available

THEMES_PATH="$HOME/.config/hyprtheme/"
THEME_BACKGROUNDS_PATH="$THEMES_PATH/current/backgrounds/"
CURRENT_BACKGROUND_LINK="$THEMES_PATH/current/background"

mapfile -d '' -t BACKGROUNDS < <(find -L "$THEME_BACKGROUNDS_PATH" -maxdepth 1 -type f -print0 2>/dev/null | sort -z)
TOTAL=${#BACKGROUNDS[@]}

if [[ $TOTAL -eq 0 ]]; then
  notify-send "No background was found for theme" -t 2000
  pkill -x swaybg
  setsid uwsm-app -- swaybg --color '#000000' >/dev/null 2>&1 &
else
  # Get current background from symlink
  if [[ -L "$CURRENT_BACKGROUND_LINK" ]]; then
    CURRENT_BACKGROUND=$(readlink "$CURRENT_BACKGROUND_LINK")
  else
    # Default to first background if no symlink exists
    CURRENT_BACKGROUND=""
  fi

  # Find current background index
  INDEX=-1
  for i in "${!BACKGROUNDS[@]}"; do
    if [[ "${BACKGROUNDS[$i]}" == "$CURRENT_BACKGROUND" ]]; then
      INDEX=$i
      break
    fi
  done

  # Get next background (wrap around)
  if [[ $INDEX -eq -1 ]]; then
    # Use the first background when no match was found
    NEW_BACKGROUND="${BACKGROUNDS[0]}"
  else
    NEXT_INDEX=$(((INDEX + 1) % TOTAL))
    NEW_BACKGROUND="${BACKGROUNDS[$NEXT_INDEX]}"
  fi

  # Set new background symlink
  ln -nsf "$NEW_BACKGROUND" "$CURRENT_BACKGROUND_LINK"

  # Relaunch swaybg
  pkill -x swaybg
  setsid uwsm-app -- swaybg -i "$CURRENT_BACKGROUND_LINK" -m fill >/dev/null 2>&1 &
fi