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!
|
|
|
|
|
|
2020-04-15 05:33:05 +02:00
|
|
|
|
private var activeExtensionPoints = [ExtensionPoint]()
|
2020-04-07 04:06:42 +02:00
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
|
|
tableView.delegate = self
|
|
|
|
|
tableView.dataSource = self
|
2020-04-09 03:22:13 +02:00
|
|
|
|
|
|
|
|
|
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
|
2020-04-09 03:22:13 +02:00
|
|
|
|
|
2020-04-14 23:47:05 +02:00
|
|
|
|
showDefaultView()
|
2020-04-07 04:06:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 03:22:13 +02:00
|
|
|
|
@IBAction func enableExtensionPoints(_ sender: Any) {
|
|
|
|
|
tableView.selectRowIndexes([], byExtendingSelection: false)
|
|
|
|
|
showController(ExtensionPointAddViewController())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func disableExtensionPoint(_ sender: Any) {
|
|
|
|
|
guard tableView.selectedRow != -1 else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 05:33:05 +02:00
|
|
|
|
let extensionPoint = activeExtensionPoints[tableView.selectedRow]
|
|
|
|
|
ExtensionPointManager.shared.deactivateExtensionPoint(extensionPoint.extensionPointID)
|
2020-04-09 03:22:13 +02:00
|
|
|
|
|
|
|
|
|
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 {
|
2020-04-15 05:33:05 +02:00
|
|
|
|
return activeExtensionPoints.count
|
2020-04-07 04:06:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
|
2020-04-15 05:33:05 +02:00
|
|
|
|
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 {
|
2020-04-15 05:33:05 +02:00
|
|
|
|
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) {
|
|
|
|
|
|
2020-04-09 03:22:13 +02:00
|
|
|
|
let selectedRow = tableView.selectedRow
|
|
|
|
|
if tableView.selectedRow == -1 {
|
|
|
|
|
deleteButton.isEnabled = false
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
deleteButton.isEnabled = true
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 05:33:05 +02:00
|
|
|
|
let extensionPoint = activeExtensionPoints[selectedRow]
|
|
|
|
|
let controller = ExtensionPointDetailViewController(extensionPoint: extensionPoint)
|
2020-04-09 03:22:13 +02:00
|
|
|
|
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
|
|
|
|
|
2020-04-09 03:22:13 +02:00
|
|
|
|
@objc func activeExtensionPointsDidChange(_ note: Notification) {
|
2020-04-14 23:47:05 +02:00
|
|
|
|
showDefaultView()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showDefaultView() {
|
2020-04-15 05:33:05 +02:00
|
|
|
|
activeExtensionPoints = Array(ExtensionPointManager.shared.activeExtensionPoints.values).sorted(by: { $0.title < $1.title })
|
2020-04-09 03:22:13 +02:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|