NetNewsWire/Mac/Preferences/General/GeneralPrefencesViewControl...

93 lines
2.4 KiB
Swift
Raw Normal View History

//
// GeneralPrefencesViewController.swift
// NetNewsWire
//
// Created by Brent Simmons on 9/3/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
import RSCore
import RSWeb
final class GeneralPreferencesViewController: NSViewController {
@IBOutlet var defaultBrowserPopup: NSPopUpButton!
public override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
commonInit()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override func viewWillAppear() {
super.viewWillAppear()
updateUI()
}
// MARK: - Notifications
@objc func applicationWillBecomeActive(_ note: Notification) {
updateUI()
}
// MARK: - Actions
@IBAction func browserPopUpDidChangeValue(_ sender: Any?) {
guard let menuItem = defaultBrowserPopup.selectedItem else {
return
}
let bundleID = menuItem.representedObject as? String
AppDefaults.shared.defaultBrowserID = bundleID
updateUI()
}
}
// MARK: - Private
private extension GeneralPreferencesViewController {
func commonInit() {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillBecomeActive(_:)), name: NSApplication.willBecomeActiveNotification, object: nil)
}
func updateUI() {
updateBrowserPopup()
}
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.shared.defaultBrowserID))
}
}