feat(src/utils): replace clipboard implementation with wl-copy and enhance copy_image support

This commit is contained in:
sankalp committed 2026-07-15 11:30:01 +05:45
1 parent 2c9a050bbb
commit 211a428cca
1 file changed
+44 -27
+44 -27
View File
@@ -5,6 +5,7 @@ import os
import re import re
import subprocess import subprocess
import threading import threading
import time
from collections.abc import Callable from collections.abc import Callable
from functools import reduce from functools import reduce
from typing import Dict, NamedTuple, Optional, TypeVar, cast from typing import Dict, NamedTuple, Optional, TypeVar, cast
@@ -131,28 +132,52 @@ def thread(target, *args, **kwargs) -> threading.Thread:
def copy_text(text: str) -> bool: def copy_text(text: str) -> bool:
try: try:
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) result = subprocess.run(
clipboard.set_text(text, -1) ["wl-copy"],
clipboard.store() input=text.encode(),
return True timeout=5,
except Exception as e:
logger.warning(
f"[functions] clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) failed: {e}"
) )
return result.returncode == 0
except FileNotFoundError:
try:
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clipboard.set_text(text, -1)
clipboard.store()
return True
except Exception as e:
logger.warning(
f"[functions] clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) failed: {e}"
)
return False
except Exception as e:
logger.warning(f"[functions] wl-copy failed: {e}")
return False return False
def copy_image(image_path: str) -> bool: def copy_image(image_path: str) -> bool:
try: try:
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) with open(image_path, "rb") as f:
image = GdkPixbuf.Pixbuf.new_from_file(image_path) image_data = f.read()
clipboard.set_image(image) result = subprocess.run(
clipboard.store() ["wl-copy", "--type", "image/png"],
return True input=image_data,
except Exception as e: timeout=5,
logger.warning(
f"[functions] clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) failed: {e}"
) )
return result.returncode == 0
except FileNotFoundError:
try:
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
image = GdkPixbuf.Pixbuf.new_from_file(image_path)
clipboard.set_image(image)
clipboard.store()
return True
except Exception as e:
logger.warning(
f"[functions] clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) failed: {e}"
)
return False
except Exception as e:
logger.warning(f"[functions] wl-copy (image) failed: {e}")
return False return False
@@ -169,18 +194,10 @@ def read_json_file(file_path: str) -> dict | None:
return None return None
def trigger_paste_shortcut(): def trigger_paste_shortcut(delay_ms: int = 50) -> bool:
try: time.sleep(delay_ms / 1000)
subprocess.Popen( result = run_command(["wtype", "-M", "ctrl", "v", "-m", "ctrl"], timeout=1)
["sh", "-c", "wl-paste | wtype -"], return result.returncode == 0
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except FileNotFoundError as e:
logger.warning(
f"[functions] subprocess.Popen( ['sh', '-c', 'wl-paste | wtype -'], std... failed: {e}"
)
def get_wifi_icon_for_strength(strength: int) -> str: def get_wifi_icon_for_strength(strength: int) -> str: