1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-22 13:11:55 +03:00
vimr/VimR/PrefStore.swift

206 lines
8.7 KiB
Swift
Raw Normal View History

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
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"
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-10-27 01:26:04 +03:00
static let editorLinespacing = "editor-linespacing"
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"
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
}
// TODO: We should generalize the persisting of pref data.
2016-10-29 19:45:14 +03:00
/**
To reset prefs
$ defaults write com.qvacua.vimr 38 -dict editor-font-name InputMonoCompressed-Regular editor-font-size 13 editor-uses-ligatures 0 open-new-window-on-reactivation 1 open-new-window-when-launching 1
$ defaults read ~/Library/Preferences/com.qvacua.VimR
*/
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"
2016-09-25 18:50:33 +03:00
fileprivate static let defaultEditorFont = NeoVimView.defaultFont
2016-10-26 23:50:15 +03:00
static let minEditorFontSize = NeoVimView.minFontSize
static let maxEditorFontSize = NeoVimView.maxFontSize
2016-10-27 09:21:29 +03:00
static let defaultEditorFontSize = NeoVimView.defaultFont.pointSize
2016-07-26 22:42:30 +03:00
2016-10-27 09:21:29 +03:00
static let defaultEditorLinespacing = NeoVimView.defaultLinespacing
static let minEditorLinespacing = NeoVimView.minLinespacing
static let maxEditorLinespacing = NeoVimView.maxLinespacing
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(
general: GeneralPrefData(openNewWindowWhenLaunching: true,
openNewWindowOnReactivation: true,
ignorePatterns: Set([ "*/.git", "*.o", "*.d", "*.dia" ].map(FileItemIgnorePattern.init))),
appearance: AppearancePrefData(editorFont: PrefStore.defaultEditorFont,
2016-10-27 01:26:04 +03:00
editorLinespacing: 1,
editorUsesLigatures: false),
2016-10-14 22:51:30 +03:00
advanced: AdvancedPrefData(useSnapshotUpdateChannel: false,
useInteractiveZsh: false),
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)
let usesLigatures = self.bool(from: prefs, for: PrefKeys.editorUsesLigatures, default: false)
2016-10-29 19:45:14 +03:00
let linespacing = self.saneLinespacing(Float((prefs[PrefKeys.editorLinespacing] as? String) ?? "1") ?? 1)
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
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-10-14 22:51:30 +03:00
let useSnapshotUpdate = self.bool(from: prefs, for: PrefKeys.useSnapshotUpdateChannel, default: false)
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,
openNewWindowOnReactivation: openNewWindowOnReactivation,
ignorePatterns: ignorePatterns
2016-08-14 15:29:50 +03:00
),
appearance: AppearancePrefData(editorFont: editorFont,
2016-10-27 01:26:04 +03:00
editorLinespacing: linespacing,
editorUsesLigatures: usesLigatures),
2016-10-14 22:51:30 +03:00
advanced: AdvancedPrefData(useSnapshotUpdateChannel: useSnapshotUpdate,
useInteractiveZsh: useInteractiveZsh),
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
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
}
2016-10-26 23:50:15 +03:00
if editorFont.pointSize < PrefStore.minEditorFontSize || editorFont.pointSize > PrefStore.maxEditorFontSize {
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-29 19:45:14 +03:00
fileprivate func saneLinespacing(_ fLinespacing: Float) -> CGFloat {
let linespacing = CGFloat(fLinespacing)
2016-10-27 09:21:29 +03:00
guard linespacing >= PrefStore.minEditorLinespacing && linespacing <= PrefStore.maxEditorLinespacing else {
return PrefStore.defaultEditorLinespacing
}
return linespacing
}
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
let mainWindowData = prefData.mainWindow
2016-08-14 16:38:41 +03:00
let ignorePatterns = PrefUtils.ignorePatternString(fromSet: generalData.ignorePatterns) as Any
let prefs: [String: Any] = [
2016-08-14 16:38:41 +03:00
// General
PrefKeys.openNewWindowWhenLaunching: generalData.openNewWindowWhenLaunching as Any,
PrefKeys.openNewWindowOnReactivation: generalData.openNewWindowOnReactivation as Any,
PrefKeys.openQuicklyIgnorePatterns: ignorePatterns,
2016-08-14 16:38:41 +03:00
// Appearance
PrefKeys.editorFontName: appearanceData.editorFont.fontName as Any,
PrefKeys.editorFontSize: appearanceData.editorFont.pointSize as Any,
2016-10-29 19:45:14 +03:00
PrefKeys.editorLinespacing: String(format: "%.2f", appearanceData.editorLinespacing) 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,
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
.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
}
}