SPB Git

Repository Public

SidebarView.swift 6.3 KB 126 lines swift

1import DesignSystem2import SwiftUI3import WorkspaceKit45/// Overview of every window, tab and session, plus saved workspaces and presets. It has no6/// background of its own: it sits directly on the chrome material.7struct SidebarView: View {8    @Bindable var state: WindowState9    let model: AppModel10    let palette: Palette1112    var body: some View {13        let _ = model.tick14        ScrollView {15            VStack(alignment: .leading, spacing: 16) {16                section("Sessions", icon: "terminal") {17                    ForEach(Array(model.windows.enumerated()), id: \.element.id) { index, window in18                        VStack(alignment: .leading, spacing: 2) {19                            HStack {20                                Text("Window \(index + 1)" + (window.id == state.id ? " · this window" : ""))21                                    .font(.system(size: TypeScale.caption, weight: .medium)).foregroundStyle(palette.secondary)22                                Spacer()23                                if window.id != state.id {24                                    IconButton(systemName: "arrow.down.left.and.arrow.up.right.square", help: "Merge into this window", size: 10) {25                                        model.mergeWindow(window, into: state)26                                    }27                                }28                            }29                            .padding(.horizontal, 6)30                            ForEach(window.tabs) { tab in31                                ForEach(tab.layout.panes, id: \.self) { pane in32                                    sessionRow(pane, tab: tab, window: window)33                                }34                            }35                        }36                    }37                }38                section("Presets", icon: "square.grid.2x2") {39                    ForEach(WorkspacePreset.allCases) { preset in40                        row(icon: "square.grid.2x2", title: preset.title, help: preset.summary) {41                            if preset == .sshFleet { state.sheet = .fleetPicker } else { model.open(preset: preset, replacing: state) }42                        }43                    }44                }45                section("Saved Workspaces", icon: "rectangle.3.group") {46                    if model.savedWorkspaces.isEmpty {47                        EmptyStateView(icon: "rectangle.3.group", title: "No saved workspace",48                                       message: "Arrange windows and panes, then press ⌘S to keep the layout.", palette: palette)49                    }50                    ForEach(model.savedWorkspaces) { workspace in51                        HStack(spacing: 4) {52                            row(icon: "rectangle.3.group", title: workspace.name,53                                help: "\(workspace.windows.count) window(s), \(workspace.paneCount) session(s). Opens new processes in the saved layout.") {54                                model.open(workspace: workspace)55                            }56                            IconButton(systemName: "trash", help: "Delete workspace", size: 10) { model.deleteWorkspace(workspace.id) }57                        }58                    }59                }60                section("Displays", icon: "display") {61                    ForEach(model.screens()) { screen in62                        row(icon: "display", title: screen.name + (screen.isMain ? " (main)" : ""),63                            help: "\(Int(screen.frame.width))×\(Int(screen.frame.height)) — click to open a window on this display") {64                            model.openWindow(on: screen.id)65                        }66                    }67                }68            }69            .padding(.horizontal, 8).padding(.vertical, 10)70        }71        .accessibilityLabel("Sidebar")72    }7374    private func section<Content: View>(_ title: String, icon: String, @ViewBuilder content: () -> Content) -> some View {75        VStack(alignment: .leading, spacing: 3) {76            Text(title.uppercased())77                .font(.system(size: TypeScale.caption, weight: .semibold)).tracking(0.6)78                .foregroundStyle(palette.secondary)79                .padding(.horizontal, 6).padding(.bottom, 2)80                .accessibilityAddTraits(.isHeader)81            content()82        }83    }8485    private func sessionRow(_ pane: PaneID, tab: TabModel, window: WindowState) -> some View {86        let session = model.session(for: pane)87        let panePalette = model.palette(for: pane)88        let active = window.id == state.id && window.selectedTabID == tab.id && tab.focusedPane == pane89        return Button {90            model.selectTab(tab.id, in: window)91            model.focus(pane, in: window)92            window.controller?.window?.makeKeyAndOrderFront(nil)93        } label: {94            HStack(spacing: 7) {95                StatusDot(activity: SessionActivity(session), palette: panePalette, pulse: model.completions[pane]?.id)96                Text(model.paneTitle(pane)).font(.system(size: TypeScale.body, weight: active ? .medium : .regular)).lineLimit(1).truncationMode(.middle)97                Spacer(minLength: 2)98                if session?.hasForegroundJob == true, let name = session?.foregroundProcessName {99                    Text(name).font(.system(size: TypeScale.caption)).foregroundStyle(palette.secondary).lineLimit(1)100                }101            }102            .padding(.horizontal, 8).padding(.vertical, 4)103            .contentShape(Rectangle())104            .hoverRow(palette, selected: active)105        }106        .buttonStyle(.plain)107        .accessibilityLabel("Session \(model.paneTitle(pane)), \(SessionActivity(session).label)")108        .accessibilityAddTraits(active ? .isSelected : [])109    }110111    private func row(icon: String, title: String, help: String, action: @escaping () -> Void) -> some View {112        Button(action: action) {113            HStack(spacing: 7) {114                Image(systemName: icon).font(.system(size: TypeScale.footnote)).frame(width: 16).foregroundStyle(palette.secondary)115                Text(title).font(.system(size: TypeScale.body)).lineLimit(1)116                Spacer(minLength: 0)117            }118            .padding(.horizontal, 8).padding(.vertical, 4)119            .contentShape(Rectangle())120            .hoverRow(palette)121        }122        .buttonStyle(.plain)123        .help(help)124    }125}126
Files main
Menu

Theme