Skip to content

Commit 0c60c49

Browse files
authored
Merge pull request #18 from Gustash/fix/window-cropping
fix: window cropping instead of pixel trimming
2 parents f08497d + f1b68f1 commit 0c60c49

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ You can install the [hyprshot](https://aur.archlinux.org/packages/hyprshot) pack
1616

1717
### Dependencies
1818

19-
- hyprland (this one should be obvious)
20-
- jq (to parse and manipulate json)
21-
- grim (to take the screenshot)
22-
- slurp (to select what to screenshot)
23-
- wl-clipboard (to copy screenshot to clipboard)
24-
- libnotify (to get notified when a screenshot is saved)
25-
- imagemagick (to trim excess transparent pixels when window is partially off-screen)
19+
- hyprland (this one should be obvious)
20+
- jq (to parse and manipulate json)
21+
- grim (to take the screenshot)
22+
- slurp (to select what to screenshot)
23+
- wl-clipboard (to copy screenshot to clipboard)
24+
- libnotify (to get notified when a screenshot is saved)
2625

2726
### Manual
2827

hyprshot

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,64 @@ function send_notification() {
5454
}
5555

5656
function 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

6094
function 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

Comments
 (0)