Native: no editor opens during a sheet switch, and none outlives one
Review #72 delta, finding 1. A commit while the next sheet loaded was refused (rightly) but its editor stayed open, and after the switch an editor could sit at the old coordinate on the new sheet until Escape. Now no editor opens while a sheet is opening (Mac grid and iOS key field), and a page change closes any open editor and resets the iOS field. Tests: testNoEditorSurvivesASheetSwitch, testAnOpenEditorClosesWhenTheSheetChanges (both fail without this).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+58
−3
native/apple/Sources/SheetsKit/SheetGrid.swift+5 −2native/apple/Sources/SheetsKit/SheetKeySink.swift+9 −1native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift+44 −0
diff --git a/native/apple/Sources/SheetsKit/SheetGrid.swift b/native/apple/Sources/SheetsKit/SheetGrid.swift| index a460842..98d37ad 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetGrid.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetGrid.swift |
| @@ -92,7 +92,7 @@ struct SheetGrid: View { |
| } |
| // The grid has the keyboard, or nothing does: a cell's field has just let |
| // go of it (SheetCellEditor) and SwiftUI has not handed it back yet. |
| - guard editState.cell == nil, model.mayWrite, !model.dirty, let window = event.window, |
| + guard editState.cell == nil, model.mayWrite, !model.dirty, !model.opening, let window = event.window, |
| model.gridFocused || window.firstResponder === window, |
| !(window.firstResponder is NSText) |
| else { return false } |
| @@ -122,7 +122,8 @@ struct SheetGrid: View { |
| } |
| #endif |
| private func edit(_ point: Coordinate) { |
| - guard model.mayWrite else { return } |
| + // Mid-switch the edit could only be committed to the sheet being left. |
| + guard model.mayWrite, !model.opening else { return } |
| #if os(macOS) |
| editState.cell = point |
| #else |
| @@ -370,6 +371,8 @@ private struct SheetSelectionObserver: View { |
| .onChange(of: model.selection.start) { _, point in |
| if state.cell != point { state.cell = nil } |
| } |
| + // A new sheet: no editor of the old one stays open over it. |
| + .onChange(of: model.page?.Sheet.ID) { state.cell = nil } |
| } |
| } |
diff --git a/native/apple/Sources/SheetsKit/SheetKeySink.swift b/native/apple/Sources/SheetsKit/SheetKeySink.swift| index 07b3615..3bc56fe 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetKeySink.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetKeySink.swift |
| @@ -27,6 +27,7 @@ struct SheetKeySink: UIViewRepresentable { |
| func updateUIView(_ field: SinkField, context: Context) { |
| field.model = model |
| field.move = move |
| + field.pageChanged(to: model.page?.Sheet.ID) |
| field.selectionChanged(to: model.selection.start) |
| if focusRequest != field.focusRequest { |
| field.focusRequest = focusRequest |
| @@ -95,6 +96,13 @@ struct SheetKeySink: UIViewRepresentable { |
| if window != nil && focusRequest > 0 { claimKeyboard() } |
| } |
| + private var sheet: UInt64? |
| + /// Another sheet opened: no edit of the old one carries over. |
| + func pageChanged(to id: UInt64?) { |
| + guard id != sheet else { return } |
| + sheet = id |
| + if typing || !(text ?? "").isEmpty { reset() } |
| + } |
| /// A commit, a cancel, or a tap moved the selection: this edit is over. |
| func selectionChanged(to point: Coordinate) { |
| guard point != cell else { return } |
| @@ -115,7 +123,7 @@ struct SheetKeySink: UIViewRepresentable { |
| guard !quiet, let model else { return } |
| let value = text ?? "" |
| if !typing { |
| - guard model.mayWrite, !model.dirty, !value.isEmpty || markedTextRange != nil else { |
| + guard model.mayWrite, !model.dirty, !model.opening, !value.isEmpty || markedTextRange != nil else { |
| if !model.mayWrite { reset() } |
| return |
| } |
diff --git a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift| index 0c62a6e..15b4f07 100644 |
| --- a/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/FirstKeystrokeTests.swift |
| @@ -282,6 +282,50 @@ final class FirstKeystrokeTests: XCTestCase { |
| print("MEASURE first key to glyph: " + report.joined(separator: "; ")) |
| } |
| + /// Review #72 delta, finding 1: no editor of the old sheet survives, or |
| + /// opens during, a switch to another. |
| + @MainActor func testNoEditorSurvivesASheetSwitch() async throws { |
| + let server = SheetServerFixture() |
| + server.holdPages() |
| + let model = fixtureModel(server) |
| + let (window, host) = try await host(model) |
| + defer { window.orderOut(nil) } |
| + key("x", code: 7, window: window) |
| + try await Task.sleep(for: .milliseconds(150)) |
| + XCTAssertFalse(editors(host).isEmpty) |
| + model.discard() |
| + let opening = Task { await model.open(SheetSummary(ID: 2, Title: "", TabID: 0, TabName: "")) } |
| + let loading = await eventually { server.requests.contains("GET /sheets/2") } |
| + XCTAssertTrue(loading) |
| + key("\u{1b}", code: 53, window: window) |
| + try await Task.sleep(for: .milliseconds(100)) |
| + key("y", code: 16, window: window) |
| + try await Task.sleep(for: .milliseconds(150)) |
| + XCTAssertTrue(editors(host).isEmpty, "no editor opens while the sheet is being replaced") |
| + server.releasePages() |
| + await opening.value |
| + try await Task.sleep(for: .milliseconds(100)) |
| + XCTAssertEqual(model.page?.Sheet.ID, 2) |
| + XCTAssertTrue(editors(host).isEmpty) |
| + XCTAssertEqual(model.draft, "") |
| + } |
| + |
| + @MainActor func testAnOpenEditorClosesWhenTheSheetChanges() async throws { |
| + let server = SheetServerFixture() |
| + let model = fixtureModel(server) |
| + let (window, host) = try await host(model) |
| + defer { window.orderOut(nil) } |
| + // A1, where the new sheet's selection starts too: only the page changes. |
| + model.select(Coordinate(r: 0, c: 0)) |
| + key("x", code: 7, window: window) |
| + try await Task.sleep(for: .milliseconds(150)) |
| + XCTAssertFalse(editors(host).isEmpty) |
| + model.discard() |
| + await model.open(SheetSummary(ID: 2, Title: "", TabID: 0, TabName: "")) |
| + try await Task.sleep(for: .milliseconds(100)) |
| + XCTAssertTrue(editors(host).isEmpty, "a stray editor of the old sheet stayed open") |
| + } |
| + |
| /// Held keys are never dropped: if the field does not come, what was typed |
| /// is handed on (to the formula bar) rather than discarded. |
| @MainActor func testHeldKeysAreHandedOnIfTheFieldNeverComes() async throws { |