From 1bc21778833c2031d6f10819a9f19ff1d7a00648 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Tue, 22 Jan 2019 01:19:16 +0530 Subject: [PATCH] macOS: Reset attributes for each syntax highlight cycle Signed-off-by: Roopesh Chander --- .../UI/macOS/View/ConfTextColorTheme.swift | 14 ++++++++++++-- .../WireGuard/UI/macOS/View/ConfTextStorage.swift | 11 ++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift index e633bdf..b258f8b 100644 --- a/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift +++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextColorTheme.swift @@ -4,10 +4,16 @@ import Cocoa protocol ConfTextColorTheme { + var defaultColor: NSColor { get } + func color(for: highlight_type) -> NSColor } 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: @@ -27,12 +33,16 @@ struct ConfTextAquaColorTheme: ConfTextColorTheme { case HighlightError.rawValue: return NSColor(hex: "#C41A16") // Strings in Xcode default: - return NSColor(hex: "#000000") // Plain text in Xcode + return defaultColor } } } 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: @@ -52,7 +62,7 @@ struct ConfTextDarkAquaColorTheme: ConfTextColorTheme { case HighlightError.rawValue: return NSColor(hex: "#FF4C4C") // Strings in Xcode default: - return NSColor(hex: "#FFFFFF") // Plain text in Xcode + return defaultColor } } } diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift index 0a38d4e..511a35c 100644 --- a/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift +++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift @@ -85,7 +85,16 @@ class ConfTextStorage: NSTextStorage { hasError = false privateKeyString = nil + let fullTextRange = NSRange(location: 0, length: (backingStore.string as NSString).length) + backingStore.beginEditing() + if let textColorTheme = textColorTheme { + let defaultAttributes: [NSAttributedString.Key: Any] = [ + .foregroundColor: textColorTheme.defaultColor, + .font: defaultFont + ] + backingStore.setAttributes(defaultAttributes, range: fullTextRange) + } var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))! while spans.pointee.type != HighlightEnd { @@ -111,7 +120,7 @@ class ConfTextStorage: NSTextStorage { backingStore.endEditing() beginEditing() - edited(.editedAttributes, range: NSRange(location: 0, length: (backingStore.string as NSString).length), changeInLength: 0) + edited(.editedAttributes, range: fullTextRange, changeInLength: 0) endEditing() }