Merge branch 'mac-candidate'

This commit is contained in:
Brent Simmons 2019-07-27 19:49:46 -07:00
commit 314fd970d1

View File

@ -43,7 +43,6 @@ protocol SidebarDelegate: class {
// MARK: - NSViewController
override func viewDidLoad() {
sidebarCellAppearance = SidebarCellAppearance(fontSize: AppDefaults.sidebarFontSize)
outlineView.dataSource = dataSource
@ -78,30 +77,9 @@ protocol SidebarDelegate: class {
}
}
// MARK: State Restoration
// private static let stateRestorationSelectedRowIndexes = "selectedRowIndexes"
//
// override func encodeRestorableState(with coder: NSCoder) {
//
// super.encodeRestorableState(with: coder)
//
// coder.encode(outlineView.selectedRowIndexes, forKey: SidebarViewController.stateRestorationSelectedRowIndexes)
// }
//
// override func restoreState(with coder: NSCoder) {
//
// super.restoreState(with: coder)
//
// if let restoredRowIndexes = coder.decodeObject(of: [NSIndexSet.self], forKey: SidebarViewController.stateRestorationSelectedRowIndexes) as? IndexSet {
// outlineView.selectRowIndexes(restoredRowIndexes, byExtendingSelection: false)
// }
// }
// MARK: - Notifications
@objc func unreadCountDidChange(_ note: Notification) {
guard let representedObject = note.object else {
return
}
@ -125,7 +103,6 @@ protocol SidebarDelegate: class {
}
@objc func userDidAddFeed(_ notification: Notification) {
guard let feed = notification.userInfo?[UserInfoKey.feed] else {
return
}
@ -133,12 +110,10 @@ protocol SidebarDelegate: class {
}
@objc func faviconDidBecomeAvailable(_ note: Notification) {
applyToAvailableCells(configureFavicon)
}
@objc func feedSettingDidChange(_ note: Notification) {
guard let feed = note.object as? Feed, let key = note.userInfo?[Feed.FeedSettingUserInfoKey] as? String else {
return
}
@ -148,7 +123,6 @@ protocol SidebarDelegate: class {
}
@objc func displayNameDidChange(_ note: Notification) {
guard let object = note.object else {
return
}
@ -159,7 +133,6 @@ protocol SidebarDelegate: class {
}
@objc func userDidRequestSidebarSelection(_ note: Notification) {
guard let feed = note.userInfo?[UserInfoKey.feed] else {
return
}
@ -175,7 +148,6 @@ protocol SidebarDelegate: class {
// MARK: - Actions
@IBAction func delete(_ sender: AnyObject?) {
if outlineView.selectionIsEmpty {
return
}
@ -183,7 +155,6 @@ protocol SidebarDelegate: class {
}
@IBAction func openInBrowser(_ sender: Any?) {
guard let feed = singleSelectedFeed, let homePageURL = feed.homePageURL else {
return
}
@ -191,29 +162,24 @@ protocol SidebarDelegate: class {
}
@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)
}
@IBAction func copy(_ sender: Any?) {
NSPasteboard.general.copyObjects(selectedObjects)
}
// MARK: - Navigation
func canGoToNextUnread() -> Bool {
if let _ = nextSelectableRowWithUnreadArticle() {
return true
}
@ -221,7 +187,6 @@ protocol SidebarDelegate: class {
}
func goToNextUnread() {
guard let row = nextSelectableRowWithUnreadArticle() else {
assertionFailure("goToNextUnread called before checking if there is a next unread.")
return
@ -230,26 +195,19 @@ protocol SidebarDelegate: class {
NSCursor.setHiddenUntilMouseMoves(true)
outlineView.selectRowIndexes(IndexSet([row]), byExtendingSelection: false)
outlineView.scrollTo(row: row)
}
func focus() {
guard let window = outlineView.window else {
return
}
window.makeFirstResponderUnlessDescendantIsFirstResponder(outlineView)
outlineView.window?.makeFirstResponderUnlessDescendantIsFirstResponder(outlineView)
}
// MARK: - Contextual Menu
func contextualMenuForSelectedObjects() -> NSMenu? {
return menu(for: selectedObjects)
}
func contextualMenuForClickedRows() -> NSMenu? {
let row = outlineView.clickedRow
guard row != -1, let node = nodeForRow(row) else {
return nil
@ -264,7 +222,7 @@ protocol SidebarDelegate: class {
return menu(for: [object])
}
// MARK: NSMenuDelegate
// MARK: - NSMenuDelegate
public func menuNeedsUpdate(_ menu: NSMenu) {
menu.removeAllItems()
@ -278,7 +236,6 @@ protocol SidebarDelegate: class {
// MARK: - NSOutlineViewDelegate
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
let node = item as! Node
if node.isGroupItem {
@ -294,13 +251,11 @@ protocol SidebarDelegate: class {
}
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
let node = item as! Node
return node.isGroupItem
}
func outlineView(_ outlineView: NSOutlineView, selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet {
// Dont 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).
@ -315,19 +270,16 @@ protocol SidebarDelegate: class {
}
func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
return !self.outlineView(outlineView, isGroupItem: item)
}
func outlineViewSelectionDidChange(_ notification: Notification) {
selectionDidChange(selectedObjects.isEmpty ? nil : selectedObjects)
// self.invalidateRestorableState()
}
//MARK: - Node Manipulation
func deleteNodes(_ nodes: [Node]) {
let nodesToDelete = treeController.normalizedSelectedNodes(nodes)
guard let undoManager = undoManager, let deleteCommand = DeleteCommand(nodesToDelete: nodesToDelete, treeController: treeController, undoManager: undoManager, errorHandler: ErrorHandler.present) else {
@ -374,7 +326,6 @@ protocol SidebarDelegate: class {
extension SidebarViewController: NSUserInterfaceValidations {
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.action == #selector(copy(_:)) {
return NSPasteboard.general.canCopyAtLeastOneObject(selectedObjects)
}
@ -412,7 +363,6 @@ private extension SidebarViewController {
}
func rebuildTreeAndReloadDataIfNeeded() {
if !animatingChanges && !BatchUpdate.shared.isPerforming {
treeController.rebuild()
outlineView.reloadData()
@ -444,7 +394,6 @@ private extension SidebarViewController {
}
func updateUnreadCounts(for objects: [AnyObject]) {
// On selection, update unread counts for folders and feeds.
// For feeds, actually fetch from database.
@ -459,7 +408,6 @@ private extension SidebarViewController {
}
func nodeForItem(_ item: AnyObject?) -> Node {
if item == nil {
return treeController.rootNode
}
@ -467,7 +415,6 @@ private extension SidebarViewController {
}
func nodeForRow(_ row: Int) -> Node? {
if row < 0 || row >= outlineView.numberOfRows {
return nil
}
@ -479,7 +426,6 @@ private extension SidebarViewController {
}
func rowHasAtLeastOneUnreadArticle(_ row: Int) -> Bool {
if let oneNode = nodeForRow(row) {
if let unreadCountProvider = oneNode.representedObject as? UnreadCountProvider {
if unreadCountProvider.unreadCount > 0 {
@ -491,7 +437,6 @@ private extension SidebarViewController {
}
func rowIsGroupItem(_ row: Int) -> Bool {
if let node = nodeForRow(row), outlineView.isGroupItem(node) {
return true
}
@ -499,7 +444,6 @@ private extension SidebarViewController {
}
func nextSelectableRowWithUnreadArticle() -> Int? {
// Skip group items, because they should never be selected.
let selectedRow = outlineView.selectedRow
@ -533,12 +477,10 @@ private extension SidebarViewController {
}
func configureUnreadCount(_ cell: SidebarCell, _ node: Node) {
cell.unreadCount = unreadCountFor(node)
}
func configureFavicon(_ cell: SidebarCell, _ node: Node) {
cell.image = imageFor(node)
}
@ -547,7 +489,6 @@ private extension SidebarViewController {
}
func imageFor(_ node: Node) -> NSImage? {
if let smallIconProvider = node.representedObject as? SmallIconProvider {
return smallIconProvider.smallIcon
}
@ -555,7 +496,6 @@ private extension SidebarViewController {
}
func nameFor(_ node: Node) -> String {
if let displayNameProvider = node.representedObject as? DisplayNameProvider {
return displayNameProvider.nameForDisplay
}
@ -563,7 +503,6 @@ private extension SidebarViewController {
}
func unreadCountFor(_ node: Node) -> Int {
if let unreadCountProvider = node.representedObject as? UnreadCountProvider {
return unreadCountProvider.unreadCount
}
@ -571,14 +510,11 @@ private extension SidebarViewController {
}
func cellForRowView(_ rowView: NSTableRowView) -> SidebarCell? {
return rowView.view(atColumn: 0) as? SidebarCell
}
func applyToAvailableCells(_ callback: (SidebarCell, Node) -> Void) {
outlineView.enumerateAvailableRowViews { (rowView: NSTableRowView, row: Int) -> Void in
guard let cell = cellForRowView(rowView), let node = nodeForRow(row) else {
return
}
@ -595,23 +531,19 @@ private extension SidebarViewController {
}
func configureCellsForRepresentedObject(_ representedObject: AnyObject) {
applyToCellsForRepresentedObject(representedObject, configure)
}
func configureUnreadCountForCellsForRepresentedObject(_ representedObject: AnyObject) {
applyToCellsForRepresentedObject(representedObject, configureUnreadCount)
}
@discardableResult
func revealAndSelectRepresentedObject(_ representedObject: AnyObject) -> Bool {
return outlineView.revealAndSelectRepresentedObject(representedObject, treeController)
}
}
private extension Node {
func representsSidebarObject(_ object: AnyObject) -> Bool {