Detect closing of tab (NSWindowTab) in WindowGroup

I have a SwiftUI app displaying tabbed windows (as NSWindowTab) in a WindowGroup:

import SwiftUI

@main
struct TabTestApp: App {
    var body: some Scene {
        WindowGroup{
            ContentView() // Hasn't any content of relevance to this question.
        }.commands{
            CommandGroup(after: .newItem) {
                Button("New Tab") {
                    guard let currentWindow = NSApp.keyWindow, let windowController = currentWindow.windowController else { return }
                    windowController.newWindowForTab(nil)
                    guard let newWindow = NSApp.keyWindow else { return }
                    if currentWindow != newWindow {
                        currentWindow.addTabbedWindow(newWindow, ordered: .above)
                    }
                }.keyboardShortcut(.init("t", modifiers: [.command]))
            }
        }
    }
}

Is there a way to detect the closing of one or multiple tabs, e.g. when the user clicks on the tab bar's "Close Other Tabs" option or pushes CMD + W in order to ask the user whether he or she wants to save changes?

What I've tried to no avail:

  • Intercept windowWillClose👉Won't be called if a single tab within a window is closed (but only once the last tab of a window is closed).
  • Handling onDissapear()👉Doesn't work since the closing cannot be cancelled.
  • Using DocumentGroup 👉Doesn't work since the app in question isn't about documents (i.e., files which are stored externally), but about data that's stored in a database.

Many thanks!

Related threads:

Detect closing of tab (NSWindowTab) in WindowGroup
 
 
Q