Files
helder/Sources/HelderUI/SourceControl/CommitBox.swift
T
2026-10-10 14:54:39 +02:00

242 lines
8.8 KiB
Swift

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)
.pointerHand()
.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)
.pointerHand()
.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
textView.fillsClipView = 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: PointerBoundTextView {
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
}
}