From 1ab17b257425c4d92355c2d106fffd20c94c0657 Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Mon, 18 May 2020 21:29:53 -0500 Subject: [PATCH] Add a preference for the browser to use to open articles - Selectable in General Preferences - Toolbar item now updates to show the browser's icon --- Mac/Browser.swift | 16 ++++++- Mac/MainWindow/MainWindowController.swift | 13 ++++-- .../GeneralPrefencesViewController.swift | 44 +++++++++++++++++++ Shared/AppNotifications.swift | 1 + 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/Mac/Browser.swift b/Mac/Browser.swift index b36a27ea0..12478e136 100644 --- a/Mac/Browser.swift +++ b/Mac/Browser.swift @@ -11,14 +11,26 @@ import RSWeb struct Browser { - static func open(_ urlString: String, invertPreference invert: Bool = false) { + static var defaultBrowser: MacWebBrowser? { + if let bundleID = AppDefaults.defaultBrowserID, let browser = MacWebBrowser(bundleIdentifier: bundleID) { + return browser + } + + return nil + } + + static func open(_ urlString: String) { // Opens according to prefs. open(urlString, inBackground: invert ? !AppDefaults.openInBrowserInBackground : AppDefaults.openInBrowserInBackground) } static func open(_ urlString: String, inBackground: Bool) { if let url = URL(string: urlString) { - MacWebBrowser.openURL(url, inBackground: inBackground) + if let defaultBrowser = defaultBrowser { + defaultBrowser.openURL(url, inBackground: inBackground) + } else { + MacWebBrowser.openURL(url, inBackground: inBackground) + } } } } diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 9cd604931..86f17178e 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -90,11 +90,14 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(defaultBrowserDidChange(_:)), name: .DefaultBrowserDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(defaultBrowserDidChange(_:)), name: NSApplication.willBecomeActiveNotification, object: nil) + DispatchQueue.main.async { self.updateWindowTitle() + self.updateOpenInBrowserToolbarItem() } - defaultBrowserDidChange(nil) } // MARK: - API @@ -170,7 +173,7 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { } - @objc func defaultBrowserDidChange(_ note: Notification?) { + func updateOpenInBrowserToolbarItem() { guard let item = self.window?.toolbar?.items.first(where: { $0.action == #selector(openArticleInBrowser(_:)) }) else { return } @@ -196,9 +199,13 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { button.image = icon - let format = NSLocalizedString("Open in $@", comment: "Open in Browser toolbar item tooltip format") + let format = NSLocalizedString("Open in %@", comment: "Open in Browser toolbar item tooltip format") item.toolTip = String(format: format, browserName) } + + @objc func defaultBrowserDidChange(_ note: Notification?) { + updateOpenInBrowserToolbarItem() + } // MARK: - Toolbar diff --git a/Mac/Preferences/General/GeneralPrefencesViewController.swift b/Mac/Preferences/General/GeneralPrefencesViewController.swift index 57996a485..5541816a6 100644 --- a/Mac/Preferences/General/GeneralPrefencesViewController.swift +++ b/Mac/Preferences/General/GeneralPrefencesViewController.swift @@ -8,10 +8,12 @@ import AppKit import RSCore +import RSWeb final class GeneralPreferencesViewController: NSViewController { @IBOutlet var defaultRSSReaderPopup: NSPopUpButton! + @IBOutlet var defaultBrowserPopup: NSPopUpButton! private var rssReaderInfo = RSSReaderInfo() public override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) { @@ -47,6 +49,16 @@ final class GeneralPreferencesViewController: NSViewController { registerAppWithBundleID(bundleID) updateUI() } + + @IBAction func browserPopUpDidChangeValue(_ sender: Any?) { + guard let menuItem = defaultBrowserPopup.selectedItem else { + return + } + let bundleID = menuItem.representedObject as? String + AppDefaults.defaultBrowserID = bundleID + NotificationCenter.default.post(name: .DefaultBrowserDidChange, object: nil) + updateUI() + } } // MARK: - Private @@ -60,6 +72,7 @@ private extension GeneralPreferencesViewController { func updateUI() { rssReaderInfo = RSSReaderInfo() updateRSSReaderPopup() + updateBrowserPopup() } func updateRSSReaderPopup() { @@ -123,6 +136,37 @@ private extension GeneralPreferencesViewController { NSWorkspace.shared.setDefaultAppBundleID(forURLScheme: "feed", to: bundleID) NSWorkspace.shared.setDefaultAppBundleID(forURLScheme: "feeds", to: bundleID) } + + func updateBrowserPopup() { + let menu = defaultBrowserPopup.menu! + let allBrowsers = MacWebBrowser.sortedBrowsers() + + menu.removeAllItems() + + let defaultBrowser = MacWebBrowser.default + + let defaultBrowserFormat = NSLocalizedString("System Default (%@)", comment: "Default browser item title format") + let defaultBrowserTitle = String(format: defaultBrowserFormat, defaultBrowser.name!) + let item = NSMenuItem(title: defaultBrowserTitle, action: nil, keyEquivalent: "") + let icon = defaultBrowser.icon! + icon.size = NSSize(width: 16.0, height: 16.0) + item.image = icon + + menu.addItem(item) + menu.addItem(NSMenuItem.separator()) + + for browser in allBrowsers { + let item = NSMenuItem(title: browser.name!, action: nil, keyEquivalent: "") + item.representedObject = browser.bundleIdentifier + + let icon = browser.icon! + icon.size = NSSize(width: 16.0, height: 16.0) + item.image = browser.icon + menu.addItem(item) + } + + defaultBrowserPopup.selectItem(at: defaultBrowserPopup.indexOfItem(withRepresentedObject: AppDefaults.defaultBrowserID)) + } } diff --git a/Shared/AppNotifications.swift b/Shared/AppNotifications.swift index 84cfd9a72..cb19cb560 100644 --- a/Shared/AppNotifications.swift +++ b/Shared/AppNotifications.swift @@ -12,6 +12,7 @@ import Articles extension Notification.Name { static let InspectableObjectsDidChange = Notification.Name("TimelineSelectionDidChangeNotification") static let UserDidAddFeed = Notification.Name("UserDidAddFeedNotification") + static let DefaultBrowserDidChange = Notification.Name("DefaultBrowserDidChangeNotification") #if !MAC_APP_STORE static let WebInspectorEnabledDidChange = Notification.Name("WebInspectorEnabledDidChange")