|
| 1 | +import SwiftUI |
| 2 | +import Defaults |
| 3 | + |
| 4 | +struct AutoKillSettingsSection: View { |
| 5 | + @Default(.autoKillRules) private var rules |
| 6 | + @State private var editingRule: AutoKillRule? |
| 7 | + @State private var isAddingRule = false |
| 8 | + |
| 9 | + var body: some View { |
| 10 | + SettingsGroup("Auto-Kill Rules", icon: "clock.badge.xmark") { |
| 11 | + VStack(spacing: 0) { |
| 12 | + SettingsRowContainer { |
| 13 | + VStack(alignment: .leading, spacing: 2) { |
| 14 | + Text("Automatically kill processes after a timeout") |
| 15 | + .fontWeight(.medium) |
| 16 | + Text("Rules are checked on each port scan cycle") |
| 17 | + .font(.caption) |
| 18 | + .foregroundStyle(.secondary) |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + SettingsDivider() |
| 23 | + |
| 24 | + if rules.isEmpty { |
| 25 | + SettingsRowContainer { |
| 26 | + HStack { |
| 27 | + Text("No rules configured") |
| 28 | + .foregroundStyle(.secondary) |
| 29 | + Spacer() |
| 30 | + Button("Add Rule") { |
| 31 | + isAddingRule = true |
| 32 | + } |
| 33 | + .controlSize(.small) |
| 34 | + } |
| 35 | + } |
| 36 | + } else { |
| 37 | + ForEach(rules) { rule in |
| 38 | + ruleRow(rule) |
| 39 | + if rule.id != rules.last?.id { |
| 40 | + SettingsDivider() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + SettingsDivider() |
| 45 | + |
| 46 | + SettingsRowContainer { |
| 47 | + HStack { |
| 48 | + Spacer() |
| 49 | + Button("Add Rule") { |
| 50 | + isAddingRule = true |
| 51 | + } |
| 52 | + .controlSize(.small) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + .sheet(isPresented: $isAddingRule) { |
| 59 | + AutoKillRuleEditor(rule: AutoKillRule(name: "New Rule")) { newRule in |
| 60 | + rules.append(newRule) |
| 61 | + } |
| 62 | + } |
| 63 | + .sheet(item: $editingRule) { rule in |
| 64 | + AutoKillRuleEditor(rule: rule) { updated in |
| 65 | + if let index = rules.firstIndex(where: { $0.id == updated.id }) { |
| 66 | + rules[index] = updated |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private func ruleRow(_ rule: AutoKillRule) -> some View { |
| 73 | + SettingsRowContainer { |
| 74 | + HStack { |
| 75 | + VStack(alignment: .leading, spacing: 2) { |
| 76 | + HStack(spacing: 6) { |
| 77 | + Circle() |
| 78 | + .fill(rule.isEnabled ? .green : .secondary) |
| 79 | + .frame(width: 8, height: 8) |
| 80 | + Text(rule.name.isEmpty ? "Unnamed Rule" : rule.name) |
| 81 | + .fontWeight(.medium) |
| 82 | + } |
| 83 | + HStack(spacing: 8) { |
| 84 | + if !rule.processPattern.isEmpty { |
| 85 | + Text("Process: \(rule.processPattern)") |
| 86 | + } |
| 87 | + if rule.port > 0 { |
| 88 | + Text("Port: \(rule.port)") |
| 89 | + } |
| 90 | + Text("Timeout: \(rule.timeoutMinutes) min") |
| 91 | + } |
| 92 | + .font(.caption) |
| 93 | + .foregroundStyle(.secondary) |
| 94 | + } |
| 95 | + |
| 96 | + Spacer() |
| 97 | + |
| 98 | + HStack(spacing: 8) { |
| 99 | + Button { |
| 100 | + editingRule = rule |
| 101 | + } label: { |
| 102 | + Image(systemName: "pencil") |
| 103 | + } |
| 104 | + .buttonStyle(.plain) |
| 105 | + |
| 106 | + Button { |
| 107 | + rules.removeAll { $0.id == rule.id } |
| 108 | + } label: { |
| 109 | + Image(systemName: "trash") |
| 110 | + .foregroundStyle(.red) |
| 111 | + } |
| 112 | + .buttonStyle(.plain) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// MARK: - Rule Editor |
| 120 | + |
| 121 | +struct AutoKillRuleEditor: View { |
| 122 | + @Environment(\.dismiss) private var dismiss |
| 123 | + @State var rule: AutoKillRule |
| 124 | + let onSave: (AutoKillRule) -> Void |
| 125 | + |
| 126 | + var body: some View { |
| 127 | + VStack(spacing: 0) { |
| 128 | + // Header |
| 129 | + Text("Edit Auto-Kill Rule") |
| 130 | + .font(.headline) |
| 131 | + .padding(.top, 20) |
| 132 | + |
| 133 | + Form { |
| 134 | + TextField("Rule Name", text: $rule.name) |
| 135 | + |
| 136 | + Section("Match Criteria") { |
| 137 | + TextField("Process Pattern (e.g. node*, python*)", text: $rule.processPattern) |
| 138 | + TextField("Port (0 = any)", value: $rule.port, format: .number) |
| 139 | + } |
| 140 | + |
| 141 | + Section("Behavior") { |
| 142 | + Stepper("Timeout: \(rule.timeoutMinutes) minutes", value: $rule.timeoutMinutes, in: 1...1440) |
| 143 | + Toggle("Notify before killing", isOn: $rule.notifyBeforeKill) |
| 144 | + Toggle("Enabled", isOn: $rule.isEnabled) |
| 145 | + } |
| 146 | + } |
| 147 | + .formStyle(.grouped) |
| 148 | + .frame(minHeight: 280) |
| 149 | + |
| 150 | + // Actions |
| 151 | + HStack { |
| 152 | + Button("Cancel") { |
| 153 | + dismiss() |
| 154 | + } |
| 155 | + .keyboardShortcut(.cancelAction) |
| 156 | + |
| 157 | + Spacer() |
| 158 | + |
| 159 | + Button("Save") { |
| 160 | + onSave(rule) |
| 161 | + dismiss() |
| 162 | + } |
| 163 | + .keyboardShortcut(.defaultAction) |
| 164 | + .disabled(rule.processPattern.isEmpty && rule.port == 0) |
| 165 | + } |
| 166 | + .padding(20) |
| 167 | + } |
| 168 | + .frame(width: 420) |
| 169 | + } |
| 170 | +} |
0 commit comments