#!/bin/bash
#Script to toggle on/off compositor, createdy by PPC, 5/7/2023, for antiX-23, full GPL license

main_window_on(){
#Make sure any previously existing window of this kind is killed, or they will stack up and fill the RAM	
eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
sleep 0.1

#tooltip=$"bla bla bla"
slider_text=$"Xcompmgr"
	#Close any previously running window for this GUI:
xdotool search "Generic Toggle for antiX" windowclose
	export -f main_window_off
switch="/usr/share/pixmaps/on.png"

#What the script should do when the switch is toggle to "on"
 xcompmgr -f -F &

#Main window
yad --form --undecorated \
	--title='Generic Toggle for antiX' --center --window-icon="/usr/share/icons/papirus-antix/48x48/actions/cm_options.png"\
	--columns=1 \
	--field="$slider_text!$switch!$tooltip":BTN "bash -c main_window_off " \
	--button=' x ' --width=250    
	eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
	sleep 0.1
}

main_window_off(){
#Make sure any previously existing window of this kind is killed, or they will stack up and fill the RAM	
eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
sleep 0.1	

#tooltip=$"bla bla bla"
slider_text="Xcompmgr"
		#Close any previously running window for this GUI:
xdotool search "Generic Toggle for antiX" windowclose
		export -f main_window_on
switch="/usr/share/pixmaps/off.png"
		
#What the script should do when the switch is toggle to "off"
pkill xcompmgr &
		
#Main window
yad --form --undecorated \
	--title='Generic Toggle for antiX' --center --window-icon="/usr/share/icons/papirus-antix/48x48/actions/cm_options.png"\
	--columns=1 \
	--field="$slider_text!$switch!$tooltip":BTN "bash -c main_window_on " \
	--button=' x ' --width=250   
	eval kill $(ps aux| grep "Generic Toogle for antiX"| grep -v "grep" | awk '{print $2}')
	sleep 0.1
}

#Export functions, so they can be used from inside other functions
export -f main_window_off main_window_on

#Read the current state of what the GUI toogles on/off and show the relevant Main Window (with the switch toggled to on or to off) 
if pgrep xcompmgr &>/dev/null; then
 main_window_on

else
 main_window_off

fi

#Kill all yad windows of this kind and exit
eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
sleep 0.5
#xdotool search "Generic Toggle for antiX" windowclose
exit
