292 lines
10 KiB
Swift
292 lines
10 KiB
Swift
import HelderCore
|
||
import SwiftUI
|
||
|
||
/// Column A: staged and unstaged rows grouped by folder, with the commit box at the foot.
|
||
public struct SourceControlColumn: View {
|
||
let model: WorkspaceModel
|
||
@State private var columnFrame = CGRect.zero
|
||
/// A cursor that follows the pointer must not scroll the list under it.
|
||
@State private var hoveredRowID: String?
|
||
|
||
public init(model: WorkspaceModel) {
|
||
self.model = model
|
||
}
|
||
|
||
public var body: some View {
|
||
Group {
|
||
if let store = model.sourceControl {
|
||
content(store)
|
||
} else {
|
||
Color.clear
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||
.background(Palette.column(.sourceControl, active: isActive))
|
||
.background(GlobalFrameReader(frame: $columnFrame))
|
||
.simultaneousGesture(TapGesture().onEnded { model.pointerFocused(.sourceControl) })
|
||
}
|
||
|
||
private var isActive: Bool { model.layout.activePanel == .sourceControl }
|
||
|
||
private func content(_ store: SourceControlStore) -> some View {
|
||
VStack(spacing: 0) {
|
||
list(store)
|
||
CommitBox(model: model, store: store)
|
||
}
|
||
.task { await store.loadIfNeeded() }
|
||
}
|
||
|
||
private func list(_ store: SourceControlStore) -> some View {
|
||
ScrollViewReader { proxy in
|
||
ScrollView {
|
||
LazyVStack(alignment: .leading, spacing: 0) {
|
||
sections(store)
|
||
}
|
||
.padding(.bottom, 8)
|
||
}
|
||
.scrollIndicators(.automatic)
|
||
.onChange(of: store.selectedRowID) { _, id in
|
||
guard isActive, let id, id != hoveredRowID else { return }
|
||
proxy.scrollTo(id)
|
||
}
|
||
}
|
||
.frame(maxHeight: .infinity)
|
||
}
|
||
|
||
@ViewBuilder
|
||
private func sections(_ store: SourceControlStore) -> some View {
|
||
let staged = store.stagedGroups
|
||
let unstaged = store.unstagedGroups
|
||
if store.hasLoaded && !store.state.isRepository {
|
||
EmptyState(text: "— Not a git repository.")
|
||
} else if staged.isEmpty && unstaged.isEmpty {
|
||
EmptyState(text: "— No changes. The working tree is clean.")
|
||
} else {
|
||
GroupHeader(title: "Staged Changes", count: store.state.stagedCount,
|
||
quickAction: staged.isEmpty ? nil : QuickAction(glyph: .minus, help: "Unstage all") {
|
||
Task { await store.unstageAll() }
|
||
})
|
||
if staged.isEmpty {
|
||
EmptyLine(text: "— Nothing staged.")
|
||
} else {
|
||
groups(staged, store)
|
||
}
|
||
Hairline().padding(.horizontal, 12).padding(.top, 16)
|
||
GroupHeader(title: "Changes", count: store.state.unstagedCount,
|
||
quickAction: unstaged.isEmpty ? nil : QuickAction(glyph: .plus, help: "Stage all") {
|
||
Task { await store.stageAll() }
|
||
})
|
||
if unstaged.isEmpty {
|
||
EmptyLine(text: "— All changes are staged.")
|
||
} else {
|
||
groups(unstaged, store)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func groups(_ groups: [ChangeGroup], _ store: SourceControlStore) -> some View {
|
||
ForEach(groups, id: \.dir) { group in
|
||
DirHeader(dir: group.dir)
|
||
ForEach(group.rows) { change in
|
||
GitRowView(
|
||
change: change,
|
||
selected: isSelected(change, store),
|
||
onOpen: { open(change, store) },
|
||
onToggleStage: { toggleStage(change) },
|
||
onHover: { hoveredRowID = change.id; store.selectedRowID = change.id },
|
||
onContextMenu: { point in showMenu(for: change, at: point, store) }
|
||
)
|
||
.reportsRowFrame(.git(change.id), to: model)
|
||
.id(change.id)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: Row state and actions
|
||
|
||
/// The open file's row (its half, when a git row opened it), the keyboard cursor, or the row whose menu is open.
|
||
private func isSelected(_ change: Change, _ store: SourceControlStore) -> Bool {
|
||
if case let .contextMenu(request) = model.overlay.current,
|
||
request.target == .gitRow(path: change.path, staged: change.staged) { return true }
|
||
if isActive && store.selectedRowID == change.id { return true }
|
||
guard let editor = model.editor, editor.activePath == change.path else { return false }
|
||
guard let side = editor.sides[change.path] else { return true }
|
||
return side == (change.staged ? .staged : .unstaged)
|
||
}
|
||
|
||
private func open(_ change: Change, _ store: SourceControlStore) {
|
||
store.selectedRowID = change.id
|
||
model.perform(.open(change.path, OpenOptions(side: change.staged ? .staged : .unstaged)))
|
||
}
|
||
|
||
private func toggleStage(_ change: Change) {
|
||
model.perform(.context(change.staged ? .unstage(change.path) : .stage(change.path)))
|
||
}
|
||
|
||
private func showMenu(for change: Change, at point: CGPoint, _ store: SourceControlStore) {
|
||
model.pointerFocused(.sourceControl)
|
||
store.selectedRowID = change.id
|
||
let anchor = CGPoint(x: columnFrame.maxX, y: point.y)
|
||
model.perform(.showContextMenu(.gitRow(path: change.path, staged: change.staged), anchor: anchor))
|
||
}
|
||
}
|
||
|
||
// MARK: Parts
|
||
|
||
/// A group's hover-only quick action (Unstage all, Stage all).
|
||
private struct QuickAction {
|
||
let glyph: IconGlyph
|
||
let help: String
|
||
let action: () -> Void
|
||
}
|
||
|
||
private struct GroupHeader: View {
|
||
let title: String
|
||
let count: Int
|
||
/// nil when the group has no rows.
|
||
let quickAction: QuickAction?
|
||
@State private var hovered = false
|
||
|
||
var body: some View {
|
||
HStack(spacing: 8) {
|
||
Text(title).typeRole(RowStyle.groupLabelRole).foregroundStyle(Palette.text4)
|
||
if let quickAction {
|
||
QuickActionButton(glyph: quickAction.glyph, help: quickAction.help, action: quickAction.action)
|
||
.frame(width: 14, height: 10)
|
||
.opacity(hovered ? 1 : 0)
|
||
}
|
||
Spacer(minLength: 0)
|
||
Text("\(count)").typeRole(RowStyle.groupLabelRole).foregroundStyle(Palette.amber)
|
||
}
|
||
.padding(.horizontal, 12)
|
||
.padding(.top, 14)
|
||
.padding(.bottom, 6)
|
||
.contentShape(Rectangle())
|
||
.onHover { hovered = $0 }
|
||
}
|
||
}
|
||
|
||
private struct DirHeader: View {
|
||
let dir: String
|
||
|
||
var body: some View {
|
||
HStack(spacing: 8) {
|
||
FolderGlyph(open: false)
|
||
Text(dir.isEmpty ? "." : dir)
|
||
.typeRole(RowStyle.dirHeadRole)
|
||
.foregroundStyle(Palette.text4)
|
||
.lineLimit(1)
|
||
.truncationMode(.head)
|
||
}
|
||
.padding(.leading, 14)
|
||
.padding(.trailing, 12)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
.frame(height: 24)
|
||
.help(dir.isEmpty ? "(project root)" : dir)
|
||
}
|
||
}
|
||
|
||
/// The column's only line when there is nothing to list.
|
||
private struct EmptyState: View {
|
||
let text: String
|
||
|
||
var body: some View {
|
||
Text(text)
|
||
.typeRole(TypeRole(.sansRegular, 12, lineHeight: 19.2))
|
||
.foregroundStyle(Palette.text4)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
.padding(.horizontal, 12)
|
||
.padding(.vertical, 14)
|
||
}
|
||
}
|
||
|
||
/// The line under a group header that has no rows.
|
||
private struct EmptyLine: View {
|
||
let text: String
|
||
|
||
var body: some View {
|
||
Text(text)
|
||
.typeRole(Typography.sourceControlRow)
|
||
.foregroundStyle(Palette.text4)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
.padding(.horizontal, 12)
|
||
.padding(.top, 2)
|
||
.padding(.bottom, 8)
|
||
}
|
||
}
|
||
|
||
/// One git row: status letter, badge, name, line counts, and the hover +/− in their place.
|
||
private struct GitRowView: View {
|
||
let change: Change
|
||
let selected: Bool
|
||
let onOpen: () -> Void
|
||
let onToggleStage: () -> Void
|
||
let onHover: () -> Void
|
||
let onContextMenu: (CGPoint) -> Void
|
||
@State private var hovered = false
|
||
|
||
var body: some View {
|
||
HStack(spacing: 8) {
|
||
Text(change.status.rawValue)
|
||
.typeRole(RowStyle.gitLetterRole)
|
||
.foregroundStyle(letterColor)
|
||
.frame(width: 12, alignment: .leading)
|
||
TypeBadge(path: change.path)
|
||
Text(PathUtils.basename(change.path))
|
||
.typeRole(selected ? TypeRole(.sansMedium, 12) : Typography.sourceControlRow)
|
||
.strikethrough(change.isDeleted, color: Palette.text4)
|
||
.foregroundStyle(nameColor)
|
||
.lineLimit(1)
|
||
.truncationMode(.tail)
|
||
Spacer(minLength: 0)
|
||
trailing
|
||
}
|
||
.padding(.leading, 14)
|
||
.padding(.trailing, 12)
|
||
.frame(height: Metrics.sourceControlRow)
|
||
.selectableRow(isSelected: selected, isHovered: hovered)
|
||
.contentShape(Rectangle())
|
||
.pointerHand()
|
||
.onTapGesture(perform: onOpen)
|
||
.onHover { inside in
|
||
hovered = inside
|
||
if inside { onHover() }
|
||
}
|
||
.overlay(RightClickArea(onRightClick: onContextMenu))
|
||
.help(change.path)
|
||
}
|
||
|
||
@ViewBuilder
|
||
private var trailing: some View {
|
||
if hovered || selected {
|
||
QuickActionButton(glyph: change.staged ? .minus : .plus,
|
||
help: change.staged ? "Unstage changes" : "Stage changes",
|
||
action: onToggleStage)
|
||
} else if change.added > 0 || change.removed > 0 {
|
||
HStack(spacing: 4) {
|
||
if change.added > 0 { Text("+\(change.added)").foregroundStyle(Palette.addedText) }
|
||
if change.removed > 0 { Text("\u{2212}\(change.removed)").foregroundStyle(Palette.removedText) }
|
||
}
|
||
.typeRole(Typography.meta)
|
||
.lineLimit(1)
|
||
.fixedSize()
|
||
}
|
||
}
|
||
|
||
/// Staged letters read teal whatever the status; unstaged ones by status.
|
||
private var letterColor: Color {
|
||
if change.staged { return Palette.stagedLetter }
|
||
switch change.status {
|
||
case .deleted: return Palette.amberDeep
|
||
case .renamed: return Palette.text3
|
||
default: return Palette.unstagedLetter
|
||
}
|
||
}
|
||
|
||
private var nameColor: Color {
|
||
if change.isDeleted { return Palette.text4 }
|
||
if selected { return Palette.text1 }
|
||
return SourceControlText.isTestFile(change.path) ? Palette.text4 : Palette.text3
|
||
}
|
||
}
|