
; =============================================================================
; eww.yuck — EWW bar configuration for hyprlua (laptop with battery variant)
; This file defines all widgets, polls, and listeners for the top status bar.
; EWW (Elkowar's Wacky Widgets) is a widget system for Wayland/X11 that uses
; a Lisp-like syntax (.yuck) for layout and SCSS for styling.
;
; Color palette (cyberqueer theme):
;   Background: #1a1a1a   (near-black)
;   Accent 1:   #E40046   (red — active/highlight)
;   Accent 2:   #5018dd   (purple — inactive/secondary)
;   Text:       #D6ABAB   (muted pink-grey)
; =============================================================================

; defwindow — declares the actual OS-level window that appears on screen.
; :monitor    — which monitor index to render on (passed at launch time)
; :windowtype "dock" — tells the compositor this is a panel, so it reserves space
; :exclusive true — prevents other windows from overlapping the bar area
; :anchor "top center" — pins the bar to the top center of the monitor
; :geometry — sets position (x=0%, y=1% from top), width (99%), height (20px)
(defwindow bar [monitor]
  :monitor monitor
  :class "ewwbar"
  :windowtype "dock"
  :geometry (geometry :x "0%"
                      :y "1%"
                      :width "99%"
                      :height "20px"
                      :anchor "top center")
                      :exclusive true
  ; Render the bar widget, passing the monitor index through for workspace tracking
  (bar :monitor_ monitor))

; defpoll — polls a shell command on an interval and stores the result.
; battery is polled every 2 seconds so the icon/percentage stays current.
; The script returns a formatted string like "󰁹 87%" for display.
(defpoll battery :interval "2s"
  "~/Dotfiles/desktopenvs/hyprland/scripts/batteryperc")



; defwidget bar — root bar widget. Uses centerbox to split into three zones:
;   left  → winsworks (battery, workspaces, active window title)
;   center → music (currently playing track)
;   right → sidestuff (IP, volume, disk, caffeine, clock, systray)
(defwidget bar [monitor_]
  (centerbox :orientation "h"
	(winsworks :monitor monitor_)
    (music)
    (sidestuff)))

; winsworks — left section of the bar.
; Shows battery percentage, workspace buttons, and the active window title.
; :halign "start" — left-aligns the content within the centerbox left cell.
(defwidget winsworks [monitor]
	(box :orientation "h" :space-evenly false :halign "start"
		; astal-menu launcher — opens the popup control centre / app drawer
		(button :class "music menu-launcher" :onclick "~/.config/scripts/menu-toggle.sh toggle top" {""})
		; Battery percentage badge — styled as a pill with class "music"
		(box :class "music" {"${battery}"})
		; Workspace dots — one button per active workspace on this monitor
		(workspaceWidget :monitor monitor)
		; Active window title — clicking opens the application drawer
		(button :onclick "~/.config/scripts/menu-toggle.sh toggle top" :class "music" {" ${activewindow}"})
        	)
)

; sidestuff — right section of the bar.
; :halign "end" — right-aligns all children within the centerbox right cell.
(defwidget sidestuff []
  (box :class "sidestuff" :orientation "h" :space-evenly false :halign "end"
    ; IP address badge — refreshed every 5s, shows current LAN/VPN IP
    (box :class "music" {"󰛳 ${IP}"})
    ; Volume slider — 󰓃 icon, drag to adjust, click to open pavucontrol mixer
    ; hyprctl eval is used to run Lua dispatch inline so the window gets the +mixer tag
    (metric :label "󰓃"
            :value volume
            :onchange "pactl set-sink-volume @DEFAULT_SINK@ {}%"
	    :onclick "killall pavucontrol || hyprctl eval 'hl.dsp.exec_cmd(\"[tag +mixer] pavucontrol\")'")
   ; Disk usage gauge — tooltip shows per-disk breakdown from dysk
   ; EWW_DISK is a built-in magic variable providing filesystem stats
   (box
	:tooltip {disks}
	(metric :label ""
            :value {round((1 - (EWW_DISK["/"].free / EWW_DISK["/"].total)) * 100, 0)}
            :onchange ""
	    :onclick ""))

    ; Caffeine toggle button — shows ☕ when active, sleeping icon when off
    (caffeine)
    ; Clock widget with calendar tooltip
    (clock)
    ; System tray — renders applets from running applications (nm-applet, blueman, etc.)
    (systray :class "music" :orientation "h" :spacing 2 :space-evenly true)
))

; music — center section: shows currently playing track via playerctl.
; Clicking play/pauses the current media player (spotify/vlc priority).
; Displays a music note icon + track name, or "None" if nothing is playing.
(defwidget music []
  (button :class "music"
       :orientation "h"
       :space-evenly false
       :halign "center"
       :onclick "~/Dotfiles/desktopenvs/hyprland/scripts/playpause.sh"
    {music != "" ? " ${music}" : " None"}))


; metric — reusable slider+label widget used for volume and disk usage.
; Parameters:
;   label    — icon/text shown as a clickable button
;   value    — current value (0-100)
;   onchange — command run when slider is dragged (use {} as placeholder for value)
;   onclick  — command run when the label button is clicked
(defwidget metric [label value onchange onclick]
  (box :orientation "h"
       :class "metric"
       :space-evenly false
    (button :class "label" :onclick onclick label)
    (scale :min 0
           :max 101
           ; Only make the slider interactive if an onchange command was provided
           :active {onchange != ""}
           :value value
           :onchange onchange)))

; deflisten — subscribes to a long-running process that emits JSON on stdout.
; hyprland-workspaces is a helper binary that streams workspace state changes.
; The "_" argument means listen for all monitors; we filter by monitor in the widget.
(deflisten workspaces "hyprland-workspaces _")

; workspaceWidget — renders one button per workspace on the given monitor.
; :onscroll — scrolling the workspace area switches workspaces; sed converts
;             "up"/"down" into "r+1"/"r-1" relative workspace references,
;             then hyprctl eval runs the Lua dispatch inline.
; The special:magic scratchpad workspace gets a distinct icon (󱂬 without a number).
(defwidget workspaceWidget [monitor]
  (eventbox :onscroll "hyprctl eval \"hl.dsp.focus({workspace='$(echo {} | sed 's/up/r+/;s/down/r-/')1'})\""
    (box :class "workspaces"
         :space-evenly false
         :width 20
      (for i in {workspaces[monitor].workspaces}
        (button
          :width 20
          ; Click a workspace button to focus it via Lua dispatch
          :onclick "hyprctl eval 'hl.dsp.focus({workspace=${i.id}})'"
          ; i.class comes from hyprland-workspaces: "active", "occupied", or ""
          :class "${i.class}"
          {i.name == "special:magic" ? "󱂬" : "󱂬${i.name}"})))))


; workspace-old — legacy workspace listener using a shell script instead of
; the hyprland-workspaces binary. Kept for reference/fallback.
(deflisten workspace-old "~/Dotfiles/desktopenvs/hyprland/scripts/workspace")
(defwidget workspaces-old []
(literal :content workspace-old))

; music poll — queries playerctl for the current track every 0.5 seconds.
; Returns an empty string when nothing is playing so the widget shows "None".
(defpoll music :interval "0.5s"
  "~/Dotfiles/desktopenvs/hyprland/scripts/playerget")


; activewindow poll — gets the focused window title/class every 0.5 seconds.
; Used in the left section of the bar as a window title breadcrumb.
(defpoll activewindow :interval "0.5s"
  "~/Dotfiles/desktopenvs/hyprland/scripts/activewindow")


; IP poll — gets the current IP address every 5 seconds.
; Slow interval because IP changes are infrequent; avoids unnecessary shell forks.
(defpoll IP :interval "5s"
  "~/Dotfiles/desktopenvs/hyprland/scripts/ip")

; volume poll — reads the default audio sink volume every 0.5 seconds.
; Fast interval so the slider reflects hardware key presses immediately.
(defpoll volume :interval "0.5s"
  "~/Dotfiles/desktopenvs/hyprland/scripts/getvol")

; time poll — reads the system clock every second for the clock widget.
; Format: HH:MM:SS|DD.MM.YYYY — the pipe character is used as a visual separator.
(defpoll time :interval "1s"
  "date '+%H:%M:%S|%d.%m.%Y'")

; clock widget — displays the current time and shows a calendar as a tooltip.
; The tooltip is populated by the `calender` poll (note: intentional spelling).
(defwidget clock []
	(box	:class "clock"
		:tooltip {calender}
		(label	:text {"${time}"})
	)
)

; calender poll — runs a script that generates a text calendar (via cal).
; Polled every 600 seconds (10 minutes) since the calendar only changes daily.
(defpoll calender :interval "600s"
    "~/Dotfiles/desktopenvs/hyprland/scripts/calender-fix.sh")

; disks poll — runs dysk to get a human-readable table of physical disk usage.
; Shown as a tooltip on the disk gauge. Polled every 10 minutes.
(defpoll disks :interval "600s"
    "~/Dotfiles/desktopenvs/hyprland/scripts/dysk-phydisks.sh")

; caffeine-active poll — checks whether the caffeine systemd-inhibit lock is active.
; Returns "true" or "false" string every 2 seconds. Used to toggle the icon.
(defpoll caffeine-active :interval "2s"
  "~/Dotfiles/desktopenvs/hyprlua/scripts/caffeine-status.sh")

; caffeine widget — a button that toggles the caffeine idle-inhibit lock.
; ☕ = caffeine is ON (screen won't sleep), 󰅺 = caffeine is OFF (normal idle).
; Tooltip shows the current state for clarity.
(defwidget caffeine []
  (button :class "music"
          :onclick "~/Dotfiles/desktopenvs/hyprlua/scripts/caffeine.sh"
          :tooltip {caffeine-active == "true" ? "Caffeine: ON" : "Caffeine: OFF"}
    {caffeine-active == "true" ? "☕" : "󰅺"}))
