sane-screenshot: fix that it couldnt save screenshots on moby, by bypassing grimshot

This commit is contained in:
2024-12-14 10:59:18 +00:00
parent 9a3cb6711a
commit d1f5ac6cc1
4 changed files with 52 additions and 6 deletions

View File

@@ -15,8 +15,11 @@
"wl-clipboard"
];
sandbox.keepPids = true; #< needed by wl-clipboard
sandbox.whitelistWayland = true;
sandbox.whitelistDbus = [ "user" ];
sandbox.whitelistWayland = true;
sandbox.extraRuntimePaths = [
"sway"
];
sandbox.autodetectCliPaths = "existingFileOrParent";
};
}

View File

@@ -1,16 +1,22 @@
{ ... }:
{
sane.programs.sane-screenshot = {
sandbox.whitelistWayland = true;
sandbox.whitelistDbus = [ "user" ]; #< to send notifications
sandbox.whitelistWayland = true;
sandbox.extraHomePaths = [
"Pictures/Screenshots"
];
sandbox.extraRuntimePaths = [
"sway"
];
sandbox.keepPidsAndProc = true; #< it's required (to copy to the clipboard), but unsure why
suggestedPrograms = [
"grim"
"jq"
"libnotify"
"slurp"
"swappy"
"sway-contrib.grimshot"
# "sway-contrib.grimshot"
"util-linux"
"wl-clipboard"
];

View File

@@ -2,7 +2,7 @@
static-nix-shell.mkBash {
pname = "sane-screenshot";
srcRoot = ./.;
pkgs = [ "libnotify" "swappy" "sway-contrib.grimshot" "util-linux" "wl-clipboard" ];
pkgs = [ "grim" "jq" "libnotify" "slurp" "swappy" "sway" "util-linux" "wl-clipboard" ];
nativeBuildInputs = [
copyDesktopItems
];

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash -p libnotify -p swappy -p sway-contrib.grimshot -p util-linux -p wl-clipboard
#!nix-shell -i bash -p bash -p grim -p jq -p libnotify -p slurp -p swappy -p sway -p util-linux -p wl-clipboard
# loosely inspired by reddit user u/dragobich
# - <https://www.reddit.com/r/hyprland/comments/12tal0f/comment/jh8j4sy/>
@@ -9,6 +9,39 @@ TIMEOUT=6
OUTDIR="$HOME/Pictures/Screenshots"
DATEFMT='%Y-%m-%d-%Hh%Mm%S'
log() {
if [ -n "$SCREENSHOT_DEBUG" ]; then
echo "$@" >&2
fi
}
takeInitialShot() {
local dest="$1"
# grimshot savecopy anything "$dest"
# XXX(2024-12-13): slurp has a bug on moby such that it errors "selection cancelled"
# when the user clicks just a single point on the screen -- rather than selecting the whole window.
# hence, i'm inlining the parts of grimshot -- which call into slurp -- and reworking those, to workaround.
local all_objs=$(swaymsg -t get_tree)
log "got objects"
local visible_objs=$(echo "$all_objs" | jq -r '.. | select(.pid? and .visible?) | .rect | "\(.x),\(.y) \(.width)x\(.height)"')
log "visible objects: $visible_objs"
local selected_geom=$(echo "$visible_objs" | slurp -o)
log "selected: $selected_geom"
if [ -n "$selected_geom" ]; then
grim -g "$selected_geom" "$dest"
else
log "capturing whole screen"
grim "$dest"
fi
if [ -f "$dest" ]; then
# conditional to avoid clearing the clipboard on error
log "copying image/png to clipboard"
wl-copy --type image/png < "$dest"
fi
}
notify() {
local fullPath="$1"
# replace $HOME with ~:
@@ -29,6 +62,10 @@ handleAction() {
local fullPath="$2"
case "$action" in
Edit)
# if the user's editing the image, then the image-data we just copied to the clipboard will be stale.
# copy the image path instead, so that we give them an object that will reflect the edited image.
# if they want image-data on the clipboard, they can use the button inside swappy.
wl-copy "$fullPath"
# the save button in swappy seems to be broken (?),
# but with --output-file it'll save on exit.
swappy --file "$fullPath" --output-file "$fullPath"
@@ -57,7 +94,7 @@ delayedCancel() {
name="$(date "+$DATEFMT").png"
dest="$OUTDIR/$name"
grimshot savecopy anything "$dest"
takeInitialShot "$dest"
notify "$dest" &
printf "screenshot copied and saved to %q\n" "$dest"