Dotfiles/desktopenvs/hyprlua/scripts/menu-toggle.sh

66 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Toggle the astal-menu popup (or open it to a section). Forwards a verb to the
# resident daemon over D-Bus; if the daemon isn't running yet, starts it first.
# (No `set -e`: a non-zero `grep`/`busctl` in the wait loop is expected and must
# not abort the script before it forwards the verb.)
#
# menu-toggle.sh -> --toggle (top, the default)
# menu-toggle.sh appdrawer -> open with the app drawer expanded
# menu-toggle.sh left -> toggle, sliding in from / pinned to the left
# menu-toggle.sh toggle right -> same, explicit verb
# menu-toggle.sh appdrawer bottom
#
# Verbs: (toggle) | show | hide | appdrawer. Sides: top | bottom | left | right.
BUS="eu.abdelbaki.astalmenu"
OBJ="/eu/abdelbaki/astalmenu"
APP="${HOME}/.config/astal-menu/main.py"
verb="${1:-}"; side="${2:-}"
# allow the side to be given as the sole argument (verb defaults to toggle)
case "$verb" in
top|bottom|left|right) side="$verb"; verb="" ;;
esac
case "$verb" in
appdrawer) VERB="--appdrawer"; ACTION="appdrawer"; HAS_PARAM=1 ;;
show) VERB="--show"; ACTION="show"; HAS_PARAM=1 ;;
hide) VERB="--hide"; ACTION="hide"; HAS_PARAM=0 ;;
*) VERB="--toggle"; ACTION="toggle"; HAS_PARAM=1 ;;
esac
case "$side" in
top|bottom|left|right) SIDE=(--side "$side") ;;
*) side=""; SIDE=() ;;
esac
registered() { busctl --user list 2>/dev/null | grep -q "$BUS"; }
if registered; then
# Fast path: the daemon is already up, so activate the action directly over
# D-Bus (~20-100ms) instead of cold-starting a second python3 + PyGObject +
# GTK4 process (~0.5s baseline, much worse under load — observed spikes past
# 5s) just to forward one verb via GApplication's command-line forwarding.
if [[ $HAS_PARAM -eq 1 ]]; then
param="[<'${side}'>]"
else
param="[]"
fi
exec gdbus call --session --dest "$BUS" --object-path "$OBJ" \
--method org.gtk.Actions.Activate "$ACTION" "$param" "{}" >/dev/null
fi
# Not registered yet: start the daemon and wait for it to come up, then retry
# via the fast path above.
"${HOME}/.config/scripts/astal-menu-start.sh" >/dev/null 2>&1 &
for _ in $(seq 1 25); do
if registered; then
exec "$0" "$@"
fi
sleep 0.2
done
# Still not registered after 5s: fall back to becoming the primary instance
# ourselves (Gio single-instance handles the routing either way).
exec python3 "$APP" "$VERB" "${SIDE[@]}"