c2f07bd8d2
Introduce a standalone macOS menu-bar app (OSGKeyboardMac) that reuses the platform-agnostic OSGKeyboardShared core: record -> cloud/local ASR -> polish -> insert. Local mode uses Qwen3-ASR via mlx-swift-asr (macOS 15+, Apple Silicon); iOS targets stay zero-SPM. Harden iCloud sync for multi-device correctness: - Per-field settings merge (appSettings.v2) so concurrent edits no longer clobber each other's unrelated fields. - Per-device usage statistics (G-Counter) that sum instead of max(). - Tombstoned dictionary/history merge so deletes propagate and entries can't resurrect. - API keys replicate via iCloud Keychain, never iCloud KVS JSON; pulling a legacy blob without key fields no longer wipes local Keychain entries. - Add a low-risk "Sync Now" action in Settings. Fix Flow keyboard mic state: stay orange until the host publishes a real ready contract, share a single MicVoiceAvailability gate, and self-heal stale cross-process heartbeat jitter instead of getting stuck. Extract shared storage (SpeechHistoryStore/UsageStatisticsStore, ConfigurationStore) into OSGKeyboardShared and add tests for the new sync/merge logic.
62 lines
2.0 KiB
Swift
62 lines
2.0 KiB
Swift
// MacTextInsertionService.swift
|
|
// OSGKeyboard · Mac
|
|
//
|
|
// Inserts transcribed text into the frontmost app: clipboard first, then
|
|
// a synthetic ⌘V (SayIt / Typeless-style). Requires Accessibility trust.
|
|
|
|
import AppKit
|
|
@preconcurrency import ApplicationServices
|
|
import Carbon
|
|
import Foundation
|
|
|
|
enum MacTextInsertionService {
|
|
enum InsertionError: Error, LocalizedError {
|
|
case accessibilityNotGranted
|
|
|
|
var errorDescription: String? {
|
|
MacL10n.string("mac.error.accessibilityRequired")
|
|
}
|
|
}
|
|
|
|
static var isAccessibilityTrusted: Bool {
|
|
AXIsProcessTrusted()
|
|
}
|
|
|
|
@discardableResult
|
|
static func requestAccessibilityIfNeeded() -> Bool {
|
|
if AXIsProcessTrusted() { return true }
|
|
let promptKey = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String
|
|
let options = [promptKey: true] as CFDictionary
|
|
return AXIsProcessTrustedWithOptions(options)
|
|
}
|
|
|
|
/// Copy to pasteboard and optionally simulate ⌘V in the front app.
|
|
static func insert(
|
|
_ text: String,
|
|
autoPaste: Bool
|
|
) throws -> Bool {
|
|
guard !text.isEmpty else { return false }
|
|
let pasteboard = NSPasteboard.general
|
|
pasteboard.clearContents()
|
|
pasteboard.setString(text, forType: .string)
|
|
|
|
guard autoPaste else { return false }
|
|
guard AXIsProcessTrusted() else { throw InsertionError.accessibilityNotGranted }
|
|
|
|
Thread.sleep(forTimeInterval: 0.08)
|
|
postCommandV()
|
|
return true
|
|
}
|
|
|
|
private static func postCommandV() {
|
|
let source = CGEventSource(stateID: .combinedSessionState)
|
|
let keyCode = CGKeyCode(kVK_ANSI_V)
|
|
let keyDown = CGEvent(keyboardEventSource: source, virtualKey: keyCode, keyDown: true)
|
|
let keyUp = CGEvent(keyboardEventSource: source, virtualKey: keyCode, keyDown: false)
|
|
keyDown?.flags = CGEventFlags.maskCommand
|
|
keyUp?.flags = CGEventFlags.maskCommand
|
|
keyDown?.post(tap: CGEventTapLocation.cghidEventTap)
|
|
keyUp?.post(tap: CGEventTapLocation.cghidEventTap)
|
|
}
|
|
}
|