#!/bin/bash
# Configuration file for rofimenu script
#
# Top level menu consists of modi names from modilist.
# Modilist is a comma separated list of default modi (drun,run...) and/or custom modi.
# Names of default modi can be set as rofi options (e.g. -display-drun Applications).
# Custom modi format: "modi_name:modi_script".
# Menu functions from this script can be used as modi like this "<menu_name>:$thisscript -menu <menu_function>"

# pause needed for smooth transition when menu command refers to other modi
DELAY=0.06
delay() {
	sleep $DELAY
}

# define modi labels for menu

FAV=" Favourites"
DRUN=" Applications"
CAT=" Categories"
RUN=" Run"
MENU=" Edit Menu"
EXIT=" Exit"

modilist="\
$FAV:$thisscript -menu ${FAV#* },\
drun,\
$CAT:$thisscript -menu ${CAT#* },\
run,\
$MENU:$thisscript -menu Menu_settings,\
$EXIT:$thisscript -menu ${EXIT#* }"

# Menu functions print lines in format "label:command".

Menu_settings() {
	echo " Edit config:$GUI_EDITOR $ROFIMENU_CONFIG && $thisscript -show \'$MENU\'"
	echo " Reset config:rm $ROFIMENU_CONFIG && delay; $thisscript -show \'$MENU\'"
	echo "──────────────:true"
	echo " Edit theme:$GUI_EDITOR $ROFIMENU_THEME && $thisscript -show \'$MENU\'"
	echo " Reset theme:rm $ROFIMENU_THEME && delay; $thisscript -show \'$MENU\'"
}

Favourites() {
	echo " Terminal:default-terminal"
	echo " File Manager:xdg-open ~"
	echo " Browser:default-browser"
	echo " Settings:default-terminal -e bmenu"
	echo " Search:rofi-finder.sh"
	# echo "$EXIT: delay; delay; $thisscript -show \'$EXIT\'"		# This refers to modi from the same script; needs delay
}

Exit() {
	echo " lock:screenlock"
	echo " suspend:systemctl suspend"
	echo " hibernate:systemctl hibernate"
	echo " logout:xdotool key --clearmodifiers super+shift+q"
	echo " reboot:systemctl reboot"
	echo " poweroff:systemctl poweroff"
}

Categories() {
	SUBMENU_MARK=""
	IFS='
'
	# Newline separated list, each line has format "[symbol ][alias:]category"
	# Category with alias will be shown in menu under that alias
	# The first entry below is an alias for " " so it shows all applications
desired="\
 Applications: 
 Favorites
 Accessories:Utility
 Development
 Documentation
 Education
 Graphics
 Internet:Network
 Multimedia:AudioVideo
 Office
 Settings
 System"

	# determine max line length and set tab position for subcategory mark
	maxlength=0
	for line in $desired ; do
		label="${line%:*}"
		if [ ${#label} -gt $maxlength ] ; then
			maxlength=${#label}
		fi
	done
	submenu_tab=$(($maxlength+3))
	
	present="$(grep Categories /usr/share/applications/*.desktop \
		| cut -d'=' -f2 \
		| sed 's/;/\n/g' \
		| LC_COLLATE=POSIX sort --ignore-case --unique)"

	linenumber=0
	for line in $desired ; do
		category="${line##*[ :]}"
		label="$(echo -e ${line%:*}\\t${SUBMENU_MARK} | expand -t $submenu_tab)"	## add submenu mark
		if [ $(echo "$present"|grep -w -c "$category") -gt 0 ] ; then
			echo "$label:activate_category \"$label\" \"$category\" $linenumber"
			linenumber=$(($linenumber+1))
		fi
	done
}

# Desktop menu parameters

DT_MODI="Desktop:$thisscript -menu Desktop"

Desktop() {
	echo " Terminal:default-terminal"
	echo " File Manager:xdg-open ~"
	echo " Browser:default-browser"
	#TODO determine number of lines before categories
	addlinenumber=3
	eval $(xdotool search --class rofi getwindowgeometry --shell)
	Categories|sed "s/\$/ $addlinenumber $X $Y/"	# pass additional lines number, X, Y
	echo " Search:rofi-finder.sh"
}

DT_WIDTH=200		# pixels
##TODO determine desktop menu line height according to theme
DT_LINE_HEIGHT=23	# pixels

DT_THEME="
*{
	lines:		20;
	scrollbar:	false;
	dynamic:	true;
}
#window {
	width:		${DT_WIDTH}px;
	children:	[ dt-mainbox ];
}
#mode-switcher {
	enabled:	false;
}
#button {
	width:		${DT_WIDTH}px;
	padding:	2px 1ch;
}
#inputbar {
	enabled:	false;
}"

activate_category() {	# shows drun modi filtered with category. If no command selected, returns to categories modi
	label="${1% *}"	# remove submenu mark
	category="$2"
	linenumber="$3"
	theme=""
	goback="$thisscript -show \"$CAT\""
	if [ $# -gt 3 ] ; then	# that means categories for desktop menu, number of lines before categories, X, Y
		addlinenumber=$4
		X=$5
		Y=$6
		linenumber=$(($linenumber+$addlinenumber))
		if [ $linenumber -gt 0 ] ; then
			i=$linenumber
			dummy="true"
			dummyline="textboxdummy"
			while [ $i -gt 1 ] ; do
				dummyline="textboxdummy,$dummyline"
				i=$(($i-1))
			done
		else
			dummy="false"
		fi

		# adjust X if too close to the right side of the screen
		MAX_X=$(wattr w $(lsw -r) )
		anchor="north"
		if [ $X -gt $((${MAX_X}-${DT_WIDTH}*2)) ] ; then
			anchor="${anchor}east"
			X=$MAX_X
		else
			anchor="${anchor}west"
		fi

		theme="$DT_THEME
			* {
				x-offset:	$X;
				y-offset:	$Y;
				anchor:		$anchor;
			}
			#window {
				width:		$((${DT_WIDTH}*2));
			}
			#mode-switcher {
				enabled:	true;
			}
			#boxdummy {
				enabled:	$dummy;
				children:	[ $dummyline ];
			}"
		goback="$thisscript -desktop $X $Y"
	fi
	command=$(delay; $thisscript \
				-only drun \
				-drun-match-fields categories,name \
				-display-drun "$label" \
				-filter "$category " \
				-run-command "echo {cmd}" \
				-run-shell-command "echo {terminal} -e {cmd}" \
				-theme-str "$theme")
	if [ -n "$command" ] ; then
		eval "$command" &
		exit
	fi
	# return to categories modi. No delay needed
	eval $goback &
	
	if [ $linenumber -eq 0 ] ; then	# if the category is on the top line
		exit
	fi

	# move rofi selection down by linenumber
	keys=""
	while [ $linenumber -gt 0 ] ; do
		keys="$keys Tab"
		linenumber=$(($linenumber-1))
	done	
	##TODO wait until rofi can take input
	delay
	delay
	xdotool search --class rofi key --delay 0 $keys
}

## rofi theme file can be set here 

# ROFIMENU_THEME="$HOME/.config/rofimenu/rofimenu.rasi"

