2018-01-21 04:06:07 +01:00
|
|
|
//
|
|
|
|
// InspectorWindowController.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/20/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
protocol Inspector: class {
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
var objects: [Any]? { get set }
|
|
|
|
var isFallbackInspector: Bool { get } // Can handle nothing-to-inspect or unexpected type of objects.
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
func canInspect(_ objects: [Any]) -> Bool
|
|
|
|
|
|
|
|
func willEndInspectingObjects() // Called on the current inspector right before objects is about to change.
|
2018-01-21 06:48:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
typealias InspectorViewController = Inspector & NSViewController
|
|
|
|
|
|
|
|
|
2018-01-21 04:06:07 +01:00
|
|
|
final class InspectorWindowController: NSWindowController {
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
private var inspectors: [InspectorViewController]!
|
2018-01-21 07:36:17 +01:00
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
private var currentInspector: InspectorViewController! {
|
2018-01-21 07:36:17 +01:00
|
|
|
didSet {
|
|
|
|
if let oldInspector = oldValue {
|
|
|
|
oldInspector.willEndInspectingObjects()
|
|
|
|
}
|
|
|
|
currentInspector.objects = objects
|
|
|
|
for inspector in inspectors {
|
|
|
|
if inspector !== currentInspector {
|
|
|
|
inspector.objects = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
show(currentInspector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var objects: [Any]? {
|
|
|
|
didSet {
|
2018-01-21 20:35:50 +01:00
|
|
|
let _ = window
|
2018-01-21 07:36:17 +01:00
|
|
|
currentInspector = inspector(for: objects)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isOpen: Bool {
|
2018-01-21 04:06:07 +01:00
|
|
|
get {
|
|
|
|
return isWindowLoaded && window!.isVisible
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
override func windowDidLoad() {
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
let nothingInspector = window?.contentViewController as! InspectorViewController
|
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Inspector"), bundle: nil)
|
|
|
|
let feedInspector = inspector("Feed", storyboard)
|
|
|
|
let folderInspector = inspector("Folder", storyboard)
|
|
|
|
let builtinSmartFeedInspector = inspector("BuiltinSmartFeed", storyboard)
|
2018-01-21 20:35:50 +01:00
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
inspectors = [feedInspector, folderInspector, builtinSmartFeedInspector, nothingInspector]
|
2018-01-21 20:35:50 +01:00
|
|
|
currentInspector = nothingInspector
|
2018-01-21 21:46:22 +01:00
|
|
|
|
2018-01-21 22:11:09 +01:00
|
|
|
window?.flippedOrigin = NSPoint(x: 256, y: 256)
|
2018-01-21 07:36:17 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
func inspector(for objects: [Any]?) -> InspectorViewController {
|
2018-01-21 07:36:17 +01:00
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
var fallbackInspector: InspectorViewController? = nil
|
2018-01-21 07:36:17 +01:00
|
|
|
|
|
|
|
for inspector in inspectors {
|
|
|
|
if inspector.isFallbackInspector {
|
|
|
|
fallbackInspector = inspector
|
|
|
|
}
|
|
|
|
else if let objects = objects, inspector.canInspect(objects) {
|
|
|
|
return inspector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallbackInspector!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension InspectorWindowController {
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
func inspector(_ identifier: String, _ storyboard: NSStoryboard) -> InspectorViewController {
|
2018-01-21 07:36:17 +01:00
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
return storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: identifier)) as! InspectorViewController
|
2018-01-21 07:36:17 +01:00
|
|
|
}
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
func show(_ inspector: InspectorViewController) {
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
guard let window = window, inspector !== window.contentViewController else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-21 21:46:22 +01:00
|
|
|
|
|
|
|
let flippedOrigin = window.flippedOrigin
|
2018-01-21 20:35:50 +01:00
|
|
|
window.contentViewController = inspector
|
2018-01-21 22:11:09 +01:00
|
|
|
if let flippedOrigin = flippedOrigin {
|
|
|
|
window.setFlippedOriginAdjustingForScreen(flippedOrigin)
|
2018-01-21 21:46:22 +01:00
|
|
|
}
|
2018-01-21 06:48:27 +01:00
|
|
|
}
|
2018-01-21 04:06:07 +01:00
|
|
|
}
|