Repository navigation
Expand file tree
/
Copy pathLGCaptureRailHost.swift
More file actions
96 lines (87 loc) · 4.45 KB
/
Copy pathLGCaptureRailHost.swift
File metadata and controls
96 lines (87 loc) · 4.45 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
import SwiftUI
#if os(iOS)
/// A shared navigation overlay, anchored to the controller's full bounds rather
/// than the split-view detail's already-inset SwiftUI content area.
struct LGCaptureRailHost: UIViewControllerRepresentable {
let content: AnyView
let edge: HorizontalEdge
let visible: Bool
let dark: Bool
func makeUIViewController(context: Context) -> Observer { Observer(content: content) }
func updateUIViewController(_ controller: Observer, context: Context) {
controller.enabled = visible
controller.edge = edge
controller.rail.rootView = visible ? content : AnyView(EmptyView())
controller.rail.view.isHidden = !visible
controller.rail.view.overrideUserInterfaceStyle = dark ? .dark : .light
controller.attach()
LogseqCrashReporting.uiState(captureRailVisible: visible && controller.rail.parent != nil)
}
static func dismantleUIViewController(_ controller: Observer, coordinator: ()) { controller.detach() }
final class Observer: UIViewController {
let rail: UIHostingController<AnyView>
var enabled = false
var edge: HorizontalEdge = .trailing
private var edgeConstraints: [HorizontalEdge: NSLayoutConstraint] = [:]
private var bottomConstraint: NSLayoutConstraint?
private weak var attachedNavigation: UINavigationController?
init(content: AnyView) {
rail = UIHostingController(rootView: content)
super.init(nibName: nil, bundle: nil)
rail.sizingOptions = .intrinsicContentSize
// The native viewport anchors already account for placement. A
// nested host must not inherit the window's side-toolbar insets.
rail.safeAreaRegions = []
rail.view.backgroundColor = .clear
rail.view.accessibilityIdentifier = "navigation.capture-rail"
}
required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
override func loadView() { view = UIView() }
override func didMove(toParent parent: UIViewController?) { super.didMove(toParent: parent); attach() }
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated); attach() }
override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews(); attach() }
func attach() {
guard enabled, let navigationController, let owner = view.window?.rootViewController,
navigationController.view.isDescendant(of: owner.view) else { return }
if attachedNavigation === navigationController, rail.parent === owner {
alignToToolbar()
return
}
detach()
owner.addChild(rail)
owner.view.addSubview(rail.view)
rail.view.translatesAutoresizingMaskIntoConstraints = false
// Own the rail outside UIKit's page stack, which replaces its child
// controllers on push/pop. Align it to that stack's full viewport.
rail.view.layer.zPosition = 1
edgeConstraints = [
.leading: rail.view.leadingAnchor.constraint(equalTo: navigationController.view.leadingAnchor, constant: 24),
.trailing: rail.view.trailingAnchor.constraint(equalTo: navigationController.view.trailingAnchor, constant: -24),
]
bottomConstraint = rail.view.bottomAnchor.constraint(equalTo: navigationController.view.bottomAnchor, constant: -24)
bottomConstraint?.isActive = true
alignToToolbar()
rail.didMove(toParent: owner)
attachedNavigation = navigationController
LogseqCrashReporting.uiState(captureRailVisible: !rail.view.isHidden)
}
private func alignToToolbar() {
guard let constraint = edgeConstraints[edge], !constraint.isActive else { return }
NSLayoutConstraint.deactivate(Array(edgeConstraints.values))
constraint.isActive = true
}
func detach() {
guard rail.parent != nil else { return }
LogseqCrashReporting.uiState(captureRailVisible: false)
rail.willMove(toParent: nil)
NSLayoutConstraint.deactivate(Array(edgeConstraints.values))
edgeConstraints = [:]
bottomConstraint?.isActive = false
bottomConstraint = nil
rail.view.removeFromSuperview()
rail.removeFromParent()
attachedNavigation = nil
}
}
}
#endif