#!/bin/sh
# summary: Show or hide the pop-up desktop.
# starchaser popup desktop: Super+` (the key above Tab). Shows or hides the hidden pop-up desktop, which slides down
# over the screen you're using. Send a window there with Super+Shift+` from the window.
#
#   starchaser popup desktop     show it, or hide it when it's showing
#   starchaser popup desktop send   send the window you're using there, and clear the "it's empty" notice;
#                                   from a window already on the pop-up desktop, send it back to the normal desktop
#
# When it's empty it doesn't open (an empty one would take the keyboard and show nothing): a notice says so instead,
# with the keys in use (a shortcut profile or your own change can move them). Never moves the pointer.
for a in "$@"; do
  case "$a" in -h|--help|help)
    sed -n '3,11p' "$0" | sed 's/^# \{0,1\}//'
    exit 0 ;;
  esac
done
case "${1:-}" in
  -?*) echo "starchaser popup desktop: unknown option '$1'" >&2; sed -n '3,11p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2 ;;
  send) ;;
  ?*) echo "starchaser popup desktop takes no arguments. Run 'starchaser popup desktop --help' for more help." >&2; exit 2 ;;
esac
run="${XDG_RUNTIME_DIR:-/tmp}"
idf="$run/starchaser-popup-desktop-notice"
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
# the notice's bookkeeping file needs its folder: with no writable folder there is nothing to close and nothing
# to keep, and the shell must never print its own redirection error over the screen
can_note() { [ -d "$run" ] && [ -w "$run" ]; }
# Closes the earlier "empty" notice, if it is still ours: the id is kept with the notification server that gave it, so
# a restarted server's unrelated notice with the same number is never closed. Prints the server's owner name.
close_old_notice() {
  owner=$(gdbus call --session --dest org.freedesktop.DBus --object-path /org/freedesktop/DBus \
            --method org.freedesktop.DBus.GetNameOwner org.freedesktop.Notifications 2>/dev/null | sed "s/[^:0-9.]//g")
  read -r old oldowner < "$idf" 2>/dev/null
  case "$old" in ''|*[!0-9]*) old= ;; esac
  if [ -n "$old" ] && [ -n "$owner" ] && [ "$oldowner" = "$owner" ]; then
    gdbus call --session --dest org.freedesktop.Notifications --object-path /org/freedesktop/Notifications \
      --method org.freedesktop.Notifications.CloseNotification "$old" >/dev/null 2>&1
  fi
}
if [ "${1:-}" = send ]; then
  # a window already on the pop-up desktop goes back to the normal desktop showing under it on this screen
  back=$(hyprctl -j activewindow 2>/dev/null | jq -r 'select(.workspace.name == "special:magic") | .address' 2>/dev/null)
  ws=$(hyprctl -j monitors 2>/dev/null | jq -r '[.[] | select(.focused)][0].activeWorkspace.id // empty' 2>/dev/null)
  case "$ws" in ''|*[!0-9]*) ws= ;; esac
  if [ -n "$back" ] && [ -n "$ws" ]; then
    hyprctl eval "hl.dispatch(hl.dsp.window.move({ workspace = \"$ws\" }))" >/dev/null 2>&1
    exit 0
  fi
  hyprctl eval 'hl.dispatch(hl.dsp.window.move({ workspace = "special:magic", follow = false }))' >/dev/null 2>&1
  # the window is there now, so a notice saying it is empty would be wrong
  can_note && ( flock -w 2 9 2>/dev/null || exit 0; close_old_notice ) 9>"$idf.lock" >/dev/null 2>&1 &
  exit 0
fi
toggle() { hyprctl eval 'hl.dispatch(hl.dsp.workspace.toggle_special("magic"))' >/dev/null 2>&1; }
# showing now on the screen in use: always hide it. If a question to the compositor fails, just toggle, so a pop-up
# desktop with windows on it is never refused.
mons=$(hyprctl monitors -j 2>/dev/null) || { toggle; exit 0; }
shown=$(printf '%s' "$mons" | jq -r 'any(.[]; .focused and .specialWorkspace.name == "special:magic")' 2>/dev/null)
case "$shown" in true) toggle; exit 0 ;; false) ;; *) toggle; exit 0 ;; esac
wins=$(hyprctl clients -j 2>/dev/null) || { toggle; exit 0; }
has=$(printf '%s' "$wins" | jq -r 'any(.[]; .workspace.name == "special:magic")' 2>/dev/null)
[ "$has" = "false" ] || { toggle; exit 0; }
# empty: say so every time it's asked for (it's an answer to a key press, not a reminder)
show=$(starchaser shortcuts key "Desktops: Hidden pop-up desktop (slides in and out)" 2>/dev/null)
send=$(starchaser shortcuts key "Desktops: Send window to the hidden desktop" 2>/dev/null)
body="Keep windows out of the way here until you need them."
[ -z "$send" ] || body="Send a window here with $send. $body Press $send again on that window to bring it back."
[ -n "$show" ] && body="$body Once a window is here, $show shows this desktop and hides it again."
case "$send $show" in *'`'*) body="$body The backtick key is left of 1 on a US keyboard." ;; esac
# Pressing it again closes the earlier notice and sends a fresh one. Reusing its id with the same words never showed
# the pop-up again (the shell only redraws a replaced notice when something in it changed). The id is kept with the
# notification server that gave it, so a restarted server's unrelated notice with the same number is never closed.
# Requests run one at a time.
can_note && (
  flock -w 2 9 2>/dev/null || exit 0     # another press is still talking to the notification server
  close_old_notice
  id=$(/bin/sh -c 'token="${XDG_RUNTIME_DIR:-/run/user/$(/usr/bin/id -u)}/starchaser-notifications/token"; if [ -r "$token" ]; then n=$1; shift; exec "$0" "$n" --credential-fd=3 "$@" 3<"$token"; else exec "$0" "$@"; fi' starchaser notify -p -a Starchaser -i view-dual -t 6000 -h boolean:transient:true "Your pop-up desktop is empty" "$body" 2>/dev/null) &&
    printf '%s %s\n' "$id" "$owner" > "$idf"
) 9>"$idf.lock" >/dev/null 2>&1 &
exit 0
