#!/bin/sh
## ScreenLiner vers.1.1
## Creating horizontal or vertical measuring lines on screen
## Puppy forum: https://www.forum.puppylinux.com/viewtopic.php?p=76743
## (c) MochiMoppel,2023,2024 ; GPLv2 applies

export SCRIPTPATH=$(realpath "$0")
export htmp=/tmp/screenline.svg
export wclass=screenliner_nofocus_above
type -p gtk2dialog >/dev/null && export GD=2 	#use gtk2dialog if available (better color picker and widget alignment)

trap "
rm $htmp 2>/dev/null
# busybox pkill -f $wclass			#uncomment this line if screenlines should be removed when closing script
" EXIT

LABELV=$'            ‎▲\nDraw vertical line\n            ‎▼'
LABELH=$'\n  ◀ Draw horiz. line ‎▶  \n'

TTIP_0=$'0 = lines movable, show in taskbar\n1 = lines not movable, skip taskbar'
TTIP_1=$'To draw a line:\n1) Click on start position (a marker will appear)\n     Do NOT drag!\n2) Move cursor until desired line length reached\n3) Click on end position '
TTIP_2=$'Save Color, Thickness and Type'
TTIP_3=$'Line thickness in px'
TTIP_4=$'Quit leaves all lines in place\nTo clear lines use Clear All button before closing\nor after a restart'

## Following values will be updated by script when Save button pressed
DEF_vCOLORPICK=#ff0000
DEF_vTHICKNESS=3
DEF_ACTIVETYPE=0

function draw_line {
	x1=${vINITXY% *}
	y1=${vINITXY#* }
	if [[ $vLENGTH = squaredot ]] ;then									#1st click generates a fixed size marker
		WIDTH=7  x1=$((x1-3))
		HEIGHT=7 y1=$((y1-3))
	elif $vHORIZONTAL ;then
		((vLENGTH == 0)) && exit										#2 clicks on same spot = no length = no line = bye! 
		((vLENGTH  < 0)) && vLENGTH=${vLENGTH#-} && x1=$((x1-vLENGTH))	#vLENGTH negative when clicked from right to left
		TITLE=hline
		WIDTH=$vLENGTH
		HEIGHT=$vTHICKNESS
	else
		((vLENGTH == 0)) && exit
		((vLENGTH  < 0)) && vLENGTH=${vLENGTH#-} && y1=$((y1-vLENGTH))	#vLENGTH negative when clicked from bottom to top
		TITLE=vline
		WIDTH=$vTHICKNESS
		HEIGHT=$vLENGTH
	fi
	[[ $vLENGTH != squaredot ]] && TTIP="${vLENGTH}x$vTHICKNESS px"
	echo '<'"svg><rect width='$WIDTH' height='$HEIGHT' fill='$vCOLORPICK'/></svg>" > $htmp
	case $vWTYPEHINT in
	1)	WINDOWTYPE='type-hint="6" skip_taskbar_hint="true"' ;;
	*)	WINDOWTYPE='' ;;
	esac
	echo '<window title="'$TITLE'" '"$WINDOWTYPE"' focus-on-map="false" decorated="false" resizable="false" margin="0" tooltip-text="'"$TTIP"'"><pixmap><input file>"'$htmp'"</input></pixmap></window>' | gtk${GD}dialog --class $wclass -sG +$x1+$y1
}; export -f draw_line

function save_options {
	sed -i "
	s/^DEF_vCOLORPICK=.*/DEF_vCOLORPICK=$vCOLORPICK/
	s/^DEF_vTHICKNESS=.*/DEF_vTHICKNESS=$vTHICKNESS/
	s/^DEF_ACTIVETYPE=.*/DEF_ACTIVETYPE=$vWTYPEHINT/
	" "$SCRIPTPATH"
};export -f save_options

export MAIN_DIALOG='<window title="ScreenLiner" image-name="/usr/share/pixmaps/screenliner.svg" window-position="1">
<vbox>

	<frame Length (in px)>
		<text>
			<variable>vLENGTH</variable>
			<input>[[ $vTIMER = true ]] && { set $(getcurpos) && [[ $vHORIZONTAL = true ]] && printf %s $(($1 - ${vINITXY% *})) || printf %s  $(($2 - ${vINITXY#* })) ;} || printf 0</input>
		</text>
	</frame>
	<frame Line Options>
		<hbox space-expand="true" space-fill="true">
			<vbox>
				<text label="Color"></text>
				<colorbutton>
					<default>"'${DEF_vCOLORPICK:-red}'"</default>
					<variable>vCOLORPICK</variable>
				</colorbutton>
			</vbox>
			<vbox>
				<text label="Thickness"></text>
				<spinbutton range-min="1" range-max="1600" range-value="'${DEF_vTHICKNESS:-3}'" tooltip-text="'"$TTIP_3"'">
					<variable>vTHICKNESS</variable>
				</spinbutton>
			</vbox>
			<vbox tooltip-text="'"$TTIP_0"'">
				<text label="Type"></text>
				<comboboxtext active="'${DEF_ACTIVETYPE:-0}'">
					<variable>vWTYPEHINT</variable>
					<item>0</item>
					<item>1</item>
				</comboboxtext>
			</vbox>
			<vbox tooltip-text="'"$TTIP_2"'">
				<text label="Save"></text>
				<button>
					<label>""</label>
					<input file stock="gtk-save"></input>
					<action>save_options</action>
				</button>
			</vbox>
		</hbox>
	</frame>

	<hbox homogeneous="true" space-fill="true">
		<button label="'"$LABELV"'" tooltip-text="'"$TTIP_1"'">
			<action>clear:vHORIZONTAL</action>
			<action>xprop 1>/dev/null</action>
			<action>enable:vTIMER</action>
			<action>refresh:vINITXY</action>
			<action>xprop 1>/dev/null &</action>
		</button>
		<button label="'"$LABELH"'" tooltip-text="'"$TTIP_1"'">
			<variable>vBUTHORI</variable>
			<action>clear:vHORIZONTAL</action>
			<action>activate:vHORIZONTAL</action>
			<action>xprop 1>/dev/null</action>
			<action>enable:vTIMER</action>
			<action>refresh:vINITXY</action>
			<action>xprop 1>/dev/null &</action>
		</button>
		<checkbox visible="false">
			<variable>vHORIZONTAL</variable>
		</checkbox>
	</hbox>

	<hbox space-expand="true" space-fill="true">
		<button label="Undo ">
			<action>refresh:vLENGTH</action>
			<action>busybox pkill -nf $wclass</action>
		</button>
		<button label="Clear All">
			<action>refresh:vLENGTH</action>
			<action>busybox pkill -f $wclass</action>
		</button>
		<button label="  Quit  " tooltip-text="'"$TTIP_4"'">
			<action>exit:</action>
		</button>
	</hbox>

	<entry visible="false">
		<variable>vINITXY</variable>
		<input>[[  $vTIMER = true ]] && getcurpos</input>
		<action>[[ $vTIMER = true ]] && vLENGTH=squaredot draw_line &</action>
	</entry>
	<timer milliseconds="true" interval="100" sensitive="false" visible="false">
		<variable>vTIMER</variable>
		<action condition="command_is_true( busybox pidof xprop 1>/dev/null && echo true )">refresh:vLENGTH</action> 
		<action condition="command_is_true( busybox pidof xprop 1>/dev/null && echo true )">break:</action> 
		<action>disable:vTIMER</action>
		<action>busybox pkill -nf $wclass && draw_line &</action>
	</timer>
</vbox>
<action signal="map-event">grabfocus:vBUTHORI</action>
</window>'

GTK_THEME=Adwaita gtk${GD}dialog	#use builtin Adwaita theme for GTK3
