401 lines
16 KiB
Swift
401 lines
16 KiB
Swift
import HelderCore
|
|
import SwiftUI
|
|
|
|
/// The four colour-coded sections, Staged on top. Rows and inline diff lines are one flat lazy list,
|
|
/// so a long expanded file only builds the lines on screen.
|
|
struct GitSectionsList: View {
|
|
let model: WorkspaceModel
|
|
let store: SourceControlStore
|
|
|
|
var body: some View {
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 0) {
|
|
ForEach(items) { item in
|
|
switch item.kind {
|
|
case let .message(text):
|
|
Text(text)
|
|
.typeRole(Typography.gitEmpty)
|
|
.foregroundStyle(Palette.textMuted)
|
|
.padding(.horizontal, 16)
|
|
.padding(.vertical, 24)
|
|
case let .header(section, separated):
|
|
GitSectionHeader(model: model, store: store, section: section)
|
|
.padding(.top, separated ? GitMetrics.sectionGap : 0)
|
|
.overlay(alignment: .top) { if separated { Hairline() } }
|
|
case let .row(kind, row):
|
|
GitFileRow(model: model, store: store, kind: kind, row: row)
|
|
case let .diffLine(row):
|
|
InlineDiffLine(row: row)
|
|
case let .diffNote(text):
|
|
Text(text)
|
|
.typeRole(Typography.meta)
|
|
.foregroundStyle(Palette.text4)
|
|
.padding(.leading, 64)
|
|
.frame(maxWidth: .infinity, minHeight: GitMetrics.diffLine, alignment: .leading)
|
|
case .diffTop:
|
|
Color.clear.frame(height: 8)
|
|
case .diffBottom:
|
|
Color.clear.frame(height: 12).hairline(.bottom)
|
|
}
|
|
}
|
|
}
|
|
.padding(.bottom, 24)
|
|
}
|
|
.scrollIndicators(.automatic)
|
|
}
|
|
|
|
private var items: [GitListItem] {
|
|
if store.hasLoaded && !store.state.isRepository { return [GitListItem(id: "empty", kind: .message("— Not a git repository."))] }
|
|
if store.sections.isEmpty {
|
|
return store.hasLoaded ? [GitListItem(id: "empty", kind: .message("— No changes. The working tree is clean."))] : []
|
|
}
|
|
var items: [GitListItem] = []
|
|
for (index, section) in store.sections.enumerated() {
|
|
let separated = index == 1 && store.sections[0].kind == .staged
|
|
items.append(GitListItem(id: "h:" + section.id, kind: .header(section, separated: separated)))
|
|
guard !store.isCollapsed(section.kind) else { continue }
|
|
for row in section.rows {
|
|
let key = SourceControlStore.rowKey(section.kind, row)
|
|
items.append(GitListItem(id: "r:" + key, kind: .row(section.kind, row)))
|
|
guard store.isExpanded(section.kind, row) else { continue }
|
|
items.append(GitListItem(id: "t:" + key, kind: .diffTop))
|
|
switch store.detail(for: row) {
|
|
case .tooBig:
|
|
items.append(GitListItem(id: "n:" + key, kind: .diffNote("Too big.")))
|
|
case let .rows(lines) where lines.isEmpty:
|
|
items.append(GitListItem(id: "n:" + key, kind: .diffNote("— No line changes.")))
|
|
case let .rows(lines):
|
|
for (offset, line) in lines.enumerated() {
|
|
items.append(GitListItem(id: "d:\(key):\(offset)", kind: .diffLine(line)))
|
|
}
|
|
}
|
|
items.append(GitListItem(id: "b:" + key, kind: .diffBottom))
|
|
}
|
|
}
|
|
return items
|
|
}
|
|
}
|
|
|
|
private struct GitListItem: Identifiable {
|
|
enum Kind {
|
|
case message(String)
|
|
case header(GitSection, separated: Bool)
|
|
case row(GitSectionKind, Change)
|
|
case diffTop
|
|
case diffLine(InlineDiffRow)
|
|
case diffNote(String)
|
|
case diffBottom
|
|
}
|
|
|
|
let id: String
|
|
let kind: Kind
|
|
}
|
|
|
|
// MARK: Section look
|
|
|
|
/// What a section is called, how it is coloured, and which bulk and row actions it offers.
|
|
private struct SectionStyle {
|
|
let label: String
|
|
let color: Color
|
|
let tint: Color
|
|
|
|
init(_ kind: GitSectionKind) {
|
|
switch kind {
|
|
case .staged: (label, color, tint) = ("staged", Palette.amber, Palette.gitStagedTint)
|
|
case .changed: (label, color, tint) = ("changed", Palette.gitModified, Palette.gitModifiedTint)
|
|
case .untracked: (label, color, tint) = ("new · untracked", Palette.gitNew, Palette.gitNewTint)
|
|
case .deleted: (label, color, tint) = ("deleted", Palette.gitDeleted, Palette.gitDeletedTint)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct GitAction {
|
|
let label: String
|
|
let isDestructive: Bool
|
|
let command: Command
|
|
}
|
|
|
|
/// Left then right; a lone action, such as `Unstage`, always lands in the right column.
|
|
/// Conflicted rows get no Discard: staging them is how a resolution is marked.
|
|
private func actions(_ kind: GitSectionKind, rows: [Change], bulk: Bool) -> [GitAction] {
|
|
let all = bulk ? " all" : ""
|
|
let paths = GitSections.uniquePaths(rows)
|
|
let stage = GitAction(label: "Stage" + all, isDestructive: false, command: .stage(paths))
|
|
switch kind {
|
|
case .staged: return [GitAction(label: "Unstage" + all, isDestructive: false, command: .unstage(paths))]
|
|
case .changed:
|
|
let discardable = GitSections.uniquePaths(rows.filter { $0.status != .unmerged })
|
|
guard !discardable.isEmpty else { return [stage] }
|
|
return [GitAction(label: "Discard" + all, isDestructive: true, command: .context(.gitDiscard(discardable))), stage]
|
|
case .untracked: return [GitAction(label: "Delete" + all, isDestructive: true, command: .context(.gitDiscard(paths))), stage]
|
|
case .deleted: return [GitAction(label: "Restore" + all, isDestructive: false, command: .context(.gitDiscard(paths))), stage]
|
|
}
|
|
}
|
|
|
|
/// Two 100pt columns, identical in headers and rows so the buttons line up.
|
|
private struct ActionsTable: View {
|
|
let model: WorkspaceModel
|
|
let actions: [GitAction]
|
|
let isHeader: Bool
|
|
|
|
var body: some View {
|
|
HStack(spacing: GitMetrics.actionGap) {
|
|
if actions.count < 2 { Color.clear.frame(width: GitMetrics.actionWidth, height: 1) }
|
|
ForEach(Array(actions.enumerated()), id: \.offset) { _, action in
|
|
GitActionButton(action: action, isHeader: isHeader) { model.perform(action.command) }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct GitActionButton: View {
|
|
let action: GitAction
|
|
let isHeader: Bool
|
|
let perform: () -> Void
|
|
@State private var isHovered = false
|
|
|
|
var body: some View {
|
|
Button(action: perform) {
|
|
Text(action.label)
|
|
.typeRole(Typography.button)
|
|
.foregroundStyle(textColor)
|
|
.frame(width: GitMetrics.actionWidth, height: isHeader ? GitMetrics.headerAction : GitMetrics.rowAction)
|
|
.background(isHovered ? Palette.raised : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(borderColor, lineWidth: Metrics.hairline))
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.pointerHand()
|
|
.tracksHover($isHovered)
|
|
}
|
|
|
|
private var textColor: Color {
|
|
if !isHeader && isHovered { return Palette.text1 }
|
|
return action.isDestructive ? Palette.amberSoft : Palette.text3
|
|
}
|
|
|
|
private var borderColor: Color {
|
|
if isHeader { return isHovered ? Palette.text4 : Palette.text5 }
|
|
return isHovered ? Palette.text5 : .clear
|
|
}
|
|
}
|
|
|
|
// MARK: Header
|
|
|
|
private struct GitSectionHeader: View {
|
|
let model: WorkspaceModel
|
|
let store: SourceControlStore
|
|
let section: GitSection
|
|
@State private var isHovered = false
|
|
|
|
var body: some View {
|
|
let style = SectionStyle(section.kind)
|
|
let open = !store.isCollapsed(section.kind)
|
|
HStack(spacing: 10) {
|
|
GitIconView(icon: open ? .chevronDown : .chevronRight, size: 14)
|
|
.foregroundStyle(open ? Palette.amber : Palette.text4)
|
|
Text(style.label).typeRole(Typography.gitLabel).foregroundStyle(style.color).lineLimit(1).fixedSize()
|
|
Text("\(section.rows.count)")
|
|
.typeRole(Typography.gitCount)
|
|
.foregroundStyle(Palette.badgeText)
|
|
.padding(.horizontal, 6)
|
|
.padding(.vertical, 3)
|
|
.background(style.color, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
Spacer(minLength: 0)
|
|
ActionsTable(model: model, actions: actions(section.kind, rows: section.rows, bulk: true),
|
|
isHeader: true)
|
|
}
|
|
.padding(.leading, 14)
|
|
.padding(.trailing, 14)
|
|
.frame(height: GitMetrics.sectionHeader)
|
|
.background(isHovered ? Palette.panel : style.tint)
|
|
.overlay(alignment: .leading) { Rectangle().fill(style.color).frame(width: GitMetrics.accentBar) }
|
|
.hairline(.bottom)
|
|
.contentShape(Rectangle())
|
|
.onTapGesture { store.toggleSection(section.kind) }
|
|
.pointerHand()
|
|
.tracksHover($isHovered)
|
|
}
|
|
}
|
|
|
|
// MARK: File row
|
|
|
|
private struct GitFileRow: View {
|
|
let model: WorkspaceModel
|
|
let store: SourceControlStore
|
|
let kind: GitSectionKind
|
|
let row: Change
|
|
@State private var isHovered = false
|
|
|
|
var body: some View {
|
|
let expanded = store.isExpanded(kind, row)
|
|
HStack(spacing: 10) {
|
|
GitIconView(icon: expanded ? .chevronDown : .chevronRight, size: 13)
|
|
.foregroundStyle(expanded ? Palette.amber : Palette.text4)
|
|
StatusBadge(status: row.status)
|
|
path
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
HStack(spacing: 0) {
|
|
Text("+\(row.added)").foregroundStyle(Palette.gitNew)
|
|
.frame(width: GitMetrics.statWidth, alignment: .trailing)
|
|
Text("\u{2212}\(row.removed)").foregroundStyle(Palette.gitModified)
|
|
.frame(width: GitMetrics.statWidth - 8, alignment: .trailing)
|
|
.padding(.trailing, 8)
|
|
}
|
|
.typeRole(Typography.gitStats)
|
|
.lineLimit(1)
|
|
ActionsTable(model: model, actions: actions(kind, rows: [row], bulk: false), isHeader: false)
|
|
}
|
|
.padding(.horizontal, 14)
|
|
.frame(height: GitMetrics.fileRow)
|
|
.background(expanded ? Palette.panel : (isHovered ? Palette.gitRowHover : .clear))
|
|
.overlay(alignment: .leading) {
|
|
Rectangle().fill(kind == .staged ? Palette.amber : .clear).frame(width: GitMetrics.accentBar)
|
|
}
|
|
.hairline(.bottom)
|
|
.contentShape(Rectangle())
|
|
.onTapGesture { store.toggleRow(kind, row) }
|
|
.pointerHand()
|
|
.overlay(RightClickArea { point in
|
|
model.perform(.showContextMenu(.gitRow(path: row.path, isDeleted: row.isDeleted), anchor: point))
|
|
})
|
|
.tracksHover($isHovered)
|
|
}
|
|
|
|
/// The folder and slash muted, the name bright; a deleted file is struck through with a muted name.
|
|
private var path: some View {
|
|
let dir = PathUtils.dirname(row.path)
|
|
let name = PathUtils.basename(row.path)
|
|
let dirText = Text(dir.isEmpty ? "" : dir + "/").foregroundColor(Palette.textMuted)
|
|
let nameText = Text(name).foregroundColor(row.isDeleted ? Palette.textMuted : Palette.text1)
|
|
return (dirText + nameText)
|
|
.strikethrough(row.isDeleted, color: Palette.textMuted)
|
|
.typeRole(Typography.gitPath)
|
|
.lineLimit(1)
|
|
.truncationMode(.tail)
|
|
}
|
|
}
|
|
|
|
private struct StatusBadge: View {
|
|
let status: GitStatus
|
|
|
|
var body: some View {
|
|
Text(status.rawValue)
|
|
.typeRole(Typography.gitStatusLetter)
|
|
.foregroundStyle(Palette.badgeText)
|
|
.frame(width: GitMetrics.statusBadge, height: GitMetrics.statusBadge)
|
|
.background(color, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
.tip(name)
|
|
}
|
|
|
|
private var color: Color {
|
|
switch status {
|
|
case .added: Palette.gitNew
|
|
case .deleted: Palette.gitDeleted
|
|
case .modified, .renamed, .unmerged: Palette.gitModified
|
|
}
|
|
}
|
|
|
|
private var name: String {
|
|
switch status {
|
|
case .added: "New file"
|
|
case .deleted: "Deleted"
|
|
case .modified: "Modified"
|
|
case .renamed: "Renamed"
|
|
case .unmerged: "Conflict"
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Inline diff
|
|
|
|
/// One side-by-side line, last commit left and working tree right, or a folded run of unchanged lines.
|
|
private struct InlineDiffLine: View {
|
|
let row: InlineDiffRow
|
|
|
|
var body: some View {
|
|
switch row {
|
|
case let .gap(count):
|
|
Text("⋯ \(count) unchanged line\(count == 1 ? "" : "s")")
|
|
.typeRole(Typography.meta)
|
|
.foregroundStyle(Palette.text4)
|
|
.padding(.leading, 64)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.frame(height: GitMetrics.diffGap)
|
|
.background(Palette.panel)
|
|
.padding(.vertical, 6)
|
|
case let .line(left, right):
|
|
HStack(spacing: 0) {
|
|
InlineDiffCellView(cell: left, side: .left)
|
|
Hairline(.vertical)
|
|
InlineDiffCellView(cell: right, side: .right)
|
|
}
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
private enum DiffSide {
|
|
case left, right
|
|
}
|
|
|
|
private struct InlineDiffCellView: View {
|
|
let cell: InlineDiffCell?
|
|
let side: DiffSide
|
|
|
|
var body: some View {
|
|
HStack(alignment: .top, spacing: 0) {
|
|
Text(cell.map { String($0.number) } ?? "")
|
|
.foregroundStyle(numberColor)
|
|
.frame(width: GitMetrics.diffGutter - 12, alignment: .trailing)
|
|
.padding(.trailing, 12)
|
|
Text(code)
|
|
.foregroundStyle(Palette.text2)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.padding(.trailing, 16)
|
|
}
|
|
.typeRole(Typography.inlineDiff)
|
|
.padding(.vertical, Self.verticalPad)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
|
.background(fill)
|
|
}
|
|
|
|
/// Centres a single line in its 22pt box; `typeRole` only spaces the lines after the first.
|
|
private static let verticalPad: CGFloat = {
|
|
let font = Typography.inlineDiff.nsFont
|
|
return max(0, (GitMetrics.diffLine - (font.ascender - font.descender + font.leading)) / 2)
|
|
}()
|
|
|
|
private var fill: Color {
|
|
guard let cell else { return Palette.inlineFiller }
|
|
switch cell.mark {
|
|
case .removed: return Palette.inlineRemovedFill
|
|
case .added: return Palette.inlineAddedFill
|
|
case nil: return .clear
|
|
}
|
|
}
|
|
|
|
private var numberColor: Color {
|
|
switch cell?.mark {
|
|
case .removed: Palette.gitModified
|
|
case .added: Palette.gitNew
|
|
case nil: Palette.text5
|
|
}
|
|
}
|
|
|
|
/// Tabs show four wide; the differing middle of a paired line gets its highlight.
|
|
private var code: AttributedString {
|
|
guard let cell else { return AttributedString() }
|
|
let characters = Array(cell.text)
|
|
func piece(_ range: Range<Int>) -> AttributedString {
|
|
AttributedString(String(characters[range]).replacingOccurrences(of: "\t", with: " "))
|
|
}
|
|
guard let highlight = cell.highlight, highlight.upperBound <= characters.count else {
|
|
return piece(0..<characters.count)
|
|
}
|
|
var middle = piece(highlight)
|
|
middle.backgroundColor = side == .left ? Palette.inlineRemovedHighlight : Palette.inlineAddedHighlight
|
|
return piece(0..<highlight.lowerBound) + middle + piece(highlight.upperBound..<characters.count)
|
|
}
|
|
}
|