Skip to content

Doubao-powered voice input for Linux, Wayland and Omarchy

test(inject): 补充 X11 剪贴板恢复测试与双语说明

quanru735c38c57902
diff --git a/README.md b/README.md
index e1de0a0..e281c1d 100644
--- a/README.md
+++ b/README.md
@@ -165,8 +165,10 @@ for manual copying.
Settings → Input → **Text input method** defaults to **Clipboard paste**,
including for existing installations. Clipboard paste replaces the current
clipboard contents; clipboard managers may save the recognized text in history.
-CopyQ is not required, and no clipboard restoration or history suppression is
-performed. Choose **Direct typing** on Hyprland to keep the clipboard unchanged.
+CopyQ is not required. The app attempts to restore one original clipboard payload
+after paste if the clipboard still contains the dictation text; clipboard history
+is not suppressed. Rich text is restored as plain text when both are available,
+so formatting is lost. Choose **Direct typing** on Hyprland to keep the clipboard unchanged.
Install `wtype` separately; this mode requires a compatible
Wayland virtual-keyboard implementation and has been tested on Hyprland.
@@ -238,7 +240,11 @@ Escape and cannot prevent it reaching the foreground.
Paste and Enter are serialized on a background worker so clipboard waits do not
block the GTK interface. Before clipboard paste, the app snapshots one primary
MIME payload and restores its original bytes only if the clipboard still contains
-the dictation text. A copy made by the user during delivery is never overwritten.
+the dictation text. Plain-text formats take priority over HTML, so rich-text
+formatting is lost when both are available. An HTML-only clipboard still restores
+as HTML; images and URI lists retain their existing priority. This is best-effort
+restoration of one payload, not all original clipboard formats.
+A copy made by the user during delivery is never overwritten.
Cancellation stops remaining input and cannot undo text already delivered.
Failed recognition can preserve partial text; failed paste preserves the result.
This slot is memory-only, not a transcript history. Exiting loses it.
diff --git a/README.zh-CN.md b/README.zh-CN.md
index ade6f91..2c5efcf 100644
--- a/README.zh-CN.md
+++ b/README.zh-CN.md
@@ -126,7 +126,8 @@ API Key 通过仅限当前用户读取的独立文件保存,不进入诊断信
设置 → 输入 → **文字输入方式**,默认使用**剪贴板粘贴**,已有配置也保持此默认值。
剪贴板粘贴会替换当前剪贴板内容,剪贴板管理器可能将识别文字存入历史。
-此功能不依赖 CopyQ,也不会恢复剪贴板或排除历史记录。
+此功能不依赖 CopyQ。粘贴后,仅当剪贴板仍是本次识别文字时,应用才尝试恢复一份原始内容,
+但不会排除剪贴板历史记录。富文本同时提供纯文本时,会恢复为纯文本,原有格式会丢失。
Hyprland 下可选择**直接输入**以保持剪贴板不变。此模式需要另行安装 `wtype`,并使用支持
Wayland 虚拟键盘的桌面,已在 Hyprland 验证。
@@ -178,7 +179,9 @@ Esc 可停止后续输入,但不能撤回已输入的文字;焦点变化时
空闲时恢复正常。已有的全局 Esc 绑定不会被覆盖,无法启用拦截时会显示提示。
其他桌面目前只能监听 Esc,无法阻止它传到前台。
粘贴和回车由后台工作线程串行执行,等待剪贴板不会阻塞 GTK 界面。粘贴前会保存一份主要
-MIME 内容;仅当剪贴板仍是本次识别文字时,才按原始字节恢复。用户在输入期间复制的新内容
+MIME 内容;仅当剪贴板仍是本次识别文字时,才按原始字节恢复。纯文本优先于 HTML,
+两者同时存在时会丢失富文本格式。只有 HTML 时仍按 HTML 恢复,图片和 URI 列表的优先级不变。
+恢复只针对选中的一份内容,无法完整还原所有剪贴板格式。用户在输入期间复制的新内容
不会被旧内容覆盖。取消会停止后续输入,但无法撤回已经发送的文字。
识别失败时可能保留已收到的部分文字,粘贴失败时会保留结果。
这里只在内存中保留一次结果,不是转写历史;退出应用会丢失该结果。
diff --git a/tests/unit/test_x11_input.py b/tests/unit/test_x11_input.py
index 15e2a55..0f9c57a 100644
--- a/tests/unit/test_x11_input.py
+++ b/tests/unit/test_x11_input.py
@@ -3,11 +3,43 @@ import subprocess
from unittest import TestCase
from unittest.mock import patch
-from doubao_input.inject.injector import Injector, active_window_needs_shift
+from doubao_input.inject.injector import ClipboardSnapshot, Injector, active_window_needs_shift
from doubao_input.inject.target import focused_target
class X11InputTest(TestCase):
+ def test_rich_text_snapshot_restores_plain_text_with_xclip(self):
+ for mime_type in ("text/plain;charset=utf-8", "text/plain", "UTF8_STRING", "STRING"):
+ with self.subTest(mime_type=mime_type):
+ instance = Injector()
+ payload = b"caf\xe9" if mime_type == "STRING" else "中文\n第二行".encode()
+ formats = f"TARGETS\ntext/html\n{mime_type}\n".encode()
+ with patch("doubao_input.inject.injector.command_candidates",
+ side_effect=lambda tool: [[tool]] if tool == "xclip" else []), \
+ patch.object(instance, "_read", side_effect=[formats, payload]) as read:
+ snapshot = instance._snapshot_clipboard()
+ self.assertEqual(snapshot, ClipboardSnapshot("x11", mime_type, payload))
+ self.assertEqual(read.call_args.args[0],
+ ["xclip", "-selection", "clipboard", "-t", mime_type, "-o"])
+ with patch.object(instance, "_current_clipboard_text", return_value="dictation"), \
+ patch("doubao_input.inject.injector.command_candidates",
+ return_value=[["xclip"]]), \
+ patch("subprocess.run") as run:
+ self.assertTrue(instance._restore_clipboard_if_unchanged(snapshot, "dictation"))
+ run.assert_called_once_with(
+ ["xclip", "-selection", "clipboard", "-t", mime_type],
+ input=payload, check=True, timeout=3)
+
+ def test_html_only_snapshot_preserves_html_with_xclip(self):
+ instance = Injector()
+ with patch("doubao_input.inject.injector.command_candidates",
+ side_effect=lambda tool: [[tool]] if tool == "xclip" else []), \
+ patch.object(instance, "_read", side_effect=[b"TARGETS\ntext/html\n", b"<b>hi</b>"]) as read:
+ snapshot = instance._snapshot_clipboard()
+ self.assertEqual(snapshot, ClipboardSnapshot("x11", "text/html", b"<b>hi</b>"))
+ self.assertEqual(read.call_args.args[0],
+ ["xclip", "-selection", "clipboard", "-t", "text/html", "-o"])
+
def test_external_window_identity_and_terminal_shortcut(self):
for app_class, shift in (("Mousepad", False), ("Xfce4-terminal", True),
("com.mitchellh.ghostty", True)):