From 0c2d0578601252a6f91539a2a9eed0a68762859d Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Mon, 18 May 2020 20:35:06 -0500 Subject: [PATCH] Add defaultBrowserID user default and make the appropriate toolbar item use that browser's icon --- Mac/AppDefaults.swift | 10 +++++++ Mac/MainWindow/MainWindowController.swift | 32 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Mac/AppDefaults.swift b/Mac/AppDefaults.swift index f7e362c68..447d5b4cf 100644 --- a/Mac/AppDefaults.swift +++ b/Mac/AppDefaults.swift @@ -33,6 +33,7 @@ struct AppDefaults { static let addFolderAccountID = "addFolderAccountID" static let importOPMLAccountID = "importOPMLAccountID" static let exportOPMLAccountID = "exportOPMLAccountID" + static let defaultBrowserID = "defaultBrowserID" // Hidden prefs static let showDebugMenu = "ShowDebugMenu" @@ -171,6 +172,15 @@ struct AppDefaults { setString(for: Key.exportOPMLAccountID, newValue) } } + + static var defaultBrowserID: String? { + get { + return string(for: Key.defaultBrowserID) + } + set { + setString(for: Key.defaultBrowserID, newValue) + } + } static var showTitleOnMainWindow: Bool { return bool(for: Key.showTitleOnMainWindow) diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 7b8012a47..9cd604931 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -93,6 +93,8 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { DispatchQueue.main.async { self.updateWindowTitle() } + + defaultBrowserDidChange(nil) } // MARK: - API @@ -167,6 +169,36 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { } } + + @objc func defaultBrowserDidChange(_ note: Notification?) { + guard let item = self.window?.toolbar?.items.first(where: { $0.action == #selector(openArticleInBrowser(_:)) }) else { + return + } + + guard let button = item.view as? NSButton else { return } + + var browserName = NSLocalizedString("Browser", comment: "Browser") + var icon = NSImage(named: "openInBrowser")! + + var currentBrowser: MacWebBrowser? + + if let browserID = AppDefaults.defaultBrowserID { + currentBrowser = MacWebBrowser(bundleIdentifier: browserID) + } else { + currentBrowser = MacWebBrowser.default + } + + if let currentBrowser = currentBrowser { + icon = currentBrowser.icon! + icon.size = NSSize(width: 16.0, height: 16.0) + browserName = currentBrowser.name! + } + + button.image = icon + + let format = NSLocalizedString("Open in $@", comment: "Open in Browser toolbar item tooltip format") + item.toolTip = String(format: format, browserName) + } // MARK: - Toolbar