macOS: Make highlighter themes static

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-01-21 22:13:14 +01:00
parent b22aeaeb20
commit d7a88300f6
3 changed files with 18 additions and 21 deletions

View File

@ -4,13 +4,13 @@
import Cocoa import Cocoa
protocol ConfTextColorTheme { protocol ConfTextColorTheme {
var defaultColor: NSColor { get } static var defaultColor: NSColor { get }
var colorMap: [UInt32: NSColor] { get } static var colorMap: [UInt32: NSColor] { get }
} }
struct ConfTextAquaColorTheme: ConfTextColorTheme { struct ConfTextAquaColorTheme: ConfTextColorTheme {
var defaultColor = NSColor(hex: "#000000") static let defaultColor = NSColor(hex: "#000000")
var colorMap: [UInt32: NSColor] = [ static let colorMap: [UInt32: NSColor] = [
HighlightSection.rawValue: NSColor(hex: "#326D74"), // Class name in Xcode HighlightSection.rawValue: NSColor(hex: "#326D74"), // Class name in Xcode
HighlightField.rawValue: NSColor(hex: "#9B2393"), // Keywords in Xcode HighlightField.rawValue: NSColor(hex: "#9B2393"), // Keywords in Xcode
HighlightPublicKey.rawValue: NSColor(hex: "#643820"), // Preprocessor directives in Xcode HighlightPublicKey.rawValue: NSColor(hex: "#643820"), // Preprocessor directives in Xcode
@ -28,8 +28,8 @@ struct ConfTextAquaColorTheme: ConfTextColorTheme {
} }
struct ConfTextDarkAquaColorTheme: ConfTextColorTheme { struct ConfTextDarkAquaColorTheme: ConfTextColorTheme {
var defaultColor = NSColor(hex: "#FFFFFF") // Plain text in Xcode static let defaultColor = NSColor(hex: "#FFFFFF") // Plain text in Xcode
var colorMap: [UInt32: NSColor] = [ static let colorMap: [UInt32: NSColor] = [
HighlightSection.rawValue: NSColor(hex: "#91D462"), // Class name in Xcode HighlightSection.rawValue: NSColor(hex: "#91D462"), // Class name in Xcode
HighlightField.rawValue: NSColor(hex: "#FC5FA3"), // Keywords in Xcode HighlightField.rawValue: NSColor(hex: "#FC5FA3"), // Keywords in Xcode
HighlightPublicKey.rawValue: NSColor(hex: "#FD8F3F"), // Preprocessor directives in Xcode HighlightPublicKey.rawValue: NSColor(hex: "#FD8F3F"), // Preprocessor directives in Xcode

View File

@ -11,7 +11,7 @@ class ConfTextStorage: NSTextStorage {
private let boldFont = NSFont.boldSystemFont(ofSize: fontSize) private let boldFont = NSFont.boldSystemFont(ofSize: fontSize)
private lazy var italicFont = NSFontManager.shared.convert(defaultFont, toHaveTrait: .italicFontMask) private lazy var italicFont = NSFontManager.shared.convert(defaultFont, toHaveTrait: .italicFontMask)
private var textColorTheme: ConfTextColorTheme? private var textColorTheme: ConfTextColorTheme.Type?
private let backingStore: NSMutableAttributedString private let backingStore: NSMutableAttributedString
private(set) var hasError = false private(set) var hasError = false
@ -47,7 +47,7 @@ class ConfTextStorage: NSTextStorage {
} }
} }
func updateAttributes(for textColorTheme: ConfTextColorTheme) { func updateAttributes(for textColorTheme: ConfTextColorTheme.Type) {
self.textColorTheme = textColorTheme self.textColorTheme = textColorTheme
highlightSyntax() highlightSyntax()
} }
@ -82,19 +82,18 @@ class ConfTextStorage: NSTextStorage {
} }
func highlightSyntax() { func highlightSyntax() {
guard let textColorTheme = textColorTheme else { return }
hasError = false hasError = false
privateKeyString = nil privateKeyString = nil
let fullTextRange = NSRange(location: 0, length: (backingStore.string as NSString).length) let fullTextRange = NSRange(location: 0, length: (backingStore.string as NSString).length)
backingStore.beginEditing() backingStore.beginEditing()
if let textColorTheme = textColorTheme { let defaultAttributes: [NSAttributedString.Key: Any] = [
let defaultAttributes: [NSAttributedString.Key: Any] = [ .foregroundColor: textColorTheme.defaultColor,
.foregroundColor: textColorTheme.defaultColor, .font: defaultFont
.font: defaultFont ]
] backingStore.setAttributes(defaultAttributes, range: fullTextRange)
backingStore.setAttributes(defaultAttributes, range: fullTextRange)
}
var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))! var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))!
while spans.pointee.type != HighlightEnd { while spans.pointee.type != HighlightEnd {
@ -102,10 +101,8 @@ class ConfTextStorage: NSTextStorage {
let range = NSRange(location: span.start, length: span.len) let range = NSRange(location: span.start, length: span.len)
backingStore.setAttributes(nonColorAttributes(for: span.type), range: range) backingStore.setAttributes(nonColorAttributes(for: span.type), range: range)
if let textColorTheme = textColorTheme { let color = textColorTheme.colorMap[span.type.rawValue] ?? textColorTheme.defaultColor
let color = textColorTheme.colorMap[span.type.rawValue] ?? textColorTheme.defaultColor backingStore.addAttribute(.foregroundColor, value: color, range: range)
backingStore.addAttribute(.foregroundColor, value: color, range: range)
}
if span.type == HighlightError { if span.type == HighlightError {
hasError = true hasError = true

View File

@ -46,9 +46,9 @@ class ConfTextView: NSTextView {
private func updateTheme() { private func updateTheme() {
switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua { switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua {
case .darkAqua: case .darkAqua:
confTextStorage.updateAttributes(for: ConfTextDarkAquaColorTheme()) confTextStorage.updateAttributes(for: ConfTextDarkAquaColorTheme.self)
default: default:
confTextStorage.updateAttributes(for: ConfTextAquaColorTheme()) confTextStorage.updateAttributes(for: ConfTextAquaColorTheme.self)
} }
} }