-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathControl.swift
310 lines (256 loc) · 10 KB
/
Control.swift
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright © 2019 Saleem Abdulrasool <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause
private protocol ControlEventCallable {
func callAsFunction(sender: Control, event: Control.Event)
}
private struct ControlEventCallback<Source: Control, Target: AnyObject>: ControlEventCallable {
private unowned(safe) let instance: Target
private let method: (Target) -> (_: Source, _: Control.Event) -> Void
public init(binding: @escaping (Target) -> (_: Source, _: Control.Event) -> Void,
on: Target) {
self.instance = on
self.method = binding
}
public init(binding: @escaping (Target) -> (_: Source) -> Void, on: Target) {
self.instance = on
self.method = { (target: Target) in { (sender: Source, _: Control.Event) in
binding(target)(sender)
}
}
}
public init(binding: @escaping (Target) -> () -> Void, on: Target) {
self.instance = on
self.method = { (target: Target) in { (_: Source, _: Control.Event) in
binding(target)()
}
}
}
public func callAsFunction(sender: Control, event: Control.Event) {
self.method(instance)(sender as! Source, event)
}
}
extension ControlEventCallback where Target: Action {
fileprivate init(invoking action: Target) {
self.instance = action
self.method = { (_: Target) in { (sender: Source, _: Control.Event) in
action.sender = sender
defer { action.sender = nil }
action.handler(action)
}
}
}
}
@inline(__always)
private var kTriggers: [Control.Event] {
Control.Event.touchEvents + Control.Event.semanticEvents + Control.Event.editingEvents
}
/// The base class for controls, which are visual elements that convey a
/// specific action or intention in response to user interactions.
public class Control: View {
private var actions: [Control.Event:[ControlEventCallable]] =
Dictionary<Control.Event, [ControlEventCallable]>(minimumCapacity: 16)
// MARK - Accessing the Control's Targets and Actions
@inline(__always)
private func addAction(_ callable: ControlEventCallable,
for controlEvents: Control.Event) {
kTriggers.filter { $0.rawValue & controlEvents.rawValue == $0.rawValue }
.forEach { self.actions[$0, default: []].append(callable) }
}
internal func addAction(_ action: Action, for controlEvents: Control.Event) {
kTriggers.filter { $0.rawValue & controlEvents.rawValue == $0.rawValue }
.forEach { self.actions[$0, default: []].append(ControlEventCallback(invoking: action)) }
}
/// Associates a target object and action method with the control.
public func addTarget<Target: AnyObject>(_ target: Target,
action: @escaping (Target) -> () -> Void,
for controlEvents: Control.Event) {
self.addAction(ControlEventCallback<Self, Target>(binding: action, on: target),
for: controlEvents)
}
/// Associates a target object and action method with the control.
public func addTarget<Source: Control, Target: AnyObject>(_ target: Target,
action: @escaping (Target) -> (_: Source) -> Void,
for controlEvents: Control.Event) {
self.addAction(ControlEventCallback(binding: action, on: target),
for: controlEvents)
}
/// Returns the events for which the control has associated actions.
public private(set) var allControlEvents: Control.Event =
Control.Event(rawValue: 0)
/// Associates a target object and action method with the control.
public func addTarget<Source: Control, Target: AnyObject>(_ target: Target,
action: @escaping (Target) -> (_: Source, _: Control.Event) -> Void,
for controlEvents: Control.Event) {
self.addAction(ControlEventCallback<Source, Target>(binding: action, on: target),
for: controlEvents)
}
// MARK - Triggering Actions
/// Calls the action methods associated with the specified events.
func sendActions(for controlEvents: Control.Event) {
kTriggers.filter { $0.rawValue & controlEvents.rawValue == $0.rawValue }
.forEach { self.actions[$0]?.forEach { $0(sender: self, event: controlEvents) } }
}
}
public extension Control {
/// The state of the control, specified as a bit mask value.
struct State: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
}
}
public extension Control.State {
/// The normal, or default state of a control—that is, enabled but neither
/// selected nor highlighted.
static var normal: Control.State {
Control.State(rawValue: 1 << 0)
}
/// Highlighted state of a control.
static var highlighted: Control.State {
Control.State(rawValue: 1 << 1)
}
/// Disabled state of a control.
static var disabled: Control.State {
Control.State(rawValue: 1 << 2)
}
/// Selected state of a control.
static var selected: Control.State {
Control.State(rawValue: 1 << 3)
}
/// Focused state of a control.
static var focused: Control.State {
Control.State(rawValue: 1 << 4)
}
/// Additional control-state flags available for application use.
static var application: Control.State {
Control.State(rawValue: 1 << 5)
}
/// Control-state flags reserved for internal framework use.
static var reserved: Control.State {
Control.State(rawValue: 1 << 6)
}
}
extension Control {
/// Constants describing the types of events possible for controls.
public struct Event: Equatable, Hashable, RawRepresentable {
public typealias RawValue = Int
public let rawValue: RawValue
public init(rawValue: RawValue) {
self.rawValue = rawValue
}
}
}
extension Control.Event {
/// A touch-down event in the control.
public static var touchDown: Control.Event {
Control.Event(rawValue: 1 << 0)
}
/// A repeated touch-down event in the control; for this event the value of
/// the UITouch tapCount method is greater than one.
public static var touchDownRepeat: Control.Event {
Control.Event(rawValue: 1 << 1)
}
/// An event where a finger is dragged inside the bounds of the control.
public static var touchDragInside: Control.Event {
Control.Event(rawValue: 1 << 2)
}
/// An event where a finger is dragged just outside the bounds of the control.
public static var touchDragOutside: Control.Event {
Control.Event(rawValue: 1 << 3)
}
/// An event where a finger is dragged into the bounds of the control.
public static var touchDragEnter: Control.Event {
Control.Event(rawValue: 1 << 4)
}
/// An event where a finger is dragged from within a control to outside its
/// bounds.
public static var touchDragExit: Control.Event {
Control.Event(rawValue: 1 << 5)
}
/// A touch-up event in the control where the finger is inside the bounds of
/// the control.
public static var touchUpInside: Control.Event {
Control.Event(rawValue: 1 << 6)
}
/// A touch-up event in the control where the finger is outside the bounds of
/// the control.
public static var touchUpOutside: Control.Event {
Control.Event(rawValue: 1 << 7)
}
/// A system event canceling the current touches for the control.
public static var touchCancel: Control.Event {
Control.Event(rawValue: 1 << 8)
}
/// A touch dragging or otherwise manipulating a control, causing it to emit
/// a series of different values.
public static var valueChanged: Control.Event {
Control.Event(rawValue: 1 << 12)
}
/// A menu action has triggered prior to the menu being presented.
public static var menuActionTriggered: Control.Event {
Control.Event(rawValue: 0)
}
/// A semantic action triggered by buttons.
public static var primaryActionTriggered: Control.Event {
Control.Event(rawValue: 1 << 13)
}
/// A touch initiating an editing session in a `TextField` object by entering
/// its bounds.
public static var editingDidBegin: Control.Event {
Control.Event(rawValue: 1 << 16)
}
/// A touch making an editing change in a `TextField` object.
public static var editingChanged: Control.Event {
Control.Event(rawValue: 1 << 17)
}
/// A touch ending an editing session in a `TextField` object by leaving its
/// bounds.
public static var editingDidEnd: Control.Event {
Control.Event(rawValue: 1 << 18)
}
/// A touch ending an editing session in a `TextField` object.
public static var editingDidEndOnExit: Control.Event {
Control.Event(rawValue: 1 << 19)
}
/// All touch events.
public static var allTouchEvents: Control.Event {
Control.Event(rawValue: RawValue(bitPattern: 0x00000fff))
}
/// All editing touches for `TextField` objects.
public static var allEditingEvents: Control.Event {
Control.Event(rawValue: RawValue(bitPattern: 0x000f0000))
}
/// A range of control-event values available for application use.
public static var applicationReserved: Control.Event {
Control.Event(rawValue: RawValue(bitPattern: 0x0f000000))
}
/// A range of control-event values reserved for internal framework use.
public static var systemReserved: Control.Event {
Control.Event(rawValue: RawValue(bitPattern: 0xf0000000))
}
/// All events, including system events.
public static var allEvents: Control.Event {
Control.Event(rawValue: RawValue(bitPattern: 0xffffffff))
}
}
extension Control.Event {
fileprivate static var touchEvents: [Control.Event] {
return [
.touchDown, .touchDownRepeat,
.touchDragInside, .touchDragOutside, .touchDragEnter, .touchDragExit,
.touchUpInside, .touchUpOutside,
.touchCancel
]
}
fileprivate static var semanticEvents: [Control.Event] {
return [
.valueChanged, /* .menuActionTriggered, */ .primaryActionTriggered
]
}
fileprivate static var editingEvents: [Control.Event] {
return [
.editingDidBegin, .editingChanged, .editingDidEnd, .editingDidEndOnExit
]
}
}