public struct GenerateView: View { @Environment(\.modelContext) private var modelContext @Environment(\.openURL) private var openURL @Bindable var job: SurfaceJob @Query private var machines: [Machine] @State private var gtextX = "" @State private var gtextY = "" @State private var gtextB = "" @State private var copiedMessage = "Copied to clipboard" @State private var toolpath: ToolPath? @State private var showShareSheet = false func shareAction(axis: String, gtext: String) { let fileName = "\(job.name)-\(axis).gcode" let fileManager = FileManager.default let docURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] let storeURL = docURL.appending(path: "GCodeGen/") let fileURL = storeURL.appendingPathComponent(fileName) symlog.message("shareAction write:", color: .yellow) do { try gtext.write(to: fileURL, atomically: true, encoding: .utf8) symlog.message("shareAction write: \(fileURL)", color: .yellow) let dsURL = "dropshare5:///action/upload?file=\(fileURL.path)" if let url = URL(string: dsURL) { openURL(url) } } catch { print("Failed to write to file") } } // struct ShareButton: View { // @Environment(\.openURL) private var openURL // let job: SurfaceJob // let title: String // var plane: String // let gtext: String // @Binding var showShareSheet: Bool // Declare as a binding // @State var showCopiedMessage = false // // var body: some View { // VStack { // Button(action: { }) // { // Text("\(job.name)-\(plane).gcode") // .font(.title) // .padding() // Image(systemName: "square.and.arrow.down") // This is the system image for save icon // .resizable() // .frame(width: 40, height: 40) // } // Button(action: { // showShareSheet = true // let pasteboard = UIPasteboard.general // let textToCopy = gtext // pasteboard.string = textToCopy // withAnimation { // showCopiedMessage = true // } // DispatchQueue.main.asyncAfter(deadline: .now() + 2) { // withAnimation { // showCopiedMessage = false // } // } // }) { // Image(systemName: "square.and.arrow.up") // This is the system image for share icon // .resizable() // .frame(width: 40, height: 40) // } // .sheet(isPresented: $showShareSheet) { // ShareSheet(activityItems: ["\(job.name)-\(plane).gcode", gtext]) // } // .buttonStyle(MyButtonStyle()) // } // .enableInjection() // } // // #if DEBUG // @ObserveInjection var forceRedraw // #endif // } // TODO: Add a button to save the gcode to a file // TODO: Add a button to send the gcode to the machine // TODO: Add a button to edit the gcode // TODO: Add gcode visulizer public var body: some View { Form { HStack { VStack { HStack { Button(action: { shareAction(axis: "X", gtext: gtextX) }) { Text("\(job.name)-X.gcode") .font(.title) .padding() Image(systemName: "square.and.arrow.down") // This is the system image for save icon .resizable() .frame(width: 40, height: 40) } } Text("X Plane") .font(.title) .padding() ScrollView(.vertical, showsIndicators: true) { Spacer() Text("\(gtextX)") .font(.system(.body, design: .monospaced)) .padding() .onAppear { gtextX = job.gcodeX } Spacer() } } VStack { Button(action: { shareAction(axis: "Y", gtext: gtextY) }) { Text("\(job.name)-Y.gcode") .font(.title) .padding() Image(systemName: "square.and.arrow.down") // This is the system image for save icon .resizable() .frame(width: 40, height: 40) } Text("Y Plane") .font(.title) .padding() ScrollView(.vertical, showsIndicators: true) { Spacer() Text("\(gtextY)") .font(.system(.body, design: .monospaced)) .padding() .onAppear { gtextY = job.gcodeY } Spacer() } } VStack { Button(action: { shareAction(axis: "B", gtext: gtextB) }) { Text("\(job.name)-B.gcode") .font(.title) .padding() Image(systemName: "square.and.arrow.down") // This is the system image for save icon .resizable() .frame(width: 40, height: 40) } Text("Boundary") .font(.title) .padding() ScrollView(.vertical, showsIndicators: true) { // Spacer() Text("\(gtextB)") .font(.system(.body, design: .monospaced)) .padding() .onAppear { gtextB = job.gcodeB } // Spacer() } } } .navigationTitle("GCode") } }