-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathViewController.swift
More file actions
807 lines (676 loc) · 29.9 KB
/
Copy pathViewController.swift
File metadata and controls
807 lines (676 loc) · 29.9 KB
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
import Foundation
import UIKit
import AsyncDisplayKit
import SwiftSignalKit
public protocol StandalonePresentableController: ViewController {
}
private func findCurrentResponder(_ view: UIView) -> UIResponder? {
if view.isFirstResponder {
return view
} else {
for subview in view.subviews {
if let result = findCurrentResponder(subview) {
return result
}
}
return nil
}
}
func findWindow(_ view: UIView) -> WindowHost? {
if let view = view as? WindowHost {
return view
} else if let superview = view.superview {
return findWindow(superview)
} else {
return nil
}
}
public enum ViewControllerPresentationAnimation {
case none
case modalSheet
}
public struct ViewControllerSupportedOrientations: Equatable {
public var regularSize: UIInterfaceOrientationMask
public var compactSize: UIInterfaceOrientationMask
public init(regularSize: UIInterfaceOrientationMask, compactSize: UIInterfaceOrientationMask) {
self.regularSize = regularSize
self.compactSize = compactSize
}
public func intersection(_ other: ViewControllerSupportedOrientations) -> ViewControllerSupportedOrientations {
return ViewControllerSupportedOrientations(regularSize: self.regularSize.intersection(other.regularSize), compactSize: self.compactSize.intersection(other.compactSize))
}
}
open class ViewControllerPresentationArguments {
public let presentationAnimation: ViewControllerPresentationAnimation
public let completion: (() -> Void)?
public init(presentationAnimation: ViewControllerPresentationAnimation, completion: (() -> Void)? = nil) {
self.presentationAnimation = presentationAnimation
self.completion = completion
}
}
public enum ViewControllerNavigationPresentation {
case `default`
case master
case modal
case flatModal
case standaloneModal
case standaloneFlatModal
case modalInLargeLayout
case modalInCompactLayout
}
public enum TabBarItemContextActionType {
case none
case always
case whenActive
}
public protocol CustomViewControllerNavigationData: AnyObject {
func combine(summary: CustomViewControllerNavigationDataSummary?) -> CustomViewControllerNavigationDataSummary?
}
public protocol CustomViewControllerNavigationDataSummary: AnyObject {
}
@objc open class ViewController: UIViewController, ContainableController {
public struct NavigationLayout {
public var navigationFrame: CGRect
public var defaultContentHeight: CGFloat
public init(navigationFrame: CGRect, defaultContentHeight: CGFloat) {
self.navigationFrame = navigationFrame
self.defaultContentHeight = defaultContentHeight
}
}
private var validLayout: ContainerViewLayout?
public var currentlyAppliedLayout: ContainerViewLayout? {
return self.validLayout
}
public let presentationContext: PresentationContext
public final var supportedOrientations: ViewControllerSupportedOrientations = ViewControllerSupportedOrientations(regularSize: .all, compactSize: .allButUpsideDown) {
didSet {
if self.supportedOrientations != oldValue {
if self.isNodeLoaded {
self.window?.invalidateSupportedOrientations()
}
}
}
}
public final var lockedOrientation: UIInterfaceOrientationMask?
public final var lockOrientation: Bool = false {
didSet {
if self.lockOrientation != oldValue {
if !self.lockOrientation {
self.lockedOrientation = nil
}
if let window = self.window {
window.invalidateSupportedOrientations()
}
}
}
}
var blocksInteractionUntilReady: Bool = false
public final var isOpaqueWhenInOverlay: Bool = false
public final var blocksBackgroundWhenInOverlay: Bool = false
public final var acceptsFocusWhenInOverlay: Bool = false
public final var automaticallyControlPresentationContextLayout: Bool = true
public var updateTransitionWhenPresentedAsModal: ((CGFloat, ContainedViewLayoutTransition) -> Void)?
public func requestUpdateParameters() {
self.modalStyleOverlayTransitionFactorUpdated?(.immediate)
}
public func combinedSupportedOrientations(currentOrientationToLock: UIInterfaceOrientationMask) -> ViewControllerSupportedOrientations {
return self.supportedOrientations
}
public final var deferScreenEdgeGestures: UIRectEdge = [] {
didSet {
if self.deferScreenEdgeGestures != oldValue {
self.window?.invalidateDeferScreenEdgeGestures()
}
}
}
public final var prefersOnScreenNavigationHidden: Bool = false {
didSet {
if self.prefersOnScreenNavigationHidden != oldValue {
self.window?.invalidatePrefersOnScreenNavigationHidden()
}
}
}
override open var prefersHomeIndicatorAutoHidden: Bool {
return self.prefersOnScreenNavigationHidden
}
open var previousItem: NavigationPreviousAction?
open var navigationPresentation: ViewControllerNavigationPresentation = .default
open var _presentedInModal: Bool = false
open var _hasGlassStyle: Bool = false
open var flatReceivesModalTransition: Bool = false
public var presentedOverCoveringView: Bool = false
public var presentationArguments: Any?
public var tabBarItemDebugTapAction: (() -> Void)?
public private(set) var modalStyleOverlayTransitionFactor: CGFloat = 0.0
public var modalStyleOverlayTransitionFactorUpdated: ((ContainedViewLayoutTransition) -> Void)?
public var customModalStyleOverlayTransitionFactorUpdated: ((ContainedViewLayoutTransition) -> Void)?
public func updateModalStyleOverlayTransitionFactor(_ value: CGFloat, transition: ContainedViewLayoutTransition) {
if self.modalStyleOverlayTransitionFactor != value {
self.modalStyleOverlayTransitionFactor = value
self.modalStyleOverlayTransitionFactorUpdated?(transition)
self.customModalStyleOverlayTransitionFactorUpdated?(transition)
}
}
private var _displayNode: ASDisplayNode?
public final var displayNode: ASDisplayNode {
get {
if let value = self._displayNode {
return value
}
else {
self.loadDisplayNode()
if self._displayNode == nil {
fatalError("displayNode should be initialized after loadDisplayNode()")
}
return self._displayNode!
}
}
set(value) {
self._displayNode = value
}
}
public final var isNodeLoaded: Bool {
return self._displayNode != nil
}
public let statusBar: StatusBar
public let navigationBar: NavigationBar?
open var transitionNavigationBar: NavigationBar? {
return self.navigationBar
}
public private(set) var toolbar: Toolbar?
public var displayNavigationBar = true
open var navigationBarRequiresEntireLayoutUpdate: Bool {
return true
}
public struct TabBarSearchState: Equatable {
public var isActive: Bool
public init(isActive: Bool) {
self.isActive = isActive
}
}
public private(set) var tabBarSearchState: TabBarSearchState?
public var tabBarSearchStateUpdated: ((ContainedViewLayoutTransition) -> Void)?
public var currentTabBarSearchNode: (() -> ASDisplayNode?)?
private weak var activeInputViewCandidate: UIResponder?
private weak var activeInputView: UIResponder?
open var hasActiveInput: Bool = false
public internal(set) var isBeingInteractivelyPopped: Bool = false
open var overlayWantsToBeBelowKeyboard: Bool {
return false
}
var internalOverlayWantsToBeBelowKeyboardUpdated: ((ContainedViewLayoutTransition) -> Void)?
public func overlayWantsToBeBelowKeyboardUpdated(transition: ContainedViewLayoutTransition) {
self.internalOverlayWantsToBeBelowKeyboardUpdated?(transition)
}
private var navigationBarOrigin: CGFloat = 0.0
open var interactiveNavivationGestureEdgeWidth: InteractiveTransitionGestureRecognizerEdgeWidth? {
return nil
}
open var navigationEdgeEffectExtension: CGFloat {
return 0.0
}
public func updateNavigationEdgeEffectExtension(transition: ContainedViewLayoutTransition) {
if let navigationBar = self.navigationBar {
navigationBar.updateEdgeEffectExtension(value: max(0.0, self.navigationEdgeEffectExtension - navigationBar.frame.maxY), transition: transition)
}
}
open func navigationLayout(layout: ContainerViewLayout) -> NavigationLayout {
let statusBarHeight: CGFloat = layout.statusBarHeight ?? 0.0
var defaultNavigationBarHeight: CGFloat
if self._presentedInModal && self._hasGlassStyle {
defaultNavigationBarHeight = 68.0
} else {
defaultNavigationBarHeight = 60.0
}
let navigationBarHeight: CGFloat = statusBarHeight + (self.navigationBar?.contentHeight(defaultHeight: defaultNavigationBarHeight) ?? defaultNavigationBarHeight)
var navigationBarFrame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: layout.size.width, height: navigationBarHeight))
navigationBarFrame.size.height += self.additionalNavigationBarHeight
if !self.displayNavigationBar {
navigationBarFrame.origin.y = -navigationBarFrame.size.height
}
self.navigationBarOrigin = navigationBarFrame.origin.y
return NavigationLayout(navigationFrame: navigationBarFrame, defaultContentHeight: defaultNavigationBarHeight)
}
open var cleanNavigationHeight: CGFloat {
if let navigationBar = self.navigationBar {
var height = navigationBar.frame.maxY
if let contentNode = navigationBar.contentNode, case .expansion = contentNode.mode {
height += contentNode.nominalHeight - contentNode.height
}
return height
} else {
return 0.0
}
}
open var additionalNavigationBarHeight: CGFloat {
return 0.0
}
public var additionalSideInsets: UIEdgeInsets = UIEdgeInsets()
private let _ready = Promise<Bool>(true)
open var ready: Promise<Bool> {
return self._ready
}
public var scrollToTop: (() -> Void)?
public var scrollToTopWithTabBar: (() -> Void)?
public var longTapWithTabBar: (() -> Void)?
public var customPresentPreviewingController: ((ViewController, ASDisplayNode) -> ViewController?)?
open func updateNavigationCustomData(_ data: Any?, progress: CGFloat, transition: ContainedViewLayoutTransition) {
}
open var customData: Any? {
get {
return nil
}
}
open var customNavigationData: CustomViewControllerNavigationData? {
get {
return nil
}
}
open var customNavigationDataSummary: CustomViewControllerNavigationDataSummary?
public internal(set) var isInFocus: Bool = false {
didSet {
if self.isInFocus != oldValue {
self.inFocusUpdated(isInFocus: self.isInFocus)
}
}
}
open func inFocusUpdated(isInFocus: Bool) {
}
public var attemptNavigation: (@escaping () -> Void) -> Bool = { _ in
return true
}
open func preferredContentSizeForLayout(_ layout: ContainerViewLayout) -> CGSize? {
return nil
}
open func didAppearInContextPreview() {
}
public var titleSignal: Signal<String?, NoError> {
return Signal { [weak self] subscriber in
guard let self else {
return EmptyDisposable
}
subscriber.putNext(self.navigationItem.title)
let listenerIndex = self.navigationItem.addSetTitleListener { title, _ in
subscriber.putNext(title)
}
return ActionDisposable { [weak self] in
if let self {
self.navigationItem.removeSetTitleListener(listenerIndex)
}
}
}
}
public init(navigationBarPresentationData: NavigationBarPresentationData?) {
self.statusBar = StatusBar()
if let navigationBarPresentationData = navigationBarPresentationData {
self.navigationBar = defaultNavigationBarImpl!(navigationBarPresentationData)
} else {
self.navigationBar = nil
}
self.presentationContext = PresentationContext()
super.init(nibName: nil, bundle: nil)
self.navigationBar?.backPressed = { [weak self] in
if let strongSelf = self, strongSelf.attemptNavigation({
guard let strongSelf = self else {
return
}
if let navigationController = strongSelf.navigationController as? NavigationController {
navigationController.filterController(strongSelf, animated: true)
} else {
strongSelf.navigationController?.popViewController(animated: true)
}
}) {
if let navigationController = strongSelf.navigationController as? NavigationController {
navigationController.filterController(strongSelf, animated: true)
} else {
strongSelf.navigationController?.popViewController(animated: true)
}
}
}
self.navigationBar?.requestContainerLayout = { [weak self] transition in
if let strongSelf = self, strongSelf.isNodeLoaded, let validLayout = strongSelf.validLayout {
if strongSelf.navigationBarRequiresEntireLayoutUpdate {
strongSelf.containerLayoutUpdated(validLayout, transition: transition)
} else {
strongSelf.updateNavigationBarLayout(validLayout, transition: transition)
}
}
}
self.navigationBar?.item = self.navigationItem
//self.automaticallyAdjustsScrollViewInsets = false
self.scrollToTopWithTabBar = { [weak self] in
self?.scrollToTop?()
}
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
}
open func updateNavigationBarLayout(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
self.applyNavigationBarLayout(layout, navigationLayout: self.navigationLayout(layout: layout), additionalBackgroundHeight: 0.0, additionalCutout: nil, transition: transition)
}
public func applyNavigationBarLayout(_ layout: ContainerViewLayout, navigationLayout: NavigationLayout, additionalBackgroundHeight: CGFloat, additionalCutout: CGSize?, transition: ContainedViewLayoutTransition) {
let statusBarHeight: CGFloat = layout.statusBarHeight ?? 0.0
var navigationBarFrame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: layout.size.width, height: navigationLayout.navigationFrame.maxY))
if !self.displayNavigationBar {
navigationBarFrame.origin.y = -navigationBarFrame.size.height
}
self.navigationBarOrigin = navigationBarFrame.origin.y
var isLandscape = layout.size.width > layout.size.height
if case .regular = layout.metrics.widthClass {
isLandscape = false
}
if let navigationBar = self.navigationBar {
if let contentNode = navigationBar.contentNode, case .expansion = contentNode.mode, !self.displayNavigationBar {
navigationBarFrame.origin.y -= navigationLayout.defaultContentHeight + statusBarHeight
navigationBarFrame.size.height += contentNode.height + navigationLayout.defaultContentHeight + statusBarHeight * 2.0
if self._presentedInModal && self._hasGlassStyle {
navigationBarFrame.size.height += 8.0
}
}
//navigationBar.backgroundColor = .blue
if let _ = navigationBar.contentNode, let _ = navigationBar.secondaryContentNode, !self.displayNavigationBar {
navigationBarFrame.size.height += navigationBar.secondaryContentHeight
}
var additionalTopHeight = statusBarHeight
if !self.displayNavigationBar {
additionalTopHeight -= statusBarHeight
if statusBarHeight != 0.0 {
additionalTopHeight += 6.0
}
}
if self._presentedInModal && self._hasGlassStyle {
additionalTopHeight += 8.0
}
navigationBar.updateLayout(size: navigationBarFrame.size, defaultHeight: navigationLayout.defaultContentHeight, additionalTopHeight: additionalTopHeight, additionalContentHeight: self.additionalNavigationBarHeight, additionalBackgroundHeight: additionalBackgroundHeight, additionalCutout: additionalCutout, leftInset: layout.safeInsets.left, rightInset: layout.safeInsets.right, appearsHidden: !self.displayNavigationBar, isLandscape: isLandscape, transition: transition)
if !transition.isAnimated {
navigationBar.layer.removeAnimation(forKey: "bounds")
navigationBar.layer.removeAnimation(forKey: "position")
}
transition.updateFrame(node: navigationBar, frame: navigationBarFrame)
navigationBar.setHidden(!self.displayNavigationBar, animated: transition.isAnimated)
}
}
open func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
self.validLayout = layout
if !self.isViewLoaded {
self.loadView()
}
if let _ = layout.statusBarHeight {
self.statusBar.frame = CGRect(origin: CGPoint(), size: CGSize(width: layout.size.width, height: 40.0))
}
self.updateNavigationBarLayout(layout, transition: transition)
if self.automaticallyControlPresentationContextLayout {
self.presentationContext.containerLayoutUpdated(layout, transition: transition)
}
}
open func navigationStackConfigurationUpdated(next: [ViewController]) {
}
open override func loadView() {
self.view = self.displayNode.view
if let navigationBar = self.navigationBar {
if navigationBar.supernode == nil {
self.displayNode.addSubnode(navigationBar)
}
}
self.view.autoresizingMask = []
self.view.addSubview(self.statusBar.view)
self.presentationContext.view = self.view
}
open func loadDisplayNode() {
self.displayNode = ASDisplayNode()
self.displayNodeDidLoad()
}
open func displayNodeDidLoad() {
if let backgroundColor = self.displayNode.backgroundColor, backgroundColor.alpha.isEqual(to: 1.0) {
self.blocksBackgroundWhenInOverlay = true
self.isOpaqueWhenInOverlay = true
}
}
public func requestLayout(transition: ContainedViewLayoutTransition) {
if self.isViewLoaded, let validLayout = self.validLayout {
self.containerLayoutUpdated(validLayout, transition: transition)
}
}
open func updateToInterfaceOrientation(_ orientation: UIInterfaceOrientation) {
}
public func setDisplayNavigationBar(_ displayNavigationBar: Bool, transition: ContainedViewLayoutTransition = .immediate) {
if displayNavigationBar != self.displayNavigationBar {
self.displayNavigationBar = displayNavigationBar
if let parent = self.parent as? TabBarController {
if parent.currentController === self {
parent.displayNavigationBar = displayNavigationBar
parent.requestLayout(transition: transition)
}
} else {
self.requestLayout(transition: transition)
}
}
}
public func setNavigationBarPresentationData(_ presentationData: NavigationBarPresentationData, animated: Bool) {
if animated, let navigationBar = self.navigationBar {
UIView.transition(with: navigationBar.view, duration: 0.3, options: [.transitionCrossDissolve], animations: {
}, completion: nil)
}
self.navigationBar?.updatePresentationData(presentationData, transition: .immediate)
if let parent = self.parent as? TabBarController {
if parent.currentController === self {
if animated, let navigationBar = parent.navigationBar {
UIView.transition(with: navigationBar.view, duration: 0.3, options: [.transitionCrossDissolve], animations: {
}, completion: nil)
}
parent.navigationBar?.updatePresentationData(presentationData, transition: .immediate)
}
}
}
public func setStatusBarStyle(_ style: StatusBarStyle, animated: Bool) {
self.statusBar.updateStatusBarStyle(style, animated: animated)
if let parent = self.parent as? TabBarController {
if parent.currentController === self {
parent.statusBar.updateStatusBarStyle(style, animated: animated)
}
}
}
override open func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
self.view.window?.rootViewController?.present(viewControllerToPresent, animated: flag, completion: completion)
}
override open func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
if let navigationController = self.navigationController as? NavigationController {
var animated = flag
if case .standaloneModal = self.navigationPresentation {
animated = false
}
navigationController.filterController(self, animated: animated)
} else {
self.presentingViewController?.dismiss(animated: flag, completion: nil)
}
}
public final var window: WindowHost? {
if let window = self.view.window as? WindowHost {
return window
} else if let result = findWindow(self.view) {
return result
} else {
if let parent = self.parent as? ViewController {
return parent.window
}
return nil
}
}
public func push(_ controller: ViewController) {
(self.navigationController as? NavigationController)?.pushViewController(controller)
}
open func replace(with controller: ViewController) {
if let navigationController = self.navigationController as? NavigationController {
var controllers = navigationController.viewControllers
controllers.removeAll(where: { $0 === self })
controllers.append(controller)
navigationController.setViewControllers(controllers, animated: true)
}
}
open func present(_ controller: ViewController, in context: PresentationContextType, with arguments: Any? = nil, blockInteraction: Bool = false, completion: @escaping () -> Void = {}) {
if !(controller is StandalonePresentableController), case .window = context, let arguments = arguments as? ViewControllerPresentationArguments, case .modalSheet = arguments.presentationAnimation, self.navigationController != nil {
controller.navigationPresentation = .modal
self.push(controller)
} else {
controller.presentationArguments = arguments
switch context {
case .current:
self.presentationContext.present(controller, on: PresentationSurfaceLevel(rawValue: 0), completion: completion)
case let .window(level):
self.window?.present(controller, on: level, blockInteraction: blockInteraction, completion: completion)
}
}
}
public func forEachController(_ f: (ContainableController) -> Bool) {
for (controller, _) in self.presentationContext.controllers {
if !f(controller) {
break
}
}
}
public func presentInGlobalOverlay(_ controller: ViewController, with arguments: Any? = nil) {
controller.presentationArguments = arguments
self.window?.presentInGlobalOverlay(controller)
}
public func addGlobalPortalHostView(sourceView: PortalSourceView) {
self.window?.addGlobalPortalHostView(sourceView: sourceView)
}
open override func viewWillDisappear(_ animated: Bool) {
self.activeInputViewCandidate = findCurrentResponder(self.view)
super.viewWillDisappear(animated)
}
open override func viewDidDisappear(_ animated: Bool) {
self.activeInputView = self.activeInputViewCandidate
super.viewDidDisappear(animated)
}
open func viewWillLeaveNavigation() {
}
open func cancelInteractiveKeyboardGestures() {
self.view.windowHost?.cancelInteractiveKeyboardGestures()
}
open override func viewDidAppear(_ animated: Bool) {
self.activeInputView = nil
super.viewDidAppear(animated)
}
open func dismiss(completion: (() -> Void)? = nil) {
if let navigationController = self.navigationController as? NavigationController {
navigationController.filterController(self, animated: true)
} else {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
completion?()
}
public final func navigationNextSibling() -> UIViewController? {
if let navigationController = self.navigationController as? NavigationController {
if let index = navigationController.viewControllers.firstIndex(where: { $0 === self }) {
if index != navigationController.viewControllers.count - 1 {
return navigationController.viewControllers[index + 1]
}
}
}
return nil
}
public func traceVisibility() -> Bool {
if !self.isViewLoaded {
return false
}
return traceViewVisibility(view: self.view, rect: self.view.bounds)
}
open func setToolbar(_ toolbar: Toolbar?, transition: ContainedViewLayoutTransition) {
if self.toolbar != toolbar {
self.toolbar = toolbar
if let parent = self.parent as? TabBarController {
if parent.currentController === self {
parent.requestLayout(transition: transition)
}
}
}
}
open func toolbarActionSelected(action: ToolbarActionOption) {
}
open var tabBarItemContextActionType: TabBarItemContextActionType = .none
open func tabBarItemContextAction(sourceView: ContextExtractedContentContainingView, gesture: ContextGesture) {
}
open func tabBarItemHasDoubleTapAction() -> Bool {
return false
}
open func tabBarItemPerformDoubleTapAction() {
}
open func tabBarDisabledAction() {
}
open func tabBarActivateSearch() {
}
open func tabBarDeactivateSearch() {
}
open func tabBarItemSwipeAction(direction: TabBarItemSwipeDirection) {
}
public func updateTabBarSearchState(_ tabBarSearchState: TabBarSearchState?, transition: ContainedViewLayoutTransition) {
if self.tabBarSearchState != tabBarSearchState {
self.tabBarSearchState = tabBarSearchState
self.tabBarSearchStateUpdated?(transition)
}
}
open func updatePossibleControllerDropContent(content: NavigationControllerDropContent?) {
}
open func acceptPossibleControllerDropContent(content: NavigationControllerDropContent) -> Bool {
return false
}
}
func traceIsOpaque(layer: CALayer, rect: CGRect) -> Bool {
if layer.bounds.contains(rect) {
if layer.isHidden {
return false
}
if layer.opacity < 0.01 {
return false
}
if let backgroundColor = layer.backgroundColor {
var alpha: CGFloat = 0.0
UIColor(cgColor: backgroundColor).getRed(nil, green: nil, blue: nil, alpha: &alpha)
if alpha > 0.95 {
return true
}
}
if let sublayers = layer.sublayers {
for sublayer in sublayers {
let sublayerRect = layer.convert(rect, to: sublayer)
if traceIsOpaque(layer: sublayer, rect: sublayerRect) {
return true
}
}
}
return false
} else {
return false
}
}
private func traceViewVisibility(view: UIView, rect: CGRect) -> Bool {
if view.isHidden {
return false
}
if view is UIWindow {
return true
} else if let superview = view.superview, let siblings = superview.layer.sublayers {
if view.window == nil {
return false
}
if let index = siblings.firstIndex(where: { $0 === view.layer }) {
let viewFrame = view.convert(rect, to: superview)
for i in (index + 1) ..< siblings.count {
if siblings[i].frame.contains(viewFrame) {
let siblingSubframe = view.layer.convert(viewFrame, to: siblings[i])
if traceIsOpaque(layer: siblings[i], rect: siblingSubframe) {
return false
}
}
}
return traceViewVisibility(view: superview, rect: viewFrame)
} else {
return false
}
} else {
return false
}
}