summaryrefslogtreecommitdiff
path: root/mac/.config/sketchybar/plugins/volume.sh
blob: 6135b1f4462ed762c88b0d11017ff5b44dfec71f (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash

# Source colors for consistent theming
source "$CONFIG_DIR/colors.sh"

# Get volume information
if [ "$SENDER" = "volume_change" ]; then
    VOLUME="$INFO"
    # Check if muted by trying to get current mute state
    MUTED=$(osascript -e "output muted of (get volume settings)" 2>/dev/null)
else
    VOLUME=$(osascript -e "output volume of (get volume settings)" 2>/dev/null)
    MUTED=$(osascript -e "output muted of (get volume settings)" 2>/dev/null)
fi

# Fallback if osascript fails
if [[ -z "$VOLUME" ]]; then
    VOLUME=50
fi

# Determine icon and color based on volume level and mute status
if [[ $MUTED == "true" ]]; then
    ICON="󰖁"  # Muted icon
    COLOR=$RED
    LABEL=""  # No label for muted state since we're not showing labels
else
    if [[ $VOLUME -gt 75 ]]; then
        ICON="󰕾"  # High volume
        COLOR=$ACCENT_PRIMARY
    elif [[ $VOLUME -gt 50 ]]; then
        ICON="󰖀"  # Medium volume
        COLOR=$ACCENT_PRIMARY
    elif [[ $VOLUME -gt 25 ]]; then
        ICON="󰕿"  # Low volume
        COLOR=$YELLOW
    elif [[ $VOLUME -gt 0 ]]; then
        ICON="󰕿"  # Very low volume
        COLOR=$ORANGE
    else
        ICON="󰖁"  # No volume
        COLOR=$RED
    fi
    LABEL=""  # No label since we're not showing labels in the bar
fi

# Update the volume item
sketchybar --set "$NAME" icon="$ICON" \
                        icon.color="$COLOR" \
                        label="$LABEL"

# Update popup if it's currently showing
if [ "$(sketchybar --query volume | jq -r '.popup.drawing')" = "on" ]; then
    # Update the slider bar
    SLIDER_BAR=""
    FILLED_SEGMENTS=$((VOLUME / 2))
    
    for i in {1..50}; do
        if [ $i -le $FILLED_SEGMENTS ]; then
            SLIDER_BAR="${SLIDER_BAR}█"
        else
            SLIDER_BAR="${SLIDER_BAR}▒"
        fi
    done
    
    # Update mute button
    if [[ $MUTED == "true" ]]; then
        MUTE_ICON="󰖁"
        MUTE_COLOR=$RED
    else
        MUTE_ICON="󰕾"
        MUTE_COLOR=$GREEN
    fi
    
    sketchybar --set volume.mute icon="$MUTE_ICON" icon.color="$MUTE_COLOR"
    sketchybar --set volume.slider icon="$SLIDER_BAR"
    sketchybar --set volume.percent icon="$VOLUME%"
fi