Repository Public
1import AppKit2import ClaudeIntegration3import Foundation4import Observation5import RemoteCore6import TerminalCore7import WorkspaceCore89/// The single source of truth for the live workspace: layout state + session ownership.10/// Every mutation goes through here so windows, tabs and panes stay consistent and the11/// persistence layer can observe one object.12@MainActor13@Observable14final class WorkspaceController {15 private(set) var workspace: WorkspaceState16 let registry: SessionRegistry17 var settings: AppSettings {18 didSet {19 if settings.appearance != oldValue.appearance { registry.appearance = settings.appearance }20 if settings != oldValue { changeHandler?() }21 }22 }23 /// The window that most recently became key; menu commands act on it.24 var keyWindow: WindowID?25 /// Windows SwiftUI should open (set by `createWindow`, consumed by the app).26 var pendingWindowsToOpen: [WindowID] = []27 /// Windows whose state was removed and whose NSWindow must be dismissed.28 var windowsToDismiss: Set<WindowID> = []29 /// Called after any workspace/settings mutation; the persistence layer debounces saves.30 var changeHandler: (() -> Void)?31 /// Persistence, when the database could be opened.32 var persistence: PersistenceCoordinator?33 /// Claude Code integration (CLI, hooks, tracker).34 let claude: ClaudeService35 var notifications: NotificationService?36 /// Configured SSH hosts (ssh config + user metadata), keyed by stable id.37 var hosts: [HostID: SSHHost] = [:] { didSet { refreshPlanner() } }38 /// Live NSWindows by workspace window id (weak), for deep links.39 private var nsWindows: [WindowID: WeakWindow] = [:]40 var missionControlOpenRequested = false41 /// Window in which the command palette should be presented (nil = hidden).42 var paletteWindow: WindowID?43 /// Inspector section to show ("Session", "Git", "Claude"); set by menu commands.44 var inspectorTab = "Session"45 /// Pane that should receive keyboard focus on next render (deep links, navigation).46 var focusRequest: PaneID?47 /// Panes created by workspace restoration, with how they were restored. Cleared when dismissed.48 var restorationNotes: [PaneID: RestorationKind] = [:]49 /// Windows whose saved display was missing and were relocated to the main screen.50 var relocatedWindows: Set<WindowID> = []5152 init(workspace: WorkspaceState, registry: SessionRegistry, settings: AppSettings = .default, claude: ClaudeService) {53 self.workspace = workspace54 self.registry = registry55 self.settings = settings56 self.claude = claude57 registry.appearance = settings.appearance58 refreshPlanner()59 claude.changeHandler = { [weak self] in60 guard let self else { return }61 self.refreshPlanner()62 self.startDeferredClaudePanes()63 }64 }6566 /// Claude panes restored before the CLI was discovered wait here; once the CLI is known they start67 /// (`--resume`), and panes that failed because the CLI was missing are retried.68 private func startDeferredClaudePanes() {69 guard claude.cli != nil else { return }70 for (paneID, pane) in workspace.panes {71 guard case .claude = pane.profile.kind else { continue }72 if let session = registry.session(pane.sessionID) {73 if case .failed = session.status, session.launchError?.contains("claude") == true { session.start(with: (try? registry.planner.launchSpec(for: pane.profile, sessionID: pane.sessionID)) ?? session.launchSpec ?? LaunchSpec(executable: "/usr/bin/false")) }74 } else {75 ensureSession(for: paneID)76 }77 }78 }7980 /// Rebuilds the immutable launch planner snapshot (CLI path, hooks file, hosts).81 func refreshPlanner() {82 registry.planner = CompositeLaunchPlanner(claudeCLIPath: claude.cli?.path, hooksSettingsPath: claude.hooksSettingsPath, hosts: hosts)83 }8485 // MARK: - Window registry & deep links8687 func register(nsWindow: NSWindow, for id: WindowID) { nsWindows[id] = WeakWindow(window: nsWindow) }8889 /// Brings the pane's window to the front, selects its tab and focuses it.90 func reveal(pane paneID: PaneID) {91 guard let location = workspace.location(ofPane: paneID) else { return }92 if let window = nsWindows[location.window]?.window {93 window.makeKeyAndOrderFront(nil)94 }95 NSApp.activate()96 focus(pane: paneID)97 }9899 // MARK: - Claude sessions100101 /// Starts an interactive Claude Code session in a new tab (or split). Returns nil when the CLI is missing.102 @discardableResult103 func newClaudeSession(in windowID: WindowID, split: SplitAxis? = nil, resumeID: UUID? = nil, directory: String? = nil) -> PaneID? {104 guard claude.cli != nil else { return nil }105 let id = resumeID ?? UUID()106 var profile = inheritedProfile(from: focusedPane(in: windowID))107 if let directory { profile.workingDirectory = directory }108 profile.kind = .claude(sessionID: id, resume: resumeID != nil)109 if let split {110 return self.split(in: windowID, axis: split, profile: profile)111 }112 guard let tab = newTab(in: windowID, profile: profile) else { return nil }113 return self.tab(tab)?.root.firstPane114 }115116 /// Pane hosting a Claude session id (lower-cased UUID string as sent by hooks).117 func pane(forClaudeSession sessionID: String) -> PaneID? {118 for (paneID, pane) in workspace.panes {119 if case .claude(let id, _) = pane.profile.kind, id.uuidString.lowercased() == sessionID.lowercased() { return paneID }120 }121 return nil122 }123124 /// Opens an SSH session to a configured host in a new tab (or split).125 @discardableResult126 func newSSHSession(host: SSHHost, in windowID: WindowID, split: SplitAxis? = nil, tmux: String? = nil) -> PaneID? {127 guard let hostID = hosts.first(where: { $0.value.id == host.id })?.key else { return nil }128 var profile = SessionProfile(kind: .ssh(host: hostID, tmuxSession: tmux ?? host.tmuxSession), workingDirectory: settings.defaultWorkingDirectory, label: host.label)129 profile.workingDirectory = ShellDiscovery.homeDirectory()130 if let split { return self.split(in: windowID, axis: split, profile: profile) }131 guard let tab = newTab(in: windowID, profile: profile) else { return nil }132 return self.tab(tab)?.root.firstPane133 }134135 // MARK: - Derived state136137 func window(_ id: WindowID) -> WindowState? { workspace.window(id) }138139 var primaryWindowID: WindowID? { workspace.windows.first?.id }140141 func tab(_ id: TabID) -> TabState? {142 for w in workspace.windows { if let t = w.tabs.first(where: { $0.id == id }) { return t } }143 return nil144 }145146 func selectedTab(in window: WindowID) -> TabState? {147 guard let w = self.window(window) else { return nil }148 return w.tabs.first { $0.id == w.selectedTab } ?? w.tabs.first149 }150151 func focusedPane(in window: WindowID) -> PaneID? {152 guard let tab = selectedTab(in: window) else { return nil }153 return tab.focusedPane ?? tab.root.firstPane154 }155156 func pane(_ id: PaneID) -> PaneState? { workspace.panes[id] }157158 func session(for pane: PaneID) -> TerminalSession? {159 guard let p = workspace.panes[pane] else { return nil }160 return registry.session(p.sessionID)161 }162163 func focusedSession(in window: WindowID) -> TerminalSession? {164 guard let pane = focusedPane(in: window) else { return nil }165 return session(for: pane)166 }167168 func title(for tab: TabState) -> String {169 if let custom = tab.customTitle, !custom.isEmpty { return custom }170 if let pane = tab.focusedPane ?? tab.root.firstPane, let s = session(for: pane) { return s.displayTitle }171 return "Terminal"172 }173174 var allSessions: [TerminalSession] { registry.orderedSessions }175176 // MARK: - Mutation plumbing177178 private func mutate(_ body: (inout WorkspaceState) -> Void) {179 var ws = workspace180 body(&ws)181 ws.updatedAt = Date()182 workspace = ws183 changeHandler?()184 }185186 private func mutateWindow(_ id: WindowID, _ body: (inout WindowState) -> Void) {187 mutate { ws in188 guard let i = ws.windowIndex(id) else { return }189 body(&ws.windows[i])190 }191 }192193 private func mutateTab(_ id: TabID, _ body: (inout TabState) -> Void) {194 mutate { ws in195 for wi in ws.windows.indices {196 if let ti = ws.windows[wi].tabs.firstIndex(where: { $0.id == id }) {197 body(&ws.windows[wi].tabs[ti])198 return199 }200 }201 }202 }203204 private func defaultProfile() -> SessionProfile {205 SessionProfile(kind: .shell(path: nil), workingDirectory: settings.defaultWorkingDirectory)206 }207208 /// New panes inherit the working directory of the pane they were split from.209 func inheritedProfile(from pane: PaneID?) -> SessionProfile {210 var profile = defaultProfile()211 if let pane, let session = session(for: pane) {212 session.probeOnce() // refresh cwd from the kernel right now (the periodic probe may be up to 2 s stale)213 if let cwd = session.currentDirectory { profile.workingDirectory = cwd }214 }215 return profile216 }217218 /// Creates the session for a pane if it is not running yet. Idempotent.219 @discardableResult220 func ensureSession(for paneID: PaneID) -> TerminalSession? {221 guard let pane = workspace.panes[paneID] else { return nil }222 if let existing = registry.session(pane.sessionID) { return existing }223 return registry.create(id: pane.sessionID, profile: pane.profile, paneID: paneID)224 }225226 // MARK: - Windows227228 /// Makes sure at least one window exists and returns the primary one.229 @discardableResult230 func ensurePrimaryWindow() -> WindowID {231 if let id = primaryWindowID { return id }232 return createWindow(open: false)233 }234235 /// Adds a window to the workspace. When `open` is true the app opens the SwiftUI window.236 @discardableResult237 func createWindow(withPane paneID: PaneID? = nil, profile: SessionProfile? = nil, open: Bool = true, role: String? = nil, geometry: WindowGeometry? = nil) -> WindowID {238 let pane: PaneState239 if let paneID, let existing = workspace.panes[paneID] {240 pane = existing241 } else {242 pane = PaneState(profile: profile ?? defaultProfile())243 }244 let tab = TabState(root: .leaf(pane.id))245 let window = WindowState(tabs: [tab], geometry: geometry, role: role)246 mutate { ws in247 ws.panes[pane.id] = pane248 ws.windows.append(window)249 }250 if open { pendingWindowsToOpen.append(window.id) }251 return window.id252 }253254 /// The user closed a window: its sessions end (processes are terminated) unless they were moved out.255 func windowDidClose(_ id: WindowID) {256 guard let w = window(id) else { return }257 let panes = w.tabs.flatMap(\.paneIDs)258 mutate { ws in ws.windows.removeAll { $0.id == id } }259 for pane in panes { removePaneRecord(pane, terminate: true) }260 if keyWindow == id { keyWindow = workspace.windows.first?.id }261 windowsToDismiss.remove(id)262 }263264 func updateGeometry(_ id: WindowID, _ geometry: WindowGeometry) {265 guard let w = window(id), w.geometry != geometry else { return }266 mutateWindow(id) { $0.geometry = geometry }267 }268269 func windowBecameKey(_ id: WindowID) {270 keyWindow = id271 // Give the keyboard to the window's focused pane if nothing in it has focus yet.272 if focusRequest == nil, let pane = focusedPane(in: id), session(for: pane)?.hasFocus != true {273 focusRequest = pane274 }275 }276277 // MARK: - Tabs278279 @discardableResult280 func newTab(in windowID: WindowID, profile: SessionProfile? = nil) -> TabID? {281 guard window(windowID) != nil else { return nil }282 let inherited = profile ?? inheritedProfile(from: focusedPane(in: windowID))283 let pane = PaneState(profile: inherited)284 let tab = TabState(root: .leaf(pane.id))285 mutate { ws in286 ws.panes[pane.id] = pane287 guard let i = ws.windowIndex(windowID) else { return }288 ws.windows[i].tabs.append(tab)289 ws.windows[i].selectedTab = tab.id290 }291 focusRequest = pane.id292 return tab.id293 }294295 func selectTab(_ tabID: TabID, in windowID: WindowID) {296 mutateWindow(windowID) { w in297 if w.tabs.contains(where: { $0.id == tabID }) { w.selectedTab = tabID }298 }299 if let pane = focusedPane(in: windowID) { focusRequest = pane }300 }301302 func selectTab(index: Int, in windowID: WindowID) {303 guard let w = window(windowID), w.tabs.indices.contains(index) else { return }304 selectTab(w.tabs[index].id, in: windowID)305 }306307 func selectAdjacentTab(in windowID: WindowID, offset: Int) {308 guard let w = window(windowID), w.tabs.count > 1,309 let current = w.tabs.firstIndex(where: { $0.id == w.selectedTab }) else { return }310 let next = (current + offset + w.tabs.count) % w.tabs.count311 selectTab(w.tabs[next].id, in: windowID)312 }313314 func renameTab(_ tabID: TabID, title: String?) {315 mutateTab(tabID) { $0.customTitle = title?.isEmpty == true ? nil : title }316 }317318 func moveTab(_ tabID: TabID, in windowID: WindowID, by offset: Int) {319 mutateWindow(windowID) { w in320 guard let i = w.tabs.firstIndex(where: { $0.id == tabID }) else { return }321 let j = max(0, min(w.tabs.count - 1, i + offset))322 guard i != j else { return }323 let t = w.tabs.remove(at: i)324 w.tabs.insert(t, at: j)325 }326 }327328 /// Closes a tab and terminates every session in it.329 func closeTab(_ tabID: TabID) {330 guard let windowID = workspace.location(ofTab: tabID), let tab = tab(tabID) else { return }331 let panes = tab.paneIDs332 mutateWindow(windowID) { w in333 guard let i = w.tabs.firstIndex(where: { $0.id == tabID }) else { return }334 w.tabs.remove(at: i)335 if w.selectedTab == tabID {336 w.selectedTab = w.tabs.isEmpty ? nil : w.tabs[min(i, w.tabs.count - 1)].id337 }338 }339 for pane in panes { removePaneRecord(pane, terminate: true) }340 closeWindowIfEmpty(windowID)341 }342343 private func closeWindowIfEmpty(_ windowID: WindowID) {344 guard let w = window(windowID), w.tabs.isEmpty else { return }345 mutate { ws in ws.windows.removeAll { $0.id == windowID } }346 windowsToDismiss.insert(windowID)347 if keyWindow == windowID { keyWindow = workspace.windows.first?.id }348 }349350 // MARK: - Panes351352 /// Splits `paneID` (or the focused pane) and starts a new session in the new pane.353 @discardableResult354 func split(_ paneID: PaneID? = nil, in windowID: WindowID, axis: SplitAxis, profile: SessionProfile? = nil) -> PaneID? {355 guard let tab = selectedTab(in: windowID), let target = paneID ?? tab.focusedPane ?? tab.root.firstPane else { return nil }356 let pane = PaneState(profile: profile ?? inheritedProfile(from: target))357 mutate { ws in358 ws.panes[pane.id] = pane359 }360 mutateTab(tab.id) { t in361 t.root = t.root.splitting(target, axis: axis, newPane: pane.id)362 t.focusedPane = pane.id363 t.maximizedPane = nil364 }365 focusRequest = pane.id366 return pane.id367 }368369 /// Whether closing this pane would kill a running foreground program (not just an idle shell).370 func closeNeedsConfirmation(_ paneID: PaneID) -> Bool {371 guard settings.confirmCloseRunningProcess, let session = session(for: paneID) else { return false }372 return session.hasForegroundProcess373 }374375 /// Removes the pane from its layout and terminates its session.376 func closePane(_ paneID: PaneID) {377 guard let location = workspace.location(ofPane: paneID), let tab = tab(location.tab) else {378 removePaneRecord(paneID, terminate: true)379 return380 }381 if tab.root.paneCount == 1 {382 closeTab(tab.id)383 return384 }385 let nextFocus = tab.neighborForFocus(after: paneID)386 mutateTab(tab.id) { t in387 if let root = t.root.removing(paneID) { t.root = root }388 if t.focusedPane == paneID { t.focusedPane = nextFocus }389 if t.maximizedPane == paneID { t.maximizedPane = nil }390 }391 removePaneRecord(paneID, terminate: true)392 if let nextFocus { focusRequest = nextFocus }393 }394395 private func removePaneRecord(_ paneID: PaneID, terminate: Bool) {396 guard let pane = workspace.panes[paneID] else { return }397 mutate { ws in ws.panes.removeValue(forKey: paneID) }398 if terminate { registry.close(pane.sessionID) }399 }400401 func focus(pane paneID: PaneID) {402 guard let location = workspace.location(ofPane: paneID) else { return }403 mutateWindow(location.window) { w in404 w.selectedTab = location.tab405 if let ti = w.tabs.firstIndex(where: { $0.id == location.tab }) {406 w.tabs[ti].focusedPane = paneID407 if let max = w.tabs[ti].maximizedPane, max != paneID { w.tabs[ti].maximizedPane = nil }408 }409 }410 focusRequest = paneID411 }412413 /// Records that the terminal in `paneID` became first responder (user click). No layout change.414 func noteFocus(pane paneID: PaneID) {415 guard let location = workspace.location(ofPane: paneID), let tab = tab(location.tab), tab.focusedPane != paneID else { return }416 mutateTab(location.tab) { $0.focusedPane = paneID }417 }418419 func focusNeighbor(_ direction: NavigationDirection, in windowID: WindowID) {420 guard let tab = selectedTab(in: windowID), let current = tab.focusedPane ?? tab.root.firstPane,421 let next = tab.root.neighbor(of: current, direction: direction) else { return }422 focus(pane: next)423 }424425 func focusNextPane(in windowID: WindowID, reverse: Bool = false) {426 guard let tab = selectedTab(in: windowID), let current = tab.focusedPane ?? tab.root.firstPane,427 let next = tab.root.nextPane(after: current, reverse: reverse) else { return }428 focus(pane: next)429 }430431 func toggleMaximize(in windowID: WindowID) {432 guard let tab = selectedTab(in: windowID), let current = tab.focusedPane ?? tab.root.firstPane, tab.root.paneCount > 1 else { return }433 mutateTab(tab.id) { $0.maximizedPane = $0.maximizedPane == current ? nil : current }434 focusRequest = current435 }436437 func swapFocusedPane(_ direction: NavigationDirection, in windowID: WindowID) {438 guard let tab = selectedTab(in: windowID), let current = tab.focusedPane ?? tab.root.firstPane,439 let other = tab.root.neighbor(of: current, direction: direction) else { return }440 mutateTab(tab.id) { $0.root = $0.root.swapping(current, other) }441 focusRequest = current442 }443444 func equalizePanes(in windowID: WindowID) {445 guard let tab = selectedTab(in: windowID) else { return }446 mutateTab(tab.id) { $0.root = $0.root.equalized() }447 }448449 func resize(split: SplitID, divider: Int, delta: Double, tab tabID: TabID) {450 mutateTab(tabID) { $0.root = $0.root.resizing(split: split, dividerIndex: divider, delta: delta) }451 }452453 /// Moves a live pane into another window as a new tab. The session and its PTY are untouched:454 /// only the layout record changes and SwiftUI reparents the terminal view.455 func movePane(_ paneID: PaneID, toWindow targetID: WindowID) {456 guard let location = workspace.location(ofPane: paneID), location.window != targetID,457 window(targetID) != nil, let tab = tab(location.tab) else { return }458 let nextFocus = tab.neighborForFocus(after: paneID)459 mutate { ws in460 // Remove from source tab/window461 if let wi = ws.windowIndex(location.window), let ti = ws.windows[wi].tabs.firstIndex(where: { $0.id == location.tab }) {462 if let root = ws.windows[wi].tabs[ti].root.removing(paneID) {463 ws.windows[wi].tabs[ti].root = root464 if ws.windows[wi].tabs[ti].focusedPane == paneID { ws.windows[wi].tabs[ti].focusedPane = nextFocus }465 if ws.windows[wi].tabs[ti].maximizedPane == paneID { ws.windows[wi].tabs[ti].maximizedPane = nil }466 } else {467 ws.windows[wi].tabs.remove(at: ti)468 if ws.windows[wi].selectedTab == location.tab {469 ws.windows[wi].selectedTab = ws.windows[wi].tabs.isEmpty ? nil : ws.windows[wi].tabs[min(ti, ws.windows[wi].tabs.count - 1)].id470 }471 }472 }473 // Add to target window as a new tab474 if let wi = ws.windowIndex(targetID) {475 let newTab = TabState(root: .leaf(paneID))476 ws.windows[wi].tabs.append(newTab)477 ws.windows[wi].selectedTab = newTab.id478 }479 }480 closeWindowIfEmpty(location.window)481 focusRequest = paneID482 }483484 /// Detaches a live pane into a brand-new window.485 func movePaneToNewWindow(_ paneID: PaneID) {486 guard workspace.location(ofPane: paneID) != nil else { return }487 // Create an empty window record (no placeholder pane), then move the pane into it as its first tab.488 let window = WindowState(tabs: [])489 mutate { ws in ws.windows.append(window) }490 movePane(paneID, toWindow: window.id)491 pendingWindowsToOpen.append(window.id)492 }493494 /// Moves a pane next to another pane (drag and drop inside a tab or across tabs of the same window).495 func movePane(_ paneID: PaneID, nextTo target: PaneID, axis: SplitAxis, after: Bool) {496 guard paneID != target, let src = workspace.location(ofPane: paneID), let dst = workspace.location(ofPane: target) else { return }497 if src.tab == dst.tab {498 mutateTab(src.tab) { t in499 t.root = t.root.moving(paneID, nextTo: target, axis: axis, after: after)500 t.focusedPane = paneID501 }502 } else {503 guard let srcTab = tab(src.tab) else { return }504 let nextFocus = srcTab.neighborForFocus(after: paneID)505 mutate { ws in506 for wi in ws.windows.indices {507 if let ti = ws.windows[wi].tabs.firstIndex(where: { $0.id == src.tab }) {508 if let root = ws.windows[wi].tabs[ti].root.removing(paneID) {509 ws.windows[wi].tabs[ti].root = root510 if ws.windows[wi].tabs[ti].focusedPane == paneID { ws.windows[wi].tabs[ti].focusedPane = nextFocus }511 } else {512 ws.windows[wi].tabs.remove(at: ti)513 if ws.windows[wi].selectedTab == src.tab { ws.windows[wi].selectedTab = ws.windows[wi].tabs.first?.id }514 }515 }516 if let ti = ws.windows[wi].tabs.firstIndex(where: { $0.id == dst.tab }) {517 ws.windows[wi].tabs[ti].root = ws.windows[wi].tabs[ti].root.splitting(target, axis: axis, newPane: paneID, after: after)518 ws.windows[wi].tabs[ti].focusedPane = paneID519 ws.windows[wi].selectedTab = dst.tab520 }521 }522 }523 closeWindowIfEmpty(src.window)524 }525 focusRequest = paneID526 }527528 // MARK: - Sessions529530 func restartSession(in paneID: PaneID) {531 guard let pane = workspace.panes[paneID] else { return }532 if let session = registry.session(pane.sessionID), !session.status.isAlive {533 session.restart()534 } else if registry.session(pane.sessionID) == nil {535 ensureSession(for: paneID)536 }537 }538539 func setLabel(_ label: String?, for paneID: PaneID) {540 guard let pane = workspace.panes[paneID] else { return }541 registry.session(pane.sessionID)?.updateLabel(label)542 mutate { ws in ws.panes[paneID]?.profile.label = label?.isEmpty == true ? nil : label }543 }544545 /// Syncs live session facts (cwd) back into the persisted pane profiles before a save.546 func snapshotForPersistence() -> WorkspaceState {547 var ws = workspace548 for (paneID, pane) in ws.panes {549 if let session = registry.session(pane.sessionID) {550 ws.panes[paneID]?.profile = session.profile551 }552 }553 return ws554 }555556 /// Replaces the workspace wholesale (restoration). Does not start sessions; views call `ensureSession`.557 func replaceWorkspace(_ newWorkspace: WorkspaceState) {558 var ws = newWorkspace559 // A restored Claude pane resumes its conversation (`claude --resume <id>`): that is Claude's own560 // persistence, and the banner says "reattached". Everything else is a brand-new process.561 for (paneID, pane) in ws.panes {562 if case .claude(let id, _) = pane.profile.kind { ws.panes[paneID]?.profile.kind = .claude(sessionID: id, resume: true) }563 }564 workspace = ws565 for (paneID, pane) in ws.panes {566 restorationNotes[paneID] = pane.profile.kind.reattaches ? .reattached : .layoutOnlyNewProcess567 }568 changeHandler?()569 // Start every restored session now (hidden tabs included) so the sidebar reflects reality,570 // then drop the banners after a while. Claude panes wait for CLI discovery (see startDeferredClaudePanes).571 Task { @MainActor [weak self] in572 guard let self else { return }573 for (paneID, pane) in ws.panes {574 if case .claude = pane.profile.kind, self.claude.cli == nil { continue }575 self.ensureSession(for: paneID)576 }577 try? await Task.sleep(for: .seconds(20))578 self.restorationNotes.removeAll()579 }580 }581582 func dismissRestorationNote(_ paneID: PaneID) {583 restorationNotes.removeValue(forKey: paneID)584 }585586 func terminateAllSessions() {587 registry.terminateAll()588 }589}590591struct WeakWindow { weak var window: NSWindow? }592593extension TabState {594 /// The pane that should take focus when `pane` disappears: a geometric neighbour, else DFS next.595 func neighborForFocus(after pane: PaneID) -> PaneID? {596 for direction in [NavigationDirection.left, .right, .up, .down] {597 if let n = root.neighbor(of: pane, direction: direction) { return n }598 }599 return root.nextPane(after: pane)600 }601}602