forked from brunophilipe/KeyCommandKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyBinding.swift
More file actions
233 lines (192 loc) · 6.63 KB
/
KeyBinding.swift
File metadata and controls
233 lines (192 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// KeyBinding.swift
// KeyCommandKit - Provides customizable key commands to iOS Apps
//
// Created by Bruno Philipe on 30/6/17.
// Copyright (C) 2017 Bruno Philipe <git@bruno.ph>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
import Foundation
let UnassignedKeyBindingInput = "Unassigned"
public class KeyBinding
{
/// An internal identifier. Must be unique, and is used to query key bindings. It is never shown to the user.
public let key: String
/// A user-friendly description. It is shown in the system discoverability UI if the `isDiscoverable` property is true.
public let name: String
/// Whether this binding should be shown in the system discoverability UI.
public let isDiscoverable: Bool
/// The default input for this binding. Can be customized by the user, but the default will always be stored.
public let input: String
/// The default modifiers for this binding. Can be customized by the user, but the default will always be stored.
public let modifiers: UIKeyModifierFlags
/// Flag that indicates wether this binding was customized by the user.
public var isCustomized: Bool
{
return false
}
/// Whether this is a customization where the user unassigned the key binding, effectively deactivating it.
var isUnassigned: Bool
{
return input == UnassignedKeyBindingInput
}
/// Makes a UIKeyCommand instance by attaching an action to the receiver binding. If the receiver binding was
/// customized to "Unassigned" by the user, this method returns `nil`.
public func make(withAction action: Selector) -> UIKeyCommand?
{
return make(withAction: action, hidden: false)
}
/// Makes a UIKeyCommand instance by attaching an action to the receiver binding. If the receiver binding was
/// customized to "Unassigned" by the user, this method returns `nil`. If `hidden` is `true`, makes the key command
/// as undiscoverable, even if it was originally registered as discoverable. This is used in the key bindings
/// editor.
internal func make(withAction action: Selector, hidden: Bool) -> UIKeyCommand?
{
if isUnassigned
{
return nil
}
if isDiscoverable, #available(iOS 9.0, *), !hidden
{
return UIKeyCommand(input: input, modifierFlags: modifiers, action: action, discoverabilityTitle: name)
}
else
{
return UIKeyCommand(input: input, modifierFlags: modifiers, action: action)
}
}
public init(key: String, name: String, input: String, modifiers: UIKeyModifierFlags, isDiscoverable: Bool = true)
{
self.key = key
self.name = name
self.input = input
self.modifiers = modifiers
self.isDiscoverable = isDiscoverable
}
}
internal class CustomizedKeyBinding: KeyBinding
{
/// Flag that indicates wether this binding was customized by the user.
override public var isCustomized: Bool
{
return true
}
var originalInput: String
var originalModifiers: UIKeyModifierFlags
init(key: String, name: String, input: String, modifiers: UIKeyModifierFlags, isDiscoverable: Bool, originalInput: String, originalModifiers: UIKeyModifierFlags)
{
self.originalInput = originalInput
self.originalModifiers = originalModifiers
super.init(key: key, name: name, input: input, modifiers: modifiers, isDiscoverable: isDiscoverable)
}
var originalBinding: KeyBinding
{
return KeyBinding(key: key, name: name, input: originalInput, modifiers: originalModifiers, isDiscoverable: isDiscoverable)
}
}
internal extension KeyBinding
{
func isEquivalent(toKeyBinding keyBinding: KeyBinding) -> Bool
{
return keyBinding.input == self.input && keyBinding.modifiers == self.modifiers
}
func isEquivalent(toKeyCommand keyCommand: UIKeyCommand) -> Bool
{
return keyCommand.input == self.input && keyCommand.modifierFlags == self.modifiers
}
var stringRepresentation: String
{
var string = ""
if modifiers.contains(.control)
{
string += "⌃"
}
if modifiers.contains(.alternate)
{
string += "⌥"
}
if modifiers.contains(.shift)
{
string += "⇧"
}
if modifiers.contains(.command)
{
string += "⌘"
}
switch input
{
case UIKeyCommand.inputLeftArrow:
string += "←"
case UIKeyCommand.inputRightArrow:
string += "→"
case UIKeyCommand.inputUpArrow:
string += "↑"
case UIKeyCommand.inputDownArrow:
string += "↓"
case UIKeyCommand.inputEscape:
string += "⎋"
case UIKeyCommand.inputBackspace:
string += "⌫"
case UIKeyCommand.inputTab:
string += "⇥"
case UIKeyCommand.inputReturn:
string += "↩︎"
default:
string += input.uppercased()
}
return string
}
func customized(input: String, modifiers: UIKeyModifierFlags) -> KeyBinding
{
return CustomizedKeyBinding(key: key, name: name, input: input, modifiers: modifiers,
isDiscoverable: isDiscoverable, originalInput: self.input,
originalModifiers: self.modifiers)
}
}
public extension Dictionary where Key == String, Value == KeyBinding
{
/// Makes UIKeyCommand objects by attaching key bindings to actions by matching their respective `key`s.
/// If a key binding was customized to "Unassigned" by the user, then this routine skips it.
func make(withActionsForKeys tuples: [(key: String, action: Selector)]) -> [UIKeyCommand]
{
var keyCommands = [UIKeyCommand]()
for (key, action) in tuples
{
if let binding = self[key], let keyCommand = binding.make(withAction: action)
{
keyCommands.append(keyCommand)
}
}
return keyCommands
}
}
extension KeyBinding: Hashable
{
public static func == (lhs: KeyBinding, rhs: KeyBinding) -> Bool
{
return lhs.hashValue == rhs.hashValue
}
public func hash(into hasher: inout Hasher)
{
hasher.combine(key)
hasher.combine(input)
hasher.combine(modifiers.rawValue)
}
}