Skip to content

Commit 22a0b23

Browse files
authored
Merge pull request #146 from IchenDEV/cursor/compat-paste-mode
feat: add a compatibility mode that pastes into inputs Utter cannot inspect, such as WeChat
2 parents f5f07bb + 9de8039 commit 22a0b23

24 files changed

Lines changed: 653 additions & 12 deletions

‎Sources/UtterContracts/DeliveryServices.swift‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import UtterRuntime
33

44
package enum DeliveryDisposition: Equatable { case notCommitted, accepted, uncertain }
55
package enum DeliveryEffect: Hashable { case none, clipboard, paste, keyPress, accessibility, keyboardInsertion }
6-
package enum DeliveryConfirmation: String, Codable, Sendable { case none, clipboardValue, targetValue }
6+
/// `unobservable`: the target gives no way to read its content, so a paste was posted once and cannot be verified.
7+
package enum DeliveryConfirmation: String, Codable, Sendable { case none, clipboardValue, targetValue, unobservable }
78
package enum DeliveryStatus: String, Codable, Sendable { case inserted, copied, uncertain, notDelivered }
89

910
@MainActor
@@ -40,11 +41,15 @@ package struct DeliveryRequest {
4041
package let command: DeliveryCommand
4142
package let target: (any OutputTargetLease)?
4243
package let allowsClipboardPaste: Bool
43-
package init(id: UUID, command: DeliveryCommand, target: (any OutputTargetLease)? = nil, allowsClipboardPaste: Bool = true) {
44+
/// Compatibility mode: may paste once into an app that exposes no focused element.
45+
package let allowsOpaquePaste: Bool
46+
package init(id: UUID, command: DeliveryCommand, target: (any OutputTargetLease)? = nil, allowsClipboardPaste: Bool = true,
47+
allowsOpaquePaste: Bool = false) {
4448
self.id = id
4549
self.command = command
4650
self.target = target
4751
self.allowsClipboardPaste = allowsClipboardPaste
52+
self.allowsOpaquePaste = allowsOpaquePaste && allowsClipboardPaste
4853
}
4954
}
5055

@@ -73,6 +78,7 @@ package struct DeliveryReceipt {
7378
}
7479
package var status: DeliveryStatus {
7580
if isConfirmedInsertion { return .inserted }
81+
if disposition == .accepted, confirmation == .unobservable { return .uncertain }
7682
if disposition == .accepted, confirmation == .clipboardValue { return .copied }
7783
return disposition == .notCommitted ? .notDelivered : .uncertain
7884
}
@@ -88,8 +94,9 @@ package struct DeliveryReceipt {
8894
let committed = (effects ?? (effect == .none ? [] : [effect])).subtracting([.none])
8995
let insertionConfirmed = confirmation == .targetValue && !committed.isDisjoint(with: [.accessibility, .paste, .keyPress, .keyboardInsertion])
9096
let copyConfirmed = confirmation == .clipboardValue && committed == [.clipboard]
97+
let pasteUnobserved = confirmation == .unobservable && committed.contains(.paste)
9198
self.disposition = committed.isEmpty ? .notCommitted
92-
: disposition == .accepted && (insertionConfirmed || copyConfirmed) ? .accepted : .uncertain
99+
: disposition == .accepted && (insertionConfirmed || copyConfirmed || pasteUnobserved) ? .accepted : .uncertain
93100
self.effects = committed
94101
self.confirmation = self.disposition == .accepted ? confirmation : .none
95102
self.reason = reason

‎Sources/UtterContracts/Resources/en.lproj/Localizable.strings‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@
656656
"mode.selection_edit" = "Edit selection";
657657
"settings.allow_clipboard_paste" = "Allow clipboard paste";
658658
"settings.allow_clipboard_paste_help" = "Use clipboard paste when direct text insertion is unavailable. Clipboard managers may record dictated text. When disabled, Utter offers an explicit copy instead.";
659+
"settings.allow_opaque_paste" = "Compatibility mode: paste into inputs Utter cannot inspect";
660+
"settings.allow_opaque_paste_help" = "For apps such as WeChat whose input box Utter cannot read. Utter pastes once and cannot confirm the result; the dictated text stays on the clipboard. Requires Allow clipboard paste.";
661+
"delivery.opaque_target" = "This app does not let Utter write into it directly. Copy the text, or turn on compatibility mode in Settings.";
662+
"delivery.compat_pasted" = "Pasted in compatibility mode. Utter cannot confirm the result.";
659663
"delivery.target_changed" = "The insertion target changed. Copy the text or start again.";
660664
"delivery.unconfirmed" = "Insertion could not be confirmed. The text is available to copy; it will not be inserted again automatically.";
661665
"delivery.unavailable" = "This field does not support direct editing.";

‎Sources/UtterContracts/Resources/zh-Hans.lproj/Localizable.strings‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@
656656
"mode.selection_edit" = "编辑选中文本";
657657
"settings.allow_clipboard_paste" = "允许剪贴板粘贴";
658658
"settings.allow_clipboard_paste_help" = "无法直接写入时使用剪贴板粘贴。剪贴板管理器可能记录口述内容。关闭后,Utter 提供显式复制。";
659+
"settings.allow_opaque_paste" = "兼容模式:向无法检查的输入框粘贴";
660+
"settings.allow_opaque_paste_help" = "用于微信等 Utter 读不到内容的输入框。Utter 只粘贴一次,无法确认是否成功,口述文本会留在剪贴板里。需要先开启“允许剪贴板粘贴”。";
661+
"delivery.opaque_target" = "此应用不允许 Utter 直接写入。可以复制文本,或在设置里开启兼容模式。";
662+
"delivery.compat_pasted" = "已按兼容模式粘贴,Utter 无法确认结果。";
659663
"delivery.target_changed" = "写入目标已变化。可以复制文本,或重新开始。";
660664
"delivery.unconfirmed" = "无法确认是否已写入。可以复制文本,Utter 不会自动再次写入。";
661665
"delivery.unavailable" = "此输入框不支持直接编辑。";

‎Sources/UtterContracts/SessionPresentation.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package struct SessionPresentation: Equatable, Sendable {
2525
case .delivering: status = .delivering; message = L("pipeline.inserting")
2626
case .completed where snapshot.deliveryStatus == .inserted: status = .inserted; message = L("status.done")
2727
case .completed where snapshot.deliveryStatus == .copied: status = .copied; message = L("delivery.copied")
28+
case .completed where snapshot.deliveryStatus == .uncertain: status = .inserted; message = L("delivery.compat_pasted")
2829
case .completed where snapshot.deliveryStatus == nil: status = .idle; message = L("status.ready")
2930
case .failed where snapshot.deliveryStatus == .uncertain:
3031
status = .uncertain; message = snapshot.error ?? L("delivery.uncertain")

‎Sources/UtterContracts/SettingsValues.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ package struct SettingsValues: Equatable, Sendable {
3737
package var customStylePrompt: String = LanguageStyle.custom.defaultPrompt
3838
package var playSounds: Bool = true
3939
package var allowClipboardPaste: Bool = true
40+
package var allowOpaqueTargetPaste: Bool = false
4041
package var enableStreamingRecognitionBeta: Bool = true
4142
package var inputLanguage: InputLanguage = .chinese
4243
package var translationTargetLanguage: TranslationLanguage = .english

‎Sources/UtterContracts/VoiceInputSettings.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package struct VoiceInputSettings {
88
package let outputMode: OutputMode
99
package let enableInstantInsert: Bool
1010
package let allowClipboardPaste: Bool
11+
/// Compatibility mode only means something while clipboard paste is allowed.
12+
package let allowOpaqueTargetPaste: Bool
1113
package let translationTargetLanguage: TranslationLanguage
1214
package let enableMemory: Bool
1315
package let memoryWindowMinutes: Int
@@ -29,6 +31,7 @@ package struct VoiceInputSettings {
2931
outputMode = settings.outputMode
3032
enableInstantInsert = settings.enableInstantInsert
3133
allowClipboardPaste = settings.allowClipboardPaste
34+
allowOpaqueTargetPaste = settings.allowOpaqueTargetPaste && settings.allowClipboardPaste
3235
translationTargetLanguage = settings.translationTargetLanguage
3336
enableMemory = settings.enableMemory
3437
memoryWindowMinutes = settings.memoryWindowMinutes

‎Sources/UtterData/SettingsStore+Load.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extension SettingsStore {
99
case remoteKeyRemapEnabled, remoteButtonMappingEnabled, remoteButtonProfile
1010
case audioGateSensitivity, audioWeakSpeechSensitivity
1111
case enableStreamingRecognitionBeta
12-
case allowClipboardPaste
12+
case allowClipboardPaste, allowOpaqueTargetPaste
1313
case inputLanguage, translationTargetLanguage
1414
case useScreenContext, screenContextMode, enableInstantInsert, hasCompletedOnboarding, uiLanguage, historyRetention
1515
case enableMemory, memoryWindowMinutes, enableCorrectionLearning, industryLexicon
@@ -90,6 +90,7 @@ extension SettingsStore {
9090
}
9191
values.playSounds = ud.object(forKey: Key.playSounds.rawValue) as? Bool ?? true
9292
values.allowClipboardPaste = ud.object(forKey: Key.allowClipboardPaste.rawValue) as? Bool ?? true
93+
values.allowOpaqueTargetPaste = ud.object(forKey: Key.allowOpaqueTargetPaste.rawValue) as? Bool ?? false
9394
values.enableStreamingRecognitionBeta = ud.object(forKey: Key.enableStreamingRecognitionBeta.rawValue) as? Bool ?? true
9495
values.inputLanguage = InputLanguage(rawValue: ud.string(forKey: Key.inputLanguage.rawValue) ?? "") ?? .chinese
9596
values.translationTargetLanguage = TranslationLanguage(

‎Sources/UtterData/SettingsStore+Persistence.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extension SettingsStore {
2828
defaults.set(values.customStylePrompt, forKey: Key.customStylePrompt.rawValue)
2929
defaults.set(values.playSounds, forKey: Key.playSounds.rawValue)
3030
defaults.set(values.allowClipboardPaste, forKey: Key.allowClipboardPaste.rawValue)
31+
defaults.set(values.allowOpaqueTargetPaste, forKey: Key.allowOpaqueTargetPaste.rawValue)
3132
defaults.set(values.enableStreamingRecognitionBeta, forKey: Key.enableStreamingRecognitionBeta.rawValue)
3233
defaults.set(values.inputLanguage.rawValue, forKey: Key.inputLanguage.rawValue)
3334
defaults.set(values.translationTargetLanguage.rawValue, forKey: Key.translationTargetLanguage.rawValue)

‎Sources/UtterMacServices/ClipboardPasteTransaction.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ enum ClipboardPasteTransaction {
2020
confirmation: written && pasteboard.text == text ? .clipboardValue : .none)
2121
}
2222

23+
/// One paste into a target nobody can read. There is nothing to confirm, so the text stays on the clipboard
24+
/// and nothing is ever retried.
25+
static func pasteUnobserved(_ text: String, pasteboard: any DeliveryPasteboard,
26+
canCommit: () -> Bool, mark: (DeliveryEffect) -> Bool,
27+
postPaste: () -> Bool,
28+
beforePaste: () async -> Void = { await Task.yield() }) async -> DeliveryCompletion {
29+
guard canCommit(), mark(.clipboard) else { return DeliveryCompletion(disposition: .notCommitted) }
30+
guard pasteboard.write(text) else { return DeliveryCompletion(disposition: .uncertain) }
31+
let ownedChange = pasteboard.changeCount
32+
await beforePaste()
33+
guard canCommit(), pasteboard.changeCount == ownedChange, pasteboard.text == text, mark(.paste),
34+
postPaste() else { return DeliveryCompletion(disposition: .uncertain) }
35+
return DeliveryCompletion(disposition: .accepted, confirmation: .unobservable)
36+
}
37+
2338
static func paste(_ text: String, pasteboard: any DeliveryPasteboard,
2439
canCommit: () -> Bool, mark: (DeliveryEffect) -> Bool,
2540
postPaste: () -> Bool, confirm: @escaping () async -> Bool,

‎Sources/UtterMacServices/DeliveryRoute.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ enum DeliveryPath: String {
55
case paste
66
case keyPress
77
case clipboardOnly
8+
case compatPaste
89
}
910

1011
enum DeliveryTargetKind: String {
1112
case web
1213
case native
14+
case opaque
1315
}
1416

1517
enum DeliveryKillSwitch {

0 commit comments

Comments
 (0)