2018-01-28 22:28:33 +01:00
|
|
|
//
|
2018-02-04 20:19:24 +01:00
|
|
|
// SidebarViewController+ContextualMenus.swift
|
2018-01-28 22:28:33 +01:00
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/28/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2018-01-28 22:28:33 +01:00
|
|
|
import Account
|
2018-01-29 01:09:18 +01:00
|
|
|
import RSCore
|
2018-01-28 22:28:33 +01:00
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
extension SidebarViewController {
|
2018-01-28 22:28:33 +01:00
|
|
|
|
|
|
|
func menu(for objects: [Any]?) -> NSMenu? {
|
|
|
|
|
|
|
|
guard let objects = objects, objects.count > 0 else {
|
2018-02-04 06:30:30 +01:00
|
|
|
return menuForNoSelection()
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-11 21:59:35 +01:00
|
|
|
if objects.count > 1 {
|
|
|
|
return menuForMultipleObjects(objects)
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-11 21:59:35 +01:00
|
|
|
let object = objects.first!
|
|
|
|
|
|
|
|
switch object {
|
|
|
|
case is Feed:
|
|
|
|
return menuForFeed(object as! Feed)
|
|
|
|
case is Folder:
|
|
|
|
return menuForFolder(object as! Folder)
|
|
|
|
case is PseudoFeed:
|
|
|
|
return menuForSmartFeed(object as! PseudoFeed)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Contextual Menu Actions
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
extension SidebarViewController {
|
2018-01-28 22:28:33 +01:00
|
|
|
|
|
|
|
@objc func openHomePageFromContextualMenu(_ sender: Any?) {
|
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
guard let menuItem = sender as? NSMenuItem, let urlString = menuItem.representedObject as? String else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Browser.open(urlString, inBackground: false)
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
@objc func copyURLFromContextualMenu(_ sender: Any?) {
|
2018-01-28 22:28:33 +01:00
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
guard let menuItem = sender as? NSMenuItem, let urlString = menuItem.representedObject as? String else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
URLPasteboardWriter.write(urlString: urlString, to: NSPasteboard.general)
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func markObjectsReadFromContextualMenu(_ sender: Any?) {
|
|
|
|
|
2018-02-04 20:45:51 +01:00
|
|
|
guard let menuItem = sender as? NSMenuItem, let objects = menuItem.representedObject as? [Any] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let articles = unreadArticles(for: objects)
|
2018-02-11 21:59:35 +01:00
|
|
|
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: Array(articles), markingRead: true, undoManager: undoManager) else {
|
2018-02-04 20:45:51 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
runCommand(markReadCommand)
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func deleteFromContextualMenu(_ sender: Any?) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func renameFromContextualMenu(_ sender: Any?) {
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
guard let window = view.window, let menuItem = sender as? NSMenuItem, let object = menuItem.representedObject as? DisplayNameProvider, object is Feed || object is Folder else {
|
2018-02-04 03:49:29 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-04 06:04:28 +01:00
|
|
|
renameWindowController = RenameWindowController(originalTitle: object.nameForDisplay, representedObject: object, delegate: self)
|
|
|
|
guard let renameSheet = renameWindowController?.window else {
|
|
|
|
return
|
2018-02-04 03:49:29 +01:00
|
|
|
}
|
2018-02-04 06:04:28 +01:00
|
|
|
window.beginSheet(renameSheet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
extension SidebarViewController: RenameWindowControllerDelegate {
|
2018-02-04 06:04:28 +01:00
|
|
|
|
|
|
|
func renameWindowController(_ windowController: RenameWindowController, didRenameObject object: Any, withNewName name: String) {
|
2018-02-04 03:49:29 +01:00
|
|
|
|
2018-02-04 06:04:28 +01:00
|
|
|
if let feed = object as? Feed {
|
|
|
|
feed.editedName = name
|
|
|
|
}
|
|
|
|
else if let folder = object as? Folder {
|
|
|
|
folder.name = name
|
|
|
|
}
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Build Contextual Menus
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
private extension SidebarViewController {
|
2018-01-28 22:28:33 +01:00
|
|
|
|
2018-02-04 06:30:30 +01:00
|
|
|
func menuForNoSelection() -> NSMenu {
|
|
|
|
|
|
|
|
let menu = NSMenu(title: "")
|
|
|
|
|
2018-02-04 20:19:24 +01:00
|
|
|
menu.addItem(withTitle: NSLocalizedString("New Feed", comment: "Command"), action: #selector(MainWindowController.showAddFeedWindow(_:)), keyEquivalent: "")
|
|
|
|
menu.addItem(withTitle: NSLocalizedString("New Folder", comment: "Command"), action: #selector(MainWindowController.showAddFolderWindow(_:)), keyEquivalent: "")
|
2018-02-04 06:30:30 +01:00
|
|
|
|
|
|
|
return menu
|
|
|
|
}
|
|
|
|
|
2018-01-28 22:28:33 +01:00
|
|
|
func menuForFeed(_ feed: Feed) -> NSMenu? {
|
|
|
|
|
|
|
|
let menu = NSMenu(title: "")
|
|
|
|
|
|
|
|
if feed.unreadCount > 0 {
|
|
|
|
menu.addItem(markAllReadMenuItem([feed]))
|
|
|
|
menu.addItem(NSMenuItem.separator())
|
|
|
|
}
|
|
|
|
|
|
|
|
if let homePageURL = feed.homePageURL, let _ = URL(string: homePageURL) {
|
|
|
|
let item = menuItem(NSLocalizedString("Open Home Page", comment: "Command"), #selector(openHomePageFromContextualMenu(_:)), homePageURL)
|
|
|
|
menu.addItem(item)
|
|
|
|
menu.addItem(NSMenuItem.separator())
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
let copyFeedURLItem = menuItem(NSLocalizedString("Copy Feed URL", comment: "Command"), #selector(copyURLFromContextualMenu(_:)), feed.url)
|
2018-01-28 22:28:33 +01:00
|
|
|
menu.addItem(copyFeedURLItem)
|
|
|
|
|
|
|
|
if let homePageURL = feed.homePageURL {
|
2018-01-29 01:09:18 +01:00
|
|
|
let item = menuItem(NSLocalizedString("Copy Home Page URL", comment: "Command"), #selector(copyURLFromContextualMenu(_:)), homePageURL)
|
2018-01-28 22:28:33 +01:00
|
|
|
menu.addItem(item)
|
|
|
|
}
|
|
|
|
menu.addItem(NSMenuItem.separator())
|
|
|
|
|
|
|
|
menu.addItem(renameMenuItem(feed))
|
2018-02-04 21:15:59 +01:00
|
|
|
// menu.addItem(deleteMenuItem([feed]))
|
2018-01-28 22:28:33 +01:00
|
|
|
|
|
|
|
return menu
|
|
|
|
}
|
|
|
|
|
|
|
|
func menuForFolder(_ folder: Folder) -> NSMenu? {
|
|
|
|
|
|
|
|
let menu = NSMenu(title: "")
|
|
|
|
|
|
|
|
if folder.unreadCount > 0 {
|
|
|
|
menu.addItem(markAllReadMenuItem([folder]))
|
|
|
|
menu.addItem(NSMenuItem.separator())
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
menu.addItem(renameMenuItem(folder))
|
2018-02-04 21:15:59 +01:00
|
|
|
// menu.addItem(deleteMenuItem([folder]))
|
2018-01-29 01:09:18 +01:00
|
|
|
|
2018-01-28 22:28:33 +01:00
|
|
|
return menu.numberOfItems > 0 ? menu : nil
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:59:35 +01:00
|
|
|
func menuForSmartFeed(_ smartFeed: PseudoFeed) -> NSMenu? {
|
2018-01-28 22:28:33 +01:00
|
|
|
|
2018-02-11 21:59:35 +01:00
|
|
|
let menu = NSMenu(title: "")
|
|
|
|
|
|
|
|
if smartFeed.unreadCount > 0 {
|
|
|
|
menu.addItem(markAllReadMenuItem([smartFeed]))
|
2018-01-29 01:09:18 +01:00
|
|
|
}
|
2018-02-11 21:59:35 +01:00
|
|
|
return menu.numberOfItems > 0 ? menu : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func menuForMultipleObjects(_ objects: [Any]) -> NSMenu? {
|
|
|
|
|
|
|
|
// guard allObjectsAreFeedsAndOrFolders(objects) else {
|
|
|
|
// return nil
|
|
|
|
// }
|
2018-01-29 01:09:18 +01:00
|
|
|
|
2018-01-28 22:28:33 +01:00
|
|
|
let menu = NSMenu(title: "")
|
|
|
|
|
|
|
|
if anyObjectInArrayHasNonZeroUnreadCount(objects) {
|
|
|
|
menu.addItem(markAllReadMenuItem(objects))
|
2018-02-04 21:15:59 +01:00
|
|
|
// menu.addItem(NSMenuItem.separator())
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-04 21:15:59 +01:00
|
|
|
// menu.addItem(deleteMenuItem(objects))
|
2018-01-28 22:28:33 +01:00
|
|
|
|
|
|
|
return menu.numberOfItems > 0 ? menu : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func markAllReadMenuItem(_ objects: [Any]) -> NSMenuItem {
|
|
|
|
|
|
|
|
return menuItem(NSLocalizedString("Mark All as Read", comment: "Command"), #selector(markObjectsReadFromContextualMenu(_:)), objects)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteMenuItem(_ objects: [Any]) -> NSMenuItem {
|
|
|
|
|
|
|
|
return menuItem(NSLocalizedString("Delete", comment: "Command"), #selector(deleteFromContextualMenu(_:)), objects)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameMenuItem(_ object: Any) -> NSMenuItem {
|
|
|
|
|
|
|
|
return menuItem(NSLocalizedString("Rename", comment: "Command"), #selector(renameFromContextualMenu(_:)), object)
|
|
|
|
}
|
|
|
|
|
|
|
|
func anyObjectInArrayHasNonZeroUnreadCount(_ objects: [Any]) -> Bool {
|
|
|
|
|
|
|
|
for object in objects {
|
|
|
|
if let unreadCountProvider = object as? UnreadCountProvider {
|
|
|
|
if unreadCountProvider.unreadCount > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-29 01:09:18 +01:00
|
|
|
func allObjectsAreFeedsAndOrFolders(_ objects: [Any]) -> Bool {
|
|
|
|
|
|
|
|
for object in objects {
|
|
|
|
if !objectIsFeedOrFolder(object) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func objectIsFeedOrFolder(_ object: Any) -> Bool {
|
|
|
|
|
|
|
|
return object is Feed || object is Folder
|
|
|
|
}
|
|
|
|
|
2018-01-28 22:28:33 +01:00
|
|
|
func menuItem(_ title: String, _ action: Selector, _ representedObject: Any) -> NSMenuItem {
|
|
|
|
|
|
|
|
let item = NSMenuItem(title: title, action: action, keyEquivalent: "")
|
|
|
|
item.representedObject = representedObject
|
|
|
|
item.target = self
|
|
|
|
return item
|
|
|
|
}
|
2018-02-04 20:45:51 +01:00
|
|
|
|
|
|
|
func unreadArticles(for objects: [Any]) -> Set<Article> {
|
|
|
|
|
|
|
|
var articles = Set<Article>()
|
|
|
|
for object in objects {
|
|
|
|
if let articleFetcher = object as? ArticleFetcher {
|
|
|
|
articles.formUnion(articleFetcher.fetchUnreadArticles())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return articles
|
|
|
|
}
|
2018-01-28 22:28:33 +01:00
|
|
|
}
|
|
|
|
|