feat(keyboard): add clipboard history and undo pastes
Ship optional keyboard clipboard history with settings, suggestion strip, and paste-permission guidance; let undo roll back clipboard inserts; bump build to 64.
This commit is contained in:
@@ -41,6 +41,8 @@ public struct AppGroupPersistor {
|
||||
state.translationTargetLocaleId = store.translationTargetLocaleId
|
||||
state.handednessPreference = store.handednessPreference
|
||||
state.cursorDragNavigationEnabled = store.cursorDragNavigationEnabled
|
||||
state.clipboardHistoryEnabled = store.clipboardHistoryEnabled
|
||||
state.clipboardCandidateBarEnabled = store.clipboardCandidateBarEnabled
|
||||
state.keyboardHapticIntensity = store.keyboardHapticIntensity
|
||||
applyAPIKeyAvailability(store: store, into: state)
|
||||
|
||||
@@ -91,6 +93,8 @@ public struct AppGroupPersistor {
|
||||
}
|
||||
state.handednessPreference = store.handednessPreference
|
||||
state.cursorDragNavigationEnabled = store.cursorDragNavigationEnabled
|
||||
state.clipboardHistoryEnabled = store.clipboardHistoryEnabled
|
||||
state.clipboardCandidateBarEnabled = store.clipboardCandidateBarEnabled
|
||||
state.keyboardHapticIntensity = store.keyboardHapticIntensity
|
||||
applyAPIKeyAvailability(store: store, into: state)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
// ClipboardCaptureCoordinator.swift
|
||||
// OSGKeyboard · Keyboard Extension
|
||||
//
|
||||
// Samples the general pasteboard on keyboard appear and while visible
|
||||
// (changeCount-driven). Writes accepted text into ClipboardHistoryStore.
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import OSGKeyboardShared
|
||||
|
||||
@MainActor
|
||||
final class ClipboardCaptureCoordinator {
|
||||
private let state: KeyboardState
|
||||
private let history: ClipboardHistoryStore
|
||||
private var pollTimer: Timer?
|
||||
private var isSecureProvider: () -> Bool = { false }
|
||||
private var hasFullAccessProvider: () -> Bool = { false }
|
||||
|
||||
init(
|
||||
state: KeyboardState,
|
||||
history: ClipboardHistoryStore = .shared
|
||||
) {
|
||||
self.state = state
|
||||
self.history = history
|
||||
}
|
||||
|
||||
func configure(
|
||||
isSecure: @escaping () -> Bool,
|
||||
hasFullAccess: @escaping () -> Bool
|
||||
) {
|
||||
isSecureProvider = isSecure
|
||||
hasFullAccessProvider = hasFullAccess
|
||||
}
|
||||
|
||||
func keyboardDidAppear() {
|
||||
history.reload()
|
||||
refreshSuggestionFromStore()
|
||||
captureIfNeeded(force: true)
|
||||
startPolling()
|
||||
}
|
||||
|
||||
func keyboardWillDisappear() {
|
||||
stopPolling()
|
||||
}
|
||||
|
||||
func refreshFlagsFromStore() {
|
||||
// Called from App Group poll — suggestion visibility may change.
|
||||
refreshSuggestionFromStore()
|
||||
}
|
||||
|
||||
func openPanelFromTopButton() {
|
||||
if state.clipboardHistoryEnabled {
|
||||
history.reload()
|
||||
state.clipboardOverlay = .historyPanel
|
||||
} else {
|
||||
state.clipboardOverlay = .enableGuide
|
||||
}
|
||||
}
|
||||
|
||||
func dismissOverlay() {
|
||||
state.clipboardOverlay = .none
|
||||
}
|
||||
|
||||
func noteUserDidInputText() {
|
||||
clearSuggestion(persistDismiss: false)
|
||||
}
|
||||
|
||||
func dismissSuggestion() {
|
||||
history.dismissSuggestion(forChangeCount: state.clipboardSuggestionChangeCount)
|
||||
clearSuggestion(persistDismiss: true)
|
||||
}
|
||||
|
||||
func insertText(_ text: String, via insert: (String) -> Void) {
|
||||
insert(text)
|
||||
// Tapping a suggestion (or history row that shares this path) must not
|
||||
// resurface the same clipboard changeCount until the pasteboard changes.
|
||||
dismissSuggestion()
|
||||
dismissOverlay()
|
||||
}
|
||||
|
||||
func clearHistory() {
|
||||
history.clearAll()
|
||||
clearSuggestion(persistDismiss: false)
|
||||
}
|
||||
|
||||
func deleteEntry(id: UUID) {
|
||||
history.remove(id: id)
|
||||
refreshSuggestionFromStore()
|
||||
}
|
||||
|
||||
// MARK: - Capture
|
||||
|
||||
private func startPolling() {
|
||||
stopPolling()
|
||||
let timer = Timer(timeInterval: 0.8, repeats: true) { [weak self] _ in
|
||||
Task { @MainActor in
|
||||
self?.captureIfNeeded(force: false)
|
||||
}
|
||||
}
|
||||
RunLoop.main.add(timer, forMode: .common)
|
||||
pollTimer = timer
|
||||
}
|
||||
|
||||
private func stopPolling() {
|
||||
pollTimer?.invalidate()
|
||||
pollTimer = nil
|
||||
}
|
||||
|
||||
private func captureIfNeeded(force: Bool) {
|
||||
guard state.clipboardHistoryEnabled else {
|
||||
clearSuggestion(persistDismiss: false)
|
||||
return
|
||||
}
|
||||
guard hasFullAccessProvider() else { return }
|
||||
guard !isSecureProvider() else { return }
|
||||
|
||||
let pasteboard = UIPasteboard.general
|
||||
let changeCount = pasteboard.changeCount
|
||||
if !force, changeCount == history.lastObservedChangeCount {
|
||||
refreshSuggestionFromStore()
|
||||
return
|
||||
}
|
||||
|
||||
// Prefer hasStrings peek before reading body (reduces empty reads).
|
||||
guard pasteboard.hasStrings else {
|
||||
history.lastObservedChangeCount = changeCount
|
||||
refreshSuggestionFromStore()
|
||||
return
|
||||
}
|
||||
|
||||
let raw = pasteboard.string
|
||||
if let entry = history.ingest(rawText: raw, changeCount: changeCount) {
|
||||
updateSuggestion(with: entry, changeCount: changeCount)
|
||||
} else {
|
||||
history.lastObservedChangeCount = changeCount
|
||||
refreshSuggestionFromStore()
|
||||
}
|
||||
}
|
||||
|
||||
private func refreshSuggestionFromStore() {
|
||||
guard state.clipboardHistoryEnabled,
|
||||
state.clipboardCandidateBarEnabled,
|
||||
!isSecureProvider(),
|
||||
let newest = history.newestEntry
|
||||
else {
|
||||
clearSuggestion(persistDismiss: false)
|
||||
return
|
||||
}
|
||||
let changeCount = newest.changeCount ?? history.lastObservedChangeCount
|
||||
guard history.shouldShowSuggestion(
|
||||
forChangeCount: changeCount,
|
||||
candidateBarEnabled: state.clipboardCandidateBarEnabled,
|
||||
historyEnabled: state.clipboardHistoryEnabled
|
||||
) else {
|
||||
clearSuggestion(persistDismiss: false)
|
||||
return
|
||||
}
|
||||
// Don't resurrect a strip the user already dismissed this session
|
||||
// unless changeCount advanced (handled in ingest).
|
||||
if state.clipboardSuggestionText == nil,
|
||||
let dismissed = history.suggestionDismissedChangeCount,
|
||||
dismissed == changeCount {
|
||||
return
|
||||
}
|
||||
state.clipboardSuggestionText = newest.text
|
||||
state.clipboardSuggestionChangeCount = changeCount
|
||||
}
|
||||
|
||||
private func updateSuggestion(with entry: ClipboardHistoryEntry, changeCount: Int) {
|
||||
guard state.clipboardCandidateBarEnabled else {
|
||||
clearSuggestion(persistDismiss: false)
|
||||
return
|
||||
}
|
||||
// Already used/dismissed this pasteboard generation — keep it hidden.
|
||||
if history.suggestionDismissedChangeCount == changeCount {
|
||||
clearSuggestion(persistDismiss: false)
|
||||
return
|
||||
}
|
||||
state.clipboardSuggestionText = entry.text
|
||||
state.clipboardSuggestionChangeCount = changeCount
|
||||
}
|
||||
|
||||
private func clearSuggestion(persistDismiss: Bool) {
|
||||
if persistDismiss {
|
||||
// already written in dismissSuggestion
|
||||
}
|
||||
state.clipboardSuggestionText = nil
|
||||
state.clipboardSuggestionChangeCount = nil
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,10 @@ import OSGKeyboardShared
|
||||
|
||||
@MainActor
|
||||
final class KeyboardTextInserter {
|
||||
/// Hosts truncate `documentContextBeforeInput` (often to the current
|
||||
/// paragraph), so a long insertion can only ever be matched by its tail.
|
||||
private static let caretVerificationLimit = 80
|
||||
|
||||
private let state: KeyboardState
|
||||
private let insertText: (String) -> Void
|
||||
private let deleteBackward: () -> Void
|
||||
@@ -18,9 +22,10 @@ final class KeyboardTextInserter {
|
||||
private let selectedText: () -> String?
|
||||
private let scheduleAutoClearError: () -> Void
|
||||
|
||||
/// Exact string last inserted by voice (including any word-boundary
|
||||
/// separator). Cleared after a successful undo or when the caret no
|
||||
/// longer sits after that text.
|
||||
/// Exact string last inserted through this inserter — dictation, AI answer,
|
||||
/// edit result or clipboard paste (including any word-boundary separator).
|
||||
/// Cleared after a successful undo or when the caret no longer sits after
|
||||
/// that text.
|
||||
private var lastInsertedText: String?
|
||||
/// Text captured when the last insertion was undone, so redo can
|
||||
/// re-apply it. Cleared by any new insertion or external edit.
|
||||
@@ -80,6 +85,7 @@ final class KeyboardTextInserter {
|
||||
)
|
||||
let inserted = separator + trimmed
|
||||
insertText(inserted)
|
||||
state.noteUserDidInputText()
|
||||
recordLastInsertion(
|
||||
inserted,
|
||||
displayText: trimmed,
|
||||
@@ -111,6 +117,7 @@ final class KeyboardTextInserter {
|
||||
)
|
||||
let inserted = separator + trimmed
|
||||
insertText(inserted)
|
||||
state.noteUserDidInputText()
|
||||
|
||||
let mutation = HistoryMutation(
|
||||
action: .append,
|
||||
@@ -134,13 +141,28 @@ final class KeyboardTextInserter {
|
||||
return true
|
||||
}
|
||||
|
||||
/// Roll back the last voice insertion when it is still at the caret.
|
||||
/// Insert clipboard text verbatim and make it undoable. Pasted text is not
|
||||
/// a dictation result: it never becomes an editable "last input" reference
|
||||
/// and never reaches the history outbox, so it only takes the undo record.
|
||||
func insertPasteboardText(_ text: String) {
|
||||
guard !text.isEmpty else { return }
|
||||
// Verbatim on purpose — paste must reproduce exactly what was copied,
|
||||
// unlike dictation which needs word-boundary hygiene.
|
||||
insertText(text)
|
||||
recordUndoableInsertion(text)
|
||||
// The paste pushed any previous input away from the caret, so the
|
||||
// "editable last input" hint no longer applies.
|
||||
clearEditHintIfPositive()
|
||||
OSGLog.keyboardExt.info("clipboard insert length=\(text.count, privacy: .public)")
|
||||
}
|
||||
|
||||
/// Roll back the last insertion when it is still at the caret.
|
||||
func undoLastInsertion() {
|
||||
if undoLastEditIfPossible() {
|
||||
return
|
||||
}
|
||||
guard let text = lastInsertedText, !text.isEmpty else { return }
|
||||
guard let preceding = contextBeforeInput(), preceding.hasSuffix(text) else {
|
||||
guard caretSitsAfter(text) else {
|
||||
clearLastInsertion()
|
||||
return
|
||||
}
|
||||
@@ -148,6 +170,9 @@ final class KeyboardTextInserter {
|
||||
isUndoing = true
|
||||
defer { isUndoing = false }
|
||||
|
||||
// `UITextDocumentProxy` has no ranged delete, so the whole insertion is
|
||||
// walked back one grapheme at a time. Staying synchronous keeps it in a
|
||||
// single run-loop turn, which the host coalesces into one visual update.
|
||||
for _ in 0..<text.count {
|
||||
deleteBackward()
|
||||
}
|
||||
@@ -157,11 +182,11 @@ final class KeyboardTextInserter {
|
||||
redoContextBefore = contextBeforeInput()
|
||||
lastInsertedText = nil
|
||||
state.undoAvailable = false
|
||||
OSGLog.keyboardExt.info("voice undo length=\(text.count, privacy: .public)")
|
||||
OSGLog.keyboardExt.info("undo length=\(text.count, privacy: .public)")
|
||||
}
|
||||
|
||||
/// Re-apply the last undone voice insertion when it is still absent from
|
||||
/// the caret (i.e. the undo was not overwritten by an external edit).
|
||||
/// Re-apply the last undone insertion when it is still absent from the
|
||||
/// caret (i.e. the undo was not overwritten by an external edit).
|
||||
func redoLastInsertion() {
|
||||
guard let text = redoText, !text.isEmpty,
|
||||
contextBeforeInput() == redoContextBefore else {
|
||||
@@ -170,17 +195,14 @@ final class KeyboardTextInserter {
|
||||
state.redoAvailable = false
|
||||
return
|
||||
}
|
||||
guard let preceding = contextBeforeInput(), !preceding.hasSuffix(text) else {
|
||||
guard !caretSitsAfter(text) else {
|
||||
redoText = nil
|
||||
state.redoAvailable = false
|
||||
return
|
||||
}
|
||||
insertText(text)
|
||||
lastInsertedText = text
|
||||
redoText = nil
|
||||
redoContextBefore = nil
|
||||
state.undoAvailable = true
|
||||
OSGLog.keyboardExt.info("voice redo length=\(text.count, privacy: .public)")
|
||||
recordUndoableInsertion(text)
|
||||
OSGLog.keyboardExt.info("redo length=\(text.count, privacy: .public)")
|
||||
}
|
||||
|
||||
/// Copy the host field's current selection to the pasteboard. Needs Full
|
||||
@@ -207,7 +229,7 @@ final class KeyboardTextInserter {
|
||||
// `deleteBackward`); the undo method manages availability itself.
|
||||
if !isUndoing {
|
||||
if let text = lastInsertedText, !text.isEmpty {
|
||||
let available = contextBeforeInput()?.hasSuffix(text) == true
|
||||
let available = caretSitsAfter(text)
|
||||
if !available {
|
||||
// Caret moved or the user edited the insertion — drop it.
|
||||
lastInsertedText = nil
|
||||
@@ -345,7 +367,6 @@ final class KeyboardTextInserter {
|
||||
HistoryMutationOutbox.enqueue(mutation)
|
||||
transaction.phase = .committed
|
||||
PendingTextEditTransactionStore.save(transaction)
|
||||
lastEditUndo = transaction
|
||||
recordLastInsertion(
|
||||
inserted,
|
||||
displayText: result,
|
||||
@@ -355,6 +376,9 @@ final class KeyboardTextInserter {
|
||||
: 0,
|
||||
pendingHistoryMutationID: mutation.id
|
||||
)
|
||||
// Set after recording: the shared bookkeeping drops any stale
|
||||
// transaction, and this one must survive as the undo target.
|
||||
lastEditUndo = transaction
|
||||
PendingTextEditTransactionStore.clear()
|
||||
return true
|
||||
}
|
||||
@@ -389,12 +413,11 @@ final class KeyboardTextInserter {
|
||||
}
|
||||
|
||||
private func undoLastEditIfPossible() -> Bool {
|
||||
guard let transaction = lastEditUndo,
|
||||
let preceding = contextBeforeInput() else {
|
||||
guard let transaction = lastEditUndo else {
|
||||
return false
|
||||
}
|
||||
let insertedAfter = lastInsertedText ?? transaction.afterText
|
||||
guard preceding.hasSuffix(insertedAfter) else {
|
||||
guard caretSitsAfter(insertedAfter) else {
|
||||
lastEditUndo = nil
|
||||
return false
|
||||
}
|
||||
@@ -436,6 +459,38 @@ final class KeyboardTextInserter {
|
||||
return true
|
||||
}
|
||||
|
||||
/// Is the caret still sitting right after `text`?
|
||||
///
|
||||
/// The comparison is limited to the tail of the insertion's last line: a
|
||||
/// truncated host context can never contain the earlier part, and text
|
||||
/// before a line break is usually stripped from `documentContextBeforeInput`.
|
||||
private func caretSitsAfter(_ text: String) -> Bool {
|
||||
guard let preceding = contextBeforeInput() else { return false }
|
||||
let lastLine: String
|
||||
if let breakRange = text.rangeOfCharacter(from: .newlines, options: .backwards) {
|
||||
lastLine = String(text[breakRange.upperBound...])
|
||||
} else {
|
||||
lastLine = text
|
||||
}
|
||||
let expected = String(lastLine.suffix(Self.caretVerificationLimit))
|
||||
// The insertion ended on a line break, so nothing measurable is left
|
||||
// before the caret — an empty context is the expected state.
|
||||
guard !expected.isEmpty else { return preceding.isEmpty }
|
||||
return preceding.hasSuffix(expected)
|
||||
}
|
||||
|
||||
/// Undo bookkeeping shared by every insertion path. Callers that also own
|
||||
/// history / editable-reference state layer `recordLastInsertion` on top.
|
||||
private func recordUndoableInsertion(_ text: String) {
|
||||
lastInsertedText = text
|
||||
redoText = nil
|
||||
redoContextBefore = nil
|
||||
// A newer insertion supersedes any edit transaction: undo must roll back
|
||||
// this text, not re-apply the original text of an older edit.
|
||||
lastEditUndo = nil
|
||||
state.undoAvailable = true
|
||||
}
|
||||
|
||||
private func recordLastInsertion(
|
||||
_ text: String,
|
||||
displayText: String,
|
||||
@@ -443,10 +498,7 @@ final class KeyboardTextInserter {
|
||||
historyEntryRevision: Int64?,
|
||||
pendingHistoryMutationID: UUID?
|
||||
) {
|
||||
lastInsertedText = text
|
||||
redoText = nil
|
||||
redoContextBefore = nil
|
||||
state.undoAvailable = true
|
||||
recordUndoableInsertion(text)
|
||||
EditableInputReferenceStore.save(
|
||||
EditableInputReference(
|
||||
historyEntryID: historyEntryID,
|
||||
@@ -474,6 +526,14 @@ final class KeyboardTextInserter {
|
||||
}
|
||||
}
|
||||
|
||||
private func clearEditHintIfPositive() {
|
||||
guard state.editHintIsPositive else { return }
|
||||
editHintTask?.cancel()
|
||||
editHintTask = nil
|
||||
state.editHint = nil
|
||||
state.editHintIsPositive = false
|
||||
}
|
||||
|
||||
private func clearLastInsertion() {
|
||||
lastInsertedText = nil
|
||||
state.undoAvailable = false
|
||||
|
||||
Reference in New Issue
Block a user