@@ -10,13 +10,11 @@ import UIKit
1010
1111public class KeyBindingEditorViewController : UIViewController
1212{
13- @IBOutlet var _instructionsLabel : UILabel !
14-
1513 var binding : KeyBinding
1614
17- var updatedBinding : KeyBinding ?
15+ var result : EditorResult ? = nil
1816
19- var completion : ( ( KeyBinding ? ) -> Void ) ? = nil
17+ var completion : ( ( EditorResult ? ) -> Void ) ? = nil
2018
2119 var editorView : KeyBindingEditorView ?
2220 {
@@ -26,7 +24,6 @@ public class KeyBindingEditorViewController: UIViewController
2624 init ( binding: KeyBinding )
2725 {
2826 self . binding = binding
29- self . updatedBinding = binding
3027 super. init ( nibName: " KeyBindingEditorView " , bundle: Bundle ( for: KeyBindingEditorViewController . self) )
3128
3229 self . preferredContentSize = CGSize ( width: 275.0 , height: 200.0 )
@@ -42,7 +39,7 @@ public class KeyBindingEditorViewController: UIViewController
4239 super. viewDidLoad ( )
4340
4441 editorView? . viewController = self
45- editorView ? . keyBindingDisplayLabel . keyBinding = binding
42+ updateLabels ( )
4643
4744 navigationItem. title = " "
4845
@@ -68,10 +65,9 @@ public class KeyBindingEditorViewController: UIViewController
6865 input: keyCommand. input!, modifiers: keyCommand. modifierFlags,
6966 isDiscoverable: false )
7067
71- self . editorView? . keyBindingDisplayLabel. keyBinding = binding
72- // self.editorView?.keyBindingDisplayLabel.font = UIFont.systemFont(ofSize: 36.0)
68+ self . result = . customize( self . binding. customized ( input: binding. input, modifiers: binding. modifiers) )
7369
74- self . updatedBinding = self . binding . customized ( input : binding . input , modifiers : binding . modifiers )
70+ self . updateLabels ( )
7571 }
7672
7773 view. addSubview ( keyBindingControl)
@@ -85,32 +81,90 @@ public class KeyBindingEditorViewController: UIViewController
8581
8682 DispatchQueue . main. async
8783 {
88- self . completion ? ( self . updatedBinding )
84+ self . completion ? ( self . result )
8985 }
9086 }
9187
9288 public func setInstructions( _ text: String )
9389 {
94- _instructionsLabel . text = text
90+ instructionsLabel . text = text
9591 }
9692
9793 public var instructionsLabel : UILabel
9894 {
99- return _instructionsLabel
95+ return editorView!. instructionsLabel
96+ }
97+
98+ public var unassignedLabel : UILabel
99+ {
100+ return editorView!. unassignedLabel
101+ }
102+
103+ @objc func save( )
104+ {
105+ dismiss ( animated: true )
100106 }
101107
102108 @objc func revert( )
103109 {
104- self . updatedBinding = nil
110+ self . result = . revert
105111
106- dismiss ( animated: true , completion : nil )
112+ dismiss ( animated: true )
107113 }
108114
109115 @objc func cancel( )
110116 {
111- self . updatedBinding = self . binding
117+ self . result = nil
112118
113- dismiss ( animated: true , completion: nil )
119+ dismiss ( animated: true )
120+ }
121+
122+ @objc func unassign( )
123+ {
124+ self . result = . unassign
125+
126+ dismiss ( animated: true )
127+ }
128+
129+ enum EditorResult
130+ {
131+ case customize( KeyBinding )
132+ case revert
133+ case unassign
134+ }
135+
136+ // Private
137+
138+ private func updateLabels( )
139+ {
140+ guard let editorView = self . editorView else
141+ {
142+ // If there's no editor view, there are no labels to update!
143+ return
144+ }
145+
146+ let binding : KeyBinding
147+
148+ // If there is a customized binding set, we read from there
149+ if case . some( . customize( let newBinding) ) = result
150+ {
151+ binding = newBinding
152+ }
153+ else
154+ {
155+ binding = self . binding
156+ }
157+
158+ let isUnassignedBinding = binding. isUnassigned
159+ editorView. unassignedLabel. isHidden = !isUnassignedBinding
160+ editorView. keyBindingDisplayLabel. isHidden = isUnassignedBinding
161+ editorView. unassignButton. isEnabled = !isUnassignedBinding
162+ editorView. saveButton. isEnabled = result != nil
163+
164+ if !isUnassignedBinding
165+ {
166+ editorView. keyBindingDisplayLabel. keyBinding = binding
167+ }
114168 }
115169}
116170
@@ -203,9 +257,18 @@ class KeyBindingEditorView: UIView
203257 var viewController : KeyBindingEditorViewController ? = nil
204258
205259 @IBOutlet var keyBindingDisplayLabel : KeyBindingLabel !
260+ @IBOutlet var instructionsLabel : UILabel !
261+ @IBOutlet var unassignedLabel : UILabel !
262+ @IBOutlet var saveButton : UIButton !
263+ @IBOutlet var unassignButton : UIButton !
206264
207265 @IBAction func save( sender: Any ? )
208266 {
209- viewController? . dismiss ( animated: true , completion: nil )
267+ viewController? . save ( )
268+ }
269+
270+ @IBAction func unassign( _ sender: Any ? )
271+ {
272+ viewController? . unassign ( )
210273 }
211274}
0 commit comments