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
This commit is contained in:
Nate Weaver 2020-05-18 21:29:53 -05:00
parent 0c2d057860
commit 1ab17b2574
4 changed files with 69 additions and 5 deletions

View File

@ -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)
}
}
}
}

View File

@ -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

View File

@ -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))
}
}

View File

@ -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")