1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-22 21:21:32 +03:00
vimr/VimR/PrefWindowComponent.swift

162 lines
4.8 KiB
Swift
Raw Normal View History

/**
* 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-24 21:32:07 +03:00
class PrefWindowComponent: NSObject, NSTableViewDataSource, NSTableViewDelegate, Component {
private static let windowWidth = CGFloat(640)
private static let defaultEditorFont = NSFont(name: "Menlo", size: 13)!
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(
appearance: AppearancePrefData(editorFont: PrefWindowComponent.defaultEditorFont)
2016-07-21 20:28:58 +03:00
)
private let windowController = NSWindowController(windowNibName: "PrefWindow")
2016-07-24 21:32:07 +03:00
private let window: NSWindow
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]
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)
]
self.window = self.windowController.window!
2016-07-24 21:32:07 +03:00
super.init()
self.addViews()
2016-07-21 20:28:58 +03:00
self.addReactions()
}
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)
}
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;
paneScrollView.hasHorizontalScroller = false;
paneScrollView.autohidesScrollers = true;
paneScrollView.borderType = .NoBorder;
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)
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)
let pane = self.panes[0].view
self.paneScrollView.documentView = pane
pane.autoPinEdgeToSuperviewEdge(.Right)
pane.autoPinEdgeToSuperviewEdge(.Left)
}
}
// MARK: - NSTableViewDataSource
2016-07-24 21:32:07 +03:00
extension PrefWindowComponent {
2016-07-21 20:28:58 +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? {
return self.paneNames[row]
}
}
// MARK: - NSTableViewDelegate
2016-07-24 21:32:07 +03:00
extension PrefWindowComponent {
2016-07-21 20:28:58 +03:00
func tableViewSelectionDidChange(notification: NSNotification) {
}
}