refactor: convert inserter module to package with wayland backend

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vitali Graf
2026-04-08 10:34:07 +02:00
parent f9402c427c
commit b47045ab9b
4 changed files with 41 additions and 33 deletions
+19
View File
@@ -0,0 +1,19 @@
"""Text-Einfügung — plattformspezifische Backends hinter gemeinsamem Interface."""
import sys
from typing import Protocol, runtime_checkable
@runtime_checkable
class Inserter(Protocol):
async def insert(self, text: str) -> None: ...
def create_inserter() -> Inserter:
"""Erstellt den plattformspezifischen Inserter."""
if sys.platform == "linux":
from whisper_local.inserter._wayland import WaylandInserter
return WaylandInserter()
else:
from whisper_local.inserter._win32 import Win32Inserter
return Win32Inserter()
@@ -1,14 +1,14 @@
"""Text-Einfügung via wl-copy + ydotool Ctrl+V."""
"""Text-Einfügung via wl-copy + ydotool Ctrl+V (Linux/Wayland)."""
import asyncio
import logging
logger = logging.getLogger(__name__)
PASTE_DELAY = 0.2 # Sekunden Wartezeit vor Clipboard-Restore
PASTE_DELAY = 0.2
class Inserter:
class WaylandInserter:
async def _run_capture(self, *cmd: str) -> bytes:
"""Führt einen Befehl aus und gibt stdout zurück."""
proc = await asyncio.create_subprocess_exec(
@@ -35,20 +35,12 @@ class Inserter:
if not text:
return
# 1. Aktuelle Zwischenablage sichern
old_clipboard = await self._run_capture("wl-paste", "--no-newline")
try:
# 2. Text in Zwischenablage kopieren
await self._run_fire("wl-copy", "--", text)
# 3. Kurz warten damit Clipboard bereit ist
await asyncio.sleep(0.05)
# 4. Ctrl+V simulieren via ydotool (Ctrl=29, V=47)
await self._run_fire("ydotool", "key", "29:1", "47:1", "47:0", "29:0")
# 5. Warten, dann Clipboard wiederherstellen
await asyncio.sleep(PASTE_DELAY)
finally:
await self._run_fire("wl-copy", "--", old_clipboard.decode())