Add a group-style row for accounts.

This commit is contained in:
Brent Simmons 2017-11-18 16:56:36 -08:00
parent ca081c41a9
commit 23ae585b12
7 changed files with 99 additions and 20 deletions

View File

@ -11,8 +11,21 @@ import RSTree
import Data
import RSCore
extension Node {
extension Array where Element == Node {
func sortedAlphabetically() -> [Node] {
return Node.nodesSortedAlphabetically(self)
}
func sortedAlphabeticallyWithFoldersAtEnd() -> [Node] {
return Node.nodesSortedAlphabeticallyWithFoldersAtEnd(self)
}
}
private extension Node {
class func nodesSortedAlphabetically(_ nodes: [Node]) -> [Node] {
return nodes.sorted { (node1, node2) -> Bool in
@ -50,3 +63,5 @@ extension Node {
}
}
}

View File

@ -61,7 +61,7 @@ private extension FeedListTreeControllerDelegate {
func childNodesForContainerNode(_ containerNode: Node, _ children: [AnyObject]) -> [Node]? {
let nodes = unsortedNodes(parent: containerNode, children: children)
return Node.nodesSortedAlphabeticallyWithFoldersAtEnd(nodes)
return nodes.sortedAlphabeticallyWithFoldersAtEnd()
}
func unsortedNodes(parent: Node, children: [AnyObject]) -> [Node] {

View File

@ -36,7 +36,7 @@ private extension FolderTreeControllerDelegate {
}
}
return Node.nodesSortedAlphabetically(folderNodes)
return folderNodes.sortedAlphabetically()
}
func createNode(_ folder: Folder, parent: Node) -> Node {

View File

@ -8,6 +8,7 @@
import AppKit
import RSCore
import RSTree
class SidebarOutlineView : NSOutlineView {
@ -56,7 +57,21 @@ class SidebarOutlineView : NSOutlineView {
super.keyDown(with: event)
}
}
override func frameOfCell(atColumn column: Int, row: Int) -> NSRect {
var frame = super.frameOfCell(atColumn: column, row: row)
let node = item(atRow: row) as! Node
if node.isGroupItem {
return frame
}
frame.origin.x -= indentationPerLevel
frame.size.width += indentationPerLevel
return frame
}
override func viewWillStartLiveResize() {
if let scrollView = self.enclosingScrollView {

View File

@ -21,6 +21,9 @@ final class SidebarTreeControllerDelegate: TreeControllerDelegate {
if node.representedObject is Folder {
return childNodesForFolderNode(node)
}
if node.representedObject is Account {
return childNodesForAccount(node)
}
return nil
}
@ -29,12 +32,17 @@ final class SidebarTreeControllerDelegate: TreeControllerDelegate {
private extension SidebarTreeControllerDelegate {
func childNodesForRootNode(_ rootNode: Node) -> [Node]? {
// The child nodes are the top-level items of the local Account.
// This will be expanded later to add synthetic feeds (All Unread, for instance)
// and other accounts.
return childNodesForContainerNode(rootNode, AccountManager.shared.localAccount.children)
// The top-level nodes are pseudo-feeds (All Unread, Starred, etc.) and accounts.
// TODO: pseudo-feeds
return sortedAccountNodes(rootNode)
}
func childNodesForAccount(_ accountNode: Node) -> [Node]? {
let account = accountNode.representedObject as! Account
return childNodesForContainerNode(accountNode, account.children)
}
func childNodesForFolderNode(_ folderNode: Node) -> [Node]? {
@ -61,8 +69,7 @@ private extension SidebarTreeControllerDelegate {
}
}
updatedChildNodes = Node.nodesSortedAlphabeticallyWithFoldersAtEnd(updatedChildNodes)
return updatedChildNodes
return updatedChildNodes.sortedAlphabeticallyWithFoldersAtEnd()
}
func createNode(representedObject: Any, parent: Node) -> Node? {
@ -73,21 +80,39 @@ private extension SidebarTreeControllerDelegate {
if let folder = representedObject as? Folder {
return createNode(folder: folder, parent: parent)
}
if let account = representedObject as? Account {
return createNode(account: account, parent: parent)
}
return nil
}
func createNode(feed: Feed, parent: Node) -> Node {
return Node(representedObject: feed, parent: parent)
return parent.createChildNode(feed)
}
func createNode(folder: Folder, parent: Node) -> Node {
let node = Node(representedObject: folder, parent: parent)
let node = parent.createChildNode(folder)
node.canHaveChildNodes = true
return node
}
func createNode(account: Account, parent: Node) -> Node {
let node = parent.createChildNode(account)
node.canHaveChildNodes = true
node.isGroupItem = true
return node
}
func sortedAccountNodes(_ parent: Node) -> [Node] {
let nodes = AccountManager.shared.accounts.map { createNode(account: $0, parent: parent) }
return nodes.sortedAlphabetically()
}
func nodeInArrayRepresentingObject(_ nodes: [Node], _ representedObject: AnyObject) -> Node? {
for oneNode in nodes {

View File

@ -118,14 +118,26 @@ import RSCore
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
let cell = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DataCell"), owner: self) as! SidebarCell
let node = item as! Node
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
configure(cell, node)
return cell
}
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
let node = item as! Node
return node.isGroupItem
}
func outlineViewSelectionDidChange(_ notification: Notification) {
// TODO: support multiple selection
@ -283,6 +295,12 @@ private extension SidebarViewController {
cell.image = imageFor(node)
}
func configureGroupCell(_ cell: NSTableCellView, _ node: Node) {
cell.objectValue = node
cell.textField?.stringValue = nameFor(node)
}
func imageFor(_ node: Node) -> NSImage? {
return nil

View File

@ -71,7 +71,13 @@ public final class Node: Equatable {
node.canHaveChildNodes = true
return node
}
public func createChildNode(_ representedObject: AnyObject) -> Node {
// Just creates doesnt add it.
return Node(representedObject: representedObject, parent: self)
}
public func childAtIndex(_ index: Int) -> Node? {
guard let childNodes = childNodes else {