Fix macOS window references to deinit BrowserViewModel

This commit is contained in:
Balazs Perlaki-Horvath 2025-04-04 00:12:54 +02:00
parent 5a2dd431b2
commit 70bf5cea3a

View File

@ -252,7 +252,7 @@ struct RootView: View {
NotificationCenter.openURL(url, context: .deepLink) NotificationCenter.openURL(url, context: .deepLink)
} }
} }
.onReceive(openURL) { [weak navigation] notification in .onReceive(openURL) { notification in
guard let url = notification.userInfo?["url"] as? URL else { guard let url = notification.userInfo?["url"] as? URL else {
return return
} }
@ -265,8 +265,8 @@ struct RootView: View {
// We need to filter it down the the last window // We need to filter it down the the last window
// (which is usually not the key window yet at this point), // (which is usually not the key window yet at this point),
// and load the content only within that // and load the content only within that
Task { @MainActor [weak navigation] in Task { @MainActor in
if windowTracker.isLastWindow(), let navigation { if windowTracker.isLastWindow() {
BrowserViewModel.getCached(tabID: navigation.currentTabId).load(url: url) BrowserViewModel.getCached(tabID: navigation.currentTabId).load(url: url)
} }
} }
@ -276,11 +276,10 @@ struct RootView: View {
break break
} }
guard controlActiveState == .key else { return } guard controlActiveState == .key else { return }
if let tabID = navigation?.currentTabId { let tabID = navigation.currentTabId
currentNavItem = .tab(objectID: tabID) currentNavItem = .tab(objectID: tabID)
BrowserViewModel.getCached(tabID: tabID).load(url: url) BrowserViewModel.getCached(tabID: tabID).load(url: url)
} }
}
.onReceive(tabCloses) { publisher in .onReceive(tabCloses) { publisher in
// closing one window either by CMD+W || red(X) close button // closing one window either by CMD+W || red(X) close button
guard windowTracker.current == publisher.object as? NSWindow else { guard windowTracker.current == publisher.object as? NSWindow else {
@ -288,6 +287,8 @@ struct RootView: View {
// but that's not comming from our window // but that's not comming from our window
return return
} }
windowTracker.current = nil // remove the reference to this window, see guard above
guard !navigation.isTerminating else { guard !navigation.isTerminating else {
// tab closed by app termination // tab closed by app termination
return return
@ -304,11 +305,11 @@ struct RootView: View {
} }
navigation?.keepOnlyTabsBy(tabIds: tabsToKeep) navigation?.keepOnlyTabsBy(tabIds: tabsToKeep)
} }
.onReceive(appTerminates) { [weak navigation] _ in .onReceive(appTerminates) { _ in
// CMD+Q -> Quit Kiwix, this also closes the last window // CMD+Q -> Quit Kiwix, this also closes the last window
navigation?.isTerminating = true navigation.isTerminating = true
}.onReceive(goForwardPublisher) { [weak navigation] _ in }.onReceive(goForwardPublisher) { _ in
guard case .tab(let tabID) = navigation?.currentItem else { guard case .tab(let tabID) = navigation.currentItem else {
return return
} }
BrowserViewModel.getCached(tabID: tabID).webView.goForward() BrowserViewModel.getCached(tabID: tabID).webView.goForward()
@ -340,8 +341,8 @@ struct RootView: View {
_ = MigrationService().migrateAll() _ = MigrationService().migrateAll()
} }
} }
.withHostingWindow { [windowTracker] hostWindow in .withHostingWindow { [weak windowTracker] hostWindow in
windowTracker.current = hostWindow windowTracker?.current = hostWindow
} }
} }
} }