Skip to content

Commit 37b7480

Browse files
committed
Add AnnotationNodeMark
1 parent 71e6f81 commit 37b7480

16 files changed

+132
-171
lines changed

Examples/ForceDirectedGraphExample/ForceDirectedGraphExample/MermaidVisualization.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,9 @@ struct MermaidVisualization: View {
8484
let parsedGraph = model.parsedGraph
8585
ForceDirectedGraph {
8686
Series(parsedGraph.0) { node in
87-
NodeMark(id: node)
88-
.symbol(.circle)
89-
.symbolSize(radius: 16)
90-
.foregroundStyle(Color(white: 1.0, opacity: 0.0))
91-
.annotation(node, alignment: .center, offset: .zero) {
92-
getLabel(node)
93-
}
87+
AnnotationNodeMark(id: node, radius: 16) {
88+
getLabel(node)
89+
}
9490
}
9591
Series(parsedGraph.1) { link in
9692
LinkMark(from: link.0, to: link.1)
@@ -115,11 +111,11 @@ struct MermaidVisualization: View {
115111
}
116112
})
117113
.ignoresSafeArea()
118-
#if !os(visionOS)
114+
#if !os(visionOS)
119115
.inspector(isPresented: .constant(true)) {
120116
MermaidInspector(model: model)
121117
}
122-
#endif
118+
#endif
123119
}
124120
}
125121

@@ -180,7 +176,7 @@ let mermaidLinkRegex = Regex {
180176
"<-"
181177
""
182178
}
183-
179+
184180
OneOrMore(.whitespace)
185181
singleNodeRegex
186182
}

Sources/Grape/Contents/AnyGraphContent.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public struct AnyGraphContent<NodeID: Hashable>: GraphContent {
99
self.storage = storage
1010
}
1111

12+
@inlinable
13+
public var body: _IdentifiableNever<NodeID> {
14+
fatalError()
15+
}
16+
1217
@inlinable
1318
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
1419
storage._attachToGraphRenderingContext(&context)

Sources/Grape/Contents/CompositedGraphContent.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
public protocol GraphComponent<NodeID>: GraphContent {
1+
public protocol GraphComponent<NodeID>: GraphContent where Body: GraphContent<NodeID> {
22

3-
associatedtype Body: GraphContent<NodeID>
4-
53
@inlinable
64
@GraphContentBuilder<Body.NodeID>
75
var body: Body { get }

Sources/Grape/Contents/GraphContent.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ import SwiftUI
33

44
public protocol GraphContent<NodeID> {
55
associatedtype NodeID: Hashable
6+
associatedtype Body: GraphContent<NodeID>
67

78
@inlinable
89
func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>)
10+
11+
@inlinable
12+
var body: Body { get }
913
}
14+
15+
16+
extension GraphContent {
17+
18+
@inlinable
19+
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
20+
body._attachToGraphRenderingContext(&context)
21+
}
22+
}

Sources/Grape/Contents/LinkMark.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import SwiftUI
33

44
public struct LinkMark<NodeID: Hashable>: GraphContent & Identifiable {
55

6+
@inlinable
7+
public var body: _IdentifiableNever<NodeID> {
8+
fatalError()
9+
}
610
// public enum LabelDisplayStrategy {
711
// case auto
812
// case specified(Bool)

Sources/Grape/Contents/ModifiedGraphContent.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ extension ModifiedGraphContent: GraphContent {
4949
content._attachToGraphRenderingContext(&context)
5050
modifier._exit(&context)
5151
}
52+
53+
@inlinable
54+
public var body: _IdentifiableNever<NodeID> {
55+
fatalError()
56+
}
5257
}

Sources/Grape/Contents/NodeMark.swift

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import SwiftUI
22
import simd
3+
import Charts
34

45
public struct NodeMark<NodeID: Hashable>: GraphContent, Identifiable, Equatable {
56

@@ -12,6 +13,11 @@ public struct NodeMark<NodeID: Hashable>: GraphContent, Identifiable, Equatable
1213
self.id = id
1314
}
1415

16+
@inlinable
17+
public var body: _IdentifiableNever<NodeID> {
18+
fatalError()
19+
}
20+
1521
@inlinable
1622
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
1723
context.nodeOperations.append(
@@ -28,9 +34,31 @@ public struct NodeMark<NodeID: Hashable>: GraphContent, Identifiable, Equatable
2834
}
2935
}
3036

31-
extension NodeMark: CustomDebugStringConvertible {
37+
public struct AnnotationNodeMark<NodeID: Hashable>: GraphContent, Identifiable {
38+
39+
public var id: NodeID
40+
41+
@usableFromInline
42+
var radius: CGFloat
43+
44+
@usableFromInline
45+
var annotation: AnyView
46+
47+
@inlinable
48+
public init(id: NodeID, radius: CGFloat, @ViewBuilder annnotation: () -> some View) {
49+
self.id = id
50+
self.radius = radius
51+
self.annotation = AnyView(annnotation())
52+
}
53+
3254
@inlinable
33-
public var debugDescription: String {
34-
return "Node(id: \(id))"
55+
public var body: some GraphContent<NodeID> {
56+
NodeMark(id: id)
57+
.symbolSize(radius: radius)
58+
.foregroundStyle(.clear)
59+
.annotation("\(id)", alignment: .center, offset: .zero) {
60+
annotation
61+
}
3562
}
36-
}
63+
64+
}

Sources/Grape/Contents/Series.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ where Data: RandomAccessCollection, Content: GraphContent<NodeID>, NodeID: Hasha
1515
self.data = data
1616
self.content = graphContent
1717
}
18+
19+
@inlinable
20+
public var body: _IdentifiableNever<NodeID> {
21+
fatalError()
22+
}
1823
}
1924

2025
extension Series: GraphContent {

Sources/Grape/Contents/_ArrayGraphContent.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ where C: GraphContent {
1313
self.storage = storage
1414
}
1515

16+
@inlinable
17+
public var body: _IdentifiableNever<NodeID> {
18+
fatalError()
19+
}
20+
1621
@inlinable
1722
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
1823
for content in storage {

Sources/Grape/Contents/_ConditionalGraphContent.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ where C1: GraphContent, C2: GraphContent, C1.NodeID == C2.NodeID {
1818
self.storage = storage
1919
}
2020

21+
@inlinable
22+
public var body: _IdentifiableNever<NodeID> {
23+
fatalError()
24+
}
25+
2126
@inlinable
2227
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
2328
switch storage {

Sources/Grape/Contents/_EmptyGraphContent.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ struct _EmptyGraphContent<NodeID: Hashable>: GraphContent {
77
@inlinable
88
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
99

10+
}
11+
12+
@inlinable
13+
public var body: _IdentifiableNever<NodeID> {
14+
fatalError()
1015
}
1116
}

Sources/Grape/Contents/_ForEach+GraphContent.swift

Lines changed: 0 additions & 132 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public enum _IdentifiableNever<ID: Hashable> { }
2+
3+
extension _IdentifiableNever: GraphContent {
4+
public typealias NodeID = ID
5+
6+
public var body: Self {
7+
fatalError()
8+
}
9+
10+
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
11+
fatalError()
12+
}
13+
}

Sources/Grape/Contents/_OptionalGraphContent.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ where C: GraphContent {
2323
content._attachToGraphRenderingContext(&context)
2424
}
2525
}
26+
27+
@inlinable
28+
public var body: _IdentifiableNever<NodeID> {
29+
fatalError()
30+
}
2631
}

Sources/Grape/Contents/_PairedGraphContent.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ where C1: GraphContent, C2: GraphContent, NodeID: Hashable, C1.NodeID == NodeID,
1919
first._attachToGraphRenderingContext(&context)
2020
second._attachToGraphRenderingContext(&context)
2121
}
22-
}
22+
23+
24+
@inlinable
25+
public var body: _IdentifiableNever<NodeID> {
26+
fatalError()
27+
}
28+
}

0 commit comments

Comments
 (0)