2018-01-21 04:06:07 +01:00
|
|
|
//
|
|
|
|
// InspectorWindowController.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-01-21 04:06:07 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/20/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
|
2021-02-06 00:06:45 +01:00
|
|
|
protocol Inspector: AnyObject {
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
@MainActor var objects: [Any]? { get set }
|
|
|
|
@MainActor var isFallbackInspector: Bool { get } // Can handle nothing-to-inspect or unexpected type of objects.
|
|
|
|
@MainActor var windowTitle: String { get }
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
@MainActor func canInspect(_ objects: [Any]) -> Bool
|
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 22:27:17 +01:00
|
|
|
class var shouldOpenAtStartup: Bool {
|
|
|
|
return UserDefaults.standard.bool(forKey: DefaultsKey.windowIsOpen)
|
|
|
|
}
|
|
|
|
|
|
|
|
var objects: [Any]? {
|
|
|
|
didSet {
|
|
|
|
let _ = window
|
|
|
|
currentInspector = inspector(for: objects)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
currentInspector.objects = objects
|
|
|
|
for inspector in inspectors {
|
|
|
|
if inspector !== currentInspector {
|
|
|
|
inspector.objects = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
show(currentInspector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:27:17 +01:00
|
|
|
private struct DefaultsKey {
|
|
|
|
static let windowIsOpen = "FloatingInspectorIsOpen"
|
|
|
|
static let windowOrigin = "FloatingInspectorOrigin"
|
2018-01-21 04:06:07 +01:00
|
|
|
}
|
|
|
|
|
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-12-09 21:32:33 +01:00
|
|
|
let storyboard = NSStoryboard(name: NSStoryboard.Name("Inspector"), bundle: nil)
|
2018-01-21 07:36:17 +01:00
|
|
|
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
|
2020-09-07 19:34:37 +02:00
|
|
|
window?.title = currentInspector.windowTitle
|
2018-01-21 21:46:22 +01:00
|
|
|
|
2018-01-21 22:36:02 +01:00
|
|
|
if let savedOrigin = originFromDefaults() {
|
|
|
|
window?.setFlippedOriginAdjustingForScreen(savedOrigin)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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!
|
|
|
|
}
|
2018-01-21 22:27:17 +01:00
|
|
|
|
|
|
|
func saveState() {
|
|
|
|
|
|
|
|
UserDefaults.standard.set(isOpen, forKey: DefaultsKey.windowIsOpen)
|
|
|
|
if isOpen, let window = window, let flippedOrigin = window.flippedOrigin {
|
|
|
|
UserDefaults.standard.set(NSStringFromPoint(flippedOrigin), forKey: DefaultsKey.windowOrigin)
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 07:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-09 21:32:33 +01:00
|
|
|
return storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(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-23 07:01:25 +01:00
|
|
|
guard let window = window else {
|
2018-01-21 20:35:50 +01:00
|
|
|
return
|
|
|
|
}
|
2020-09-05 15:13:51 +02:00
|
|
|
|
2020-09-07 20:36:12 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
window.title = inspector.windowTitle
|
|
|
|
}
|
2020-09-05 15:13:51 +02:00
|
|
|
|
2018-01-21 21:46:22 +01:00
|
|
|
let flippedOrigin = window.flippedOrigin
|
2018-01-23 07:01:25 +01:00
|
|
|
|
|
|
|
if window.contentViewController != inspector {
|
|
|
|
window.contentViewController = inspector
|
|
|
|
window.makeFirstResponder(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
window.layoutIfNeeded()
|
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 22:36:02 +01:00
|
|
|
|
|
|
|
func originFromDefaults() -> NSPoint? {
|
|
|
|
|
|
|
|
guard let originString = UserDefaults.standard.string(forKey: DefaultsKey.windowOrigin) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let point = NSPointFromString(originString)
|
|
|
|
return point == NSPoint.zero ? nil : point
|
|
|
|
}
|
2018-01-21 04:06:07 +01:00
|
|
|
}
|