2016-11-20 20:40:12 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import RxSwift
|
|
|
|
import PureLayout
|
|
|
|
|
2016-11-23 23:33:26 +03:00
|
|
|
enum BufferListAction {
|
|
|
|
|
|
|
|
case open(buffer: NeoVimBuffer)
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:42:11 +03:00
|
|
|
class BufferListComponent: ViewComponent, NSTableViewDataSource, NSTableViewDelegate {
|
|
|
|
|
2016-11-23 00:34:00 +03:00
|
|
|
fileprivate var buffers: [NeoVimBuffer] = []
|
|
|
|
fileprivate let bufferList = NSTableView.standardTableView()
|
2016-11-22 21:42:11 +03:00
|
|
|
|
2016-11-23 00:34:00 +03:00
|
|
|
fileprivate let fileItemService: FileItemService
|
|
|
|
fileprivate let genericIcon: NSImage
|
2016-11-20 20:40:12 +03:00
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:34:00 +03:00
|
|
|
init(source: Observable<Any>, fileItemService: FileItemService) {
|
|
|
|
self.fileItemService = fileItemService
|
|
|
|
self.genericIcon = fileItemService.icon(forType: "public.data")
|
|
|
|
|
2016-11-20 20:40:12 +03:00
|
|
|
super.init(source: source)
|
2016-11-22 21:42:11 +03:00
|
|
|
|
|
|
|
self.bufferList.dataSource = self
|
|
|
|
self.bufferList.delegate = self
|
2016-11-23 23:33:26 +03:00
|
|
|
self.bufferList.doubleAction = #selector(BufferListComponent.doubleClickAction)
|
2016-11-20 20:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override func addViews() {
|
2016-11-22 21:42:11 +03:00
|
|
|
let scrollView = NSScrollView.standardScrollView()
|
|
|
|
scrollView.borderType = .noBorder
|
|
|
|
scrollView.documentView = self.bufferList
|
|
|
|
|
|
|
|
self.addSubview(scrollView)
|
|
|
|
scrollView.autoPinEdgesToSuperviewEdges()
|
2016-11-20 20:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override func subscription(source: Observable<Any>) -> Disposable {
|
2016-11-22 22:57:50 +03:00
|
|
|
return source
|
|
|
|
.filter { $0 is MainWindowAction }
|
|
|
|
.map { $0 as! MainWindowAction }
|
|
|
|
.subscribe(onNext: { action in
|
|
|
|
switch action {
|
|
|
|
|
|
|
|
case let .changeBufferList(mainWindow:_, buffers:buffers):
|
|
|
|
self.buffers = buffers
|
|
|
|
self.bufferList.reloadData()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
2016-11-20 20:40:12 +03:00
|
|
|
}
|
2016-11-23 23:33:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Actions
|
|
|
|
extension BufferListComponent {
|
|
|
|
|
|
|
|
func doubleClickAction(_ sender: Any?) {
|
|
|
|
let clickedRow = self.bufferList.clickedRow
|
|
|
|
guard clickedRow >= 0 && clickedRow < self.buffers.count else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.publish(event: BufferListAction.open(buffer: self.buffers[clickedRow]))
|
|
|
|
}
|
2016-11-20 20:40:12 +03:00
|
|
|
}
|
2016-11-22 21:42:11 +03:00
|
|
|
|
|
|
|
// MARK: - NSTableViewDataSource
|
|
|
|
extension BufferListComponent {
|
|
|
|
|
|
|
|
@objc(numberOfRowsInTableView:)
|
|
|
|
func numberOfRows(in tableView: NSTableView) -> Int {
|
2016-11-22 22:57:50 +03:00
|
|
|
return self.buffers.count
|
2016-11-22 21:42:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDelegate
|
|
|
|
extension BufferListComponent {
|
|
|
|
|
2016-11-23 00:34:00 +03:00
|
|
|
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
let cachedCell = tableView.make(withIdentifier: "buffer-list-row", owner: self)
|
|
|
|
let cell = cachedCell as? ImageAndTextTableCell ?? ImageAndTextTableCell(withIdentifier: "buffer-list-row")
|
|
|
|
|
|
|
|
let buffer = self.buffers[row]
|
|
|
|
cell.text = buffer.name ?? "No Name"
|
|
|
|
cell.image = self.icon(forBuffer: buffer)
|
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func icon(forBuffer buffer: NeoVimBuffer) -> NSImage? {
|
|
|
|
if let fileName = buffer.fileName {
|
|
|
|
|
|
|
|
if let url = URL(string: fileName) {
|
|
|
|
return self.fileItemService.icon(forUrl: url)
|
|
|
|
} else {
|
|
|
|
return self.genericIcon
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return self.genericIcon
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 21:42:11 +03:00
|
|
|
}
|