restores Helder in all its glory

This commit is contained in:
2026-09-28 11:34:49 +02:00
parent c2b38b853c
commit 1d29d0952d
280 changed files with 29205 additions and 7 deletions
@@ -0,0 +1,238 @@
import AppKit
import HelderCore
import SwiftUI
/// The foot of the Source Control column, edge to edge: the message field over the Commit and push buttons.
struct CommitBox: View {
let model: WorkspaceModel
let store: SourceControlStore
@State private var focused = false
var body: some View {
VStack(spacing: 0) {
field
HStack(spacing: 0) {
CommitButton(isEnabled: store.state.stagedCount > 0) { model.perform(.commit) }
PushButton(lit: store.state.canPush, busy: store.isPushing,
help: SourceControlText.pushTooltip(canPush: store.state.canPush, ahead: store.state.ahead)) {
model.perform(.push)
}
}
.frame(height: 32)
}
}
private var field: some View {
CommitTextView(
text: store.commitMessage,
focusToken: model.focusRequest?.target == .commitMessage ? model.focusRequest?.id : nil,
onChange: { store.setCommitMessage($0) },
onCommit: commitFromField,
onFocusChange: fieldFocusChanged
)
.overlay(alignment: .topLeading) {
if store.commitMessage.isEmpty {
Text("Shift+Enter to commit")
.typeRole(Typography.sourceControlRow)
.foregroundStyle(Palette.text4)
.padding(.leading, CommitTextView.textInset.width)
.padding(.top, CommitTextView.textInset.height)
.allowsHitTesting(false)
}
}
.frame(maxWidth: .infinity)
.frame(height: 72)
.background(Palette.editor)
.hairline(.top)
.overlay {
if focused {
ZStack {
Rectangle().strokeBorder(Palette.amber, lineWidth: Metrics.accentRule)
Rectangle().inset(by: Metrics.accentRule).strokeBorder(Palette.focusRing, lineWidth: Metrics.focusRingWidth)
}
.allowsHitTesting(false)
}
}
.animation(Metrics.smallControlTransition, value: focused)
}
private func fieldFocusChanged(_ isFocused: Bool) {
focused = isFocused
guard isFocused else { return }
model.focusTaken(.commitMessage)
model.pointerFocused(.sourceControl)
}
/// Shift/⌘/Ctrl+Return commits only when there is something to commit; the button always asks the model.
private func commitFromField() {
guard store.canCommit else { return }
model.perform(.commit)
}
}
/// The amber button that fills the action row; nothing staged gives the resting panel look.
private struct CommitButton: View {
let isEnabled: Bool
let action: () -> Void
@State private var hovered = false
var body: some View {
Button(action: action) {
HStack(spacing: 6) {
Text("Commit").typeRole(Typography.commitButton)
IconView(glyph: .arrowUp)
}
.foregroundStyle(isEnabled ? Palette.commitInk : Palette.text4)
.padding(.horizontal, 11)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(isEnabled ? (hovered ? Palette.amberSoft : Palette.amber) : Palette.panel)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.disabled(!isEnabled)
.onHover { hovered = $0 }
.animation(Metrics.colorTransition, value: hovered)
.help("Commit the staged files (⌘↵)")
}
}
/// The 36pt push button right of Commit; lit (amber on the tint) while the branch is ahead of its remote.
private struct PushButton: View {
let lit: Bool
let busy: Bool
let help: String
let action: () -> Void
@State private var hovered = false
var body: some View {
Button(action: action) {
IconView(glyph: .push)
.foregroundStyle(iconColor)
.frame(width: 36)
.frame(maxHeight: .infinity)
.background(lit ? Palette.amberTint : (hovered ? Palette.hover : Palette.panel))
.hairline(.top)
.hairline(.leading)
.contentShape(Rectangle())
.opacity(busy ? 0.4 : 1)
}
.buttonStyle(.plain)
.disabled(busy)
.onHover { hovered = $0 }
.animation(Metrics.colorTransition, value: hovered)
.help(help)
}
private var iconColor: Color {
if lit { return hovered ? Palette.amberSoft : Palette.amber }
return hovered ? Palette.amber : Palette.text3
}
}
/// A plain NSTextView, because SwiftUI's fields cannot tell Shift+Return from Return.
struct CommitTextView: NSViewRepresentable {
/// Where the first glyph sits: 9pt from the top, 12pt from the left.
static let textInset = CGSize(width: 12, height: 9)
let text: String
/// A new value takes first responder once.
let focusToken: UUID?
let onChange: (String) -> Void
let onCommit: () -> Void
let onFocusChange: (Bool) -> Void
func makeCoordinator() -> Coordinator { Coordinator() }
func makeNSView(context: Context) -> NSScrollView {
let scroll = NSScrollView()
scroll.drawsBackground = false
scroll.hasVerticalScroller = false
scroll.borderType = .noBorder
let textView = CommitNSTextView()
textView.delegate = context.coordinator
textView.drawsBackground = false
textView.isRichText = false
textView.allowsUndo = true
textView.isAutomaticQuoteSubstitutionEnabled = false
textView.isAutomaticDashSubstitutionEnabled = false
textView.isAutomaticSpellingCorrectionEnabled = false
textView.isContinuousSpellCheckingEnabled = false
textView.font = Typography.sourceControlRow.nsFont
textView.textColor = NSColor(hex: 0xE4E7E6)
textView.insertionPointColor = NSColor(hex: 0xE8913A)
let padding = textView.textContainer?.lineFragmentPadding ?? 5
textView.textContainerInset = NSSize(width: Self.textInset.width - padding, height: Self.textInset.height)
textView.isVerticallyResizable = true
textView.isHorizontallyResizable = false
textView.autoresizingMask = [.width]
textView.textContainer?.widthTracksTextView = true
let paragraph = NSMutableParagraphStyle()
paragraph.minimumLineHeight = 18
paragraph.maximumLineHeight = 18
textView.defaultParagraphStyle = paragraph
textView.typingAttributes[.paragraphStyle] = paragraph
scroll.documentView = textView
context.coordinator.textView = textView
return scroll
}
func updateNSView(_ scroll: NSScrollView, context: Context) {
let coordinator = context.coordinator
coordinator.parent = self
guard let textView = coordinator.textView else { return }
textView.onCommit = onCommit
textView.onFocusChange = onFocusChange
if textView.string != text {
textView.string = text
}
if let focusToken, coordinator.lastFocusToken != focusToken {
coordinator.lastFocusToken = focusToken
DispatchQueue.main.async { textView.window?.makeFirstResponder(textView) }
}
}
final class Coordinator: NSObject, NSTextViewDelegate {
var parent: CommitTextView?
weak var textView: CommitNSTextView?
var lastFocusToken: UUID?
func textDidChange(_ notification: Notification) {
guard let textView else { return }
let normalized = SourceControlText.normalizeCommitMessage(textView.string)
if normalized != textView.string {
let caret = textView.selectedRange()
textView.string = normalized
textView.setSelectedRange(NSRange(location: min(caret.location, (normalized as NSString).length), length: 0))
}
parent?.onChange(normalized)
}
}
}
final class CommitNSTextView: NSTextView {
var onCommit: (() -> Void)?
var onFocusChange: ((Bool) -> Void)?
override func keyDown(with event: NSEvent) {
let isReturn = event.keyCode == 36 || event.keyCode == 76
let flags = event.modifierFlags.intersection([.shift, .command, .control])
if isReturn && !flags.isEmpty {
onCommit?()
return
}
super.keyDown(with: event)
}
override func becomeFirstResponder() -> Bool {
let accepted = super.becomeFirstResponder()
if accepted { onFocusChange?(true) }
return accepted
}
override func resignFirstResponder() -> Bool {
let accepted = super.resignFirstResponder()
if accepted { onFocusChange?(false) }
return accepted
}
}
@@ -0,0 +1,290 @@
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())
.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
}
}