From 7cc06ae6100566f193755c4a709474dc3cddf10f Mon Sep 17 00:00:00 2001 From: Jamie McDonald Date: Tue, 5 Dec 2023 17:20:47 -0800 Subject: [PATCH] Fix accessory mode behaviour --- .swift-version | 2 +- macOS/Arrivals/ArrivalsApp.swift | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.swift-version b/.swift-version index b64ab70..95ee81a 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -5.7.2 +5.9 diff --git a/macOS/Arrivals/ArrivalsApp.swift b/macOS/Arrivals/ArrivalsApp.swift index d707b78..64a6c65 100644 --- a/macOS/Arrivals/ArrivalsApp.swift +++ b/macOS/Arrivals/ArrivalsApp.swift @@ -3,20 +3,38 @@ import SwiftUI @main struct ArrivalsApp: App { @NSApplicationDelegateAdaptor(AppDelegate.self) var delegate + @State private var settingsWindow: NSWindow? var body: some Scene { Settings { SettingsView() - .onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { newValue in - // TODO: Not this - if (!newValue.description.contains("TUINSWindow")) { - NSApplication.accessoryMode() + .background(WindowAccessor(window: $settingsWindow)) + .onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { notif in + if let window = notif.object as? NSWindow { + print("Window \(window.windowNumber) is closing. Settings is \(settingsWindow?.windowNumber ?? -1).") + if window.windowNumber == settingsWindow?.windowNumber { + NSApplication.accessoryMode() + } } } } } } +struct WindowAccessor: NSViewRepresentable { + @Binding var window: NSWindow? + + func makeNSView(context _: Context) -> NSView { + let view = NSView() + DispatchQueue.main.async { + self.window = view.window + } + return view + } + + func updateNSView(_: NSView, context _: Context) {} +} + class PopoverState: ObservableObject { @Published var isShown = false }