-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathContainableController.swift
329 lines (290 loc) · 11.5 KB
/
ContainableController.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// ContainableController.swift
// TGUIKit
//
// Created by keepcoder on 23/10/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
public enum ContainedViewLayoutTransitionCurve {
case linear
case easeInOut
case easeOut
case spring
case legacy
}
public let listViewAnimationCurveSystem: (CGFloat) -> CGFloat = { t in
return bezierPoint(0.23, 1.0, 0.32, 1.0, t)
}
public let listViewAnimationCurveEaseInOut: (CGFloat) -> CGFloat = { t in
return bezierPoint(0.42, 0.0, 0.58, 1.0, t)
}
public let listViewAnimationCurveLinear: (CGFloat) -> CGFloat = { t in
return t
}
public extension ContainedViewLayoutTransitionCurve {
var timingFunction: CAMediaTimingFunctionName {
switch self {
case .easeInOut:
return CAMediaTimingFunctionName.easeInEaseOut
case .spring:
return CAMediaTimingFunctionName.spring
case .legacy:
return CAMediaTimingFunctionName.easeInEaseOut
case .linear:
return CAMediaTimingFunctionName.linear
case .easeOut:
return CAMediaTimingFunctionName.easeOut
}
}
func solve(at offset: CGFloat) -> CGFloat {
switch self {
case .easeInOut:
return listViewAnimationCurveEaseInOut(offset)
case .spring:
return listViewAnimationCurveSystem(offset)
case .legacy:
return listViewAnimationCurveEaseInOut(offset)
case .easeOut:
return listViewAnimationCurveEaseInOut(offset)
case .linear:
return listViewAnimationCurveLinear(offset)
}
}
}
public enum ContainedViewLayoutTransition {
case immediate
case animated(duration: Double, curve: ContainedViewLayoutTransitionCurve)
public var isAnimated: Bool {
switch self {
case .immediate:
return false
case .animated:
return true
}
}
public var duration: Double {
switch self {
case .immediate:
return 0
case let .animated(duration, _):
return duration
}
}
public var timingFunction:CAMediaTimingFunctionName {
switch self {
case .immediate:
return .linear
case let .animated(_, curve):
return curve.timingFunction
}
}
}
public extension ContainedViewLayoutTransition {
func updateFrame(view: NSView, frame: CGRect, completion: ((Bool) -> Void)? = nil) {
switch self {
case .immediate:
view.frame = frame
if let completion = completion {
completion(true)
}
case let .animated(duration, curve):
var curve = curve
if view is NSVisualEffectView {
curve = .legacy
}
switch curve {
case .legacy:
NSAnimationContext.runAnimationGroup({ ctx in
ctx.duration = duration
ctx.timingFunction = .init(name: curve.timingFunction)
view.animator().frame = frame
}, completionHandler: {
completion?(true)
})
default:
var ignoreSize: Bool = false
if let view = view as? TableView {
view.change(size: frame.size, animated: true, duration: duration, timingFunction: curve.timingFunction, completion: { completed in
completion?(completed)
})
}
var notifyOnOrigin: Bool = false
var notifyOnSize: Bool = false
if frame.size != view.frame.size {
notifyOnSize = true
} else if frame.origin != view.frame.origin {
notifyOnOrigin = true
} else {
completion?(true)
}
view._change(size: frame.size, animated: true, duration: duration, timingFunction: curve.timingFunction, completion: { completed in
if notifyOnSize {
completion?(completed)
}
})
view._change(pos: frame.origin, animated: true, duration: duration, timingFunction: curve.timingFunction, completion: { completed in
if notifyOnOrigin {
completion?(completed)
}
})
}
}
}
func updateFrame(layer: CALayer, frame: CGRect, completion: ((Bool) -> Void)? = nil, save: Bool = true, updatePosition: Bool = false) {
switch self {
case .immediate:
layer.frame = frame
if let completion = completion {
completion(true)
}
case let .animated(duration, _):
func animateSize(_ layer: CALayer) -> Void {
var presentBounds:NSRect = layer.bounds
let presentation = layer.presentation()
if let presentation = presentation, layer.animation(forKey:"bounds") != nil {
presentBounds.size.width = NSWidth(presentation.bounds)
presentBounds.size.height = NSHeight(presentation.bounds)
}
layer.animateBounds(from: presentBounds, to: frame.size.bounds, duration: duration, timingFunction: timingFunction, completion: completion)
}
func animatePos(_ layer: CALayer) -> Void {
var presentRect:NSPoint = layer.position
let presentation = layer.presentation()
if let presentation = presentation, layer.animation(forKey:"position") != nil {
presentRect.x = presentation.position.x
presentRect.y = presentation.position.y
}
layer.animatePosition(from: presentRect, to: frame.origin, duration: duration, timingFunction: timingFunction, completion: completion)
}
if layer.frame.origin != frame.origin {
animatePos(layer)
}
if layer.frame.size != frame.size {
animateSize(layer)
}
if save {
if updatePosition {
layer.frame = frame.size.bounds
layer.position = frame.origin
} else {
layer.frame = frame
}
}
}
}
func updateTransformScale(layer: CALayer, scale: CGFloat, beginWithCurrentState: Bool = false, completion: ((Bool) -> Void)? = nil) {
let t = layer.transform
let currentScale = sqrt((t.m11 * t.m11) + (t.m12 * t.m12) + (t.m13 * t.m13))
if currentScale.isEqual(to: scale) {
if let completion = completion {
completion(true)
}
return
}
switch self {
case .immediate:
layer.transform = CATransform3DMakeScale(scale, scale, 1.0)
if let completion = completion {
completion(true)
}
case let .animated(duration, curve):
let previousScale: CGFloat
if beginWithCurrentState, let presentation = layer.presentation() {
let t = presentation.transform
previousScale = sqrt((t.m11 * t.m11) + (t.m12 * t.m12) + (t.m13 * t.m13))
} else {
previousScale = currentScale
}
layer.animateScaleSpring(from: previousScale, to: scale, duration: duration, bounce: false, completion: { result in
if let completion = completion {
completion(result)
}
})
}
}
func updateAlpha(view: NSView, alpha: CGFloat, completion: ((Bool) -> Void)? = nil) {
switch self {
case .immediate:
view.layer?.opacity = Float(alpha)
if let completion = completion {
completion(true)
}
case let .animated(duration, curve):
let previousAlpha = view.layer?.presentation()?.opacity ?? view.layer?.opacity ?? 1
view.layer?.opacity = Float(alpha)
view.layer?.animateAlpha(from: CGFloat(previousAlpha), to: alpha, duration: duration, timingFunction: curve.timingFunction, completion: { result in
if let completion = completion {
completion(result)
}
})
}
}
func updateAlpha(layer: CALayer, alpha: CGFloat, completion: ((Bool) -> Void)? = nil) {
switch self {
case .immediate:
layer.opacity = Float(alpha)
if let completion = completion {
completion(true)
}
case let .animated(duration, curve):
let previousAlpha = layer.presentation()?.opacity ?? layer.opacity
layer.opacity = Float(alpha)
layer.animateAlpha(from: CGFloat(previousAlpha), to: alpha, duration: duration, timingFunction: curve.timingFunction, completion: { result in
if let completion = completion {
completion(result)
}
})
}
}
// func updatePosition(layer: CALayer, position: NSPoint, completion: ((Bool) -> Void)? = nil) {
// switch self {
// case .immediate:
// layer.position = position
// if let completion = completion {
// completion(true)
// }
// case let .animated(duration, curve):
// let previousAlpha = layer.presentation()?.opacity ?? layer.opacity
// layer.position = position
// layer.animateAlpha(from: CGFloat(previousAlpha), to: alpha, duration: duration, timingFunction: curve.timingFunction, completion: { result in
// if let completion = completion {
// completion(result)
// }
// })
// }
// }
func animatePositionWithKeyframes(layer: CALayer, keyframes: [CGPoint], removeOnCompletion: Bool = true, additive: Bool = false, completion: ((Bool) -> Void)? = nil) {
switch self {
case .immediate:
completion?(true)
case let .animated(duration, curve):
layer.animateKeyframes(values: keyframes.map(NSValue.init(point:)), duration: duration, keyPath: "position", timingFunction: curve.timingFunction, removeOnCompletion: removeOnCompletion, completion: completion)
}
}
func setShapeLayerStrokeEnd(layer: CAShapeLayer, strokeEnd: CGFloat, completion: ((Bool) -> Void)? = nil) {
switch self {
case .immediate:
layer.strokeEnd = strokeEnd
completion?(true)
case let .animated(duration, curve):
let previousStrokeEnd = layer.strokeEnd
layer.strokeEnd = strokeEnd
layer.animate(
from: previousStrokeEnd as NSNumber,
to: strokeEnd as NSNumber,
keyPath: "strokeEnd",
timingFunction: curve.timingFunction,
duration: duration,
delay: 0.0,
removeOnCompletion: true,
additive: false,
completion: completion
)
}
}
}
public protocol ContainableController: class {
var view: View! { get }
func containerLayoutUpdated(transition: ContainedViewLayoutTransition)
}