mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-17 15:41:34 +00:00
* style: apply SwiftFormat 0.62.1 rules Refs #103202 * ci: enforce deterministic Swift lint Refs #103202 * refactor: keep gateway connect lint-clean Refs #103202 * style: keep iOS typography checks warning-free * ci: route MLX Swift changes through pre-push * fix: preserve native i18n extraction after Swift cleanup * refactor: keep rebased Swift surfaces lint-clean * style: format latest Swift additions * chore: refresh native i18n inventory * style: keep generated Swift formatter-clean * fix: preserve node route invalidation callbacks * fix: keep native translation IDs stable * fix: retain native translation identifiers * fix: preserve translations across Swift source moves
147 lines
4.6 KiB
Swift
147 lines
4.6 KiB
Swift
import AppKit
|
|
import Foundation
|
|
|
|
/// A borderless panel that can still accept key focus (needed for typing).
|
|
final class WebChatPanel: NSPanel {
|
|
override var canBecomeKey: Bool {
|
|
true
|
|
}
|
|
|
|
override var canBecomeMain: Bool {
|
|
true
|
|
}
|
|
}
|
|
|
|
enum WebChatPresentation {
|
|
case window
|
|
case panel(anchorProvider: () -> NSRect?)
|
|
|
|
var isPanel: Bool {
|
|
if case .panel = self {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
final class WebChatManager {
|
|
static let shared = WebChatManager()
|
|
|
|
private var windowController: WebChatSwiftUIWindowController?
|
|
private var windowSessionKey: String?
|
|
private var panelController: WebChatSwiftUIWindowController?
|
|
private var panelSessionKey: String?
|
|
private var currentChatSessionKey: String?
|
|
private var cachedPreferredSessionKey: String?
|
|
|
|
var onPanelVisibilityChanged: ((Bool) -> Void)?
|
|
|
|
var activeSessionKey: String? {
|
|
self.currentChatSessionKey ?? self.panelSessionKey ?? self.windowSessionKey
|
|
}
|
|
|
|
func show(sessionKey: String) {
|
|
self.closePanel()
|
|
if let controller = self.windowController {
|
|
// The window shell switches sessions in place (sidebar, /new);
|
|
// windowSessionKey tracks those switches, so a window already on
|
|
// the requested session must not be torn down and re-bootstrapped.
|
|
if self.windowSessionKey == sessionKey {
|
|
controller.show()
|
|
return
|
|
}
|
|
|
|
controller.close()
|
|
self.windowController = nil
|
|
self.windowSessionKey = nil
|
|
}
|
|
let controller = WebChatSwiftUIWindowController(sessionKey: sessionKey, presentation: .window)
|
|
controller.onVisibilityChanged = { [weak self] visible in
|
|
self?.onPanelVisibilityChanged?(visible)
|
|
}
|
|
controller.onSessionKeyChanged = { [weak self] key in
|
|
self?.windowSessionKey = key
|
|
self?.currentChatSessionKey = key
|
|
}
|
|
self.windowController = controller
|
|
self.windowSessionKey = sessionKey
|
|
self.currentChatSessionKey = sessionKey
|
|
controller.show()
|
|
}
|
|
|
|
func togglePanel(sessionKey: String, anchorProvider: @escaping () -> NSRect?) {
|
|
if let controller = self.panelController {
|
|
if self.panelSessionKey != sessionKey {
|
|
controller.close()
|
|
self.panelController = nil
|
|
self.panelSessionKey = nil
|
|
} else {
|
|
if controller.isVisible {
|
|
controller.close()
|
|
} else {
|
|
controller.presentAnchored(anchorProvider: anchorProvider)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
let controller = WebChatSwiftUIWindowController(
|
|
sessionKey: sessionKey,
|
|
presentation: .panel(anchorProvider: anchorProvider))
|
|
controller.onClosed = { [weak self] in
|
|
self?.panelHidden()
|
|
}
|
|
controller.onVisibilityChanged = { [weak self] visible in
|
|
self?.onPanelVisibilityChanged?(visible)
|
|
}
|
|
controller.onSessionKeyChanged = { [weak self] key in
|
|
self?.panelSessionKey = key
|
|
self?.currentChatSessionKey = key
|
|
}
|
|
self.panelController = controller
|
|
self.panelSessionKey = sessionKey
|
|
self.currentChatSessionKey = sessionKey
|
|
controller.presentAnchored(anchorProvider: anchorProvider)
|
|
}
|
|
|
|
func recordActiveSessionKey(_ sessionKey: String) {
|
|
let trimmed = sessionKey.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !trimmed.isEmpty else { return }
|
|
self.currentChatSessionKey = trimmed
|
|
}
|
|
|
|
func closePanel() {
|
|
self.panelController?.close()
|
|
}
|
|
|
|
func preferredSessionKey() async -> String {
|
|
if let cachedPreferredSessionKey {
|
|
return cachedPreferredSessionKey
|
|
}
|
|
let key = await GatewayConnection.shared.mainSessionKey()
|
|
self.cachedPreferredSessionKey = key
|
|
return key
|
|
}
|
|
|
|
func resetTunnels() {
|
|
self.windowController?.close()
|
|
self.windowController = nil
|
|
self.windowSessionKey = nil
|
|
self.panelController?.close()
|
|
self.panelController = nil
|
|
self.panelSessionKey = nil
|
|
self.currentChatSessionKey = nil
|
|
self.cachedPreferredSessionKey = nil
|
|
}
|
|
|
|
func close() {
|
|
self.resetTunnels()
|
|
}
|
|
|
|
private func panelHidden() {
|
|
self.onPanelVisibilityChanged?(false)
|
|
// Keep panel controller cached so reopening doesn't re-bootstrap.
|
|
}
|
|
}
|