2019-01-07 12:47:27 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.
|
2019-01-07 12:47:27 +00:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class ConfTextView: NSTextView {
|
|
|
|
|
|
|
|
private let confTextStorage = ConfTextStorage()
|
|
|
|
|
2019-01-22 07:27:27 +00:00
|
|
|
@objc dynamic var hasError: Bool = false
|
2019-01-08 19:11:36 +00:00
|
|
|
@objc dynamic var privateKeyString: String?
|
2019-02-16 12:55:17 +00:00
|
|
|
@objc dynamic var singlePeerAllowedIPs: [String]?
|
2019-01-07 12:47:27 +00:00
|
|
|
|
|
|
|
override var string: String {
|
|
|
|
didSet {
|
|
|
|
confTextStorage.highlightSyntax()
|
2019-03-22 10:22:39 +00:00
|
|
|
updateConfigData()
|
2019-01-07 12:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-01-10 10:15:40 +00:00
|
|
|
allowsUndo = true
|
|
|
|
isAutomaticSpellingCorrectionEnabled = false
|
|
|
|
isAutomaticDataDetectionEnabled = false
|
|
|
|
isAutomaticLinkDetectionEnabled = false
|
|
|
|
isAutomaticTextCompletionEnabled = false
|
|
|
|
isAutomaticTextReplacementEnabled = false
|
|
|
|
isAutomaticDashSubstitutionEnabled = false
|
|
|
|
isAutomaticQuoteSubstitutionEnabled = false
|
2019-01-07 12:47:27 +00:00
|
|
|
updateTheme()
|
|
|
|
delegate = self
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidChangeEffectiveAppearance() {
|
|
|
|
updateTheme()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateTheme() {
|
|
|
|
switch effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) ?? .aqua {
|
|
|
|
case .darkAqua:
|
2019-01-21 21:13:14 +00:00
|
|
|
confTextStorage.updateAttributes(for: ConfTextDarkAquaColorTheme.self)
|
2019-01-07 12:47:27 +00:00
|
|
|
default:
|
2019-01-21 21:13:14 +00:00
|
|
|
confTextStorage.updateAttributes(for: ConfTextAquaColorTheme.self)
|
2019-01-07 12:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 10:22:39 +00:00
|
|
|
private func updateConfigData() {
|
|
|
|
if hasError != confTextStorage.hasError {
|
|
|
|
hasError = confTextStorage.hasError
|
|
|
|
}
|
|
|
|
if privateKeyString != confTextStorage.privateKeyString {
|
|
|
|
privateKeyString = confTextStorage.privateKeyString
|
|
|
|
}
|
|
|
|
let hasSyntaxError = confTextStorage.hasError
|
|
|
|
let hasSemanticError = confTextStorage.privateKeyString == nil || !confTextStorage.lastOnePeerHasPublicKey
|
|
|
|
let updatedSinglePeerAllowedIPs = confTextStorage.hasOnePeer && !hasSyntaxError && !hasSemanticError ? confTextStorage.lastOnePeerAllowedIPs : nil
|
|
|
|
if singlePeerAllowedIPs != updatedSinglePeerAllowedIPs {
|
|
|
|
singlePeerAllowedIPs = updatedSinglePeerAllowedIPs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:55:17 +00:00
|
|
|
func setConfText(_ text: String) {
|
|
|
|
let fullTextRange = NSRange(location: 0, length: (string as NSString).length)
|
|
|
|
if shouldChangeText(in: fullTextRange, replacementString: text) {
|
|
|
|
replaceCharacters(in: fullTextRange, with: text)
|
|
|
|
didChangeText()
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 12:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ConfTextView: NSTextViewDelegate {
|
|
|
|
|
|
|
|
func textDidChange(_ notification: Notification) {
|
|
|
|
confTextStorage.highlightSyntax()
|
2019-03-22 10:22:39 +00:00
|
|
|
updateConfigData()
|
2019-01-07 12:47:27 +00:00
|
|
|
needsDisplay = true
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|