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
|
2016-10-14 22:51:30 +03:00
|
|
|
import Sparkle
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-10-03 15:58:49 +03:00
|
|
|
struct PrefData {
|
|
|
|
var general: GeneralPrefData
|
|
|
|
var appearance: AppearancePrefData
|
|
|
|
var advanced: AdvancedPrefData
|
|
|
|
|
|
|
|
var mainWindow: MainWindowPrefData
|
|
|
|
}
|
|
|
|
|
2016-07-26 22:42:30 +03:00
|
|
|
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-09-24 17:31:14 +03:00
|
|
|
|
2016-10-14 22:51:30 +03:00
|
|
|
static let useSnapshotUpdateChannel = "use-snapshot-update-channel"
|
2016-09-24 17:31:14 +03:00
|
|
|
static let useInteractiveZsh = "use-interactive-zsh"
|
2016-10-03 15:58:49 +03:00
|
|
|
|
|
|
|
static let isAllToolsVisible = "is-all-tools-visible"
|
|
|
|
static let isToolButtonsShown = "is-tool-buttons-visible"
|
|
|
|
|
|
|
|
static let isFileBrowserOpen = "is-file-browser-visible"
|
|
|
|
static let fileBrowserWidth = "file-browser-width"
|
2016-07-26 22:42:30 +03:00
|
|
|
}
|
|
|
|
|
2016-10-03 15:58:49 +03:00
|
|
|
// TODO: We should generalize the persisting of pref data.
|
2016-09-27 19:02:05 +03:00
|
|
|
class PrefStore: StandardFlow {
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate static let compatibleVersion = "38"
|
|
|
|
fileprivate 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
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let userDefaults = UserDefaults.standard
|
|
|
|
fileprivate let fontManager = NSFontManager.shared()
|
2016-07-26 22:42:30 +03:00
|
|
|
|
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-09-24 17:31:14 +03:00
|
|
|
appearance: AppearancePrefData(editorFont: PrefStore.defaultEditorFont, editorUsesLigatures: false),
|
2016-10-14 22:51:30 +03:00
|
|
|
advanced: AdvancedPrefData(useSnapshotUpdateChannel: false,
|
|
|
|
useInteractiveZsh: false),
|
2016-10-03 15:58:49 +03:00
|
|
|
mainWindow: MainWindowPrefData(isAllToolsVisible: true,
|
|
|
|
isToolButtonsVisible: true,
|
|
|
|
isFileBrowserVisible: true,
|
|
|
|
fileBrowserWidth: 200)
|
2016-08-14 15:29:50 +03:00
|
|
|
)
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
override init(source: Observable<Any>) {
|
|
|
|
super.init(source: source)
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
if let prefs = self.userDefaults.dictionary(forKey: PrefStore.compatibleVersion) {
|
2016-07-26 22:42:30 +03:00
|
|
|
self.data = self.prefDataFromDict(prefs)
|
|
|
|
} else {
|
|
|
|
self.userDefaults.setValue(self.prefsDict(self.data), forKey: PrefStore.compatibleVersion)
|
|
|
|
}
|
2016-07-27 00:40:20 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func prefDataFromDict(_ prefs: [String: Any]) -> PrefData {
|
2016-07-26 22:42:30 +03:00
|
|
|
|
|
|
|
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)
|
2016-10-03 15:58:49 +03:00
|
|
|
|
|
|
|
let usesLigatures = self.bool(from: prefs, for: PrefKeys.editorUsesLigatures, default: false)
|
|
|
|
let openNewWindowWhenLaunching = self.bool(from: prefs, for: PrefKeys.openNewWindowWhenLaunching, default: true)
|
|
|
|
let openNewWindowOnReactivation = self.bool(from: prefs, for: PrefKeys.openNewWindowOnReactivation, default: true)
|
2016-08-14 00:01:31 +03:00
|
|
|
|
2016-09-11 11:53:15 +03:00
|
|
|
let ignorePatternsList = (prefs[PrefKeys.openQuicklyIgnorePatterns] as? String) ?? "*/.git, *.o, *.d, *.dia"
|
2016-09-11 15:28:56 +03:00
|
|
|
let ignorePatterns = PrefUtils.ignorePatterns(fromString: ignorePatternsList)
|
2016-09-11 11:53:15 +03:00
|
|
|
|
2016-10-14 22:51:30 +03:00
|
|
|
let useSnapshotUpdate = self.bool(from: prefs, for: PrefKeys.useSnapshotUpdateChannel, default: false)
|
2016-10-03 15:58:49 +03:00
|
|
|
let useInteractiveZsh = self.bool(from: prefs, for: PrefKeys.useInteractiveZsh, default: false)
|
|
|
|
|
|
|
|
let isAllToolsVisible = self.bool(from: prefs, for: PrefKeys.isAllToolsVisible, default: true)
|
|
|
|
let isToolButtonsVisible = self.bool(from: prefs, for: PrefKeys.isToolButtonsShown, default: true)
|
|
|
|
let isFileBrowserVisible = self.bool(from: prefs, for: PrefKeys.isFileBrowserOpen, default: true)
|
|
|
|
let fileBrowserWidth = (prefs[PrefKeys.fileBrowserWidth] as? NSNumber)?.floatValue ?? Float(200)
|
2016-09-24 17:31:14 +03:00
|
|
|
|
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
|
|
|
),
|
2016-09-24 17:31:14 +03:00
|
|
|
appearance: AppearancePrefData(editorFont: editorFont, editorUsesLigatures: usesLigatures),
|
2016-10-14 22:51:30 +03:00
|
|
|
advanced: AdvancedPrefData(useSnapshotUpdateChannel: useSnapshotUpdate,
|
|
|
|
useInteractiveZsh: useInteractiveZsh),
|
2016-10-03 15:58:49 +03:00
|
|
|
mainWindow: MainWindowPrefData(isAllToolsVisible: isAllToolsVisible,
|
|
|
|
isToolButtonsVisible: isToolButtonsVisible,
|
|
|
|
isFileBrowserVisible: isFileBrowserVisible,
|
|
|
|
fileBrowserWidth: fileBrowserWidth)
|
2016-08-14 15:29:50 +03:00
|
|
|
)
|
2016-08-14 00:01:31 +03:00
|
|
|
}
|
2016-07-26 22:42:30 +03:00
|
|
|
|
2016-10-03 15:58:49 +03:00
|
|
|
fileprivate func bool(from prefs: [String: Any], for key: String, default defaultValue: Bool) -> Bool {
|
|
|
|
return (prefs[key] as? NSNumber)?.boolValue ?? defaultValue
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func saneFont(_ fontName: String, fontSize: CGFloat) -> NSFont {
|
2016-08-14 00:01:31 +03:00
|
|
|
var editorFont = NSFont(name: fontName, size: fontSize) ?? PrefStore.defaultEditorFont
|
2016-09-25 18:50:33 +03:00
|
|
|
if !editorFont.isFixedPitch {
|
|
|
|
editorFont = fontManager.convert(PrefStore.defaultEditorFont, toSize: editorFont.pointSize)
|
2016-07-26 22:42:30 +03:00
|
|
|
}
|
|
|
|
if editorFont.pointSize < PrefStore.minimumEditorFontSize
|
|
|
|
|| editorFont.pointSize > PrefStore.maximumEditorFontSize {
|
2016-09-25 18:50:33 +03:00
|
|
|
editorFont = fontManager.convert(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
|
|
|
}
|
|
|
|
|
2016-10-03 15:58:49 +03:00
|
|
|
fileprivate func prefsDict(_ prefData: PrefData) -> [String: Any] {
|
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-09-24 17:31:14 +03:00
|
|
|
let advancedData = prefData.advanced
|
2016-10-03 15:58:49 +03:00
|
|
|
let mainWindowData = prefData.mainWindow
|
2016-08-14 16:38:41 +03:00
|
|
|
|
2016-10-03 15:58:49 +03:00
|
|
|
let ignorePatterns = PrefUtils.ignorePatternString(fromSet: generalData.ignorePatterns) as Any
|
|
|
|
|
|
|
|
let prefs: [String: Any] = [
|
2016-08-14 16:38:41 +03:00
|
|
|
// General
|
2016-10-03 15:58:49 +03:00
|
|
|
PrefKeys.openNewWindowWhenLaunching: generalData.openNewWindowWhenLaunching as Any,
|
|
|
|
PrefKeys.openNewWindowOnReactivation: generalData.openNewWindowOnReactivation as Any,
|
|
|
|
PrefKeys.openQuicklyIgnorePatterns: ignorePatterns,
|
2016-08-14 16:38:41 +03:00
|
|
|
|
|
|
|
// Appearance
|
2016-10-03 15:58:49 +03:00
|
|
|
PrefKeys.editorFontName: appearanceData.editorFont.fontName as Any,
|
|
|
|
PrefKeys.editorFontSize: appearanceData.editorFont.pointSize as Any,
|
|
|
|
PrefKeys.editorUsesLigatures: appearanceData.editorUsesLigatures as Any,
|
2016-09-24 17:31:14 +03:00
|
|
|
|
|
|
|
// Advanced
|
2016-10-14 22:51:30 +03:00
|
|
|
PrefKeys.useSnapshotUpdateChannel: advancedData.useSnapshotUpdateChannel as Any,
|
2016-10-03 15:58:49 +03:00
|
|
|
PrefKeys.useInteractiveZsh: advancedData.useInteractiveZsh as Any,
|
|
|
|
|
|
|
|
// MainWindow
|
|
|
|
PrefKeys.isAllToolsVisible: mainWindowData.isAllToolsVisible,
|
|
|
|
PrefKeys.isToolButtonsShown: mainWindowData.isToolButtonsVisible,
|
|
|
|
PrefKeys.isFileBrowserOpen: mainWindowData.isFileBrowserVisible,
|
|
|
|
PrefKeys.fileBrowserWidth: mainWindowData.fileBrowserWidth
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-27 19:02:05 +03:00
|
|
|
override func subscription(source: Observable<Any>) -> Disposable {
|
|
|
|
return source
|
2016-10-03 15:58:49 +03:00
|
|
|
.filter { $0 is PrefData || $0 is MainWindowPrefData }
|
|
|
|
.subscribe(onNext: { [unowned self] data in
|
|
|
|
switch data {
|
|
|
|
case let prefData as PrefData:
|
|
|
|
self.data = prefData
|
|
|
|
|
|
|
|
case let mainWindowPrefData as MainWindowPrefData:
|
|
|
|
self.data.mainWindow = mainWindowPrefData
|
|
|
|
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.userDefaults.setValue(self.prefsDict(self.data), forKey: PrefStore.compatibleVersion)
|
|
|
|
self.publish(event: self.data)
|
|
|
|
})
|
2016-07-26 22:42:30 +03:00
|
|
|
}
|
|
|
|
}
|