#!/bin/sh
# summary: Show or hide the drop-down terminal.
# starchaser dropdown: Super+Shift+T. A terminal that slides down from the top of the screen over everything, and
# back up with the same key. It keeps running while hidden (it lives on its own hidden desktop, special:dropdown),
# so what you were doing is still there next time. Typing `exit` in it closes it; the next press opens a new one.
#
#   starchaser dropdown          show it (starting it the first time), or hide it when it's showing
#   starchaser dropdown hide     hide it if it's showing, else nothing (the Hide x the cockpit draws on it)
#
# It is 90% as wide as the space the taskbar leaves, with equal gaps on both sides, and half that space tall, at the
# top. Super+F (window modes) leaves it alone, so it keeps that shape. Never moves the pointer.
case "${1:-}" in -h|--help|help)
    sed -n '3,11p' "$0" | sed 's/^# \{0,1\}//'
    exit 0 ;;
hide) HIDE=1 ;;
-?*) echo "starchaser dropdown: unknown option '$1'" >&2
     echo "Run 'starchaser dropdown --help' for more help." >&2
    sed -n '3,11p' "$0" | sed 's/^# \{0,1\}//' >&2
    exit 2 ;;
?*) echo "starchaser dropdown: unknown argument '$1' (it takes none, or hide)" >&2
     echo "Run 'starchaser dropdown --help' for more help." >&2
    exit 2 ;;
esac
CLASS=starchaser-dropdown
SPECIAL=special:dropdown

dsp() { hyprctl eval "hl.dispatch($1)" >/dev/null 2>&1; }

# its address and the desktop it's on, or nothing when it isn't running (a compositor that answers in words, not
# JSON, is the same as no drop-down: jq's own error text never reaches the person)
find_it() { hyprctl clients -j 2>/dev/null | jq -r --arg c "$CLASS" 'first(.[] | select(.class == $c) | "\(.address) \(.workspace.name)") // empty' 2>/dev/null; }

# size and place it on the screen in use: the usable area is the screen less its reserved edges (the taskbar, on any
# side); 90% of that width with equal gaps left and right, half its height, near its top (the same as windows.lua's
# placeDropdown, which sizes it when it first opens). If the screen
# can't be read, hyprland.lua's window rule (which sized it when it opened) stays in charge.
place() {
    hyprctl monitors -j 2>/dev/null | jq -r '.[] | select(.focused)
        | (if (.transform % 2) == 1 then [.height, .width] else [.width, .height] end) as $wh
        | ($wh[0] / .scale) as $mw | ($wh[1] / .scale) as $mh
        | ((.reserved // [0, 0, 0, 0]) + [0, 0, 0, 0]) as $r
        | ($mw - $r[0] - $r[2]) as $aw
        | ($aw * 0.9 | round) as $w
        | ($mh - $r[1] - $r[3]) as $ah
        | ($ah * 0.5 | round) as $h
        | "\((.x + $r[0] + ($aw - $w) / 2) | round) \((.y + $r[1] + $ah * 0.04) | round) \($w) \($h)"' 2>/dev/null | {
        read -r x y w h || exit 0
        [ -n "$h" ] || exit 0
        dsp "hl.dsp.window.resize({ x = $w, y = $h, window = \"address:$1\" })"
        dsp "hl.dsp.window.move({ x = $x, y = $y, window = \"address:$1\" })"
    }
}

# give it the keyboard once it's showing: when nothing had the keyboard before (say the welcome just closed), showing
# its desktop alone left it deaf until clicked. Focus only (cursor.no_warps): the pointer stays put.
focus() { dsp "hl.dsp.focus({ window = \"address:$1\" })"; }

showing() { hyprctl monitors -j 2>/dev/null | jq -e --arg s "$SPECIAL" '.[] | select(.focused) | .specialWorkspace.name == $s' >/dev/null 2>&1; }

toggle() {
    addr=$1 ws=$2
    # minimized (Super+H) or moved off its hidden desktop: put it back there first, or the toggle shows an empty one
    if [ "$ws" != "$SPECIAL" ]; then
        dsp "hl.dsp.window.move({ workspace = \"$SPECIAL\", follow = false, window = \"address:$addr\" })"
        showing && { place "$addr"; focus "$addr"; exit 0; }
    fi
    if showing; then
        dsp 'hl.dsp.workspace.toggle_special("dropdown")'
    else
        dsp 'hl.dsp.workspace.toggle_special("dropdown")'
        place "$addr"
        focus "$addr"
    fi
    exit 0
}

it=$(find_it)
# hide: only ever hides (a click on a Hide x the cockpit still drew a moment after the key hid it never shows it
# again, or starts a new one)
if [ -n "${HIDE:-}" ]; then
    [ -n "$it" ] && showing && [ "${it#* }" = "$SPECIAL" ] && dsp 'hl.dsp.workspace.toggle_special("dropdown")'
    exit 0
fi
[ -n "$it" ] && toggle $it
# the first time: start it (the rule puts it on the hidden desktop, quietly), then slide that desktop down once it's up.
# One start at a time: a second press while it's still starting does nothing, instead of starting a second terminal.
run="${XDG_RUNTIME_DIR:-/tmp}"
mkdir -p "$run" 2>/dev/null     # a runtime folder that's gone since sign-in is made again; one that can't be is skipped
[ -w "$run" ] || exit 0
exec 9>"$run/starchaser-dropdown.lock"
flock -n 9 || exit 0
it=$(find_it)
[ -n "$it" ] && toggle $it
setsid starchaser terminal --class "$CLASS" --title "Drop-down terminal" >/dev/null 2>&1 < /dev/null 9>&- &
i=0
while [ $i -lt 40 ]; do
    it=$(find_it)
    if [ -n "$it" ]; then
        # shown on the screen you're using, unless it's already showing there
        showing || dsp 'hl.dsp.workspace.toggle_special("dropdown")'
        place "${it%% *}"
        focus "${it%% *}"
        exit 0
    fi
    sleep 0.1
    i=$((i + 1))
done
exit 0
