Revert to a single solution, no settings

This commit is contained in:
Balazs Perlaki-Horvath 2025-05-02 14:15:13 +02:00 committed by Kelson
parent 27b7e464e5
commit c8f1943559
8 changed files with 38 additions and 104 deletions

View File

@ -167,21 +167,6 @@ struct Kiwix: App {
window.identifier?.rawValue == "donation"
}?.close()
}
private class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
/// Handling file download complete notification
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
Task {
if let zimFileID = UUID(uuidString: response.notification.request.identifier),
let mainPageURL = await ZimFileService.shared.getMainPageURL(zimFileID: zimFileID) {
NSWorkspace.shared.open(mainPageURL)
}
await MainActor.run { completionHandler() }
}
}
}
}
struct RootView: View {
@ -196,7 +181,6 @@ struct RootView: View {
// Open file alerts
@State private var isOpenFileAlertPresented = false
@State private var openFileAlert: OpenFileAlert?
@Default(.externalEventTabOpenPolicy) private var externalEventTabOpenPolicy
private let primaryItems: [MenuItem] = [.bookmarks]
private let libraryItems: [MenuItem] = [.opened, .categories, .downloads, .new]
@ -207,6 +191,8 @@ struct RootView: View {
private let goForwardPublisher = NotificationCenter.default.publisher(for: .goForward)
/// Close other tabs then the ones received
private let keepOnlyTabs = NotificationCenter.default.publisher(for: .keepOnlyTabs)
// in essence it's the "zim://" value
private static let zimURL: String = "\(KiwixURLSchemeHandler.ZIMScheme)://"
var body: some View {
NavigationSplitView {
@ -274,21 +260,23 @@ struct RootView: View {
}
}
.onOpenURL { url in
/// if the app was just started via URL or file (wasn't open before)
/// we want to load the content in the first window
/// otherwise in a new tab (but within the currently active window)
let isAppStart = NSApplication.shared.windows.count == 1
if url.isFileURL {
// from opening an external file
let browser = BrowserViewModel.getCached(tabID: navigation.currentTabId)
if externalEventTabOpenPolicy == .openInNewDetachedWindow {
browser.forceLoadingState() // we are already in the new window, show loading immediately
if isAppStart {
browser.forceLoadingState()
}
Task { // open the ZIM file
if let metadata = await LibraryOperations.open(url: url),
let mainPageURL = await ZimFileService.shared.getMainPageURL(zimFileID: metadata.fileID) {
switch externalEventTabOpenPolicy {
case .openInNewTabSameWindow:
browser.createNewWindow(with: mainPageURL)
case .openInNewDetachedWindow:
if isAppStart {
browser.load(url: mainPageURL)
} else {
browser.createNewWindow(with: mainPageURL)
}
} else {
await browser.clear()
@ -298,8 +286,10 @@ struct RootView: View {
}
} else if url.isZIMURL {
// from deeplinks
switch externalEventTabOpenPolicy {
case .openInNewTabSameWindow:
if isAppStart {
let browser = BrowserViewModel.getCached(tabID: navigation.currentTabId)
browser.load(url: url)
} else {
Task { @MainActor in
// we want to open the deeplink a new tab (in the currently active window)
// at this point though, the latest tab is active, that received the deeplink handling
@ -309,11 +299,6 @@ struct RootView: View {
let browser = BrowserViewModel.getCached(tabID: navigation.currentTabId)
browser.createNewWindow(with: url)
}
case .openInNewDetachedWindow:
// in this case the system created a new detached window / single tab
// that is currently active
let browser = BrowserViewModel.getCached(tabID: navigation.currentTabId)
browser.load(url: url)
}
}
}
@ -404,7 +389,10 @@ struct RootView: View {
.withHostingWindow { [weak windowTracker] hostWindow in
windowTracker?.current = hostWindow
}
.modifier(ExternalEventHandler(openInSameWindow: externalEventTabOpenPolicy == .openInNewTabSameWindow))
.handlesExternalEvents(
preferring: Set([Self.zimURL]),
allowing: Set([Self.zimURL, "file:///"])
)
}
}

View File

@ -13,4 +13,23 @@
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
#if os(macOS)
import Foundation
import UserNotifications
import AppKit
final class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
/// Handling file download complete notification
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
Task {
if let zimFileID = UUID(uuidString: response.notification.request.identifier),
let mainPageURL = await ZimFileService.shared.getMainPageURL(zimFileID: zimFileID) {
NSWorkspace.shared.open(mainPageURL)
}
await MainActor.run { completionHandler() }
}
}
}
#endif

View File

@ -159,7 +159,6 @@
"reading_settings.zoom.title" = "Page zoom";
"reading_settings.zoom.reset.button" = "Reset";
"reading_settings.external_link.title" = "External Link";
"reading_settings.external_events_tab_opening.title" = "URLs and files should";
"reading_settings.search_snippet.title" = "Search snippet";
"reading_settings.tab.reading" = "Reading";
"library_settings.catalog.title" = "Catalog";
@ -256,8 +255,6 @@
"enum.external_link_loading_policy.always_ask" = "Always Ask";
"enum.external_link_loading_policy.always_load" = "Always Load";
"enum.external_link_loading_policy.never_load" = "Never Load";
"enum.external_event.open_in_new_tab_same_window" = "Open in a new tab in the active window";
"enum.external_event.open_new_detached_window" = "Open in a new detached window";
"enum.flavor.max" = "max";
"enum.flavor.no_pic" = "no pic";
"enum.flavor.mini" = "mini";

View File

@ -124,7 +124,6 @@
"reading_settings.zoom.title" = "Label title for a percentage selector in app settings, eg: 100%";
"reading_settings.zoom.reset.button" = "Button title for percentage selector for zoom level in app settings";
"reading_settings.external_link.title" = "Label title for option picker. External Link: Always Ask | Always Load | Never Load";
"reading_settings.external_events_tab_opening.title" = "Label title for option picker. URLs and files should: Open in a new tab in the active window | Open in a new detached window";
"reading_settings.search_snippet.title" = "Label title for option picker. Search snippet: Disabled | First Paragraph | First Sentence | Matches";
"reading_settings.tab.reading" = "Settings page tab title";
"library_settings.catalog.title" = "In settings: Label next to button Catalog: [Refresh] Last Refresh: 30 minutes ago";
@ -212,8 +211,6 @@
"enum.external_link_loading_policy.always_ask" = "Settings picker option title: how external links should be treated";
"enum.external_link_loading_policy.always_load" = "Settings picker option title: how external links should be treated";
"enum.external_link_loading_policy.never_load" = "Settings picker option title: how external links should be treated";
"enum.external_event.open_in_new_tab_same_window" = "Settings picker option title: how to handle zim urls, and files opened in Finder: they should be opened in a new tab attached to the currently active window";
"enum.external_event.open_new_detached_window" = "Settings picker option title: how to handle zim urls, and files opened in Finder: they should be opened in a new detached window on top of the currently running application window";
"enum.flavor.max" = "short, abreviated label displayed in the corner of zim file cards, describing the flavour of the file. The shorter the better.";
"enum.flavor.no_pic" = "short, abreviated label displayed in the corner of zim file cards, describing the flavour of the file. The shorter the better.";
"enum.flavor.mini" = "short, abreviated label displayed in the corner of zim file cards, describing the flavour of the file. The shorter the better.";

View File

@ -23,9 +23,6 @@ extension Defaults.Keys {
static let externalLinkLoadingPolicy = Key<ExternalLinkLoadingPolicy>(
"externalLinkLoadingPolicy", default: Brand.defaultExternalLinkPolicy
)
static let externalEventTabOpenPolicy = Key<ExternalEventOpeningPolicy>(
"ExternalEventOpeningPolicy", default: .openInNewTabSameWindow
)
static let searchResultSnippetMode = Key<SearchResultSnippetMode>(
"searchResultSnippetMode", default: Brand.defaultSearchSnippetMode
)

View File

@ -138,21 +138,6 @@ enum ExternalLinkLoadingPolicy: String, CaseIterable, Identifiable, Defaults.Ser
}
}
enum ExternalEventOpeningPolicy: String, CaseIterable, Identifiable, Defaults.Serializable {
var id: String { self.rawValue }
case openInNewTabSameWindow
case openInNewDetachedWindow
var name: String {
switch self {
case .openInNewTabSameWindow:
return LocalString.enum_external_event_open_in_new_tab_same_window
case .openInNewDetachedWindow:
return LocalString.enum_external_event_open_new_detached_window
}
}
}
enum OpenURLContext {
case deepLink(id: UUID?)
}

View File

@ -22,7 +22,6 @@ struct ReadingSettings: View {
@Default(.externalLinkLoadingPolicy) private var externalLinkLoadingPolicy
@Default(.searchResultSnippetMode) private var searchResultSnippetMode
@Default(.webViewPageZoom) private var webViewPageZoom
@Default(.externalEventTabOpenPolicy) private var externalEventTabOpenPolicy
var body: some View {
let isSnippet = Binding {
@ -52,16 +51,6 @@ struct ReadingSettings: View {
} label: { }
}
}
if !AppType.isCustom {
SettingSection(name: LocalString.reading_settings_external_events_tab_opening_title) {
Picker(selection: $externalEventTabOpenPolicy) {
ForEach(ExternalEventOpeningPolicy.allCases) { policy in
Text(policy.name).tag(policy)
}
} label: { }
}
}
if FeatureFlags.showSearchSnippetInSettings {
SettingSection(name: LocalString.reading_settings_search_snippet_title) {
Toggle(" ", isOn: isSnippet)

View File

@ -1,38 +0,0 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
#if os(macOS)
import SwiftUI
struct ExternalEventHandler: ViewModifier {
let openInSameWindow: Bool
// in essence it's the "zim://" value
static private let zimURL: String = "\(KiwixURLSchemeHandler.ZIMScheme)://"
func body(content: Content) -> some View {
if openInSameWindow {
content.handlesExternalEvents(
preferring: Set([Self.zimURL]),
allowing: Set([Self.zimURL, "file:///"])
)
} else {
content
}
}
}
#endif