Files
boss/apps/boss-agent-mac/Sources/BossAgentApp.swift
2026-05-12 17:04:40 +08:00

78 lines
2.7 KiB
Swift

import Cocoa
import WebKit
final class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate {
private var window: NSWindow?
private var webView: WKWebView?
func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.setActivationPolicy(.regular)
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.setValue(false, forKey: "drawsBackground")
webView.navigationDelegate = self
self.webView = webView
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 1180, height: 780),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false
)
window.title = "boss-agent"
window.titlebarAppearsTransparent = true
window.isMovableByWindowBackground = true
window.contentView = webView
window.center()
window.makeKeyAndOrderFront(nil)
self.window = window
loadAgentPanel()
NSApp.activate(ignoringOtherApps: true)
}
private func loadAgentPanel() {
guard let url = URL(string: "http://127.0.0.1:4317/boss-agent") else {
loadFallback()
return
}
webView?.load(URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData))
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
loadFallback()
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
loadFallback()
}
private func loadFallback() {
let html = """
<!doctype html>
<html lang="zh-CN">
<meta charset="utf-8">
<style>
body { margin:0; min-height:100vh; display:grid; place-items:center; background:#f6f8f5; font-family:-apple-system,BlinkMacSystemFont,'PingFang SC',sans-serif; color:#111418; }
.card { width:520px; padding:32px; border-radius:24px; background:white; border:1px solid #e8ece9; box-shadow:0 24px 70px rgba(22,38,29,.12); }
h1 { margin:0 0 10px; font-size:28px; letter-spacing:-.04em; }
p { color:#707982; line-height:1.7; margin:0; }
</style>
<body>
<section class="card">
<h1>boss-agent 未启动</h1>
<p>请先启动本机 local-agent 服务,然后重新打开 boss-agent。</p>
</section>
</body>
</html>
"""
webView?.loadHTMLString(html, baseURL: nil)
}
}
let app = NSApplication.shared
let delegate = AppDelegate()
app.delegate = delegate
app.run()