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

116 lines
2.9 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import RxSwift
import PureLayout
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
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")
super.init(source: source)
2016-11-22 21:42:11 +03:00
self.bufferList.dataSource = self
self.bufferList.delegate = self
self.bufferList.doubleAction = #selector(BufferListComponent.doubleClickAction)
}
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()
}
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
}
})
}
}
// 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-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
}