aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/View
diff options
context:
space:
mode:
Diffstat (limited to 'WireGuard/WireGuard/UI/macOS/View')
-rw-r--r--WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift58
-rw-r--r--WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift107
-rw-r--r--WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift6
3 files changed, 85 insertions, 86 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift
new file mode 100644
index 0000000..e633bdf
--- /dev/null
+++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+protocol ConfTextColorTheme {
+ func color(for: highlight_type) -> NSColor
+}
+
+struct ConfTextAquaColorTheme: ConfTextColorTheme {
+ 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 NSColor(hex: "#000000") // Plain text in Xcode
+ }
+ }
+}
+
+struct ConfTextDarkAquaColorTheme: ConfTextColorTheme {
+ 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 NSColor(hex: "#FFFFFF") // Plain text in Xcode
+ }
+ }
+}
diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift
index 7d32b7e..0a38d4e 100644
--- a/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift
+++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift
@@ -7,24 +7,11 @@ private let fontSize: CGFloat = 15
class ConfTextStorage: NSTextStorage {
- struct TextColorTheme {
- let plainText: NSColor
- let sections: NSColor
- let keyType: NSColor
- let key: NSColor
- let url: NSColor
- let urlAttribute: NSColor
- let comments: NSColor
- let number: NSColor
- let error: NSColor
- }
-
let defaultFont = NSFontManager.shared.convertWeight(true, of: NSFont.systemFont(ofSize: fontSize))
private let boldFont = NSFont.boldSystemFont(ofSize: fontSize)
private lazy var italicFont = NSFontManager.shared.convert(defaultFont, toHaveTrait: .italicFontMask)
- private var defaultAttributes: [NSAttributedString.Key: Any]! //swiftlint:disable:this implicitly_unwrapped_optional
- private var highlightAttributes: [UInt32: [NSAttributedString.Key: Any]]! //swiftlint:disable:this implicitly_unwrapped_optional
+ private var textColorTheme: ConfTextColorTheme?
private let backingStore: NSMutableAttributedString
private(set) var hasError = false
@@ -43,73 +30,25 @@ class ConfTextStorage: NSTextStorage {
fatalError("init(pasteboardPropertyList:ofType:) has not been implemented")
}
- //swiftlint:disable:next function_body_length
- func updateAttributes(for theme: TextColorTheme) {
- self.defaultAttributes = [
- .foregroundColor: theme.plainText,
- .font: defaultFont
- ]
-
- self.highlightAttributes = [
- HighlightSection.rawValue: [
- .foregroundColor: theme.sections,
- .font: boldFont
- ],
- HighlightField.rawValue: [
- .foregroundColor: theme.keyType,
- .font: boldFont
- ],
- HighlightPublicKey.rawValue: [
- .foregroundColor: theme.key,
- .font: defaultFont
- ],
- HighlightPrivateKey.rawValue: [
- .foregroundColor: theme.key,
- .font: defaultFont
- ],
- HighlightPresharedKey.rawValue: [
- .foregroundColor: theme.key,
- .font: defaultFont
- ],
- HighlightIP.rawValue: [
- .foregroundColor: theme.url,
- .font: defaultFont
- ],
- HighlightCidr.rawValue: [
- .foregroundColor: theme.urlAttribute,
- .font: defaultFont
- ],
- HighlightHost.rawValue: [
- .foregroundColor: theme.url,
- .font: defaultFont
- ],
- HighlightPort.rawValue: [
- .foregroundColor: theme.urlAttribute,
- .font: defaultFont
- ],
- HighlightMTU.rawValue: [
- .foregroundColor: theme.number,
- .font: defaultFont
- ],
- HighlightKeepalive.rawValue: [
- .foregroundColor: theme.number,
- .font: defaultFont
- ],
- HighlightComment.rawValue: [
- .foregroundColor: theme.comments,
- .font: italicFont
- ],
- HighlightDelimiter.rawValue: [
- .foregroundColor: theme.plainText,
- .font: defaultFont
- ],
- HighlightError.rawValue: [
- .foregroundColor: theme.error,
- .font: defaultFont,
- .underlineStyle: 1
- ]
- ]
+ func nonColorAttributes(for highlightType: highlight_type) -> [NSAttributedString.Key: Any] {
+ switch highlightType.rawValue {
+ case HighlightSection.rawValue, HighlightField.rawValue:
+ return [.font: boldFont]
+ case HighlightPublicKey.rawValue, HighlightPrivateKey.rawValue, HighlightPresharedKey.rawValue,
+ HighlightIP.rawValue, HighlightCidr.rawValue, HighlightHost.rawValue, HighlightPort.rawValue,
+ HighlightMTU.rawValue, HighlightKeepalive.rawValue, HighlightDelimiter.rawValue:
+ return [.font: defaultFont]
+ case HighlightComment.rawValue:
+ return [.font: italicFont]
+ case HighlightError.rawValue:
+ return [.font: defaultFont, .underlineStyle: 1]
+ default:
+ return [:]
+ }
+ }
+ func updateAttributes(for textColorTheme: ConfTextColorTheme) {
+ self.textColorTheme = textColorTheme
highlightSyntax()
}
@@ -152,8 +91,12 @@ class ConfTextStorage: NSTextStorage {
while spans.pointee.type != HighlightEnd {
let span = spans.pointee
- let attributes = self.highlightAttributes[span.type.rawValue] ?? defaultAttributes
- backingStore.setAttributes(attributes, 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)
+ if let textColorTheme = textColorTheme {
+ let color = textColorTheme.color(for: span.type)
+ backingStore.addAttribute(.foregroundColor, value: color, range: range)
+ }
if span.type == HighlightError {
hasError = true
diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
index 2a9f633..481cbaa 100644
--- a/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
+++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
@@ -44,14 +44,12 @@ class ConfTextView: NSTextView {
}
private func updateTheme() {
- let theme: ConfTextStorage.TextColorTheme
switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua {
case .darkAqua:
- theme = ConfTextStorage.TextColorTheme(plainText: NSColor(hex: "#FFFFFF"), sections: NSColor(hex: "#91D462"), keyType: NSColor(hex: "#FC5FA3"), key: NSColor(hex: "#FD8F3F"), url: NSColor(hex: "#53A5FB"), urlAttribute: NSColor(hex: "#75B492"), comments: NSColor(hex: "#6C7986"), number: NSColor(hex: "#9686F5"), error: NSColor(hex: "#FF4C4C"))
+ confTextStorage.updateAttributes(for: ConfTextDarkAquaColorTheme())
default:
- theme = ConfTextStorage.TextColorTheme(plainText: NSColor(hex: "#000000"), sections: NSColor(hex: "#326D74"), keyType: NSColor(hex: "#9B2393"), key: NSColor(hex: "#643820"), url: NSColor(hex: "#0E0EFF"), urlAttribute: NSColor(hex: "#815F03"), comments: NSColor(hex: "#536579"), number: NSColor(hex: "#1C00CF"), error: NSColor(hex: "#C41A16"))
+ confTextStorage.updateAttributes(for: ConfTextAquaColorTheme())
}
- confTextStorage.updateAttributes(for: theme)
}
}