|
| 1 | +import SwiftData |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +struct LiteralHeadersSection<VPN: VPNService>: View { |
| 5 | + @EnvironmentObject var vpn: VPN |
| 6 | + @EnvironmentObject var settings: Settings |
| 7 | + |
| 8 | + @State private var selectedHeader: LiteralHeader.ID? |
| 9 | + @State private var editingHeader: LiteralHeader? |
| 10 | + @State private var addingNewHeader = false |
| 11 | + |
| 12 | + let inspection = Inspection<Self>() |
| 13 | + |
| 14 | + var body: some View { |
| 15 | + Section { |
| 16 | + Toggle(isOn: settings.$useLiteralHeaders) { |
| 17 | + Text("HTTP Headers") |
| 18 | + Text("When enabled, these headers will be included on all outgoing HTTP requests.") |
| 19 | + if vpn.state != .disabled { Text("Cannot be modified while Coder VPN is enabled.") } |
| 20 | + } |
| 21 | + .controlSize(.large) |
| 22 | + |
| 23 | + Table(settings.literalHeaders, selection: $selectedHeader) { |
| 24 | + TableColumn("Header", value: \.header) |
| 25 | + TableColumn("Value", value: \.value) |
| 26 | + }.opacity(settings.useLiteralHeaders ? 1 : 0.5) |
| 27 | + .frame(minWidth: 400, minHeight: 200) |
| 28 | + .padding(.bottom, 25) |
| 29 | + .overlay(alignment: .bottom) { |
| 30 | + VStack(alignment: .leading, spacing: 0) { |
| 31 | + Divider() |
| 32 | + HStack(spacing: 0) { |
| 33 | + Button { |
| 34 | + addingNewHeader = true |
| 35 | + } label: { |
| 36 | + Image(systemName: "plus") |
| 37 | + .frame(width: 24, height: 24) |
| 38 | + } |
| 39 | + Divider() |
| 40 | + Button { |
| 41 | + settings.literalHeaders.removeAll { $0.id == selectedHeader } |
| 42 | + selectedHeader = nil |
| 43 | + } label: { |
| 44 | + Image(systemName: "minus") |
| 45 | + .frame(width: 24, height: 24) |
| 46 | + }.disabled(selectedHeader == nil) |
| 47 | + } |
| 48 | + .buttonStyle(.borderless) |
| 49 | + } |
| 50 | + .background(.primary.opacity(0.04)) |
| 51 | + .fixedSize(horizontal: false, vertical: true) |
| 52 | + } |
| 53 | + .background(.primary.opacity(0.04)) |
| 54 | + .contextMenu(forSelectionType: LiteralHeader.ID.self, menu: { _ in }, |
| 55 | + primaryAction: { selectedHeaders in |
| 56 | + if let firstHeader = selectedHeaders.first { |
| 57 | + editingHeader = settings.literalHeaders.first(where: { $0.id == firstHeader }) |
| 58 | + } |
| 59 | + }) |
| 60 | + .disabled(!settings.useLiteralHeaders) |
| 61 | + } |
| 62 | + .sheet(isPresented: $addingNewHeader) { |
| 63 | + LiteralHeaderModal() |
| 64 | + } |
| 65 | + .sheet(item: $editingHeader) { header in |
| 66 | + LiteralHeaderModal(existingHeader: header) |
| 67 | + }.onTapGesture { |
| 68 | + selectedHeader = nil |
| 69 | + }.disabled(vpn.state != .disabled) |
| 70 | + .onReceive(inspection.notice) { self.inspection.visit(self, $0) } // ViewInspector |
| 71 | + } |
| 72 | +} |
0 commit comments