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

150 lines
4.4 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
2016-09-24 17:31:14 +03:00
var advanced: AdvancedPrefData
}
class PrefWindowComponent: WindowComponent, NSWindowDelegate, 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 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-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
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
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
}
.subscribeNext { [unowned self] action in self.publish(event: action) }
.addDisposableTo(self.disposeBag)
}
func windowWillClose(notification: NSNotification) {
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-08-14 15:29:50 +03:00
func numberOfRowsInTableView(_: NSTableView) -> Int {
2016-09-24 17:35:27 +03:00
return self.panes.count
}
2016-07-21 20:28:58 +03:00
func tableView(_: NSTableView, objectValueForTableColumn _: NSTableColumn?, row: Int) -> AnyObject? {
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-08-14 15:29:50 +03:00
func tableViewSelectionDidChange(_: NSNotification) {
let idx = self.categoryView.selectedRow
self.currentPane = self.panes[idx]
}
}