Repository Public
1import SwiftUI2import TerminalCore3import WorkspaceCore45/// Lays out a tab's pane tree. Panes are positioned absolutely from the tree geometry so every6/// pane keeps its SwiftUI identity (keyed by `PaneID`) across splits, swaps and resizes — the7/// hosted terminal view is therefore reparented at most once and never recreated.8struct PaneCanvasView: View {9 @Environment(WorkspaceController.self) private var controller10 let windowID: WindowID11 let tab: TabState1213 var body: some View {14 GeometryReader { proxy in15 let bounds = CGRect(origin: .zero, size: proxy.size)16 let frames: [PaneID: CGRect] = {17 if let max = tab.maximizedPane, tab.root.contains(max) { return [max: bounds] }18 return tab.root.frames(in: bounds, dividerThickness: NXTokens.dividerThickness)19 }()20 let dividers: [DividerGeometry] = tab.maximizedPane == nil ? tab.root.dividers(in: bounds, dividerThickness: NXTokens.dividerThickness) : [DividerGeometry]()21 ZStack(alignment: .topLeading) {22 ForEach(tab.paneIDs, id: \.self) { paneID in23 if let frame = frames[paneID] {24 PaneView(windowID: windowID, tabID: tab.id, paneID: paneID,25 isFocused: (tab.focusedPane ?? tab.root.firstPane) == paneID,26 showsChrome: tab.paneIDs.count > 1)27 .frame(width: frame.width, height: frame.height)28 .offset(x: frame.minX, y: frame.minY)29 } else {30 // Hidden (another pane is maximized): keep the view alive but out of sight.31 PaneView(windowID: windowID, tabID: tab.id, paneID: paneID, isFocused: false, showsChrome: false)32 .frame(width: 0, height: 0)33 .hidden()34 }35 }36 ForEach(dividers, id: \.self) { divider in37 DividerHandle(divider: divider, tabID: tab.id)38 .frame(width: divider.frame.width, height: divider.frame.height)39 .offset(x: divider.frame.minX, y: divider.frame.minY)40 }41 }42 .frame(width: proxy.size.width, height: proxy.size.height, alignment: .topLeading)43 }44 .background(Color(nsColor: NSColor(hex: controller.settings.appearance.theme.background) ?? .black))45 }46}4748struct DividerHandle: View {49 @Environment(WorkspaceController.self) private var controller50 let divider: DividerGeometry51 let tabID: TabID52 @State private var dragStart: CGFloat?5354 var body: some View {55 Rectangle()56 .fill(NXTokens.separator.opacity(0.6))57 .contentShape(Rectangle())58 .onHover { inside in59 if inside { (divider.axis == .horizontal ? NSCursor.resizeLeftRight : NSCursor.resizeUpDown).push() } else { NSCursor.pop() }60 }61 .gesture(62 DragGesture(minimumDistance: 1, coordinateSpace: .global)63 .onChanged { value in64 let extent = max(divider.splitExtent, 1)65 let delta = divider.axis == .horizontal ? value.translation.width : value.translation.height66 let previous = dragStart ?? 067 let step = (delta - previous) / extent68 dragStart = delta69 controller.resize(split: divider.splitID, divider: divider.dividerIndex, delta: Double(step), tab: tabID)70 }71 .onEnded { _ in dragStart = nil }72 )73 .accessibilityLabel(divider.axis == .horizontal ? "Vertical split divider" : "Horizontal split divider")74 }75}7677/// One pane: the hosted terminal plus a focus ring, an optional label chip and an exit overlay.78struct PaneView: View {79 @Environment(WorkspaceController.self) private var controller80 let windowID: WindowID81 let tabID: TabID82 let paneID: PaneID83 let isFocused: Bool84 let showsChrome: Bool8586 var body: some View {87 let session = controller.ensureSession(for: paneID)88 ZStack(alignment: .topTrailing) {89 if let session {90 terminalHost(session)91 labelChip(session)92 if let note = controller.restorationNotes[paneID] {93 RestorationBanner(kind: note) { controller.dismissRestorationNote(paneID) }94 }95 if !session.status.isAlive {96 ExitedOverlay(session: session, paneID: paneID)97 }98 } else {99 Text("Pane has no session").foregroundStyle(.secondary)100 }101 }102 .overlay(focusRing)103 .contextMenu { contextMenuItems }104 .accessibilityElement(children: .contain)105 .accessibilityLabel("Terminal pane \(session?.displayTitle ?? "")")106 }107108 private func terminalHost(_ session: TerminalSession) -> some View {109 let wantsFocus = controller.focusRequest == paneID110 let confirm: ((String, @escaping @MainActor () -> Void) -> Void)? = controller.settings.pasteProtection ? { text, proceed in pasteConfirmation(text, proceed: proceed) } : nil111 return TerminalHostView(session: session,112 requestFocus: wantsFocus,113 onFocused: { handleFocused() },114 pasteConfirmation: confirm,115 bellStyle: controller.settings.bellStyle)116 }117118 private func handleFocused() {119 controller.noteFocus(pane: paneID)120 if controller.focusRequest == paneID { controller.focusRequest = nil }121 }122123 @ViewBuilder124 private func labelChip(_ session: TerminalSession) -> some View {125 if showsChrome, let label = session.label, !label.isEmpty {126 Text(label)127 .font(.system(size: 10, weight: .medium))128 .padding(.horizontal, 6).padding(.vertical, 2)129 .background(Capsule().fill(.ultraThinMaterial))130 .padding(6)131 .allowsHitTesting(false)132 }133 }134135 @ViewBuilder136 private var contextMenuItems: some View {137 Button("Split Right") { controller.split(paneID, in: windowID, axis: .horizontal) }138 Button("Split Down") { controller.split(paneID, in: windowID, axis: .vertical) }139 Button(controller.tab(tabID)?.maximizedPane == paneID ? "Restore Pane" : "Maximize Pane") {140 controller.focus(pane: paneID); controller.toggleMaximize(in: windowID)141 }142 Divider()143 Menu("Move Pane to Window") {144 ForEach(controller.workspace.windows.filter { $0.id != windowID }) { w in145 Button(windowLabel(w)) { controller.movePane(paneID, toWindow: w.id) }146 }147 Button("New Window") { controller.movePaneToNewWindow(paneID) }148 }149 Divider()150 Button("Close Pane", role: .destructive) { closePane() }151 }152153 private var focusRing: some View {154 let color: Color = (isFocused && showsChrome) ? NXTokens.focusRing : Color(white: 0, opacity: 0)155 return Rectangle().strokeBorder(color, lineWidth: 1).allowsHitTesting(false)156 }157158 private func windowLabel(_ w: WindowState) -> String {159 let titles = w.tabs.prefix(2).map { controller.title(for: $0) }.joined(separator: ", ")160 return titles.isEmpty ? "Window \(w.id.shortString)" : titles161 }162163 private func closePane() {164 if controller.closeNeedsConfirmation(paneID) {165 ConfirmationPresenter.confirm(title: "Close this pane?", message: "A program is still running in it and will be terminated.", action: "Close Pane") {166 controller.closePane(paneID)167 }168 } else {169 controller.closePane(paneID)170 }171 }172173 private func pasteConfirmation(_ text: String, proceed: @escaping @MainActor () -> Void) {174 let lines = text.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline).count175 let preview = text.prefix(300)176 ConfirmationPresenter.confirm(title: "Paste \(lines) lines?",177 message: "The clipboard contains multiple lines; each newline may run a command.\n\n\(preview)\(text.count > 300 ? "…" : "")",178 action: "Paste", destructive: false, onConfirm: proceed)179 }180}181182struct ExitedOverlay: View {183 @Environment(WorkspaceController.self) private var controller184 let session: TerminalSession185 let paneID: PaneID186187 private var isSSH: Bool { if case .ssh = session.profile.kind { return true }; return false }188 private var exitText: String {189 if isSSH, case .exited(let code) = session.status {190 return code == 255 ? "SSH connection lost or refused (exit 255)" : "SSH session ended (\(code.map(String.init) ?? "signal"))"191 }192 return session.status.label193 }194195 var body: some View {196 VStack {197 Spacer()198 HStack(spacing: 10) {199 Image(systemName: isSSH ? "network.slash" : "stop.circle").accessibilityHidden(true)200 Text(exitText).font(.system(size: 12, weight: .medium))201 Button(isSSH ? "Reconnect" : "Restart") { controller.restartSession(in: paneID) }.keyboardShortcut(.defaultAction)202 Button("Close") { controller.closePane(paneID) }203 }204 .padding(10)205 .background(RoundedRectangle(cornerRadius: NXTokens.cornerRadius).fill(.regularMaterial))206 .padding(12)207 }208 .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)209 }210}211212/// Tells the truth about restoration: a re-created shell is not the process that was running before.213struct RestorationBanner: View {214 let kind: RestorationKind215 let dismiss: () -> Void216217 var body: some View {218 HStack(spacing: 8) {219 Image(systemName: kind == .reattached ? "arrow.triangle.2.circlepath" : "doc.on.doc")220 .accessibilityHidden(true)221 Text(kind == .reattached ? "Layout restored · session reattached (tmux/resume)" : "Layout restored · new process started (previous one was not reattached)")222 .font(.system(size: 11))223 Button { dismiss() } label: { Image(systemName: "xmark").font(.system(size: 9, weight: .bold)) }224 .buttonStyle(.borderless)225 .accessibilityLabel("Dismiss")226 }227 .padding(.horizontal, 10).padding(.vertical, 5)228 .background(Capsule().fill(.regularMaterial))229 .padding(8)230 .frame(maxWidth: .infinity, alignment: .topLeading)231 .accessibilityElement(children: .combine)232 }233}234