#!/usr/bin/env bash
# niri helpers for the eww scripts. niri-only — the Hyprland/Sway abstraction
# (and its compositor detector) was removed; the old dual-compat version is in
# ~/eww-backup-dual-*. Source this:  . "$(dirname "$0")/wm.sh"
#
# Provides:
#   $WM                       always "niri" (kept so callers' $WM checks resolve)
#   wm_clients                normalised JSON [{id,pid,class,title,ws}]
#   wm_focus <id>             focus a window by id
#   wm_goto_ws <idx>          focus a workspace by index
#   wm_cycle_ws <up|down>     focus prev/next workspace
#   wm_cursor_y               empty (niri has no cursor-position IPC)
#   wm_outputs                number of active outputs
#   wm_lock / wm_exit         lock screen / quit the compositor
#   wm_blur_layer <ns>        no-op (niri layer effects live in config.kdl)

WM=niri
export WM

wm_clients() {
  niri msg --json windows 2>/dev/null | jq -c '
    [ .[] | { id:    (.id|tostring),
              pid:   (.pid // 0),
              class: (.app_id // ""),
              title: (.title // ""),
              ws:    ((.workspace_id // "") | tostring) } ]'
}

wm_focus()   { [ -n "${1:-}" ] && niri msg action focus-window --id "$1" >/dev/null 2>&1; }
wm_goto_ws() { niri msg action focus-workspace "$1" >/dev/null 2>&1; }
wm_cycle_ws() {
  if [ "$1" = up ]; then niri msg action focus-workspace-up   >/dev/null 2>&1
  else                   niri msg action focus-workspace-down >/dev/null 2>&1; fi
}

wm_cursor_y() { printf ''; }   # niri exposes no pointer position over IPC

wm_outputs() { niri msg --json outputs 2>/dev/null | jq '[.[]|select(.logical!=null)]|length'; }

wm_lock() { swaylock -f; }
wm_exit() { niri msg action quit --skip-confirmation >/dev/null 2>&1; }

# Blur/rounding/shadow for eww's layer surfaces is declarative in config.kdl
# (a layer-rule matched on the gtk-layer-shell namespace) — nothing to do live.
wm_blur_layer() { :; }
