fix(insertion): macOS 插入后恢复用户剪贴板 (#525)#626
Merged
Merged
Conversation
此前 macOS 的 insert() 只写剪贴板 + post Cmd+V,完全忽略 restore_clipboard_after_paste 参数;整套剪贴板恢复机制被 #[cfg(not(target_os = "macos"))] 排除,导致设置里的 「恢复剪贴板」开关在 macOS 上无效(issue #525,实测 macOS 26.5 / M4 Pro)。 - 把剪贴板恢复机制(保存原内容 → 延迟回写 → 仅当剪贴板仍是刚插入文字时才恢复)改为 跨平台可用,去掉 cfg(not macos) 门控;insert_with_clipboard_restore 仍仅 Windows/ Linux 使用(依赖 PasteShortcut 模拟),故保留其门控。 - macOS insert() 改为:保存原剪贴板 → 写转写文字 → Cmd+V → 粘贴成功且开关开启时按 CLIPBOARD_RESTORE_DELAY 延迟恢复;粘贴失败则保留转写文字供手动粘贴、不恢复。 - CLIPBOARD_RESTORE_DELAY 合并为统一常量;删除随之失效的 macos_insert_status_after_paste。 - should_restore_clipboard 守卫测试改为跨平台运行,新增 macOS 成功路径测试。
Contributor
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
H-Chris233
approved these changes
Jun 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
背景
issue #525:开启「恢复剪贴板」后,macOS 上语音转写插入完成,系统剪贴板仍是转写文字,没有恢复为用户触发前手动复制的内容(反馈者实测 macOS 26.5 / M4 Pro,1.3.4 已默认开启该开关仍无效)。
根因
macOS 的
TextInserter::insert()只做copy_to_clipboard(text)+post_cmd_v(),完全忽略restore_clipboard_after_paste参数(其签名注释直言「两个_参数仅为对齐跨平台签名」)。而整套剪贴板恢复机制(ClipboardRestorePlan/schedule_clipboard_restore/restore_clipboard_after_delay/should_restore_clipboard…)都被#[cfg(not(target_os = "macos"))]排除,macOS 根本没有实现恢复。设置里的开关在 macOS 上是空操作。改动(仅
insertion.rs,单一职责)cfg(not macos)门控。insert_with_clipboard_restore仍仅 Windows/Linux 使用(依赖PasteShortcut模拟),保留其门控。insert()改为:保存原剪贴板 → 写转写文字 → Cmd+V → 粘贴成功且开关开启时按CLIPBOARD_RESTORE_DELAY(750ms)延迟恢复;粘贴失败则保留转写文字供用户手动粘贴、不恢复。should_restore_clipboard守卫:仅当延迟后剪贴板仍是刚插入的转写文字时才回写,避免覆盖用户期间的新复制内容。CLIPBOARD_RESTORE_DELAY合并为统一常量;删除随之失效的macos_insert_status_after_paste。验证
cargo check --manifest-path src-tauri/Cargo.toml✅cargo test --lib insertion✅(8/8):restore_only_when_clipboard_still_holds_inserted_text守卫测试改为跨平台运行;新增 macOS 成功路径测试macos_paste_success_reports_inserted_and_guards_restore。🤖 Generated with Claude Code
PR Type
Bug fix, Enhancement
Description
Enable clipboard restoration on macOS after pasting transcribed text
Save user clipboard before insertion; restore after paste with delay guard
Remove
#[cfg(not(target_os = "macos"))]gate on restore logicUpdate tests to run cross-platform for
should_restore_clipboardDiagram Walkthrough
flowchart LR A["User copies text"] --> B["Trigger voice dictation"] B --> C["Save user clipboard<br/>(previous_text)"] C --> D["Copy transcribed text<br/>to system clipboard"] D --> E["Simulate paste<br/>(Cmd+V on macOS)"] E --> F{"Paste success?"} F -- "Yes" --> G{"restore_clipboard_after_paste<br/>enabled?"} G -- "Yes" --> H["Delay 750ms"] H --> I{"Clipboard still equals<br/>transcribed text?"} I -- "Yes" --> J["Restore user clipboard<br/>(previous_text)"] I -- "No" --> K["Skip restore<br/>(user changed clipboard)"] G -- "No" --> L["Keep transcribed text<br/>in clipboard"] F -- "No" --> M["Keep transcribed text<br/>for manual paste"]File Walkthrough
insertion.rs
Refactor clipboard restore to include macOSopenless-all/app/src-tauri/src/insertion.rs
#[cfg(not(target_os = "macos"))]from clipboard restorestructures and functions, making them cross-platform.
insert()to save original clipboard, write transcribedtext, paste, and schedule restore if enabled.
macos_insert_status_after_pasteand unifiedCLIPBOARD_RESTORE_DELAYconstant.should_restore_clipboardwith positiveand negative cases.