macOS: Edit view: Update public key as you edit
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
@@ -27,6 +27,7 @@ class ConfTextStorage: NSTextStorage {
|
|||||||
|
|
||||||
private let backingStore: NSMutableAttributedString
|
private let backingStore: NSMutableAttributedString
|
||||||
private(set) var hasError = false
|
private(set) var hasError = false
|
||||||
|
private(set) var privateKeyString: String?
|
||||||
|
|
||||||
override init() {
|
override init() {
|
||||||
backingStore = NSMutableAttributedString(string: "")
|
backingStore = NSMutableAttributedString(string: "")
|
||||||
@@ -129,6 +130,7 @@ class ConfTextStorage: NSTextStorage {
|
|||||||
|
|
||||||
func highlightSyntax() {
|
func highlightSyntax() {
|
||||||
hasError = false
|
hasError = false
|
||||||
|
privateKeyString = nil
|
||||||
|
|
||||||
backingStore.beginEditing()
|
backingStore.beginEditing()
|
||||||
var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))!
|
var spans = highlight_config(backingStore.string.cString(using: String.Encoding.utf8))!
|
||||||
@@ -143,6 +145,10 @@ class ConfTextStorage: NSTextStorage {
|
|||||||
hasError = true
|
hasError = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if span.type == HighlightPrivateKey {
|
||||||
|
privateKeyString = backingStore.attributedSubstring(from: NSRange(location: span.start, length: span.len)).string
|
||||||
|
}
|
||||||
|
|
||||||
spans = spans.successor()
|
spans = spans.successor()
|
||||||
}
|
}
|
||||||
backingStore.endEditing()
|
backingStore.endEditing()
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ class ConfTextView: NSTextView {
|
|||||||
private let confTextStorage = ConfTextStorage()
|
private let confTextStorage = ConfTextStorage()
|
||||||
|
|
||||||
var hasError: Bool { return confTextStorage.hasError }
|
var hasError: Bool { return confTextStorage.hasError }
|
||||||
|
@objc dynamic var privateKeyString: String?
|
||||||
|
|
||||||
override var string: String {
|
override var string: String {
|
||||||
didSet {
|
didSet {
|
||||||
@@ -51,6 +52,9 @@ extension ConfTextView: NSTextViewDelegate {
|
|||||||
|
|
||||||
func textDidChange(_ notification: Notification) {
|
func textDidChange(_ notification: Notification) {
|
||||||
confTextStorage.highlightSyntax()
|
confTextStorage.highlightSyntax()
|
||||||
|
if privateKeyString != confTextStorage.privateKeyString {
|
||||||
|
privateKeyString = confTextStorage.privateKeyString
|
||||||
|
}
|
||||||
needsDisplay = true
|
needsDisplay = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class TunnelEditViewController: NSViewController {
|
|||||||
return publicKeyRow
|
return publicKeyRow
|
||||||
}()
|
}()
|
||||||
|
|
||||||
let textView: NSTextView = {
|
let textView: ConfTextView = {
|
||||||
let textView = ConfTextView()
|
let textView = ConfTextView()
|
||||||
let minWidth: CGFloat = 120
|
let minWidth: CGFloat = 120
|
||||||
let minHeight: CGFloat = 60
|
let minHeight: CGFloat = 60
|
||||||
@@ -62,6 +62,8 @@ class TunnelEditViewController: NSViewController {
|
|||||||
let tunnelsManager: TunnelsManager
|
let tunnelsManager: TunnelsManager
|
||||||
let tunnel: TunnelContainer?
|
let tunnel: TunnelContainer?
|
||||||
|
|
||||||
|
var textViewObservationToken: AnyObject?
|
||||||
|
|
||||||
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer?) {
|
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer?) {
|
||||||
self.tunnelsManager = tunnelsManager
|
self.tunnelsManager = tunnelsManager
|
||||||
self.tunnel = tunnel
|
self.tunnel = tunnel
|
||||||
@@ -75,8 +77,19 @@ class TunnelEditViewController: NSViewController {
|
|||||||
override func loadView() {
|
override func loadView() {
|
||||||
if let tunnel = tunnel, let tunnelConfiguration = tunnel.tunnelConfiguration {
|
if let tunnel = tunnel, let tunnelConfiguration = tunnel.tunnelConfiguration {
|
||||||
nameRow.value = tunnel.name
|
nameRow.value = tunnel.name
|
||||||
publicKeyRow.value = tunnelConfiguration.interface.publicKey.base64EncodedString()
|
|
||||||
textView.string = tunnelConfiguration.asWgQuickConfig()
|
textView.string = tunnelConfiguration.asWgQuickConfig()
|
||||||
|
publicKeyRow.value = tunnelConfiguration.interface.publicKey.base64EncodedString()
|
||||||
|
textView.privateKeyString = tunnelConfiguration.interface.privateKey.base64EncodedString()
|
||||||
|
textViewObservationToken = textView.observe(\.privateKeyString) { [weak publicKeyRow] textView, _ in
|
||||||
|
if let privateKeyString = textView.privateKeyString,
|
||||||
|
let privateKey = Data(base64Encoded: privateKeyString),
|
||||||
|
privateKey.count == TunnelConfiguration.keyLength {
|
||||||
|
let publicKey = Curve25519.generatePublicKey(fromPrivateKey: privateKey)
|
||||||
|
publicKeyRow?.value = publicKey.base64EncodedString()
|
||||||
|
} else {
|
||||||
|
publicKeyRow?.value = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollView.documentView = textView
|
scrollView.documentView = textView
|
||||||
|
|||||||
Reference in New Issue
Block a user