1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-26 23:36:08 +03:00
vimr/VimR/PrefWindowComponent.swift

144 lines
4.3 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import RxSwift
import PureLayout
class PrefWindowComponent: WindowComponent, NSWindowDelegate, NSTableViewDataSource, NSTableViewDelegate {
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
fileprivate var data: PrefData
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
fileprivate let categoryView = NSTableView.standardSourceListTableView()
fileprivate let categoryScrollView = NSScrollView.standardScrollView()
fileprivate let paneContainer = NSScrollView(forAutoLayout: ())
2016-09-25 18:50:33 +03:00
fileprivate let panes: [PrefPane]
fileprivate var currentPane: PrefPane {
2016-08-14 15:29:50 +03:00
get {
return self.paneContainer.documentView as! PrefPane
}
set {
self.paneContainer.documentView = newValue
// Auto-layout seems to be smart enough not to add redundant constraints.
2016-08-25 10:27:59 +03:00
if newValue.pinToContainer {
newValue.autoPinEdgesToSuperviewEdges()
}
2016-08-14 15:29:50 +03:00
}
}
2016-07-27 00:40:20 +03:00
init(source: Observable<Any>, initialData: PrefData) {
self.data = initialData
2016-07-21 20:28:58 +03:00
self.panes = [
2016-08-14 15:29:50 +03:00
GeneralPrefPane(source: source, initialData: self.data.general),
2016-09-24 17:31:14 +03:00
AppearancePrefPane(source: source, initialData: self.data.appearance),
AdvancedPrefPane(source: source, initialData: self.data.advanced)
2016-07-21 20:28:58 +03:00
]
super.init(source: source, nibName: "PrefWindow")
self.window.delegate = self
2016-07-21 20:28:58 +03:00
self.addReactions()
}
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
override func subscription(source: Observable<Any>) -> Disposable {
return source
2016-07-27 00:40:20 +03:00
.filter { $0 is PrefData }
.map { $0 as! PrefData }
2016-09-25 19:10:07 +03:00
.subscribe(onNext: { [unowned self] prefData in
if prefData.appearance.editorFont == self.data.appearance.editorFont
&& prefData.appearance.editorUsesLigatures == self.data.appearance.editorUsesLigatures {
2016-07-27 00:40:20 +03:00
return
}
self.data = prefData
2016-09-25 19:10:07 +03:00
})
2016-07-21 20:28:58 +03:00
}
override func addViews() {
let categoryView = self.categoryView
2016-09-25 18:50:33 +03:00
categoryView.dataSource = self
categoryView.delegate = self
let categoryScrollView = self.categoryScrollView
categoryScrollView.documentView = categoryView
2016-08-14 15:29:50 +03:00
let paneContainer = self.paneContainer
2016-08-25 10:27:59 +03:00
paneContainer.hasVerticalScroller = true
paneContainer.hasHorizontalScroller = true
paneContainer.autohidesScrollers = true
2016-09-25 18:50:33 +03:00
paneContainer.borderType = .noBorder
2016-08-25 10:27:59 +03:00
paneContainer.autoresizesSubviews = false
2016-09-25 18:50:33 +03:00
paneContainer.backgroundColor = NSColor.windowBackgroundColor
2016-07-24 21:32:07 +03:00
self.window.contentView?.addSubview(categoryScrollView)
2016-08-14 15:29:50 +03:00
self.window.contentView?.addSubview(paneContainer)
2016-09-25 18:50:33 +03:00
categoryScrollView.autoSetDimension(.width, toSize: 150)
categoryScrollView.autoPinEdge(toSuperviewEdge: .top, withInset: -1)
categoryScrollView.autoPinEdge(toSuperviewEdge: .bottom, withInset: -1)
categoryScrollView.autoPinEdge(toSuperviewEdge: .left, withInset: -1)
2016-08-14 15:29:50 +03:00
2016-09-25 18:50:33 +03:00
paneContainer.autoSetDimension(.width, toSize: 200, relation: .greaterThanOrEqual)
paneContainer.autoPinEdge(toSuperviewEdge: .top)
paneContainer.autoPinEdge(toSuperviewEdge: .right)
paneContainer.autoPinEdge(toSuperviewEdge: .bottom)
paneContainer.autoPinEdge(.left, to: .right, of: categoryScrollView)
2016-08-14 15:29:50 +03:00
self.currentPane = self.panes[0]
}
2016-09-25 18:50:33 +03:00
fileprivate func addReactions() {
self.panes
.map { $0.sink }
.toMergedObservables()
.map { [unowned self] action in
switch action {
case let data as AppearancePrefData:
self.data.appearance = data
case let data as GeneralPrefData:
self.data.general = data
2016-09-24 17:31:14 +03:00
case let data as AdvancedPrefData:
self.data.advanced = data
default:
NSLog("nothing to see here")
}
return self.data
}
2016-09-25 19:10:07 +03:00
.subscribe(onNext: { [unowned self] action in self.publish(event: action) })
.addDisposableTo(self.disposeBag)
}
2016-09-25 18:50:33 +03:00
func windowWillClose(_ notification: Notification) {
self.panes.forEach { $0.windowWillClose() }
}
}
// MARK: - NSTableViewDataSource
2016-07-24 21:32:07 +03:00
extension PrefWindowComponent {
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
@objc(numberOfRowsInTableView:) func numberOfRows(in _: NSTableView) -> Int {
2016-09-24 17:35:27 +03:00
return self.panes.count
}
2016-09-25 18:50:33 +03:00
@objc(tableView:objectValueForTableColumn:row:) func tableView(_: NSTableView, objectValueFor _: NSTableColumn?, row: Int) -> Any? {
2016-09-24 17:35:27 +03:00
return self.panes[row].displayName
}
}
// MARK: - NSTableViewDelegate
2016-07-24 21:32:07 +03:00
extension PrefWindowComponent {
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
func tableViewSelectionDidChange(_: Notification) {
2016-08-14 15:29:50 +03:00
let idx = self.categoryView.selectedRow
self.currentPane = self.panes[idx]
}
}