Files
openclaw-openclaw/apps/macos/Sources/OpenClaw/WebChatManager.swift
Peter Steinberger bb658d0a6d feat(macos): redesign chat window as native shell with sessions sidebar, toolbar pickers, slash commands, and context usage (#101103)
* feat(chat-ui): native macOS chat window shell with sessions sidebar, context usage, and slash commands

* feat(macos): host redesigned chat window shell and complete gateway chat transport

* fix(chat-ui): gate slash panel on catalog support and register window shell shortcuts outside menus

* fix(chat-ui): make sidebar selection tags optional and sync programmatic composer text into the focused NSTextView

* fix(macos): track in-window session switches for chat window reuse

* fix(chat-ui): re-bootstrap in place when deleting the active main session

* docs(macos): describe the redesigned native chat window

* fix(macos): scope bare global session RPCs to the routing default agent

* style(chat-ui): restore blank line between rebased helpers

* fix(macos): scope selected global session actions

* fix(macos): refresh chat routing identity

* fix(chat-ui): hide delete for main sessions

* fix(macos): remove duplicate session patch path

* fix(chat-ui): resolve sidebar session aliases

* fix(chat-ui): isolate sidebar alias resolution

* fix(chat-ui): reconcile active session aliases

* fix(chat-ui): prefer selected session aliases

* chore(i18n): refresh chat window inventory

* fix(chat-ui): return sidebar filter result

* refactor(chat-ui): split session actions
2026-07-07 02:06:10 +01:00

143 lines
4.5 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.
}
}