macOS: Disable save button if the syntax highlighter detects any errors

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-01-22 12:57:27 +05:30
parent d37e230ee0
commit 6f9fa35c5a
2 changed files with 10 additions and 3 deletions

View File

@ -7,7 +7,7 @@ class ConfTextView: NSTextView {
private let confTextStorage = ConfTextStorage() private let confTextStorage = ConfTextStorage()
var hasError: Bool { return confTextStorage.hasError } @objc dynamic var hasError: Bool = false
@objc dynamic var privateKeyString: String? @objc dynamic var privateKeyString: String?
override var string: String { override var string: String {
@ -58,6 +58,9 @@ extension ConfTextView: NSTextViewDelegate {
func textDidChange(_ notification: Notification) { func textDidChange(_ notification: Notification) {
confTextStorage.highlightSyntax() confTextStorage.highlightSyntax()
if hasError != confTextStorage.hasError {
hasError = confTextStorage.hasError
}
if privateKeyString != confTextStorage.privateKeyString { if privateKeyString != confTextStorage.privateKeyString {
privateKeyString = confTextStorage.privateKeyString privateKeyString = confTextStorage.privateKeyString
} }

View File

@ -84,7 +84,8 @@ class TunnelEditViewController: NSViewController {
weak var delegate: TunnelEditViewControllerDelegate? weak var delegate: TunnelEditViewControllerDelegate?
var textViewObservationToken: AnyObject? var privateKeyObservationToken: AnyObject?
var hasErrorObservationToken: AnyObject?
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer?) { init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer?) {
self.tunnelsManager = tunnelsManager self.tunnelsManager = tunnelsManager
@ -119,7 +120,7 @@ class TunnelEditViewController: NSViewController {
textView.string = bootstrappingText textView.string = bootstrappingText
selectedActivateOnDemandOption = .none selectedActivateOnDemandOption = .none
} }
textViewObservationToken = textView.observe(\.privateKeyString) { [weak publicKeyRow] textView, _ in privateKeyObservationToken = textView.observe(\.privateKeyString) { [weak publicKeyRow] textView, _ in
if let privateKeyString = textView.privateKeyString, if let privateKeyString = textView.privateKeyString,
let privateKey = Data(base64Encoded: privateKeyString), let privateKey = Data(base64Encoded: privateKeyString),
privateKey.count == TunnelConfiguration.keyLength { privateKey.count == TunnelConfiguration.keyLength {
@ -129,6 +130,9 @@ class TunnelEditViewController: NSViewController {
publicKeyRow?.value = "" publicKeyRow?.value = ""
} }
} }
hasErrorObservationToken = textView.observe(\.hasError) { [weak saveButton] textView, _ in
saveButton?.isEnabled = !textView.hasError
}
onDemandRow.valueOptions = activateOnDemandOptions.map { TunnelViewModel.activateOnDemandOptionText(for: $0) } onDemandRow.valueOptions = activateOnDemandOptions.map { TunnelViewModel.activateOnDemandOptionText(for: $0) }
onDemandRow.selectedOptionIndex = activateOnDemandOptions.firstIndex(of: selectedActivateOnDemandOption)! onDemandRow.selectedOptionIndex = activateOnDemandOptions.firstIndex(of: selectedActivateOnDemandOption)!