fix(ipad): ship iPad P0 layout/globe fixes, edit-last-input, drop clipboard commands

Adapt typing/voice surfaces for iPad width and height, add the system globe
key and last-input editing flow, harden host-only Rime deployment, and remove
clipboard voice commands. Bump build to 61.
This commit is contained in:
Rocky
2026-08-10 13:50:50 +08:00
parent 53abad2050
commit 2bc8c1b87d
85 changed files with 5703 additions and 3997 deletions
@@ -1,32 +0,0 @@
// ClipboardPasteboardReader.swift
// OSGKeyboard · Keyboard Extension
//
// Pasteboard peeks for clipboard-command UI and long-press snapshot capture.
//
// Idle affordance must use metadata only (`hasStrings` / `changeCount`) so the
// systemalert never appears while the keyboard is merely open.
// Content reads (`string`) happen only on an explicit long-press.
import UIKit
import OSGKeyboardShared
enum ClipboardPasteboardReader {
/// Metadata-only does not trigger the paste permission prompt.
static func changeCount() -> Int {
UIPasteboard.general.changeCount
}
/// Metadata-only whether the pasteboard currently holds string items.
static func hasStrings() -> Bool {
UIPasteboard.general.hasStrings
}
/// Content sample for long-press snapshot. May present the system paste alert.
static func sample() -> (changeCount: Int, text: String?) {
let board = UIPasteboard.general
let changeCount = board.changeCount
// Prefer plain strings; avoid forcing non-text pasteboard items.
let text = board.hasStrings ? board.string : nil
return (changeCount, text)
}
}
@@ -12,6 +12,10 @@ final class KeyboardConfigSync {
private let state: KeyboardState
private let persistor: AppGroupPersistor
private let onFlowSessionChanged: () -> Void
/// Fired on every App Group config change the host posts one after a
/// successful Rime deployment, which is the keyboard's only signal that
/// typing resources just became available.
private let onConfigChanged: () -> Void
/// Grace period after a chip-side translation write during which the
/// 1 Hz App Group poll must not overwrite `translationTargetLocaleId`.
@@ -25,11 +29,13 @@ final class KeyboardConfigSync {
init(
state: KeyboardState,
persistor: AppGroupPersistor,
onFlowSessionChanged: @escaping () -> Void
onFlowSessionChanged: @escaping () -> Void,
onConfigChanged: @escaping () -> Void = {}
) {
self.state = state
self.persistor = persistor
self.onFlowSessionChanged = onFlowSessionChanged
self.onConfigChanged = onConfigChanged
}
func installDarwinObservers() {
@@ -49,7 +55,9 @@ final class KeyboardConfigSync {
configDarwinObserver = FlowSessionDarwinObserver(
notificationName: AppGroupConfigDarwin.notificationName
) { [weak self] in
self?.refreshConfigFromAppGroup()
guard let self else { return }
self.refreshConfigFromAppGroup()
self.onConfigChanged()
}
}
File diff suppressed because it is too large Load Diff
@@ -5,6 +5,7 @@
// without re-running LLM polish in the extension. Also tracks the last
// voice insertion so the undo button can roll it back safely.
import UIKit
import OSGKeyboardShared
@MainActor
@@ -13,12 +14,21 @@ final class KeyboardTextInserter {
private let insertText: (String) -> Void
private let deleteBackward: () -> Void
private let contextBeforeInput: () -> String?
private let fieldContextProvider: () -> FlowFieldContext?
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.
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.
private var redoText: String?
private var redoContextBefore: String?
private let extensionInstanceID = UUID()
private var lastEditUndo: PendingTextEditTransaction?
private var editHintTask: Task<Void, Never>?
/// Suppresses availability refresh while we walk `deleteBackward`
/// for undo, so intermediate contexts don't flicker the button.
private var isUndoing = false
@@ -28,12 +38,16 @@ final class KeyboardTextInserter {
insertText: @escaping (String) -> Void,
deleteBackward: @escaping () -> Void,
contextBeforeInput: @escaping () -> String?,
fieldContextProvider: @escaping () -> FlowFieldContext?,
selectedText: @escaping () -> String?,
scheduleAutoClearError: @escaping () -> Void
) {
self.state = state
self.insertText = insertText
self.deleteBackward = deleteBackward
self.contextBeforeInput = contextBeforeInput
self.fieldContextProvider = fieldContextProvider
self.selectedText = selectedText
self.scheduleAutoClearError = scheduleAutoClearError
}
@@ -66,7 +80,13 @@ final class KeyboardTextInserter {
)
let inserted = separator + trimmed
insertText(inserted)
recordLastInsertion(inserted)
recordLastInsertion(
inserted,
displayText: trimmed,
historyEntryID: delivery.historyEntryID,
historyEntryRevision: delivery.historyEntryRevision,
pendingHistoryMutationID: nil
)
state.lastTranscript = ""
state.level = 0
if let warning = delivery.polishWarning {
@@ -80,6 +100,9 @@ final class KeyboardTextInserter {
/// Roll back the last voice 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 {
clearLastInsertion()
@@ -92,35 +115,332 @@ final class KeyboardTextInserter {
for _ in 0..<text.count {
deleteBackward()
}
clearLastInsertion()
// Stash the exact inserted string (incl. separator) for redo before
// dropping the live undo record.
redoText = text
redoContextBefore = contextBeforeInput()
lastInsertedText = nil
state.undoAvailable = false
OSGLog.keyboardExt.info("voice undo length=\(text.count, privacy: .public)")
}
/// Re-evaluate whether the recorded insertion is still undoable.
/// Call from `textDidChange` / `selectionDidChange`.
func refreshUndoAvailability() {
guard !isUndoing else { return }
guard let text = lastInsertedText, !text.isEmpty else {
if state.undoAvailable { state.undoAvailable = false }
/// 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).
func redoLastInsertion() {
guard let text = redoText, !text.isEmpty,
contextBeforeInput() == redoContextBefore else {
redoText = nil
redoContextBefore = nil
state.redoAvailable = false
return
}
let available = contextBeforeInput()?.hasSuffix(text) == true
if !available {
// Caret moved or the user edited the insertion drop the record.
lastInsertedText = nil
guard let preceding = contextBeforeInput(), !preceding.hasSuffix(text) else {
redoText = nil
state.redoAvailable = false
return
}
if state.undoAvailable != available {
state.undoAvailable = available
insertText(text)
lastInsertedText = text
redoText = nil
redoContextBefore = nil
state.undoAvailable = true
OSGLog.keyboardExt.info("voice redo length=\(text.count, privacy: .public)")
}
/// Copy the host field's current selection to the pasteboard. Needs Full
/// Access for `selectedText` on some hosts; silently no-ops otherwise.
func copySelection() {
guard let text = selectedText(), !text.isEmpty else { return }
UIPasteboard.general.string = text
OSGLog.keyboardExt.info("copy length=\(text.count, privacy: .public)")
}
/// Copy the host field's current selection, then delete it.
func cutSelection() {
guard let text = selectedText(), !text.isEmpty else { return }
UIPasteboard.general.string = text
// `deleteBackward` removes the active selection in one operation.
deleteBackward()
OSGLog.keyboardExt.info("cut length=\(text.count, privacy: .public)")
}
/// Re-evaluate undo / redo / copy / cut availability. Call from
/// `textDidChange` / `selectionDidChange`.
func refreshEditingAvailability() {
// Undo: only re-check when not mid-undo (avoids flicker while we walk
// `deleteBackward`); the undo method manages availability itself.
if !isUndoing {
if let text = lastInsertedText, !text.isEmpty {
let available = contextBeforeInput()?.hasSuffix(text) == true
if !available {
// Caret moved or the user edited the insertion drop it.
lastInsertedText = nil
state.undoAvailable = false
} else if !state.undoAvailable {
state.undoAvailable = true
}
} else if state.undoAvailable {
state.undoAvailable = false
}
}
// Redo: a stashed insertion that hasn't been overwritten by an edit.
if redoText != nil, contextBeforeInput() != redoContextBefore {
redoText = nil
redoContextBefore = nil
}
let redo = redoText != nil && !(redoText?.isEmpty ?? true)
if state.redoAvailable != redo {
state.redoAvailable = redo
}
// Copy / cut: a non-empty selection exists in the host field.
let hasSelection = (selectedText()?.isEmpty == false)
if state.copyAvailable != hasSelection {
state.copyAvailable = hasSelection
}
if state.cutAvailable != hasSelection {
state.cutAvailable = hasSelection
}
}
private func recordLastInsertion(_ text: String) {
func editableReference() -> EditableInputReference? {
guard var reference = EditableInputReferenceStore.load() else {
return nil
}
if let mutationID = reference.pendingHistoryMutationID,
let receipt = HistoryMutationReceiptStore.receipt(for: mutationID) {
reference = EditableInputReference(
targetID: reference.targetID,
historyEntryID: receipt.entryID,
historyEntryRevision: receipt.revision,
displayText: reference.displayText,
insertedText: reference.insertedText,
postInsertionFingerprint: reference.postInsertionFingerprint,
extensionInstanceID: reference.extensionInstanceID,
observedDocumentRevision: reference.observedDocumentRevision,
createdAt: reference.createdAt
)
EditableInputReferenceStore.save(reference)
}
guard reference.isWithinLengthBudget,
let preceding = contextBeforeInput(),
preceding.hasSuffix(reference.insertedText) else {
return nil
}
guard reference.postInsertionFingerprint == nil
|| reference.postInsertionFingerprint
== fieldContextProvider()?.deliveryFingerprint else {
return nil
}
if reference.extensionInstanceID == extensionInstanceID,
lastInsertedText == reference.insertedText {
return reference
}
return reference.isFullyVerified(
contextBeforeInput: preceding,
fieldFingerprint: fieldContextProvider()?.deliveryFingerprint
) ? reference : nil
}
@discardableResult
func applyEdit(_ review: EditReview, append: Bool) -> Bool {
let source = review.source.reference
let result = review.resultText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !result.isEmpty else { return false }
let mode: PendingTextEditTransaction.DeliveryMode = append ? .append : .replace
if append {
guard let context = fieldContextProvider(),
!context.isSecureEntry,
context.isContextAvailable else {
return false
}
}
if !append {
guard editableReference()?.targetID == source.targetID else { return false }
}
let historyEntryID = append
? UUID()
: (source.historyEntryID ?? UUID())
let historyAction: HistoryMutation.Action = append || source.historyEntryID == nil
? .append
: .update
let mutation = HistoryMutation(
action: historyAction,
entryID: historyEntryID,
expectedRevision: historyAction == .update
? source.historyEntryRevision
: nil,
text: result
)
var transaction = PendingTextEditTransaction(
deliveryMode: mode,
beforeText: source.insertedText,
afterText: result,
expectedFieldFingerprint: fieldContextProvider()?.deliveryFingerprint,
historyMutation: mutation
)
PendingTextEditTransactionStore.save(transaction)
if !append {
for _ in source.insertedText {
deleteBackward()
}
}
let separator = DictationTextComposer.insertionSeparator(
previousContext: contextBeforeInput(),
insertion: result
)
let inserted = separator + result
transaction.appliedInsertedText = inserted
PendingTextEditTransactionStore.save(transaction)
insertText(inserted)
let verificationSuffix = String(inserted.suffix(80))
guard contextBeforeInput()?.hasSuffix(verificationSuffix) == true else {
// Never blindly delete after a partial/opaque host insertion. The
// durable transaction lets a later presentation reconcile safely.
return false
}
transaction.phase = .fieldApplied
PendingTextEditTransactionStore.save(transaction)
HistoryMutationOutbox.enqueue(mutation)
transaction.phase = .committed
PendingTextEditTransactionStore.save(transaction)
lastEditUndo = transaction
recordLastInsertion(
inserted,
displayText: result,
historyEntryID: historyEntryID,
historyEntryRevision: historyAction == .update
? (source.historyEntryRevision ?? 0) + 1
: 0,
pendingHistoryMutationID: mutation.id
)
PendingTextEditTransactionStore.clear()
return true
}
@discardableResult
func recoverPendingEditTransactionIfNeeded() -> Bool {
guard let transaction = PendingTextEditTransactionStore.load(),
let preceding = contextBeforeInput() else {
return false
}
let currentFingerprint = fieldContextProvider()?.deliveryFingerprint
let appliedText = transaction.appliedInsertedText ?? transaction.afterText
if transaction.phase != .prepared,
preceding.hasSuffix(appliedText) {
HistoryMutationOutbox.enqueue(transaction.historyMutation)
PendingTextEditTransactionStore.clear()
return true
}
if transaction.phase == .prepared,
currentFingerprint != transaction.expectedFieldFingerprint,
preceding.hasSuffix(appliedText) {
HistoryMutationOutbox.enqueue(transaction.historyMutation)
PendingTextEditTransactionStore.clear()
return true
}
if transaction.deliveryMode == .replace,
preceding.hasSuffix(transaction.beforeText) {
PendingTextEditTransactionStore.clear()
return false
}
return false
}
private func undoLastEditIfPossible() -> Bool {
guard let transaction = lastEditUndo,
let preceding = contextBeforeInput() else {
return false
}
let insertedAfter = lastInsertedText ?? transaction.afterText
guard preceding.hasSuffix(insertedAfter) else {
lastEditUndo = nil
return false
}
for _ in insertedAfter {
deleteBackward()
}
switch transaction.deliveryMode {
case .replace:
insertText(transaction.beforeText)
let restore = HistoryMutation(
action: transaction.historyMutation.action == .append ? .delete : .restore,
entryID: transaction.historyMutation.entryID,
expectedRevision: transaction.historyMutation.expectedRevision.map { $0 + 1 },
text: transaction.beforeText
.trimmingCharacters(in: .whitespacesAndNewlines)
)
HistoryMutationOutbox.enqueue(restore)
recordLastInsertion(
transaction.beforeText,
displayText: transaction.beforeText
.trimmingCharacters(in: .whitespacesAndNewlines),
historyEntryID: transaction.historyMutation.action == .append
? nil
: transaction.historyMutation.entryID,
historyEntryRevision: transaction.historyMutation.expectedRevision.map { $0 + 2 },
pendingHistoryMutationID: restore.id
)
case .append:
HistoryMutationOutbox.enqueue(
HistoryMutation(
action: .delete,
entryID: transaction.historyMutation.entryID
)
)
clearLastInsertion()
}
lastEditUndo = nil
return true
}
private func recordLastInsertion(
_ text: String,
displayText: String,
historyEntryID: UUID?,
historyEntryRevision: Int64?,
pendingHistoryMutationID: UUID?
) {
lastInsertedText = text
redoText = nil
redoContextBefore = nil
state.undoAvailable = true
EditableInputReferenceStore.save(
EditableInputReference(
historyEntryID: historyEntryID,
historyEntryRevision: historyEntryRevision,
pendingHistoryMutationID: pendingHistoryMutationID,
displayText: displayText,
insertedText: text,
postInsertionFingerprint: fieldContextProvider()?.deliveryFingerprint,
extensionInstanceID: extensionInstanceID
)
)
editHintTask?.cancel()
let hint = ExtL10n.string("keyboard.edit.hint.available")
state.editHint = hint
state.editHintIsPositive = true
editHintTask = Task { @MainActor [weak self] in
try? await Task.sleep(nanoseconds: 10_000_000_000)
guard !Task.isCancelled,
self?.state.editHint == hint,
self?.state.editHintIsPositive == true else {
return
}
self?.state.editHint = nil
self?.state.editHintIsPositive = false
}
}
private func clearLastInsertion() {
lastInsertedText = nil
state.undoAvailable = false
EditableInputReferenceStore.clear()
}
}
@@ -0,0 +1,275 @@
// LastInputEditCoordinator.swift
// OSGKeyboard · Keyboard Extension
//
// Product workflow for explicit editing. Flow transport remains owned by
// KeyboardFlowCoordinator; this coordinator owns only edit state and delivery.
import Foundation
import OSGKeyboardShared
@MainActor
final class LastInputEditCoordinator {
private let state: KeyboardState
private let textInserter: KeyboardTextInserter
private let beginFlow: (EditableInputReference) -> FlowUtteranceStartDisposition
private let stopFlow: () -> Void
private let abortFlow: () -> Void
private let acknowledge: (FlowAck.DeliveryOutcome) -> Void
private var hintTask: Task<Void, Never>?
private var activeUtteranceID: UUID?
private var reviewedUtteranceID: UUID?
private var reviewedRevision: Int64?
init(
state: KeyboardState,
textInserter: KeyboardTextInserter,
beginFlow: @escaping (EditableInputReference) -> FlowUtteranceStartDisposition,
stopFlow: @escaping () -> Void,
abortFlow: @escaping () -> Void,
acknowledge: @escaping (FlowAck.DeliveryOutcome) -> Void
) {
self.state = state
self.textInserter = textInserter
self.beginFlow = beginFlow
self.stopFlow = stopFlow
self.abortFlow = abortFlow
self.acknowledge = acknowledge
}
func begin() {
switch state.editSession {
case .inactive, .failed:
break
default:
return
}
guard !state.micDisabled else {
showHint(ExtL10n.string("keyboard.edit.error.llmUnavailable"))
return
}
switch state.micVoiceAvailability {
case .unavailable(.missingAPIKey):
showHint(ExtL10n.string("keyboard.edit.error.llmUnavailable"))
return
case .unavailable(.noFullAccess):
showHint(ExtL10n.string("keyboard.error.fullAccessRequired"))
return
case .unavailable(.appGroupUnavailable):
showHint(ExtL10n.string("keyboard.error.appGroupCommunication"))
return
case .unavailable(.onboardingIncomplete):
showHint(ExtL10n.string("keyboard.hint.finishSetupInApp"))
return
case .unavailable(.hostNotReady),
.unavailable(.preparingSession),
.ready,
.recording,
.processing:
break
}
guard let reference = textInserter.editableReference() else {
showHint(ExtL10n.string("keyboard.edit.error.noTarget"))
return
}
start(reference)
}
private func start(_ reference: EditableInputReference) {
let source = EditSessionSource(reference: reference)
state.lastTranscript = ""
let disposition = beginFlow(reference)
guard let utteranceID = disposition.utteranceID else {
let message = startFailureMessage(for: disposition)
state.editSession = .failed(source, message: message)
state.phase = .idle
state.lastTranscript = ""
EditUsageMetricsStore.record(.failed)
return
}
activeUtteranceID = utteranceID
reviewedUtteranceID = nil
reviewedRevision = nil
state.editCanReplaceOriginal = true
state.editSession = .preparing(source)
state.phase = .requestingPermissions
EditUsageMetricsStore.record(.entered)
KeyboardHapticFeedback.play(
role: .action,
intensity: state.keyboardHapticIntensity
)
OSGLog.keyboardExt.info(
"edit.start issued utterance=\(self.activeUtteranceID?.uuidString.prefix(8) ?? "nil", privacy: .public)"
)
}
func hostRecordingConfirmed() {
guard case .preparing(let source) = state.editSession else { return }
state.editSession = .listening(source)
state.phase = .recording
KeyboardHapticFeedback.play(
role: .action,
intensity: state.keyboardHapticIntensity
)
OSGLog.keyboardExt.info(
"edit.hostRecording.confirmed utterance=\(self.activeUtteranceID?.uuidString.prefix(8) ?? "nil", privacy: .public)"
)
}
func stopListening() {
guard case .listening(let source) = state.editSession else { return }
state.editSession = .processing(source)
state.phase = .processing
stopFlow()
}
func receive(result: FlowResult) {
guard result.resolvedUtteranceMode == .editLastInput,
result.utteranceId == activeUtteranceID,
case .processing(let source) = state.editSession,
reviewedUtteranceID != result.utteranceId
|| reviewedRevision != result.revision,
let output = result.text else {
return
}
switch EditOutputValidator.validate(
sourceText: source.reference.displayText,
output: output
) {
case .success(let validated):
reviewedUtteranceID = result.utteranceId
reviewedRevision = result.revision
let review = EditReview(
source: source,
resultText: validated,
utteranceID: result.utteranceId
)
state.editCanReplaceOriginal =
textInserter.editableReference()?.targetID == source.reference.targetID
state.editSession = .review(review)
state.phase = .processing
KeyboardHapticFeedback.play(
role: .action,
intensity: state.keyboardHapticIntensity
)
case .failure(.unchanged):
fail(ExtL10n.string("keyboard.edit.error.unchanged"))
acknowledge(.rejected)
case .failure:
fail(ExtL10n.string("keyboard.edit.error.processing"))
acknowledge(.rejected)
}
}
func fail(_ message: String) {
guard let source = state.editSession.source else {
showHint(message)
return
}
state.editSession = .failed(source, message: message)
state.phase = .idle
state.lastTranscript = ""
EditUsageMetricsStore.record(.failed)
activeUtteranceID = nil
reviewedUtteranceID = nil
reviewedRevision = nil
}
func refreshContext() {
guard let source = state.editSession.source else { return }
state.editCanReplaceOriginal =
textInserter.editableReference()?.targetID == source.reference.targetID
}
func confirm() {
guard case .review(let review) = state.editSession else { return }
let shouldAppend = !state.editCanReplaceOriginal
state.editSession = shouldAppend ? .appending(review) : .applying(review)
let applied = textInserter.applyEdit(review, append: shouldAppend)
guard applied else {
state.editSession = .review(review)
state.editCanReplaceOriginal = false
return
}
acknowledge(shouldAppend ? .appended : .replaced)
EditUsageMetricsStore.record(shouldAppend ? .appended : .replaced)
state.editSession = .inactive
state.editCanReplaceOriginal = false
state.phase = .idle
state.lastTranscript = ""
activeUtteranceID = nil
reviewedUtteranceID = nil
reviewedRevision = nil
KeyboardHapticFeedback.play(
role: .action,
intensity: state.keyboardHapticIntensity
)
}
func close() {
if state.editSession.review != nil {
acknowledge(.rejected)
} else {
abortFlow()
}
state.editSession = .inactive
state.editCanReplaceOriginal = false
state.phase = .idle
state.lastTranscript = ""
EditUsageMetricsStore.record(.cancelled)
activeUtteranceID = nil
reviewedUtteranceID = nil
reviewedRevision = nil
}
func showAvailabilityHintAfterDictation() {
guard textInserter.editableReference() != nil else { return }
showHint(
ExtL10n.string("keyboard.edit.hint.available"),
isPositive: true,
durationNanoseconds: 10_000_000_000
)
}
private func showHint(
_ message: String,
isPositive: Bool = false,
durationNanoseconds: UInt64 = 2_500_000_000
) {
hintTask?.cancel()
state.editHint = message
state.editHintIsPositive = isPositive
hintTask = Task { @MainActor [weak self] in
try? await Task.sleep(nanoseconds: durationNanoseconds)
guard !Task.isCancelled,
self?.state.editHint == message,
self?.state.editHintIsPositive == isPositive else {
return
}
self?.state.editHint = nil
self?.state.editHintIsPositive = false
}
}
private func startFailureMessage(
for disposition: FlowUtteranceStartDisposition
) -> String {
guard case .rejected(let reason) = disposition else {
return ExtL10n.string("keyboard.edit.error.startTimeout")
}
switch reason {
case .missingAPIKey:
return ExtL10n.string("keyboard.edit.error.llmUnavailable")
case .noFullAccess:
return ExtL10n.string("keyboard.error.fullAccessRequired")
case .appGroupUnavailable:
return ExtL10n.string("keyboard.error.appGroupCommunication")
case .onboardingIncomplete:
return ExtL10n.string("keyboard.hint.finishSetupInApp")
case .pipelineBusy:
return ExtL10n.string("keyboard.edit.error.processing")
case .hostUnavailable:
return ExtL10n.string("keyboard.edit.error.startTimeout")
}
}
}