2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// MainWindowController.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 8/1/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
2017-09-17 21:22:15 +02:00
|
|
|
import Data
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
private let kWindowFrameKey = "MainWindow"
|
|
|
|
|
|
|
|
class MainWindowController : NSWindowController, NSUserInterfaceValidations {
|
|
|
|
|
|
|
|
// MARK: NSWindowController
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
|
|
|
|
super.windowDidLoad()
|
|
|
|
|
2017-05-29 21:14:07 +02:00
|
|
|
// window?.titleVisibility = .hidden
|
2017-09-17 21:54:08 +02:00
|
|
|
window?.setFrameUsingName(NSWindow.FrameAutosaveName(rawValue: kWindowFrameKey), force: true)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
detailSplitViewItem?.minimumThickness = 384
|
|
|
|
|
2017-09-18 01:30:45 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminate(_:)), name: NSApplication.willTerminateNotification, object: nil)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(appNavigationKeyPressed(_:)), name: .AppNavigationKeyPressed, object: nil)
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Notifications
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
@objc func applicationWillTerminate(_ note: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
window?.saveFrame(usingName: NSWindow.FrameAutosaveName(rawValue: kWindowFrameKey))
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
@objc func appNavigationKeyPressed(_ note: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2017-09-24 21:24:44 +02:00
|
|
|
guard let navigationKey = note.appInfo?.navigationKey else {
|
2017-05-27 19:43:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let contentView = window?.contentView, let view = note.object as? NSView, view.isDescendant(of: contentView) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-24 21:24:44 +02:00
|
|
|
print(navigationKey)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
@objc func refreshProgressDidChange(_ note: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
rs_performSelectorCoalesced(#selector(MainWindowController.coalescedMakeToolbarValidate(_:)), with: nil, afterDelay: 0.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Toolbar
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
@objc func coalescedMakeToolbarValidate(_ sender: Any) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
window?.toolbar?.validateVisibleItems()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: NSUserInterfaceValidations
|
|
|
|
|
|
|
|
public func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
|
|
|
|
|
|
|
|
if item.action == #selector(openArticleInBrowser(_:)) {
|
|
|
|
return currentLink != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.action == #selector(nextUnread(_:)) {
|
|
|
|
return canGoToNextUnread()
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.action == #selector(markAllAsRead(_:)) {
|
|
|
|
return canMarkAllAsRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func openArticleInBrowser(_ sender: AnyObject?) {
|
|
|
|
|
|
|
|
if let link = currentLink {
|
2017-10-06 03:12:58 +02:00
|
|
|
Browser.open(link)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func nextUnread(_ sender: AnyObject?) {
|
|
|
|
|
|
|
|
guard let timelineViewController = timelineViewController, let sidebarViewController = sidebarViewController else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTimelineViewFirstResponder() {
|
|
|
|
window!.makeFirstResponderUnlessDescendantIsFirstResponder(timelineViewController.tableView)
|
|
|
|
}
|
|
|
|
|
|
|
|
if timelineViewController.canGoToNextUnread() {
|
|
|
|
timelineViewController.goToNextUnread()
|
|
|
|
makeTimelineViewFirstResponder()
|
|
|
|
}
|
|
|
|
else if sidebarViewController.canGoToNextUnread() {
|
|
|
|
sidebarViewController.goToNextUnread()
|
|
|
|
makeTimelineViewFirstResponder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func markAllAsRead(_ sender: AnyObject?) {
|
|
|
|
|
|
|
|
timelineViewController?.markAllAsRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func toggleSidebar(_ sender: AnyObject?) {
|
|
|
|
|
|
|
|
splitViewController!.toggleSidebar(sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MainWindowController {
|
|
|
|
|
|
|
|
var splitViewController: NSSplitViewController? {
|
|
|
|
get {
|
|
|
|
guard let viewController = contentViewController else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return viewController.childViewControllers.first as? NSSplitViewController
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var sidebarViewController: SidebarViewController? {
|
|
|
|
get {
|
|
|
|
return splitViewController?.splitViewItems[0].viewController as? SidebarViewController
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var timelineViewController: TimelineViewController? {
|
|
|
|
get {
|
|
|
|
return splitViewController?.splitViewItems[1].viewController as? TimelineViewController
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var detailSplitViewItem: NSSplitViewItem? {
|
|
|
|
|
|
|
|
return splitViewController?.splitViewItems[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
var detailViewController: DetailViewController? {
|
|
|
|
|
|
|
|
get {
|
|
|
|
return splitViewController?.splitViewItems[2].viewController as? DetailViewController
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectedArticles: [Article]? {
|
|
|
|
get {
|
|
|
|
return timelineViewController?.selectedArticles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var oneSelectedArticle: Article? {
|
|
|
|
get {
|
|
|
|
if let articles = selectedArticles {
|
|
|
|
return articles.count == 1 ? articles[0] : nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentLink: String? {
|
|
|
|
get {
|
|
|
|
if let article = oneSelectedArticle {
|
2017-09-18 01:30:45 +02:00
|
|
|
return article.preferredLink
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func canGoToNextUnread() -> Bool {
|
|
|
|
|
|
|
|
guard let timelineViewController = timelineViewController, let sidebarViewController = sidebarViewController else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return timelineViewController.canGoToNextUnread() || sidebarViewController.canGoToNextUnread()
|
|
|
|
}
|
|
|
|
|
|
|
|
func canMarkAllAsRead() -> Bool {
|
|
|
|
|
|
|
|
return timelineViewController?.canMarkAllAsRead() ?? false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|