mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-16 17:04:43 +00:00
App/ 根目录原有 73 个散落源文件,已无法从目录读出任何分组信息。 按「目录即分层」重组为 Composition / Application / Presentation / UI / PlatformEffects / Resources 六个目录,UI 层再按子视图分目录(Workspace、 Home、Servers(含 Plugin)、Subscriptions、Rules、Settings、Diagnostics、 StatusMenu)。纯移动:类型名、模块名与 import 均未改动,App/ 根层不再有 散落文件。 配套改动: - project.yml:Core 测试镜像的 excludes 由裸文件名改为 `**/` 递归形式。 XcodeGen 的 exclude 不跨 `/`,裸名只匹配 App 根层;MainApp.swift 下移后 旧写法会静默失效,把 @main 组合根编进测试镜像。info.path 与 entitlements.path 指向 App/Resources/;并排除 `**/*.md`——App/ 下任何 非源码文件都会被 XcodeGen 收进 Resources 阶段并封进签名后的 bundle。 - SkeletonTests 的架构守卫由「默认扫描全部 App/ + 7 个豁免文件 + 1 个豁免 前缀 + 4 个豁免目录」改为正面圈定 App/UI/,豁免登记全部删除;新增运行时 适配器放进 Application/ 即自动落在扫描范围外,无需再登记。 - README 工程结构说明同步;根 .gitignore 的生成物路径随 Resources/ 归位。 验证:clean build 0 error;task test 1022 用例全通过(含两个架构守卫); task format 无改动(证明纯移动);task lint 与移动前同为 21 条内容型 warning;packaging-gate.sh ALL CHECKS PASSED,确认 plist/entitlements 归位 后签名与 entitlements 语义不变。 注意:迁移时须先删旧的生成物再 xcodegen generate,否则遗留的 App/Info.plist 会被当作普通资源扫进 Resources 阶段。
121 lines
4.5 KiB
Swift
121 lines
4.5 KiB
Swift
import AppKit
|
||
import SwiftUI
|
||
|
||
/// All explicitly anchored application windows share one coordinator. Sheets and
|
||
/// unrelated windows do not participate. Hiding does not count as closing.
|
||
@MainActor
|
||
protocol WindowActivationPolicyApplying: AnyObject {
|
||
func apply(_ policy: NSApplication.ActivationPolicy)
|
||
}
|
||
|
||
@MainActor
|
||
final class WindowActivationPolicyCoordinator: ObservableObject {
|
||
private let anchoredWindows = NSHashTable<NSWindow>.weakObjects()
|
||
private var openWindows: Set<ObjectIdentifier> = []
|
||
private let applier: any WindowActivationPolicyApplying
|
||
|
||
/// `applier` 注入以便 hermetic 单测;生产实现里单测 host 不触碰真实 NSApp。
|
||
init(applying applier: any WindowActivationPolicyApplying) {
|
||
self.applier = applier
|
||
let center = NotificationCenter.default
|
||
center.addObserver(
|
||
self, selector: #selector(windowDidBecomeKey(_:)),
|
||
name: NSWindow.didBecomeKeyNotification, object: nil)
|
||
center.addObserver(
|
||
self, selector: #selector(windowWillClose(_:)),
|
||
name: NSWindow.willCloseNotification, object: nil)
|
||
center.addObserver(
|
||
self, selector: #selector(windowVisibilityChanged(_:)),
|
||
name: NSWindow.didChangeOcclusionStateNotification, object: nil)
|
||
}
|
||
|
||
deinit {
|
||
NotificationCenter.default.removeObserver(self)
|
||
}
|
||
|
||
/// 锚点视图捕获到宿主窗口时上交(幂等)。窗口已可见才立刻收敛(覆盖不走
|
||
/// key 事件的可见路径);静默启动预建未呈现的窗口保持 accessory,等打开
|
||
/// 后的 key 事件。
|
||
func anchorDidAttach(to window: NSWindow) {
|
||
anchoredWindows.add(window)
|
||
if window.isVisible {
|
||
openWindows.insert(ObjectIdentifier(window))
|
||
applier.apply(.regular)
|
||
}
|
||
}
|
||
|
||
@objc private func windowDidBecomeKey(_ notification: Notification) {
|
||
guard let window = trackedWindow(in: notification) else { return }
|
||
openWindows.insert(ObjectIdentifier(window))
|
||
applier.apply(.regular)
|
||
}
|
||
|
||
@objc private func windowWillClose(_ notification: Notification) {
|
||
guard let window = trackedWindow(in: notification) else { return }
|
||
// A background window can be presented without ever becoming key. Include
|
||
// visible siblings even if their initial visibility notification was missed.
|
||
for sibling in anchoredWindows.allObjects where sibling !== window && sibling.isVisible {
|
||
openWindows.insert(ObjectIdentifier(sibling))
|
||
}
|
||
openWindows.remove(ObjectIdentifier(window))
|
||
if openWindows.isEmpty { applier.apply(.accessory) }
|
||
}
|
||
|
||
@objc private func windowVisibilityChanged(_ notification: Notification) {
|
||
guard let window = trackedWindow(in: notification), window.isVisible else { return }
|
||
let inserted = openWindows.insert(ObjectIdentifier(window)).inserted
|
||
if inserted { applier.apply(.regular) }
|
||
}
|
||
|
||
private func trackedWindow(in notification: Notification) -> NSWindow? {
|
||
guard let window = notification.object as? NSWindow, anchoredWindows.contains(window)
|
||
else { return nil }
|
||
return window
|
||
}
|
||
}
|
||
|
||
/// 挂在 Window scene 内容上的壳修饰器:零尺寸锚点视图捕获宿主 NSWindow,
|
||
/// 协调器据其生命周期收敛 app 激活策略。NSApp 侧 applier 由 MainApp 注入
|
||
/// (hermetic 判定 `ApplicationDependencies` 是组合根的 private 类型)。
|
||
struct WindowActivationPolicy: ViewModifier {
|
||
@ObservedObject var coordinator: WindowActivationPolicyCoordinator
|
||
|
||
func body(content: Content) -> some View {
|
||
content.background(WindowActivationAnchor(onAttach: coordinator.anchorDidAttach))
|
||
}
|
||
}
|
||
|
||
/// 锚点视图:随内容装进宿主窗口,`viewDidMoveToWindow` 上交宿主。
|
||
private struct WindowActivationAnchor: NSViewRepresentable {
|
||
let onAttach: (NSWindow) -> Void
|
||
|
||
func makeNSView(context: Context) -> AnchorView {
|
||
AnchorView(onAttach: onAttach)
|
||
}
|
||
|
||
func updateNSView(_ nsView: AnchorView, context: Context) {}
|
||
|
||
final class AnchorView: NSView {
|
||
private let onAttach: (NSWindow) -> Void
|
||
|
||
init(onAttach: @escaping (NSWindow) -> Void) {
|
||
self.onAttach = onAttach
|
||
super.init(frame: .zero)
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) {
|
||
fatalError("NSView 不走 storyboard 初始化")
|
||
}
|
||
|
||
override func viewDidMoveToWindow() {
|
||
super.viewDidMoveToWindow()
|
||
// 关窗时 SwiftUI 可能置 nil(复用窗口语义下内容也可能保留);nil 不回写,
|
||
// 已捕获引用留给 willClose 匹配,重开由重新 attach 或 key 事件覆盖。
|
||
if let window {
|
||
onAttach(window)
|
||
}
|
||
}
|
||
}
|
||
}
|