From 9e3b6597107fec353ce8267730d27f5cdadcec01 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 30 Jan 2025 21:45:17 -0800 Subject: [PATCH] Create a private shared property for use in window restoration. --- Mac/AppDelegate.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index f4984280b..a6395a42f 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -19,7 +19,7 @@ import OSLog import CrashReporter import Sparkle -var appDelegate: AppDelegate! +//var appDelegate: AppDelegate! @NSApplicationMain final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, UNUserNotificationCenterDelegate, UnreadCountProvider, SPUStandardUserDriverDelegate, SPUUpdaterDelegate { @@ -95,6 +95,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat private var themeImportPath: String? + private static var shared: AppDelegate? + override init() { NSWindow.allowsAutomaticWindowTabbing = false super.init() @@ -113,7 +115,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat NotificationCenter.default.addObserver(self, selector: #selector(mainWindowWillClose(_:)), name: .mainWindowControllerWillClose, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(didWakeNotification(_:)), name: NSWorkspace.didWakeNotification, object: nil) - appDelegate = self + Self.shared = self } // MARK: - API @@ -905,11 +907,14 @@ extension AppDelegate: NSWindowRestoration { @objc static func restoreWindow(withIdentifier identifier: NSUserInterfaceItemIdentifier, state: NSCoder, completionHandler: @escaping (NSWindow?, Error?) -> Void) { var mainWindow: NSWindow? if identifier.rawValue == WindowRestorationIdentifiers.mainWindow { - mainWindow = appDelegate.createAndShowMainWindow().window + if let shared { + mainWindow = shared.createAndShowMainWindow().window + } else { + assertionFailure("Expected shared instance of AppDelegate to exist during window restoration.") + } } completionHandler(mainWindow, nil) } - } // Handle Notification Actions