Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/StatusMenuModel.swift
T
Qiu Yuzhou 2fe401cd50 feat(ng2): add catalog workflow module as single UI-facing seam (#41)
建立目录工作流深 module(App/CatalogWorkflow/),收窄主窗口与配置 UI 对
配置目录、凭据存储、订阅快照、Legacy 导入与运行时收敛的访问:

- UI-facing interface:非敏感树/编辑面/订阅 projection、opaque NodeID 查询、
  typed command(ServerEditDraft 等)、typed error 与 structured outcome
  (BatchImportOutcome 逐行点名、RemovalOutcome selection invalidation);
  秘密值只在显式编辑/分享/订阅 URL 命令中出现。
- 服务器编辑:配置与凭据作为一个逻辑变更,CredentialWriteJournal 保证目录
  持久化失败时旧配置与旧凭据都保持有效(story 12/13,含测试)。
- 提交与收敛分离:目录提交成功与 runtimeSync 收敛阶段是独立观察面,运行时
  失败不回滚已提交目录(story 38/39,含测试)。
- 订阅:创建/刷新/换址/删除沿用原子快照、last-good 保留与凭据回滚;卡片
  projection 只含 host,结构化刷新状态由呈现层本地化。
- Legacy 导入端到端编排:不驱动运行时收敛、不自动重复、显式再导入创建独立
  分组;handoff 仍经组合根注入的独立边界。
- Deletion test:删除 CatalogViewModel/Types/+Subscriptions 旧 facade,全部
  视图与状态菜单迁移到新 seam;选择、pane、sheet、alert 状态收归 UI 持有
  (ErrorAlertPresenter)。导入失败行以行号点名,不再回显 ss:// 行内容。

测试经 module interface 观察(395 项全部通过):持久化失败凭据回滚、提交/
收敛阶段分离、收敛失败不回滚、导入不触发运行时等新增场景;既有 Domain、
协调器与运行时测试保持不变。

Out of scope(spec #41):设置编辑、ProxyRuntimeController、Legacy handoff、
新视觉设计、独立编译 target。
2026-09-22 11:39:07 +08:00

124 lines
4.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// 状态菜单八项白名单的呈现模型(spec #21 D11,issue #31):从运行状态、
/// 模式、目录树与监听设置派生菜单数据。不持有 AppKit 类型,纯逻辑可测;
/// 视觉与交互细节(勾选标记、禁用态)在 ProxyStatusMenu 呈现,不建自动测试。
enum StatusMenuModel {
/// 头部状态摘要:运行状态、当前模式、活动目标(含点名失败细节)。
struct Summary: Equatable {
/// 代理开关的当前意图:true 为已开(菜单项呈现「停止代理」)。
let isOn: Bool
let status: String
/// 启动/激活/系统代理失败的点名原因,随状态摘要一并呈现。
let detail: String?
let modeLabel: String
/// 活动目标从根到节点的显示名路径;未激活为 nil。
let targetPath: String?
}
/// 运行状态 → 摘要映射(与主窗口诊断区口径一致)。
static func summary(
state: ProxyRuntimeController.ProxyState,
mode: ProxyMode,
targetPath: String?
) -> Summary {
let status: String
let detail: String?
switch state {
case .off:
status = "代理未运行"
detail = nil
case .starting:
status = "正在启动代理…"
detail = nil
case .running:
status = "代理运行中"
detail = nil
case .firewallBlocked(let reason):
status = "代理运行中(局域网受阻)"
detail = reason
case .launchFailed(let reason):
status = "启动失败"
detail = reason
case .activationFailed(let reason):
status = "无法启动"
detail = reason
case .requiresApproval:
status = "等待允许后台代理"
detail = "请在系统设置-登录项中允许 ShadowsocksX-NG 后台项"
case .serviceFailed(let reason):
status = "服务管理失败"
detail = reason
case .systemProxyFailed(let reason):
status = "系统代理未应用"
detail = reason
}
return Summary(
isOn: isOn(state: state), status: status, detail: detail, modeLabel: mode.label,
targetPath: targetPath)
}
/// 代理开关的当前意图(菜单开关与全局快捷键共用的同一判定):启动失败等
/// 中间态视为未开——两个入口的下一步动作都是「启动」。
static func isOn(state: ProxyRuntimeController.ProxyState) -> Bool {
switch state {
case .off, .launchFailed, .activationFailed, .serviceFailed:
false
case .starting, .running, .firewallBlocked, .requiresApproval, .systemProxyFailed:
true
}
}
/// 复制 HTTP 导出行(D11):shell 可直接 source 的 http/https 双导出;
/// HTTP 入站未启用时无行可复制(菜单项禁用)。地址取监听范围的对外地址
/// (回环态 127.0.0.1,主机态为对外公布地址),与 PAC 语义一致。
static func httpExportLine(settings: SslocalListenSettings) -> String? {
guard settings.httpProxyEnabled else { return nil }
let endpoint = "http://\(settings.scope.advertisedAddress):\(settings.httpPort)"
return "export http_proxy=\(endpoint);export https_proxy=\(endpoint);"
}
/// 全局快捷键「切换模式」的循环序:仅内置三模式参与;外部 PAC 的 URL 只能
/// 在设置区配置(#33),不进循环——快捷键从外部 PAC 回到第一个可用内置模式。
static func nextMode(
after mode: ProxyMode,
availableModes: Set<ProxyModeKind> = ProxySettings.defaultEnabledModes
) -> ProxyMode {
let cycle = [ProxyModeKind.pac, .global, .manual].filter(availableModes.contains)
guard !cycle.isEmpty else { return mode }
guard let currentIndex = cycle.firstIndex(of: mode.kind) else {
return proxyMode(for: cycle[0])
}
return proxyMode(for: cycle[(currentIndex + 1) % cycle.count])
}
private static func proxyMode(for kind: ProxyModeKind) -> ProxyMode {
switch kind {
case .pac: .pac
case .global: .global
case .manual: .manual
case .externalPAC: .pac
}
}
/// 活动目标的显示名路径(根 → 节点," / " 连接);基于目录树 projection
/// 派生(issue #41)。未激活或目标已不在树中为 nil。
static func targetPath(in nodes: [CatalogTreeNode], activeTargetID: NodeID?) -> String? {
guard let activeTargetID else { return nil }
var path: [String] = []
func search(_ node: CatalogTreeNode) -> Bool {
if node.id == activeTargetID {
path.append(node.name)
return true
}
for child in node.children ?? [] where search(child) {
path.insert(node.name, at: 0)
return true
}
return false
}
for node in nodes where search(node) { break }
return path.isEmpty ? nil : path.joined(separator: " / ")
}
}