Skip to content

fix(insertion): macOS 插入后恢复用户剪贴板 (#525)#626

Merged
H-Chris233 merged 1 commit into
betafrom
fix/issue-525-macos-clipboard-restore
Jun 9, 2026
Merged

fix(insertion): macOS 插入后恢复用户剪贴板 (#525)#626
H-Chris233 merged 1 commit into
betafrom
fix/issue-525-macos-clipboard-restore

Conversation

@appergb

@appergb appergb commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

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 模拟),保留其门控。
  • macOS 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
  • 行为验证(需 TCC 权限的真机粘贴)建议在签名 bundle 上回归:手动复制文字 → 语音转写插入 → 确认剪贴板恢复为原内容。

🤖 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 logic

  • Update tests to run cross-platform for should_restore_clipboard


Diagram 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"]
Loading

File Walkthrough

Relevant files
Bug fix
insertion.rs
Refactor clipboard restore to include macOS                           

openless-all/app/src-tauri/src/insertion.rs

  • Removed #[cfg(not(target_os = "macos"))] from clipboard restore
    structures and functions, making them cross-platform.
  • Modified macOS insert() to save original clipboard, write transcribed
    text, paste, and schedule restore if enabled.
  • Deleted macos_insert_status_after_paste and unified
    CLIPBOARD_RESTORE_DELAY constant.
  • Updated macOS test to verify should_restore_clipboard with positive
    and negative cases.
+33/-45 

此前 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 成功路径测试。
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

525 - Fully compliant

Compliant requirements:

  • 在插入转写文字前保存当前剪贴板内容(通过 copy_to_clipboard_with_restore_plan 读取并记录)
  • 完成粘贴后恢复保存的剪贴板内容(通过 schedule_clipboard_restoreshould_restore_clipboard 守卫)
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected

@H-Chris233 H-Chris233 merged commit 6abc03c into beta Jun 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants