Files
helder/Sources/HelderUI/Shell/TitleBarView.swift
T
2026-10-23 09:40:20 +02:00

173 lines
7.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import HelderCore
import SwiftUI
/// 44pt bar: traffic-light inset, wordmark, project · branch, the open file's breadcrumb, the pane toggles
/// centred on the window, and the toolbar.
struct TitleBarView: View {
let model: WorkspaceModel
var body: some View {
TitleBarLayout(leading: model.layout.isFullScreen ? 14 : Metrics.trafficLightInset, trailing: 14, gap: Metrics.titleBarGap) {
HStack(alignment: .firstTextBaseline, spacing: Metrics.titleClusterGap) {
Wordmark()
ProjectName(name: model.name) { model.perform(.openFolder) }
Text("·").typeRole(Typography.path).foregroundStyle(Palette.text4)
Text(model.sourceControl?.state.branch ?? "—")
.typeRole(Typography.branch)
.foregroundStyle(Palette.amber)
.lineLimit(1)
}
.fixedSize()
Breadcrumb(path: model.editor?.activePath, isDirty: model.editor.map { e in e.activePath.map(e.isDirty) ?? false } ?? false)
.frame(maxWidth: .infinity, alignment: .leading)
PaneToggles(model: model)
TitleToolbar(model: model)
}
.frame(height: Metrics.titleBar)
.background(Palette.panel)
.hairline(.bottom)
}
}
/// Lays out the four title-bar parts: the pane toggles sit on the window's centre, and the breadcrumb
/// gets only the room left of them. The toggles move left only when the toolbar would run into them.
private struct TitleBarLayout: Layout {
var leading: CGFloat
var trailing: CGFloat
var gap: CGFloat
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
CGSize(width: proposal.width ?? 0, height: proposal.height ?? Metrics.titleBar)
}
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
guard subviews.count == 4 else { return }
let (cluster, crumb, toggles, toolbar) = (subviews[0], subviews[1], subviews[2], subviews[3])
let y = bounds.midY
let clusterSize = cluster.sizeThatFits(.unspecified)
let togglesSize = toggles.sizeThatFits(.unspecified)
let toolbarSize = toolbar.sizeThatFits(.unspecified)
let clusterX = bounds.minX + leading
let toolbarX = bounds.maxX - trailing - toolbarSize.width
let togglesX = min(bounds.midX - togglesSize.width / 2, toolbarX - gap - togglesSize.width)
let crumbX = clusterX + clusterSize.width + gap
cluster.place(at: CGPoint(x: clusterX, y: y), anchor: .leading, proposal: ProposedViewSize(clusterSize))
toggles.place(at: CGPoint(x: togglesX, y: y), anchor: .leading, proposal: ProposedViewSize(togglesSize))
toolbar.place(at: CGPoint(x: toolbarX, y: y), anchor: .leading, proposal: ProposedViewSize(toolbarSize))
crumb.place(at: CGPoint(x: crumbX, y: y), anchor: .leading,
proposal: ProposedViewSize(width: max(0, togglesX - gap - crumbX), height: bounds.height))
}
}
private struct ProjectName: View {
let name: String
let action: () -> Void
@State private var isHovered = false
var body: some View {
Text(name)
.typeRole(Typography.path)
.foregroundStyle(isHovered ? Palette.text2 : Palette.text4)
.lineLimit(1)
.onHover { isHovered = $0 }
.pointerHand()
.onTapGesture(perform: action)
.help("Open folder…")
.animation(Metrics.colorTransition, value: isHovered)
}
}
/// `resources › views › form.blade.php`: segments in text-4, the file name in text-1 600, a dot when unsaved.
private struct Breadcrumb: View {
let path: String?
let isDirty: Bool
var body: some View {
if let path {
let segments = path.split(separator: "/").map(String.init)
HStack(spacing: Metrics.crumbGap) {
ForEach(Array(segments.enumerated()), id: \.offset) { index, segment in
if index > 0 { Text("›").foregroundStyle(Palette.text4) }
if index == segments.count - 1 {
Text(segment).typeRole(Typography.crumbLeaf).foregroundStyle(Palette.text1).layoutPriority(1)
} else {
Text(segment).foregroundStyle(Palette.text4)
}
}
if isDirty {
Text("●").font(.system(size: 10)).foregroundStyle(Palette.amber).padding(.leading, 4).help("Unsaved changes")
}
}
.typeRole(Typography.path)
.lineLimit(1)
.truncationMode(.head)
} else {
Spacer(minLength: 0)
}
}
}
/// Git · Explorer · File · Claude: one toggle per column. Hiding a column keeps everything inside it.
private struct PaneToggles: View {
let model: WorkspaceModel
var body: some View {
ButtonGroup([
toggle("Git", .sourceControl, column: "Source Control"),
toggle("Explorer", .explorer, column: "Explorer"),
toggle("File", .editor, column: "File"),
toggle("Claude", .terminal, column: "Claude"),
])
}
private func toggle(_ label: String, _ panel: Panel, column: String) -> GroupItem {
let shown = model.layout.isVisible(panel)
let tip = shown
? Tooltip.stated(column + " column: ", "shown", ". Click to hide it.")
: Tooltip.stated(column + " column: ", "hidden", ". Click to show it.")
return GroupItem(label, kind: .toggle(isOn: shown), tip: tip) {
model.perform(.togglePane(panel))
}
}
}
/// Search ⌘F · Fluid ⌘L · Hidden ⌘. · Note ⌘N, then `?`. Search and Note are actions; Fluid and Hidden toggle.
/// While Helder reads aloud, a Stop toggle leads the group; switching it off stops the reading.
private struct TitleToolbar: View {
let model: WorkspaceModel
var body: some View {
let layout = model.layout
HStack(spacing: Metrics.toolbarIconGap) {
ButtonGroup(stop + [
GroupItem("Search", keys: "⌘F", kind: .action,
tip: Text("Search the open file, the project and the file names.")) { model.perform(.showSearch(seed: "")) },
GroupItem("Fluid", keys: "⌘L", kind: .toggle(isOn: layout.fluid),
tip: layout.fluid
? Tooltip.stated("Column widths: ", "fluid", " — the columns re-fit on resize and focus. Click to lock them.")
: Tooltip.stated("Column widths: ", "locked", " — the columns keep their size. Click to make them fluid.")) {
model.perform(.toggleAutoFit)
},
GroupItem("Hidden", keys: "⌘.", kind: .toggle(isOn: layout.showHidden),
tip: layout.showHidden
? Tooltip.stated("Hidden files: ", "shown", " — dotfiles appear in the tree and the search. Click to hide them.")
: Tooltip.stated("Hidden files: ", "hidden", " — dotfiles are excluded from the tree and the search. Click to show them.")) {
model.perform(.toggleHidden)
},
GroupItem("Note", keys: "⌘N", kind: .action,
tip: Text("Project note (.notes.txt) — kept next to this project.")) { model.perform(.showNote) },
], tipEdge: .trailing)
ToolbarIconButton("?") { model.perform(.showShortcuts) }
.tip("Keyboard shortcuts.", edge: .trailing)
}
.fixedSize()
}
private var stop: [GroupItem] {
guard model.readAloud.isReading else { return [] }
return [GroupItem("Stop", kind: .toggle(isOn: true), tip: Tooltip.stated("Read aloud: ", "playing", ". Click to stop.")) {
model.perform(.stopReading)
}]
}
}