mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
What changed - Experiments › Record typing logs (off by default, App Group key experiments.typingLogs). It is separate from Learn how I type, which is unchanged. While it is on, the keyboard writes NDJSON day files to TypingLogs/yyyy-MM-dd.ndjson in the App Group. Events: session_start/session_end (field traits, keyboard size and safe area, orientation, appearance, device, build, locale); layout (every key's frame and hit frame, the strip and its suggestion slots, and a hash, with the key list written once per file); touch (began/moved/ended/cancelled with raw x/y, majorRadius and tolerance, force, UITouch.timestamp, the key the engine resolved, and the key under the point when they differ; moves are throttled to 2 pt and 8 ms); edit (insert, delete, word delete, autocorrect, autocorrect undo, suggestion, double-space period, return and send, paste, undo dictation, trackpad cursor); candidates; context (documentContextBefore/AfterInput capped at 200 characters, after each word, return and dictation and once host changes settle, marked cleared when the host empties the field); ui (accents, trackpad, the Hex key and its menu, strip chips, globe); dictation (raw, learned fixes, spelling, tagger, Apple Intelligence, translation, formatting, inserted text, durations, engine and locale, from in-keyboard and app dictation); and dictation_outcome (from a second DictationEditTracker). The schema is in ios/docs/typing-logs.md. - Password, new password, one-time code, every credit card content type, and secure fields write one skipped line and nothing else. A new field, or new traits, starts a new session. - TypingLogger buffers events on the main thread. TypingLogStore encodes and appends them on a serial utility queue every 2 s, when the keyboard goes away, and on a memory warning. Each day is capped at 64 MB with one cap_reached line, and files are kept 14 days. When the App Group can't be reached (no Full Access), events are dropped silently. Nothing is sent. - Hooks: FullKeyboardEngine.typingLog, and the KeyboardKeyPlaneView touch path, which the UITouch overrides now reach through began/moved/ended/cancelled(id:sample:). Every call is skipped while the logger is nil, so typing is unchanged with the experiment off. KeyboardSnapshot gains optional stages, sent only while the experiment is on. KeyboardSpeech.transcribe also returns its locale. - App: the toggle and a Typing logs screen (each day with its size and event count, Share per day, Share all through the share sheet, Delete all with a confirmation). Also --snap-state experiments and typing-logs scenes, and an --experiment-typing-logs launch argument. Why Ryan wants to export a day of raw typing to his Mac, to test and train prediction models and a per-user touch model (adaptive key targets). Verified - xcodebuild test on the iPhone 18 Pro simulator: 177 tests, 0 failures. This includes 9 new TypingLogTests: line format and escaping; day rotation with layouts in full once per file; Delete all while the writer holds the file open; the daily cap (tested at 4 KB) across two writers; 14-day retention; the skip rule; a skipped session writing one line; and a real FullKeyboardView typing 25 taps through the plane's touch path (autocorrect, delete, double-space period, the 123 layer, return, a long-press é). That log's edits replay to the field's text, "Don't go yet. 7 ok\nCafé". An independent Python replay of the same file matched, and every lifted key's point was inside its logged hit frame. - Overhead, simulator Debug, 3,000 taps: about 9 µs a tap on the main thread (38 → 47 µs) and about 1 KB a tap (5.2 lines), so about 12 MB an hour at 200 characters a minute. - The Release generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds. - Simulator screenshots of the toggle and the Typing logs screen. - Not verified on a physical iPhone: real touch radii and sampling rates, how often hosts call textDidChange, whether documentIdentifier stays stable, the extension's write cost, and AirDrop from the share sheet.
160 lines
6 KiB
Swift
160 lines
6 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
/// Record typing logs: each day's file, to share (AirDrop it to a Mac) or delete.
|
|
struct TypingLogsView: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var days: [TypingLogDay] = []
|
|
/// Event counts by day and size, counted off the main thread when a row shows.
|
|
@State private var counts: [String: Int] = [:]
|
|
@State private var confirmingDelete = false
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Section {
|
|
if days.isEmpty {
|
|
Text("Type with Hex's keyboard and each day's log shows up here.")
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
ForEach(days) { day in
|
|
row(day)
|
|
.task(id: countKey(day)) { await count(day) }
|
|
}
|
|
} footer: {
|
|
Text("One file per day, kept 14 days, one event per line. Password, code and card fields are never logged.")
|
|
}
|
|
|
|
if !days.isEmpty {
|
|
Section {
|
|
Button("Delete all", role: .destructive) {
|
|
confirmingDelete = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Typing logs")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Done") { dismiss() }
|
|
}
|
|
ToolbarItem(placement: .primaryAction) {
|
|
Button("Share all") {
|
|
ShareSheet.present(days.map(\.url))
|
|
}
|
|
.disabled(days.isEmpty)
|
|
}
|
|
}
|
|
.confirmationDialog("Delete all typing logs?", isPresented: $confirmingDelete, titleVisibility: .visible) {
|
|
Button("Delete all", role: .destructive) {
|
|
TypingLogStore.deleteAll()
|
|
reload()
|
|
}
|
|
} message: {
|
|
Text("Every day's log is removed from this iPhone.")
|
|
}
|
|
.refreshable { reload() }
|
|
}
|
|
.preferredColorScheme(.dark)
|
|
.onAppear(perform: reload)
|
|
}
|
|
|
|
private func row(_ day: TypingLogDay) -> some View {
|
|
HStack {
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(title(day))
|
|
Text(detail(day))
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
Spacer()
|
|
Button {
|
|
ShareSheet.present([day.url])
|
|
} label: {
|
|
Image(systemName: "square.and.arrow.up")
|
|
}
|
|
.buttonStyle(.borderless)
|
|
.accessibilityLabel("Share \(title(day))")
|
|
}
|
|
}
|
|
|
|
private func title(_ day: TypingLogDay) -> String {
|
|
guard let date = day.date else { return day.day }
|
|
if Calendar.current.isDateInToday(date) { return "Today" }
|
|
if Calendar.current.isDateInYesterday(date) { return "Yesterday" }
|
|
return date.formatted(.dateTime.weekday(.wide).month(.abbreviated).day())
|
|
}
|
|
|
|
private func detail(_ day: TypingLogDay) -> String {
|
|
let size = ByteCountFormatter.string(fromByteCount: Int64(day.bytes), countStyle: .file)
|
|
guard let count = counts[countKey(day)] else { return size }
|
|
return "\(size) · \(count.formatted()) " + (count == 1 ? "event" : "events")
|
|
}
|
|
|
|
private func countKey(_ day: TypingLogDay) -> String {
|
|
"\(day.day)|\(day.bytes)"
|
|
}
|
|
|
|
private func count(_ day: TypingLogDay) async {
|
|
let key = countKey(day)
|
|
guard counts[key] == nil else { return }
|
|
let url = day.url
|
|
let count = await Task.detached(priority: .utility) { TypingLogStore.countEvents(in: url) }.value
|
|
counts[key] = count
|
|
}
|
|
|
|
private func reload() {
|
|
TypingLogStore.prune(in: TypingLogStore.defaultDirectory)
|
|
days = TypingLogStore.days()
|
|
}
|
|
}
|
|
|
|
/// The system share sheet for files, over whatever is showing.
|
|
@MainActor
|
|
enum ShareSheet {
|
|
static func present(_ urls: [URL]) {
|
|
guard !urls.isEmpty else { return }
|
|
let scene = UIApplication.shared.connectedScenes
|
|
.compactMap { $0 as? UIWindowScene }
|
|
.first { $0.activationState == .foregroundActive }
|
|
guard var top = scene?.keyWindow?.rootViewController else { return }
|
|
while let presented = top.presentedViewController, !presented.isBeingDismissed {
|
|
top = presented
|
|
}
|
|
let controller = UIActivityViewController(activityItems: urls, applicationActivities: nil)
|
|
controller.popoverPresentationController?.sourceView = top.view
|
|
controller.popoverPresentationController?.sourceRect = CGRect(x: top.view.bounds.midX, y: 60, width: 1, height: 1)
|
|
top.present(controller, animated: true)
|
|
}
|
|
}
|
|
|
|
/// `--snap-state experiments` (the Experiments card) and `--snap-state typing-logs`
|
|
/// (the Typing logs screen) on Simulator; pass `--experiment-typing-logs` to show it on.
|
|
struct ExperimentsSnapshotStage: View {
|
|
let scene: String
|
|
|
|
static var requestedScene: String? {
|
|
let args = ProcessInfo.processInfo.arguments
|
|
guard let idx = args.firstIndex(of: "--snap-state"), idx + 1 < args.count else { return nil }
|
|
let value = args[idx + 1]
|
|
return value == "experiments" || value == "typing-logs" ? value : nil
|
|
}
|
|
|
|
var body: some View {
|
|
if scene == "typing-logs" {
|
|
TypingLogsView()
|
|
} else {
|
|
ScrollView {
|
|
ExperimentsCard()
|
|
.frame(maxWidth: 500)
|
|
.padding(.horizontal, 20)
|
|
.padding(.top, 14)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.background(HexTheme.bgDeep.ignoresSafeArea())
|
|
.preferredColorScheme(.dark)
|
|
}
|
|
}
|
|
}
|