-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathNavigationViewController.swift
1403 lines (1067 loc) · 57 KB
/
NavigationViewController.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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// NavigationViewController.swift
// TGUIKit
//
// Created by keepcoder on 15/09/16.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
import SwiftSignalKit
import AppKit
public enum ViewControllerStyle {
case push
case pop
case none
}
open class NavigationHeaderView : View {
public private(set) weak var header:NavigationHeader?
public let ready:Promise<Bool> = Promise()
public private(set) var height: CGFloat
public init(_ header:NavigationHeader) {
self.header = header
self.height = header.height
super.init(frame: NSMakeRect(0, 0, 0, header.height))
self.autoresizingMask = [.width]
}
open func update(with contextObject: Any) {
self.header?.contextObject = contextObject
}
open func destroy() {
self.header?.contextObject = nil
}
open func hide(_ animated: Bool) {
destroy()
self.header?.hide(animated)
}
required public init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
open class NavigationHeader {
public var contextObject: Any? = nil
fileprivate weak var supplyHeader:NavigationHeader?
fileprivate weak var simpleHeader:NavigationHeader?
public let height:CGFloat
fileprivate var realHeight: CGFloat = 0
let initializer:(NavigationHeader, Any, NavigationHeaderView?)->(NavigationHeaderView, CGFloat)
weak var navigation:NavigationViewController?
fileprivate let disposable:MetaDisposable = MetaDisposable()
fileprivate(set) var isShown:Bool = false
public var needShown:Bool = false
public init(_ height:CGFloat, initializer:@escaping(NavigationHeader, Any, NavigationHeaderView?)->(NavigationHeaderView, CGFloat)) {
self.height = height
self.initializer = initializer
}
fileprivate var _view:NavigationHeaderView?
public var view:NavigationHeaderView {
return _view!
}
deinit {
disposable.dispose()
}
open func show(_ animated:Bool, contextObject: Any) {
assert(navigation != nil)
self.contextObject = contextObject
let initialized = initializer(self, contextObject, self._view)
if isShown {
if initialized.0 === _view {
return
} else if _view != nil {
hide(animated)
}
}
isShown = true
if let navigation = navigation {
readyShow(navigation, animated: animated, initialized: initialized)
}
}
func readyShow(_ navigation: NavigationViewController, animated: Bool, initialized: (NavigationHeaderView, CGFloat)) {
self._view = initialized.0
self.realHeight = initialized.1
let view = self.view
let realHeight = self.realHeight
let height = self.height
view.frame = NSMakeRect(0, -height, navigation.containerView.frame.width, realHeight)
disposable.set((view.ready.get() |> filter {$0} |> take(1)).start(next: { [weak navigation, weak self, weak view] (ready) in
if let navigation = navigation, let view = view {
var contentInset = height + navigation.controller.barHeight
self?.needShown = true
if let header = self?.supplyHeader, header.needShown {
navigation.containerView.addSubview(view, positioned: .below, relativeTo: header.view)
} else {
navigation.containerView.addSubview(view, positioned: .above, relativeTo: navigation.controller.view)
}
var inset:CGFloat = 0
var barInset: CGFloat = height
if let supplyHeader = self?.supplyHeader, supplyHeader.needShown {
inset += supplyHeader.height
contentInset += supplyHeader.height
barInset += supplyHeader.height
}
let transition: ContainedViewLayoutTransition
if animated {
transition = .animated(duration: 0.2, curve: .easeOut)
} else {
transition = .immediate
}
let size = navigation.navigationBar.frame.size
transition.updateFrame(view: navigation.navigationBar, frame: CGRect(origin: NSMakePoint(0, barInset), size: size))
transition.updateFrame(view: view, frame: NSMakeRect(0, inset, view.frame.width, view.frame.height))
navigation.controller.updateFrame(NSMakeRect(0, contentInset, navigation.controller.frame.width, navigation.frame.height - contentInset), transition: transition)
}
}))
}
open func hide(_ animated:Bool) {
assert(navigation != nil)
self.contextObject = nil
if !isShown {
return
}
needShown = false
isShown = false
if let navigation = navigation, let view = _view {
_view = nil
performSubviewPosRemoval(view, pos: NSMakePoint(0, -height), animated: animated, duration: 0.2, timingFunction: .easeOut)
var barInset: CGFloat = 0
var inset:CGFloat = navigation.controller.barHeight
if let supplyHeader = supplyHeader, supplyHeader.needShown {
inset += supplyHeader.height
barInset += supplyHeader.height
}
let transition: ContainedViewLayoutTransition
if animated {
transition = .animated(duration: 0.2, curve: .easeOut)
} else {
transition = .immediate
}
let size = navigation.navigationBar.frame.size
transition.updateFrame(view: navigation.navigationBar, frame: CGRect(origin: NSMakePoint(0, barInset), size: size))
navigation.controller.updateFrame(CGRect(origin: NSMakePoint(0, inset), size: NSMakeSize(navigation.controller.frame.width, navigation.frame.height - inset)), transition: transition)
}
}
}
public class CallNavigationHeader : NavigationHeader {
override func readyShow(_ navigation: NavigationViewController, animated: Bool, initialized: (NavigationHeaderView, CGFloat)) {
self._view = initialized.0
self.realHeight = initialized.1
self._view = initialized.0
self.realHeight = initialized.1
let view = self.view
let realHeight = self.realHeight
let height = self.height
view.frame = NSMakeRect(0, -realHeight, navigation.containerView.frame.width, realHeight)
disposable.set((view.ready.get() |> take(1)).start(next: { [weak navigation, weak view, weak self] (ready) in
if let navigation = navigation, let view = view {
self?.needShown = true
var contentInset = height + navigation.controller.barHeight
if let simpleHeader = self?.simpleHeader, simpleHeader.needShown {
navigation.containerView.addSubview(view, positioned: .above, relativeTo: simpleHeader.view)
} else {
navigation.containerView.addSubview(view, positioned: .above, relativeTo: navigation.controller.view)
}
var barInset: CGFloat = height
let transition: ContainedViewLayoutTransition
if animated {
transition = .animated(duration: 0.2, curve: .easeOut)
} else {
transition = .immediate
}
if let simpleHeader = self?.simpleHeader, simpleHeader.needShown {
let view = simpleHeader.view
let size = view.frame.size
transition.updateFrame(view: view, frame: CGRect(origin: NSMakePoint(0, height), size: size))
contentInset += simpleHeader.height
barInset += simpleHeader.height
}
let size = navigation.navigationBar.frame.size
transition.updateFrame(view: navigation.navigationBar, frame: CGRect(origin: NSMakePoint(0, barInset), size: size))
transition.updateFrame(view: view, frame: CGRect(origin: .zero, size: view.frame.size))
navigation.controller.updateFrame(NSMakeRect(0, contentInset, navigation.controller.frame.width, navigation.frame.height - contentInset), transition: transition)
}
}))
}
public override func hide(_ animated:Bool) {
assert(navigation != nil)
if !isShown {
return
}
needShown = false
isShown = false
if let navigation = navigation, let view = _view {
_view = nil
performSubviewPosRemoval(view, pos: NSMakePoint(0, -realHeight), animated: animated, duration: 0.2, timingFunction: .easeOut)
let transition: ContainedViewLayoutTransition
if animated {
transition = .animated(duration: 0.2, curve: .easeOut)
} else {
transition = .immediate
}
var contentInset: CGFloat = navigation.controller.barHeight
if let header = simpleHeader, header.needShown {
transition.updateFrame(view: header.view, frame: CGRect.init(origin: NSMakePoint(0, 0), size: header.view.frame.size))
contentInset += header.height
}
transition.updateFrame(view: navigation.navigationBar, frame: CGRect.init(origin: NSMakePoint(0, contentInset - navigation.navigationBar.frame.height), size: navigation.navigationBar.frame.size))
navigation.controller.updateFrame(NSMakeRect(0, contentInset, navigation.controller.frame.width, navigation.frame.height - contentInset), transition: transition)
}
}
}
enum NavigationShadowDirection {
case left, right
}
final class NavigationShadowView : View {
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
var direction: NavigationShadowDirection = .left {
didSet {
needsDisplay = true
}
}
var getColor:()->NSColor = { presentation.colors.background }
override func draw(_ layer: CALayer, in ctx: CGContext) {
super.draw(layer, in: ctx)
ctx.clear(NSMakeRect(0, 0, frame.width, frame.height))
ctx.setStrokeColor(getColor().cgColor)
ctx.setShadow(offset: CGSize.zero, blur: 8, color: .black)
ctx.setBlendMode(.multiply)
ctx.strokeLineSegments(between: [CGPoint(x: bounds.width, y: 0), CGPoint(x: bounds.width, y: bounds.height)])
// let colorSpace = CGColorSpaceCreateDeviceRGB()
// let array = [NSColor.clear.cgColor, NSColor.black.withAlphaComponent(0.2).cgColor]
// let gradient = CGGradient(colorsSpace: colorSpace, colors: NSArray(array: array), locations: nil)!
//
// ctx.drawLinearGradient(gradient, start: NSMakePoint(0, 0), end: CGPoint(x: frame.width, y: 0), options: CGGradientDrawingOptions())
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
open class NavigationViewController: ViewController, CALayerDelegate,CAAnimationDelegate {
public var applyAppearOnLoad: Bool = true
public var canAddControllers: Bool = true
public private(set) var modalAction:NavigationModalAction?
let shadowView:NavigationShadowView = NavigationShadowView(frame: NSMakeRect(0, 0, 20, 0))
let navigationRightBorder: View = View()
let navigationLeftBorder: View = View()
var stack:[ViewController] = [ViewController]()
public var cleanupAfterDeinit: Bool = true
var lock:Bool = false {
didSet {
var bp:Int = 0
bp += 1
}
}
public var hasBarRightBorder: Bool = false {
didSet {
navigationRightBorder.isEventLess = true
navigationRightBorder.isHidden = !hasBarRightBorder
navigationRightBorder.backgroundColor = presentation.colors.border
}
}
public var hasBarLeftBorder: Bool = false {
didSet {
navigationLeftBorder.isEventLess = true
navigationLeftBorder.isHidden = !hasBarLeftBorder
navigationLeftBorder.backgroundColor = presentation.colors.border
}
}
private let _window: Window
open override var window: Window? {
return _window
}
public var empty:ViewController {
didSet {
empty.navigationController = self
let prev = self.stack.last
self.stack.remove(at: 0)
self.stack.insert(empty, at: 0)
var controllerInset:CGFloat = 0
if let header = header, header.needShown {
controllerInset += header.height
}
if let header = callHeader, header.needShown {
controllerInset += header.height
}
let rect = NSMakeRect(0, controllerInset, self.bounds.width, self.bounds.height - controllerInset - bar.height)
empty.loadViewIfNeeded(rect)
if prev == oldValue {
controller = empty
oldValue.removeFromSuperview()
containerView.addSubview(empty.view)
if let header = header, header.needShown {
header.view.removeFromSuperview()
containerView.addSubview(header.view, positioned: .below, relativeTo: navigationBar)
}
if let header = callHeader, header.needShown {
header.view.removeFromSuperview()
containerView.addSubview(header.view, positioned: .below, relativeTo: navigationBar)
}
}
empty.view.frame = rect
}
}
open var isLocked:Bool {
return lock
}
public private(set) var controller:ViewController {
didSet {
currentControllerDidChange()
}
}
func _setController(_ controller:ViewController) {
self.controller = controller
}
private(set) public var navigationBar:NavigationBarView = NavigationBarView()
let pushDisposable:MetaDisposable = MetaDisposable()
let popDisposable:MetaDisposable = MetaDisposable()
private(set) public var header:NavigationHeader?
private(set) public var callHeader:CallNavigationHeader?
var containerView:BackgroundView = BackgroundView(frame: NSZeroRect)
public var backgroundMode: TableBackgroundMode {
get {
return containerView.backgroundMode
}
set {
containerView.backgroundMode = newValue
}
}
public func doBackgroundAction() {
DispatchQueue.main.async { [weak self] in
self?.containerView.doAction()
}
}
public func set(header:NavigationHeader?) {
self.header?.hide(false)
header?.navigation = self
header?.supplyHeader = callHeader
callHeader?.simpleHeader = header
self.header = header
}
public func set(callHeader:CallNavigationHeader?) {
self.callHeader?.hide(false)
callHeader?.navigation = self
header?.supplyHeader = callHeader
callHeader?.simpleHeader = header
self.callHeader = callHeader
}
open override func loadView() {
super.loadView();
}
public var navigationBarLeftPosition: CGFloat = 0 {
didSet {
navigationBar.needsLayout = true
}
}
open override func viewDidLoad() {
super.viewDidLoad()
containerView.frame = bounds
//self.view.autoresizesSubviews = true
//containerView.autoresizesSubviews = true
//containerView.autoresizingMask = [.width, .height]
self.view.addSubview(containerView, positioned: .below, relativeTo: self.view.subviews.first)
controller._frameRect = bounds
if self.applyAppearOnLoad {
controller.viewWillAppear(false)
}
controller.navigationController = self
containerView.addSubview(navigationBar)
navigationBar.frame = NSMakeRect(0, 0, NSWidth(containerView.frame), controller.barHeight)
controller.view.frame = NSMakeRect(0, controller.barHeight , NSWidth(containerView.frame), NSHeight(containerView.frame) - controller.barHeight)
navigationBar.switchViews(left: controller.leftBarView, center: controller.centerBarView, right: controller.rightBarView, controller: controller, style: .none, animationStyle: controller.animationStyle, liveSwiping: false)
containerView.addSubview(controller.view)
self.view.addSubview(navigationRightBorder)
self.view.addSubview(navigationLeftBorder)
// self.view.addSubview(navigationLeftBorder)
navigationRightBorder.frame = NSMakeRect(frame.width - .borderSize, 0, .borderSize, frame.height)
navigationLeftBorder.frame = NSMakeRect(-.borderSize, 0, .borderSize, frame.height)
if self.applyAppearOnLoad {
Queue.mainQueue().justDispatch {
self.controller.viewDidAppear(false)
}
}
}
var barInset: CGFloat {
var barInset:CGFloat = 0
if let header = header, header.needShown {
barInset += header.height
}
if let header = callHeader, header.needShown {
barInset += header.height
}
return barInset
}
override open func swapNavigationBar(leftView: BarView?, centerView: BarView?, rightView: BarView?, animation: NavigationBarSwapAnimation) {
navigationBar.frame = NSMakeRect(0, self.navigationBar.frame.minY, containerView.frame.width, controller.barHeight)
if let leftView = leftView {
navigationBar.switchLeftView(leftView, animation: animation)
}
if let centerView = centerView {
navigationBar.switchCenterView(centerView, animation: animation)
}
if let rightView = rightView {
navigationBar.switchRightView(rightView, animation: animation)
}
}
var containerSize: NSSize {
return frame.size
}
open override func viewDidResized(_ size: NSSize) {
super.viewDidResized(size)
let settings = self.controller.stake
let keepLeft: CGFloat = size.width == settings.keepLeft ? 0 : settings.keepLeft
let keepTop = size.width == settings.keepLeft ? 0 : settings.keepTop()
let width = containerSize.width - keepLeft
let point = NSMakePoint(keepLeft, 0)
containerView.frame = NSMakeRect(0, 0, containerSize.width, containerSize.height)
navigationBar.frame = NSMakeRect(point.x, navigationBar.frame.minY, width, controller.bar.height)
navigationRightBorder.frame = NSMakeRect(size.width - .borderSize, keepTop, .borderSize, frame.height)
navigationLeftBorder.frame = NSMakeRect(point.x - .borderSize, keepTop, .borderSize, size.height)
if let header = callHeader, header.needShown {
header.view.setFrameSize(NSMakeSize(width, header.realHeight))
}
if let header = header, header.needShown {
header.view.setFrameSize(NSMakeSize(width, header.realHeight))
}
if controller.isLoaded() {
controller.frame = NSMakeRect(point.x, barInset + controller.barHeight + keepTop, width, containerSize.height - barInset - controller.barHeight)
if let tied = controller.tied {
containerView.addSubview(tied.view, positioned: .below, relativeTo: controller.view)
tied.view.frame = NSMakeRect(0, barInset + tied.bar.height, containerSize.width, containerSize.height - barInset - tied.bar.height)
if tied.widthOnDisappear != nil {
tied.widthOnDisappear = containerSize.width
}
}
}
}
public func cancelCurrentController() {
pushDisposable.set(nil)
}
open override var canBecomeResponder: Bool {
return false
}
open func currentControllerDidChange() {
}
public override var backgroundColor: NSColor {
set {
self.view.background = newValue
navigationBar.backgroundColor = newValue
}
get {
return self.view.background
}
}
public init(_ empty:ViewController, _ window: Window) {
self.empty = empty
self.controller = empty
self._window = window
self.stack.append(controller)
super.init()
navigationBar.navigation = self
bar = .init(height: 0)
shadowView.getColor = { [weak self] in
self?.controller.backgroundColor ?? presentation.colors.background
}
}
public var stackCount:Int {
return stack.count
}
deinit {
self.popDisposable.dispose()
self.pushDisposable.dispose()
if cleanupAfterDeinit {
while !stack.isEmpty {
let value = stack.removeFirst()
value.removeFromSuperview()
}
}
}
public func stackInsert(_ controller:ViewController, at: Int) -> Void {
controller.navigationController = self
controller.loadViewIfNeeded(self.bounds)
stack.insert(controller, at: at)
}
open func push(_ controller:ViewController, _ animated:Bool = true, style: ViewControllerStyle? = nil) -> Void {
// if isLocked {
// return
// }
if !self.canAddControllers {
return
}
if controller.abolishWhenNavigationSame, controller.className == self.controller.className {
return
}
controller.navigationController = self
controller.loadViewIfNeeded(self.bounds)
self.pushDisposable.set((controller.ready.get() |> take(1)).start(next: {[weak self] _ in
if let strongSelf = self {
controller.navigationController = strongSelf
if let index = strongSelf.stack.firstIndex(of: controller) {
strongSelf.stack.remove(at: index)
}
strongSelf.stack.append(controller)
let newStyle:ViewControllerStyle
if let style = style {
newStyle = style
} else {
newStyle = animated && strongSelf.stack.count > 1 ? .push : .none
}
CATransaction.begin()
strongSelf.show(controller, newStyle)
CATransaction.commit()
}
}))
}
func show(_ controller:ViewController,_ style:ViewControllerStyle) -> Void {
lock = true
guard let window = self.window else {return}
if style == .none {
window.abortSwiping()
}
defer {
CATransaction.commit()
}
CATransaction.begin()
let keepLeft = frame.width == controller.stake.keepLeft ? 0 : controller.stake.keepLeft
let previous:ViewController = self.controller;
controller.navigationController = self
previous.setToNextController(controller, style: style)
controller.setToPreviousController(previous, style: style)
let keepTop = frame.width == controller.stake.keepLeft ? 0 : controller.stake.keepTop()
var contentInset = controller.barHeight + keepTop
let size = CGSize(width: containerView.frame.width - keepLeft, height: containerView.frame.height)
let point = NSMakePoint(keepLeft, contentInset)
if(previous === controller && stackCount > 1) {
previous.viewWillDisappear(false)
previous.viewDidDisappear(false)
controller.viewWillAppear(false)
controller.viewDidAppear(false)
_ = controller.becomeFirstResponder()
lock = false
navigationRightBorder.frame = NSMakeRect(frame.width - .borderSize, keepTop, .borderSize, frame.height)
navigationLeftBorder.frame = NSMakeRect(keepLeft - .borderSize, keepTop, .borderSize, frame.height)
return;
}
var barInset:CGFloat = 0
if let header = callHeader, header.needShown {
header.view.frame = NSMakeRect(point.x, 0, size.width, header.realHeight)
contentInset += header.height
barInset += header.height
}
if let header = header, header.needShown {
header.view.frame = NSMakeRect(point.x, barInset, size.width, header.realHeight)
containerView.addSubview(header.view, positioned: .below, relativeTo: self.navigationBar)
contentInset += header.height
barInset += header.height
}
let animatePosBar: Bool = controller.bar.height != previous.bar.height && style != .none
self.navigationBar.frame = NSMakeRect(point.x, barInset, size.width, animatePosBar && controller.bar.height == 0 ? previous.bar.height : controller.bar.height)
self.navigationBar.layer?.removeAllAnimations()
if keepLeft > 0 || previous.stake.keepIn {
controller.tied = previous
} else {
controller.tied = nil
}
previous.tied = nil
// controller.view.removeFromSuperview()
let popInteractiveInset: CGFloat? = window.inLiveSwiping ? controller.frame.minX : nil
controller.view.frame = NSMakeRect(point.x, contentInset, size.width, size.height - contentInset)
let reloadHeaders = { [weak self] in
if let header = self?.callHeader, header.needShown {
self?.containerView.addSubview(header.view, positioned: .above, relativeTo: controller.view)
}
if let header = self?.header, header.needShown {
self?.containerView.addSubview(header.view, positioned: .above, relativeTo: controller.view)
}
}
var pfrom:CGFloat = 0, pto:CGFloat = 0, nto:CGFloat = 0, nfrom:CGFloat = 0;
var sto: CGFloat = 0
switch style {
case .push:
controller.view.disableHierarchyDynamicContent()
previous.viewWillDisappear(true);
controller.viewWillAppear(true);
nfrom = popInteractiveInset != nil ? popInteractiveInset! : frame.width
nto = keepLeft
pfrom = previous.view.frame.minX
if previous.stake.keepLeft > 0 && previous.stake.keepLeft != frame.width {
pto = frame.maxX
} else if keepLeft > 0 {
pto = 0
} else {
pto = -round(NSWidth(self.frame)/3.0)
}
previous.view.setFrameOrigin(NSMakePoint(pfrom, previous.frame.minY))
controller.view.setFrameOrigin(NSMakePoint(nfrom, controller.frame.minY))
containerView.addSubview(controller.view, positioned: .above, relativeTo: previous.view)
sto = -shadowView.frame.width + keepLeft
if controller.isOpaque {
addShadowView(.right, controller: controller, updateOrigin: shadowView.superview == nil)
}
if animatePosBar {
navigationBar.layer?.animatePosition(from: NSMakePoint(nfrom, navigationBar.frame.minY), to: NSMakePoint(nto, barInset), duration: previous.animationStyle.duration, timingFunction: previous.animationStyle.function)
}
case .pop:
controller.view.disableHierarchyDynamicContent()
previous.viewWillDisappear(true);
controller.viewWillAppear(true);
if let popInteractiveInset = popInteractiveInset {
nfrom = popInteractiveInset
} else if previous.stake.keepLeft > 0 && previous.stake.keepLeft != frame.width {
nfrom = 0
} else {
nfrom = -round(frame.width/3.0)
}
nto = 0
pfrom = previous.view.frame.minX
pto = frame.width
previous.view.setFrameOrigin(NSMakePoint(pfrom, previous.frame.minY))
controller.view.setFrameOrigin(NSMakePoint(nfrom, controller.frame.minY))
containerView.addSubview(controller.view, positioned: .below, relativeTo: previous.view)
if animatePosBar {
navigationBar.layer?.animatePosition(from: NSMakePoint(pfrom, barInset), to: NSMakePoint(pto, barInset), duration: previous.animationStyle.duration, timingFunction: previous.animationStyle.function, removeOnCompletion: false, completion: { [weak controller, weak navigationBar] completed in
if let controller = controller {
navigationBar?.frame = NSMakeRect(0, barInset, controller.frame.width, controller.bar.height)
}
navigationBar?.layer?.removeAllAnimations()
})
}
sto = frame.width
if controller.isOpaque {
addShadowView(.left, controller: previous, updateOrigin: shadowView.superview == nil)
}
case .none:
previous.viewWillDisappear(false);
previous.view.removeFromSuperview()
containerView.addSubview(controller.view)
self.controller = controller
controller.viewWillAppear(false);
previous.viewDidDisappear(false);
controller.viewDidAppear(false);
_ = controller.becomeFirstResponder();
self.navigationBar.switchViews(left: controller.leftBarView, center: controller.centerBarView, right: controller.rightBarView, controller: controller, style: style, animationStyle: controller.animationStyle, liveSwiping: false)
lock = false
navigationBar.removeFromSuperview()
navigationBar.frame = NSMakeRect(point.x, barInset, controller.frame.width, controller.bar.height)
navigationBar.removeFromSuperview()
containerView.addSubview(navigationBar, positioned: .below, relativeTo: self.controller.view)
reloadHeaders()
navigationRightBorder.frame = NSMakeRect(frame.width - .borderSize, keepTop, .borderSize, frame.height)
navigationLeftBorder.frame = NSMakeRect(keepLeft - .borderSize, keepTop, .borderSize, frame.height)
return // without animations
}
self.controller = controller
let prevBackgroundView: NSView = containerView.copy(previous.bgMode)
let nextBackgroundView: NSView = containerView.copy(controller.bgMode)
if !controller.isOpaque {
controller.view.addSubview(nextBackgroundView, positioned: .below, relativeTo: controller.view.subviews.first)
nextBackgroundView.setFrameOrigin(NSMakePoint(nextBackgroundView.frame.minX, -controller.view.frame.minY))
}
if !previous.isOpaque {
previous.view.addSubview(prevBackgroundView, positioned: .below, relativeTo: previous.view.subviews.first)
prevBackgroundView.setFrameOrigin(NSMakePoint(prevBackgroundView.frame.minX, -previous.view.frame.minY))
}
if let index = stack.firstIndex(of: previous) {
if previous.removeAfterDisapper {
self.stack.remove(at: index)
} else if previous.stake.keepLeft > 0 {
self.stack.remove(at: index)
}
}
containerView.addSubview(navigationBar, positioned: .below, relativeTo: self.controller.view)
reloadHeaders()
navigationRightBorder.frame = NSMakeRect(frame.width - .borderSize, 0, .borderSize, frame.height)
let n_left_previous = navigationLeftBorder.frame.minY
if keepLeft > 0 {
navigationLeftBorder.frame = NSMakeRect(keepLeft - .borderSize, keepTop, .borderSize, frame.height)
navigationLeftBorder.layer?.animatePosition(from: NSMakePoint(nfrom, keepTop), to: navigationLeftBorder.frame.origin, duration: controller.animationStyle.duration, timingFunction: controller.animationStyle.function)
} else {
navigationLeftBorder.frame = NSMakeRect(pto - .borderSize, n_left_previous, .borderSize, frame.height)
navigationLeftBorder.layer?.animatePosition(from: NSMakePoint(pfrom, n_left_previous), to: navigationLeftBorder.frame.origin, duration: controller.animationStyle.duration, timingFunction: controller.animationStyle.function)
}
shadowView.change(opacity: shadowView.direction == .left || keepLeft > 0 ? 0.2 : 1, animated: true, duration: previous.animationStyle.duration, timingFunction: previous.animationStyle.function)
shadowView.change(pos: NSMakePoint(sto, shadowView.frame.minY), animated: true, duration: previous.animationStyle.duration, timingFunction: previous.animationStyle.function, completion: { [weak self] completed in
if completed {
self?.shadowView.removeFromSuperview()
}
})
if !animatePosBar || (animatePosBar && style == .push) {
self.navigationBar.switchViews(left: controller.leftBarView, center: controller.centerBarView, right: controller.rightBarView, controller: controller, style: animatePosBar ? .none : style, animationStyle: controller.animationStyle, liveSwiping: window.inLiveSwiping)
}
previous.view._change(pos: NSMakePoint(pto, previous.frame.minY), animated: true, duration: controller.animationStyle.duration, timingFunction: .spring, completion: { [weak prevBackgroundView] completed in
if completed, keepLeft == 0 || previous.stake.keepLeft > 0 {
previous.view.removeFromSuperview()
previous.viewDidDisappear(true);
prevBackgroundView?.removeFromSuperview()
}
});
controller.view._change(pos: NSMakePoint(nto, controller.frame.minY), animated: true, duration: controller.animationStyle.duration, timingFunction: .spring, completion: { [weak nextBackgroundView, weak self] completed in
guard let `self` = self else { return }
if completed {
controller.viewDidAppear(true);
_ = controller.becomeFirstResponder()
nextBackgroundView?.removeFromSuperview()
}
self.navigationRightBorder.frame = NSMakeRect(self.frame.width - .borderSize, 0, .borderSize, self.frame.height)
self.lock = false
controller.view.restoreHierarchyDynamicContent()
})
}
public func first(_ f:(ViewController)->Bool) -> ViewController? {
return self.stack.first(where: f)
}
func addShadowView(_ shadowDirection: NavigationShadowDirection, controller: ViewController, updateOrigin: Bool = true) {
if updateOrigin {
shadowView.layer?.opacity = shadowDirection == .left ? 1.0 : 0.0
}
shadowView.layer?.removeAllAnimations()
var shadowInset: CGFloat = 0
if let callHeader = callHeader, callHeader.isShown {
shadowInset += callHeader.height
}
let keepTop = frame.width == controller.stake.keepLeft ? 0 : controller.stake.keepTop()
let keepLeft = frame.width == controller.stake.keepLeft ? 0 : controller.stake.keepLeft
shadowInset += keepTop
let leftInset: CGFloat = keepLeft
shadowView.direction = shadowDirection
shadowView.frame = NSMakeRect(updateOrigin ? (shadowDirection == .left ? -shadowView.frame.width + leftInset : containerView.frame.width) : shadowView.frame.minX, shadowInset, shadowView.frame.width, containerView.frame.height - shadowInset)
containerView.addSubview(shadowView, positioned: .below, relativeTo: navigationBar)
}
open override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
navigationBar.updateLocalizationAndTheme(theme: theme)
if let callHeader = callHeader, callHeader.needShown {
callHeader.view.updateLocalizationAndTheme(theme: theme)
}
if let header = header, header.needShown {
header.view.updateLocalizationAndTheme(theme: theme)
}
navigationRightBorder.backgroundColor = presentation.colors.border
for controller in self.stack {
if controller != self.controller, controller.isLoaded() {
controller.leftBarView.updateLocalizationAndTheme(theme: theme)