1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-29 16:56:40 +03:00
vimr/VimR/FileBrowserComponent.swift

128 lines
3.8 KiB
Swift
Raw Normal View History

2016-09-27 19:02:05 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import RxSwift
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
}
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
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
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)
self.fileItemService.sink
.filter { $0 is FileItemServiceChange }
.map { $0 as! FileItemServiceChange }
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [unowned self] action in
switch action {
case let .childrenChanged(root, fileItem):
guard root == self.cwd else {
return
}
// FIXME: restore expanded states
if fileItem?.url == self.cwd {
self.fileView.reloadItem(nil, reloadChildren: true)
}
guard self.fileView.row(forItem: fileItem) > -1 else {
return
}
2016-11-25 17:25:55 +03:00
// FIXME: restore expanded states
self.fileView.reloadItem(fileItem, reloadChildren: true)
}
})
.addDisposableTo(self.disposeBag)
2016-09-27 19:02:05 +03:00
}
override func addViews() {
let scrollView = NSScrollView.standardScrollView()
scrollView.borderType = .noBorder
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 }
.observeOn(MainScheduler.instance)
2016-09-27 19:02:05 +03:00
.subscribe(onNext: { [unowned self] action in
switch action {
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
}
})
}
}