2016-09-27 19:02:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import RxSwift
|
2016-11-20 20:40:12 +03:00
|
|
|
import PureLayout
|
2016-09-27 19:02:05 +03:00
|
|
|
|
2016-10-02 15:07:12 +03:00
|
|
|
enum FileBrowserAction {
|
|
|
|
|
|
|
|
case open(url: URL)
|
2016-11-12 18:42:10 +03:00
|
|
|
case openInNewTab(url: URL)
|
|
|
|
case openInCurrentTab(url: URL)
|
|
|
|
case openInHorizontalSplit(url: URL)
|
|
|
|
case openInVerticalSplit(url: URL)
|
|
|
|
case setAsWorkingDirectory(url: URL)
|
|
|
|
case setParentAsWorkingDirectory(url: URL)
|
2016-10-02 15:07:12 +03:00
|
|
|
}
|
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
class FileBrowserComponent: ViewComponent {
|
2016-09-27 19:02:05 +03:00
|
|
|
|
2016-10-03 16:03:18 +03:00
|
|
|
fileprivate let fileView: FileOutlineView
|
2016-09-27 19:02:05 +03:00
|
|
|
fileprivate let fileItemService: FileItemService
|
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
fileprivate var cwd: URL {
|
|
|
|
get {
|
|
|
|
return self.fileView.cwd
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
self.fileView.cwd = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2016-10-03 16:03:18 +03:00
|
|
|
override var isFirstResponder: Bool {
|
|
|
|
return self.fileView.isFirstResponder
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
init(source: Observable<Any>, fileItemService: FileItemService) {
|
|
|
|
self.fileItemService = fileItemService
|
2016-10-05 22:45:52 +03:00
|
|
|
self.fileView = FileOutlineView(source: source, fileItemService: fileItemService)
|
2016-11-12 18:42:10 +03:00
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
super.init(source: source)
|
2016-10-03 16:03:18 +03:00
|
|
|
self.addReactions()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func beFirstResponder() {
|
|
|
|
self.window?.makeFirstResponder(self.fileView)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func addReactions() {
|
|
|
|
self.fileView.sink
|
|
|
|
.filter { $0 is FileOutlineViewAction }
|
|
|
|
.map { $0 as! FileOutlineViewAction }
|
2016-11-12 18:42:10 +03:00
|
|
|
.map {
|
|
|
|
switch $0 {
|
|
|
|
case let .open(fileItem): return FileBrowserAction.open(url: fileItem.url)
|
|
|
|
case let .openFileInNewTab(fileItem): return FileBrowserAction.openInNewTab(url: fileItem.url)
|
|
|
|
case let .openFileInCurrentTab(fileItem): return FileBrowserAction.openInCurrentTab(url: fileItem.url)
|
|
|
|
case let .openFileInHorizontalSplit(fileItem): return FileBrowserAction.openInHorizontalSplit(url: fileItem.url)
|
|
|
|
case let .openFileInVerticalSplit(fileItem): return FileBrowserAction.openInVerticalSplit(url: fileItem.url)
|
|
|
|
case let .setAsWorkingDirectory(fileItem): return FileBrowserAction.setAsWorkingDirectory(url: fileItem.url)
|
|
|
|
case let .setParentAsWorkingDirectory(fileItem):
|
|
|
|
return FileBrowserAction.setParentAsWorkingDirectory(url: fileItem.url)
|
2016-10-03 16:03:18 +03:00
|
|
|
}
|
2016-11-12 18:42:10 +03:00
|
|
|
}
|
|
|
|
.subscribe(onNext: { [unowned self] action in self.publish(event: action) })
|
2016-10-03 16:03:18 +03:00
|
|
|
.addDisposableTo(self.disposeBag)
|
2016-10-04 01:23:54 +03:00
|
|
|
|
|
|
|
self.fileItemService.sink
|
|
|
|
.filter { $0 is FileItemServiceChange }
|
|
|
|
.map { $0 as! FileItemServiceChange }
|
2016-11-03 21:57:19 +03:00
|
|
|
.observeOn(MainScheduler.instance)
|
2016-10-04 01:23:54 +03:00
|
|
|
.subscribe(onNext: { [unowned self] action in
|
|
|
|
switch action {
|
|
|
|
case let .childrenChanged(root, fileItem):
|
|
|
|
guard root == self.cwd else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
// FIXME: restore expanded states
|
|
|
|
if fileItem?.url == self.cwd {
|
2016-11-03 21:57:19 +03:00
|
|
|
self.fileView.reloadItem(nil, reloadChildren: true)
|
2016-10-05 22:45:52 +03:00
|
|
|
}
|
|
|
|
|
2016-10-05 00:34:02 +03:00
|
|
|
guard self.fileView.row(forItem: fileItem) > -1 else {
|
|
|
|
return
|
|
|
|
}
|
2016-11-25 17:25:55 +03:00
|
|
|
|
2016-10-05 22:45:52 +03:00
|
|
|
// FIXME: restore expanded states
|
2016-11-03 21:57:19 +03:00
|
|
|
self.fileView.reloadItem(fileItem, reloadChildren: true)
|
2016-10-04 01:23:54 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.addDisposableTo(self.disposeBag)
|
2016-09-27 19:02:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override func addViews() {
|
|
|
|
let scrollView = NSScrollView.standardScrollView()
|
|
|
|
scrollView.borderType = .noBorder
|
2016-11-25 19:03:31 +03:00
|
|
|
scrollView.documentView = self.fileView
|
2016-09-27 19:02:05 +03:00
|
|
|
|
|
|
|
self.addSubview(scrollView)
|
|
|
|
scrollView.autoPinEdgesToSuperviewEdges()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func subscription(source: Observable<Any>) -> Disposable {
|
|
|
|
return source
|
|
|
|
.filter { $0 is MainWindowAction }
|
|
|
|
.map { $0 as! MainWindowAction }
|
2016-11-03 21:57:19 +03:00
|
|
|
.observeOn(MainScheduler.instance)
|
2016-09-27 19:02:05 +03:00
|
|
|
.subscribe(onNext: { [unowned self] action in
|
|
|
|
switch action {
|
2016-11-22 01:17:24 +03:00
|
|
|
case let .changeCwd(mainWindow: _, cwd: cwd):
|
|
|
|
self.cwd = cwd
|
2016-10-03 20:35:58 +03:00
|
|
|
// NSLog("cwd changed to \(self.cwd) of \(mainWindow.uuid)")
|
2016-09-27 19:02:05 +03:00
|
|
|
self.fileView.reloadData()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|