Files
helder/Sources/HelderUI/Shell/WorkspaceView.swift
T
2026-10-05 09:24:25 +02:00

170 lines
6.5 KiB
Swift

import AppKit
import HelderCore
import SwiftUI
/// The launcher without a project; with one, the title bar, four columns and status bar, overlays above.
struct WorkspaceView: View {
let model: WorkspaceModel
var body: some View {
ZStack {
if model.root == nil {
if model.isReady { LauncherScreen(model: model) } else { Palette.editor }
} else {
workspace
}
ToastStack(toasts: model.toasts)
if model.isFocusFlashVisible { FocusFlash(model: model) }
}
.ignoresSafeArea()
}
private var workspace: some View {
GeometryReader { proxy in
VStack(spacing: 0) {
TitleBarView(model: model)
.zIndex(2)
Columns(model: model)
.frame(maxHeight: .infinity)
.zIndex(1)
StatusBarView(model: model)
}
.overlay { OverlayHost(model: model) }
.overlay {
if model.overlay.current == .launcher { RecentProjectsOverlay(model: model) }
}
.onAppear { model.layout.windowResized(width: proxy.size.width) }
.onChange(of: proxy.size.width) { _, width in model.layout.windowResized(width: width) }
}
}
}
/// The four columns and their splitters. Every column stays mounted: a pane toggle only hides it, so the
/// terminals, the editor buffer and the tree keep their state. The editor is the flex column; with the editor
/// hidden the agent column fills. The agent column is keyed by the project so its terminals respawn on a switch.
private struct Columns: View {
let model: WorkspaceModel
/// The editor's last shown width, kept while it is hidden so its text does not re-wrap.
@State private var editorWidth = Metrics.minEditorWidth
var body: some View {
let layout = model.layout
let v = layout.visibility
let agentFills = v.agent && !v.editor
let hiddenEditorWidth = max(editorWidth, Metrics.minEditorWidth)
HStack(spacing: 0) {
SourceControlColumn(model: model)
.frame(width: layout.widths.sourceControl)
.refreshFlash(model.refreshTick)
.pane(shown: v.sourceControl)
Splitter(onDrag: { layout.dragSourceControl(by: $0) }, onEnd: layout.endDrag)
.pane(shown: v.sourceControl && (v.explorer || v.editor || v.agent))
ExplorerColumn(model: model)
.frame(width: layout.widths.explorer)
.refreshFlash(model.refreshTick)
.pane(shown: v.explorer)
Splitter(onDrag: { layout.dragExplorer(by: $0) }, onEnd: layout.endDrag)
.pane(shown: v.explorer && (v.editor || v.agent))
EditorColumn(model: model)
.frame(minWidth: v.editor ? Metrics.minEditorWidth : hiddenEditorWidth, maxWidth: v.editor ? .infinity : hiddenEditorWidth)
.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in
if v.editor { editorWidth = width }
}
.refreshFlash(model.refreshTick)
.pane(shown: v.editor)
Splitter(onDrag: { layout.dragAgent(by: $0) }, onEnd: layout.endDrag)
.pane(shown: v.editor && v.agent)
AgentColumn(model: model)
.frame(minWidth: agentFills ? Metrics.minEditorWidth : layout.widths.agent,
maxWidth: agentFills ? .infinity : layout.widths.agent)
.id(model.root)
.pane(shown: v.agent)
if !v.editor && !v.agent {
Spacer(minLength: 0)
}
}
.background(Palette.editor)
.background(HiddenPaneFocusGuard(visibility: v))
.clipped()
}
}
/// Hides a column without unmounting it. The hidden column keeps its own width, so a terminal never
/// resizes to nothing, and is parked outside the window, so its AppKit views own no pixels, clicks or cursor.
private struct PaneVisibility: ViewModifier {
static let parkedOffset: CGFloat = 100_000
let shown: Bool
func body(content: Content) -> some View {
content
.frame(width: shown ? nil : 0, alignment: .leading)
.opacity(shown ? 1 : 0)
.allowsHitTesting(shown)
.accessibilityHidden(!shown)
.offset(x: shown ? 0 : Self.parkedOffset)
}
}
extension View {
fileprivate func pane(shown: Bool) -> some View {
modifier(PaneVisibility(shown: shown))
}
}
/// When a column is hidden while it holds the keyboard (a terminal, the code, the commit box), the window
/// takes the keyboard back, so typing never lands in a pane nobody can see.
private struct HiddenPaneFocusGuard: NSViewRepresentable {
let visibility: ColumnVisibility
func makeNSView(context: Context) -> GuardView {
GuardView()
}
func updateNSView(_ view: GuardView, context: Context) {
view.visibility = visibility
}
final class GuardView: NSView {
var visibility: ColumnVisibility? {
didSet {
guard let oldValue, oldValue != visibility else { return }
DispatchQueue.main.async { [weak self] in self?.releaseParkedResponder() }
}
}
override func hitTest(_ point: NSPoint) -> NSView? { nil }
private func releaseParkedResponder() {
guard let window, let responder = window.firstResponder as? NSView, let content = window.contentView else { return }
let view = responder.enclosingScrollView ?? responder
if view.convert(view.bounds, to: nil).minX >= content.bounds.maxX { window.makeFirstResponder(nil) }
}
}
}
/// ⌘R lightens a column for a moment and lets it settle back; each new tick replays it.
private struct RefreshFlash: ViewModifier {
let tick: Int
@State private var strength = 0.0
func body(content: Content) -> some View {
content
.overlay {
Palette.refreshFlash
.blendMode(.plusLighter)
.opacity(strength)
.allowsHitTesting(false)
}
.onChange(of: tick) {
strength = 1
withAnimation(.easeOut(duration: Metrics.refreshFlashDuration)) { strength = 0 }
}
}
}
extension View {
fileprivate func refreshFlash(_ tick: Int) -> some View {
modifier(RefreshFlash(tick: tick))
}
}