Files
audiogame-manager/.includes/dialog-interface.sh

281 lines
7.8 KiB
Bash

#!/usr/bin/env bash
# Dialog interface wrapper for audiogame-manager
# Automatically switches between dialog (console) and yad (GUI) based on DISPLAY environment
# This provides better accessibility for GUI environments while maintaining console functionality
# Note: dialogType is now detected in the main script before DISPLAY is modified
# This ensures console detection works correctly when AGM sets DISPLAY=":0"
# If dialogType is not set (e.g., when called from standalone scripts), detect it
if [[ -z "$dialogType" ]]; then
if [[ -z "$DISPLAY" ]]; then
dialogType="dialog"
else
dialogType="yad"
fi
fi
# Wrapper function for menu selection
# Usage: agm_menu "title" "backtitle" "text" option1 "description1" option2 "description2" ...
agm_menu() {
local title="$1"
local backTitle="$2"
local text="$3"
shift 3
if [[ "$dialogType" == "yad" ]]; then
# Build yad list format: Display only, then map back to value
local yadList=""
declare -A valueMap
while [[ $# -gt 0 ]]; do
local option="$1"
local description="$2"
valueMap["$description"]="$option"
if [[ -n "$yadList" ]]; then
yadList="$yadList\n"
fi
yadList="${yadList}${description}"
shift 2
done
local selectedDescription
selectedDescription=$(echo -e "$yadList" | yad --list \
--title="$title" \
--text="$text" \
--column="Option" \
--no-headers \
--selectable-labels \
--search-column=1 \
--height=400 \
--width=600)
# Return the mapped value
if [[ -n "$selectedDescription" ]]; then
echo "${valueMap["$selectedDescription"]}"
fi
else
# Build dialog menu format with mapping (same approach as yad)
local dialogArgs=()
declare -A valueMap
while [[ $# -gt 0 ]]; do
local option="$1"
local description="$2"
valueMap["$description"]="$option"
dialogArgs+=("$description" "$description")
shift 2
done
local selectedDescription
selectedDescription=$(dialog --backtitle "$backTitle" \
--title "$title" \
--no-tags \
--menu "$text" 0 0 0 \
"${dialogArgs[@]}" \
--stdout)
# Return the mapped value
if [[ -n "$selectedDescription" ]]; then
echo "${valueMap["$selectedDescription"]}"
fi
fi
}
# Wrapper function for checklist selection
# Usage: agm_checklist "title" "backtitle" "text" option1 "description1" "status1" option2 "description2" "status2" ...
agm_checklist() {
local title="$1"
local backTitle="$2"
local text="$3"
shift 3
if [[ "$dialogType" == "yad" ]]; then
local yadList=""
while [[ $# -gt 0 ]]; do
local option="$1"
local description="$2"
local status="$3"
local checked="FALSE"
[[ "$status" == "on" ]] && checked="TRUE"
if [[ -n "$yadList" ]]; then
yadList="$yadList\n"
fi
yadList="${yadList}${checked}|${description}|${option}"
shift 3
done
echo -e "$yadList" | yad --list \
--title="$title" \
--text="$text" \
--checklist \
--column="Select:CHK" \
--column="Option" \
--column="Value:HD" \
--hide-column=3 \
--print-column=3 \
--no-headers \
--selectable-labels \
--height=400 \
--width=600 \
--separator=" "
else
local dialogArgs=()
while [[ $# -gt 0 ]]; do
dialogArgs+=("$1" "$2" "$3")
shift 3
done
dialog --backtitle "$backTitle" \
--title "$title" \
--checklist "$text" 0 0 0 \
"${dialogArgs[@]}" \
--stdout
fi
}
# Wrapper function for input dialog
# Usage: agm_inputbox "title" "backtitle" "text" "default_value"
agm_inputbox() {
local title="$1"
local backTitle="$2"
local text="$3"
local defaultValue="$4"
if [[ "$dialogType" == "yad" ]]; then
yad --entry \
--title="$title" \
--text="$text" \
--entry-text="$defaultValue" \
--selectable-labels \
--width=400
else
dialog --backtitle "$backTitle" \
--title "$title" \
--inputbox "$text" 0 0 "$defaultValue" \
--stdout
fi
}
# Wrapper function for message box
# Usage: agm_msgbox "title" "backtitle" "text"
agm_msgbox() {
local title="$1"
local backTitle="$2"
local text="$3"
if [[ "$dialogType" == "yad" ]]; then
yad --info \
--title="$title" \
--text="$text" \
--selectable-labels \
--show-cursor \
--width=400
else
dialog --backtitle "$backTitle" \
--title "$title" \
--msgbox "$text" 0 0
fi
}
# Wrapper function for yes/no dialog
# Usage: agm_yesno "title" "backtitle" "text"
agm_yesno() {
local title="$1"
local backTitle="$2"
local text="$3"
if [[ "$dialogType" == "yad" ]]; then
yad --question \
--title="$title" \
--text="$text" \
--selectable-labels \
--width=400
else
dialog --backtitle "$backTitle" \
--title "$title" \
--yesno "$text" 0 0
fi
}
# Wrapper function for info box (non-blocking message)
# Usage: agm_infobox "title" "backtitle" "text"
agm_infobox() {
local title="$1"
local backTitle="$2"
local text="$3"
if [[ "$dialogType" == "yad" ]]; then
# For yad, we'll use a notification since infobox is non-blocking
yad --notification \
--text="$text" \
--timeout=3
else
dialog --backtitle "$backTitle" \
--title "$title" \
--infobox "$text" 0 0
fi
}
# Wrapper function for progress box
# Usage: command | agm_progressbox "title" "text"
agm_progressbox() {
local title="$1"
local text="$2"
if [[ "$dialogType" == "yad" ]]; then
yad --progress \
--title="$title" \
--text="$text" \
--pulsate \
--auto-close \
--no-buttons \
--show-cursor \
--width=400
else
dialog --title "$title" \
--progressbox "$text" 20 70
fi
}
# Wrapper function for file selection
# Usage: agm_fselect "title" "backtitle" "default_path"
agm_fselect() {
local title="$1"
local backTitle="$2"
local defaultPath="$3"
if [[ "$dialogType" == "yad" ]]; then
yad --file \
--title="$title" \
--filename="$defaultPath" \
--width=600 \
--height=400
else
dialog --backtitle "$backTitle" \
--title "$title" \
--fselect "$defaultPath" 0 0 \
--stdout
fi
}
# Wrapper function for directory selection
# Usage: agm_dselect "title" "backtitle" "default_path"
agm_dselect() {
local title="$1"
local backTitle="$2"
local defaultPath="$3"
if [[ "$dialogType" == "yad" ]]; then
yad --file \
--directory \
--title="$title" \
--filename="$defaultPath" \
--width=600 \
--height=400
else
dialog --backtitle "$backTitle" \
--title "$title" \
--dselect "$defaultPath" 0 0 \
--stdout
fi
}