66//
77
88import AGDebugKit
9+ import AttributeGraph
910import SwiftUI
1011
1112@available ( macOS 13 , * )
1213struct AGDebugItem : Equatable , Identifiable {
1314 var url : URL
14-
15- init ( name: String ) {
16- url = URL ( filePath: NSTemporaryDirectory ( ) . appending ( " \( name) .json " ) )
15+ var format : Format
16+
17+ enum Format : String , CaseIterable , Identifiable {
18+ var id : String { rawValue }
19+
20+ case json, dot
1721 }
18-
22+
23+ init ( name: String , format: Format ) {
24+ url = URL ( filePath: NSTemporaryDirectory ( ) . appending ( " \( name) . \( format. rawValue) " ) )
25+ self . format = format
26+ }
27+
1928 var id : String { url. absoluteString }
2029}
2130
@@ -24,12 +33,26 @@ struct AGDebugModifier: ViewModifier {
2433 @State private var showInspector = false
2534 @State private var items : [ AGDebugItem ] = [ ]
2635
36+ fileprivate static var sharedGraphbitPattern : Int = 0
37+ @State private var format : AGDebugItem . Format = . dot
38+
2739 func body( content: Content ) -> some View {
2840 content
2941 . toolbar {
42+ Picker ( " Format " , selection: $format) {
43+ ForEach ( AGDebugItem . Format. allCases) {
44+ Text ( $0. rawValue) . tag ( $0)
45+ }
46+ } . pickerStyle ( . segmented)
3047 Button {
31- let item = AGDebugItem ( name: Date . now. ISO8601Format ( ) )
32- Graph . archiveGraph ( name: item. url. lastPathComponent)
48+ let item = AGDebugItem ( name: Date . now. ISO8601Format ( ) , format: format)
49+ let name = item. url. lastPathComponent
50+ switch format {
51+ case . json:
52+ Graph . archiveGraph ( name: name)
53+ case . dot:
54+ Graph . _graphExport ( AGDebugModifier . sharedGraphbitPattern, name: name)
55+ }
3356 items. append ( item)
3457 } label: {
3558 Image ( systemName: " doc.badge.plus " )
@@ -43,6 +66,7 @@ struct AGDebugModifier: ViewModifier {
4366 . inspector ( isPresented: $showInspector) {
4467 inspectorView
4568 }
69+ . overlay { _GraphFetcher ( ) }
4670 }
4771
4872 private var inspectorView : some View {
@@ -89,16 +113,16 @@ struct AGDebugModifier: ViewModifier {
89113 private func openAction( _ url: URL ) {
90114 _ = NSWorkspace . shared. open ( url)
91115 }
92-
116+
93117 // MARK: - Move
94-
118+
95119 @State private var moving = false
96120 @State private var moveURL : URL ?
97121 private func moveAction( _ url: URL ) {
98122 moveURL = url
99123 moving = true
100124 }
101-
125+
102126 // MARK: - Delete
103127
104128 private func deleteAction( _ url: URL ) throws {
@@ -113,3 +137,28 @@ extension View {
113137 modifier ( AGDebugModifier ( ) )
114138 }
115139}
140+
141+ struct _GraphFetcher : View {
142+ var body : Never { fatalError ( " Unimplemented " ) }
143+
144+ static func _makeView( view: _GraphValue < Self > , inputs: _ViewInputs ) -> _ViewOutputs {
145+ if let current = AGSubgraph . current {
146+ let graph = current. graph
147+ if #available( macOS 14 , * ) {
148+ AGDebugModifier . sharedGraphbitPattern = unsafeBitCast ( graph, to: Int . self)
149+ }
150+ }
151+
152+ return withUnsafePointer ( to: view) { pointer in
153+ let view = UnsafeRawPointer ( pointer) . assumingMemoryBound ( to: _GraphValue< EmptyView> . self )
154+ return EmptyView . _makeView ( view: view. pointee, inputs: inputs)
155+ }
156+ }
157+
158+ static func _makeViewList( view: _GraphValue < Self > , inputs: _ViewListInputs ) -> _ViewListOutputs {
159+ withUnsafePointer ( to: view) { pointer in
160+ let view = UnsafeRawPointer ( pointer) . assumingMemoryBound ( to: _GraphValue< EmptyView> . self )
161+ return EmptyView . _makeViewList ( view: view. pointee, inputs: inputs)
162+ }
163+ }
164+ }
0 commit comments