Repository Public
1import Foundation2import CoreGraphics34/// What kind of process a pane hosts. Stored so that a workspace can be *re-created*5/// after a restart (layout restoration), which is explicitly not process reattachment —6/// only `tmux`-backed sessions genuinely reattach.7public enum SessionKind: Codable, Sendable, Hashable {8 /// A local interactive shell. `nil` means the user's login shell.9 case shell(path: String?)10 /// The official Claude Code CLI, launched in a PTY with its own session ID.11 case claude(sessionID: UUID, resume: Bool)12 /// An OpenSSH connection to a configured host, optionally attaching a tmux session.13 case ssh(host: HostID, tmuxSession: String?)14 /// A local tmux session (`tmux new-session -A -s name`) that survives app termination.15 case tmux(sessionName: String)16 /// An arbitrary local command (argument array, never a shell string).17 case command(executable: String, arguments: [String])1819 public var isRemote: Bool {20 if case .ssh = self { return true }21 return false22 }2324 public var reattaches: Bool {25 switch self {26 case .tmux: return true27 case .ssh(_, let tmux): return tmux != nil28 case .claude(_, let resume): return resume29 default: return false30 }31 }32}3334/// Everything needed to (re)create the process behind a pane.35public struct SessionProfile: Codable, Sendable, Hashable {36 public var kind: SessionKind37 public var workingDirectory: String?38 /// Explicit user label ("agent-3", "db migration"), never inferred from terminal text.39 public var label: String?40 public var environment: [String: String]4142 public init(kind: SessionKind = .shell(path: nil), workingDirectory: String? = nil, label: String? = nil, environment: [String: String] = [:]) {43 self.kind = kind44 self.workingDirectory = workingDirectory45 self.label = label46 self.environment = environment47 }48}4950/// A pane: a slot in a layout tree that displays exactly one session.51public struct PaneState: Codable, Sendable, Hashable, Identifiable {52 public var id: PaneID53 public var sessionID: SessionID54 public var profile: SessionProfile5556 public init(id: PaneID = PaneID(), sessionID: SessionID = SessionID(), profile: SessionProfile = SessionProfile()) {57 self.id = id58 self.sessionID = sessionID59 self.profile = profile60 }61}6263public struct TabState: Codable, Sendable, Hashable, Identifiable {64 public var id: TabID65 /// User-set title; when nil the UI derives one from the focused session.66 public var customTitle: String?67 public var root: LayoutNode68 public var focusedPane: PaneID?69 public var maximizedPane: PaneID?7071 public init(id: TabID = TabID(), customTitle: String? = nil, root: LayoutNode, focusedPane: PaneID? = nil, maximizedPane: PaneID? = nil) {72 self.id = id73 self.customTitle = customTitle74 self.root = root75 self.focusedPane = focusedPane ?? root.firstPane76 self.maximizedPane = maximizedPane77 }7879 public var paneIDs: [PaneID] { root.paneIDs }80}8182/// Window geometry in AppKit screen coordinates plus the display it was last seen on.83public struct WindowGeometry: Codable, Sendable, Hashable {84 public var frame: CGRect85 /// `CGDirectDisplayID` of the screen the window was on, if known.86 public var displayID: UInt32?87 /// Human-readable display name (e.g. "Studio Display") for restoration messages.88 public var displayName: String?89 public var isFullScreen: Bool9091 public init(frame: CGRect, displayID: UInt32? = nil, displayName: String? = nil, isFullScreen: Bool = false) {92 self.frame = frame93 self.displayID = displayID94 self.displayName = displayName95 self.isFullScreen = isFullScreen96 }97}9899public struct WindowState: Codable, Sendable, Hashable, Identifiable {100 public var id: WindowID101 public var tabs: [TabState]102 public var selectedTab: TabID?103 public var geometry: WindowGeometry?104 /// Optional role used by multi-monitor presets ("terminals", "mission-control", "git").105 public var role: String?106107 public init(id: WindowID = WindowID(), tabs: [TabState], selectedTab: TabID? = nil, geometry: WindowGeometry? = nil, role: String? = nil) {108 self.id = id109 self.tabs = tabs110 self.selectedTab = selectedTab ?? tabs.first?.id111 self.geometry = geometry112 self.role = role113 }114}115116public struct WorkspaceState: Codable, Sendable, Hashable, Identifiable {117 public static let schemaVersion = 1118119 public var id: WorkspaceID120 public var name: String121 public var windows: [WindowState]122 public var panes: [PaneID: PaneState]123 public var createdAt: Date124 public var updatedAt: Date125126 public init(id: WorkspaceID = WorkspaceID(), name: String, windows: [WindowState] = [], panes: [PaneID: PaneState] = [:], createdAt: Date = Date(), updatedAt: Date = Date()) {127 self.id = id128 self.name = name129 self.windows = windows130 self.panes = panes131 self.createdAt = createdAt132 self.updatedAt = updatedAt133 }134135 // MARK: Lookup helpers136137 public func window(_ id: WindowID) -> WindowState? { windows.first { $0.id == id } }138139 public func windowIndex(_ id: WindowID) -> Int? { windows.firstIndex { $0.id == id } }140141 public func location(ofPane pane: PaneID) -> (window: WindowID, tab: TabID)? {142 for window in windows {143 for tab in window.tabs where tab.root.contains(pane) {144 return (window.id, tab.id)145 }146 }147 return nil148 }149150 public func location(ofTab tab: TabID) -> WindowID? {151 windows.first { $0.tabs.contains { $0.id == tab } }?.id152 }153154 public func pane(forSession session: SessionID) -> PaneState? {155 panes.values.first { $0.sessionID == session }156 }157158 /// All sessions referenced by any layout. Sessions not referenced are orphans and may be closed.159 public var referencedSessions: Set<SessionID> {160 var set = Set<SessionID>()161 for window in windows {162 for tab in window.tabs {163 for paneID in tab.paneIDs {164 if let pane = panes[paneID] { set.insert(pane.sessionID) }165 }166 }167 }168 return set169 }170171 /// Validates that every leaf in every tab has a pane record and every pane is referenced once.172 public var isConsistent: Bool {173 var seen = Set<PaneID>()174 for window in windows {175 for tab in window.tabs {176 guard tab.root.isWellFormed else { return false }177 for paneID in tab.paneIDs {178 guard panes[paneID] != nil, !seen.contains(paneID) else { return false }179 seen.insert(paneID)180 }181 }182 }183 return seen.count == panes.count184 }185186 // MARK: Factories187188 public static func single(name: String = "Default", profile: SessionProfile = SessionProfile()) -> WorkspaceState {189 let pane = PaneState(profile: profile)190 let tab = TabState(root: .leaf(pane.id))191 let window = WindowState(tabs: [tab])192 return WorkspaceState(name: name, windows: [window], panes: [pane.id: pane])193 }194}195