1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 06:43:24 +03:00

GH-270 Store linespacing in the user defaults

This commit is contained in:
Tae Won Ha 2016-10-26 22:59:48 +02:00
parent 861a362f9f
commit 12a9a54efe
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 72 additions and 12 deletions

View File

@ -30,6 +30,10 @@ public class NeoVimView: NSView, NSUserInterfaceValidations {
public static let minFontSize = CGFloat(4)
public static let maxFontSize = CGFloat(128)
public static let defaultFont = NSFont.userFixedPitchFont(ofSize: 13)!
public static let defaultLinespacing = CGFloat(1)
public static let minLinespacing = CGFloat(0.5)
public static let maxLinespacing = CGFloat(8)
public let uuid = UUID().uuidString
public weak var delegate: NeoVimViewDelegate?
@ -42,7 +46,24 @@ public class NeoVimView: NSView, NSUserInterfaceValidations {
self.needsDisplay = true
}
}
public var linespacing: CGFloat {
get {
return self._linespacing
}
set {
guard newValue >= NeoVimView.minLinespacing && newValue <= NeoVimView.maxLinespacing else {
return
}
self._linespacing = newValue
self.drawer.linespacing = self.linespacing
self.updateFontMetaData()
}
}
public var font: NSFont {
get {
return self._font
@ -60,11 +81,8 @@ public class NeoVimView: NSView, NSUserInterfaceValidations {
self._font = newValue
self.drawer.font = self.font
self.cellSize = self.drawer.cellSize
self.descent = self.drawer.descent
self.leading = self.drawer.leading
self.resizeNeoVimUiTo(size: self.bounds.size)
self.updateFontMetaData()
}
}
@ -92,6 +110,7 @@ public class NeoVimView: NSView, NSUserInterfaceValidations {
].flatMap { $0 }
fileprivate var _font = NeoVimView.defaultFont
fileprivate var _linespacing = NeoVimView.defaultLinespacing
fileprivate let agent: NeoVimAgent
fileprivate let drawer: TextDrawer
@ -185,6 +204,14 @@ public class NeoVimView: NSView, NSUserInterfaceValidations {
NSLog("\(Set(buffers))")
NSLog("DEBUG 1 - End")
}
fileprivate func updateFontMetaData() {
self.cellSize = self.drawer.cellSize
self.descent = self.drawer.descent
self.leading = self.drawer.leading
self.resizeNeoVimUiTo(size: self.bounds.size)
}
}
// MARK: - API

View File

@ -9,11 +9,14 @@ import RxSwift
struct AppearancePrefData: Equatable {
let editorFont: NSFont
let editorFontLinespacing: CGFloat
let editorUsesLigatures: Bool
}
func == (left: AppearancePrefData, right: AppearancePrefData) -> Bool {
return left.editorUsesLigatures == right.editorUsesLigatures && left.editorFont.isEqual(to: right.editorFont)
return left.editorUsesLigatures == right.editorUsesLigatures
&& left.editorFont.isEqual(to: right.editorFont)
&& left.editorFontLinespacing == right.editorFontLinespacing
}
class AppearancePrefPane: PrefPane, NSComboBoxDelegate, NSControlTextEditingDelegate {
@ -181,7 +184,9 @@ class AppearancePrefPane: PrefPane, NSComboBoxDelegate, NSControlTextEditingDele
extension AppearancePrefPane {
func usesLigaturesAction(_ sender: NSButton) {
self.set(data: AppearancePrefData(editorFont: self.data.editorFont, editorUsesLigatures: sender.boolState))
self.set(data: AppearancePrefData(editorFont: self.data.editorFont,
editorFontLinespacing: self.data.editorFontLinespacing,
editorUsesLigatures: sender.boolState))
}
func fontPopupAction(_ sender: NSPopUpButton) {
@ -197,7 +202,9 @@ extension AppearancePrefPane {
return
}
self.set(data: AppearancePrefData(editorFont: newFont, editorUsesLigatures: self.data.editorUsesLigatures))
self.set(data: AppearancePrefData(editorFont: newFont,
editorFontLinespacing: self.data.editorFontLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
}
func comboBoxSelectionDidChange(_ notification: Notification) {
@ -208,14 +215,18 @@ extension AppearancePrefPane {
let newFontSize = self.cappedFontSize(Int(self.sizes[self.sizeCombo.indexOfSelectedItem]))
let newFont = self.fontManager.convert(self.data.editorFont, toSize: newFontSize)
self.set(data: AppearancePrefData(editorFont: newFont, editorUsesLigatures: self.data.editorUsesLigatures))
self.set(data: AppearancePrefData(editorFont: newFont,
editorFontLinespacing: self.data.editorFontLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
}
func sizeComboBoxDidEnter(_ sender: AnyObject!) {
let newFontSize = self.cappedFontSize(self.sizeCombo.integerValue)
let newFont = self.fontManager.convert(self.data.editorFont, toSize: newFontSize)
self.set(data: AppearancePrefData(editorFont: newFont, editorUsesLigatures: self.data.editorUsesLigatures))
self.set(data: AppearancePrefData(editorFont: newFont,
editorFontLinespacing: self.data.editorFontLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
}
fileprivate func cappedFontSize(_ size: Int) -> CGFloat {

View File

@ -22,6 +22,7 @@ private class PrefKeys {
static let editorFontName = "editor-font-name"
static let editorFontSize = "editor-font-size"
static let editorFontLinespacing = "editor-font-linespacing"
static let editorUsesLigatures = "editor-uses-ligatures"
static let useSnapshotUpdateChannel = "use-snapshot-update-channel"
@ -38,10 +39,15 @@ private class PrefKeys {
class PrefStore: StandardFlow {
fileprivate static let compatibleVersion = "38"
fileprivate static let defaultEditorFont = NeoVimView.defaultFont
static let minEditorFontSize = NeoVimView.minFontSize
static let maxEditorFontSize = NeoVimView.maxFontSize
fileprivate static let defaultEditorFontLinespacing = NeoVimView.defaultLinespacing
static let minEditorFontLinespacing = NeoVimView.minLinespacing
static let maxEditorFontLinespacing = NeoVimView.maxLinespacing
fileprivate let userDefaults = UserDefaults.standard
fileprivate let fontManager = NSFontManager.shared()
@ -49,7 +55,9 @@ class PrefStore: StandardFlow {
general: GeneralPrefData(openNewWindowWhenLaunching: true,
openNewWindowOnReactivation: true,
ignorePatterns: Set([ "*/.git", "*.o", "*.d", "*.dia" ].map(FileItemIgnorePattern.init))),
appearance: AppearancePrefData(editorFont: PrefStore.defaultEditorFont, editorUsesLigatures: false),
appearance: AppearancePrefData(editorFont: PrefStore.defaultEditorFont,
editorFontLinespacing: 1,
editorUsesLigatures: false),
advanced: AdvancedPrefData(useSnapshotUpdateChannel: false,
useInteractiveZsh: false),
mainWindow: MainWindowPrefData(isAllToolsVisible: true,
@ -77,6 +85,9 @@ class PrefStore: StandardFlow {
let editorFont = self.saneFont(editorFontName, fontSize: editorFontSize)
let usesLigatures = self.bool(from: prefs, for: PrefKeys.editorUsesLigatures, default: false)
let linespacing = self.saneLinespacing(
CGFloat((prefs[PrefKeys.editorFontLinespacing] as? NSNumber)?.floatValue ?? Float(1))
)
let openNewWindowWhenLaunching = self.bool(from: prefs, for: PrefKeys.openNewWindowWhenLaunching, default: true)
let openNewWindowOnReactivation = self.bool(from: prefs, for: PrefKeys.openNewWindowOnReactivation, default: true)
@ -97,7 +108,9 @@ class PrefStore: StandardFlow {
openNewWindowOnReactivation: openNewWindowOnReactivation,
ignorePatterns: ignorePatterns
),
appearance: AppearancePrefData(editorFont: editorFont, editorUsesLigatures: usesLigatures),
appearance: AppearancePrefData(editorFont: editorFont,
editorFontLinespacing: linespacing,
editorUsesLigatures: usesLigatures),
advanced: AdvancedPrefData(useSnapshotUpdateChannel: useSnapshotUpdate,
useInteractiveZsh: useInteractiveZsh),
mainWindow: MainWindowPrefData(isAllToolsVisible: isAllToolsVisible,
@ -123,6 +136,14 @@ class PrefStore: StandardFlow {
return editorFont
}
fileprivate func saneLinespacing(_ linespacing: CGFloat) -> CGFloat {
guard linespacing >= PrefStore.minEditorFontLinespacing && linespacing <= PrefStore.maxEditorFontLinespacing else {
return PrefStore.defaultEditorFontLinespacing
}
return linespacing
}
fileprivate func prefsDict(_ prefData: PrefData) -> [String: Any] {
let generalData = prefData.general
let appearanceData = prefData.appearance
@ -140,6 +161,7 @@ class PrefStore: StandardFlow {
// Appearance
PrefKeys.editorFontName: appearanceData.editorFont.fontName as Any,
PrefKeys.editorFontSize: appearanceData.editorFont.pointSize as Any,
PrefKeys.editorFontLinespacing: appearanceData.editorFontLinespacing as Any,
PrefKeys.editorUsesLigatures: appearanceData.editorUsesLigatures as Any,
// Advanced