mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-14 02:27:38 -07:00
limit the join network combobox to hex characters.
Join only clickable when a valid network is entered
This commit is contained in:
parent
225c2b095b
commit
b4a8057994
3 changed files with 103 additions and 5 deletions
|
@ -8,11 +8,77 @@
|
|||
|
||||
import Cocoa
|
||||
|
||||
class JoinNetworkViewController: NSViewController {
|
||||
extension String {
|
||||
func contains(find: String) -> Bool {
|
||||
return self.rangeOfString(find) != nil
|
||||
}
|
||||
|
||||
func trunc(length: Int, trailing: String? = "...") -> String {
|
||||
if self.characters.count > length {
|
||||
return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "")
|
||||
} else {
|
||||
return self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JoinNetworkViewController: NSViewController, NSComboBoxDelegate {
|
||||
|
||||
@IBOutlet var network: NSComboBox!
|
||||
@IBOutlet var joinButton: NSButton!
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
// Do view setup here.
|
||||
network.setDelegate(self)
|
||||
}
|
||||
|
||||
|
||||
@IBAction func onJoinClicked(sender: AnyObject?) {
|
||||
let networkId = UInt64(network.stringValue, radix: 16)
|
||||
|
||||
// TODO: Execute join network call
|
||||
|
||||
network.stringValue = ""
|
||||
}
|
||||
|
||||
|
||||
// NSComboBoxDelegate Methods
|
||||
|
||||
override func controlTextDidChange(obj: NSNotification) {
|
||||
let cb = obj.object as! NSComboBox
|
||||
let value = cb.stringValue
|
||||
|
||||
|
||||
let allowedCharacters = "abcdefABCDEF0123456789"
|
||||
|
||||
var outValue = ""
|
||||
|
||||
for char in value.characters {
|
||||
if !allowedCharacters.contains(String(char)) {
|
||||
NSBeep()
|
||||
}
|
||||
else {
|
||||
outValue += String(char)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 16 {
|
||||
joinButton.enabled = true
|
||||
}
|
||||
else {
|
||||
|
||||
if outValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 16 {
|
||||
outValue = outValue.trunc(16, trailing: "")
|
||||
NSBeep()
|
||||
joinButton.enabled = true
|
||||
}
|
||||
else {
|
||||
joinButton.enabled = false
|
||||
}
|
||||
}
|
||||
|
||||
cb.stringValue = outValue
|
||||
}
|
||||
// end NSComboBoxDelegate Methods
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue