aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2019-01-07 14:47:27 +0200
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:34 +0530
commit59bfa7f1df470af711187335f604422dfafca634 (patch)
treebcc80efb0af5a800fc830837616c63d06301a878 /WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
parentmacOS: Tunnel edit view (diff)
downloadwireguard-apple-59bfa7f1df470af711187335f604422dfafca634.tar.xz
wireguard-apple-59bfa7f1df470af711187335f604422dfafca634.zip
Added syntax highlighting conf textview
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift')
-rw-r--r--WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift57
1 files changed, 57 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
new file mode 100644
index 0000000..8526e6c
--- /dev/null
+++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import Cocoa
+
+class ConfTextView: NSTextView {
+
+ private let confTextStorage = ConfTextStorage()
+
+ var hasError: Bool { return confTextStorage.hasError }
+
+ override var string: String {
+ didSet {
+ confTextStorage.highlightSyntax()
+ }
+ }
+
+ init() {
+ let textContainer = NSTextContainer()
+ let layoutManager = NSLayoutManager()
+ layoutManager.addTextContainer(textContainer)
+ confTextStorage.addLayoutManager(layoutManager)
+ super.init(frame: CGRect(x: 0, y: 0, width: 1, height: 60), textContainer: textContainer)
+ font = confTextStorage.defaultFont
+ updateTheme()
+ delegate = self
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ override func viewDidChangeEffectiveAppearance() {
+ updateTheme()
+ }
+
+ private func updateTheme() {
+ let theme: ConfTextStorage.TextColorTheme
+ switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua {
+ case .darkAqua:
+ theme = ConfTextStorage.TextColorTheme(black: NSColor(hex: "#c7c7c7"), red: NSColor(hex: "#dc322f"), green: NSColor(hex: "#859900"), yellow: NSColor(hex: "#c7c400"), blue: NSColor(hex: "#268bd2"), magenta: NSColor(hex: "#d33682"), cyan: NSColor(hex: "#2aa198"), white: NSColor(hex: "#383838"), default: NSColor(hex: "#c7c7c7"))
+ default:
+ theme = ConfTextStorage.TextColorTheme(black: NSColor(hex: "#000000"), red: NSColor(hex: "#c91b00"), green: NSColor(hex: "#00c200"), yellow: NSColor(hex: "#c7c400"), blue: NSColor(hex: "#0225c7"), magenta: NSColor(hex: "#c930c7"), cyan: NSColor(hex: "#00c5c7"), white: NSColor(hex: "#c7c7c7"), default: NSColor(hex: "#000000"))
+ }
+ confTextStorage.updateAttributes(for: theme)
+ }
+
+}
+
+extension ConfTextView: NSTextViewDelegate {
+
+ func textDidChange(_ notification: Notification) {
+ confTextStorage.highlightSyntax()
+ needsDisplay = true
+ }
+
+}