Repository navigation
Expand file tree
/
Copy pathOutlinerIndentGuides.swift
More file actions
95 lines (89 loc) · 3.55 KB
/
Copy pathOutlinerIndentGuides.swift
File metadata and controls
95 lines (89 loc) · 3.55 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
import SwiftUI
/// One path per row keeps ancestor guides continuous across virtualized rows.
enum OutlinerGuideGeometry {
static func path(depth: Int, expanded: Bool, size: CGSize) -> CGPath {
let path = CGMutablePath()
for level in 0..<max(0, depth) {
let x = OutlinerLayoutMetrics.guideCenterX(level: level)
path.move(to: CGPoint(x: x, y: 0))
path.addLine(to: CGPoint(x: x, y: size.height))
}
let guideStart = OutlinerLayoutMetrics.rowVerticalPadding + OutlinerLayoutMetrics.bulletHitSize / 2
+ OutlinerLayoutMetrics.bulletInkRadius
if expanded, size.height > guideStart {
let x = OutlinerLayoutMetrics.bulletCenterX(depth: max(0, depth))
path.move(to: CGPoint(x: x, y: guideStart))
path.addLine(to: CGPoint(x: x, y: size.height))
}
return path
}
}
#if os(iOS)
import UIKit
struct OutlinerIndentGuides: UIViewRepresentable {
let identifier: String
let depth: Int
let expanded: Bool
init(row: OutlinerRowPayload) {
identifier = row.uuid
depth = max(0, row.depth)
expanded = row.hasChildren && !row.collapsed
}
init(identifier: String, guideCount: Int) {
self.identifier = identifier
depth = max(0, guideCount)
expanded = false
}
func makeUIView(context: Context) -> OutlinerIndentGuideView { OutlinerIndentGuideView() }
func updateUIView(_ view: OutlinerIndentGuideView, context: Context) { view.update(identifier: identifier, depth: depth, expanded: expanded) }
}
@MainActor final class OutlinerIndentGuideView: UIView {
override class var layerClass: AnyClass { CAShapeLayer.self }
private var depth = 0
private var expanded = false
override init(frame: CGRect) {
super.init(frame: frame)
isOpaque = false
isUserInteractionEnabled = false
isAccessibilityElement = false
(layer as? CAShapeLayer)?.fillColor = nil
(layer as? CAShapeLayer)?.lineWidth = 1
updateColor()
registerForTraitChanges([UITraitUserInterfaceStyle.self, UITraitAccessibilityContrast.self]) {
(view: OutlinerIndentGuideView, _: UITraitCollection) in view.updateColor()
}
}
required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
func update(_ row: OutlinerRowPayload) {
update(identifier: row.uuid, depth: max(0, row.depth), expanded: row.hasChildren && !row.collapsed)
}
func update(identifier: String, depth: Int, expanded: Bool) {
accessibilityIdentifier = "outliner.indent-guides." + identifier
self.depth = depth
self.expanded = expanded
isHidden = depth == 0 && !expanded
setNeedsLayout()
}
override func layoutSubviews() {
super.layoutSubviews()
(layer as? CAShapeLayer)?.path = OutlinerGuideGeometry.path(depth: depth, expanded: expanded, size: bounds.size)
}
private func updateColor() {
(layer as? CAShapeLayer)?.strokeColor = UIColor.secondaryLabel
.resolvedColor(with: traitCollection).withAlphaComponent(0.18).cgColor
}
}
#else
struct OutlinerIndentGuides: View {
let row: OutlinerRowPayload
var body: some View {
Canvas { context, size in
context.stroke(Path(OutlinerGuideGeometry.path(depth: row.depth,
expanded: row.hasChildren && !row.collapsed, size: size)),
with: .color(.secondary.opacity(0.18)), lineWidth: 1)
}
.allowsHitTesting(false)
.accessibilityHidden(true)
}
}
#endif