fix: screenshot on hyprland

This commit is contained in:
2025-10-08 15:04:27 +02:00
parent ab3023a7e0
commit 12ca32e0d4
5 changed files with 26 additions and 28 deletions

View File

@@ -10,7 +10,6 @@ full KDE Plasma 6 setup # non-essential for sure
# hypr* # hypr*
hyprland hyprland
hyprpaper
hypridle hypridle
hyprlock hyprlock
hyprshot hyprshot
@@ -29,6 +28,7 @@ kitty # normal terminal
ghostty # floating terminal, for btop for example ghostty # floating terminal, for btop for example
# under surface # under surface
swww # wallpaper daemon
mako # notification daemon mako # notification daemon
gnome-keyring # --password-store=gnome-libsecret gnome-keyring # --password-store=gnome-libsecret
wl-clipboard wl-clipboard

View File

@@ -2,5 +2,4 @@ things I have installed:
everything in `./hyprland-ricing.txt`, in addition to: everything in `./hyprland-ricing.txt`, in addition to:
swww
xwayland-satellite xwayland-satellite

View File

@@ -5,6 +5,7 @@ import os
from datetime import datetime from datetime import datetime
from enum import Enum from enum import Enum
import time import time
from pathlib import Path
# autopep8: off # autopep8: off
import gi import gi
@@ -19,23 +20,20 @@ class ScreenshotType(Enum):
WINDOW = "window" WINDOW = "window"
SCREENSHOT_DIR = os.path.join( SCREENSHOT_DIR = Path.home() / "Pictures" / "Screenshots"
os.environ.get("XDG_PICTURES_DIR", os.path.expanduser("~/Pictures")),
"Screenshots"
)
def wait_until_file_exists(filepath: str, timeout: int = 5): def wait_until_file_exists(filepath: Path, timeout: int = 5):
"""Wait until a file exists or timeout.""" """Wait until a file exists or timeout."""
start_time = time.time() start_time = time.time()
while not os.path.isfile(filepath): while not filepath.exists():
if time.time() - start_time > timeout: if time.time() - start_time > timeout:
return False return False
time.sleep(0.1) time.sleep(0.1)
return True return True
def take_screenshot(typeStr: str, filepath: str): def take_screenshot(filepath: Path, typeStr: str):
type = ScreenshotType(typeStr) type = ScreenshotType(typeStr)
currentDesktop = os.environ.get("XDG_CURRENT_DESKTOP", "") currentDesktop = os.environ.get("XDG_CURRENT_DESKTOP", "")
if "Hyprland" in currentDesktop: if "Hyprland" in currentDesktop:
@@ -44,7 +42,7 @@ def take_screenshot(typeStr: str, filepath: str):
ScreenshotType.AREA: f"hyprshot -z -m region -o {SCREENSHOT_DIR} -f ", ScreenshotType.AREA: f"hyprshot -z -m region -o {SCREENSHOT_DIR} -f ",
ScreenshotType.WINDOW: f"hyprshot -z -m window -o {SCREENSHOT_DIR} -f ", ScreenshotType.WINDOW: f"hyprshot -z -m window -o {SCREENSHOT_DIR} -f ",
} }
os.system(f"{cmd[type]}{filepath}") os.system(f"{cmd[type]}{filepath.name}")
wait_until_file_exists(filepath) wait_until_file_exists(filepath)
elif "niri" in currentDesktop: elif "niri" in currentDesktop:
cmd = { cmd = {
@@ -52,24 +50,24 @@ def take_screenshot(typeStr: str, filepath: str):
ScreenshotType.AREA: f"niri msg action screenshot", ScreenshotType.AREA: f"niri msg action screenshot",
ScreenshotType.WINDOW: f"niri msg action screenshot-window", ScreenshotType.WINDOW: f"niri msg action screenshot-window",
} }
niriScreenshotPath = SCREENSHOT_DIR + os.sep + ".niri_screenshot.png" niriScreenshotPath = SCREENSHOT_DIR / ".niri_screenshot.png"
if os.path.isfile(niriScreenshotPath): if niriScreenshotPath.exists():
os.remove(niriScreenshotPath) niriScreenshotPath.unlink()
os.system(cmd[type]) os.system(cmd[type])
wait_until_file_exists(niriScreenshotPath) wait_until_file_exists(niriScreenshotPath)
if os.path.isfile(niriScreenshotPath): if niriScreenshotPath.exists():
os.rename(niriScreenshotPath, filepath) niriScreenshotPath.rename(filepath)
wait_until_file_exists(filepath) wait_until_file_exists(filepath)
else: else:
print("Unsupported desktop environment.") print("Unsupported desktop environment.")
exit(1) exit(1)
def edit_screenshot(filepath: str): def edit_screenshot(filepath: Path):
os.system(f"gradia {SCREENSHOT_DIR}{os.sep}{filepath}") os.system(f"gradia {filepath}")
def file_name(dir, prefix="screenshot", ext=".png"): def file_name(dir: Path, prefix="screenshot", ext=".png"):
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
return f"{prefix}_{timestamp}{ext}" return f"{prefix}_{timestamp}{ext}"
@@ -84,15 +82,15 @@ if __name__ == "__main__":
args = parser.parse_args() args = parser.parse_args()
# file path # file path
os.makedirs(SCREENSHOT_DIR, exist_ok=True) SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)
filename = file_name(SCREENSHOT_DIR) filename = file_name(SCREENSHOT_DIR)
filepath = os.path.join(SCREENSHOT_DIR, filename) filepath = SCREENSHOT_DIR / filename
# take screenshot # take screenshot
take_screenshot(args.type, filepath) take_screenshot(filepath, args.type)
# check if successful # check if successful
if not os.path.isfile(filepath): if not filepath.exists():
print("Failed to take screenshot.") print("Failed to take screenshot.")
exit(1) exit(1)
@@ -104,7 +102,7 @@ if __name__ == "__main__":
def edit_callback(n, action, user_data): def edit_callback(n, action, user_data):
global editing global editing
editing = True editing = True
edit_screenshot(filename) edit_screenshot(filepath)
n.close() n.close()
loop.quit() loop.quit()

View File

@@ -3,7 +3,8 @@ exec-once = config-switch Hyprland
# Bar, wallpaper # Bar, wallpaper
exec-once = waybar exec-once = waybar
exec-once = hyprpaper exec-once = swww-daemon -n background
exec-once = sleep 1 && swww img -n background $(find $HOME/.local/share/wallpaper/current -type f | head -n 1) --transition-type fade --transition-duration 2
# Input method # Input method
exec-once = fcitx5 exec-once = fcitx5

View File

@@ -176,10 +176,10 @@ xwayland {
force_zero_scaling = true force_zero_scaling = true
} }
# debug { debug {
# overlay = true # overlay = true
# disable_logs = false disable_logs = false
# } }
experimental { experimental {
xx_color_management_v4 = true xx_color_management_v4 = true