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-08 00:18:35 +03:00
|
|
|
class OpenQuicklyWindowComponent: WindowComponent,
|
|
|
|
NSWindowDelegate,
|
|
|
|
NSTableViewDelegate, NSTableViewDataSource,
|
|
|
|
NSTextFieldDelegate
|
|
|
|
{
|
2016-09-05 20:15:54 +03:00
|
|
|
let scanCondition = NSCondition()
|
|
|
|
var pauseScan = false
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate(set) var pattern = ""
|
|
|
|
fileprivate(set) var cwd = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true) {
|
2016-09-05 20:15:54 +03:00
|
|
|
didSet {
|
2016-09-25 18:50:33 +03:00
|
|
|
self.cwdPathCompsCount = self.cwd.pathComponents.count
|
|
|
|
self.cwdControl.url = self.cwd
|
2016-09-05 20:15:54 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate(set) var flatFileItems = [FileItem]()
|
|
|
|
fileprivate(set) var fileViewItems = [ScoredFileItem]()
|
2016-09-05 20:15:54 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let userInitiatedScheduler = ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .userInitiated)
|
2016-09-02 19:22:40 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let searchField = NSTextField(forAutoLayout: ())
|
|
|
|
fileprivate let progressIndicator = NSProgressIndicator(forAutoLayout: ())
|
|
|
|
fileprivate let cwdControl = NSPathControl(forAutoLayout: ())
|
|
|
|
fileprivate let countField = NSTextField(forAutoLayout: ())
|
|
|
|
fileprivate let fileView = NSTableView.standardTableView()
|
2016-09-03 11:17:22 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let fileItemService: FileItemService
|
2016-09-03 11:17:22 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate var count = 0
|
|
|
|
fileprivate var perSessionDisposeBag = DisposeBag()
|
2016-08-30 23:25:34 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate var cwdPathCompsCount = 0
|
|
|
|
fileprivate let searchStream: Observable<String>
|
|
|
|
fileprivate let filterOpQueue = OperationQueue()
|
2016-09-08 00:18:35 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
weak fileprivate var mainWindow: MainWindowComponent?
|
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-25 18:50:33 +03:00
|
|
|
self.searchStream = self.searchField.rx.textInput.text
|
2016-09-05 20:15:54 +03:00
|
|
|
.throttle(0.2, scheduler: MainScheduler.instance)
|
|
|
|
.distinctUntilChanged()
|
2016-09-11 15:28:56 +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-25 18:50:33 +03:00
|
|
|
self.filterOpQueue.qualityOfService = .userInitiated
|
2016-09-04 00:13:52 +03:00
|
|
|
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-25 18:50:33 +03:00
|
|
|
searchField.rx.delegate.setForwardToDelegate(self, retainDelegate: false)
|
2016-09-08 00:18:35 +03:00
|
|
|
|
2016-09-05 20:15:54 +03:00
|
|
|
let progressIndicator = self.progressIndicator
|
2016-09-25 18:50:33 +03:00
|
|
|
progressIndicator.isIndeterminate = true
|
|
|
|
progressIndicator.isDisplayedWhenStopped = false
|
|
|
|
progressIndicator.style = .spinningStyle
|
|
|
|
progressIndicator.controlSize = .small
|
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-25 18:50:33 +03:00
|
|
|
fileView.dataSource = self
|
|
|
|
fileView.delegate = 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
|
2016-09-25 18:50:33 +03:00
|
|
|
cwdControl.pathStyle = .standard
|
|
|
|
cwdControl.backgroundColor = NSColor.clear
|
2016-09-03 00:35:18 +03:00
|
|
|
cwdControl.refusesFirstResponder = true
|
2016-09-25 18:50:33 +03:00
|
|
|
cwdControl.cell?.controlSize = .small
|
|
|
|
cwdControl.cell?.font = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize())
|
|
|
|
cwdControl.setContentCompressionResistancePriority(NSLayoutPriorityDefaultLow, for:.horizontal)
|
2016-09-03 00:35:18 +03:00
|
|
|
|
2016-09-03 11:17:22 +03:00
|
|
|
let countField = self.countField
|
2016-09-25 18:50:33 +03:00
|
|
|
countField.isEditable = false
|
|
|
|
countField.isBordered = false
|
|
|
|
countField.alignment = .right
|
|
|
|
countField.backgroundColor = NSColor.clear
|
2016-09-03 11:17:22 +03:00
|
|
|
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-25 18:50:33 +03:00
|
|
|
searchField.autoPinEdge(toSuperviewEdge: .top, withInset: 8)
|
|
|
|
searchField.autoPinEdge(toSuperviewEdge: .right, withInset: 8)
|
|
|
|
searchField.autoPinEdge(toSuperviewEdge: .left, withInset: 8)
|
2016-09-03 00:35:18 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
progressIndicator.autoAlignAxis(.horizontal, toSameAxisOf: searchField)
|
|
|
|
progressIndicator.autoPinEdge(.right, to: .right, of: searchField, withOffset: -4)
|
2016-09-05 20:15:54 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileScrollView.autoPinEdge(.top, to: .bottom, of: searchField, withOffset: 8)
|
|
|
|
fileScrollView.autoPinEdge(toSuperviewEdge: .left, withInset: -1)
|
|
|
|
fileScrollView.autoPinEdge(toSuperviewEdge: .right, withInset: -1)
|
|
|
|
fileScrollView.autoSetDimension(.height, toSize: 200, relation: .greaterThanOrEqual)
|
2016-09-03 00:44:05 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
cwdControl.autoPinEdge(.top, to: .bottom, of: fileScrollView, withOffset: 4)
|
|
|
|
cwdControl.autoPinEdge(toSuperviewEdge: .left, withInset: 2)
|
|
|
|
cwdControl.autoPinEdge(toSuperviewEdge: .bottom, withInset: 4)
|
2016-09-03 11:17:22 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
countField.autoPinEdge(.top, to: .bottom, of: fileScrollView, withOffset: 4)
|
|
|
|
countField.autoPinEdge(toSuperviewEdge: .right, withInset: 2)
|
|
|
|
countField.autoPinEdge(.left, to: .right, of: cwdControl, withOffset: 4)
|
2016-08-30 23:25:34 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
override func subscription(source: Observable<Any>) -> Disposable {
|
2016-09-25 19:10:07 +03:00
|
|
|
return Disposables.create()
|
2016-08-30 23:25:34 +03:00
|
|
|
}
|
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-08 00:18:35 +03:00
|
|
|
self.mainWindow = mainWindow
|
|
|
|
self.mainWindow?.sink
|
|
|
|
.filter { $0 is MainWindowAction }
|
|
|
|
.map { $0 as! MainWindowAction }
|
2016-09-25 19:10:07 +03:00
|
|
|
.subscribe(onNext: { [unowned self] action in
|
2016-09-08 00:18:35 +03:00
|
|
|
switch action {
|
|
|
|
case .close:
|
|
|
|
self.window.performClose(self)
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2016-09-25 19:10:07 +03:00
|
|
|
})
|
2016-09-08 00:18:35 +03:00
|
|
|
.addDisposableTo(self.perSessionDisposeBag)
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
self.cwd = mainWindow.cwd as URL
|
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)
|
2016-09-25 19:10:07 +03:00
|
|
|
.do(onNext: { [unowned self] items in
|
2016-09-05 20:15:54 +03:00
|
|
|
self.scanCondition.lock()
|
|
|
|
while self.pauseScan {
|
|
|
|
self.scanCondition.wait()
|
|
|
|
}
|
|
|
|
self.scanCondition.unlock()
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
self.flatFileItems.append(contentsOf: items)
|
2016-09-05 20:15:54 +03:00
|
|
|
self.resetAndAddFilterOperation()
|
2016-09-25 19:10:07 +03:00
|
|
|
})
|
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
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func resetAndAddFilterOperation() {
|
2016-09-05 20:15:54 +03:00
|
|
|
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 {
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
@objc(numberOfRowsInTableView:) func numberOfRows(in _: NSTableView) -> Int {
|
2016-09-05 20:15:54 +03:00
|
|
|
return self.fileViewItems.count
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
2016-09-08 00:18:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDelegate
|
|
|
|
extension OpenQuicklyWindowComponent {
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
|
2016-09-08 00:18:35 +03:00
|
|
|
return OpenQuicklyFileViewRow()
|
|
|
|
}
|
2016-09-05 20:15:54 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
@objc(tableView:viewForTableColumn:row:) func tableView(_ tableView: NSTableView, viewFor _: NSTableColumn?, row: Int) -> NSView? {
|
|
|
|
let cachedCell = tableView.make(withIdentifier: "file-view-row", owner: self)
|
2016-09-05 20:15:54 +03:00
|
|
|
let cell = cachedCell as? ImageAndTextTableCell ?? ImageAndTextTableCell(withIdentifier: "file-view-row")
|
|
|
|
|
|
|
|
let url = self.fileViewItems[row].url
|
2016-09-25 18:50:33 +03:00
|
|
|
cell.text = self.rowText(forUrl: url as URL)
|
2016-09-06 00:29:32 +03:00
|
|
|
cell.image = self.fileItemService.icon(forUrl: url)
|
2016-09-04 00:13:52 +03:00
|
|
|
|
|
|
|
return cell
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
2016-09-04 00:13:52 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func tableViewSelectionDidChange(_: Notification) {
|
2016-09-08 00:18:35 +03:00
|
|
|
// NSLog("\(#function): selection changed")
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func rowText(forUrl url: URL) -> NSAttributedString {
|
|
|
|
let pathComps = url.pathComponents
|
2016-09-05 20:15:54 +03:00
|
|
|
let truncatedPathComps = pathComps[self.cwdPathCompsCount..<pathComps.count]
|
|
|
|
let name = truncatedPathComps.last!
|
|
|
|
|
|
|
|
if truncatedPathComps.dropLast().isEmpty {
|
|
|
|
return NSMutableAttributedString(string: name)
|
|
|
|
}
|
|
|
|
|
|
|
|
let rowText: NSMutableAttributedString
|
2016-09-25 18:50:33 +03:00
|
|
|
let pathInfo = truncatedPathComps.dropLast().reversed().joined(separator: " / ")
|
2016-09-05 20:15:54 +03:00
|
|
|
rowText = NSMutableAttributedString(string: "\(name) — \(pathInfo)")
|
|
|
|
rowText.addAttribute(NSForegroundColorAttributeName,
|
2016-09-25 18:50:33 +03:00
|
|
|
value: NSColor.lightGray,
|
2016-09-05 20:15:54 +03:00
|
|
|
range: NSRange(location:name.characters.count,
|
|
|
|
length: pathInfo.characters.count + 3))
|
|
|
|
|
|
|
|
return rowText
|
|
|
|
}
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
|
|
|
|
2016-09-08 00:18:35 +03:00
|
|
|
// MARK: - NSTextFieldDelegate
|
2016-09-03 00:44:05 +03:00
|
|
|
extension OpenQuicklyWindowComponent {
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
@objc(control:textView:doCommandBySelector:) func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
|
2016-09-08 00:18:35 +03:00
|
|
|
switch commandSelector {
|
|
|
|
case NSSelectorFromString("cancelOperation:"):
|
|
|
|
self.window.performClose(self)
|
|
|
|
return true
|
|
|
|
|
|
|
|
case NSSelectorFromString("insertNewline:"):
|
|
|
|
self.mainWindow?.open(urls: [self.fileViewItems[self.fileView.selectedRow].url])
|
|
|
|
self.window.performClose(self)
|
|
|
|
return true
|
|
|
|
|
|
|
|
case NSSelectorFromString("moveUp:"):
|
|
|
|
self.moveSelection(ofTableView: self.fileView, byDelta: -1)
|
|
|
|
return true
|
|
|
|
|
|
|
|
case NSSelectorFromString("moveDown:"):
|
|
|
|
self.moveSelection(ofTableView: self.fileView, byDelta: 1)
|
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func moveSelection(ofTableView tableView: NSTableView, byDelta delta: Int) {
|
2016-09-08 00:18:35 +03:00
|
|
|
let selectedRow = tableView.selectedRow
|
|
|
|
let lastIdx = tableView.numberOfRows - 1
|
|
|
|
let targetIdx: Int
|
|
|
|
|
|
|
|
if selectedRow + delta < 0 {
|
|
|
|
targetIdx = 0
|
|
|
|
} else if selectedRow + delta > lastIdx {
|
|
|
|
targetIdx = lastIdx
|
|
|
|
} else {
|
|
|
|
targetIdx = selectedRow + delta
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
tableView.selectRowIndexes(IndexSet(integer: targetIdx), byExtendingSelection: false)
|
2016-09-08 00:18:35 +03:00
|
|
|
tableView.scrollRowToVisible(targetIdx)
|
2016-09-03 00:44:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-03 11:17:22 +03:00
|
|
|
|
|
|
|
// MARK: - NSWindowDelegate
|
|
|
|
extension OpenQuicklyWindowComponent {
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func windowWillClose(_ notification: Notification) {
|
2016-09-05 20:15:54 +03:00
|
|
|
self.endProgress()
|
2016-09-08 00:18:35 +03:00
|
|
|
|
|
|
|
self.mainWindow = nil
|
|
|
|
|
|
|
|
self.filterOpQueue.cancelAllOperations()
|
2016-09-05 20:15:54 +03:00
|
|
|
|
|
|
|
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-11 15:37:04 +03:00
|
|
|
self.fileView.reloadData()
|
2016-09-04 00:13:52 +03:00
|
|
|
|
|
|
|
self.searchField.stringValue = ""
|
|
|
|
self.countField.stringValue = "0 items"
|
2016-09-03 11:17:22 +03:00
|
|
|
}
|
2016-09-11 15:37:04 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func windowDidResignKey(_ notification: Notification) {
|
2016-09-11 15:37:04 +03:00
|
|
|
self.window.performClose(self)
|
|
|
|
}
|
2016-09-03 11:17:22 +03:00
|
|
|
}
|