2016-10-03 16:03:18 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
2016-11-25 17:25:55 +03:00
|
|
|
import PureLayout
|
2016-10-03 16:03:18 +03:00
|
|
|
import RxSwift
|
|
|
|
|
2017-02-24 00:15:35 +03:00
|
|
|
class FileOutlineView: NSOutlineView,
|
|
|
|
UiComponent,
|
|
|
|
NSOutlineViewDataSource,
|
|
|
|
NSOutlineViewDelegate {
|
2016-11-25 14:02:55 +03:00
|
|
|
|
2017-02-24 00:15:35 +03:00
|
|
|
typealias StateType = MainWindow.State
|
2016-11-25 14:02:55 +03:00
|
|
|
|
2017-02-24 00:15:35 +03:00
|
|
|
required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
|
2017-04-26 20:40:42 +03:00
|
|
|
self.emit = emitter.typedEmit()
|
2017-02-24 00:15:35 +03:00
|
|
|
self.uuid = state.uuid
|
2017-04-22 16:56:13 +03:00
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
self.root = FileBrowserItem(state.cwd)
|
2017-02-26 12:55:07 +03:00
|
|
|
self.isShowHidden = state.fileBrowserShowHidden
|
2016-11-25 14:02:55 +03:00
|
|
|
|
2017-02-24 00:15:35 +03:00
|
|
|
super.init(frame: .zero)
|
2016-11-25 19:03:31 +03:00
|
|
|
NSOutlineView.configure(toStandard: self)
|
2016-10-05 22:45:52 +03:00
|
|
|
|
|
|
|
self.dataSource = self
|
|
|
|
self.delegate = self
|
2016-11-12 18:42:10 +03:00
|
|
|
|
|
|
|
guard Bundle.main.loadNibNamed("FileBrowserMenu", owner: self, topLevelObjects: nil) else {
|
|
|
|
NSLog("WARN: FileBrowserMenu.xib could not be loaded")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-19 13:42:16 +03:00
|
|
|
// If the target of the menu items is set to the first responder, the actions are not invoked
|
|
|
|
// at all when the file monitor fires in the background...
|
|
|
|
// Dunno why it worked before the redesign... -_-
|
2017-02-26 12:12:41 +03:00
|
|
|
self.menu?.items.forEach { $0.target = self }
|
|
|
|
|
2016-11-12 18:42:10 +03:00
|
|
|
self.doubleAction = #selector(FileOutlineView.doubleClickAction)
|
2017-02-24 00:15:35 +03:00
|
|
|
|
|
|
|
source
|
2017-05-16 23:54:49 +03:00
|
|
|
.filter { !self.reloadData(for: $0) }
|
|
|
|
.filter { $0.lastFileSystemUpdate.mark != self.lastFileSystemUpdateMark }
|
2017-05-11 21:50:53 +03:00
|
|
|
.throttle(2 * FileMonitor.fileSystemEventsLatency + 1,
|
|
|
|
latest: true,
|
|
|
|
scheduler: SerialDispatchQueueScheduler(qos: .background))
|
2017-02-24 00:15:35 +03:00
|
|
|
.observeOn(MainScheduler.instance)
|
2017-05-15 20:18:51 +03:00
|
|
|
.subscribe(onNext: { state in
|
2017-05-12 00:33:59 +03:00
|
|
|
self.lastFileSystemUpdateMark = state.lastFileSystemUpdate.mark
|
2017-05-14 13:06:21 +03:00
|
|
|
self.update(state.lastFileSystemUpdate.payload)
|
2017-05-12 00:33:59 +03:00
|
|
|
})
|
|
|
|
.disposed(by: self.disposeBag)
|
|
|
|
|
|
|
|
source
|
|
|
|
.observeOn(MainScheduler.instance)
|
2017-05-15 20:18:51 +03:00
|
|
|
.subscribe(onNext: { state in
|
2017-03-31 20:32:01 +03:00
|
|
|
if state.viewToBeFocused != nil, case .fileBrowser = state.viewToBeFocused! {
|
2017-02-25 00:47:32 +03:00
|
|
|
self.beFirstResponder()
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:54:49 +03:00
|
|
|
guard self.reloadData(for: state) else {
|
2017-02-24 01:58:53 +03:00
|
|
|
return
|
|
|
|
}
|
2017-05-16 23:54:49 +03:00
|
|
|
|
|
|
|
self.isShowHidden = state.fileBrowserShowHidden
|
|
|
|
self.lastFileSystemUpdateMark = state.lastFileSystemUpdate.mark
|
|
|
|
self.root = FileBrowserItem(state.cwd)
|
|
|
|
self.reloadData()
|
2017-02-24 00:15:35 +03:00
|
|
|
})
|
2017-04-22 17:30:40 +03:00
|
|
|
.disposed(by: self.disposeBag)
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
2016-11-25 17:25:55 +03:00
|
|
|
|
2016-12-11 18:14:51 +03:00
|
|
|
func select(_ url: URL) {
|
2017-02-24 00:15:35 +03:00
|
|
|
var stack = [self.root]
|
2016-12-11 21:47:17 +03:00
|
|
|
|
|
|
|
while let item = stack.popLast() {
|
2017-05-19 13:57:04 +03:00
|
|
|
self.expandItem(item)
|
2016-12-11 21:47:17 +03:00
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
if item.url.isDirectParent(of: url) {
|
|
|
|
if let targetItem = item.children.first(where: { $0.url == url }) {
|
2017-05-19 13:57:04 +03:00
|
|
|
let targetRow = self.row(forItem: targetItem)
|
|
|
|
self.selectRowIndexes(IndexSet(integer: targetRow), byExtendingSelection: false)
|
|
|
|
self.scrollRowToVisible(targetRow)
|
2016-12-11 21:47:17 +03:00
|
|
|
}
|
2017-05-19 13:57:04 +03:00
|
|
|
|
2016-12-11 21:47:17 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
stack.append(contentsOf: item.children.filter { $0.url.isParent(of: url) })
|
2016-12-11 21:47:17 +03:00
|
|
|
}
|
2016-12-11 18:14:51 +03:00
|
|
|
}
|
|
|
|
|
2017-04-22 16:56:13 +03:00
|
|
|
fileprivate let emit: (UuidAction<FileBrowser.Action>) -> Void
|
2017-02-24 00:15:35 +03:00
|
|
|
fileprivate let disposeBag = DisposeBag()
|
|
|
|
|
|
|
|
fileprivate let uuid: String
|
2017-02-24 01:39:31 +03:00
|
|
|
fileprivate var lastFileSystemUpdateMark = Token()
|
2017-02-24 00:15:35 +03:00
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
fileprivate var cwd: URL {
|
|
|
|
return self.root.url
|
|
|
|
}
|
2017-02-26 13:00:52 +03:00
|
|
|
fileprivate var isShowHidden: Bool
|
|
|
|
|
2017-02-24 00:15:35 +03:00
|
|
|
fileprivate var root: FileBrowserItem
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:54:49 +03:00
|
|
|
fileprivate func reloadData(for state: StateType) -> Bool {
|
|
|
|
if self.isShowHidden != state.fileBrowserShowHidden {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if state.cwd != self.cwd {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
fileprivate func update(_ url: URL) {
|
2017-02-24 01:39:31 +03:00
|
|
|
guard let fileBrowserItem = self.fileBrowserItem(with: url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.update(fileBrowserItem)
|
|
|
|
}
|
|
|
|
|
2017-05-19 13:42:16 +03:00
|
|
|
fileprivate func handleRemovals(for fileBrowserItem: FileBrowserItem,
|
|
|
|
new newChildren: [FileBrowserItem]) {
|
2016-11-27 00:12:41 +03:00
|
|
|
let curChildren = fileBrowserItem.children
|
2016-11-26 17:13:21 +03:00
|
|
|
|
|
|
|
let curPreparedChildren = self.prepare(curChildren)
|
|
|
|
let newPreparedChildren = self.prepare(newChildren)
|
|
|
|
|
2017-05-19 11:25:50 +03:00
|
|
|
let indicesToRemove = curPreparedChildren
|
2017-02-24 00:15:35 +03:00
|
|
|
.enumerated()
|
2017-04-28 19:40:56 +03:00
|
|
|
.filter { (_, fileBrowserItem) in newPreparedChildren.contains(fileBrowserItem) == false }
|
|
|
|
.map { (idx, _) in idx }
|
2016-11-26 19:59:55 +03:00
|
|
|
|
2017-06-06 14:47:23 +03:00
|
|
|
logger.debug("\(fileBrowserItem): \(curPreparedChildren) vs. \(indicesToRemove)")
|
|
|
|
|
2016-11-26 17:13:21 +03:00
|
|
|
fileBrowserItem.children = curChildren.filter { newChildren.contains($0) }
|
|
|
|
|
2016-11-26 19:59:55 +03:00
|
|
|
let parent = fileBrowserItem == self.root ? nil : fileBrowserItem
|
2017-05-19 11:25:50 +03:00
|
|
|
self.removeItems(at: IndexSet(indicesToRemove), inParent: parent)
|
2016-11-26 19:59:55 +03:00
|
|
|
}
|
|
|
|
|
2017-05-19 13:42:16 +03:00
|
|
|
fileprivate func handleAdditions(for fileBrowserItem: FileBrowserItem,
|
|
|
|
new newChildren: [FileBrowserItem]) {
|
2016-11-27 00:12:41 +03:00
|
|
|
let curChildren = fileBrowserItem.children
|
2016-11-26 19:59:55 +03:00
|
|
|
|
|
|
|
let curPreparedChildren = self.prepare(curChildren)
|
|
|
|
let newPreparedChildren = self.prepare(newChildren)
|
|
|
|
|
2016-11-26 20:05:35 +03:00
|
|
|
let indicesToInsert = newPreparedChildren
|
2017-02-24 00:15:35 +03:00
|
|
|
.enumerated()
|
2017-05-19 11:25:50 +03:00
|
|
|
.filter { (_, fileBrowserItem) in curPreparedChildren.contains(fileBrowserItem) == false }
|
|
|
|
.map { (idx, _) in idx }
|
2016-11-26 19:59:55 +03:00
|
|
|
|
2017-06-06 14:47:23 +03:00
|
|
|
logger.debug("\(fileBrowserItem): \(curPreparedChildren) vs. \(indicesToInsert)")
|
|
|
|
|
|
|
|
// We don't just take newChildren because NSOutlineView look at the pointer equality for
|
|
|
|
// preserving the expanded states...
|
|
|
|
fileBrowserItem.children = newChildren.substituting(elements: curChildren)
|
|
|
|
|
2016-11-26 20:05:35 +03:00
|
|
|
let parent = fileBrowserItem == self.root ? nil : fileBrowserItem
|
|
|
|
self.insertItems(at: IndexSet(indicesToInsert), inParent: parent)
|
2016-11-26 19:59:55 +03:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
fileprivate func sortedChildren(of url: URL) -> [FileBrowserItem] {
|
|
|
|
return FileUtils.directDescendants(of: url).map(FileBrowserItem.init).sorted()
|
|
|
|
}
|
|
|
|
|
2016-11-26 19:59:55 +03:00
|
|
|
fileprivate func update(_ fileBrowserItem: FileBrowserItem) {
|
2017-05-14 01:03:20 +03:00
|
|
|
let url = fileBrowserItem.url
|
2016-11-26 19:59:55 +03:00
|
|
|
|
|
|
|
// Sort the array to keep the order.
|
2017-05-14 01:03:20 +03:00
|
|
|
let newChildren = self.sortedChildren(of: url)
|
2016-11-26 19:59:55 +03:00
|
|
|
|
2017-06-06 14:47:23 +03:00
|
|
|
self.beginUpdates()
|
2016-11-26 19:59:55 +03:00
|
|
|
self.handleRemovals(for: fileBrowserItem, new: newChildren)
|
2017-06-06 14:47:23 +03:00
|
|
|
self.endUpdates()
|
|
|
|
|
|
|
|
self.beginUpdates()
|
2016-11-26 19:59:55 +03:00
|
|
|
self.handleAdditions(for: fileBrowserItem, new: newChildren)
|
2017-06-06 14:47:23 +03:00
|
|
|
self.endUpdates()
|
|
|
|
|
2017-05-19 13:32:35 +03:00
|
|
|
fileBrowserItem.isChildrenScanned = true
|
|
|
|
|
2017-05-19 01:25:47 +03:00
|
|
|
fileBrowserItem.children.filter { self.isItemExpanded($0) }.forEach(self.update)
|
2016-11-25 23:41:25 +03:00
|
|
|
}
|
2016-11-25 17:25:55 +03:00
|
|
|
|
2016-11-25 23:41:25 +03:00
|
|
|
fileprivate func fileBrowserItem(with url: URL) -> FileBrowserItem? {
|
2016-11-26 12:34:52 +03:00
|
|
|
if self.cwd == url {
|
|
|
|
return self.root
|
|
|
|
}
|
|
|
|
|
2016-11-25 23:41:25 +03:00
|
|
|
guard self.cwd.isParent(of: url) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let rootPathComps = self.cwd.pathComponents
|
|
|
|
let pathComps = url.pathComponents
|
2017-02-24 00:15:35 +03:00
|
|
|
let childPart = pathComps[rootPathComps.count..<pathComps.count]
|
2016-11-25 23:17:24 +03:00
|
|
|
|
2016-11-25 23:41:25 +03:00
|
|
|
return childPart.reduce(self.root) { (resultItem, childName) -> FileBrowserItem? in
|
|
|
|
guard let parent = resultItem else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
return parent.child(with: parent.url.appendingPathComponent(childName))
|
2016-11-25 23:41:25 +03:00
|
|
|
}
|
2016-11-25 23:17:24 +03:00
|
|
|
}
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSOutlineViewDataSource
|
|
|
|
extension FileOutlineView {
|
|
|
|
|
2017-05-19 13:57:04 +03:00
|
|
|
fileprivate func scanChildrenIfNecessary(_ fileBrowserItem: FileBrowserItem) {
|
2017-05-19 13:32:35 +03:00
|
|
|
guard fileBrowserItem.isChildrenScanned == false else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileBrowserItem.children = self.sortedChildren(of: fileBrowserItem.url)
|
|
|
|
fileBrowserItem.isChildrenScanned = true
|
|
|
|
}
|
|
|
|
|
2016-11-26 12:34:52 +03:00
|
|
|
fileprivate func prepare(_ children: [FileBrowserItem]) -> [FileBrowserItem] {
|
2017-06-06 14:47:23 +03:00
|
|
|
return self.isShowHidden ? children : children.filter { !$0.isHidden }
|
2016-11-26 12:34:52 +03:00
|
|
|
}
|
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
func outlineView(_: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
|
|
|
|
if item == nil {
|
2017-05-19 13:32:35 +03:00
|
|
|
self.scanChildrenIfNecessary(self.root)
|
2016-11-25 14:02:55 +03:00
|
|
|
|
2016-11-26 12:34:52 +03:00
|
|
|
return self.prepare(self.root.children).count
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let fileBrowserItem = item as? FileBrowserItem else {
|
2016-10-05 22:45:52 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
if fileBrowserItem.url.isDir {
|
2017-05-19 13:32:35 +03:00
|
|
|
self.scanChildrenIfNecessary(fileBrowserItem)
|
2016-11-26 12:34:52 +03:00
|
|
|
return self.prepare(fileBrowserItem.children).count
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func outlineView(_: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
|
2016-11-25 19:03:31 +03:00
|
|
|
let level = self.level(forItem: item)
|
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
if item == nil {
|
2016-11-25 19:03:31 +03:00
|
|
|
self.adjustColumnWidth(for: self.root.children, outlineViewLevel: level)
|
2016-11-26 12:34:52 +03:00
|
|
|
return self.prepare(self.root.children)[index]
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let fileBrowserItem = item as? FileBrowserItem else {
|
2016-10-05 22:45:52 +03:00
|
|
|
preconditionFailure("Should not happen")
|
|
|
|
}
|
|
|
|
|
2016-11-25 19:03:31 +03:00
|
|
|
self.adjustColumnWidth(for: fileBrowserItem.children, outlineViewLevel: level)
|
2016-11-26 12:34:52 +03:00
|
|
|
return self.prepare(fileBrowserItem.children)[index]
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-26 12:34:52 +03:00
|
|
|
func outlineView(_: NSOutlineView, isItemExpandable item: Any) -> Bool {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let fileBrowserItem = item as? FileBrowserItem else {
|
2016-10-05 22:45:52 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
return fileBrowserItem.url.isDir
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-26 12:34:52 +03:00
|
|
|
@objc(outlineView: objectValueForTableColumn:byItem:)
|
2016-10-05 22:45:52 +03:00
|
|
|
func outlineView(_: NSOutlineView, objectValueFor: NSTableColumn?, byItem item: Any?) -> Any? {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let fileBrowserItem = item as? FileBrowserItem else {
|
2016-10-05 22:45:52 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-25 14:02:55 +03:00
|
|
|
return fileBrowserItem
|
2016-10-03 16:03:18 +03:00
|
|
|
}
|
2016-10-05 22:45:52 +03:00
|
|
|
|
2016-11-27 00:27:42 +03:00
|
|
|
fileprivate func adjustColumnWidth() {
|
|
|
|
let column = self.outlineTableColumn!
|
|
|
|
|
|
|
|
let rows = (0..<self.numberOfRows).map {
|
|
|
|
(item: self.item(atRow: $0) as! FileBrowserItem?, level: self.level(forRow: $0))
|
|
|
|
}
|
|
|
|
|
|
|
|
let cellWidth = rows.concurrentChunkMap(20) {
|
2017-05-11 22:11:34 +03:00
|
|
|
guard let fileBrowserItem = $0.item else {
|
|
|
|
return 0
|
|
|
|
}
|
2016-11-27 00:27:42 +03:00
|
|
|
|
2017-05-19 13:42:16 +03:00
|
|
|
// + 2 just to have a buffer... -_-
|
2017-05-14 01:03:20 +03:00
|
|
|
return ImageAndTextTableCell.width(with: fileBrowserItem.url.lastPathComponent)
|
2017-05-19 13:42:16 +03:00
|
|
|
+ (CGFloat($0.level + 2) * (self.indentationPerLevel + 2))
|
2017-05-11 22:11:34 +03:00
|
|
|
}.max() ?? column.width
|
2016-11-27 00:27:42 +03:00
|
|
|
|
|
|
|
guard column.minWidth != cellWidth else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
column.minWidth = cellWidth
|
|
|
|
column.maxWidth = cellWidth
|
|
|
|
}
|
|
|
|
|
2016-11-25 19:03:31 +03:00
|
|
|
fileprivate func adjustColumnWidth(for items: [FileBrowserItem], outlineViewLevel level: Int) {
|
2016-11-26 23:14:58 +03:00
|
|
|
let column = self.outlineTableColumn!
|
|
|
|
|
2016-11-27 00:12:41 +03:00
|
|
|
// It seems like that caching the widths is slower due to thread-safeness of NSCache...
|
2016-11-26 23:14:58 +03:00
|
|
|
let cellWidth = items.concurrentChunkMap(20) {
|
2017-05-14 01:03:20 +03:00
|
|
|
let result = ImageAndTextTableCell.width(with: $0.url.lastPathComponent)
|
2017-05-11 22:11:34 +03:00
|
|
|
return result
|
|
|
|
}.max() ?? column.width
|
2016-11-25 17:25:55 +03:00
|
|
|
|
2017-05-19 13:42:16 +03:00
|
|
|
// + 2 just to have a buffer... -_-
|
|
|
|
let width = cellWidth + (CGFloat(level + 2) * (self.indentationPerLevel + 2))
|
2016-11-26 23:14:58 +03:00
|
|
|
|
2016-11-25 19:03:31 +03:00
|
|
|
guard column.minWidth < width else {
|
2016-11-25 17:25:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-25 19:03:31 +03:00
|
|
|
column.minWidth = width
|
|
|
|
column.maxWidth = width
|
2016-11-25 17:25:55 +03:00
|
|
|
}
|
2016-11-25 19:03:31 +03:00
|
|
|
}
|
2016-11-25 17:25:55 +03:00
|
|
|
|
2016-11-25 19:03:31 +03:00
|
|
|
// MARK: - NSOutlineViewDelegate
|
|
|
|
extension FileOutlineView {
|
2016-11-25 17:25:55 +03:00
|
|
|
|
2016-11-26 12:34:52 +03:00
|
|
|
@objc(outlineView: viewForTableColumn:item:)
|
2016-10-05 22:45:52 +03:00
|
|
|
func outlineView(_: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let fileBrowserItem = item as? FileBrowserItem else {
|
2016-10-05 22:45:52 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-19 13:42:16 +03:00
|
|
|
let cachedCell =
|
|
|
|
(self.make(withIdentifier: "file-view-row", owner: self) as? ImageAndTextTableCell)?.reset()
|
2016-11-27 13:04:48 +03:00
|
|
|
let cell = cachedCell ?? ImageAndTextTableCell(withIdentifier: "file-view-row")
|
2016-10-05 22:45:52 +03:00
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
cell.text = fileBrowserItem.url.lastPathComponent
|
|
|
|
let icon = FileUtils.icon(forUrl: fileBrowserItem.url)
|
2017-06-06 14:47:23 +03:00
|
|
|
cell.image = fileBrowserItem.isHidden
|
2017-05-19 13:42:16 +03:00
|
|
|
? icon?.tinting(with: NSColor.white.withAlphaComponent(0.4))
|
|
|
|
: icon
|
2016-10-05 22:45:52 +03:00
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
func outlineView(_: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
|
|
|
|
return 20
|
|
|
|
}
|
2016-11-25 15:19:41 +03:00
|
|
|
|
|
|
|
func outlineViewItemDidCollapse(_ notification: Notification) {
|
2016-11-27 00:27:42 +03:00
|
|
|
self.adjustColumnWidth()
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:42:10 +03:00
|
|
|
// MARK: - Actions
|
|
|
|
extension FileOutlineView {
|
|
|
|
|
|
|
|
@IBAction func doubleClickAction(_: Any?) {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
if item.url.isDir {
|
2016-11-12 18:42:10 +03:00
|
|
|
self.toggle(item: item)
|
|
|
|
} else {
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .open(url: item.url, mode: .default))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func openInNewTab(_: Any?) {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .open(url: item.url, mode: .newTab))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func openInCurrentTab(_: Any?) {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .open(url: item.url, mode: .currentTab))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func openInHorizontalSplit(_: Any?) {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .open(url: item.url, mode: .horizontalSplit))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func openInVerticalSplit(_: Any?) {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .open(url: item.url, mode: .verticalSplit))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func setAsWorkingDirectory(_: Any?) {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
guard item.url.isDir else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .setAsWorkingDirectory(item.url))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSUserInterfaceValidations
|
|
|
|
extension FileOutlineView {
|
|
|
|
|
|
|
|
override func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let clickedItem = self.clickedItem as? FileBrowserItem else {
|
2016-11-12 18:42:10 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-14 00:53:57 +03:00
|
|
|
if item.action == #selector(setAsWorkingDirectory(_:)) {
|
2017-05-14 01:03:20 +03:00
|
|
|
return clickedItem.url.isDir
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
// MARK: - NSView
|
|
|
|
extension FileOutlineView {
|
2016-10-03 16:03:18 +03:00
|
|
|
|
|
|
|
override func keyDown(with event: NSEvent) {
|
|
|
|
guard let char = event.charactersIgnoringModifiers?.characters.first else {
|
|
|
|
super.keyDown(with: event)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-25 14:02:55 +03:00
|
|
|
guard let item = self.selectedItem as? FileBrowserItem else {
|
2016-10-03 16:03:18 +03:00
|
|
|
super.keyDown(with: event)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch char {
|
|
|
|
case " ", "\r": // Why "\r" and not "\n"?
|
2017-05-14 01:03:20 +03:00
|
|
|
if item.url.isDir || item.url.isPackage {
|
2016-10-03 16:03:18 +03:00
|
|
|
self.toggle(item: item)
|
|
|
|
} else {
|
2017-04-22 16:56:13 +03:00
|
|
|
self.emit(
|
2017-05-14 01:03:20 +03:00
|
|
|
UuidAction(uuid: self.uuid, action: .open(url: item.url, mode: .newTab))
|
2017-02-24 12:51:24 +03:00
|
|
|
)
|
2016-10-03 16:03:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
super.keyDown(with: event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 00:15:35 +03:00
|
|
|
|
|
|
|
fileprivate class FileBrowserItem: Hashable, Comparable, CustomStringConvertible {
|
|
|
|
|
|
|
|
static func ==(left: FileBrowserItem, right: FileBrowserItem) -> Bool {
|
2017-05-14 01:03:20 +03:00
|
|
|
return left.url == right.url
|
2017-02-24 00:15:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static func <(left: FileBrowserItem, right: FileBrowserItem) -> Bool {
|
2017-05-14 01:03:20 +03:00
|
|
|
return left.url.lastPathComponent < right.url.lastPathComponent
|
2017-02-24 00:15:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var hashValue: Int {
|
2017-05-14 01:03:20 +03:00
|
|
|
return self.url.hashValue
|
2017-02-24 00:15:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
2017-05-14 01:03:20 +03:00
|
|
|
return self.url.path
|
2017-02-24 00:15:35 +03:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
let url: URL
|
2017-06-06 14:47:23 +03:00
|
|
|
let isHidden: Bool
|
2017-02-24 00:15:35 +03:00
|
|
|
var children: [FileBrowserItem] = []
|
|
|
|
var isChildrenScanned = false
|
|
|
|
|
2017-05-14 01:03:20 +03:00
|
|
|
init(_ url: URL) {
|
|
|
|
self.url = url
|
2017-06-06 14:47:23 +03:00
|
|
|
|
|
|
|
// We cache the value here since we often get the value when the file is not there, eg when
|
|
|
|
// updating because the file gets deleted (in self.prepare() function)
|
|
|
|
self.isHidden = url.isHidden
|
2017-02-24 00:15:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func child(with url: URL) -> FileBrowserItem? {
|
2017-05-14 01:03:20 +03:00
|
|
|
return self.children.first { $0.url == url }
|
2017-02-24 00:15:35 +03:00
|
|
|
}
|
|
|
|
}
|