2016-08-30 23:25:34 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import PureLayout
|
|
|
|
import RxSwift
|
2016-09-03 11:17:22 +03:00
|
|
|
import RxCocoa
|
2016-08-30 23:25:34 +03:00
|
|
|
|
2016-09-01 21:10:40 +03:00
|
|
|
class OpenQuicklyWindowComponent: WindowComponent, NSWindowDelegate, NSTableViewDelegate, NSTableViewDataSource {
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
let scanCondition = NSCondition()
|
|
|
|
var pauseScan = false
|
|
|
|
|
|
|
|
private(set) var pattern = ""
|
|
|
|
private(set) var cwd = NSURL(fileURLWithPath: NSHomeDirectory(), isDirectory: true) {
|
|
|
|
didSet {
|
|
|
|
self.cwdPathCompsCount = self.cwd.pathComponents!.count
|
|
|
|
self.cwdControl.URL = self.cwd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private(set) var flatFileItems = [FileItem]()
|
|
|
|
private(set) var fileViewItems = [ScoredFileItem]()
|
|
|
|
|
|
|
|
private let userInitiatedScheduler = ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .UserInitiated)
|
2016-09-02 19:22:40 +03:00
|
|
|
|
|
|
|
private let searchField = NSTextField(forAutoLayout: ())
|
2016-09-05 20:15:54 +03:00
|
|
|
private let progressIndicator = NSProgressIndicator(forAutoLayout: ())
|
2016-09-03 00:35:18 +03:00
|
|
|
private let cwdControl = NSPathControl(forAutoLayout: ())
|
2016-09-03 11:17:22 +03:00
|
|
|
private let countField = NSTextField(forAutoLayout: ())
|
|
|
|
private let fileView = NSTableView.standardSourceListTableView()
|
|
|
|
|
|
|
|
private let fileItemService: FileItemService
|
|
|
|
|
|
|
|
private var count = 0
|
2016-09-05 20:15:54 +03:00
|
|
|
private var perSessionDisposeBag = DisposeBag()
|
2016-08-30 23:25:34 +03:00
|
|
|
|
2016-09-04 00:13:52 +03:00
|
|
|
private var cwdPathCompsCount = 0
|
2016-09-05 20:15:54 +03:00
|
|
|
private let searchStream: Observable<String>
|
2016-09-04 00:13:52 +03:00
|
|
|
private let filterOpQueue = NSOperationQueue()
|
2016-09-03 00:57:39 +03:00
|
|
|
|
2016-09-03 11:17:22 +03:00
|
|
|
init(source: Observable<Any>, fileItemService: FileItemService) {
|
|
|
|
self.fileItemService = fileItemService
|
2016-09-05 20:15:54 +03:00
|
|
|
self.searchStream = self.searchField.rx_text
|
|
|
|
.throttle(0.2, scheduler: MainScheduler.instance)
|
|
|
|
.distinctUntilChanged()
|
2016-09-03 11:17:22 +03:00
|
|
|
|
2016-08-30 23:25:34 +03:00
|
|
|
super.init(source: source, nibName: "OpenQuicklyWindow")
|
2016-09-01 21:10:40 +03:00
|
|
|
|
|
|
|
self.window.delegate = self
|
2016-09-04 00:13:52 +03:00
|
|
|
self.filterOpQueue.qualityOfService = .UserInitiated
|
|
|
|
self.filterOpQueue.name = "open-quickly-filter-operation-queue"
|
2016-08-30 23:25:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override func addViews() {
|
2016-09-03 00:44:05 +03:00
|
|
|
let searchField = self.searchField
|
2016-09-05 20:15:54 +03:00
|
|
|
let progressIndicator = self.progressIndicator
|
|
|
|
progressIndicator.indeterminate = true
|
|
|
|
progressIndicator.displayedWhenStopped = false
|
|
|
|
progressIndicator.style = .SpinningStyle
|
|
|
|
progressIndicator.controlSize = .SmallControlSize
|
2016-09-03 00:44:05 +03:00
|
|
|
|
2016-09-03 11:17:22 +03:00
|
|
|
let fileView = self.fileView
|
2016-09-04 00:13:52 +03:00
|
|
|
fileView.intercellSpacing = CGSize(width: 4, height: 4)
|
2016-09-03 00:44:05 +03:00
|
|
|
fileView.setDataSource(self)
|
|
|
|
fileView.setDelegate(self)
|
2016-09-04 00:13:52 +03:00
|
|
|
|
2016-09-03 00:55:19 +03:00
|
|
|
let fileScrollView = NSScrollView.standardScrollView()
|
2016-09-03 11:17:22 +03:00
|
|
|
fileScrollView.autoresizesSubviews = true
|
2016-09-03 00:44:05 +03:00
|
|
|
fileScrollView.documentView = fileView
|
|
|
|
|
2016-09-03 00:35:18 +03:00
|
|
|
let cwdControl = self.cwdControl
|
|
|
|
cwdControl.pathStyle = .Standard
|
|
|
|
cwdControl.backgroundColor = NSColor.clearColor()
|
|
|
|
cwdControl.refusesFirstResponder = true
|
|
|
|
cwdControl.cell?.controlSize = .SmallControlSize
|
|
|
|
cwdControl.cell?.font = NSFont.systemFontOfSize(NSFont.smallSystemFontSize())
|
|
|
|
cwdControl.setContentCompressionResistancePriority(NSLayoutPriorityDefaultLow, forOrientation:.Horizontal)
|
|
|
|
|
2016-09-03 11:17:22 +03:00
|
|
|
let countField = self.countField
|
|
|
|
countField.editable = false
|
|
|
|
countField.bordered = false
|
|
|
|
countField.alignment = .Right
|
|
|
|
countField.backgroundColor = NSColor.clearColor()
|
|
|
|
countField.stringValue = "0 items"
|
|
|
|
|
2016-09-03 00:44:05 +03:00
|
|
|
let contentView = self.window.contentView!
|
|
|
|
contentView.addSubview(searchField)
|
2016-09-05 20:15:54 +03:00
|
|
|
contentView.addSubview(progressIndicator)
|
2016-09-03 00:44:05 +03:00
|
|
|
contentView.addSubview(fileScrollView)
|
|
|
|
contentView.addSubview(cwdControl)
|
2016-09-03 11:17:22 +03:00
|
|
|
contentView.addSubview(countField)
|
2016-09-01 21:10:40 +03:00
|
|
|
|
2016-09-03 00:35:18 +03:00
|
|
|
searchField.autoPinEdgeToSuperviewEdge(.Top, withInset: 18)
|
|
|
|
searchField.autoPinEdgeToSuperviewEdge(.Right, withInset: 18)
|
|
|
|
searchField.autoPinEdgeToSuperviewEdge(.Left, withInset: 18)
|
|
|
|
|
2016-09-05 20:15:54 +03:00
|
|
|
progressIndicator.autoAlignAxis(.Horizontal, toSameAxisOfView: searchField)
|
|
|
|
progressIndicator.autoPinEdge(.Right, toEdge: .Right, ofView: searchField, withOffset: -4)
|
|
|
|
|
2016-09-03 00:44:05 +03:00
|
|
|
fileScrollView.autoPinEdge(.Top, toEdge: .Bottom, ofView: searchField, withOffset: 18)
|
|
|
|
fileScrollView.autoPinEdge(.Right, toEdge: .Right, ofView: searchField)
|
|
|
|
fileScrollView.autoPinEdge(.Left, toEdge: .Left, ofView: searchField)
|
|
|
|
fileScrollView.autoSetDimension(.Height, toSize: 300)
|
|
|
|
|
2016-09-03 11:17:22 +03:00
|
|
|
cwdControl.autoPinEdge(.Top, toEdge: .Bottom, ofView: fileScrollView, withOffset: 18)
|
|
|
|
cwdControl.autoPinEdgeToSuperviewEdge(.Left, withInset: 18)
|
2016-09-03 00:35:18 +03:00
|
|
|
cwdControl.autoPinEdgeToSuperviewEdge(.Bottom, withInset: 18)
|
2016-09-03 11:17:22 +03:00
|
|
|
|
|
|
|
countField.autoPinEdge(.Top, toEdge: .Bottom, ofView: fileScrollView, withOffset: 18)
|
|
|
|
countField.autoPinEdgeToSuperviewEdge(.Right, withInset: 18)
|
|
|
|
countField.autoPinEdge(.Left, toEdge: .Right, ofView: cwdControl)
|
2016-08-30 23:25:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override func subscription(source source: Observable<Any>) -> Disposable {
|
|
|
|
return NopDisposable.instance
|
|
|
|
}
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
func reloadFileView(withScoredItems items: [ScoredFileItem]) {
|
|
|
|
self.fileViewItems = items
|
|
|
|
self.fileView.reloadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
func startProgress() {
|
|
|
|
self.progressIndicator.startAnimation(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func endProgress() {
|
|
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
}
|
2016-09-02 19:22:40 +03:00
|
|
|
|
2016-09-03 00:35:18 +03:00
|
|
|
func show(forMainWindow mainWindow: MainWindowComponent) {
|
2016-09-03 00:57:39 +03:00
|
|
|
self.cwd = mainWindow.cwd
|
2016-09-03 11:17:22 +03:00
|
|
|
let flatFiles = self.fileItemService.flatFileItems(ofUrl: self.cwd)
|
|
|
|
.subscribeOn(self.userInitiatedScheduler)
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
self.searchStream
|
|
|
|
.subscribe(onNext: { [unowned self] pattern in
|
|
|
|
self.pattern = pattern
|
|
|
|
self.resetAndAddFilterOperation()
|
|
|
|
})
|
|
|
|
.addDisposableTo(self.perSessionDisposeBag)
|
2016-09-03 11:17:22 +03:00
|
|
|
|
|
|
|
flatFiles
|
2016-09-05 20:15:54 +03:00
|
|
|
.subscribeOn(self.userInitiatedScheduler)
|
|
|
|
.doOnNext{ [unowned self] items in
|
|
|
|
self.scanCondition.lock()
|
|
|
|
while self.pauseScan {
|
|
|
|
self.scanCondition.wait()
|
|
|
|
}
|
|
|
|
self.scanCondition.unlock()
|
|
|
|
|
|
|
|
self.flatFileItems.appendContentsOf(items)
|
|
|
|
self.resetAndAddFilterOperation()
|
|
|
|
}
|
2016-09-03 11:17:22 +03:00
|
|
|
.observeOn(MainScheduler.instance)
|
2016-09-04 00:13:52 +03:00
|
|
|
.subscribe(onNext: { [unowned self] items in
|
2016-09-03 11:17:22 +03:00
|
|
|
self.count += items.count
|
|
|
|
self.countField.stringValue = "\(self.count) items"
|
2016-09-04 00:13:52 +03:00
|
|
|
})
|
2016-09-05 20:15:54 +03:00
|
|
|
.addDisposableTo(self.perSessionDisposeBag)
|
2016-09-03 11:17:22 +03:00
|
|
|
|
2016-09-03 00:35:18 +03:00
|
|
|
self.show()
|
2016-09-02 19:22:40 +03:00
|
|
|
self.searchField.becomeFirstResponder()
|
|
|
|
}
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
private func resetAndAddFilterOperation() {
|
|
|
|
self.filterOpQueue.cancelAllOperations()
|
|
|
|
let op = OpenQuicklyFilterOperation(forOpenQuicklyWindow: self)
|
|
|
|
self.filterOpQueue.addOperation(op)
|
|
|
|
}
|
2016-08-30 23:25:34 +03:00
|
|
|
}
|
2016-09-03 00:44:05 +03:00
|
|
|
|
|
|
|
// MARK: - NSTableViewDataSource
|
|
|
|
extension OpenQuicklyWindowComponent {
|
|
|
|
|
|
|
|
func numberOfRowsInTableView(_: NSTableView) -> Int {
|
2016-09-05 20:15:54 +03:00
|
|
|
return self.fileViewItems.count
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
func tableView(tableView: NSTableView, viewForTableColumn _: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
let cachedCell = tableView.makeViewWithIdentifier("file-view-row", owner: self)
|
|
|
|
let cell = cachedCell as? ImageAndTextTableCell ?? ImageAndTextTableCell(withIdentifier: "file-view-row")
|
|
|
|
|
|
|
|
let url = self.fileViewItems[row].url
|
|
|
|
cell.textField.attributedStringValue = self.rowText(forUrl: url)
|
2016-09-04 00:13:52 +03:00
|
|
|
cell.imageView.image = self.fileItemService.icon(forUrl: url)
|
|
|
|
|
|
|
|
return cell
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
2016-09-04 00:13:52 +03:00
|
|
|
|
2016-09-05 20:15:54 +03:00
|
|
|
private func rowText(forUrl url: NSURL) -> NSAttributedString {
|
|
|
|
let pathComps = url.pathComponents!
|
|
|
|
let truncatedPathComps = pathComps[self.cwdPathCompsCount..<pathComps.count]
|
|
|
|
let name = truncatedPathComps.last!
|
|
|
|
|
|
|
|
if truncatedPathComps.dropLast().isEmpty {
|
|
|
|
return NSMutableAttributedString(string: name)
|
|
|
|
}
|
|
|
|
|
|
|
|
let rowText: NSMutableAttributedString
|
|
|
|
let pathInfo = truncatedPathComps.dropLast().reverse().joinWithSeparator(" / ")
|
|
|
|
rowText = NSMutableAttributedString(string: "\(name) — \(pathInfo)")
|
|
|
|
rowText.addAttribute(NSForegroundColorAttributeName,
|
|
|
|
value: NSColor.lightGrayColor(),
|
|
|
|
range: NSRange(location:name.characters.count,
|
|
|
|
length: pathInfo.characters.count + 3))
|
|
|
|
|
|
|
|
return rowText
|
|
|
|
}
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDelegate
|
|
|
|
extension OpenQuicklyWindowComponent {
|
|
|
|
|
|
|
|
func tableViewSelectionDidChange(_: NSNotification) {
|
2016-09-04 00:13:52 +03:00
|
|
|
// NSLog("\(#function): selection changed")
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-03 11:17:22 +03:00
|
|
|
|
|
|
|
// MARK: - NSWindowDelegate
|
|
|
|
extension OpenQuicklyWindowComponent {
|
|
|
|
|
2016-09-04 00:13:52 +03:00
|
|
|
func windowWillClose(notification: NSNotification) {
|
2016-09-05 20:15:54 +03:00
|
|
|
self.filterOpQueue.cancelAllOperations()
|
|
|
|
|
|
|
|
self.endProgress()
|
|
|
|
|
|
|
|
self.perSessionDisposeBag = DisposeBag()
|
|
|
|
self.pauseScan = false
|
2016-09-03 11:17:22 +03:00
|
|
|
self.count = 0
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
self.pattern = ""
|
|
|
|
self.flatFileItems = []
|
|
|
|
self.fileViewItems = []
|
2016-09-04 00:13:52 +03:00
|
|
|
|
|
|
|
self.searchField.stringValue = ""
|
|
|
|
self.countField.stringValue = "0 items"
|
2016-09-03 11:17:22 +03:00
|
|
|
}
|
|
|
|
}
|