Docs native S2a: Close on No longer available goes through the close guard
It called SwiftUI's dismiss, which closes the window without asking its delegate, so DocsCloseGuard never asked about unsent changes (Fable on #169, finding 11). The callout now closes its own window with performClose, which the guard decides. DocsWindowCloserTests: with changes unsent the window stays and the guard's sheet is up (red with close()); with nothing unsent it closes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+72
−4
native/apple/Sources/DocsKit/DocsDocumentHeader.swift+26 −2native/apple/Sources/DocsKit/DocsWorkspace.swift+1 −2native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift+45 −0
diff --git a/native/apple/Sources/DocsKit/DocsDocumentHeader.swift b/native/apple/Sources/DocsKit/DocsDocumentHeader.swift| index 5f5a8e3..2ddf90b 100644 |
| --- a/native/apple/Sources/DocsKit/DocsDocumentHeader.swift |
| +++ b/native/apple/Sources/DocsKit/DocsDocumentHeader.swift |
| @@ -193,7 +193,10 @@ struct DocsPillStyle: ButtonStyle { |
| let model: DocsModel |
| @Environment(\.locale) private var locale |
| let discard: () -> Void |
| - let close: () -> Void |
| + /// Close (row 1): through the window's own close, so `DocsCloseGuard` |
| + /// still asks about unsent changes. nil uses the window this view is in. |
| + var close: (() -> Void)? = nil |
| + @State private var closer = DocsWindowCloser() |
| /// The account flow, as Settings' Sign in… opens it (the home window). |
| var signIn: () -> Void = {} |
| var body: some View { |
| @@ -208,7 +211,7 @@ struct DocsPillStyle: ButtonStyle { |
| } |
| if model.documentAccess == .unavailable { |
| callout(Text("native.unavailable.body", bundle: DocsResources.bundle), negative: true) { |
| - Button { close() } label: { Text("native.close", bundle: DocsResources.bundle) } |
| + Button { if let close { close() } else { closer.close() } } label: { Text("native.close", bundle: DocsResources.bundle) } |
| if model.hasUnsavedChanges { |
| Button(role: .destructive) { discard() } label: { Text("native.discard.action", bundle: DocsResources.bundle) } |
| } |
| @@ -224,6 +227,7 @@ struct DocsPillStyle: ButtonStyle { |
| } |
| .padding(.top, DocsSpacing.sp3) |
| .frame(maxWidth: 560) |
| + .background { DocsWindowReader(closer: closer).frame(width: 0, height: 0) } |
| } |
| private func callout<Actions: View>(_ text: Text, negative: Bool, @ViewBuilder actions: () -> Actions) -> some View { |
| HStack(spacing: DocsSpacing.sp3) { |
| @@ -242,6 +246,26 @@ struct DocsPillStyle: ButtonStyle { |
| } |
| #endif |
| +#if os(macOS) |
| +/// Closes the window a view is in the way the close button does |
| +/// (`performClose`), so the window's delegate (`DocsCloseGuard`) decides: |
| +/// unsent changes are asked about, never dropped. |
| +@MainActor final class DocsWindowCloser { |
| + weak var window: NSWindow? |
| + func close() { window?.performClose(nil) } |
| +} |
| + |
| +struct DocsWindowReader: NSViewRepresentable { |
| + let closer: DocsWindowCloser |
| + func makeNSView(context: Context) -> ReaderView { let view = ReaderView(); view.closer = closer; return view } |
| + func updateNSView(_ view: ReaderView, context: Context) { view.closer = closer; closer.window = view.window } |
| + final class ReaderView: NSView { |
| + weak var closer: DocsWindowCloser? |
| + override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); closer?.window = window } |
| + } |
| +} |
| +#endif |
| + |
| /// One rename's commit rules (Fable on #169, finding 2). It is finished |
| /// only by a commit the model accepts, or by putting the title back: |
| /// - Return: commit; refused, the field stays with the error shown, and a |
diff --git a/native/apple/Sources/DocsKit/DocsWorkspace.swift b/native/apple/Sources/DocsKit/DocsWorkspace.swift| index f0f2c06..8aaa430 100644 |
| --- a/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| +++ b/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| @@ -39,7 +39,6 @@ import SwiftUI |
| /// The floating Format inspector, one state for the whole app (spec D1). |
| @AppStorage(DocsInspectorState.key) private var inspectorOpen = DocsInspectorState.defaultOpen |
| @Environment(\.layoutDirection) private var layoutDirection |
| - @Environment(\.dismiss) private var dismissWindow |
| #endif |
| /// The subtitle's team (spec D7): the account's team, or the server |
| /// account's name. nil says the state alone. |
| @@ -311,7 +310,7 @@ import SwiftUI |
| // One title (spec §1): the window's title stays the document's, |
| // for the Window menu, tabs and VoiceOver, but the system does |
| // not draw it; Docs' title area does, with the subtitle. |
| - .overlay(alignment: .top) { DocsDocumentCallouts(model: model, discard: { discarding = true }, close: { dismissWindow() }, signIn: { openWindow(id: "home") }) } |
| + .overlay(alignment: .top) { DocsDocumentCallouts(model: model, discard: { discarding = true }, signIn: { openWindow(id: "home") }) } |
| .toolbar(removing: .title) |
| .toolbar { |
| ToolbarItem(placement: .navigation) { |
diff --git a/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift b/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift| new file mode 100644 |
| index 0000000..b83c3e8 |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift |
| @@ -0,0 +1,45 @@ |
| +#if os(macOS) |
| +import AppKit |
| +import XCTest |
| +@testable import DocsKit |
| + |
| +/// Close on the "No longer available" callout (Fable on #169, finding 11) |
| +/// goes through the window's own close, so DocsCloseGuard asks about unsent |
| +/// changes rather than the window simply going away. |
| +@MainActor final class DocsWindowCloserTests: XCTestCase { |
| + func testCloseAsksTheCloseGuardWhenChangesAreUnsent() async throws { |
| + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) |
| + window.isReleasedWhenClosed = false |
| + let content = NSView(frame: window.contentLayoutRect) |
| + window.contentView = content |
| + let guardView = DocsCloseGuardView() |
| + guardView.hasUnsaved = { true } |
| + content.addSubview(guardView) |
| + window.orderFrontRegardless() |
| + defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil) } |
| + XCTAssertTrue(window.delegate === guardView, "the guard is the window's delegate") |
| + |
| + let closer = DocsWindowCloser() |
| + closer.window = window |
| + closer.close() |
| + try await Task.sleep(for: .milliseconds(300)) |
| + XCTAssertTrue(window.isVisible, "unsent changes: the window stays") |
| + XCTAssertNotNil(window.attachedSheet, "and the close guard asks") |
| + } |
| + |
| + func testCloseClosesAWindowWithNothingUnsent() async throws { |
| + let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) |
| + window.isReleasedWhenClosed = false |
| + let content = NSView(frame: window.contentLayoutRect) |
| + window.contentView = content |
| + let guardView = DocsCloseGuardView() |
| + content.addSubview(guardView) |
| + window.orderFrontRegardless() |
| + let closer = DocsWindowCloser() |
| + closer.window = window |
| + closer.close() |
| + try await Task.sleep(for: .milliseconds(200)) |
| + XCTAssertFalse(window.isVisible) |
| + } |
| +} |
| +#endif |