Skip to content

Commit 52f07f6

Browse files
committed
Adds selection for gigabytes and megabytes fixes #27
1 parent 437fcff commit 52f07f6

File tree

2 files changed

+82
-30
lines changed

2 files changed

+82
-30
lines changed

TmpDisk/Base.lproj/Main.storyboard

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@
732732
</textFieldCell>
733733
</textField>
734734
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="E2a-a2-Prg">
735-
<rect key="frame" x="195" y="318" width="250" height="21"/>
735+
<rect key="frame" x="195" y="318" width="284" height="21"/>
736736
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
737737
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" placeholderString="You must enter a Disk Name" drawsBackground="YES" id="5ee-ag-nb5">
738738
<font key="font" metaFont="system"/>
@@ -741,7 +741,7 @@
741741
</textFieldCell>
742742
</textField>
743743
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="xyN-2s-pFi">
744-
<rect key="frame" x="216" y="289" width="95" height="21"/>
744+
<rect key="frame" x="216" y="289" width="110" height="21"/>
745745
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
746746
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" title="16" placeholderString="0" drawsBackground="YES" id="nYC-7d-c8X">
747747
<font key="font" metaFont="system"/>
@@ -794,7 +794,7 @@
794794
<stepper horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="P5u-9X-RMA">
795795
<rect key="frame" x="192" y="286" width="19" height="28"/>
796796
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
797-
<stepperCell key="cell" continuous="YES" alignment="left" increment="16" minValue="16" maxValue="32000" doubleValue="16" valueWraps="YES" id="Rpa-l3-js0"/>
797+
<stepperCell key="cell" continuous="YES" alignment="left" increment="16" minValue="16" maxValue="128000" doubleValue="16" valueWraps="YES" id="Rpa-l3-js0"/>
798798
<connections>
799799
<action selector="sizeStepped:" target="4AE-e9-iwB" id="qiA-T1-NOt"/>
800800
</connections>
@@ -829,7 +829,7 @@
829829
</textFieldCell>
830830
</textField>
831831
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="H7j-O1-ZBb">
832-
<rect key="frame" x="195" y="259" width="250" height="21"/>
832+
<rect key="frame" x="195" y="259" width="284" height="21"/>
833833
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
834834
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" placeholderString="Comma separated folder names" drawsBackground="YES" id="ini-oz-Wpw">
835835
<font key="font" metaFont="system"/>
@@ -838,7 +838,7 @@
838838
</textFieldCell>
839839
</textField>
840840
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="MSl-ao-5Ke">
841-
<rect key="frame" x="355" y="285" width="94" height="25"/>
841+
<rect key="frame" x="389" y="285" width="94" height="25"/>
842842
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
843843
<popUpButtonCell key="cell" type="push" title="Custom" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="gcJ-GN-9gz" id="rZ6-s2-bcs">
844844
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
@@ -857,15 +857,6 @@
857857
<action selector="sizeSelected:" target="4AE-e9-iwB" id="qPb-lU-iJO"/>
858858
</connections>
859859
</popUpButton>
860-
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="aqV-UW-cbZ">
861-
<rect key="frame" x="317" y="291" width="24" height="16"/>
862-
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
863-
<textFieldCell key="cell" alignment="left" title="MB" id="hdG-Me-bAt">
864-
<font key="font" usesAppearanceFont="YES"/>
865-
<color key="textColor" name="secondaryLabelColor" catalog="System" colorSpace="catalog"/>
866-
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
867-
</textFieldCell>
868-
</textField>
869860
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="9uZ-cc-D1t">
870861
<rect key="frame" x="193" y="95" width="118" height="18"/>
871862
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
@@ -918,6 +909,23 @@
918909
<action selector="createTapped:" target="4AE-e9-iwB" id="4T6-VP-krq"/>
919910
</connections>
920911
</button>
912+
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="V2p-wL-s18">
913+
<rect key="frame" x="331" y="285" width="57" height="25"/>
914+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
915+
<popUpButtonCell key="cell" type="push" title="MB" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="JxI-0N-oYo" id="ssH-8t-toD">
916+
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
917+
<font key="font" metaFont="menu"/>
918+
<menu key="menu" id="G57-Hf-orQ">
919+
<items>
920+
<menuItem title="MB" state="on" id="JxI-0N-oYo"/>
921+
<menuItem title="GB" id="QDX-KN-Ki5"/>
922+
</items>
923+
</menu>
924+
</popUpButtonCell>
925+
<connections>
926+
<action selector="unitSelected:" target="4AE-e9-iwB" id="KRZ-WI-2q8"/>
927+
</connections>
928+
</popUpButton>
921929
</subviews>
922930
</view>
923931
<connections>
@@ -928,7 +936,7 @@
928936
<outlet property="diskSizeLabel" destination="O1S-ta-weE" id="goH-tF-DiN"/>
929937
<outlet property="diskSizeSelector" destination="MSl-ao-5Ke" id="I5b-Ym-R4q"/>
930938
<outlet property="diskSizeStepper" destination="P5u-9X-RMA" id="HBq-pW-un9"/>
931-
<outlet property="diskSizeSuffixLabel" destination="aqV-UW-cbZ" id="sAX-I9-2Um"/>
939+
<outlet property="diskUnitSelector" destination="V2p-wL-s18" id="NGY-n8-1FI"/>
932940
<outlet property="folders" destination="H7j-O1-ZBb" id="4Rz-tD-q48"/>
933941
<outlet property="hidden" destination="9uZ-cc-D1t" id="pCK-PN-B6b"/>
934942
<outlet property="icon" destination="1T3-Bk-PAe" id="2hJ-Pd-SiY"/>
@@ -1000,11 +1008,11 @@
10001008
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
10011009
<prototypeCellViews>
10021010
<tableCellView id="lFv-aq-hWE">
1003-
<rect key="frame" x="8" y="0.0" width="165" height="24"/>
1011+
<rect key="frame" x="8" y="0.0" width="166" height="24"/>
10041012
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
10051013
<subviews>
10061014
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="l4G-Ut-HMs">
1007-
<rect key="frame" x="0.0" y="4" width="165" height="16"/>
1015+
<rect key="frame" x="0.0" y="4" width="166" height="16"/>
10081016
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
10091017
<textFieldCell key="cell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" title="Table View Cell" id="XWF-fr-bC7">
10101018
<font key="font" usesAppearanceFont="YES"/>
@@ -1035,11 +1043,11 @@
10351043
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
10361044
<prototypeCellViews>
10371045
<tableCellView id="JML-g3-qvT">
1038-
<rect key="frame" x="190" y="0.0" width="113" height="24"/>
1046+
<rect key="frame" x="190" y="0.0" width="94" height="24"/>
10391047
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
10401048
<subviews>
10411049
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="DfW-y0-Gzf">
1042-
<rect key="frame" x="0.0" y="4" width="113" height="16"/>
1050+
<rect key="frame" x="0.0" y="4" width="94" height="16"/>
10431051
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
10441052
<textFieldCell key="cell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" title="Table View Cell" id="t3z-IH-wJR">
10451053
<font key="font" usesAppearanceFont="YES"/>
@@ -1070,11 +1078,11 @@
10701078
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
10711079
<prototypeCellViews>
10721080
<tableCellView id="m4U-hC-HcX">
1073-
<rect key="frame" x="289" y="0.0" width="188" height="24"/>
1081+
<rect key="frame" x="289" y="0.0" width="168" height="24"/>
10741082
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
10751083
<subviews>
10761084
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="5ZJ-fg-CTz">
1077-
<rect key="frame" x="0.0" y="4" width="188" height="16"/>
1085+
<rect key="frame" x="0.0" y="4" width="168" height="16"/>
10781086
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
10791087
<textFieldCell key="cell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" title="Table View Cell" id="qXn-wF-aJV">
10801088
<font key="font" usesAppearanceFont="YES"/>
@@ -1309,7 +1317,7 @@
13091317
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
13101318
<prototypeCellViews>
13111319
<tableCellView id="2W4-bt-YC3">
1312-
<rect key="frame" x="894" y="0.0" width="79" height="24"/>
1320+
<rect key="frame" x="894" y="0.0" width="80" height="24"/>
13131321
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
13141322
<subviews>
13151323
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NeM-An-jyl">

TmpDisk/Views/NewTmpDiskViewController.swift

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class NewTmpDiskViewController: NSViewController, NSTextFieldDelegate {
3030
@IBOutlet weak var diskSizeLabel: NSTextField!
3131
@IBOutlet weak var diskSizeStepper: NSStepper!
3232
@IBOutlet weak var diskSize: NSTextField!
33-
@IBOutlet weak var diskSizeSuffixLabel: NSTextField!
3433
@IBOutlet weak var folders: NSTextField!
3534

3635
@IBOutlet weak var icon: NSImageView!
3736

37+
@IBOutlet weak var diskUnitSelector: NSPopUpButton!
3838
@IBOutlet weak var diskSizeSelector: NSPopUpButton!
3939

4040
@IBOutlet weak var autoCreate: NSButton!
@@ -44,6 +44,7 @@ class NewTmpDiskViewController: NSViewController, NSTextFieldDelegate {
4444
@IBOutlet weak var journaled: NSButton!
4545

4646
var volume = TmpDiskVolume()
47+
var unitIndex = 0
4748

4849
// MARK: - View controller lifecycle
4950

@@ -61,27 +62,56 @@ class NewTmpDiskViewController: NSViewController, NSTextFieldDelegate {
6162
self.volume.name = textField.stringValue
6263
}
6364
if let textField = obj.object as? NSTextField, self.diskSize.identifier == textField.identifier {
64-
self.volume.size = textField.integerValue
65+
self.setVolumeSize()
6566
}
6667
if let textField = obj.object as? NSTextField, self.folders.identifier == textField.identifier {
6768
self.volume.folders = textField.stringValue.split(separator: ",").map { String($0) }
6869
}
6970
}
7071

71-
// MARK: - IBACtions
72+
// MARK: - IBActions
7273

7374
@IBAction func sizeStepped(_ sender: NSStepper) {
74-
self.volume.size = sender.integerValue
75-
self.diskSize.stringValue = "\(sender.integerValue)"
75+
if unitIndex == 0 {
76+
self.diskSize.stringValue = String(sender.integerValue)
77+
} else {
78+
let dSize = sender.integerValue / 1000
79+
self.diskSize.stringValue = String(format: "%.2f", dSize)
80+
}
81+
}
82+
83+
@IBAction func unitSelected(_ sender: NSPopUpButton) {
84+
switch sender.indexOfSelectedItem {
85+
case 0:
86+
if unitIndex == 1 {
87+
let dSize = self.diskSize.doubleValue * 1000
88+
self.diskSize.stringValue = String(Int(dSize))
89+
unitIndex = 0
90+
}
91+
break
92+
case 1:
93+
if unitIndex == 0 {
94+
let dSize = self.diskSize.doubleValue / 1000.0
95+
self.diskSize.stringValue = String(format: "%.2f", dSize)
96+
unitIndex = 1
97+
}
98+
break
99+
default:
100+
return
101+
}
76102
}
77103

78104
@IBAction func sizeSelected(_ sender: NSPopUpButton) {
79105
switch sender.indexOfSelectedItem {
80106
case 1, 2, 3, 4:
81107
let percent = [0.1, 0.25, 0.5, 0.75][sender.indexOfSelectedItem - 1]
82108
let dSize = (percent * Double(ProcessInfo.init().physicalMemory)) / 1024 / 1024
83-
self.volume.size = Int(dSize)
84-
self.diskSize.stringValue = String(Int(dSize))
109+
if unitIndex == 0 {
110+
self.diskSize.stringValue = String(Int(dSize))
111+
} else {
112+
let newDSize = dSize / 1000.0
113+
self.diskSize.stringValue = String(format: "%.2f", newDSize)
114+
}
85115
sender.selectItem(at: 0)
86116
break
87117
default:
@@ -164,6 +194,8 @@ class NewTmpDiskViewController: NSViewController, NSTextFieldDelegate {
164194
sender.addSubview(spinner)
165195
sender.isEnabled = false
166196

197+
self.setVolumeSize()
198+
167199
TmpDiskManager.shared.createTmpDisk(volume: self.volume) { error in
168200
DispatchQueue.main.async {
169201
spinner.removeFromSuperview()
@@ -180,7 +212,11 @@ class NewTmpDiskViewController: NSViewController, NSTextFieldDelegate {
180212
self.showError(message: NSLocalizedString("A volume with this name already exists", comment: ""))
181213
break;
182214
case .invalidSize:
183-
self.showError(message: NSLocalizedString("Size must be a number of megabytes > 0", comment: ""))
215+
if self.unitIndex == 0 {
216+
self.showError(message: NSLocalizedString("Size must be a number of megabytes > 0", comment: ""))
217+
} else {
218+
self.showError(message: NSLocalizedString("Size must be a number of gigabytes >= 0.01", comment: ""))
219+
}
184220
break;
185221
case .failed:
186222
self.showError(message: NSLocalizedString("Failed to create TmpDisk", comment: ""))
@@ -197,6 +233,14 @@ class NewTmpDiskViewController: NSViewController, NSTextFieldDelegate {
197233

198234
// MARK: - Internal functions
199235

236+
func setVolumeSize() {
237+
if unitIndex == 0 {
238+
self.volume.size = self.diskSize.integerValue
239+
} else {
240+
self.volume.size = Int(self.diskSize.doubleValue * 1000)
241+
}
242+
}
243+
200244
func showError(message: String) {
201245
let alert = NSAlert()
202246
alert.messageText = message

0 commit comments

Comments
 (0)