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

142 lines
4.1 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 {
2016-08-14 15:29:50 +03:00
var general: GeneralPrefData
2016-07-21 20:28:58 +03:00
var appearance: AppearancePrefData
}
class PrefWindowComponent: WindowComponent, NSTableViewDataSource, NSTableViewDelegate {
2016-07-21 20:28:58 +03:00
2016-07-27 00:40:20 +03:00
private var data: PrefData
2016-07-21 20:28:58 +03:00
2016-09-03 00:55:19 +03:00
private let categoryView = NSTableView.standardSourceListTableView()
private let categoryScrollView = NSScrollView.standardScrollView()
2016-08-14 15:29:50 +03:00
private let paneContainer = NSScrollView(forAutoLayout: ())
2016-08-14 15:29:50 +03:00
private let paneNames = [ "General", "Appearance" ]
private let panes: [PrefPane]
private var currentPane: PrefPane {
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-07-27 00:40:20 +03:00
AppearancePrefPane(source: source, initialData: self.data.appearance)
2016-07-21 20:28:58 +03:00
]
super.init(source: source, nibName: "PrefWindow")
2016-07-21 20:28:58 +03:00
self.addReactions()
}
2016-07-21 20:28:58 +03:00
override func subscription(source source: Observable<Any>) -> Disposable {
return source
2016-07-27 00:40:20 +03:00
.filter { $0 is PrefData }
.map { $0 as! PrefData }
.subscribeNext { [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-07-21 20:28:58 +03:00
}
override func addViews() {
let categoryView = self.categoryView
categoryView.setDataSource(self)
categoryView.setDelegate(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
paneContainer.borderType = .NoBorder
paneContainer.autoresizesSubviews = false
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)
categoryScrollView.autoSetDimension(.Width, toSize: 150)
categoryScrollView.autoPinEdgeToSuperviewEdge(.Top, withInset: -1)
categoryScrollView.autoPinEdgeToSuperviewEdge(.Bottom, withInset: -1)
categoryScrollView.autoPinEdgeToSuperviewEdge(.Left, withInset: -1)
2016-08-14 15:29:50 +03:00
paneContainer.autoSetDimension(.Width, toSize: 200, relation: .GreaterThanOrEqual)
paneContainer.autoPinEdgeToSuperviewEdge(.Top)
paneContainer.autoPinEdgeToSuperviewEdge(.Right)
paneContainer.autoPinEdgeToSuperviewEdge(.Bottom)
paneContainer.autoPinEdge(.Left, toEdge: .Right, ofView: categoryScrollView)
self.currentPane = self.panes[0]
}
private 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
default:
NSLog("nothing to see here")
}
return self.data
}
.subscribeNext { [unowned self] action in self.publish(event: action) }
.addDisposableTo(self.disposeBag)
}
}
// MARK: - NSTableViewDataSource
2016-07-24 21:32:07 +03:00
extension PrefWindowComponent {
2016-07-21 20:28:58 +03:00
2016-08-14 15:29:50 +03:00
func numberOfRowsInTableView(_: 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
2016-08-14 15:29:50 +03:00
func tableViewSelectionDidChange(_: NSNotification) {
let idx = self.categoryView.selectedRow
self.currentPane = self.panes[idx]
}
}