mabox-tools/bin/clicksnap

97 lines
3.1 KiB
Plaintext
Raw Normal View History

2021-05-03 18:01:21 +02:00
#!/bin/bash
2021-05-04 18:37:00 +02:00
### clicksnap - click on the appropriate area of the window to snap it in a given direction.
# Works with active and inactive windows
# Author: Daniel Napora <napcok@gmail.com>
# https://maboxlinux.org
#
2021-05-15 02:38:38 +02:00
CONFIG_DIR="$HOME/.config/deskgrid"
CONFIG_FILE="$CONFIG_DIR/deskgrid.conf"
mkdir -p $CONFIG_DIR
if [ ! -f $CONFIG_FILE ]; then
cat <<EOF > ${CONFIG_FILE}
# Gap between windows in pixels (reasonable values: 0 8 16 24)
gap=16
# Grid columns (12 16 24)
grid_x=16
# Grid rows (6 12 16)
grid_y=12
#
titlebar_height=18
#Notifications true or false
enable_notifications=true
# Outer gap (disable if you use WM margins)
show_outer_gap=true
# Only for clicksnap action
activate_window=false
EOF
fi
source <(grep = $CONFIG_FILE)
2021-05-03 18:01:21 +02:00
2021-05-15 02:38:38 +02:00
GAP=${gap:-16}
TITLEBAR_HEIGHT=${titlebar_height:-18}
2021-05-07 12:42:30 +02:00
2021-05-03 18:01:21 +02:00
eval $(xdotool getmouselocation --shell)
Mouse_x="$X"
Mouse_y="$Y"
2021-05-04 18:37:00 +02:00
HEX_ID=$(printf '0x%x\n' $WINDOW)
CHILD_ID=$(xwininfo -id $HEX_ID -children|grep "\"" | awk '{print $1}')
2021-05-04 22:50:19 +02:00
if xwininfo -id $CHILD_ID -wm |grep Dock ; then exit 0 ;fi # Ignore Dock eg. tint2
2021-05-04 18:37:00 +02:00
if xwininfo -id $CHILD_ID -wm |grep Undecorated ; then # Undecorated
T="0"
else # Decorated
T="$TITLEBAR_HEIGHT"
fi
2021-05-07 12:42:30 +02:00
## OUTER GAP
if [[ "$show_outer_gap" == "true" ]]; then OUT_GAP="$GAP" ; else OUT_GAP="0" ; fi
echo "OUT_GAP: $OUT_GAP"
2021-05-03 18:01:21 +02:00
eval $(xdotool getwindowgeometry --shell $WINDOW)
Win_x="$X"
Win_y="$Y"
Win_width="$WIDTH"
Win_height="$HEIGHT"
Rel_x="$((Mouse_x-Win_x))"
Rel_y="$((Mouse_y-Win_y))"
pos_x="$(((Mouse_x-Win_x)/(Win_width/3)))"
pos_y="$(((Mouse_y-Win_y)/(Win_height/3)))"
POS_CODE="$pos_x$pos_y"
OFFSET=$(wmctrl -d |grep "*" | awk -F' ' '{print $8}')
REALSIZE=$(wmctrl -d |grep "*" | awk -F' ' '{print $9}')
AVAIL_X="${REALSIZE%x*}"
AVAIL_Y="${REALSIZE#*x}"
OFF_X="${OFFSET%,*}"
OFF_Y="${OFFSET#*,}"
case $POS_CODE in
00) # top-left
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
10) # top
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
20) # top-right
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
01) # left
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
11) # center
2021-05-04 18:37:00 +02:00
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));;
2021-05-03 18:01:21 +02:00
21) # right
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
02) # bottom-left
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
12) # bottom
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
22) # bottom-right
2021-05-07 12:42:30 +02:00
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));;
2021-05-03 18:01:21 +02:00
esac
2021-05-04 18:37:00 +02:00
xdotool windowsize $WINDOW $W $H
2021-05-03 18:01:21 +02:00
xdotool windowmove $WINDOW $X $Y
if [ $activate_window == "true" ]; then xdotool windowactivate $WINDOW; fi