-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCallNavigationHeaderView.swift
866 lines (680 loc) · 26.2 KB
/
CallNavigationHeaderView.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//
// CallNavigationHeaderView.swift
// Telegram
//
// Created by keepcoder on 05/05/2017.
// Copyright © 2017 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import SwiftSignalKit
import TelegramCore
import Postbox
private let blue = NSColor(rgb: 0x0078ff)
private let lightBlue = NSColor(rgb: 0x59c7f8)
private let green = NSColor(rgb: 0x33c659)
private let purple = NSColor(rgb: 0x766EE9)
private let lightPurple = NSColor(rgb: 0xF05459)
class CallStatusBarBackgroundViewLegacy : View, CallStatusBarBackground {
var audioLevel: Float = 0
var speaking:(Bool, Bool, Bool)? = nil {
didSet {
if let speaking = self.speaking, (speaking.0 != oldValue?.0 || speaking.1 != oldValue?.1 || speaking.2 != oldValue?.2) {
let targetColors: [NSColor]
if speaking.1 {
if speaking.2 {
if speaking.0 {
targetColors = [green, blue]
} else {
targetColors = [blue, lightBlue]
}
} else {
targetColors = [purple, lightPurple]
}
} else {
targetColors = [theme.colors.grayIcon, theme.colors.grayIcon.lighter()]
}
self.backgroundColor = targetColors.first ?? theme.colors.accent
}
}
}
}
class CallStatusBarBackgroundView: View, CallStatusBarBackground {
private let foregroundView: View
private let foregroundGradientLayer: CAGradientLayer
private let maskCurveLayer: VoiceCurveLayer
var audioLevel: Float = 0.0 {
didSet {
self.maskCurveLayer.updateLevel(CGFloat(audioLevel))
}
}
var speaking:(Bool, Bool, Bool)? = nil {
didSet {
if let speaking = self.speaking, (speaking.0 != oldValue?.0 || speaking.1 != oldValue?.1 || speaking.2 != oldValue?.2) {
let initialColors = self.foregroundGradientLayer.colors
let targetColors: [CGColor]
if speaking.1 {
if speaking.2 {
if speaking.0 {
targetColors = [green.cgColor, blue.cgColor]
} else {
targetColors = [blue.cgColor, lightBlue.cgColor]
}
} else {
targetColors = [purple.cgColor, lightPurple.cgColor]
}
} else {
targetColors = [theme.colors.grayIcon.cgColor, theme.colors.grayIcon.lighter().cgColor]
}
self.foregroundGradientLayer.colors = targetColors
self.foregroundGradientLayer.animate(from: initialColors as AnyObject, to: targetColors as AnyObject, keyPath: "colors", timingFunction: .linear, duration: 0.3)
}
}
}
override init() {
self.foregroundView = View()
self.foregroundGradientLayer = CAGradientLayer()
self.maskCurveLayer = VoiceCurveLayer(frame: CGRect(), maxLevel: 2.5, smallCurveRange: (0.0, 0.0), mediumCurveRange: (0.1, 0.55), bigCurveRange: (0.1, 1.0))
self.maskCurveLayer.setColor(NSColor(rgb: 0xffffff))
super.init()
self.addSubview(self.foregroundView)
self.foregroundView.layer?.addSublayer(self.foregroundGradientLayer)
self.foregroundGradientLayer.colors = [theme.colors.grayIcon.cgColor, theme.colors.grayIcon.lighter().cgColor]
self.foregroundGradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
self.foregroundGradientLayer.endPoint = CGPoint(x: 2.0, y: 0.5)
self.foregroundView.layer?.mask = maskCurveLayer
//layer?.addSublayer(maskCurveLayer)
self.updateAnimations()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
override func layout() {
super.layout()
CATransaction.begin()
CATransaction.setDisableActions(true)
self.foregroundView.frame = NSMakeRect(0, 0, frame.width, frame.height)
self.foregroundGradientLayer.frame = foregroundView.bounds
self.maskCurveLayer.frame = NSMakeRect(0, 0, frame.width, frame.height)
CATransaction.commit()
}
private let occlusionDisposable = MetaDisposable()
private var isCurrentlyInHierarchy: Bool = false {
didSet {
updateAnimations()
}
}
deinit {
occlusionDisposable.dispose()
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
if let window = window as? Window {
occlusionDisposable.set(window.takeOcclusionState.start(next: { [weak self] value in
self?.isCurrentlyInHierarchy = value.contains(.visible)
}))
} else {
occlusionDisposable.set(nil)
isCurrentlyInHierarchy = false
}
}
func updateAnimations() {
if !isCurrentlyInHierarchy {
self.foregroundGradientLayer.removeAllAnimations()
self.maskCurveLayer.stopAnimating()
return
}
self.maskCurveLayer.startAnimating()
}
}
private final class VoiceCurveLayer: SimpleLayer {
private let smallCurve: CurveLayer
private let mediumCurve: CurveLayer
private let bigCurve: CurveLayer
private let maxLevel: CGFloat
private var displayLinkAnimator: ConstantDisplayLinkAnimator?
private var audioLevel: CGFloat = 0.0
var presentationAudioLevel: CGFloat = 0.0
private(set) var isAnimating = false
public typealias CurveRange = (min: CGFloat, max: CGFloat)
public init(
frame: CGRect,
maxLevel: CGFloat,
smallCurveRange: CurveRange,
mediumCurveRange: CurveRange,
bigCurveRange: CurveRange
) {
self.maxLevel = maxLevel
self.smallCurve = CurveLayer(
pointsCount: 7,
minRandomness: 1,
maxRandomness: 1.3,
minSpeed: 0.9,
maxSpeed: 3.2,
minOffset: smallCurveRange.min,
maxOffset: smallCurveRange.max
)
self.mediumCurve = CurveLayer(
pointsCount: 7,
minRandomness: 1.2,
maxRandomness: 1.5,
minSpeed: 1.0,
maxSpeed: 4.4,
minOffset: mediumCurveRange.min,
maxOffset: mediumCurveRange.max
)
self.bigCurve = CurveLayer(
pointsCount: 7,
minRandomness: 1.2,
maxRandomness: 1.7,
minSpeed: 1.0,
maxSpeed: 5.8,
minOffset: bigCurveRange.min,
maxOffset: bigCurveRange.max
)
super.init()
self.addSublayer(bigCurve)
self.addSublayer(mediumCurve)
self.addSublayer(smallCurve)
displayLinkAnimator = ConstantDisplayLinkAnimator() { [weak self] in
guard let strongSelf = self else { return }
strongSelf.presentationAudioLevel = strongSelf.presentationAudioLevel * 0.9 + strongSelf.audioLevel * 0.1
strongSelf.smallCurve.level = strongSelf.presentationAudioLevel
strongSelf.mediumCurve.level = strongSelf.presentationAudioLevel
strongSelf.bigCurve.level = strongSelf.presentationAudioLevel
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func setColor(_ color: NSColor) {
smallCurve.setColor(color.withAlphaComponent(1.0))
mediumCurve.setColor(color.withAlphaComponent(0.55))
bigCurve.setColor(color.withAlphaComponent(0.35))
}
public func updateLevel(_ level: CGFloat) {
let normalizedLevel = min(1, max(level / maxLevel, 0))
smallCurve.updateSpeedLevel(to: normalizedLevel)
mediumCurve.updateSpeedLevel(to: normalizedLevel)
bigCurve.updateSpeedLevel(to: normalizedLevel)
audioLevel = normalizedLevel
}
public func startAnimating() {
guard !isAnimating else { return }
isAnimating = true
updateCurvesState()
displayLinkAnimator?.isPaused = false
}
public func stopAnimating() {
self.stopAnimating(duration: 0.15)
}
public func stopAnimating(duration: Double) {
guard isAnimating else { return }
isAnimating = false
updateCurvesState()
displayLinkAnimator?.isPaused = true
}
private func updateCurvesState() {
if isAnimating {
if smallCurve.frame.size != .zero {
smallCurve.startAnimating()
mediumCurve.startAnimating()
bigCurve.startAnimating()
}
} else {
smallCurve.stopAnimating()
mediumCurve.stopAnimating()
bigCurve.stopAnimating()
}
}
override var frame: NSRect {
didSet {
if oldValue != frame {
smallCurve.frame = bounds
mediumCurve.frame = bounds
bigCurve.frame = bounds
updateCurvesState()
}
}
}
}
final class CurveLayer: SimpleShapeLayer {
let pointsCount: Int
let smoothness: CGFloat
let minRandomness: CGFloat
let maxRandomness: CGFloat
let minSpeed: CGFloat
let maxSpeed: CGFloat
let minOffset: CGFloat
let maxOffset: CGFloat
var level: CGFloat = 0 {
didSet {
guard self.minOffset > 0.0 else {
return
}
let lv = minOffset + (maxOffset - minOffset) * level
self.transform = CATransform3DMakeTranslation(0.0, lv * 16.0, 0.0)
}
}
private var curveAnimation: DisplayLinkAnimator?
private var speedLevel: CGFloat = 0
private var lastSpeedLevel: CGFloat = 0
private var transition: CGFloat = 0 {
didSet {
guard let currentPoints = currentPoints else { return }
let width = self.bounds.width
let smoothness = self.smoothness
let signal: Signal<CGPath, NoError> = Signal { subscriber in
subscriber.putNext(.smoothCurve(through: currentPoints, length: width, smoothness: smoothness, curve: true))
subscriber.putCompletion()
return EmptyDisposable
}
|> runOn(.concurrentBackgroundQueue())
|> deliverOnMainQueue
_ = signal.start(next: { [weak self] path in
self?.path = path
})
}
}
override var frame: CGRect {
didSet {
if oldValue != frame {
self.position = CGPoint(x: self.bounds.width / 2.0, y: self.bounds.height / 2.0)
self.bounds = self.bounds
}
if self.frame.size != oldValue.size {
self.fromPoints = nil
self.toPoints = nil
self.curveAnimation = nil
self.animateToNewShape()
}
}
}
private var fromPoints: [CGPoint]?
private var toPoints: [CGPoint]?
private var currentPoints: [CGPoint]? {
guard let fromPoints = fromPoints, let toPoints = toPoints else { return nil }
return fromPoints.enumerated().map { offset, fromPoint in
let toPoint = toPoints[offset]
return CGPoint(
x: fromPoint.x + (toPoint.x - fromPoint.x) * transition,
y: fromPoint.y + (toPoint.y - fromPoint.y) * transition
)
}
}
init(
pointsCount: Int,
minRandomness: CGFloat,
maxRandomness: CGFloat,
minSpeed: CGFloat,
maxSpeed: CGFloat,
minOffset: CGFloat,
maxOffset: CGFloat
) {
self.pointsCount = pointsCount
self.minRandomness = minRandomness
self.maxRandomness = maxRandomness
self.minSpeed = minSpeed
self.maxSpeed = maxSpeed
self.minOffset = minOffset
self.maxOffset = maxOffset
self.smoothness = 0.35
super.init()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required override init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
func setColor(_ color: NSColor) {
self.fillColor = color.cgColor
}
func updateSpeedLevel(to newSpeedLevel: CGFloat) {
speedLevel = max(speedLevel, newSpeedLevel)
}
func startAnimating() {
animateToNewShape()
}
func stopAnimating() {
fromPoints = currentPoints
toPoints = nil
self.curveAnimation?.invalidate()
curveAnimation = nil
}
private func animateToNewShape() {
if curveAnimation != nil {
fromPoints = currentPoints
toPoints = nil
curveAnimation = nil
}
if fromPoints == nil {
fromPoints = generateNextCurve(for: bounds.size)
}
if toPoints == nil {
toPoints = generateNextCurve(for: bounds.size)
}
let duration = CGFloat(1 / (minSpeed + (maxSpeed - minSpeed) * speedLevel))
let fromValue: CGFloat = 0
let toValue: CGFloat = 1
let animation = DisplayLinkAnimator(duration: Double(duration), from: fromValue, to: toValue, update: { [weak self] value in
self?.transition = value
}, completion: { [weak self] in
guard let `self` = self else {
return
}
self.fromPoints = self.currentPoints
self.toPoints = nil
self.curveAnimation = nil
self.animateToNewShape()
})
self.curveAnimation = animation
lastSpeedLevel = speedLevel
speedLevel = 0
}
private func generateNextCurve(for size: CGSize) -> [CGPoint] {
let randomness = minRandomness + (maxRandomness - minRandomness) * speedLevel
return curve(pointsCount: pointsCount, randomness: randomness).map {
return CGPoint(x: $0.x * CGFloat(size.width), y: size.height - 18.0 + $0.y * 12.0)
}
}
private func curve(pointsCount: Int, randomness: CGFloat) -> [CGPoint] {
let segment = 1.0 / CGFloat(pointsCount - 1)
let rgen = { () -> CGFloat in
let accuracy: UInt32 = 1000
let random = arc4random_uniform(accuracy)
return CGFloat(random) / CGFloat(accuracy)
}
let rangeStart: CGFloat = 1.0 / (1.0 + randomness / 10.0)
let points = (0 ..< pointsCount).map { i -> CGPoint in
let randPointOffset = (rangeStart + CGFloat(rgen()) * (1 - rangeStart)) / 2
let segmentRandomness: CGFloat = randomness
let pointX: CGFloat
let pointY: CGFloat
let randomXDelta: CGFloat
if i == 0 {
pointX = 0.0
pointY = 0.0
randomXDelta = 0.0
} else if i == pointsCount - 1 {
pointX = 1.0
pointY = 0.0
randomXDelta = 0.0
} else {
pointX = segment * CGFloat(i)
pointY = ((segmentRandomness * CGFloat(arc4random_uniform(100)) / CGFloat(100)) - segmentRandomness * 0.5) * randPointOffset
randomXDelta = segment - segment * randPointOffset
}
return CGPoint(x: pointX + randomXDelta, y: pointY)
}
return points
}
}
protocol CallStatusBarBackground : View {
var audioLevel: Float { get set }
var speaking:(Bool, Bool, Bool)? { get set }
}
class CallHeaderBasicView : NavigationHeaderView {
private let _backgroundView: CallStatusBarBackground
var backgroundView: CallStatusBarBackground {
return _backgroundView
}
private let container = View()
fileprivate let callInfo:TextButton = TextButton()
fileprivate let endCall:ImageButton = ImageButton()
fileprivate let statusTextView:TextView = TextView()
fileprivate let muteControl:ImageButton = ImageButton()
fileprivate let capView = View()
let disposable = MetaDisposable()
let hideDisposable = MetaDisposable()
override func hide(_ animated: Bool) {
super.hide(true)
disposable.set(nil)
hideDisposable.set(nil)
}
private var statusTimer: SwiftSignalKit.Timer?
var status: CallControllerStatusValue = .text("", nil) {
didSet {
if self.status != oldValue {
self.statusTimer?.invalidate()
if self.status.hasTimer == true {
self.statusTimer = SwiftSignalKit.Timer(timeout: 0.5, repeat: true, completion: { [weak self] in
self?.updateStatus()
}, queue: Queue.mainQueue())
self.statusTimer?.start()
self.updateStatus()
} else {
self.updateStatus()
}
}
}
}
private func updateStatus(animated: Bool = true) {
var statusText: String = ""
switch self.status {
case let .text(text, _):
statusText = text
case let .timer(referenceTime, _):
let duration = Int32(CFAbsoluteTimeGetCurrent() - referenceTime)
let durationString: String
if duration > 60 * 60 {
durationString = String(format: "%02d:%02d:%02d", arguments: [duration / 3600, (duration / 60) % 60, duration % 60])
} else {
durationString = String(format: "%02d:%02d", arguments: [(duration / 60) % 60, duration % 60])
}
statusText = durationString
case let .startsIn(time):
statusText = strings().chatHeaderVoiceChatStartsIn(timerText(time - Int(Date().timeIntervalSince1970)))
}
let layout = TextViewLayout.init(.initialize(string: statusText, color: .white, font: .normal(13)))
layout.measure(width: .greatestFiniteMagnitude)
self.statusTextView.update(layout)
needsLayout = true
}
deinit {
disposable.dispose()
hideDisposable.dispose()
}
override init(_ header: NavigationHeader) {
if #available(OSX 10.12, *) {
self._backgroundView = CallStatusBarBackgroundView()
} else {
self._backgroundView = CallStatusBarBackgroundViewLegacy()
}
super.init(header)
backgroundView.frame = bounds
backgroundView.wantsLayer = true
addSubview(capView)
addSubview(backgroundView)
addSubview(container)
statusTextView.backgroundColor = .clear
statusTextView.userInteractionEnabled = false
statusTextView.isSelectable = false
callInfo.set(font: .medium(.text), for: .Normal)
callInfo.disableActions()
container.addSubview(callInfo)
callInfo.userInteractionEnabled = false
endCall.disableActions()
container.addSubview(endCall)
endCall.scaleOnClick = true
muteControl.scaleOnClick = true
container.addSubview(statusTextView)
callInfo.set(handler: { [weak self] _ in
self?.showInfoWindow()
}, for: .Click)
endCall.set(handler: { [weak self] _ in
self?.hangUp()
}, for: .Click)
muteControl.autohighlight = false
container.addSubview(muteControl)
muteControl.set(handler: { [weak self] _ in
self?.toggleMute()
}, for: .Click)
updateLocalizationAndTheme(theme: theme)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
func toggleMute() {
}
func showInfoWindow() {
}
func hangUp() {
}
func setInfo(_ text: String) {
self.callInfo.set(text: text, for: .Normal)
}
func setMicroIcon(_ image: CGImage) {
muteControl.set(image: image, for: .Normal)
_ = muteControl.sizeToFit()
}
override func mouseUp(with event: NSEvent) {
super.mouseUp(with: event)
let point = self.convert(event.locationInWindow, from: nil)
if let header = header, point.y <= header.height {
showInfoWindow()
}
}
var blueColor:NSColor {
return theme.colors.accentSelect
}
var grayColor:NSColor {
return theme.colors.grayText
}
func getEndText() -> String {
return strings().callHeaderEndCall
}
override func layout() {
super.layout()
capView.frame = NSMakeRect(0, 0, frame.width, height)
backgroundView.frame = bounds
container.frame = NSMakeRect(0, 0, frame.width, height)
muteControl.centerY(x:18)
statusTextView.centerY(x: muteControl.frame.maxX + 6)
endCall.centerY(x: frame.width - endCall.frame.width - 20)
_ = callInfo.sizeToFit(NSZeroSize, NSMakeSize(frame.width - statusTextView.frame.width - 60 - 20 - endCall.frame.width - 10, callInfo.frame.height), thatFit: false)
let rect = container.focus(callInfo.frame.size)
callInfo.setFrameOrigin(NSMakePoint(max(statusTextView.frame.maxX + 10, min(rect.minX, endCall.frame.minX - 10 - callInfo.frame.width)), rect.minY))
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
let theme = (theme as! TelegramPresentationTheme)
self.capView.backgroundColor = backgroundView.backgroundColor
endCall.set(image: theme.icons.callInlineDecline, for: .Normal)
endCall.set(image: theme.icons.callInlineDecline, for: .Highlight)
_ = endCall.sizeToFit(NSMakeSize(10, 10), thatFit: false)
callInfo.set(color: .white, for: .Normal)
needsLayout = true
}
}
class CallNavigationHeaderView: CallHeaderBasicView {
var session: PCallSession? {
get {
self.header?.contextObject as? PCallSession
}
}
private let audioLevelDisposable = MetaDisposable()
deinit {
audioLevelDisposable.dispose()
}
fileprivate weak var accountPeer: Peer?
fileprivate var state: CallState?
override func showInfoWindow() {
if let session = self.session {
showCallWindow(session)
}
}
override func hangUp() {
self.session?.hangUpCurrentCall()
}
override func toggleMute() {
session?.toggleMute()
}
override func update(with contextObject: Any) {
super.update(with: contextObject)
let session = contextObject as! PCallSession
let account = session.account
let signal = Signal<Peer?, NoError>.single(session.peer) |> then(session.account.postbox.loadedPeerWithId(session.peerId) |> map(Optional.init) |> deliverOnMainQueue)
let accountPeer: Signal<Peer?, NoError> = session.accountContext.sharedContext.activeAccounts |> mapToSignal { accounts in
if accounts.accounts.count == 1 {
return .single(nil)
} else {
return account.postbox.loadedPeerWithId(account.peerId) |> map(Optional.init)
}
}
disposable.set(combineLatest(queue: .mainQueue(), session.state, signal, accountPeer).start(next: { [weak self] state, peer, accountPeer in
if let peer = peer {
self?.setInfo(peer.displayTitle)
}
self?.updateState(state, accountPeer: accountPeer, animated: false)
self?.needsLayout = true
self?.ready.set(.single(true))
}))
audioLevelDisposable.set((session.audioLevel |> deliverOnMainQueue).start(next: { [weak self] value in
self?.backgroundView.audioLevel = value
}))
hideDisposable.set((session.canBeRemoved |> deliverOnMainQueue).start(next: { [weak self] value in
if value {
self?.hide(true)
}
}))
}
private func updateState(_ state:CallState, accountPeer: Peer?, animated: Bool) {
self.state = state
self.status = state.state.statusText(accountPeer, state.videoState)
var isConnected: Bool = false
let isMuted = state.isMuted
switch state.state {
case .active:
isConnected = true
default:
isConnected = false
}
self.backgroundView.speaking = (isConnected && !isMuted, isConnected, true)
if animated {
backgroundView.layer?.animateBackground()
}
setMicroIcon(!state.isMuted ? theme.icons.callInlineUnmuted : theme.icons.callInlineMuted)
needsLayout = true
switch state.state {
case let .terminated(_, reason, _):
if let reason = reason, reason.recall {
} else {
muteControl.removeAllHandlers()
endCall.removeAllHandlers()
callInfo.removeAllHandlers()
muteControl.change(opacity: 0.8, animated: animated)
endCall.change(opacity: 0.8, animated: animated)
statusTextView._change(opacity: 0.8, animated: animated)
callInfo._change(opacity: 0.8, animated: animated)
}
default:
break
}
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
if let state = state {
self.updateState(state, accountPeer: accountPeer, animated: false)
}
}
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(_ header: NavigationHeader) {
super.init(header)
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}