Docs native S1a: the toolbar follows the text once input settles (#170, final 1)
Fable's final check, finding 1 (medium). refreshToolbarSelection keeps the last state while input is pending, and every call after a key came too early: plain typing reuses its generation, so didRender returned before its refresh, and nothing refreshed when the queue drained. Return at the end of a heading left the style at Heading 1; typing over a selection left Clear formatting enabled; a caret moved during a 401 pause kept the old style after the session's 200. It now refreshes wherever DocumentCommands.isEnabled can turn true: the queue's drain, every draw (before didRender's guard), composition ending, and the detail, inputPaused and sessionPaused setters. SurfaceChromeTests: one case per repro, each comparing the toolbar's state with one computed afresh; all three failed before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+69
−2
native/apple/Sources/DocsKit/DocsModel.swift+12 −2native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift+57 −0
diff --git a/native/apple/Sources/DocsKit/DocsModel.swift b/native/apple/Sources/DocsKit/DocsModel.swift| index 5edae79..f653425 100644 |
| --- a/native/apple/Sources/DocsKit/DocsModel.swift |
| +++ b/native/apple/Sources/DocsKit/DocsModel.swift |
| @@ -25,6 +25,7 @@ import RastrilloNative |
| guard newValue != _detail else { return } |
| withMutation(keyPath: \.detail) { _detail = newValue } |
| pushEditability() |
| + refreshToolbarSelection() |
| } |
| } |
| /// The newest projection published. Not observed: the editor draws it |
| @@ -72,6 +73,7 @@ import RastrilloNative |
| guard newValue != _inputPaused else { return } |
| withMutation(keyPath: \.inputPaused) { _inputPaused = newValue } |
| pushEditability() |
| + refreshToolbarSelection() |
| // Not a flush trigger (spec §7 Design 6, "Two events are not |
| // triggers"): canRender does not read inputPaused, and must not, |
| // since a reader is paused for good and still sees peers' |
| @@ -94,6 +96,7 @@ import RastrilloNative |
| guard newValue != _sessionPaused else { return } |
| withMutation(keyPath: \.sessionPaused) { _sessionPaused = newValue } |
| pushEditability() |
| + refreshToolbarSelection() |
| } |
| } |
| /// Typing and commands are refused: paused for any reason. |
| @@ -447,6 +450,7 @@ import RastrilloNative |
| editingTask = nil |
| // Trigger (b): the queue drained. |
| surface.flushIfDeferred() |
| + refreshToolbarSelection() |
| } |
| } |
| while documentEpoch == epoch, !editing.isEmpty, let replica { |
| @@ -469,6 +473,7 @@ import RastrilloNative |
| if !active { |
| // Trigger (c): composition ended. |
| surface.flushIfDeferred() |
| + refreshToolbarSelection() |
| if editingTask == nil { Task { await publish() } } |
| } |
| } |
| @@ -476,6 +481,9 @@ import RastrilloNative |
| func currentSelection() -> NSRange { selection } |
| func selectionMovedExplicitly() { typingMarks = nil } |
| func didRender(_ generation: UInt64) { |
| + // Every draw, also of a generation already rendered (plain typing |
| + // reuses its source): the toolbar may show the selection now. |
| + defer { refreshToolbarSelection() } |
| // A delayed acknowledgement cannot retire the currently displayed source. |
| guard generation > renderedGeneration else { return } |
| renderedGeneration = generation |
| @@ -483,7 +491,6 @@ import RastrilloNative |
| // projectionGeneration, not renderedGeneration, so trigger (d) is |
| // projectionGeneration's didSet. The call costs nothing. |
| surface.flushIfDeferred() |
| - refreshToolbarSelection() |
| } |
| private func publish() async { await publications.run { await self.publishCurrent() } } |
| private func publishCurrent() async { |
| @@ -832,7 +839,10 @@ import RastrilloNative |
| refreshToolbarSelection() |
| } |
| /// The toolbar's state, from the live text at the selection; kept while |
| - /// input is pending, as before. |
| + /// input is pending, as before. Called wherever `DocumentCommands.isEnabled` |
| + /// can turn true (the queue draining, a draw, composition ending, input |
| + /// or the session unpaused, the access changing) and when the selection |
| + /// moves, so the kept state never outlives the input that held it. |
| func refreshToolbarSelection() { |
| guard let value = DocumentToolbarSelection(commands: DocumentCommands(model: self, content: surface.content)), |
| value != toolbarSelection else { return } |
diff --git a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift| index c33376b..6e9fd9e 100644 |
| --- a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift |
| @@ -91,6 +91,63 @@ import XCTest |
| XCTAssertTrue(model.history.canUndo, "the reader's history is the projection's") |
| } |
| + /// The toolbar and the inspector show `toolbarSelection`: once input |
| + /// settles it must equal one computed afresh from the live text at the |
| + /// selection (Fable on #170, final check, finding 1). |
| + private var fresh: DocumentToolbarSelection? { DocumentToolbarSelection(commands: DocumentCommands(model: model, content: model.surface.content)) } |
| + private func settle() async throws { |
| + try await until("the queue drained and drawn") { !model.hasPendingInput && model.surface.newest.map { $0.revision == model.surface.appliedRevision } ?? false } |
| + try await Task.sleep(for: .milliseconds(20)) |
| + } |
| + |
| + /// Return at the end of a heading: the caret is in a new paragraph. |
| + func testTheToolbarFollowsReturnAtTheEndOfAHeading() async throws { |
| + let heading = (model.surface.content.string as NSString).range(of: "Lists, an image and a rule") |
| + Keyboard.select(NSRange(location: NSMaxRange(heading), length: 0), in: text) |
| + try await settle() |
| + XCTAssertEqual(model.toolbarSelection.style, "heading1") |
| + Keyboard.type("\n", in: text) |
| + try await settle() |
| + let now = try XCTUnwrap(fresh) |
| + XCTAssertEqual(now.style, "paragraph") |
| + XCTAssertEqual(model.toolbarSelection, now, "the toolbar shows the new paragraph's style") |
| + } |
| + |
| + /// Typing over a selection: nothing is selected afterwards, so Clear |
| + /// formatting is disabled. |
| + func testTheToolbarFollowsTypingOverASelection() async throws { |
| + let line = (model.surface.content.string as NSString).range(of: LoopFixture.middle) |
| + Keyboard.select(NSRange(location: line.location, length: 8), in: text) |
| + try await settle() |
| + XCTAssertTrue(model.toolbarSelection.hasSelection) |
| + Keyboard.type("q", in: text) |
| + try await settle() |
| + let now = try XCTUnwrap(fresh) |
| + XCTAssertFalse(now.hasSelection) |
| + XCTAssertEqual(model.toolbarSelection, now, "nothing selected after typing over it") |
| + } |
| + |
| + /// The caret moves while input is paused (a 401), then the re-minted |
| + /// session's 200 resumes it: the toolbar shows where the caret is now. |
| + func testTheToolbarFollowsTheCaretMovedWhileInputWasPaused() async throws { |
| + let line = (model.surface.content.string as NSString).range(of: LoopFixture.middle) |
| + Keyboard.select(NSRange(location: line.location + 2, length: 0), in: text) |
| + try await settle() |
| + XCTAssertEqual(model.toolbarSelection.style, "paragraph") |
| + await server.answer(401) |
| + await model.revalidateAccess() |
| + XCTAssertTrue(model.inputBlocked) |
| + let heading = (model.surface.content.string as NSString).range(of: "Lists, an image and a rule") |
| + Keyboard.select(NSRange(location: heading.location + 2, length: 0), in: text) |
| + await server.answer(200) |
| + await model.revalidateAccess() |
| + XCTAssertFalse(model.inputBlocked) |
| + try await settle() |
| + let now = try XCTUnwrap(fresh) |
| + XCTAssertEqual(now.style, "heading1") |
| + XCTAssertEqual(model.toolbarSelection, now, "the toolbar shows the heading the caret moved into") |
| + } |
| + |
| #if DOCS_PROBES |
| /// The live word count recounts after a draw without rendering the |
| /// document: it reads the projection's text and the text view's storage. |