(defpoll volume :interval "1s"
  "wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{printf \"%d\", $2*100}'")

(defpoll muted :interval "1s"
  "wpctl get-volume @DEFAULT_AUDIO_SINK@ | grep -c MUTED || echo 0")

(defpoll sink-inputs :interval "2s"
  "~/.config/eww/scripts/get-sinks.sh")

(defwidget volume-overlay []
  (box :class "volume-overlay" :orientation "v" :spacing 12 :space-evenly false
    (box :class "header" :orientation "h" :space-evenly false :spacing 8
      (label :class "header-icon" :text "󰕾")
      (label :class "header-title" :text "Volume Mixer"))

    (box :class "sink-row" :orientation "v" :spacing 4
      (box :orientation "h" :space-evenly false :spacing 8
        (label :class "app-icon" :text "󰓃")
        (label :class "app-name" :text "Master")
        (label :class "app-vol" :text "${volume}%"))
      (scale :class "vol-slider master-slider"
             :min 0 :max 100 :value volume
             :onchange "wpctl set-volume @DEFAULT_AUDIO_SINK@ {}%"))

    (box :class "divider")

    (for sink in sink-inputs
      (box :class "sink-row" :orientation "v" :spacing 4
        (box :orientation "h" :space-evenly false :spacing 8
          (label :class "app-icon" :text "󰓃")
          (label :class "app-name" :text {sink.name})
          (label :class "app-vol" :text "${sink.volume}%"))
        (scale :class "vol-slider"
               :min 0 :max 100 :value {sink.volume}
               :onchange "wpctl set-volume ${sink.id} {}%")))

    (button :class "mute-btn ${muted == "1" ? "muted" : ""}"
            :onclick "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"
      (label :text "${muted == "1" ? "󰝟  Muted" : "󰕾  Mute"}"))))

(defwindow volume-mixer
  :monitor 0
  :geometry (geometry :x "0px" :y "40px"
                      :width "280px"
                      :anchor "top right")
  :stacking "overlay"
  :exclusive false
  (volume-overlay))

