From e9b84d92194e7dcadeaf574d698bdd0574629319 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Thu, 23 Sep 2021 20:12:35 +0800 Subject: [PATCH] Theme Import/Opening Changes Mac: - Better error messages - The alert displayed contains an additional button to open the theme's folder (when clicked it will not dismiss the alert). --- Mac/AppDelegate.swift | 36 +++++++++++++------ Shared/ArticleStyles/ArticleTheme.swift | 1 - .../ArticleThemeDownloader.swift | 1 - 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index ebf1bd30d..cfb108eed 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -107,6 +107,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, private var softwareUpdater: SPUUpdater! private var crashReporter: PLCrashReporter! #endif + + private var themeImportPath: String? override init() { NSWindow.allowsAutomaticWindowTabbing = false @@ -890,15 +892,11 @@ internal extension AppDelegate { } else { importTheme() } - } } } catch { - NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error" : error]) + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error" : error, "path": filename]) } - - - } func confirmImportSuccess(themeName: String) { @@ -921,16 +919,16 @@ internal extension AppDelegate { let window = mainWindowController?.window else { return } - + themeImportPath = userInfo["path"] as? String var informativeText: String = "" if let decodingError = error as? DecodingError { switch decodingError { case .typeMismatch(let type, _): - informativeText = "Type '\(type)' mismatch." + informativeText = "the type—'\(type)'—is mismatched." case .valueNotFound(let value, _): - informativeText = "Value '\(value)' not found." + informativeText = "the value—'\(value)'—is not found." case .keyNotFound(let codingKey, _): - informativeText = "Key '\(codingKey.stringValue)' not found." + informativeText = "the key—'\(codingKey.stringValue)'—is not found." case .dataCorrupted( _): informativeText = error.localizedDescription default: @@ -944,9 +942,25 @@ internal extension AppDelegate { let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("Theme Error", comment: "Theme download error") - alert.informativeText = NSLocalizedString("This theme cannot be imported due to the following error: \(informativeText)", comment: "Theme download error information") + alert.informativeText = NSLocalizedString("This theme cannot be used because \(informativeText)", comment: "Theme download error information") + alert.addButton(withTitle: NSLocalizedString("Open Theme Folder", comment: "Open Theme Folder")) alert.addButton(withTitle: NSLocalizedString("OK", comment: "OK")) - alert.beginSheetModal(for: window) + + let button = alert.buttons.first + button?.target = self + button?.action = #selector(self.openThemesFolder(_:)) + + alert.runModal() + } + } + + @objc func openThemesFolder(_ sender: Any) { + if themeImportPath == nil { + let url = URL(fileURLWithPath: ArticleThemesManager.shared.folderPath) + NSWorkspace.shared.open(url) + } else { + let url = URL(fileURLWithPath: themeImportPath!) + NSWorkspace.shared.open(url.deletingLastPathComponent()) } } diff --git a/Shared/ArticleStyles/ArticleTheme.swift b/Shared/ArticleStyles/ArticleTheme.swift index dc03e816f..e40bde46f 100644 --- a/Shared/ArticleStyles/ArticleTheme.swift +++ b/Shared/ArticleStyles/ArticleTheme.swift @@ -58,7 +58,6 @@ struct ArticleTheme: Equatable { let data = try Data(contentsOf: URL(fileURLWithPath: infoPath)) self.info = try PropertyListDecoder().decode(ArticleThemePlist.self, from: data) - let corePath = Bundle.main.path(forResource: "core", ofType: "css")! let stylesheetPath = (path as NSString).appendingPathComponent("stylesheet.css") if let stylesheetCSS = Self.stringAtPath(stylesheetPath) { diff --git a/Shared/ArticleStyles/ArticleThemeDownloader.swift b/Shared/ArticleStyles/ArticleThemeDownloader.swift index 0e94437ee..7c3e5534c 100644 --- a/Shared/ArticleStyles/ArticleThemeDownloader.swift +++ b/Shared/ArticleStyles/ArticleThemeDownloader.swift @@ -22,7 +22,6 @@ public class ArticleThemeDownloader { } } - public static let shared = ArticleThemeDownloader() private init() {}