native: the Format sheet's state goes with it, and ••• waits for it
Fable on #180 (3). `formatting` and the header's `sheetOpen` outlived the document: Back, a swipe, Delete, Discard and losing the right to write left them set, and a text view built again under the sheet (a new `editorIdentity`) came up with the keyboard and no inset. - `closeFormatting()` closes the sheet and undoes at once what it changed (keyboard suppression, the inset, `sheetOpen`): from `showLibrary()`, when the document's view disappears (where no `onChange` runs any more) and when access stops being writable. - Each ••• action closes the sheet first and runs in its `onDismiss`, so the next presentation (Share, Rename, Word count, ...) is not dropped. - The surface remembers the suppression and the sheet's height and gives them to a text view it attaches. Tests: PhoneDocumentTests.testANewTextViewUnderTheSheetKeepsItsState (failed before: the new text view had the keyboard and no inset); the writer XCUITest now opens Word count from ••• with the sheet up (the sheet goes, the count comes, the keyboard returns) and goes Back with the sheet up (no sheet on return, and the keyboard comes, not an empty input view; this step failed while the reset relied on onChange).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+88
−13
native/apple/Sources/DocsKit/DocsWorkspace.swift+36 −9native/apple/Sources/DocsKit/DocumentSurface.swift+11 −1native/apple/Tests/DocsKitTests/PhoneDocumentTests.swift+22 −0native/ios/UITests/NativeUITests.swift+19 −3
diff --git a/native/apple/Sources/DocsKit/DocsWorkspace.swift b/native/apple/Sources/DocsKit/DocsWorkspace.swift| index 87e05d5..e56f57b 100644 |
| --- a/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| +++ b/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| @@ -36,6 +36,9 @@ import SwiftUI |
| @State private var phoneChrome: DocsPhoneEditorChrome |
| /// The Format sheet (Aa), and Rename's alert. |
| @State private var formatting = false |
| + /// A ••• action waiting for the Format sheet to go (two sheets cannot be |
| + /// up at once: the second would be dropped). |
| + @State private var afterFormatting: (() -> Void)? |
| @State private var renamingAlert = false |
| @State private var renameText = "" |
| #endif |
| @@ -325,6 +328,7 @@ import SwiftUI |
| private func showLibrary() { |
| column = .sidebar |
| #if os(iOS) |
| + closeFormatting() |
| phonePath = [] |
| #endif |
| } |
| @@ -701,7 +705,11 @@ extension DocsWorkspace { |
| DocsPhoneDocumentControls(model: model, origin: client.origin, paged: $paged, actions: phoneActions) |
| } |
| } |
| - .sheet(isPresented: $formatting) { |
| + .sheet(isPresented: $formatting, onDismiss: { |
| + let next = afterFormatting |
| + afterFormatting = nil |
| + next?() |
| + }) { |
| DocsFormatSheet(model: model, close: { formatting = false }) |
| .presentationDetents([.medium]) |
| .presentationBackgroundInteraction(.enabled(upThrough: .medium)) |
| @@ -719,6 +727,10 @@ extension DocsWorkspace { |
| phoneChrome.header.sheetOpen = open |
| } |
| .onAppear { phoneChrome.openFormat = { formatting = true } } |
| + // The sheet goes with the document (Back, a swipe, Delete, Discard) |
| + // and with the right to write. |
| + .onDisappear { closeFormatting() } |
| + .onChange(of: model.documentAccess) { _, access in if access != .writable { closeFormatting() } } |
| .alert(Text("docs.document.rename", bundle: DocsResources.bundle), isPresented: $renamingAlert) { |
| TextField(text: $renameText) { Text("docs.list.create.placeholder", bundle: DocsResources.bundle) } |
| .accessibilityIdentifier("docs.rename.field") |
| @@ -737,20 +749,35 @@ extension DocsWorkspace { |
| .sheet(isPresented: $showingColophon) { DocsColophonView { showingColophon = false } } |
| } |
| + /// The Format sheet closed, and what it changed undone at once: a view |
| + /// that has gone runs no `onChange`, and a text view attached later |
| + /// takes the surface's state (Fable on #180). |
| + func closeFormatting() { |
| + formatting = false |
| + afterFormatting = nil |
| + model.surface.suppressKeyboard(false) |
| + model.surface.sheetCovers(0) |
| + phoneChrome.header.sheetOpen = false |
| + } |
| + |
| var phoneActions: DocsPhoneActions { |
| var actions = DocsPhoneActions() |
| - actions.share = { share() } |
| - actions.rename = { |
| + // Each closes the Format sheet first, then runs. |
| + func run(_ action: @escaping () -> Void) -> () -> Void { |
| + { if formatting { afterFormatting = action; formatting = false } else { action() } } |
| + } |
| + actions.share = run { share() } |
| + actions.rename = run { |
| guard model.documentAccess == .writable else { return } |
| renameText = model.detail?.title ?? "" |
| renamingAlert = true |
| } |
| - actions.export = { kind in exportRequest = kind } |
| - actions.wordCount = { countingWords = true } |
| - actions.openDocument = { jumping = true } |
| - actions.shortcuts = { showingShortcuts = true } |
| - actions.colophon = { showingColophon = true } |
| - actions.delete = { requestDelete() } |
| + actions.export = { kind in run { exportRequest = kind }() } |
| + actions.wordCount = run { countingWords = true } |
| + actions.openDocument = run { jumping = true } |
| + actions.shortcuts = run { showingShortcuts = true } |
| + actions.colophon = run { showingColophon = true } |
| + actions.delete = run { requestDelete() } |
| actions.format = { formatting = true } |
| return actions |
| } |
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift| index 8c371bc..b49c8f6 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -162,6 +162,9 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| text.isEditable = isEditable |
| #if os(iOS) |
| text.showsKeyboardBar = isEditable |
| + // The Format sheet's state, for a text view built while it is open. |
| + text.keyboardSuppressed = keyboardSuppressed |
| + text.bottomObstruction = sheetHeight |
| #endif |
| text.ruleCommand = { [weak self] command in self?.ruleEdit(command) ?? false } |
| text.clipboardEdit = { [weak self] blocks, range, replacement in self?.clipboardEdit(blocks: blocks, range: range, replacement: replacement) ?? false } |
| @@ -191,12 +194,19 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| } |
| #if os(iOS) |
| + /// Remembered for a text view attached while the sheet is open. |
| + private var keyboardSuppressed = false |
| + private var sheetHeight: CGFloat = 0 |
| /// The Format sheet is open (iPhone, S3): the keyboard goes, the text |
| /// view keeps first responder and the selection. |
| - func suppressKeyboard(_ suppressed: Bool) { textView?.keyboardSuppressed = suppressed } |
| + func suppressKeyboard(_ suppressed: Bool) { |
| + keyboardSuppressed = suppressed |
| + textView?.keyboardSuppressed = suppressed |
| + } |
| /// The sheet's height over the text: it scrolls clear of it, the |
| /// selection kept in view above it. |
| func sheetCovers(_ height: CGFloat) { |
| + sheetHeight = height |
| guard let text = textView, abs(text.bottomObstruction - height) > 0.5 else { return } |
| text.bottomObstruction = height |
| if height > 0 { text.scrollRangeToVisible(text.selectedRange) } |
diff --git a/native/apple/Tests/DocsKitTests/PhoneDocumentTests.swift b/native/apple/Tests/DocsKitTests/PhoneDocumentTests.swift| index b6fbb15..4f4894c 100644 |
| --- a/native/apple/Tests/DocsKitTests/PhoneDocumentTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/PhoneDocumentTests.swift |
| @@ -66,6 +66,28 @@ import XCTest |
| XCTAssertEqual(text.contentOffset.y, offset.y, accuracy: 0.5, "nor did the scroll") |
| } |
| + /// A text view the surface attaches while the Format sheet is open (the |
| + /// editor built again: a new `editorIdentity`) gets the sheet's state: |
| + /// no keyboard, and the text clear of the sheet (Fable on #180). |
| + func testANewTextViewUnderTheSheetKeepsItsState() { |
| + let surface = DocumentSurface() |
| + surface.isEditable = true |
| + let first = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) |
| + surface.attach(first) |
| + surface.suppressKeyboard(true) |
| + surface.sheetCovers(300) |
| + let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) |
| + surface.attach(second) |
| + XCTAssertTrue(second.keyboardSuppressed, "the keyboard stays away under the sheet") |
| + XCTAssertEqual(second.bottomObstruction, 300, "and the text clear of it") |
| + surface.suppressKeyboard(false) |
| + surface.sheetCovers(0) |
| + let third = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) |
| + surface.attach(third) |
| + XCTAssertFalse(third.keyboardSuppressed) |
| + XCTAssertEqual(third.bottomObstruction, 0) |
| + } |
| + |
| /// Spec §3, "Header and scrolling": the title folds into the bar on the |
| /// keyboard and with the Format sheet, and unfolds only at the top with |
| /// the keyboard down and the sheet closed. |
diff --git a/native/ios/UITests/NativeUITests.swift b/native/ios/UITests/NativeUITests.swift| index b41ec41..5e223d3 100644 |
| --- a/native/ios/UITests/NativeUITests.swift |
| +++ b/native/ios/UITests/NativeUITests.swift |
| @@ -328,9 +328,15 @@ final class NativeUITests: XCTestCase { |
| XCTAssertFalse(app.buttons["docs.keyboard.bold"].exists, "no keyboard bar under the sheet") |
| let sheet = XCTAttachment(screenshot: app.screenshot()); sheet.name = "S3 Format sheet over a selection"; sheet.lifetime = .keepAlways; add(sheet) |
| try audit(app) |
| - app.buttons["docs.format.close"].tap() |
| - XCTAssertTrue(anything(app, "docs.format.sheet").waitForNonExistence(timeout: 5)) |
| - XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "closing the sheet brings the keyboard back: it was up") |
| + // ••• with the sheet up: the sheet goes, then what was asked for |
| + // comes (two sheets cannot be up at once). |
| + app.buttons["docs.more"].tap() |
| + app.buttons["Word count"].tap() |
| + XCTAssertTrue(anything(app, "docs.format.sheet").waitForNonExistence(timeout: 5), "••• closes the Format sheet") |
| + XCTAssertTrue(app.navigationBars["Word count"].waitForExistence(timeout: 5), "then shows the word count") |
| + app.navigationBars["Word count"].buttons["Close"].tap() |
| + XCTAssertTrue(app.navigationBars["Word count"].waitForNonExistence(timeout: 5)) |
| + XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "the keyboard comes back with the sheet gone: it was up") |
| // Keyboard down, back at the top: the title unfolds; a scroll folds it. |
| app.buttons["docs.keyboard.hide"].tap() |
| @@ -339,6 +345,16 @@ final class NativeUITests: XCTestCase { |
| XCTAssertTrue(inline.waitForNonExistence(timeout: 5), "at the top with the keyboard down, the title unfolds") |
| editor.swipeUp() |
| XCTAssertTrue(inline.waitForExistence(timeout: 5), "scrolling past the title folds it") |
| + |
| + // Back with the Format sheet up: nothing of it is left behind. |
| + app.buttons["docs.format.toggle"].tap() |
| + XCTAssertTrue(anything(app, "docs.format.sheet").waitForExistence(timeout: 5)) |
| + app.buttons["BackButton"].tap() |
| + XCTAssertTrue(anything(app, "docs.format.sheet").waitForNonExistence(timeout: 5), "Back takes the sheet with it") |
| + let again = try openPhoneDocument(app, "Native review document") |
| + XCTAssertFalse(anything(app, "docs.format.sheet").waitForExistence(timeout: 2), "and it is not there when the document opens again") |
| + again.tap() |
| + XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "the keyboard comes, not an empty input view") |
| } |
| /// A reader: back and ••• only, ••• without Share, Rename or Delete, |