Allow inline editing, in the sidebar, only for Renamable-conforming items.

This commit is contained in:
Brent Simmons 2018-11-22 13:58:24 -08:00
parent b900cbc0b7
commit b33f992738
1 changed files with 28 additions and 16 deletions

View File

@ -7,6 +7,7 @@
// //
import Foundation import Foundation
import RSCore
import DB5 import DB5
import Account import Account
import RSTree import RSTree
@ -15,6 +16,12 @@ private var textSizeCache = [String: NSSize]()
class SidebarCell : NSTableCellView { class SidebarCell : NSTableCellView {
override var objectValue: Any? {
didSet {
didSetObjectValue()
}
}
var image: NSImage? { var image: NSImage? {
didSet { didSet {
if let image = image { if let image = image {
@ -76,16 +83,20 @@ class SidebarCell : NSTableCellView {
} }
} }
var node: Node? {
return objectValue as? Node
}
override var isFlipped: Bool { override var isFlipped: Bool {
return true return true
} }
private func commonInit() { private func commonInit() {
unreadCountView.translatesAutoresizingMaskIntoConstraints = false unreadCountView.translatesAutoresizingMaskIntoConstraints = false
imageView?.translatesAutoresizingMaskIntoConstraints = false imageView?.translatesAutoresizingMaskIntoConstraints = false
textField?.translatesAutoresizingMaskIntoConstraints = false textField?.translatesAutoresizingMaskIntoConstraints = false
addSubview(unreadCountView) addSubview(unreadCountView)
updateTextFieldIsEditable()
} }
override init(frame frameRect: NSRect) { override init(frame frameRect: NSRect) {
@ -115,29 +126,30 @@ class SidebarCell : NSTableCellView {
} }
@IBAction func editingEnded(_ sender: NSTextField) { @IBAction func editingEnded(_ sender: NSTextField) {
if let renamableItem = node?.representedObject as? Renamable {
guard let node = objectValue as? Node else { renamableItem.rename(to: sender.stringValue)
return
} }
if let feed = node.representedObject as? Feed {
feed.editedName = sender.stringValue
} }
else if let folder = node.representedObject as? Folder {
folder.name = sender.stringValue
}
}
} }
private extension SidebarCell { private extension SidebarCell {
func layoutWith(_ layout: SidebarCellLayout) { func layoutWith(_ layout: SidebarCellLayout) {
imageView?.rs_setFrameIfNotEqual(layout.faviconRect) imageView?.rs_setFrameIfNotEqual(layout.faviconRect)
textField?.rs_setFrameIfNotEqual(layout.titleRect) textField?.rs_setFrameIfNotEqual(layout.titleRect)
unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect) unreadCountView.rs_setFrameIfNotEqual(layout.unreadCountRect)
} }
func didSetObjectValue() {
updateTextFieldIsEditable()
}
func updateTextFieldIsEditable() {
var canRename = false
if let _ = node?.representedObject as? Renamable {
canRename = true
}
textField?.isEditable = canRename
}
} }