Skip to content

Commit

Permalink
Merge pull request #18 from Gustash/fix/window-cropping
Browse files Browse the repository at this point in the history
fix: window cropping instead of pixel trimming
  • Loading branch information
Gustash authored Sep 28, 2023
2 parents f08497d + f1b68f1 commit 0c60c49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ You can install the [hyprshot](https://aur.archlinux.org/packages/hyprshot) pack

### Dependencies

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

### Manual

Expand Down
47 changes: 40 additions & 7 deletions hyprshot
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,64 @@ function send_notification() {
}

function trim() {
convert "${1}" -trim +repage "${1}"
local geometry="${1}"
local xy_str=$(echo "${geometry}" | cut -d' ' -f1)
local wh_str=$(echo "${geometry}" | cut -d' ' -f2)
local x=`echo "${xy_str}" | cut -d',' -f1`
local y=`echo "${xy_str}" | cut -d',' -f2`
local width=`echo "${wh_str}" | cut -dx -f1`
local height=`echo "${wh_str}" | cut -dx -f2`

local max_width=`hyprctl monitors -j | jq '[.[] | (.x + .width)] | max'`
local max_height=`hyprctl monitors -j | jq '[.[] | (.y + .height)] | max'`

local cropped_x=$x
local cropped_y=$y
local cropped_width=$width
local cropped_height=$height

if ((x + width > max_width)); then
cropped_width=$((max_width - x))
fi
if ((y + height > max_height)); then
cropped_height=$((max_height - y))
fi

if ((x < 0)); then
cropped_x=0
cropped_width=$((cropped_width + x))
fi
if ((y < 0)); then
cropped_y=0
cropped_height=$((cropped_height + y))
fi

printf "%s,%s %sx%s\n" \
"${cropped_x}" "${cropped_y}" \
"${cropped_width}" "${cropped_height}"
}

function save_geometry() {
Print "Geometry: %s\n" "${1}"
local cropped_geometry=`trim "${1}"`
Print "Crop: %s\n" "${cropped_geometry}"
local output=""

if [ $RAW -eq 1 ]; then
grim -g "${1}" - | trim -
grim -g "${cropped_geometry}" -
return 0
fi

if [ $CLIPBOARD -eq 0 ]; then
mkdir -p "$SAVEDIR"
grim -g "${1}" "$SAVE_FULLPATH"
grim -g "${cropped_geometry}" "$SAVE_FULLPATH"
output="$SAVE_FULLPATH"
# Trim transparent pixels, in case the window was floating and partially
# outside the monitor
trim "${output}"
wl-copy < "$output"
[ -z "$COMMAND" ] || {
"$COMMAND" "$output"
}
else
wl-copy < <(grim -g "${1}" - | trim -)
wl-copy < <(grim -g "${cropped_geometry}" -)
fi

send_notification $output
Expand Down

0 comments on commit 0c60c49

Please sign in to comment.