2018-09-13 05:23:23 +02:00
|
|
|
//
|
|
|
|
// FolderTreeMenu.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 9/12/18.
|
|
|
|
// Copyright © 2018 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import RSCore
|
|
|
|
import RSTree
|
2018-09-22 21:46:24 +02:00
|
|
|
import Account
|
2018-09-13 05:23:23 +02:00
|
|
|
|
|
|
|
class FolderTreeMenu {
|
|
|
|
|
2020-08-12 03:19:17 +02:00
|
|
|
static func createFolderPopupMenu(with rootNode: Node, restrictToSpecialAccounts: Bool = false) -> NSMenu {
|
2018-09-13 05:23:23 +02:00
|
|
|
|
|
|
|
let menu = NSMenu(title: "Folders")
|
2019-09-20 22:37:10 +02:00
|
|
|
menu.autoenablesItems = false
|
2018-09-13 05:23:23 +02:00
|
|
|
|
2019-05-01 21:03:59 +02:00
|
|
|
for childNode in rootNode.childNodes {
|
|
|
|
|
2019-09-20 22:37:10 +02:00
|
|
|
guard let account = childNode.representedObject as? Account else {
|
2019-05-01 21:03:59 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-12 03:19:17 +02:00
|
|
|
if restrictToSpecialAccounts && !(account.type == .onMyMac || account.type == .cloudKit) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:37:10 +02:00
|
|
|
let menuItem = NSMenuItem(title: account.nameForDisplay, action: nil, keyEquivalent: "")
|
2019-05-01 21:03:59 +02:00
|
|
|
menuItem.representedObject = childNode.representedObject
|
2019-09-20 22:37:10 +02:00
|
|
|
|
|
|
|
if account.behaviors.contains(.disallowFeedInRootFolder) {
|
|
|
|
menuItem.isEnabled = false
|
|
|
|
}
|
|
|
|
|
2019-05-01 21:03:59 +02:00
|
|
|
menu.addItem(menuItem)
|
|
|
|
|
|
|
|
let childNodes = childNode.childNodes
|
|
|
|
addFolderItemsToMenuWithNodes(menu: menu, nodes: childNodes, indentationLevel: 1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:23:23 +02:00
|
|
|
return menu
|
|
|
|
}
|
2018-09-22 21:46:24 +02:00
|
|
|
|
2019-05-02 00:33:08 +02:00
|
|
|
static func select(account: Account, folder: Folder?, in popupButton: NSPopUpButton) {
|
2018-09-22 21:46:24 +02:00
|
|
|
for menuItem in popupButton.itemArray {
|
2019-05-02 00:33:08 +02:00
|
|
|
if let oneAccount = menuItem.representedObject as? Account, oneAccount == account && folder == nil {
|
2018-09-22 21:46:24 +02:00
|
|
|
popupButton.select(menuItem)
|
|
|
|
return
|
|
|
|
}
|
2019-05-02 00:33:08 +02:00
|
|
|
if let oneFolder = menuItem.representedObject as? Folder, oneFolder == folder {
|
|
|
|
if oneFolder.account == account {
|
|
|
|
popupButton.select(menuItem)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 21:46:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:23:23 +02:00
|
|
|
private static func addFolderItemsToMenuWithNodes(menu: NSMenu, nodes: [Node], indentationLevel: Int) {
|
|
|
|
|
|
|
|
nodes.forEach { (oneNode) in
|
|
|
|
|
|
|
|
if let nameProvider = oneNode.representedObject as? DisplayNameProvider {
|
|
|
|
|
|
|
|
let menuItem = NSMenuItem(title: nameProvider.nameForDisplay, action: nil, keyEquivalent: "")
|
|
|
|
menuItem.indentationLevel = indentationLevel
|
|
|
|
menuItem.representedObject = oneNode.representedObject
|
|
|
|
menu.addItem(menuItem)
|
|
|
|
|
|
|
|
if oneNode.numberOfChildNodes > 0 {
|
|
|
|
addFolderItemsToMenuWithNodes(menu: menu, nodes: oneNode.childNodes, indentationLevel: indentationLevel + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|