NetNewsWire/Mac/Preferences/ExtensionPoints/ExtensionPointPreferencesVi...

128 lines
3.6 KiB
Swift
Raw Normal View History

2020-04-07 04:06:42 +02:00
//
2020-04-07 22:25:33 +02:00
// ExtensionsPreferencesViewController.swift
2020-04-07 04:06:42 +02:00
// NetNewsWire
//
// Created by Maurice Parker on 4/6/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import AppKit
2020-04-07 22:25:33 +02:00
final class ExtensionPointPreferencesViewController: NSViewController {
2020-04-07 04:06:42 +02:00
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var detailView: NSView!
@IBOutlet weak var deleteButton: NSButton!
private var activeExtensionPoints = [ExtensionPoint]()
2020-04-07 04:06:42 +02:00
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
NotificationCenter.default.addObserver(self, selector: #selector(activeExtensionPointsDidChange(_:)), name: .ActiveExtensionPointsDidChange, object: nil)
2020-04-07 04:06:42 +02:00
// Fix tableView frame  for some reason IB wants it 1pt wider than the clip view. This leads to unwanted horizontal scrolling.
var rTable = tableView.frame
rTable.size.width = tableView.superview!.frame.size.width
tableView.frame = rTable
showDefaultView()
2020-04-07 04:06:42 +02:00
}
@IBAction func enableExtensionPoints(_ sender: Any) {
tableView.selectRowIndexes([], byExtendingSelection: false)
showController(ExtensionPointAddViewController())
}
@IBAction func disableExtensionPoint(_ sender: Any) {
guard tableView.selectedRow != -1 else {
return
}
let extensionPoint = activeExtensionPoints[tableView.selectedRow]
ExtensionPointManager.shared.deactivateExtensionPoint(extensionPoint.extensionPointID)
showController(ExtensionPointAddViewController())
}
2020-04-07 04:06:42 +02:00
}
// MARK: - NSTableViewDataSource
2020-04-07 22:25:33 +02:00
extension ExtensionPointPreferencesViewController: NSTableViewDataSource {
2020-04-07 04:06:42 +02:00
func numberOfRows(in tableView: NSTableView) -> Int {
return activeExtensionPoints.count
2020-04-07 04:06:42 +02:00
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
return activeExtensionPoints[row]
2020-04-07 04:06:42 +02:00
}
}
// MARK: - NSTableViewDelegate
2020-04-07 22:25:33 +02:00
extension ExtensionPointPreferencesViewController: NSTableViewDelegate {
2020-04-07 04:06:42 +02:00
private static let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "AccountCell")
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), owner: nil) as? NSTableCellView {
let extensionPoint = activeExtensionPoints[row]
cell.textField?.stringValue = extensionPoint.title
cell.imageView?.image = extensionPoint.templateImage
2020-04-07 04:06:42 +02:00
return cell
}
return nil
}
func tableViewSelectionDidChange(_ notification: Notification) {
let selectedRow = tableView.selectedRow
if tableView.selectedRow == -1 {
deleteButton.isEnabled = false
return
} else {
deleteButton.isEnabled = true
}
let extensionPoint = activeExtensionPoints[selectedRow]
let controller = ExtensionPointDetailViewController(extensionPoint: extensionPoint)
showController(controller)
2020-04-07 04:06:42 +02:00
}
}
// MARK: - Private
2020-04-07 22:25:33 +02:00
private extension ExtensionPointPreferencesViewController {
2020-04-07 04:06:42 +02:00
@objc func activeExtensionPointsDidChange(_ note: Notification) {
showDefaultView()
}
func showDefaultView() {
activeExtensionPoints = Array(ExtensionPointManager.shared.activeExtensionPoints.values).sorted(by: { $0.title < $1.title })
tableView.reloadData()
showController(ExtensionPointAddViewController())
}
2020-04-07 04:06:42 +02:00
func showController(_ controller: NSViewController) {
if let controller = children.first {
children.removeAll()
controller.view.removeFromSuperview()
}
addChild(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
detailView.addSubview(controller.view)
detailView.addFullSizeConstraints(forSubview: controller.view)
}
}