adds nice git interface
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
import AppKit
|
||||
import HelderCore
|
||||
import SwiftUI
|
||||
|
||||
/// Local and origin branches; a click switches, but only while the working tree is clean.
|
||||
struct GitBranchesPanel: View {
|
||||
let model: WorkspaceModel
|
||||
let store: SourceControlStore
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
header
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
group("local")
|
||||
ForEach(store.branches.local, id: \.self) { name in
|
||||
BranchRow(model: model, item: item(name, remote: false))
|
||||
}
|
||||
group("remote · origin")
|
||||
ForEach(store.branches.remote, id: \.self) { name in
|
||||
BranchRow(model: model, item: item(name, remote: true))
|
||||
}
|
||||
}
|
||||
.padding(.bottom, 12)
|
||||
.background(OverlayScrollers())
|
||||
}
|
||||
.scrollIndicators(.automatic)
|
||||
}
|
||||
}
|
||||
|
||||
private var header: some View {
|
||||
let count = store.changedPathCount
|
||||
return HStack(spacing: 12) {
|
||||
Text("[ branches ]").typeRole(Typography.panelLabel).foregroundStyle(Palette.text4).fixedSize()
|
||||
Spacer(minLength: 0)
|
||||
Text(store.isDirty ? "— \(count) uncommitted file\(count == 1 ? "" : "s") · switching locked" : "clean · click to switch")
|
||||
.typeRole(Typography.path)
|
||||
.foregroundStyle(store.isDirty ? Palette.amberSoft : Palette.textMuted)
|
||||
.lineLimit(1)
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
.frame(height: GitMetrics.stripHeader)
|
||||
}
|
||||
|
||||
private func group(_ label: String) -> some View {
|
||||
Text(label)
|
||||
.typeRole(Typography.panelLabel)
|
||||
.foregroundStyle(Palette.text4)
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.top, 8)
|
||||
.padding(.bottom, 4)
|
||||
}
|
||||
|
||||
private func item(_ name: String, remote: Bool) -> BranchItem {
|
||||
let current = !remote && name == store.state.branch
|
||||
let tracked = remote && store.branches.isTracked(name)
|
||||
let isCurrentUpstream = remote && GitBranchList.localName(ofRemote: name) == store.state.branch
|
||||
let canSwitch = !current && !isCurrentUpstream && !store.isDirty
|
||||
return BranchItem(
|
||||
name: name, remote: remote, current: current, locked: !current && store.isDirty,
|
||||
meta: current ? "current" : (tracked ? "tracked" : ""),
|
||||
action: canSwitch ? (remote && !tracked ? "checkout →" : "switch →") : "",
|
||||
clickable: !current && !isCurrentUpstream
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private struct BranchItem {
|
||||
let name: String
|
||||
let remote: Bool
|
||||
let current: Bool
|
||||
/// Another branch while the tree is dirty: dimmed, and a click only explains why.
|
||||
let locked: Bool
|
||||
let meta: String
|
||||
let action: String
|
||||
let clickable: Bool
|
||||
}
|
||||
|
||||
private struct BranchRow: View {
|
||||
let model: WorkspaceModel
|
||||
let item: BranchItem
|
||||
@State private var isHovered = false
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 10) {
|
||||
GitIconView(icon: item.remote ? .cloud : .gitBranch, size: 13)
|
||||
.foregroundStyle(item.current ? Palette.amber : Palette.text4)
|
||||
Text(item.name)
|
||||
.typeRole(item.current ? Typography.branchCurrent : Typography.path)
|
||||
.foregroundStyle(item.current ? Palette.text1 : Palette.text3)
|
||||
.lineLimit(1)
|
||||
.truncationMode(.tail)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
if !item.meta.isEmpty {
|
||||
Text(item.meta).typeRole(Typography.meta).foregroundStyle(Palette.text4).fixedSize()
|
||||
}
|
||||
if !item.action.isEmpty {
|
||||
Text(item.action).typeRole(Typography.gitCount).foregroundStyle(Palette.amber).fixedSize()
|
||||
}
|
||||
}
|
||||
.padding(.leading, 16)
|
||||
.padding(.trailing, 16)
|
||||
.frame(height: GitMetrics.branchRow)
|
||||
.background(isHovered ? Palette.raised : (item.current ? Palette.gitCurrentBranch : .clear))
|
||||
.overlay(alignment: .leading) {
|
||||
Rectangle().fill(item.current ? Palette.amber : .clear).frame(width: GitMetrics.accentBar)
|
||||
}
|
||||
.opacity(item.locked ? 0.4 : 1)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
guard item.clickable else { return }
|
||||
model.perform(.switchBranch(item.name, remote: item.remote))
|
||||
}
|
||||
.pointerHand(item.clickable && !item.locked)
|
||||
.tracksHover($isHovered)
|
||||
.modifier(LockedTip(isLocked: item.locked))
|
||||
}
|
||||
}
|
||||
|
||||
private struct LockedTip: ViewModifier {
|
||||
let isLocked: Bool
|
||||
|
||||
func body(content: Content) -> some View {
|
||||
if isLocked {
|
||||
content.tip("Commit or discard your changes first")
|
||||
} else {
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Puts the enclosing scroll view's scrollers over the content, so rows keep their 16px right padding.
|
||||
/// A legacy scroller would take its width from the rows whenever the list overflows.
|
||||
private struct OverlayScrollers: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> OverlayScrollersProbe { OverlayScrollersProbe() }
|
||||
func updateNSView(_ view: OverlayScrollersProbe, context: Context) { view.apply() }
|
||||
}
|
||||
|
||||
private final class OverlayScrollersProbe: NSView {
|
||||
private var observer: NSObjectProtocol?
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
apply()
|
||||
guard observer == nil, window != nil else { return }
|
||||
observer = NotificationCenter.default.addObserver(
|
||||
forName: NSScroller.preferredScrollerStyleDidChangeNotification, object: nil, queue: .main
|
||||
) { [weak self] _ in
|
||||
MainActor.assumeIsolated { self?.apply() }
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let observer { NotificationCenter.default.removeObserver(observer) }
|
||||
}
|
||||
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
|
||||
func apply() {
|
||||
guard let scroll = enclosingScrollView, scroll.scrollerStyle != .overlay else { return }
|
||||
scroll.scrollerStyle = .overlay
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user