-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support user-supplied literal headers
- Loading branch information
1 parent
5d97953
commit dc8972e
Showing
17 changed files
with
472 additions
and
64 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Coder Desktop/Coder Desktop/Views/Settings/GeneralTab.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import LaunchAtLogin | ||
import SwiftUI | ||
|
||
struct GeneralTab: View { | ||
var body: some View { | ||
Form { | ||
Section { | ||
LaunchAtLogin.Toggle("Launch at Login") | ||
} | ||
}.formStyle(.grouped) | ||
} | ||
} | ||
|
||
#Preview { | ||
GeneralTab() | ||
} |
46 changes: 46 additions & 0 deletions
46
Coder Desktop/Coder Desktop/Views/Settings/LiteralHeaderModal.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import SwiftData | ||
import SwiftUI | ||
|
||
struct LiteralHeaderModal: View { | ||
var existingHeader: LiteralHeader? | ||
|
||
@EnvironmentObject var settings: Settings | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
@State private var header: String = "" | ||
@State private var value: String = "" | ||
|
||
var body: some View { | ||
VStack(spacing: 0) { | ||
Form { | ||
Section { | ||
TextField("Header", text: $header) | ||
TextField("Value", text: $value) | ||
} | ||
}.formStyle(.grouped).scrollDisabled(true).padding(.horizontal) | ||
Divider() | ||
HStack { | ||
Spacer() | ||
Button("Cancel", action: { dismiss() }).keyboardShortcut(.cancelAction) | ||
Button(existingHeader == nil ? "Add" : "Save", action: submit) | ||
.keyboardShortcut(.defaultAction) | ||
}.padding(20) | ||
}.onAppear { | ||
if let existingHeader { | ||
self.header = existingHeader.header | ||
self.value = existingHeader.value | ||
} | ||
} | ||
} | ||
|
||
func submit() { | ||
defer { dismiss() } | ||
if let existingHeader { | ||
settings.literalHeaders.removeAll { $0 == existingHeader } | ||
} | ||
let newHeader = LiteralHeader(header: header, value: value) | ||
if !settings.literalHeaders.contains(newHeader) { | ||
settings.literalHeaders.append(newHeader) | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
Coder Desktop/Coder Desktop/Views/Settings/LiteralHeadersSection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import SwiftData | ||
import SwiftUI | ||
|
||
struct LiteralHeadersSection<VPN: VPNService>: View { | ||
@EnvironmentObject var vpn: VPN | ||
@EnvironmentObject var settings: Settings | ||
|
||
@State private var selectedHeader: LiteralHeader.ID? | ||
@State private var editingHeader: LiteralHeader? | ||
@State private var addingNewHeader = false | ||
|
||
let inspection = Inspection<Self>() | ||
|
||
var body: some View { | ||
Section { | ||
Toggle(isOn: settings.$useLiteralHeaders) { | ||
Text("HTTP Headers") | ||
Text("When enabled, these headers will be included on all outgoing HTTP requests.") | ||
if vpn.state != .disabled { Text("Cannot be modified while Coder VPN is enabled.") } | ||
} | ||
.controlSize(.large) | ||
|
||
Table(settings.literalHeaders, selection: $selectedHeader) { | ||
TableColumn("Header", value: \.header) | ||
TableColumn("Value", value: \.value) | ||
}.opacity(settings.useLiteralHeaders ? 1 : 0.5) | ||
.frame(minWidth: 400, minHeight: 200) | ||
.padding(.bottom, 25) | ||
.overlay(alignment: .bottom) { | ||
VStack(alignment: .leading, spacing: 0) { | ||
Divider() | ||
HStack(spacing: 0) { | ||
Button { | ||
addingNewHeader = true | ||
} label: { | ||
Image(systemName: "plus") | ||
.frame(width: 24, height: 24) | ||
} | ||
Divider() | ||
Button { | ||
settings.literalHeaders.removeAll { $0.id == selectedHeader } | ||
selectedHeader = nil | ||
} label: { | ||
Image(systemName: "minus") | ||
.frame(width: 24, height: 24) | ||
}.disabled(selectedHeader == nil) | ||
} | ||
.buttonStyle(.borderless) | ||
} | ||
.background(.primary.opacity(0.04)) | ||
.fixedSize(horizontal: false, vertical: true) | ||
} | ||
.background(.primary.opacity(0.04)) | ||
.contextMenu(forSelectionType: LiteralHeader.ID.self, menu: { _ in }, | ||
primaryAction: { selectedHeaders in | ||
if let firstHeader = selectedHeaders.first { | ||
editingHeader = settings.literalHeaders.first(where: { $0.id == firstHeader }) | ||
} | ||
}) | ||
.disabled(!settings.useLiteralHeaders) | ||
} | ||
.sheet(isPresented: $addingNewHeader) { | ||
LiteralHeaderModal() | ||
} | ||
.sheet(item: $editingHeader) { header in | ||
LiteralHeaderModal(existingHeader: header) | ||
}.onTapGesture { | ||
selectedHeader = nil | ||
}.disabled(vpn.state != .disabled) | ||
.onReceive(inspection.notice) { self.inspection.visit(self, $0) } // ViewInspector | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Coder Desktop/Coder Desktop/Views/Settings/NetworkTab.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import SwiftData | ||
import SwiftUI | ||
|
||
struct NetworkTab<VPN: VPNService>: View { | ||
var body: some View { | ||
Form { | ||
LiteralHeadersSection<VPN>() | ||
} | ||
.formStyle(.grouped) | ||
} | ||
} | ||
|
||
#Preview { | ||
NetworkTab<PreviewVPN>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import SwiftUI | ||
|
||
struct SettingsView<VPN: VPNService>: View { | ||
@AppStorage("SettingsSelectedIndex") private var selection: SettingsTab = .general | ||
|
||
var body: some View { | ||
TabView(selection: $selection) { | ||
GeneralTab() | ||
.tabItem { | ||
Label("General", systemImage: "gearshape") | ||
}.tag(SettingsTab.general) | ||
NetworkTab<VPN>() | ||
.tabItem { | ||
Label("Network", systemImage: "dot.radiowaves.left.and.right") | ||
}.tag(SettingsTab.network) | ||
}.frame(width: 600) | ||
.frame(maxHeight: 500) | ||
.scrollContentBackground(.hidden) | ||
.fixedSize() | ||
} | ||
} | ||
|
||
enum SettingsTab: Int { | ||
case general | ||
case network | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Combine | ||
import SwiftUI | ||
|
||
// This is required for inspecting stateful views | ||
final class Inspection<V> { | ||
|
Oops, something went wrong.