import HelderCore import SwiftUI /// The bar above the code: which file is open, whether it is unsaved or changed, and its Preview and Diff buttons. /// With no file open it stays as an empty bar, so the column's top edge lines up with its neighbours. struct EditorBar: View { var editor: EditorStore? var presentation: EditorPresentation? var body: some View { HStack(spacing: 10) { if let editor, let presentation { content(editor, presentation) } } .padding(.horizontal, 14) .frame(height: Metrics.editorBar) .frame(maxWidth: .infinity, alignment: .leading) .background(Palette.editor) .overlay(alignment: .bottom) { Rectangle().fill(Palette.raised).frame(height: Metrics.hairline) } } @ViewBuilder private func content(_ editor: EditorStore, _ presentation: EditorPresentation) -> some View { let path = presentation.path TypeBadge(path: path) Text(path) .typeRole(Typography.path) .foregroundStyle(Palette.text2) .lineLimit(1) .truncationMode(.head) .help(path) if editor.isDirty(path) { Circle().fill(Palette.text2).frame(width: 7, height: 7) .accessibilityLabel("Unsaved changes") } if let diff = editor.diff(for: path), presentation.hasDiff { HStack(spacing: 8) { if diff.added > 0 { Text("+\(diff.added)").foregroundStyle(Palette.addedText) } if diff.removed > 0 { Text("\u{2212}\(diff.removed)").foregroundStyle(Palette.removedText) } } .typeRole(TypeRole(.monoMedium, 12)) } Spacer(minLength: 0) ForEach(presentation.barButtons, id: \.self) { button in switch button { case .preview: SegmentButton("Preview", isOn: presentation.mode == .preview) { editor.togglePreview() } case .diff: SegmentButton("Diff", isOn: presentation.mode == .diff) { editor.setMode(.diff, for: path) } } } } }