macOS: Make color theme use a dict

This commit is contained in:
Roopesh Chander 2019-01-22 01:58:58 +05:30
parent dd38cbe7ac
commit 1df572218d
2 changed files with 34 additions and 55 deletions

View File

@ -5,64 +5,43 @@ import Cocoa
protocol ConfTextColorTheme {
var defaultColor: NSColor { get }
func color(for: highlight_type) -> NSColor
var colorMap: [UInt32: NSColor] { get }
}
struct ConfTextAquaColorTheme: ConfTextColorTheme {
var defaultColor: NSColor {
return NSColor(hex: "#000000") // Plain text in Xcode
}
func color(for highlightType: highlight_type) -> NSColor {
switch highlightType.rawValue {
case HighlightSection.rawValue:
return NSColor(hex: "#326D74") // Class name in Xcode
case HighlightField.rawValue:
return NSColor(hex: "#9B2393") // Keywords in Xcode
case HighlightPublicKey.rawValue, HighlightPrivateKey.rawValue, HighlightPresharedKey.rawValue:
return NSColor(hex: "#643820") // Preprocessor directives in Xcode
case HighlightIP.rawValue, HighlightHost.rawValue:
return NSColor(hex: "#0E0EFF") // URLs in Xcode
case HighlightCidr.rawValue, HighlightPort.rawValue:
return NSColor(hex: "#815F03") // Attributes in Xcode
case HighlightMTU.rawValue, HighlightKeepalive.rawValue:
return NSColor(hex: "#1C00CF") // Numbers in Xcode
case HighlightComment.rawValue:
return NSColor(hex: "#536579") // Comments in Xcode
case HighlightError.rawValue:
return NSColor(hex: "#C41A16") // Strings in Xcode
default:
return defaultColor
}
}
var defaultColor = NSColor(hex: "#000000")
var colorMap: [UInt32: NSColor] = [
HighlightSection.rawValue: NSColor(hex: "#326D74"), // Class name in Xcode
HighlightField.rawValue: NSColor(hex: "#9B2393"), // Keywords in Xcode
HighlightPublicKey.rawValue: NSColor(hex: "#643820"), // Preprocessor directives in Xcode
HighlightPrivateKey.rawValue: NSColor(hex: "#643820"), // Preprocessor directives in Xcode
HighlightPresharedKey.rawValue: NSColor(hex: "#643820"), // Preprocessor directives in Xcode
HighlightIP.rawValue: NSColor(hex: "#0E0EFF"), // URLs in Xcode
HighlightHost.rawValue: NSColor(hex: "#0E0EFF"), // URLs in Xcode
HighlightCidr.rawValue: NSColor(hex: "#815F03"), // Attributes in Xcode
HighlightPort.rawValue: NSColor(hex: "#815F03"), // Attributes in Xcode
HighlightMTU.rawValue: NSColor(hex: "#1C00CF"), // Numbers in Xcode
HighlightKeepalive.rawValue: NSColor(hex: "#1C00CF"), // Numbers in Xcode
HighlightComment.rawValue: NSColor(hex: "#536579"), // Comments in Xcode
HighlightError.rawValue: NSColor(hex: "#C41A16") // Strings in Xcode
]
}
struct ConfTextDarkAquaColorTheme: ConfTextColorTheme {
var defaultColor: NSColor {
return NSColor(hex: "#FFFFFF") // Plain text in Xcode
}
func color(for highlightType: highlight_type) -> NSColor {
switch highlightType.rawValue {
case HighlightSection.rawValue:
return NSColor(hex: "#91D462") // Class name in Xcode
case HighlightField.rawValue:
return NSColor(hex: "#FC5FA3") // Keywords in Xcode
case HighlightPublicKey.rawValue, HighlightPrivateKey.rawValue, HighlightPresharedKey.rawValue:
return NSColor(hex: "#FD8F3F") // Preprocessor directives in Xcode
case HighlightIP.rawValue, HighlightHost.rawValue:
return NSColor(hex: "#53A5FB") // URLs in Xcode
case HighlightCidr.rawValue, HighlightPort.rawValue:
return NSColor(hex: "#75B492") // Attributes in Xcode
case HighlightMTU.rawValue, HighlightKeepalive.rawValue:
return NSColor(hex: "#9686F5") // Numbers in Xcode
case HighlightComment.rawValue:
return NSColor(hex: "#6C7986") // Comments in Xcode
case HighlightError.rawValue:
return NSColor(hex: "#FF4C4C") // Strings in Xcode
default:
return defaultColor
}
}
var defaultColor = NSColor(hex: "#FFFFFF") // Plain text in Xcode
var colorMap: [UInt32: NSColor] = [
HighlightSection.rawValue: NSColor(hex: "#91D462"), // Class name in Xcode
HighlightField.rawValue: NSColor(hex: "#FC5FA3"), // Keywords in Xcode
HighlightPublicKey.rawValue: NSColor(hex: "#FD8F3F"), // Preprocessor directives in Xcode
HighlightPrivateKey.rawValue: NSColor(hex: "#FD8F3F"), // Preprocessor directives in Xcode
HighlightPresharedKey.rawValue: NSColor(hex: "#FD8F3F"), // Preprocessor directives in Xcode
HighlightIP.rawValue: NSColor(hex: "#53A5FB"), // URLs in Xcode
HighlightHost.rawValue: NSColor(hex: "#53A5FB"), // URLs in Xcode
HighlightCidr.rawValue: NSColor(hex: "#75B492"), // Attributes in Xcode
HighlightPort.rawValue: NSColor(hex: "#75B492"), // Attributes in Xcode
HighlightMTU.rawValue: NSColor(hex: "#9686F5"), // Numbers in Xcode
HighlightKeepalive.rawValue: NSColor(hex: "#9686F5"), // Numbers in Xcode
HighlightComment.rawValue: NSColor(hex: "#6C7986"), // Comments in Xcode
HighlightError.rawValue: NSColor(hex: "#FF4C4C") // Strings in Xcode
]
}

View File

@ -103,7 +103,7 @@ class ConfTextStorage: NSTextStorage {
let range = NSRange(location: span.start, length: span.len)
backingStore.setAttributes(nonColorAttributes(for: span.type), range: range)
if let textColorTheme = textColorTheme {
let color = textColorTheme.color(for: span.type)
let color = textColorTheme.colorMap[span.type.rawValue] ?? textColorTheme.defaultColor
backingStore.addAttribute(.foregroundColor, value: color, range: range)
}