Mac: a key that finds the cell's field late goes to the formula bar too
Review #72 delta 2, finding 2. When the next key, not the timer, noticed that the type-ahead had expired, the held text went to the formula bar and that key itself was lost. It is now handed on with the rest. The type-ahead's timeout is a grid parameter so a test can make it lapse. Test: testAKeyThatFindsTheFieldLateIsHandedOnToo.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+30
−4
native/apple/Sources/SheetsKit/SheetGrid.swift+8 −1native/apple/Sources/SheetsKit/SheetTypeAhead.swift+3 −3native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift+19 −0
diff --git a/native/apple/Sources/SheetsKit/SheetGrid.swift b/native/apple/Sources/SheetsKit/SheetGrid.swift| index f7c3b61..8676be7 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetGrid.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetGrid.swift |
| @@ -23,6 +23,9 @@ struct SheetGrid: View { |
| @State private var keyboardRequest = 0 |
| #endif |
| var beginEditing: () -> Void = {} |
| + /// How long typed keys wait for the cell's field before going to the |
| + /// formula bar instead. Tests shorten it. |
| + var typeAheadTimeout: TimeInterval = 1 |
| var body: some View { |
| content.equatable() |
| .accessibilityLabel("Spreadsheet grid") |
| @@ -85,7 +88,11 @@ struct SheetGrid: View { |
| private func handleKey(_ event: NSEvent) -> Bool { |
| if typeAhead.awaitingField { |
| if typeAhead.expired || editState.cell == nil { |
| + // This key arrived after the field should have: it goes on to the |
| + // formula bar with the rest, not into the void. |
| + if SheetTypeAhead.startsTyping(event) { typeAhead.hold(event) } |
| fieldNeverCame() |
| + return SheetTypeAhead.startsTyping(event) |
| } else if event.modifierFlags.intersection([.command, .control]).isEmpty { |
| typeAhead.hold(event) |
| return true |
| @@ -108,7 +115,7 @@ struct SheetGrid: View { |
| // cell may be scrolled out of view: bring it into view too. |
| let point = model.selection.start |
| scroller.reveal(point, at: metrics.frame(point)) |
| - typeAhead.begin(with: event, expired: fieldNeverCame) |
| + typeAhead.begin(with: event, timeout: typeAheadTimeout, expired: fieldNeverCame) |
| edit(point) |
| if editState.cell == nil { typeAhead.cancel(); return false } |
| return true |
diff --git a/native/apple/Sources/SheetsKit/SheetTypeAhead.swift b/native/apple/Sources/SheetsKit/SheetTypeAhead.swift| index 79596c4..cad13bd 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetTypeAhead.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetTypeAhead.swift |
| @@ -42,13 +42,13 @@ import SwiftUI |
| private var generation = 0 |
| /// `expired` runs if the field has not come a second later; the grid then |
| /// hands what was typed to the formula bar rather than drop it. |
| - func begin(with event: NSEvent, expired: @escaping () -> Void) { |
| + func begin(with event: NSEvent, timeout: TimeInterval = 1, expired: @escaping () -> Void) { |
| held = [event] |
| awaitingField = true |
| - deadline = ProcessInfo.processInfo.systemUptime + 1 |
| + deadline = ProcessInfo.processInfo.systemUptime + timeout |
| generation += 1 |
| let current = generation |
| - DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in |
| + DispatchQueue.main.asyncAfter(deadline: .now() + timeout) { [weak self] in |
| guard let self, self.awaitingField, self.generation == current else { return } |
| expired() |
| } |
diff --git a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift| index 95200cf..63782c6 100644 |
| --- a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift |
| @@ -368,6 +368,25 @@ final class FirstKeystrokeTests: XCTestCase { |
| XCTAssertEqual(handedOn, "xy", "the typed text, without the Return") |
| } |
| + /// Review #72 delta 2, finding 2: when the next key (not the timer) finds |
| + /// the field late, that key goes to the formula bar too. |
| + @MainActor func testAKeyThatFindsTheFieldLateIsHandedOnToo() async throws { |
| + let model = fixtureModel(SheetServerFixture()) |
| + var formulaBar = 0 |
| + let host = NSHostingView(rootView: SheetGrid(model: model, beginEditing: { formulaBar += 1 }, typeAheadTimeout: 0)) |
| + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 640, height: 400), styleMask: [.titled], backing: .buffered, defer: false) |
| + window.contentView = host; NSApp.setActivationPolicy(.regular); window.makeKeyAndOrderFront(nil); NSApp.activate() |
| + defer { window.orderOut(nil) } |
| + try await Task.sleep(for: .milliseconds(200)) |
| + click(NSPoint(x: 100, y: 400 - 32 - 32 - 16), window: window) |
| + try await Task.sleep(for: .milliseconds(100)) |
| + // Both in one turn of the run loop: the second finds the deadline passed. |
| + key("x", code: 7, window: window) |
| + key("y", code: 16, window: window) |
| + XCTAssertEqual(model.draft, "xy", "the late key was lost") |
| + XCTAssertEqual(formulaBar, 1, "the edit carries on in the formula bar") |
| + } |
| + |
| @MainActor func testWhichKeysStartTyping() { |
| func event(_ chars: String, _ ignoring: String, _ code: UInt16, _ flags: NSEvent.ModifierFlags = []) -> NSEvent { |
| NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: flags, timestamp: 0, windowNumber: 0, context: nil, characters: chars, charactersIgnoringModifiers: ignoring, isARepeat: false, keyCode: code)! |