#!/usr/bin/env bash
# chroma rofi menus: launcher | clipboard | power
R="$HOME/.config/chroma/rofi"
pkill -u "$(id -u)" -x rofi && exit 0     # pressing the key again closes the menu

case "$1" in
launcher)
    exec rofi -show drun -theme "$R/chroma.rasi" -display-drun "Search" ;;
clipboard)
    sel=$(cliphist list | rofi -dmenu -p "History" -theme "$R/chroma.rasi" -theme-str 'textbox-title {
    content: "Clipboard";
}
textbox-jp {
    content: "history";
}'  -display-columns 2) || exit
    cliphist decode <<<"$sel" | wl-copy ;;
power)
    lock="Lock"; logout="Log out"; suspend="Sleep"; reboot="Restart"; off="Shut down"
    ch=$(printf '%s\n' "$lock" "$suspend" "$logout" "$reboot" "$off" |
         rofi -dmenu -theme "$R/power.rasi" -selected-row 0) || exit
    case "$ch" in
        "$lock")    "$HOME/.config/chroma/bin/chroma-lock" ;;
        "$suspend") systemctl suspend ;;
        "$logout")  hyprctl dispatch 'hl.dsp.exit()' ;;
        "$reboot")  systemctl reboot ;;
        "$off")     systemctl poweroff ;;
    esac ;;
*) echo "usage: chroma-menu launcher|clipboard|power"; exit 1 ;;
esac
