mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-19 04:34:59 +00:00
448 lines
14 KiB
Swift
448 lines
14 KiB
Swift
import SwiftUI
|
||
|
||
/// 设置分区:常规、后台代理客户端、系统代理与插件管理,采用
|
||
/// 分组卡,行式呈现(主文案 + 次说明 + 行尾控件)。设置项由各自编辑器独立保存;
|
||
/// 登录项与静默启动开关绑定独立控制器。
|
||
struct SettingsView: View {
|
||
let plugins: PluginManagementModel
|
||
@ObservedObject var workflow: SettingsWorkflow
|
||
@ObservedObject var loginController: LaunchAtLoginController
|
||
@ObservedObject var silentLaunch: SilentLaunchController
|
||
@State private var presentedEditor: SettingsEditorSheet?
|
||
|
||
var body: some View {
|
||
Form {
|
||
generalSection
|
||
endpointSection
|
||
systemProxySection
|
||
PluginManagementSection(model: plugins)
|
||
}
|
||
.formStyle(.grouped)
|
||
.sheet(item: $presentedEditor) { editor in
|
||
switch editor {
|
||
case .ports(let session):
|
||
PortSettingsEditorSheet(workflow: workflow, initialDraft: session.initialDraft)
|
||
case .listener(let session):
|
||
ListenerModeEditorSheet(workflow: workflow, initialMode: session.initialMode)
|
||
case .proxyExceptions(let session):
|
||
ProxyExceptionsEditorSheet(workflow: workflow, initialValue: session.initialValue)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 常规
|
||
|
||
private var generalSection: some View {
|
||
Section {
|
||
VStack(alignment: .leading, spacing: 4) {
|
||
Toggle(
|
||
isOn: Binding(
|
||
get: { loginController.isEnabled },
|
||
set: { loginController.setEnabled($0) })
|
||
) {
|
||
settingCopy("登录时启动 ShadowsocksX-NG2", note: "启动菜单栏应用,不会自动开启代理")
|
||
}
|
||
.toggleStyle(.switch)
|
||
if loginController.requiresApproval {
|
||
Text("请在系统设置 → 登录项 → 允许在后台运行中批准 ShadowsocksX-NG2。")
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
if let loginError = loginController.errorMessage {
|
||
Text(loginError)
|
||
.font(.caption)
|
||
.foregroundStyle(.red)
|
||
}
|
||
}
|
||
.padding(.vertical, 4)
|
||
VStack(alignment: .leading, spacing: 4) {
|
||
Toggle(
|
||
isOn: Binding(
|
||
get: { silentLaunch.isEnabled },
|
||
set: { silentLaunch.setEnabled($0) })
|
||
) {
|
||
settingCopy("静默启动", note: "启动时后台运行,不显示主窗口;下次启动生效")
|
||
}
|
||
.toggleStyle(.switch)
|
||
}
|
||
.padding(.vertical, 4)
|
||
} header: {
|
||
sectionHeader("常规")
|
||
}
|
||
}
|
||
|
||
// MARK: - 后台代理客户端
|
||
|
||
private var endpointSection: some View {
|
||
Section {
|
||
if let failure = workflow.lastFailure {
|
||
Label(failure.presentableMessage, systemImage: "exclamationmark.triangle.fill")
|
||
.font(.footnote)
|
||
.foregroundStyle(.red)
|
||
}
|
||
settingRow("监听方式") {
|
||
Button {
|
||
presentedEditor = .listener(
|
||
ListenerModeEditorSession(initialMode: workflow.beginListenerModeEditing()))
|
||
} label: {
|
||
HStack(spacing: 8) {
|
||
Text(workflow.committedListenerMode.displayName)
|
||
Image(systemName: "chevron.right")
|
||
.font(.caption.weight(.semibold))
|
||
}
|
||
}
|
||
.buttonStyle(.bordered)
|
||
.accessibilityLabel("监听方式")
|
||
.accessibilityValue(workflow.committedListenerMode.displayName)
|
||
}
|
||
portSettingsRow
|
||
} header: {
|
||
sectionHeader("后台代理客户端")
|
||
}
|
||
}
|
||
|
||
// MARK: - 系统代理
|
||
|
||
private var systemProxySection: some View {
|
||
Section {
|
||
settingRow(
|
||
"额外系统代理例外",
|
||
note: "只追加到系统代理;应用固定绕过规则仍生效"
|
||
) {
|
||
Button {
|
||
presentedEditor = .proxyExceptions(
|
||
ProxyExceptionsEditorSession(initialValue: workflow.beginProxyExceptionsEditing()))
|
||
} label: {
|
||
HStack(spacing: 8) {
|
||
Text("\(workflow.committedProxyExceptionCount) 项")
|
||
Image(systemName: "chevron.right")
|
||
.font(.caption.weight(.semibold))
|
||
}
|
||
}
|
||
.buttonStyle(.bordered)
|
||
.accessibilityLabel("额外系统代理例外")
|
||
.accessibilityValue("\(workflow.committedProxyExceptionCount) 项")
|
||
.accessibilityHint("编辑并保存额外的系统代理例外")
|
||
}
|
||
} header: {
|
||
sectionHeader("系统代理")
|
||
}
|
||
}
|
||
|
||
// MARK: - 呈现组件
|
||
|
||
/// 分组卡标题行(票 #57)。
|
||
private func sectionHeader(_ title: String) -> some View {
|
||
Text(title)
|
||
.font(.headline)
|
||
.padding(.vertical, 2)
|
||
}
|
||
|
||
/// 主文案 + 可选次说明(票 #57 原型行式;后台代理客户端区不配次说明)。
|
||
private func settingCopy(_ title: String, note: String? = nil) -> some View {
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
Text(title)
|
||
.font(.body)
|
||
if let note {
|
||
Text(note)
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 行式布局:左侧文案,右侧控件(票 #57)。
|
||
private func settingRow<Control: View>(
|
||
_ title: String, note: String? = nil, @ViewBuilder control: () -> Control
|
||
) -> some View {
|
||
HStack(spacing: 16) {
|
||
settingCopy(title, note: note)
|
||
Spacer(minLength: 16)
|
||
control()
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 端口设置摘要与编辑框
|
||
|
||
extension SettingsView {
|
||
fileprivate var portSettingsRow: some View {
|
||
let ports = workflow.committedPortDraft
|
||
let socksPort = ports.socksPort.formatted(.number.grouping(.never))
|
||
let httpPort = ports.httpPort.formatted(.number.grouping(.never))
|
||
return settingRow("端口设置") {
|
||
Button {
|
||
presentedEditor = .ports(
|
||
PortSettingsEditorSession(initialDraft: workflow.beginPortSettingsEditing()))
|
||
} label: {
|
||
HStack(spacing: 8) {
|
||
Text("SOCKS \(socksPort) / HTTP \(httpPort)")
|
||
.monospacedDigit()
|
||
Image(systemName: "chevron.right")
|
||
.font(.caption.weight(.semibold))
|
||
}
|
||
}
|
||
.buttonStyle(.bordered)
|
||
.accessibilityLabel("端口设置")
|
||
.accessibilityValue("SOCKS \(socksPort),HTTP 代理 \(httpPort)")
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
private enum SettingsEditorSheet: Identifiable {
|
||
case ports(PortSettingsEditorSession)
|
||
case listener(ListenerModeEditorSession)
|
||
case proxyExceptions(ProxyExceptionsEditorSession)
|
||
|
||
var id: UUID {
|
||
switch self {
|
||
case .ports(let session): session.id
|
||
case .listener(let session): session.id
|
||
case .proxyExceptions(let session): session.id
|
||
}
|
||
}
|
||
}
|
||
|
||
private struct PortSettingsEditorSession: Identifiable {
|
||
let id = UUID()
|
||
let initialDraft: SettingsPortDraft
|
||
}
|
||
|
||
private struct ProxyExceptionsEditorSession: Identifiable {
|
||
let id = UUID()
|
||
let initialValue: String
|
||
}
|
||
|
||
private struct ProxyExceptionsEditorSheet: View {
|
||
@ObservedObject var workflow: SettingsWorkflow
|
||
@Environment(\.dismiss) private var dismiss
|
||
@State private var draft: String
|
||
|
||
init(workflow: SettingsWorkflow, initialValue: String) {
|
||
self.workflow = workflow
|
||
_draft = State(initialValue: initialValue)
|
||
}
|
||
|
||
var body: some View {
|
||
VStack(spacing: 0) {
|
||
ScrollView {
|
||
VStack(alignment: .leading, spacing: 14) {
|
||
Text("额外系统代理例外")
|
||
.font(.title2)
|
||
Text("添加希望 macOS 系统代理跳过的主机名、域名或 IP/CIDR。留空并确定会清除用户添加的条目。")
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
|
||
TextEditor(text: $draft)
|
||
.font(.system(.body, design: .monospaced))
|
||
.scrollContentBackground(.hidden)
|
||
.padding(6)
|
||
.frame(height: 112)
|
||
.background(.background, in: RoundedRectangle(cornerRadius: 8))
|
||
.overlay {
|
||
RoundedRectangle(cornerRadius: 8)
|
||
.stroke(.quaternary, lineWidth: 1)
|
||
}
|
||
.disabled(workflow.isCommitting)
|
||
.accessibilityLabel("额外系统代理例外")
|
||
|
||
Text(
|
||
"格式:域名、主机名或 IP/CIDR,以逗号、顿号、空格或换行分隔。示例:127.0.0.1, 192.168.0.0/16, localhost。"
|
||
)
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
|
||
Divider()
|
||
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
Text("应用固定的系统代理例外")
|
||
.font(.headline)
|
||
Text(FixedLocalProxyRanges.systemProxyExceptions.joined(separator: "\n"))
|
||
.font(.system(.caption, design: .monospaced))
|
||
.textSelection(.enabled)
|
||
Text("应用系统代理配置时,还会固定开启“不包括简单主机名(Exclude simple hostnames)”。")
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
Text("应用固定的 ACL 绕过规则")
|
||
.font(.headline)
|
||
Text(FixedLocalProxyRanges.aclBypassRules.joined(separator: "\n"))
|
||
.font(.system(.caption, design: .monospaced))
|
||
.textSelection(.enabled)
|
||
Text("这些规则用于本机 SOCKS/HTTP 入站;上方输入只追加到系统代理例外,不会修改 ACL。")
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
}
|
||
|
||
if let failure = workflow.lastFailure {
|
||
Label(failure.presentableMessage, systemImage: "exclamationmark.triangle.fill")
|
||
.font(.footnote)
|
||
.foregroundStyle(.red)
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
.padding(.bottom, 16)
|
||
}
|
||
|
||
HStack {
|
||
Spacer()
|
||
Button("取消", role: .cancel) {
|
||
dismiss()
|
||
}
|
||
.keyboardShortcut(.cancelAction)
|
||
.disabled(workflow.isCommitting)
|
||
|
||
Button(workflow.isCommitting ? "保存中…" : "确定") {
|
||
Task {
|
||
if case .persisted = await workflow.saveProxyExceptions(draft) {
|
||
dismiss()
|
||
}
|
||
}
|
||
}
|
||
.keyboardShortcut(.defaultAction)
|
||
.disabled(workflow.isCommitting)
|
||
}
|
||
.padding(.top, 12)
|
||
}
|
||
.padding(24)
|
||
.frame(width: 560, height: 640)
|
||
.interactiveDismissDisabled(workflow.isCommitting)
|
||
}
|
||
}
|
||
|
||
private struct PortSettingsEditorSheet: View {
|
||
@ObservedObject var workflow: SettingsWorkflow
|
||
@Environment(\.dismiss) private var dismiss
|
||
@State private var draft: SettingsPortDraft
|
||
|
||
init(workflow: SettingsWorkflow, initialDraft: SettingsPortDraft) {
|
||
self.workflow = workflow
|
||
_draft = State(initialValue: initialDraft)
|
||
}
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 16) {
|
||
Text("端口设置")
|
||
.font(.title2)
|
||
Text("端口范围为 1000–65535,SOCKS5 与 HTTP 代理端口必须不同。")
|
||
.font(.caption)
|
||
.foregroundStyle(.secondary)
|
||
|
||
VStack(alignment: .leading, spacing: 14) {
|
||
portField(.socks, title: "SOCKS 代理端口")
|
||
portField(.http, title: "HTTP 代理端口")
|
||
}
|
||
|
||
if let failure = workflow.lastFailure {
|
||
Label(failure.presentableMessage, systemImage: "exclamationmark.triangle.fill")
|
||
.font(.footnote)
|
||
.foregroundStyle(.red)
|
||
}
|
||
|
||
HStack {
|
||
Spacer()
|
||
Button("取消", role: .cancel) {
|
||
dismiss()
|
||
}
|
||
.keyboardShortcut(.cancelAction)
|
||
.disabled(workflow.isCommitting)
|
||
|
||
Button(workflow.isCommitting ? "保存中…" : "保存") {
|
||
Task {
|
||
if case .persisted = await workflow.savePortSettings(draft) {
|
||
dismiss()
|
||
}
|
||
}
|
||
}
|
||
.keyboardShortcut(.defaultAction)
|
||
.disabled(!workflow.canSavePortSettings(draft))
|
||
}
|
||
}
|
||
.padding(24)
|
||
.frame(width: 460)
|
||
.interactiveDismissDisabled(workflow.isCommitting)
|
||
.task(id: draft) {
|
||
workflow.refreshPortEditorOccupancy(for: draft)
|
||
}
|
||
}
|
||
|
||
private func portField(_ id: SettingsPortID, title: String) -> some View {
|
||
let state = workflow.portFieldState(for: id, editorDraft: draft)
|
||
return VStack(alignment: .leading, spacing: 5) {
|
||
HStack(spacing: 16) {
|
||
Text(title)
|
||
Spacer(minLength: 12)
|
||
TextField(title, value: portBinding(for: id), format: .number.grouping(.never))
|
||
.textFieldStyle(.roundedBorder)
|
||
.multilineTextAlignment(.trailing)
|
||
.monospacedDigit()
|
||
.frame(width: 112)
|
||
.disabled(workflow.isCommitting)
|
||
}
|
||
|
||
ForEach(state.issues, id: \.self) { issue in
|
||
Text(AppPresentation.message(for: issue))
|
||
.font(.caption)
|
||
.foregroundStyle(.red)
|
||
.multilineTextAlignment(.trailing)
|
||
.frame(maxWidth: .infinity, alignment: .trailing)
|
||
}
|
||
|
||
if let occupancy = occupancyMessage(for: state) {
|
||
Text(occupancy)
|
||
.font(.caption)
|
||
.foregroundStyle(occupancyColor(for: state))
|
||
.multilineTextAlignment(.trailing)
|
||
.frame(maxWidth: .infinity, alignment: .trailing)
|
||
}
|
||
|
||
if state.canSuggestFreePort {
|
||
Button("建议空闲端口") {
|
||
Task {
|
||
let outcome = await workflow.suggestFreePort(for: id, from: draft)
|
||
if case .suggestedPort(let port, let value) = outcome {
|
||
draft.setPortValue(value, for: port)
|
||
}
|
||
}
|
||
}
|
||
.font(.caption)
|
||
.disabled(workflow.isCommitting)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func portBinding(for id: SettingsPortID) -> Binding<Int> {
|
||
Binding(
|
||
get: { draft.portValue(for: id) },
|
||
set: { draft.setPortValue($0, for: id) })
|
||
}
|
||
|
||
private func occupancyMessage(for state: SettingsPortFieldState) -> String? {
|
||
guard let occupancy = state.occupancy else { return nil }
|
||
switch occupancy {
|
||
case .free:
|
||
return nil
|
||
case .occupied(let occupier):
|
||
if state.isRuntimePortException {
|
||
return "当前代理正在使用此端口"
|
||
}
|
||
return "当前端口已占用" + (occupier.map { "(" + $0 + ")" } ?? "")
|
||
case .unknown(let detail):
|
||
return "端口状态无法确定:" + detail
|
||
}
|
||
}
|
||
|
||
private func occupancyColor(for state: SettingsPortFieldState) -> Color {
|
||
guard let occupancy = state.occupancy else { return .secondary }
|
||
switch occupancy {
|
||
case .free, .unknown:
|
||
return .secondary
|
||
case .occupied:
|
||
return state.isRuntimePortException ? .secondary : .orange
|
||
}
|
||
}
|
||
}
|