#!/usr/bin/env bash
# Handy force-shows its main window at startup whenever the tray icon is
# disabled (lib.rs: "If start_hidden but tray is disabled, we must show
# the window anyway" — it can't know the SUPER+G/SHIFT+G binds are the
# way back in). So --start-hidden alone can't keep the two instances
# hidden at boot. Instead: watch for the first two Handy windows and
# dispatch a close — Handy intercepts close and hides the window.
set -euo pipefail

hidden=0
for _ in $(seq 1 150); do # ~30 s window after boot
    while read -r addr; do
        # Lua-form dispatch: the Lua config provider parses hyprctl
        # dispatch args as a Lua expression.
        hyprctl dispatch "hl.dsp.window.close({ window = 'address:$addr' })" >/dev/null
        hidden=$((hidden + 1))
    done < <(hyprctl clients -j | jq -r '.[] | select(.class == "handy") | .address')
    [ "$hidden" -ge 2 ] && exit 0
    sleep 0.2
done
exit 0
