@@ -54,31 +54,64 @@ function send_notification() {
5454}
5555
5656function trim() {
57- convert " ${1} " -trim +repage " ${1} "
57+ local geometry=" ${1} "
58+ local xy_str=$( echo " ${geometry} " | cut -d' ' -f1)
59+ local wh_str=$( echo " ${geometry} " | cut -d' ' -f2)
60+ local x=` echo " ${xy_str} " | cut -d' ,' -f1`
61+ local y=` echo " ${xy_str} " | cut -d' ,' -f2`
62+ local width=` echo " ${wh_str} " | cut -dx -f1`
63+ local height=` echo " ${wh_str} " | cut -dx -f2`
64+
65+ local max_width=` hyprctl monitors -j | jq ' [.[] | (.x + .width)] | max' `
66+ local max_height=` hyprctl monitors -j | jq ' [.[] | (.y + .height)] | max' `
67+
68+ local cropped_x=$x
69+ local cropped_y=$y
70+ local cropped_width=$width
71+ local cropped_height=$height
72+
73+ if (( x + width > max_width)) ; then
74+ cropped_width=$(( max_width - x))
75+ fi
76+ if (( y + height > max_height)) ; then
77+ cropped_height=$(( max_height - y))
78+ fi
79+
80+ if (( x < 0 )) ; then
81+ cropped_x=0
82+ cropped_width=$(( cropped_width + x))
83+ fi
84+ if (( y < 0 )) ; then
85+ cropped_y=0
86+ cropped_height=$(( cropped_height + y))
87+ fi
88+
89+ printf " %s,%s %sx%s\n" \
90+ " ${cropped_x} " " ${cropped_y} " \
91+ " ${cropped_width} " " ${cropped_height} "
5892}
5993
6094function save_geometry() {
6195 Print " Geometry: %s\n" " ${1} "
96+ local cropped_geometry=` trim " ${1} " `
97+ Print " Crop: %s\n" " ${cropped_geometry} "
6298 local output=" "
6399
64100 if [ $RAW -eq 1 ]; then
65- grim -g " ${1} " - | trim -
101+ grim -g " ${cropped_geometry} " -
66102 return 0
67103 fi
68104
69105 if [ $CLIPBOARD -eq 0 ]; then
70106 mkdir -p " $SAVEDIR "
71- grim -g " ${1 } " " $SAVE_FULLPATH "
107+ grim -g " ${cropped_geometry } " " $SAVE_FULLPATH "
72108 output=" $SAVE_FULLPATH "
73- # Trim transparent pixels, in case the window was floating and partially
74- # outside the monitor
75- trim " ${output} "
76109 wl-copy < " $output "
77110 [ -z " $COMMAND " ] || {
78111 " $COMMAND " " $output "
79112 }
80113 else
81- wl-copy < <( grim -g " ${1} " - | trim -)
114+ wl-copy < <( grim -g " ${cropped_geometry} " -)
82115 fi
83116
84117 send_notification $output
0 commit comments