SPB Git

Repository Public

TabStripView.swift 4.7 KB 112 lines swift

1import SwiftUI2import WorkspaceCore34struct TabStripView: View {5    @Environment(WorkspaceController.self) private var controller6    let windowID: WindowID7    @State private var renaming: TabID?8    @State private var renameText = ""910    var body: some View {11        let window = controller.window(windowID)12        HStack(spacing: 0) {13            ScrollView(.horizontal, showsIndicators: false) {14                HStack(spacing: 1) {15                    ForEach(window?.tabs ?? []) { tab in16                        tabItem(tab, selected: tab.id == window?.selectedTab)17                    }18                }19            }20            Button { controller.newTab(in: windowID) } label: {21                Image(systemName: "plus").font(.system(size: 11, weight: .medium))22            }23            .buttonStyle(.borderless)24            .padding(.horizontal, 8)25            .help("New Tab (⌘T)")26            .accessibilityLabel("New Tab")27        }28        .frame(height: NXTokens.tabHeight)29        .background(NXTokens.chromeBackground)30    }3132    @ViewBuilder33    private func tabItem(_ tab: TabState, selected: Bool) -> some View {34        let alive = tab.paneIDs.contains { controller.session(for: $0)?.status.isAlive == true }35        HStack(spacing: 6) {36            Image(systemName: NXStatusGlyph.symbol(alive: alive))37                .font(.system(size: 6))38                .foregroundStyle(alive ? Color.green : Color.secondary)39                .accessibilityLabel(alive ? "running" : "exited")40            if renaming == tab.id {41                TextField("Tab name", text: $renameText, onCommit: {42                    controller.renameTab(tab.id, title: renameText)43                    renaming = nil44                })45                .textFieldStyle(.plain)46                .frame(width: 120)47            } else {48                Text(controller.title(for: tab))49                    .font(.system(size: 12, weight: selected ? .medium : .regular))50                    .lineLimit(1)51                    .frame(maxWidth: 180)52            }53            if tab.paneIDs.count > 1 {54                Text("\(tab.paneIDs.count)")55                    .font(.system(size: 9, weight: .medium))56                    .padding(.horizontal, 4).padding(.vertical, 1)57                    .background(Capsule().fill(Color.secondary.opacity(0.2)))58                    .accessibilityLabel("\(tab.paneIDs.count) panes")59            }60            Button { closeTab(tab) } label: {61                Image(systemName: "xmark").font(.system(size: 9, weight: .bold))62            }63            .buttonStyle(.borderless)64            .foregroundStyle(.secondary)65            .accessibilityLabel("Close Tab")66        }67        .padding(.horizontal, 10)68        .frame(height: NXTokens.tabHeight)69        .background(selected ? NXTokens.panelBackground : Color.clear)70        .overlay(alignment: .bottom) {71            if selected { Rectangle().fill(NXTokens.accent).frame(height: 2) }72        }73        .contentShape(Rectangle())74        .onTapGesture { controller.selectTab(tab.id, in: windowID) }75        .contextMenu {76            Button("Rename…") { renameText = tab.customTitle ?? ""; renaming = tab.id }77            Button("Move Left") { controller.moveTab(tab.id, in: windowID, by: -1) }78            Button("Move Right") { controller.moveTab(tab.id, in: windowID, by: 1) }79            Divider()80            Button("Close Tab", role: .destructive) { closeTab(tab) }81        }82        .accessibilityElement(children: .combine)83        .accessibilityAddTraits(selected ? [.isSelected] : [])84    }8586    private func closeTab(_ tab: TabState) {87        let needsConfirm = tab.paneIDs.contains { controller.closeNeedsConfirmation($0) }88        if needsConfirm {89            ConfirmationPresenter.confirm(title: "Close this tab?", message: "A program is still running in one of its panes. Closing the tab will terminate it.", action: "Close Tab") {90                controller.closeTab(tab.id)91            }92        } else {93            controller.closeTab(tab.id)94        }95    }96}9798/// AppKit alert helper for destructive confirmations (process termination, dangerous git actions).99@MainActor100enum ConfirmationPresenter {101    static func confirm(title: String, message: String, action: String, destructive: Bool = true, onConfirm: @escaping @MainActor () -> Void) {102        let alert = NSAlert()103        alert.messageText = title104        alert.informativeText = message105        alert.alertStyle = destructive ? .warning : .informational106        let confirm = alert.addButton(withTitle: action)107        if destructive { confirm.hasDestructiveAction = true }108        alert.addButton(withTitle: "Cancel")109        if alert.runModal() == .alertFirstButtonReturn { onConfirm() }110    }111}112
Files main
Menu

Theme