From 80977b95dec7224519c0a9b775f4fe4a203f6966 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 9 Jan 2019 00:41:36 +0530 Subject: macOS: Edit view: Update public key as you edit Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift | 6 ++++++ WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift | 4 ++++ 2 files changed, 10 insertions(+) (limited to 'WireGuard/WireGuard/UI/macOS/View') diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift index 2a9d36d..75c1829 100644 --- a/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift +++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift @@ -27,6 +27,7 @@ class ConfTextStorage: NSTextStorage { private let backingStore: NSMutableAttributedString private(set) var hasError = false + private(set) var privateKeyString: String? override init() { backingStore = NSMutableAttributedString(string: "") @@ -129,6 +130,7 @@ class ConfTextStorage: NSTextStorage { func highlightSyntax() { hasError = false + privateKeyString = nil backingStore.beginEditing() var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))! @@ -143,6 +145,10 @@ class ConfTextStorage: NSTextStorage { hasError = true } + if span.type == HighlightPrivateKey { + privateKeyString = backingStore.attributedSubstring(from: NSRange(location: span.start, length: span.len)).string + } + spans = spans.successor() } backingStore.endEditing() diff --git a/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift b/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift index cd91b01..eda5ee9 100644 --- a/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift +++ b/WireGuard/WireGuard/UI/macOS/View/ConfTextView.swift @@ -8,6 +8,7 @@ class ConfTextView: NSTextView { private let confTextStorage = ConfTextStorage() var hasError: Bool { return confTextStorage.hasError } + @objc dynamic var privateKeyString: String? override var string: String { didSet { @@ -51,6 +52,9 @@ extension ConfTextView: NSTextViewDelegate { func textDidChange(_ notification: Notification) { confTextStorage.highlightSyntax() + if privateKeyString != confTextStorage.privateKeyString { + privateKeyString = confTextStorage.privateKeyString + } needsDisplay = true } -- cgit v1.2.3-59-g8ed1b