oficina / docs Public

Delete…: close the window once the confirmation sheet has gone (#178, finding 2)

Reproduced: a SwiftUI confirmation dialog whose button closes the window at
once (the fastest a delete can come back from a local server) left the
window open. performClose on a window with a sheet still attached beeps and
does nothing, so a fast delete left an empty window behind. DocsWindowCloser
now waits for the attached sheet to go (at most two seconds), then closes
through performClose as before, so DocsCloseGuard still decides.

Test: DocsWindowCloserTests.testCloseFromAConfirmationDialogClosesTheWindow,
which failed (window still open after 5 s) before this change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 246311433bd35eb44fda70063dd2bbb3edfbdbeb parent 8614d80
2 files changed, +66 −1
  • native/apple/Sources/DocsKit/DocsDocumentHeader.swift +15 −1
  • native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift +51 −0
diff --git a/native/apple/Sources/DocsKit/DocsDocumentHeader.swift b/native/apple/Sources/DocsKit/DocsDocumentHeader.swift
index 7e3b606..c85d2fe 100644
--- a/native/apple/Sources/DocsKit/DocsDocumentHeader.swift
+++ b/native/apple/Sources/DocsKit/DocsDocumentHeader.swift
@@ -268,7 +268,21 @@ struct DocsPillStyle: ButtonStyle {
/// unsent changes are asked about, never dropped.
@MainActor final class DocsWindowCloser {
weak var window: NSWindow?
- func close() { window?.performClose(nil) }
+ /// A window with a sheet still attached refuses `performClose` (it beeps
+ /// and stays). Delete…'s confirmation is such a sheet, and a fast server
+ /// answers while it is still going away (Fable on #178, finding 2), so
+ /// the close waits for the sheet to go first, for at most two seconds.
+ func close() {
+ guard let window else { return }
+ guard window.attachedSheet != nil else { window.performClose(nil); return }
+ Task { @MainActor [weak window] in
+ for _ in 0..<100 {
+ guard let window, window.attachedSheet != nil else { break }
+ try? await Task.sleep(for: .milliseconds(20))
+ }
+ window?.performClose(nil)
+ }
+ }
}
struct DocsWindowReader: NSViewRepresentable {
diff --git a/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift b/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift
index b83c3e8..1f2867d 100644
--- a/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift
+++ b/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift
@@ -1,5 +1,6 @@
#if os(macOS)
import AppKit
+import SwiftUI
import XCTest
@testable import DocsKit
@@ -41,5 +42,55 @@ import XCTest
try await Task.sleep(for: .milliseconds(200))
XCTAssertFalse(window.isVisible)
}
+
+ /// Delete… (Fable on #178, finding 2): the confirmation is a sheet, and a
+ /// fast server answers before it has gone. A close asked for while a
+ /// sheet is still attached must still close the window, not beep and
+ /// leave it empty. Here the dialog's own button closes at once, the
+ /// fastest a delete can come back.
+ func testCloseFromAConfirmationDialogClosesTheWindow() async throws {
+ let closer = DocsWindowCloser()
+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false)
+ window.isReleasedWhenClosed = false
+ window.contentView = NSHostingView(rootView: DialogHost(closer: closer))
+ window.orderFrontRegardless()
+ defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil); window.contentView = nil }
+ try await until("the confirmation is up") { window.attachedSheet != nil }
+ let sheet = try XCTUnwrap(window.attachedSheet)
+ let confirm = try XCTUnwrap(Self.button("Delete document", in: sheet.contentView), "the dialog's Delete document button")
+ XCTAssertTrue(closer.window === window)
+ confirm.performClick(nil)
+ try await until("the window closed") { !window.isVisible }
+ }
+
+ private struct Timeout: Error { let what: String }
+ private func until(_ what: String, _ condition: () -> Bool) async throws {
+ let deadline = Date().addingTimeInterval(5)
+ while !condition() {
+ guard Date() < deadline else { XCTFail("timed out waiting for \(what)"); throw Timeout(what: what) }
+ while let event = NSApp.nextEvent(matching: .any, until: Date(), inMode: .default, dequeue: true) { NSApp.sendEvent(event) }
+ try await Task.sleep(for: .milliseconds(10))
+ }
+ }
+
+ private static func button(_ title: String, in view: NSView?) -> NSButton? {
+ guard let view else { return nil }
+ if let button = view as? NSButton, button.title == title { return button }
+ return view.subviews.lazy.compactMap { button(title, in: $0) }.first
+ }
+}
+
+private struct DialogHost: View {
+ let closer: DocsWindowCloser
+ @State private var asking = false
+ var body: some View {
+ Color.clear.frame(width: 400, height: 300)
+ .background { DocsWindowReader(closer: closer).frame(width: 0, height: 0) }
+ .task { asking = true }
+ .confirmationDialog(Text(verbatim: "Delete?"), isPresented: $asking, titleVisibility: .visible) {
+ Button(role: .destructive) { closer.close() } label: { Text(verbatim: "Delete document") }
+ Button(role: .cancel) {} label: { Text(verbatim: "Cancel") }
+ }
+ }
}
#endif