Skip to content

Commit

Permalink
Add AnnotationNodeMark
Browse files Browse the repository at this point in the history
  • Loading branch information
li3zhen1 committed Jan 13, 2025
1 parent 71e6f81 commit 37b7480
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,9 @@ struct MermaidVisualization: View {
let parsedGraph = model.parsedGraph
ForceDirectedGraph {
Series(parsedGraph.0) { node in
NodeMark(id: node)
.symbol(.circle)
.symbolSize(radius: 16)
.foregroundStyle(Color(white: 1.0, opacity: 0.0))
.annotation(node, alignment: .center, offset: .zero) {
getLabel(node)
}
AnnotationNodeMark(id: node, radius: 16) {
getLabel(node)
}
}
Series(parsedGraph.1) { link in
LinkMark(from: link.0, to: link.1)
Expand All @@ -115,11 +111,11 @@ struct MermaidVisualization: View {
}
})
.ignoresSafeArea()
#if !os(visionOS)
#if !os(visionOS)
.inspector(isPresented: .constant(true)) {
MermaidInspector(model: model)
}
#endif
#endif
}
}

Expand Down Expand Up @@ -180,7 +176,7 @@ let mermaidLinkRegex = Regex {
"<-"
""
}

OneOrMore(.whitespace)
singleNodeRegex
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/AnyGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public struct AnyGraphContent<NodeID: Hashable>: GraphContent {
self.storage = storage
}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}

@inlinable
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
storage._attachToGraphRenderingContext(&context)
Expand Down
4 changes: 1 addition & 3 deletions Sources/Grape/Contents/CompositedGraphContent.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
public protocol GraphComponent<NodeID>: GraphContent {
public protocol GraphComponent<NodeID>: GraphContent where Body: GraphContent<NodeID> {

associatedtype Body: GraphContent<NodeID>

@inlinable
@GraphContentBuilder<Body.NodeID>
var body: Body { get }
Expand Down
13 changes: 13 additions & 0 deletions Sources/Grape/Contents/GraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@ import SwiftUI

public protocol GraphContent<NodeID> {
associatedtype NodeID: Hashable
associatedtype Body: GraphContent<NodeID>

@inlinable
func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>)

@inlinable
var body: Body { get }
}


extension GraphContent {

@inlinable
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
body._attachToGraphRenderingContext(&context)
}
}
4 changes: 4 additions & 0 deletions Sources/Grape/Contents/LinkMark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import SwiftUI

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

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}
// public enum LabelDisplayStrategy {
// case auto
// case specified(Bool)
Expand Down
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/ModifiedGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ extension ModifiedGraphContent: GraphContent {
content._attachToGraphRenderingContext(&context)
modifier._exit(&context)
}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}
}
36 changes: 32 additions & 4 deletions Sources/Grape/Contents/NodeMark.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftUI
import simd
import Charts

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

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

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}

@inlinable
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
context.nodeOperations.append(
Expand All @@ -28,9 +34,31 @@ public struct NodeMark<NodeID: Hashable>: GraphContent, Identifiable, Equatable
}
}

extension NodeMark: CustomDebugStringConvertible {
public struct AnnotationNodeMark<NodeID: Hashable>: GraphContent, Identifiable {

public var id: NodeID

@usableFromInline
var radius: CGFloat

@usableFromInline
var annotation: AnyView

@inlinable
public init(id: NodeID, radius: CGFloat, @ViewBuilder annnotation: () -> some View) {
self.id = id
self.radius = radius
self.annotation = AnyView(annnotation())
}

@inlinable
public var debugDescription: String {
return "Node(id: \(id))"
public var body: some GraphContent<NodeID> {
NodeMark(id: id)
.symbolSize(radius: radius)
.foregroundStyle(.clear)
.annotation("\(id)", alignment: .center, offset: .zero) {
annotation
}
}
}

}
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/Series.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ where Data: RandomAccessCollection, Content: GraphContent<NodeID>, NodeID: Hasha
self.data = data
self.content = graphContent
}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}
}

extension Series: GraphContent {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/_ArrayGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ where C: GraphContent {
self.storage = storage
}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}

@inlinable
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
for content in storage {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/_ConditionalGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ where C1: GraphContent, C2: GraphContent, C1.NodeID == C2.NodeID {
self.storage = storage
}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}

@inlinable
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
switch storage {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/_EmptyGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ struct _EmptyGraphContent<NodeID: Hashable>: GraphContent {
@inlinable
public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {

}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}
}
132 changes: 0 additions & 132 deletions Sources/Grape/Contents/_ForEach+GraphContent.swift

This file was deleted.

13 changes: 13 additions & 0 deletions Sources/Grape/Contents/_IdentifiableNever.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public enum _IdentifiableNever<ID: Hashable> { }

extension _IdentifiableNever: GraphContent {
public typealias NodeID = ID

public var body: Self {
fatalError()
}

public func _attachToGraphRenderingContext(_ context: inout _GraphRenderingContext<NodeID>) {
fatalError()
}
}
5 changes: 5 additions & 0 deletions Sources/Grape/Contents/_OptionalGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ where C: GraphContent {
content._attachToGraphRenderingContext(&context)
}
}

@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}
}
8 changes: 7 additions & 1 deletion Sources/Grape/Contents/_PairedGraphContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ where C1: GraphContent, C2: GraphContent, NodeID: Hashable, C1.NodeID == NodeID,
first._attachToGraphRenderingContext(&context)
second._attachToGraphRenderingContext(&context)
}
}


@inlinable
public var body: _IdentifiableNever<NodeID> {
fatalError()
}
}
Loading

0 comments on commit 37b7480

Please sign in to comment.