limit the join network combobox to hex characters.

Join only clickable when a valid network is entered
This commit is contained in:
Grant Limberg 2016-05-16 17:51:14 -07:00
parent 225c2b095b
commit b4a8057994
3 changed files with 103 additions and 5 deletions

View File

@ -287,6 +287,7 @@
93326BE71CE7C816005CA2AC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};

View File

@ -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
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10116" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10116"/>
@ -7,14 +7,45 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="JoinNetworkViewController" customModule="ZeroTier_One" customModuleProvider="target">
<connections>
<outlet property="joinButton" destination="BGy-vd-NQX" id="LGE-2G-7ND"/>
<outlet property="network" destination="BQy-d9-BNg" id="Yf7-BG-c84"/>
<outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="Hz6-mo-xeY">
<rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
<rect key="frame" x="0.0" y="0.0" width="369" height="101"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="puT-Yk-CWC">
<rect key="frame" x="18" y="61" width="107" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Enter Network ID" id="oYH-gS-BX5">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<comboBox verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="BQy-d9-BNg">
<rect key="frame" x="131" y="57" width="221" height="26"/>
<comboBoxCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" completes="NO" numberOfVisibleItems="5" id="n71-4S-AaI">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</comboBoxCell>
</comboBox>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="BGy-vd-NQX">
<rect key="frame" x="274" y="13" width="81" height="32"/>
<buttonCell key="cell" type="push" title="Join" bezelStyle="rounded" alignment="center" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="6Rp-TA-XLl">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onJoinClicked:" target="-2" id="kzC-GT-JKb"/>
</connections>
</button>
</subviews>
<point key="canvasLocation" x="249.5" y="361.5"/>
</customView>
</objects>
</document>