2016-07-26 22:42:30 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
2016-08-03 22:41:43 +03:00
|
|
|
import Cocoa
|
2016-07-26 22:42:30 +03:00
|
|
|
import RxSwift
|
|
|
|
|
|
|
|
private class PrefKeys {
|
|
|
|
|
2016-08-14 16:38:41 +03:00
|
|
|
static let openNewWindowWhenLaunching = "open-new-window-when-launching"
|
|
|
|
static let openNewWindowOnReactivation = "open-new-window-on-reactivation"
|
2016-09-11 11:53:15 +03:00
|
|
|
static let openQuicklyIgnorePatterns = "open-quickly-ignore-patterns"
|
2016-08-14 16:38:41 +03:00
|
|
|
|
2016-07-26 22:42:30 +03:00
|
|
|
static let editorFontName = "editor-font-name"
|
|
|
|
static let editorFontSize = "editor-font-size"
|
2016-07-30 23:06:42 +03:00
|
|
|
static let editorUsesLigatures = "editor-uses-ligatures"
|
2016-07-26 22:42:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class PrefStore: Store {
|
|
|
|
|
|
|
|
private static let compatibleVersion = "38"
|
2016-07-29 19:25:32 +03:00
|
|
|
private static let defaultEditorFont = NeoVimView.defaultFont
|
2016-07-30 19:17:03 +03:00
|
|
|
static let minimumEditorFontSize = NeoVimView.minFontSize
|
|
|
|
static let maximumEditorFontSize = NeoVimView.maxFontSize
|
2016-07-26 22:42:30 +03:00
|
|
|
|
|
|
|
private let source: Observable<Any>
|
|
|
|
private let disposeBag = DisposeBag()
|
|
|
|
|
|
|
|
private let subject = PublishSubject<Any>()
|
|
|
|
var sink: Observable<Any> {
|
|
|
|
return self.subject.asObservable()
|
|
|
|
}
|
|
|
|
|
|
|
|
private let userDefaults = NSUserDefaults.standardUserDefaults()
|
|
|
|
private let fontManager = NSFontManager.sharedFontManager()
|
|
|
|
|
2016-08-14 15:29:50 +03:00
|
|
|
var data = PrefData(
|
2016-09-11 11:53:15 +03:00
|
|
|
general: GeneralPrefData(openNewWindowWhenLaunching: true,
|
|
|
|
openNewWindowOnReactivation: true,
|
|
|
|
ignorePatterns: Set([ "*/.git", "*.o", "*.d", "*.dia" ].map(FileItemIgnorePattern.init))),
|
2016-08-14 15:29:50 +03:00
|
|
|
appearance: AppearancePrefData(editorFont: PrefStore.defaultEditorFont, editorUsesLigatures: false)
|
|
|
|
)
|
2016-07-26 22:42:30 +03:00
|
|
|
|
|
|
|
init(source: Observable<Any>) {
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
if let prefs = self.userDefaults.dictionaryForKey(PrefStore.compatibleVersion) {
|
|
|
|
self.data = self.prefDataFromDict(prefs)
|
|
|
|
} else {
|
|
|
|
self.userDefaults.setValue(self.prefsDict(self.data), forKey: PrefStore.compatibleVersion)
|
|
|
|
}
|
|
|
|
|
2016-07-26 23:51:05 +03:00
|
|
|
self.addReactions()
|
2016-07-26 22:42:30 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 00:40:20 +03:00
|
|
|
deinit {
|
|
|
|
self.subject.onCompleted()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:53:15 +03:00
|
|
|
private func ignorePatterns(fromString str: String) -> Set<FileItemIgnorePattern> {
|
|
|
|
return Set(str
|
|
|
|
.componentsSeparatedByString(",")
|
|
|
|
.map {
|
|
|
|
FileItemIgnorePattern(pattern: $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func ignorePatternString(fromSet set: Set<FileItemIgnorePattern>) -> String {
|
|
|
|
return set.reduce("") { "\($0), \($1.pattern)" }
|
|
|
|
}
|
|
|
|
|
2016-07-26 22:42:30 +03:00
|
|
|
private func prefDataFromDict(prefs: [String: AnyObject]) -> PrefData {
|
|
|
|
|
|
|
|
let editorFontName = prefs[PrefKeys.editorFontName] as? String ?? PrefStore.defaultEditorFont.fontName
|
2016-08-14 00:01:31 +03:00
|
|
|
let editorFontSize = CGFloat(
|
|
|
|
(prefs[PrefKeys.editorFontSize] as? NSNumber)?.floatValue ?? Float(PrefStore.defaultEditorFont.pointSize)
|
|
|
|
)
|
|
|
|
let editorFont = self.saneFont(editorFontName, fontSize: editorFontSize)
|
|
|
|
|
|
|
|
let usesLigatures = (prefs[PrefKeys.editorUsesLigatures] as? NSNumber)?.boolValue ?? false
|
|
|
|
let openNewWindowWhenLaunching = (prefs[PrefKeys.openNewWindowWhenLaunching] as? NSNumber)?.boolValue ?? true
|
|
|
|
let openNewWindowOnReactivation = (prefs[PrefKeys.openNewWindowOnReactivation] as? NSNumber)?.boolValue ?? true
|
|
|
|
|
2016-09-11 11:53:15 +03:00
|
|
|
let ignorePatternsList = (prefs[PrefKeys.openQuicklyIgnorePatterns] as? String) ?? "*/.git, *.o, *.d, *.dia"
|
|
|
|
let ignorePatterns = self.ignorePatterns(fromString: ignorePatternsList)
|
|
|
|
|
2016-08-14 15:29:50 +03:00
|
|
|
return PrefData(
|
|
|
|
general: GeneralPrefData(
|
|
|
|
openNewWindowWhenLaunching: openNewWindowWhenLaunching,
|
2016-09-11 11:53:15 +03:00
|
|
|
openNewWindowOnReactivation: openNewWindowOnReactivation,
|
|
|
|
ignorePatterns: ignorePatterns
|
2016-08-14 15:29:50 +03:00
|
|
|
),
|
|
|
|
appearance: AppearancePrefData(editorFont: editorFont, editorUsesLigatures: usesLigatures)
|
|
|
|
)
|
2016-08-14 00:01:31 +03:00
|
|
|
}
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-08-14 00:01:31 +03:00
|
|
|
private func saneFont(fontName: String, fontSize: CGFloat) -> NSFont {
|
|
|
|
var editorFont = NSFont(name: fontName, size: fontSize) ?? PrefStore.defaultEditorFont
|
2016-07-26 22:42:30 +03:00
|
|
|
if !editorFont.fixedPitch {
|
|
|
|
editorFont = fontManager.convertFont(PrefStore.defaultEditorFont, toSize: editorFont.pointSize)
|
|
|
|
}
|
|
|
|
if editorFont.pointSize < PrefStore.minimumEditorFontSize
|
|
|
|
|| editorFont.pointSize > PrefStore.maximumEditorFontSize {
|
2016-08-14 00:01:31 +03:00
|
|
|
editorFont = fontManager.convertFont(editorFont, toSize: PrefStore.defaultEditorFont.pointSize)
|
2016-07-30 23:06:42 +03:00
|
|
|
}
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-08-14 00:01:31 +03:00
|
|
|
return editorFont
|
2016-07-26 22:42:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private func prefsDict(prefData: PrefData) -> [String: AnyObject] {
|
2016-08-14 16:38:41 +03:00
|
|
|
let generalData = prefData.general
|
2016-07-26 22:42:30 +03:00
|
|
|
let appearanceData = prefData.appearance
|
2016-08-14 16:38:41 +03:00
|
|
|
|
2016-07-26 22:42:30 +03:00
|
|
|
let prefs: [String: AnyObject] = [
|
2016-08-14 16:38:41 +03:00
|
|
|
// General
|
|
|
|
PrefKeys.openNewWindowWhenLaunching: generalData.openNewWindowWhenLaunching,
|
|
|
|
PrefKeys.openNewWindowOnReactivation: generalData.openNewWindowOnReactivation,
|
2016-09-11 11:53:15 +03:00
|
|
|
PrefKeys.openQuicklyIgnorePatterns: self.ignorePatternString(fromSet: generalData.ignorePatterns),
|
2016-08-14 16:38:41 +03:00
|
|
|
|
|
|
|
// Appearance
|
2016-07-31 00:04:20 +03:00
|
|
|
PrefKeys.editorFontName: appearanceData.editorFont.fontName,
|
|
|
|
PrefKeys.editorFontSize: appearanceData.editorFont.pointSize,
|
|
|
|
PrefKeys.editorUsesLigatures: appearanceData.editorUsesLigatures
|
2016-07-26 22:42:30 +03:00
|
|
|
]
|
2016-08-14 16:38:41 +03:00
|
|
|
|
2016-07-26 22:42:30 +03:00
|
|
|
return prefs
|
|
|
|
}
|
|
|
|
|
|
|
|
private func addReactions() {
|
|
|
|
self.source
|
|
|
|
.filter { $0 is PrefData }
|
|
|
|
.map { $0 as! PrefData }
|
|
|
|
.subscribeNext { [unowned self] prefData in
|
|
|
|
self.data = prefData
|
|
|
|
self.userDefaults.setValue(self.prefsDict(prefData), forKey: PrefStore.compatibleVersion)
|
|
|
|
self.subject.onNext(prefData)
|
|
|
|
}
|
|
|
|
.addDisposableTo(self.disposeBag)
|
|
|
|
}
|
|
|
|
}
|