Mac 后台常驻(隐藏 Dock + 关窗续跑):引擎 / 监听上移 App 级

实现②隐藏 Dock 图标 + ③可靠后台刷新,交付单窗口、可后台常驻的 App。

- 新增 MacBackgroundController(@MainActor App 级单例):持有 engine + auth(脱离窗口 @State,生命周期与 App 一致)。把引擎离屏 WebView 挂常驻隐藏 NSWindow(始终在窗口层级、避 WebKit 无窗口挂起,且脱离主窗口生命周期——关主窗口引擎不销毁,SSE / presence / 传输 / 剪贴板续跑)+ App 级 ProcessInfo.beginActivity 防 App Nap + 按登录态启停剪贴板 monitor / 全局热键。observeAuth(withObservationTracking 桥 Observation→命令式)观察 auth.session 驱动 start/stop
- 新增 MacAppDelegate(@NSApplicationDelegateAdaptor):applicationDidFinishLaunching 应用 Dock 策略 + bootstrap 后台运行时;applicationShouldTerminateAfterLastWindowClosed=false——关最后一个窗口不退出,菜单栏常驻续跑
- CommilitiaDropMacApp:engine / auth 改由 MacBackgroundController 持有(不再窗口 @State);MacAppRoot 只做登录门界面切换、不挂引擎生命周期。MacRootView 移除内嵌 EngineWebView + 移除窗口 onAppear/onDisappear 的 monitor/hotkey(全上移 controller)——关窗引擎 / 同步 / 热键不再停
- 设置加「后台」区:隐藏 Dock 图标开关(@AppStorage macHideDock → NSApp.setActivationPolicy .accessory/.regular,默认 regular 显示)+ 后台常驻说明;剪贴板自动同步开关改调 controller.refreshClipboardMonitor;登出只 auth.logout(引擎复位交 controller 随会话变 nil 统一处理)。菜单栏「打开主窗口」先 NSApp.activate 再 openWindow(accessory / 关窗后窗口可前置可见)
- 三项能力齐备:①菜单栏图标(既有 MenuBarExtra)②隐藏 Dock(新增,可设置切换)③可靠后台刷新(新增:引擎 / monitor / 热键 App 级持有 + 隐藏窗口托 WebView + App 级 App Nap 防护 + 关窗不退出续跑)
- 验证:just mac-build(CommilitiaDropMac.app)+ iOS 模拟器(CommilitiaDrop.app)均 BUILD SUCCEEDED;ad-hoc 启动冒烟通过(进程存活、无崩溃报告、干净退出)。运行期后台同步须用户登录后自测(Broker OAuth 需账号)
This commit is contained in:
2026-07-11 12:26:34 +08:00
parent a33a668286
commit 34f0db215c
5 changed files with 172 additions and 40 deletions
+12 -18
View File
@@ -1,15 +1,19 @@
#if os(macOS)
import SwiftUI
// macOS + MacRootView iOS iOS
// EngineController WKWebView JS + libwebrtc AuthManagerBroker
// WebView MacRootView iOS CDropApp
// / /
// macOS + MacRootView iOS
// WKWebView JS + libwebrtc Broker App MacBackgroundController
// WebView MacAppDelegate /
// MacBackgroundControlleriOS CDropApp
@main
struct CommilitiaDropMacApp: App
{
@State private var auth = AuthManager()
@State private var engine = EngineController()
@NSApplicationDelegateAdaptor(MacAppDelegate.self) private var appDelegate
// engine / auth App MacBackgroundController App
// .environment
private var engine: EngineController { MacBackgroundController.shared.engine }
private var auth: AuthManager { MacBackgroundController.shared.auth }
var body: some Scene
{
@@ -40,8 +44,8 @@ struct CommilitiaDropMacApp: App
}
}
// MacRootView WebView Broker
// iOS AppRootCDropApp auth userId
// Broker / /
// MacBackgroundController auth.session
struct MacAppRoot: View
{
let auth: AuthManager
@@ -63,16 +67,6 @@ struct MacAppRoot: View
.environment(auth)
}
}
.onAppear
{
engine.auth = auth
// userId reset
if let uid = auth.session?.user.id { engine.activateRecords(userId: uid) }
}
.onChange(of: auth.session?.user.id)
{ _, uid in
if let uid { engine.activateRecords(userId: uid) }
}
}
}
#endif