snapwin improvements

master
Daniel Napora 2022-02-10 21:28:53 +01:00
parent 86700dddf5
commit 1d63afbc07
3 changed files with 108 additions and 59 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash
### deskgrid - click on the window and select area to place it on the grid
# Works with active and inactive windows
# (C) Daniel Napora <napcok@gmail.com>, 2021
# (C) Daniel Napora <napcok@gmail.com>, 2021-22
# https://maboxlinux.org
#
@ -16,9 +16,7 @@ gap=16
# Grid columns (12 16 24)
columns=12
# Grid rows (6 12 16)
rows=6
#
titlebar_height=18
rows=12
#Notifications true or false
notifications=true
# Outer gap (disable if you use WM margins)
@ -31,7 +29,7 @@ source <(grep = $CONFIG_FILE)
GAP=${gap:-16}
COLUMNS=${columns:-12}
ROWS=${rows:-6}
ROWS=${rows:-12}
TITLEBAR_HEIGHT=${titlebar_height:-18}
case "$LANG" in
@ -55,13 +53,11 @@ HEX_ID=$(printf '0x%x\n' $WINDOW)
CHILD_ID=$(xwininfo -id $HEX_ID -children|grep "\"" | awk '{print $1}')
if xwininfo -id $CHILD_ID -wm |grep Dock ; then exit 0 ;fi # Ignore Dock eg. tint2
##Calculate TITLEBAR_HEIGHT - should be rewrited to use frame extends
# xprop _NET_FRAME_EXTENTS
if xwininfo -id $CHILD_ID -wm |grep Undecorated ; then # Undecorated
T="0"
else # Decorated
T="$TITLEBAR_HEIGHT"
fi
CHILD=$(printf %i $CHILD_ID)
read BORDER_L BORDER_R BORDER_T BORDER_B <<< "$(xprop -id $CHILD _NET_FRAME_EXTENTS | awk ' {gsub(/,/,"");print $3,$4,$5,$6}')"
BORDERCOMP_X=$((BORDER_L+BORDER_R))
BORDERCOMP_Y=$((BORDER_T+BORDER_B))
xdotool windowminimize --sync $WINDOW
# Screen dimensions, margins and available size
@ -97,16 +93,14 @@ if [[ $show_outer_gap = true ]]; then GF="1" ; else GF="0" ; fi
if [[ $start_x = "0" ]]; then GAP_X="$((GAP*GF))" ; else GAP_X=$((GAP/2)) ; fi
if [[ $start_y = "0" ]]; then GAP_Y="$((GAP*GF))" ; else GAP_Y=$((GAP/2)) ; fi
if [[ $end_x = $((COLUMNS-1)) ]]; then GAP_X_END="$((GAP*GF))" ; else GAP_X_END=$((GAP/2)) ; fi
if [[ $end_y = $((ROWS-1)) ]]; then GAP_Y_END="$((GAP*GF))" ; else GAP_Y_END=$((GAP/2)) ; fi
if [[ $end_x = $((COLUMNS-1)) ]]; then GAP_X_END="$((GAP/2))" ; else GAP_X_END=$((GAP/2)) ; fi
if [[ $end_y = $((ROWS-1)) ]]; then GAP_Y_END="$((GAP/2))" ; else GAP_Y_END=$((GAP/2)) ; fi
SIZE="$(((end_x-start_x+1)*(AVAIL_X/COLUMNS)-GAP_X-GAP_X_END)) $(((end_y-start_y+1)*(AVAIL_Y/ROWS)-GAP_Y-GAP_Y_END-T))"
SIZE="$(((end_x-start_x+1)*(AVAIL_X/COLUMNS)-GAP_X/2-GAP_X_END)) $(((end_y-start_y+1)*(AVAIL_Y/ROWS)-GAP_Y/2-GAP_Y_END-BORDERCOMP_Y))"
POSITION="$((start_x*AVAIL_X/COLUMNS+GAP_X+MARGIN_X)) $((start_y*AVAIL_Y/ROWS+GAP_Y+MARGIN_Y))"
POSITION="$((start_x*AVAIL_X/COLUMNS+GAP_X/2+MARGIN_X)) $((start_y*AVAIL_Y/ROWS+GAP_Y/2+MARGIN_Y))"
xdotool windowsize $WINDOW $SIZE
xdotool windowmove $WINDOW $POSITION
xdotool windowmap $WINDOW

View File

@ -10,9 +10,8 @@ gap=16
# Grid columns (12 16 24)
columns=12
# Grid rows (6 12 16)
rows=6
rows=12
#
titlebar_height=18
#Notifications true or false
notifications=true
# Outer gap (disable if you use WM margins)

View File

@ -30,9 +30,7 @@ gap=16
# Grid columns (12 16 24)
columns=12
# Grid rows (6 12 16)
rows=6
#
titlebar_height=18
rows=12
#Notifications true or false
notifications=true
# Outer gap (disable if you use WM margins)
@ -43,10 +41,9 @@ EOF
fi
source <(grep = $CONFIG_FILE)
GAP=${gap:-16}
TITLEBAR_HEIGHT=${titlebar_height:-18}
## OUTER GAP
if [[ "$show_outer_gap" == "true" ]]; then OUT_GAP="$GAP" ; else OUT_GAP="0" ; fi
if [[ "$show_outer_gap" == "true" ]]; then OUT_GAP=$((GAP/2)) ; else OUT_GAP="0" ; fi
OFFSET=$(wmctrl -d |grep "*" | awk -F' ' '{print $8}')
REALSIZE=$(wmctrl -d |grep "*" | awk -F' ' '{print $9}')
@ -58,49 +55,105 @@ TITLEBAR_HEIGHT=${titlebar_height:-18}
OFF_Y="${OFFSET#*,}"
}
_getwin() { #get active window (only when invoked by keyboard)
_config
WINDOW=$(xdotool getactivewindow)
HEX_ID=$(printf '0x%x\n' $WINDOW)
#####echo "$HEX_ID"
if xwininfo -id $HEX_ID -wm |grep Undecorated ; then # Undecorated
T="0"
else # Decorated
T="$TITLEBAR_HEIGHT"
fi
#####echo "$T"
}
_movewin() {
#echo "AVAIL_X: $AVAIL_X | OFF_X: $OFF_X | OUT_GAP: $OUT_GAP | GAP: $GAP | BORDERCOMP_X: $BORDERCOMP_X"
WIDTH_FULL=$((AVAIL_X-OUT_GAP*2-BORDERCOMP_X))
WIDTH_HALF=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X))
WIDTH_SMALL=$((AVAIL_X/3-OUT_GAP-GAP/2-BORDERCOMP_X))
WIDTH_WIDE=$((WIDTH_SMALL*2+GAP+BORDERCOMP_X))
HEIGHT_FULL=$((AVAIL_Y-OUT_GAP*2-BORDERCOMP_Y))
#echo "Width FULL: $WIDTH_FULL"
#echo "Width HALF: $WIDTH_HALF"
#echo "Width SMALL: $WIDTH_SMALL"
#echo "Width WIDE: $WIDTH_WIDE"
case $POS_CODE in
00|topleft) # top-left
W=$((AVAIL_X/2-OUT_GAP-GAP/2)) H=$((AVAIL_Y/2-T-OUT_GAP-GAP/2)) X=$((0+OFF_X+OUT_GAP)) Y=$((0+OFF_Y+OUT_GAP));;
#echo "xdotool getwindowgeometry --shell $WINDOW"
#eval xdotool getwindowgeometry --shell "$WINDOW"
W=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X)) H=$((AVAIL_Y/2-BORDERCOMP_Y-OUT_GAP-GAP/2)) XPOS=$((OFF_X+OUT_GAP)) YPOS=$((OFF_Y+OUT_GAP));
#echo -e "XPOS: $XPOS \nYPOS: $YPOS \nW: $W \nWINDOW $WINDOW"
#echo "W+BORDERCOMP_X: $((W+BORDERCOMP_X))"
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$((W+BORDERCOMP_X))" ]] && W=$WIDTH_SMALL
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -lt "$WIDTH_HALF+BORDERCOMP_X" ]] && W=$WIDTH_WIDE
;;
10|top) # top
W=$((AVAIL_X-OUT_GAP*2)) H=$((AVAIL_Y/2-T-OUT_GAP-GAP/2)) X=$((0+OFF_X+OUT_GAP)) Y=$((0+OFF_Y+OUT_GAP));;
W=$WIDTH_FULL H=$((AVAIL_Y/2-BORDERCOMP_Y-OUT_GAP-GAP/2)) XPOS=$((0+OFF_X+OUT_GAP)) YPOS=$((0+OFF_Y+OUT_GAP))
[[ "$X" -eq "$XPOS" && "$WIDTH" -eq "$WIDTH_FULL+$BORDERCOMP_X" ]] && W=$WIDTH_SMALL XPOS=$((OFF_X+AVAIL_X/2-WIDTH_SMALL/2-BORDERCOMP_X/2))
;;
20|topright) # top-right
W=$((AVAIL_X/2-OUT_GAP-GAP/2)) H=$((AVAIL_Y/2-T-OUT_GAP-GAP/2)) X=$((AVAIL_X/2+OFF_X+GAP/2)) Y=$((0+OFF_Y+OUT_GAP));;
W=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X)) H=$((AVAIL_Y/2-BORDERCOMP_Y-OUT_GAP-GAP/2)) XPOS=$((AVAIL_X/2+OFF_X+GAP/2)) YPOS=$((OFF_Y+OUT_GAP))
eval xdotool getwindowgeometry --shell "$WINDOW"
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$((W+BORDERCOMP_X))" ]] && W="$WIDTH_SMALL" XPOS=$((OFF_X+AVAIL_X-WIDTH_SMALL-OUT_GAP-BORDERCOMP_X))
# echo -e "XPOS: $XPOS \nYPOS: $YPOS \nW: $W \nWINDOW $WINDOW"
[[ "$X" -eq "$((AVAIL_X-WIDTH_SMALL+OFF_X-OUT_GAP-BORDERCOMP_X))" && "$Y" -eq "$YPOS" && "$WIDTH" -lt "$((WIDTH_HALF+BORDERCOMP_X))" ]] && W=$WIDTH_WIDE XPOS=$((AVAIL_X-WIDTH_WIDE+OFF_X-OUT_GAP-BORDERCOMP_X))
;;
01|left) # left
W=$((AVAIL_X/2-OUT_GAP-GAP/2)) H=$((AVAIL_Y-T-OUT_GAP*2)) X=$((0+OFF_X+OUT_GAP)) Y=$((0+OFF_Y+OUT_GAP));;
W=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X)) H=$((AVAIL_Y-BORDERCOMP_Y-OUT_GAP*2)) XPOS=$((0+OFF_X+OUT_GAP)) YPOS=$((0+OFF_Y+OUT_GAP))
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$((W+BORDERCOMP_X))" ]] && W=$WIDTH_SMALL
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -lt "$WIDTH_HALF+BORDERCOMP_X" ]] && W=$WIDTH_WIDE;;
11|center) # center
W=$((AVAIL_X/8*6-OFF_X)) H=$((AVAIL_Y/8*6-T)) X=$((AVAIL_X/8+OFF_X/2)) Y=$((AVAIL_Y/8+OFF_Y/2));;
HEIGHT_SMALL=$((AVAIL_Y/3-OUT_GAP-GAP/2-BORDERCOMP_Y))
W=$WIDTH_FULL H=$HEIGHT_FULL XPOS=$((OFF_X+OUT_GAP)) YPOS=$((OFF_Y+OUT_GAP))
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$WIDTH_FULL+BORDERCOMP_X" ]] && W=$WIDTH_SMALL XPOS=$((OFF_X+AVAIL_X/2-WIDTH_SMALL/2-BORDERCOMP_X/2))
;;
21|right) # right
W=$((AVAIL_X/2-OUT_GAP-GAP/2)) H=$((AVAIL_Y-T-OUT_GAP*2)) X=$((AVAIL_X/2+OFF_X+GAP/2)) Y=$((0+OFF_Y+OUT_GAP));;
W=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X)) H=$((AVAIL_Y-BORDERCOMP_Y-OUT_GAP*2)) XPOS=$((AVAIL_X/2+OFF_X+GAP/2)) YPOS=$((0+OFF_Y+OUT_GAP))
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$((W+BORDERCOMP_X))" ]] && W="$WIDTH_SMALL" XPOS=$((OFF_X+AVAIL_X-WIDTH_SMALL-OUT_GAP-BORDERCOMP_X))
[[ "$X" -eq "$((AVAIL_X-WIDTH_SMALL+OFF_X-OUT_GAP-BORDERCOMP_X))" && "$Y" -eq "$YPOS" && "$WIDTH" -lt "$((WIDTH_HALF+BORDERCOMP_X))" ]] && W=$WIDTH_WIDE XPOS=$((AVAIL_X-WIDTH_WIDE+OFF_X-OUT_GAP-BORDERCOMP_X))
;;
02|bottomleft) # bottom-left
W=$((AVAIL_X/2-OUT_GAP-GAP/2)) H=$((AVAIL_Y/2-T-OUT_GAP-GAP/2)) X=$((0+OFF_X+OUT_GAP)) Y=$((AVAIL_Y/2+OFF_Y+GAP/2));;
W=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X)) H=$((AVAIL_Y/2-BORDERCOMP_Y-OUT_GAP-GAP/2)) XPOS=$((0+OFF_X+OUT_GAP)) YPOS=$((AVAIL_Y/2+OFF_Y+GAP/2))
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$((W+BORDERCOMP_X))" ]] && W=$WIDTH_SMALL
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -lt "$WIDTH_HALF+BORDERCOMP_X" ]] && W=$WIDTH_WIDE
;;
12|bottom) # bottom
W=$((AVAIL_X-OUT_GAP*2)) H=$((AVAIL_Y/2-T-OUT_GAP-GAP/2)) X=$((0+OFF_X+OUT_GAP)) Y=$((AVAIL_Y/2+OFF_Y+GAP/2));;
W=$WIDTH_FULL H=$((AVAIL_Y/2-BORDERCOMP_Y-OUT_GAP-GAP/2)) XPOS=$((0+OFF_X+OUT_GAP)) YPOS=$((AVAIL_Y/2+OFF_Y+GAP/2))
[[ "$X" -eq "$XPOS" && "$WIDTH" -eq "$WIDTH_FULL+$BORDERCOMP_X" ]] && W=$WIDTH_SMALL XPOS=$((OFF_X+AVAIL_X/2-WIDTH_SMALL/2-BORDERCOMP_X/2))
;;
22|bottomright) # bottom-right
W=$((AVAIL_X/2-OUT_GAP-GAP/2)) H=$((AVAIL_Y/2-T-OUT_GAP-GAP/2)) X=$((AVAIL_X/2+OFF_X+GAP/2)) Y=$((AVAIL_Y/2+OFF_Y+GAP/2));;
W=$((AVAIL_X/2-OUT_GAP-GAP/2-BORDERCOMP_X)) H=$((AVAIL_Y/2-BORDERCOMP_Y-OUT_GAP-GAP/2)) XPOS=$((AVAIL_X/2+OFF_X+GAP/2)) YPOS=$((AVAIL_Y/2+OFF_Y+GAP/2))
[[ "$X" -eq "$XPOS" && "$Y" -eq "$YPOS" && "$WIDTH" -eq "$((W+BORDERCOMP_X))" ]] && W="$WIDTH_SMALL" XPOS=$((OFF_X+AVAIL_X-WIDTH_SMALL-OUT_GAP-BORDERCOMP_X))
[[ "$X" -eq "$((AVAIL_X-WIDTH_SMALL+OFF_X-OUT_GAP-BORDERCOMP_X))" && "$Y" -eq "$YPOS" && "$WIDTH" -lt "$((WIDTH_HALF+BORDERCOMP_X))" ]] && W=$WIDTH_WIDE XPOS=$((AVAIL_X-WIDTH_WIDE+OFF_X-OUT_GAP-BORDERCOMP_X));;
esac
xdotool windowsize $WINDOW $W $H
xdotool windowmove $WINDOW $X $Y
xdotool windowmove $WINDOW $XPOS $YPOS
#echo "Po akcji =============="
#eval xdotool getwindowgeometry --shell "$WINDOW"
if [ $activate_window == "true" ]; then xdotool windowactivate $WINDOW; fi
}
_getwin() { #get active window (only when invoked by keyboard)
_config
WIN=$(xdotool getactivewindow)
echo "Win: $WIN"
HEX_ID=$(printf '0x%x\n' $WIN)
echo $HEX_ID
#####echo "$HEX_ID"
wmctrl -i -r $HEX_ID -b remove,maximized_vert,maximized_horz
WINDOW=$(xwininfo -id $(xdotool getactivewindow) -int -tree | awk '/^ *Parent/ {print $4}')
echo "WINDOW: $WINDOW"
#WINDOW=$(xwininfo -id $WIN -int -tree | awk '/^ *Parent/ {print $4}')
#WINDOW=$WIN
winFRAME=$(xprop -id $WIN _NET_FRAME_EXTENTS | awk ' {gsub(/,/,"");print $3,$4,$5,$6}')
read BORDER_L BORDER_R BORDER_T BORDER_B <<< "$winFRAME"
echo "winFRAME: $winFRAME"
BORDERCOMP_X=$((BORDER_L+BORDER_R))
BORDERCOMP_Y=$((BORDER_T+BORDER_B))
echo "BORDERCOMP_X $BORDERCOMP_X"
echo "Przekazuję ID OKNA: $WINDOW"
_movewin
}
clicksnap() {
_config
#xdotool windowactivate $WINDOW
### Clicksnap mouse action start
eval $(xdotool getmouselocation --shell)
Mouse_x="$X"
@ -110,11 +163,6 @@ clicksnap() {
wmctrl -i -r $HEX_ID -b remove,maximized_vert,maximized_horz
CHILD_ID=$(xwininfo -id $HEX_ID -children|grep "\"" | awk '{print $1}')
if xwininfo -id $CHILD_ID -wm |grep Dock ; then exit 0 ;fi # Ignore Dock eg. tint2
if xwininfo -id $CHILD_ID -wm |grep Undecorated ; then # Undecorated
T="0"
else # Decorated
T="$TITLEBAR_HEIGHT"
fi
eval $(xdotool getwindowgeometry --shell $WINDOW)
Win_x="$X"
@ -131,15 +179,23 @@ wmctrl -i -r $HEX_ID -b remove,maximized_vert,maximized_horz
pos_y="$((Mouse_y*3/AVAIL_Y))"
POS_CODE="$pos_x$pos_y"
fi
#xdotool windowminimize $WINDOW
CHILD=$(printf %i $CHILD_ID)
#echo "CHILD Potrzebny dla FRAME: $CHILD"
read BORDER_L BORDER_R BORDER_T BORDER_B <<< "$(xprop -id $CHILD _NET_FRAME_EXTENTS | awk ' {gsub(/,/,"");print $3,$4,$5,$6}')"
BORDERCOMP_X=$((BORDER_L+BORDER_R))
BORDERCOMP_Y=$((BORDER_T+BORDER_B))
#echo "BORDERCOMP_X $BORDERCOMP_X"
#echo "BORDERCOMP_Y $BORDERCOMP_Y"
#echo "Przekazuję ID OKNA: $WINDOW"
_movewin
}
moveto() {
_getwin
POS_CODE="$1"
_movewin
_getwin
}
usage() {
grep "^#:" $0 | while read DOC; do printf '%s\n' "${DOC###:}"; done