From d56cd8ebe8c64ce337f736168550146d670eaa83 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sat, 7 Sep 2024 12:09:49 +0200 Subject: [PATCH 1/3] Remove macOS semi-working window restoration --- App/App_macOS.swift | 16 +++---- ViewModel/BrowserViewModel.swift | 75 +------------------------------- 2 files changed, 7 insertions(+), 84 deletions(-) diff --git a/App/App_macOS.swift b/App/App_macOS.swift index 3660adbac..1f21f82ef 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -136,13 +136,8 @@ struct RootView: View { case .reading: BrowserTab().environmentObject(browser) .withHostingWindow { window in - if let windowNumber = window?.windowNumber { - browser.restoreByWindowNumber(windowNumber: windowNumber, - urlToTabIdConverter: navigation.tabIDFor(url:)) - } else { - if FeatureFlags.hasLibrary == false { - browser.loadMainArticle() - } + if FeatureFlags.hasLibrary == false { + browser.loadMainArticle() } } case .bookmarks: @@ -169,7 +164,7 @@ struct RootView: View { if url.isFileURL { NotificationCenter.openFiles([url], context: .file) } else if url.isKiwixURL { - NotificationCenter.openURL(url) + NotificationCenter.openURL(url, inNewTab: true) } } .onReceive(openURL) { notification in @@ -193,7 +188,8 @@ struct RootView: View { navigation.currentItem = .reading browser.load(url: url) } - .onReceive(tabCloses) { publisher in + .onReceive(tabCloses) { publisher in + // closing one window either by CMD+W || red(X) close button guard windowTracker.current == publisher.object as? NSWindow else { // when exiting full screen video, we get the same notification // but that's not comming from our window @@ -210,8 +206,8 @@ struct RootView: View { } } .onReceive(appTerminates) { _ in + // CMD+Q -> Quit Kiwix, this also closes the last window navigation.isTerminating = true - browser.persistAllTabIdsFromWindows() }.task { switch AppType.current { case .kiwix: diff --git a/ViewModel/BrowserViewModel.swift b/ViewModel/BrowserViewModel.swift index a694359ef..ba69dd2db 100644 --- a/ViewModel/BrowserViewModel.swift +++ b/ViewModel/BrowserViewModel.swift @@ -70,15 +70,7 @@ final class BrowserViewModel: NSObject, ObservableObject, @Published var externalURL: URL? private var metaData: URLContentMetaData? - private(set) var tabID: NSManagedObjectID? { - didSet { -#if os(macOS) - if let tabID, tabID != oldValue { - storeTabIDInCurrentWindow() - } -#endif - } - } + private(set) var tabID: NSManagedObjectID? #if os(macOS) private var windowURLs: [URL] { UserDefaults.standard[.windowURLs] @@ -554,71 +546,6 @@ final class BrowserViewModel: NSObject, ObservableObject, } #endif - // MARK: - TabID management via NSWindow for macOS - -#if os(macOS) - private (set) var windowNumber: Int? - - // RESTORATION - func restoreByWindowNumber( - windowNumber currentNumber: Int, - urlToTabIdConverter: @MainActor @escaping (URL?) -> NSManagedObjectID - ) { - windowNumber = currentNumber - let windows = NSApplication.shared.windows - let tabURL: URL? - - guard let currentWindow = windowBy(number: currentNumber), - let index = windows.firstIndex(of: currentWindow) else { return } - - // find the url for this window in user defaults, by pure index - if 0 <= index, - index < windowURLs.count { - tabURL = windowURLs[index] - } else { - tabURL = nil - } - Task { - await MainActor.run { - let tabID = urlToTabIdConverter(tabURL) // if url is nil it will create a new tab - self.tabID = tabID - restoreBy(tabID: tabID) - } - } - } - - private func indexOf(windowNumber number: Int, in windows: [NSWindow]) -> Int? { - let windowNumbers = windows.map { $0.windowNumber } - guard windowNumbers.contains(number), - let index = windowNumbers.firstIndex(of: number) else { - return nil - } - return index - } - - // PERSISTENCE: - func persistAllTabIdsFromWindows() { - let urls = NSApplication.shared.windows.compactMap { window in - window.accessibilityURL() - } - UserDefaults.standard[.windowURLs] = urls - } - - private func storeTabIDInCurrentWindow() { - guard let tabID, - let windowNumber, - let currentWindow = windowBy(number: windowNumber) else { - return - } - let url = tabID.uriRepresentation() - currentWindow.setAccessibilityURL(url) - } - - private func windowBy(number: Int) -> NSWindow? { - NSApplication.shared.windows.first { $0.windowNumber == number } - } -#endif - private func createNewTabID() -> NSManagedObjectID { if let tabID { return tabID } let context = Database.shared.viewContext From c576f2b7b072030c7f7a310303c9f6728fa23b6f Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sat, 7 Sep 2024 14:47:20 +0200 Subject: [PATCH 2/3] Remove window persistence --- App/App_macOS.swift | 13 ++++-- ViewModel/BrowserViewModel.swift | 68 +++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/App/App_macOS.swift b/App/App_macOS.swift index 1f21f82ef..1fa86f713 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -136,8 +136,13 @@ struct RootView: View { case .reading: BrowserTab().environmentObject(browser) .withHostingWindow { window in - if FeatureFlags.hasLibrary == false { - browser.loadMainArticle() + if let windowNumber = window?.windowNumber { + browser.restoreByWindowNumber(windowNumber: windowNumber, + urlToTabIdConverter: navigation.tabIDFor(url:)) + } else { + if FeatureFlags.hasLibrary == false { + browser.loadMainArticle() + } } } case .bookmarks: @@ -164,7 +169,7 @@ struct RootView: View { if url.isFileURL { NotificationCenter.openFiles([url], context: .file) } else if url.isKiwixURL { - NotificationCenter.openURL(url, inNewTab: true) + NotificationCenter.openURL(url) } } .onReceive(openURL) { notification in @@ -188,7 +193,7 @@ struct RootView: View { navigation.currentItem = .reading browser.load(url: url) } - .onReceive(tabCloses) { publisher in + .onReceive(tabCloses) { publisher in // closing one window either by CMD+W || red(X) close button guard windowTracker.current == publisher.object as? NSWindow else { // when exiting full screen video, we get the same notification diff --git a/ViewModel/BrowserViewModel.swift b/ViewModel/BrowserViewModel.swift index ba69dd2db..387462806 100644 --- a/ViewModel/BrowserViewModel.swift +++ b/ViewModel/BrowserViewModel.swift @@ -70,7 +70,15 @@ final class BrowserViewModel: NSObject, ObservableObject, @Published var externalURL: URL? private var metaData: URLContentMetaData? - private(set) var tabID: NSManagedObjectID? + private(set) var tabID: NSManagedObjectID? { + didSet { +#if os(macOS) + if let tabID, tabID != oldValue { + storeTabIDInCurrentWindow() + } +#endif + } + } #if os(macOS) private var windowURLs: [URL] { UserDefaults.standard[.windowURLs] @@ -546,6 +554,64 @@ final class BrowserViewModel: NSObject, ObservableObject, } #endif + // MARK: - TabID management via NSWindow for macOS + +#if os(macOS) + private (set) var windowNumber: Int? + + // RESTORATION + func restoreByWindowNumber( + windowNumber currentNumber: Int, + urlToTabIdConverter: @MainActor @escaping (URL?) -> NSManagedObjectID + ) { + windowNumber = currentNumber + let windows = NSApplication.shared.windows + let tabURL: URL? + + guard let currentWindow = windowBy(number: currentNumber), + let index = windows.firstIndex(of: currentWindow) else { return } + + // find the url for this window in user defaults, by pure index + if 0 <= index, + index < windowURLs.count { + tabURL = windowURLs[index] + } else { + tabURL = nil + } + Task { + await MainActor.run { + let tabID = urlToTabIdConverter(tabURL) // if url is nil it will create a new tab + self.tabID = tabID + restoreBy(tabID: tabID) + } + } + } + + private func indexOf(windowNumber number: Int, in windows: [NSWindow]) -> Int? { + let windowNumbers = windows.map { $0.windowNumber } + guard windowNumbers.contains(number), + let index = windowNumbers.firstIndex(of: number) else { + return nil + } + return index + } + + // PERSISTENCE: + private func storeTabIDInCurrentWindow() { + guard let tabID, + let windowNumber, + let currentWindow = windowBy(number: windowNumber) else { + return + } + let url = tabID.uriRepresentation() + currentWindow.setAccessibilityURL(url) + } + + private func windowBy(number: Int) -> NSWindow? { + NSApplication.shared.windows.first { $0.windowNumber == number } + } +#endif + private func createNewTabID() -> NSManagedObjectID { if let tabID { return tabID } let context = Database.shared.viewContext From b13e4662b8feb03b4e8bdefcd7fa584a5553aada Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sat, 7 Sep 2024 15:33:01 +0200 Subject: [PATCH 3/3] Include change in CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef23a527e..dbad08efb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 3.5.2 - +- FIX: + - Opening ZIM file from macOS Finder (@BPerlakiH #968) ## 3.5.1 - FIX: