2017-05-27 19:43:27 +02:00
|
|
|
|
//
|
|
|
|
|
// SidebarViewController.swift
|
|
|
|
|
// Evergreen
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/26/15.
|
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
|
import AppKit
|
2017-05-27 19:43:27 +02:00
|
|
|
|
import RSTree
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import Articles
|
2017-09-17 21:34:10 +02:00
|
|
|
|
import Account
|
2017-11-04 22:53:21 +01:00
|
|
|
|
import RSCore
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-11-05 06:51:14 +01:00
|
|
|
|
@objc class SidebarViewController: NSViewController, NSOutlineViewDelegate, NSOutlineViewDataSource, UndoableCommandRunner {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-10-21 21:14:45 +02:00
|
|
|
|
@IBOutlet var outlineView: SidebarOutlineView!
|
2018-01-29 01:09:18 +01:00
|
|
|
|
@IBOutlet var gearMenuDelegate: SidebarGearMenuDelegate!
|
2018-02-07 22:17:28 +01:00
|
|
|
|
@IBOutlet var contextualMenuDelegate: SidebarContextualMenuDelegate!
|
2018-01-29 01:09:18 +01:00
|
|
|
|
|
2017-10-19 22:27:59 +02:00
|
|
|
|
let treeControllerDelegate = SidebarTreeControllerDelegate()
|
|
|
|
|
lazy var treeController: TreeController = {
|
2018-02-13 07:02:51 +01:00
|
|
|
|
return TreeController(delegate: treeControllerDelegate)
|
2017-10-19 22:27:59 +02:00
|
|
|
|
}()
|
2018-02-13 07:02:51 +01:00
|
|
|
|
lazy var dataSource: SidebarOutlineDataSource = {
|
|
|
|
|
return SidebarOutlineDataSource(treeController: treeController)
|
|
|
|
|
}()
|
|
|
|
|
|
2017-11-05 06:51:14 +01:00
|
|
|
|
var undoableCommands = [UndoableCommand]()
|
2017-11-05 21:14:36 +01:00
|
|
|
|
private var animatingChanges = false
|
2017-11-25 06:39:59 +01:00
|
|
|
|
private var sidebarCellAppearance: SidebarCellAppearance!
|
2017-10-05 22:15:32 +02:00
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
|
var renameWindowController: RenameWindowController?
|
|
|
|
|
|
2018-01-21 20:35:50 +01:00
|
|
|
|
var selectedObjects: [AnyObject] {
|
|
|
|
|
return selectedNodes.representedObjects()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
// MARK: - NSViewController
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
2017-11-25 06:39:59 +01:00
|
|
|
|
sidebarCellAppearance = SidebarCellAppearance(theme: appDelegate.currentTheme, fontSize: AppDefaults.shared.sidebarFontSize)
|
|
|
|
|
|
2018-02-13 07:02:51 +01:00
|
|
|
|
outlineView.dataSource = dataSource
|
2017-11-08 06:14:58 +01:00
|
|
|
|
outlineView.setDraggingSourceOperationMask(.move, forLocal: true)
|
|
|
|
|
outlineView.setDraggingSourceOperationMask(.copy, forLocal: false)
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2017-10-19 22:27:59 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil)
|
2017-10-22 00:56:01 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddFeed(_:)), name: .UserDidAddFeed, object: nil)
|
2017-11-15 22:26:10 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidPerform(_:)), name: .BatchUpdateDidPerform, object: nil)
|
2017-11-25 20:14:42 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
|
2017-11-25 22:48:14 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(feedSettingDidChange(_:)), name: .FeedSettingDidChange, object: nil)
|
2018-01-24 06:07:29 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
outlineView.reloadData()
|
2017-11-19 02:10:47 +01:00
|
|
|
|
|
|
|
|
|
// Always expand all group items on initial display.
|
|
|
|
|
var row = 0
|
|
|
|
|
while(true) {
|
|
|
|
|
guard let item = outlineView.item(atRow: row) else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
let node = item as! Node
|
|
|
|
|
if node.isGroupItem {
|
|
|
|
|
outlineView.expandItem(item)
|
|
|
|
|
}
|
|
|
|
|
row += 1
|
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
// MARK: - Notifications
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-11-05 07:05:20 +01:00
|
|
|
|
guard let representedObject = note.object else {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2017-11-26 00:21:35 +01:00
|
|
|
|
configureUnreadCountForCellsForRepresentedObject(representedObject as AnyObject)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
@objc func containerChildrenDidChange(_ note: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
rebuildTreeAndReloadDataIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
@objc func batchUpdateDidPerform(_ notification: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
rebuildTreeAndReloadDataIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
@objc func userDidAddFeed(_ notification: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-12-18 21:34:07 +01:00
|
|
|
|
guard let feed = notification.userInfo?[UserInfoKey.feed] else {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2017-12-18 21:34:07 +01:00
|
|
|
|
revealAndSelectRepresentedObject(feed as AnyObject)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:14:42 +01:00
|
|
|
|
@objc func faviconDidBecomeAvailable(_ note: Notification) {
|
|
|
|
|
|
2017-12-14 06:16:52 +01:00
|
|
|
|
applyToAvailableCells(configureFavicon)
|
2017-11-25 20:14:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 22:48:14 +01:00
|
|
|
|
@objc func feedSettingDidChange(_ note: Notification) {
|
|
|
|
|
|
|
|
|
|
guard let feed = note.object as? Feed else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
configureCellsForRepresentedObject(feed)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 06:07:29 +01:00
|
|
|
|
@objc func displayNameDidChange(_ note: Notification) {
|
|
|
|
|
|
|
|
|
|
guard let object = note.object else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
configureCellsForRepresentedObject(object as AnyObject)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
// MARK: - Actions
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
@IBAction func delete(_ sender: AnyObject?) {
|
|
|
|
|
|
|
|
|
|
if outlineView.selectionIsEmpty {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 21:14:45 +02:00
|
|
|
|
let nodesToDelete = treeController.normalizedSelectedNodes(selectedNodes)
|
2017-11-05 06:51:14 +01:00
|
|
|
|
|
2018-02-26 07:02:55 +01:00
|
|
|
|
guard let undoManager = undoManager, let deleteCommand = DeleteFromSidebarCommand(nodesToDelete: nodesToDelete, treeController: treeController, undoManager: undoManager) else {
|
2017-11-05 06:51:14 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 21:14:36 +01:00
|
|
|
|
animatingChanges = true
|
2017-05-27 19:43:27 +02:00
|
|
|
|
outlineView.beginUpdates()
|
|
|
|
|
|
2017-11-26 02:44:54 +01:00
|
|
|
|
let indexSetsGroupedByParent = Node.indexSetsGroupedByParent(nodesToDelete)
|
|
|
|
|
for (parent, indexSet) in indexSetsGroupedByParent {
|
|
|
|
|
outlineView.removeItems(at: indexSet, inParent: parent.isRoot ? nil : parent, withAnimation: [.slideDown])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outlineView.endUpdates()
|
|
|
|
|
|
|
|
|
|
runCommand(deleteCommand)
|
2017-11-05 21:14:36 +01:00
|
|
|
|
animatingChanges = false
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 21:59:31 +01:00
|
|
|
|
@IBAction func openInBrowser(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
guard let feed = singleSelectedFeed, let homePageURL = feed.homePageURL else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
Browser.open(homePageURL)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-27 21:39:07 +01:00
|
|
|
|
@IBAction func gotoToday(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
outlineView.revealAndSelectRepresentedObject(SmartFeedsController.shared.todayFeed, treeController)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func gotoAllUnread(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
outlineView.revealAndSelectRepresentedObject(SmartFeedsController.shared.unreadFeed, treeController)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func gotoStarred(_ sender: Any?) {
|
|
|
|
|
|
|
|
|
|
outlineView.revealAndSelectRepresentedObject(SmartFeedsController.shared.starredFeed, treeController)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 03:58:50 +01:00
|
|
|
|
@IBAction func copy(_ sender: Any?) {
|
|
|
|
|
|
2018-02-12 07:10:28 +01:00
|
|
|
|
NSPasteboard.general.copyObjects(selectedObjects)
|
2018-02-12 03:58:50 +01:00
|
|
|
|
}
|
2018-02-12 07:10:28 +01:00
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
// MARK: - Navigation
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
func canGoToNextUnread() -> Bool {
|
|
|
|
|
|
2018-02-16 02:50:31 +01:00
|
|
|
|
if let _ = nextSelectableRowWithUnreadArticle() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func goToNextUnread() {
|
|
|
|
|
|
2018-02-16 02:50:31 +01:00
|
|
|
|
guard let row = nextSelectableRowWithUnreadArticle() else {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
assertionFailure("goToNextUnread called before checking if there is a next unread.")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outlineView.selectRowIndexes(IndexSet([row]), byExtendingSelection: false)
|
|
|
|
|
}
|
2017-12-20 00:24:38 +01:00
|
|
|
|
|
|
|
|
|
func focus() {
|
|
|
|
|
|
|
|
|
|
guard let window = outlineView.window else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
window.makeFirstResponderUnlessDescendantIsFirstResponder(outlineView)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
// MARK: - Contextual Menu
|
2018-01-29 01:09:18 +01:00
|
|
|
|
|
|
|
|
|
func contextualMenuForSelectedObjects() -> NSMenu? {
|
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
|
return menu(for: selectedObjects)
|
2018-01-29 01:09:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 22:25:17 +01:00
|
|
|
|
func contextualMenuForClickedRows() -> NSMenu? {
|
|
|
|
|
|
|
|
|
|
let row = outlineView.clickedRow
|
|
|
|
|
guard row != -1, let node = nodeForRow(row) else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 06:23:18 +01:00
|
|
|
|
if outlineView.selectedRowIndexes.contains(row) {
|
|
|
|
|
// If the clickedRow is part of the selected rows, then do a contextual menu for all the selected rows.
|
|
|
|
|
return contextualMenuForSelectedObjects()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 22:25:17 +01:00
|
|
|
|
let object = node.representedObject
|
|
|
|
|
return menu(for: [object])
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 06:08:36 +01:00
|
|
|
|
// MARK: - NSOutlineViewDelegate
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
|
|
|
|
|
|
|
|
|
|
let node = item as! Node
|
2017-11-19 01:56:36 +01:00
|
|
|
|
|
|
|
|
|
if node.isGroupItem {
|
|
|
|
|
let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HeaderCell"), owner: self) as! NSTableCellView
|
|
|
|
|
configureGroupCell(cell, node)
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DataCell"), owner: self) as! SidebarCell
|
2017-05-27 19:43:27 +02:00
|
|
|
|
configure(cell, node)
|
|
|
|
|
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 01:56:36 +01:00
|
|
|
|
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
|
|
|
|
|
|
|
|
|
|
let node = item as! Node
|
|
|
|
|
return node.isGroupItem
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 06:01:16 +01:00
|
|
|
|
func outlineView(_ outlineView: NSOutlineView, selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet {
|
|
|
|
|
|
|
|
|
|
// Don’t allow selecting group items.
|
|
|
|
|
// If any index in IndexSet contains a group item,
|
|
|
|
|
// return the current selection (not a modified version of the proposed selection).
|
|
|
|
|
|
|
|
|
|
for index in proposedSelectionIndexes {
|
|
|
|
|
if let node = nodeForRow(index), node.isGroupItem {
|
|
|
|
|
return outlineView.selectedRowIndexes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return proposedSelectionIndexes
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 23:43:29 +01:00
|
|
|
|
func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
|
|
|
|
|
|
|
|
|
|
return !self.outlineView(outlineView, isGroupItem: item)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
func outlineViewSelectionDidChange(_ notification: Notification) {
|
|
|
|
|
|
2018-02-13 07:13:37 +01:00
|
|
|
|
postSidebarSelectionDidChangeNotification(selectedObjects.isEmpty ? nil : selectedObjects)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-13 07:02:51 +01:00
|
|
|
|
// MARK: - NSUserInterfaceValidations
|
2018-02-12 07:10:28 +01:00
|
|
|
|
|
|
|
|
|
extension SidebarViewController: NSUserInterfaceValidations {
|
|
|
|
|
|
|
|
|
|
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
|
|
|
|
|
|
|
|
|
|
if item.action == #selector(copy(_:)) {
|
|
|
|
|
return NSPasteboard.general.canCopyAtLeastOneObject(selectedObjects)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
//MARK: - Private
|
|
|
|
|
|
|
|
|
|
private extension SidebarViewController {
|
|
|
|
|
|
|
|
|
|
var selectedNodes: [Node] {
|
2017-12-20 21:59:31 +01:00
|
|
|
|
if let nodes = outlineView.selectedItems as? [Node] {
|
|
|
|
|
return nodes
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2017-12-20 21:59:31 +01:00
|
|
|
|
return [Node]()
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2017-12-20 21:59:31 +01:00
|
|
|
|
|
|
|
|
|
var singleSelectedNode: Node? {
|
|
|
|
|
guard selectedNodes.count == 1 else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return selectedNodes.first!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var singleSelectedFeed: Feed? {
|
|
|
|
|
guard let node = singleSelectedNode else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return node.representedObject as? Feed
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
func rebuildTreeAndReloadDataIfNeeded() {
|
|
|
|
|
|
2017-11-05 21:14:36 +01:00
|
|
|
|
if !animatingChanges && !BatchUpdate.shared.isPerforming {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
treeController.rebuild()
|
|
|
|
|
outlineView.reloadData()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 07:05:20 +01:00
|
|
|
|
func postSidebarSelectionDidChangeNotification(_ selectedObjects: [AnyObject]?) {
|
2017-09-24 21:24:44 +02:00
|
|
|
|
|
2017-12-18 21:34:07 +01:00
|
|
|
|
var userInfo = UserInfoDictionary()
|
2017-10-20 06:56:30 +02:00
|
|
|
|
if let objects = selectedObjects {
|
2017-12-18 21:34:07 +01:00
|
|
|
|
userInfo[UserInfoKey.objects] = objects
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2017-12-18 21:34:07 +01:00
|
|
|
|
userInfo[UserInfoKey.view] = outlineView
|
2017-10-20 06:56:30 +02:00
|
|
|
|
|
2017-12-18 21:34:07 +01:00
|
|
|
|
NotificationCenter.default.post(name: .SidebarSelectionDidChange, object: self, userInfo: userInfo)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 07:05:20 +01:00
|
|
|
|
func updateUnreadCounts(for objects: [AnyObject]) {
|
2017-10-19 06:53:45 +02:00
|
|
|
|
|
|
|
|
|
// On selection, update unread counts for folders and feeds.
|
|
|
|
|
// For feeds, actually fetch from database.
|
|
|
|
|
|
|
|
|
|
for object in objects {
|
|
|
|
|
if let feed = object as? Feed, let account = feed.account {
|
|
|
|
|
account.updateUnreadCounts(for: Set([feed]))
|
|
|
|
|
}
|
2017-10-22 01:37:40 +02:00
|
|
|
|
else if let folder = object as? Folder, let account = folder.account {
|
|
|
|
|
account.updateUnreadCounts(for: folder.flattenedFeeds())
|
2017-10-19 06:53:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
func nodeForItem(_ item: AnyObject?) -> Node {
|
|
|
|
|
|
|
|
|
|
if item == nil {
|
|
|
|
|
return treeController.rootNode
|
|
|
|
|
}
|
|
|
|
|
return item as! Node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nodeForRow(_ row: Int) -> Node? {
|
|
|
|
|
|
|
|
|
|
if row < 0 || row >= outlineView.numberOfRows {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let node = outlineView.item(atRow: row) as? Node {
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rowHasAtLeastOneUnreadArticle(_ row: Int) -> Bool {
|
|
|
|
|
|
|
|
|
|
if let oneNode = nodeForRow(row) {
|
|
|
|
|
if let unreadCountProvider = oneNode.representedObject as? UnreadCountProvider {
|
|
|
|
|
if unreadCountProvider.unreadCount > 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 02:50:31 +01:00
|
|
|
|
func rowIsGroupItem(_ row: Int) -> Bool {
|
|
|
|
|
|
|
|
|
|
if let node = nodeForRow(row), outlineView.isGroupItem(node) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nextSelectableRowWithUnreadArticle() -> Int? {
|
|
|
|
|
|
|
|
|
|
// Skip group items, because they should never be selected.
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
let selectedRow = outlineView.selectedRow
|
|
|
|
|
let numberOfRows = outlineView.numberOfRows
|
|
|
|
|
var row = selectedRow + 1
|
|
|
|
|
|
|
|
|
|
while (row < numberOfRows) {
|
2018-02-16 02:50:31 +01:00
|
|
|
|
if rowHasAtLeastOneUnreadArticle(row) && !rowIsGroupItem(row) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return row
|
|
|
|
|
}
|
|
|
|
|
row += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
row = 0
|
|
|
|
|
while (row <= selectedRow) {
|
2018-02-16 02:50:31 +01:00
|
|
|
|
if rowHasAtLeastOneUnreadArticle(row) && !rowIsGroupItem(row) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return row
|
|
|
|
|
}
|
|
|
|
|
row += 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func configure(_ cell: SidebarCell, _ node: Node) {
|
|
|
|
|
|
2017-11-25 06:39:59 +01:00
|
|
|
|
cell.cellAppearance = sidebarCellAppearance
|
2017-05-27 19:43:27 +02:00
|
|
|
|
cell.objectValue = node
|
|
|
|
|
cell.name = nameFor(node)
|
2017-11-26 01:16:03 +01:00
|
|
|
|
configureUnreadCount(cell, node)
|
2017-12-14 06:16:52 +01:00
|
|
|
|
configureFavicon(cell, node)
|
2017-12-17 19:51:46 +01:00
|
|
|
|
cell.shouldShowImage = node.representedObject is SmallIconProvider
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 00:21:35 +01:00
|
|
|
|
func configureUnreadCount(_ cell: SidebarCell, _ node: Node) {
|
|
|
|
|
|
|
|
|
|
cell.unreadCount = unreadCountFor(node)
|
2017-12-14 06:16:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func configureFavicon(_ cell: SidebarCell, _ node: Node) {
|
|
|
|
|
|
|
|
|
|
cell.image = imageFor(node)
|
2017-11-26 00:21:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 01:56:36 +01:00
|
|
|
|
func configureGroupCell(_ cell: NSTableCellView, _ node: Node) {
|
|
|
|
|
|
|
|
|
|
cell.objectValue = node
|
|
|
|
|
cell.textField?.stringValue = nameFor(node)
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
func imageFor(_ node: Node) -> NSImage? {
|
|
|
|
|
|
2017-12-17 19:51:46 +01:00
|
|
|
|
if let smallIconProvider = node.representedObject as? SmallIconProvider {
|
|
|
|
|
return smallIconProvider.smallIcon
|
2017-11-25 06:57:28 +01:00
|
|
|
|
}
|
2017-12-17 19:51:46 +01:00
|
|
|
|
return nil
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nameFor(_ node: Node) -> String {
|
|
|
|
|
|
|
|
|
|
if let displayNameProvider = node.representedObject as? DisplayNameProvider {
|
|
|
|
|
return displayNameProvider.nameForDisplay
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unreadCountFor(_ node: Node) -> Int {
|
|
|
|
|
|
|
|
|
|
if let unreadCountProvider = node.representedObject as? UnreadCountProvider {
|
|
|
|
|
return unreadCountProvider.unreadCount
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:14:42 +01:00
|
|
|
|
func cellForRowView(_ rowView: NSTableRowView) -> SidebarCell? {
|
|
|
|
|
|
|
|
|
|
return rowView.view(atColumn: 0) as? SidebarCell
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
func availableSidebarCells() -> [SidebarCell] {
|
|
|
|
|
|
|
|
|
|
var cells = [SidebarCell]()
|
|
|
|
|
|
|
|
|
|
outlineView.enumerateAvailableRowViews { (rowView: NSTableRowView, _: Int) -> Void in
|
2017-11-25 20:14:42 +01:00
|
|
|
|
if let cell = cellForRowView(rowView) {
|
|
|
|
|
cells += [cell]
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cells
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:14:42 +01:00
|
|
|
|
func configureAvailableCells() {
|
|
|
|
|
|
2017-11-26 01:16:03 +01:00
|
|
|
|
applyToAvailableCells(configure)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applyToAvailableCells(_ callback: (SidebarCell, Node) -> Void) {
|
|
|
|
|
|
2017-11-25 20:14:42 +01:00
|
|
|
|
outlineView.enumerateAvailableRowViews { (rowView: NSTableRowView, row: Int) -> Void in
|
|
|
|
|
|
|
|
|
|
guard let cell = cellForRowView(rowView), let node = nodeForRow(row) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-11-26 01:16:03 +01:00
|
|
|
|
callback(cell, node)
|
2017-11-25 20:14:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 01:16:03 +01:00
|
|
|
|
func applyToCellsForRepresentedObject(_ representedObject: AnyObject, _ callback: (SidebarCell, Node) -> Void) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-11-26 01:16:03 +01:00
|
|
|
|
applyToAvailableCells { (cell, node) in
|
|
|
|
|
if node.representedObject === representedObject {
|
|
|
|
|
callback(cell, node)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 01:16:03 +01:00
|
|
|
|
func configureCellsForRepresentedObject(_ representedObject: AnyObject) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-11-26 01:16:03 +01:00
|
|
|
|
applyToCellsForRepresentedObject(representedObject, configure)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-26 00:21:35 +01:00
|
|
|
|
func configureUnreadCountForCellsForRepresentedObject(_ representedObject: AnyObject) {
|
|
|
|
|
|
2017-11-26 01:16:03 +01:00
|
|
|
|
applyToCellsForRepresentedObject(representedObject, configureUnreadCount)
|
2017-11-26 00:21:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
@discardableResult
|
2017-11-05 07:05:20 +01:00
|
|
|
|
func revealAndSelectRepresentedObject(_ representedObject: AnyObject) -> Bool {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
return outlineView.revealAndSelectRepresentedObject(representedObject, treeController)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 22:15:07 +02:00
|
|
|
|
func folderParentForNode(_ node: Node) -> Container? {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-10-02 22:15:07 +02:00
|
|
|
|
if let folder = node.parent?.representedObject as? Container {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return folder
|
|
|
|
|
}
|
|
|
|
|
if let feed = node.representedObject as? Feed {
|
|
|
|
|
return feed.account
|
|
|
|
|
}
|
|
|
|
|
if let folder = node.representedObject as? Folder {
|
|
|
|
|
return folder.account
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func deleteItemForNode(_ node: Node) {
|
|
|
|
|
|
2017-10-02 22:15:07 +02:00
|
|
|
|
// if let folder = folderParentForNode(node) {
|
|
|
|
|
// folder.deleteItems([node.representedObject])
|
|
|
|
|
// }
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func deleteItemsForNodes(_ nodes: [Node]) {
|
|
|
|
|
|
|
|
|
|
nodes.forEach { (oneNode) in
|
|
|
|
|
|
|
|
|
|
deleteItemForNode(oneNode)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-26 02:44:54 +01:00
|
|
|
|
|
|
|
|
|
func commonParentItemForNodes(_ nodes: [Node]) -> Node? {
|
|
|
|
|
|
|
|
|
|
if nodes.isEmpty {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let parent = nodes.first!.parent else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
for node in nodes {
|
|
|
|
|
if node.parent !== parent {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return parent
|
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 03:04:36 +01:00
|
|
|
|
|