-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathCallControl.swift
246 lines (209 loc) · 9.47 KB
/
CallControl.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
//
// CallControl.swift
// Telegram
//
// Created by Mikhail Filimonov on 14/08/2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import TelegramMedia
struct CallControlData {
enum Mode: Equatable {
case normal(NSColor, CGImage)
case visualEffect(CGImage)
case animated(LocalAnimatedSticker, NSColor)
}
let text: String?
let mode: Mode
let iconSize: NSSize
func isProperView(_ view: NSView?) -> Bool {
if view == nil {
return false
}
switch mode {
case .visualEffect:
return view is NSVisualEffectView
case .animated:
return view is LottiePlayerView
case .normal:
return !(view is LottiePlayerView) && !(view is NSVisualEffectView)
}
}
}
final class CallControl : Control {
private let imageView: ImageView = ImageView()
private var imageBackgroundView:NSView? = nil
private var textView: TextView?
private var progressView: RadialProgressView?
private var isLoading: Bool = false
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.imageView.isEventLess = true
}
func updateLoading(_ isLoading: Bool, animated: Bool) {
self.isLoading = isLoading
if isLoading {
if progressView == nil {
progressView = RadialProgressView(theme: RadialProgressTheme(backgroundColor: .clear, foregroundColor: .grayIcon), twist: true)
addSubview(progressView!)
if animated {
progressView?.layer?.animateAlpha(from: 0, to: 1, duration: 0.4)
}
}
let rect = imageBackgroundView?.bounds ?? NSMakeRect(0, 0, 40, 40)
progressView?.frame = NSMakeRect(rect.minX + 1, rect.minY + 1, rect.width - 2, rect.height - 2)
progressView?.state = .ImpossibleFetching(progress: 0.2, force: !animated)
} else {
if let progressView = self.progressView {
self.progressView = nil
progressView.state = .ImpossibleFetching(progress: 1.0, force: false)
if animated {
progressView.layer?.animateAlpha(from: 1, to: 0, duration: 0.4, removeOnCompletion: false, completion: { [weak progressView] _ in
progressView?.removeFromSuperview()
})
} else {
progressView.removeFromSuperview()
}
}
}
}
override var isEnabled: Bool {
get {
return super.isEnabled && !isLoading
}
set {
super.isEnabled = newValue
}
}
override var mouseDownCanMoveWindow: Bool {
return false
}
private var previousState: ControlState?
override func stateDidUpdate( _ state: ControlState) {
switch controlState {
case .Highlight:
imageBackgroundView?._change(opacity: 0.9)
textView?.change(opacity: 0.9)
imageBackgroundView?.layer?.animateScaleCenter(from: 1, to: 0.95, duration: 0.2, removeOnCompletion: false)
default:
imageBackgroundView?._change(opacity: 1.0)
textView?.change(opacity: 1.0)
if let previousState = previousState, previousState == .Highlight {
imageBackgroundView?.layer?.animateScaleCenter(from: 0.95, to: 1.0, duration: 0.2)
}
}
previousState = state
}
func updateEnabled(_ enabled: Bool, animated: Bool) {
self.isEnabled = enabled
change(opacity: enabled ? 1 : 0.7, animated: animated)
}
var size: NSSize {
return imageBackgroundView?.frame.size ?? frame.size
}
func updateWithData(_ data: CallControlData, animated: Bool) {
if let text = data.text {
let current: TextView
if let textView = self.textView {
current = textView
} else {
current = TextView()
self.textView = current
current.isSelectable = false
current.userInteractionEnabled = false
current.isEventLess = true
addSubview(current)
current.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)
}
let layout = TextViewLayout(.initialize(string: text, color: .white, font: .normal(12)), maximumNumberOfLines: 1)
layout.measure(width: max(data.iconSize.width, 100))
current.update(layout)
} else {
if let textView = self.textView {
self.textView = nil
textView.layer?.animateAlpha(from: 1, to: 0, duration: 0.2, removeOnCompletion: false, completion: { [weak textView] _ in
textView?.removeFromSuperview()
})
}
}
switch data.mode {
case let .visualEffect(icon):
if !data.isProperView(self.imageBackgroundView) {
self.imageBackgroundView?.removeFromSuperview()
self.imageBackgroundView = NSVisualEffectView(frame: NSMakeRect(0, 0, data.iconSize.width, data.iconSize.height))
self.imageBackgroundView?.wantsLayer = true
self.addSubview(self.imageBackgroundView!)
}
let view = self.imageBackgroundView as! NSVisualEffectView
view.material = .light
view.state = .active
view.blendingMode = .withinWindow
imageView.isHidden = false
imageView.animates = animated
imageView.image = icon
imageView.setFrameSize(data.iconSize)
case let .normal(color, icon):
if !data.isProperView(self.imageBackgroundView) {
self.imageBackgroundView?.removeFromSuperview()
self.imageBackgroundView = NSView(frame: NSMakeRect(0, 0, data.iconSize.width, data.iconSize.height))
self.imageBackgroundView?.wantsLayer = true
self.addSubview(self.imageBackgroundView!)
}
imageView.isHidden = false
imageView.animates = animated
imageView.image = icon
imageView.setFrameSize(data.iconSize)
self.imageBackgroundView?.background = color
case let .animated(value, color):
if !data.isProperView(self.imageBackgroundView) {
self.imageBackgroundView?.removeFromSuperview()
self.imageBackgroundView = LottiePlayerView(frame: NSMakeRect(0, 0, data.iconSize.width, data.iconSize.height))
self.imageBackgroundView?.wantsLayer = true
self.addSubview(self.imageBackgroundView!)
}
imageView.isHidden = true
let player = self.imageBackgroundView as? LottiePlayerView
if let animationData = value.data {
let policy: LottiePlayPolicy
if animated {
policy = .toEnd(from: 1)
} else {
policy = .toEnd(from: .max)
}
player?.set(LottieAnimation(compressed: animationData, key: .init(key: .bundle(value.rawValue), size: data.iconSize), type: .lottie, cachePurpose: .none, playPolicy: policy, colors: [.init(keyPath: "", color: NSColor(0xffffff))], runOnQueue: .mainQueue()))
}
self.imageBackgroundView?.background = color
}
imageView.removeFromSuperview()
self.imageBackgroundView?.addSubview(imageView)
imageBackgroundView!._change(size: data.iconSize, animated: animated)
imageBackgroundView!.layer?.cornerRadius = data.iconSize.height / 2
if let textView = self.textView {
change(size: NSMakeSize(max(data.iconSize.width, textView.frame.width), data.iconSize.height + 5 + textView.frame.height), animated: animated)
imageView._change(pos: imageBackgroundView!.focus(imageView.frame.size).origin, animated: animated)
textView._change(pos: NSMakePoint(floorToScreenPixels(backingScaleFactor, (frame.width - textView.frame.width) / 2), imageBackgroundView!.frame.height + 5), animated: animated)
imageBackgroundView!._change(pos: NSMakePoint(floorToScreenPixels(backingScaleFactor, (frame.width - imageBackgroundView!.frame.width) / 2), 0), animated: animated)
} else {
change(size: data.iconSize, animated: animated)
imageView._change(pos: imageBackgroundView!.focus(imageView.frame.size).origin, animated: animated)
imageBackgroundView!._change(pos: focus(imageBackgroundView!.frame.size).origin, animated: animated)
}
needsLayout = true
}
override func layout() {
super.layout()
imageView.center()
if let imageBackgroundView = imageBackgroundView {
if let textView = textView {
imageBackgroundView.centerX(y: 0)
textView.setFrameOrigin(NSMakePoint(floorToScreenPixels(backingScaleFactor, (frame.width - textView.frame.width) / 2), imageBackgroundView.frame.height + 5))
} else {
imageBackgroundView.center()
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}