Files
I38/scripts/desktop.sh

160 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# This file is part of I38.
# I38 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# I38 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with I38. If not, see <https://www.gnu.org/licenses/>.
trim_whitespace() {
local text="$1"
text="${text#"${text%%[![:space:]]*}"}"
text="${text%"${text##*[![:space:]]}"}"
printf '%s' "$text"
}
get_shortcuts_file() {
local configHome="${XDG_CONFIG_HOME:-$HOME/.config}"
local candidatePath=
if [[ -n "${SWAYSOCK:-}" ]]; then
candidatePath="${configHome}/sway/desktop_shortcuts"
if [[ -r "$candidatePath" ]]; then
printf '%s\n' "$candidatePath"
return 0
fi
else
candidatePath="${configHome}/i3/desktop_shortcuts"
if [[ -r "$candidatePath" ]]; then
printf '%s\n' "$candidatePath"
return 0
fi
fi
candidatePath="${configHome}/i3/desktop_shortcuts"
if [[ -r "$candidatePath" ]]; then
printf '%s\n' "$candidatePath"
return 0
fi
candidatePath="${configHome}/sway/desktop_shortcuts"
if [[ -r "$candidatePath" ]]; then
printf '%s\n' "$candidatePath"
return 0
fi
return 1
}
build_launch_command() {
local shortcutCommand="$1"
local shortcutWorkspace="$2"
local wmCommand=
local workspaceEscaped=
if [[ -z "$shortcutWorkspace" ]]; then
printf '%s\n' "$shortcutCommand"
return 0
fi
if [[ -n "${SWAYSOCK:-}" ]] && command -v swaymsg &> /dev/null; then
wmCommand="swaymsg"
elif [[ -n "${I3SOCK:-}" ]] && command -v i3-msg &> /dev/null; then
wmCommand="i3-msg"
elif [[ -n "${WAYLAND_DISPLAY:-}" ]] && command -v swaymsg &> /dev/null; then
wmCommand="swaymsg"
elif command -v i3-msg &> /dev/null; then
wmCommand="i3-msg"
fi
if [[ -z "$wmCommand" ]]; then
printf '%s\n' "$shortcutCommand"
return 0
fi
if [[ "$shortcutWorkspace" =~ ^[0-9]+$ ]]; then
printf '%s workspace number %s && %s\n' "$wmCommand" "$shortcutWorkspace" "$shortcutCommand"
return 0
fi
workspaceEscaped=$(printf '%q' "$shortcutWorkspace")
printf '%s workspace %s && %s\n' "$wmCommand" "$workspaceEscaped" "$shortcutCommand"
}
desktopPath="${HOME}/Desktop"
if [[ ! -d "$desktopPath" ]]; then
exit 0
fi
declare -a shortcutMenuList
shortcutsFile="$(get_shortcuts_file)"
if [[ -n "$shortcutsFile" ]]; then
# Optional quick-launch format:
# Pretty Name|command with args|workspace
# Firefox|/usr/bin/firefox|9
# Brave|/usr/bin/brave|
while IFS= read -r rawLine || [[ -n "$rawLine" ]]; do
line="$(trim_whitespace "$rawLine")"
if [[ -z "$line" ]] || [[ "$line" =~ ^# ]]; then
continue
fi
IFS='|' read -r shortcutName shortcutCommand shortcutWorkspace _ <<< "$line"
shortcutName="$(trim_whitespace "$shortcutName")"
shortcutCommand="$(trim_whitespace "$shortcutCommand")"
shortcutWorkspace="$(trim_whitespace "$shortcutWorkspace")"
if [[ -z "$shortcutName" ]] || [[ -z "$shortcutCommand" ]]; then
continue
fi
shortcutMenuList+=("$shortcutName" "$(build_launch_command "$shortcutCommand" "$shortcutWorkspace")")
done < "$shortcutsFile"
fi
# Discover directories and files on the desktop.
mapfile -t desktopDirs < <(find "$desktopPath" -mindepth 1 -maxdepth 1 -type d)
mapfile -t desktopFiles < <(find "$desktopPath" -mindepth 1 -maxdepth 1 -type f)
desktopItems=("${desktopDirs[@]}" "${desktopFiles[@]}")
# Build menu for yad.
declare -a desktopMenuList
for desktopItem in "${desktopItems[@]}"; do
case "$desktopItem" in
*.desktop)
desktopName="$(grep -m 1 "^Name=" "$desktopItem" | cut -f2- -d '=')"
if [[ -z "$desktopName" ]]; then
desktopName="${desktopItem##*/}"
fi
desktopMenuList+=("$desktopName" "gio launch \"$desktopItem\"")
;;
*)
desktopMenuList+=("${desktopItem##*/}" "/usr/bin/env xdg-open \"$desktopItem\"")
;;
esac
done
menuList=("${shortcutMenuList[@]}" "${desktopMenuList[@]}")
if [[ "${#menuList[@]}" -eq 0 ]]; then
exit 0
fi
if launch=$(yad --list \
--title="I38 Desktop" \
--column="Items" \
--column="Launch Command" \
--close-on-unfocus \
--hide-column=2 \
--search-column=1 \
--skip-taskbar \
"${menuList[@]}"); then
launch="${launch%|}"
launch="${launch##*|}"
eval "$launch"
fi