2016-07-19 20:34:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import RxSwift
|
|
|
|
import PureLayout
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
struct PrefData {
|
|
|
|
var appearance: AppearancePrefData
|
2016-07-19 20:34:05 +03:00
|
|
|
}
|
|
|
|
|
2016-07-24 21:32:07 +03:00
|
|
|
class PrefWindowComponent: NSObject, NSTableViewDataSource, NSTableViewDelegate, Component {
|
2016-07-19 20:34:05 +03:00
|
|
|
|
2016-07-24 23:31:19 +03:00
|
|
|
private static let windowWidth = CGFloat(640)
|
|
|
|
private static let defaultEditorFont = NSFont(name: "Menlo", size: 13)!
|
|
|
|
|
2016-07-19 20:34:05 +03:00
|
|
|
private let windowMask = NSTitledWindowMask
|
|
|
|
| NSResizableWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
|
|
|
|
|
|
|
|
private let source: Observable<Any>
|
|
|
|
private let disposeBag = DisposeBag()
|
2016-07-21 20:28:58 +03:00
|
|
|
|
|
|
|
private let subject = PublishSubject<Any>()
|
|
|
|
var sink: Observable<Any> {
|
|
|
|
return self.subject.asObservable()
|
|
|
|
}
|
|
|
|
|
|
|
|
private var data = PrefData(
|
2016-07-24 23:31:19 +03:00
|
|
|
appearance: AppearancePrefData(editorFont: PrefWindowComponent.defaultEditorFont)
|
2016-07-21 20:28:58 +03:00
|
|
|
)
|
|
|
|
|
2016-07-25 20:47:14 +03:00
|
|
|
private let windowController = NSWindowController(windowNibName: "PrefWindow")
|
2016-07-24 21:32:07 +03:00
|
|
|
private let window: NSWindow
|
|
|
|
|
2016-07-19 20:34:05 +03:00
|
|
|
private let categoryView = NSTableView(frame: CGRect.zero)
|
|
|
|
private let categoryScrollView = NSScrollView(forAutoLayout: ())
|
|
|
|
private let paneScrollView = NSScrollView(forAutoLayout: ())
|
|
|
|
|
|
|
|
private let paneNames = [ "Appearance" ]
|
2016-07-21 20:28:58 +03:00
|
|
|
private let panes: [ViewComponent]
|
2016-07-19 20:34:05 +03:00
|
|
|
|
|
|
|
init(source: Observable<Any>) {
|
|
|
|
self.source = source
|
2016-07-21 20:28:58 +03:00
|
|
|
|
|
|
|
self.panes = [
|
|
|
|
AppearancePrefPane(source: Observable.empty(), data: self.data.appearance)
|
|
|
|
]
|
2016-07-19 20:34:05 +03:00
|
|
|
|
2016-07-25 20:47:14 +03:00
|
|
|
self.window = self.windowController.window!
|
2016-07-19 20:34:05 +03:00
|
|
|
|
2016-07-24 21:32:07 +03:00
|
|
|
super.init()
|
2016-07-19 20:34:05 +03:00
|
|
|
|
|
|
|
self.addViews()
|
2016-07-21 20:28:58 +03:00
|
|
|
self.addReactions()
|
2016-07-19 20:34:05 +03:00
|
|
|
}
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-24 21:32:07 +03:00
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
deinit {
|
|
|
|
self.subject.onCompleted()
|
|
|
|
}
|
|
|
|
|
2016-07-24 21:32:07 +03:00
|
|
|
func show() {
|
|
|
|
self.window.makeKeyAndOrderFront(self)
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
private func addReactions() {
|
|
|
|
self.panes
|
|
|
|
.map { $0.sink }
|
|
|
|
.toObservable()
|
|
|
|
.flatMap { $0 }
|
2016-07-24 21:32:07 +03:00
|
|
|
.map { [unowned self] action in
|
2016-07-21 20:28:58 +03:00
|
|
|
switch action {
|
|
|
|
case let data as AppearancePrefData:
|
|
|
|
self.data.appearance = data
|
|
|
|
default:
|
|
|
|
NSLog("nothing to see here")
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.data
|
|
|
|
}
|
2016-07-24 21:32:07 +03:00
|
|
|
.subscribeNext { [unowned self] action in self.subject.onNext(action) }
|
2016-07-21 20:28:58 +03:00
|
|
|
.addDisposableTo(self.disposeBag)
|
|
|
|
}
|
|
|
|
|
2016-07-19 20:34:05 +03:00
|
|
|
private func addViews() {
|
|
|
|
let tableColumn = NSTableColumn(identifier: "name")
|
|
|
|
let textFieldCell = NSTextFieldCell()
|
|
|
|
textFieldCell.allowsEditingTextAttributes = false
|
|
|
|
textFieldCell.lineBreakMode = .ByTruncatingTail
|
|
|
|
tableColumn.dataCell = textFieldCell
|
|
|
|
|
|
|
|
let categoryView = self.categoryView
|
|
|
|
categoryView.addTableColumn(tableColumn)
|
|
|
|
categoryView.rowSizeStyle = .Default
|
|
|
|
categoryView.sizeLastColumnToFit()
|
|
|
|
categoryView.allowsEmptySelection = false
|
|
|
|
categoryView.allowsMultipleSelection = false
|
|
|
|
categoryView.headerView = nil
|
|
|
|
categoryView.focusRingType = .None
|
|
|
|
categoryView.selectionHighlightStyle = .SourceList
|
|
|
|
categoryView.setDataSource(self)
|
|
|
|
categoryView.setDelegate(self)
|
|
|
|
|
|
|
|
let categoryScrollView = self.categoryScrollView
|
|
|
|
categoryScrollView.hasVerticalScroller = true
|
|
|
|
categoryScrollView.hasHorizontalScroller = true
|
|
|
|
categoryScrollView.autohidesScrollers = true
|
|
|
|
categoryScrollView.borderType = .BezelBorder
|
|
|
|
categoryScrollView.documentView = categoryView
|
|
|
|
|
|
|
|
let paneScrollView = self.paneScrollView
|
|
|
|
paneScrollView.hasVerticalScroller = true;
|
2016-07-24 23:31:19 +03:00
|
|
|
paneScrollView.hasHorizontalScroller = false;
|
2016-07-19 20:34:05 +03:00
|
|
|
paneScrollView.autohidesScrollers = true;
|
2016-07-24 23:31:19 +03:00
|
|
|
paneScrollView.borderType = .NoBorder;
|
2016-07-19 20:34:05 +03:00
|
|
|
paneScrollView.autoresizesSubviews = true;
|
|
|
|
paneScrollView.backgroundColor = NSColor.windowBackgroundColor();
|
|
|
|
|
2016-07-24 21:32:07 +03:00
|
|
|
self.window.contentView?.addSubview(categoryScrollView)
|
|
|
|
self.window.contentView?.addSubview(paneScrollView)
|
2016-07-19 20:34:05 +03:00
|
|
|
|
|
|
|
categoryScrollView.autoSetDimension(.Width, toSize: 150)
|
|
|
|
categoryScrollView.autoPinEdgeToSuperviewEdge(.Top, withInset: -1)
|
|
|
|
categoryScrollView.autoPinEdgeToSuperviewEdge(.Bottom, withInset: -1)
|
|
|
|
categoryScrollView.autoPinEdgeToSuperviewEdge(.Left, withInset: -1)
|
|
|
|
paneScrollView.autoSetDimension(.Width, toSize: 200, relation: .GreaterThanOrEqual)
|
|
|
|
paneScrollView.autoPinEdgeToSuperviewEdge(.Top)
|
|
|
|
paneScrollView.autoPinEdgeToSuperviewEdge(.Right)
|
|
|
|
paneScrollView.autoPinEdgeToSuperviewEdge(.Bottom)
|
|
|
|
paneScrollView.autoPinEdge(.Left, toEdge: .Right, ofView: categoryScrollView)
|
|
|
|
|
2016-07-24 23:31:19 +03:00
|
|
|
let pane = self.panes[0].view
|
|
|
|
self.paneScrollView.documentView = pane
|
|
|
|
pane.autoPinEdgeToSuperviewEdge(.Right)
|
|
|
|
pane.autoPinEdgeToSuperviewEdge(.Left)
|
2016-07-19 20:34:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDataSource
|
2016-07-24 21:32:07 +03:00
|
|
|
extension PrefWindowComponent {
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-19 20:34:05 +03:00
|
|
|
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
|
|
|
|
return self.paneNames.count
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:28:58 +03:00
|
|
|
func tableView(_: NSTableView, objectValueForTableColumn _: NSTableColumn?, row: Int) -> AnyObject? {
|
2016-07-19 20:34:05 +03:00
|
|
|
return self.paneNames[row]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - NSTableViewDelegate
|
2016-07-24 21:32:07 +03:00
|
|
|
extension PrefWindowComponent {
|
2016-07-21 20:28:58 +03:00
|
|
|
|
2016-07-19 20:34:05 +03:00
|
|
|
func tableViewSelectionDidChange(notification: NSNotification) {
|
|
|
|
}
|
|
|
|
}
|