(defpoll bar-workspace-data :interval "0.1s" :initial "[]" `~/.config/eww/widgets/bar/scripts/workspaces.sh`)

(defwidget bar-workspaces []
  (box 
    :class "widget workspaces"
    :space-evenly false
    :hexpand false
    :halign "start"
    (for workspace in bar-workspace-data
      (eventbox
        :onclick "${EWW_CMD} open bar-workspace-window --arg workspaceId=${workspace.id}"
        :cursor "pointer"
        (box
          :space-evenly false
          :spacing 10
          :class "workspace ${workspace.activeOnAnyScreen ? "active": ""} ${workspace.activeOnCurrentScreen ? "current": ""}"

          (box 
            :space-evenly false
            :spacing 10
            (label
              :text {workspace.name}
            )
            (box
              :spacing 10
              (for window in {workspace.windows}
                (label
                  :text {window.icon}
                )
              )
            )
          )
        )
      )
    )
  )
)

(defwindow bar-workspace-window [workspaceId]
  :monitor 1
  :geometry (geometry 
  :x "10px"
  :y "50px"
  :width "512px"
  :height "0px"
  :anchor "top left")
  :stacking "fg"

  (box 

    (for workspace in bar-workspace-data
      (box 
        :orientation "v"
        :vexpand false
        :valign "start"
        :class "widget default-padding"
        :space-evenly false
        :spacing 10
        :visible {workspaceId == workspace.id}
        (for window in {workspace.windows}
          (eventbox
            :cursor "pointer"
            :onclick "hyprctl dispatch focuswindow pid:${window.pid} && ${EWW_CMD} close bar-workspace-window"
            :onrightclick "${EWW_CMD} close bar-workspace-window"

            (label
              :halign "start"
              :text "${window.icon} ${window.title}"
              :truncate false
            )
          )
        )
      )
    )
  )
)
