| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | // Native windows must never silently discard a focused or failed edit. |
| 5 | struct SheetCloseGuard: NSViewRepresentable { |
| 6 | let model: SheetModel |
| 7 | func makeNSView(context: Context) -> CloseGuardView { |
| 8 | let view = CloseGuardView() |
| 9 | view.model = model |
| 10 | return view |
| 11 | } |
| 12 | func updateNSView(_ view: CloseGuardView, context: Context) { view.model = model } |
| 13 | } |
| 14 | @MainActor final class CloseGuardView: NSView, NSWindowDelegate { |
| 15 | weak var model: SheetModel? |
| 16 | // AppKit delegate messaging and mutation occur on the window’s main thread. |
| 17 | nonisolated(unsafe) weak var previousDelegate: (any NSWindowDelegate)? |
| 18 | override func viewDidMoveToWindow() { |
| 19 | super.viewDidMoveToWindow() |
| 20 | guard let window, window.delegate !== self else { return } |
| 21 | SheetCloseRegistry.views.removeAll { $0.value == nil } |
| 22 | if !SheetCloseRegistry.views.contains(where: { $0.value === self }) { |
| 23 | SheetCloseRegistry.views.append(WeakCloseGuard(value: self)) |
| 24 | } |
| 25 | previousDelegate = window.delegate |
| 26 | window.delegate = self |
| 27 | } |
| 28 | override func responds(to selector: Selector!) -> Bool { |
| 29 | super.responds(to: selector) || previousDelegate?.responds(to: selector) == true |
| 30 | } |
| 31 | override func forwardingTarget(for selector: Selector!) -> Any? { previousDelegate } |
| 32 | func windowShouldClose(_ sender: NSWindow) -> Bool { |
| 33 | guard let model else { return previousDelegate?.windowShouldClose?(sender) ?? true } |
| 34 | if model.busy { |
| 35 | model.error = "Wait for the current save to finish before closing this window." |
| 36 | return false |
| 37 | } |
| 38 | guard model.hasUnconfirmedChanges else { |
| 39 | return previousDelegate?.windowShouldClose?(sender) ?? true |
| 40 | } |
| 41 | if !model.dirty && !model.hasFailedEdits { |
| 42 | // Only edits on their way: let them land, then close; any that did |
| 43 | // not (an ended session is not waited out) are asked about now. |
| 44 | Task { @MainActor in |
| 45 | await model.settle(hurry: true) |
| 46 | if !model.hasUnconfirmedChanges || model.hasFailedEdits { sender.performClose(nil) } |
| 47 | } |
| 48 | return false |
| 49 | } |
| 50 | let alert = NSAlert() |
| 51 | alert.messageText = "Keep your cell edit?" |
| 52 | alert.informativeText = "This cell has changes that have not been saved." |
| 53 | alert.addButton(withTitle: "Save") |
| 54 | alert.addButton(withTitle: "Discard") |
| 55 | alert.addButton(withTitle: "Cancel") |
| 56 | alert.beginSheetModal(for: sender) { response in |
| 57 | Task { @MainActor in |
| 58 | if response == .alertFirstButtonReturn { |
| 59 | await model.saveAll(hurry: true) |
| 60 | if !model.hasUnconfirmedChanges { sender.performClose(nil) } |
| 61 | } else if response == .alertSecondButtonReturn { |
| 62 | model.discardAll() |
| 63 | await model.settle(hurry: true) |
| 64 | model.revertWrites() |
| 65 | sender.performClose(nil) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return false |
| 70 | } |
| 71 | } |
| 72 | @MainActor struct WeakCloseGuard { weak var value: CloseGuardView? } |
| 73 | @MainActor enum SheetCloseRegistry { static var views: [WeakCloseGuard] = [] } |
| 74 | @MainActor public final class SheetsApplicationDelegate: NSObject, NSApplicationDelegate { |
| 75 | public func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply |
| 76 | { |
| 77 | let models = SheetCloseRegistry.views.compactMap { $0.value?.model } |
| 78 | if models.contains(where: { $0.busy }) { return .terminateCancel } |
| 79 | let pending = models.filter { $0.hasUnconfirmedChanges } |
| 80 | if pending.isEmpty { return .terminateNow } |
| 81 | if pending.allSatisfy({ !$0.dirty && !$0.hasFailedEdits }) { |
| 82 | // Only edits on their way: quit once they land. Any that did not (an |
| 83 | // ended session is not waited out) are asked about at once. |
| 84 | Task { @MainActor in |
| 85 | for model in pending { await model.settle(hurry: true) } |
| 86 | let clean = !pending.contains { $0.hasUnconfirmedChanges } |
| 87 | sender.reply(toApplicationShouldTerminate: clean) |
| 88 | if !clean { DispatchQueue.main.async { NSApp.terminate(nil) } } |
| 89 | } |
| 90 | return .terminateLater |
| 91 | } |
| 92 | let alert = NSAlert() |
| 93 | alert.messageText = "Save your spreadsheet edits before quitting?" |
| 94 | alert.addButton(withTitle: "Save") |
| 95 | alert.addButton(withTitle: "Discard") |
| 96 | alert.addButton(withTitle: "Cancel") |
| 97 | switch alert.runModal() { |
| 98 | case .alertFirstButtonReturn: |
| 99 | Task { @MainActor in |
| 100 | for model in pending { await model.saveAll(hurry: true) } |
| 101 | sender.reply(toApplicationShouldTerminate: !pending.contains { $0.hasUnconfirmedChanges }) |
| 102 | } |
| 103 | return .terminateLater |
| 104 | case .alertSecondButtonReturn: |
| 105 | pending.forEach { $0.discardAll() } |
| 106 | Task { @MainActor in |
| 107 | for model in pending { await model.settle(hurry: true) } |
| 108 | sender.reply(toApplicationShouldTerminate: true) |
| 109 | } |
| 110 | return .terminateLater |
| 111 | default: return .terminateCancel |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | #endif |
| 116 | |