Files
helder/Sources/HelderUI/Shell/TitleBarView.swift
T

193 lines
8.7 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)
}
}
}
/// 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("Explorer", .explorer),
toggle("File", .editor),
toggle("Claude", .terminal),
])
}
private func toggle(_ label: String, _ panel: Panel) -> GroupItem {
let shown = model.layout.isVisible(panel)
let tip = shown
? Tooltip.stated(label + " column: ", "shown", ". Click to hide it.")
: Tooltip.stated(label + " column: ", "hidden", ". Click to show it.")
return GroupItem(label, kind: .toggle(isOn: shown), tip: tip) {
model.perform(.togglePane(panel))
}
}
}
/// Git ⌘G · Schema ⌘D · Search ⌘F · Fluid ⌘L · Hidden ⌘., then `?`. Search is an action; the others toggle.
/// Schema shows only in a Laravel app. While Helder reads aloud, a Stop toggle leads the group.
private struct TitleToolbar: View {
let model: WorkspaceModel
var body: some View {
let layout = model.layout
HStack(spacing: Metrics.toolbarIconGap) {
ButtonGroup(stop + [git] + schema + [
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)
},
], tipEdge: .trailing)
ToolbarIconButton("?") { model.perform(.showShortcuts) }
.tip("Keyboard shortcuts.", edge: .trailing)
}
.fixedSize()
}
/// N counts changed files, a file both staged and unstaged once; the dot lights while the view is open.
private var git: GroupItem {
let open = model.layout.isGitViewOpen
let count = Set(model.sourceControl?.state.changes.map(\.path) ?? []).count
let tip = open
? Tooltip.stated("Git view: ", "open", ". Click to close it (⌘G).")
: Tooltip.stated("Git view: ", "closed", ". Click to open it (⌘G).")
return GroupItem(count > 0 ? "Git \(count)" : "Git", id: "Git", keys: "⌘G", kind: .toggle(isOn: open), tip: tip) {
model.perform(.toggleGit)
}
}
private var schema: [GroupItem] {
guard model.schema?.isLaravelApp == true else { return [] }
let open = model.layout.isSchemaViewOpen
let tip = open
? Tooltip.stated("Schema view: ", "open", ". Click to close it (⌘D).")
: Tooltip.stated("Schema view: ", "closed", ". Click to open the database diagram (⌘D).")
return [GroupItem("Schema", keys: "⌘D", kind: .toggle(isOn: open), tip: tip) {
model.perform(.toggleSchemaView)
}]
}
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)
}]
}
}