283 lines
12 KiB
Swift
283 lines
12 KiB
Swift
import AppKit
|
|
import HelderCore
|
|
import SwiftUI
|
|
|
|
/// The message field over Gen. Commit Message, the author, the ⌘↵ hint and the three commit buttons.
|
|
struct GitCommitBlock: View {
|
|
let model: WorkspaceModel
|
|
let store: SourceControlStore
|
|
@State private var isFocused = false
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
GitMessageField(
|
|
text: store.commitMessage,
|
|
focusToken: model.focusRequest?.target == .commitMessage ? model.focusRequest?.id : nil,
|
|
onChange: { store.setCommitMessage($0) },
|
|
onFocusChange: focusChanged
|
|
)
|
|
.background(Palette.editor)
|
|
.overlay(alignment: .bottom) {
|
|
Rectangle().fill(isFocused ? Palette.amber : Palette.raised).frame(height: Metrics.hairline)
|
|
}
|
|
.animation(Metrics.colorTransition, value: isFocused)
|
|
HStack(spacing: 16) {
|
|
GenerateMessageButton(model: model, store: store)
|
|
if let author = store.author {
|
|
Text(author).typeRole(Typography.path).foregroundStyle(Palette.textMuted).lineLimit(1)
|
|
}
|
|
Text("⌘↵ commits").typeRole(Typography.path).foregroundStyle(Palette.text4).fixedSize()
|
|
Spacer(minLength: 0)
|
|
CommitButtons(model: model, store: store)
|
|
}
|
|
.padding(.leading, 16)
|
|
.padding(.trailing, 14)
|
|
.frame(height: GitMetrics.commitFooter)
|
|
}
|
|
.background(Palette.panel)
|
|
.hairline(.bottom)
|
|
}
|
|
|
|
private func focusChanged(_ focused: Bool) {
|
|
isFocused = focused
|
|
if focused { model.focusTaken(.commitMessage) }
|
|
}
|
|
}
|
|
|
|
/// `Gen. Commit Message`: the Claude CLI writes the message for the staged changes. Dims while nothing is staged
|
|
/// and while a message is being written.
|
|
private struct GenerateMessageButton: View {
|
|
let model: WorkspaceModel
|
|
let store: SourceControlStore
|
|
@State private var hovered = false
|
|
|
|
var body: some View {
|
|
let color = hovered ? Palette.text1 : Palette.text3
|
|
Button { model.perform(.generateCommitMessage) } label: {
|
|
HStack(spacing: 7) {
|
|
SparkGlyph()
|
|
.stroke(color, style: StrokeStyle(lineWidth: 1.1 * 12 / 16, lineJoin: .round))
|
|
.frame(width: 12, height: 12)
|
|
Text(store.isGeneratingMessage ? "Generating…" : "Gen. Commit Message")
|
|
.typeRole(Typography.gitCommitButton)
|
|
.foregroundStyle(color)
|
|
}
|
|
.padding(.horizontal, 12)
|
|
.frame(height: GitMetrics.commitButton)
|
|
.background(hovered ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.pointerHand()
|
|
.tracksHover($hovered)
|
|
.disabledLook(!store.canGenerateMessage)
|
|
.fixedSize()
|
|
}
|
|
}
|
|
|
|
/// `Add, Stage & Commit` and `Commit N files` beside the primary `Commit and push`. The last two dim while nothing is staged;
|
|
/// with files staged they always run, so git's refusal of an empty message shows in the console.
|
|
private struct CommitButtons: View {
|
|
let model: WorkspaceModel
|
|
let store: SourceControlStore
|
|
@State private var stageAllHovered = false
|
|
@State private var commitHovered = false
|
|
@State private var pushHovered = false
|
|
|
|
var body: some View {
|
|
let count = store.stagedPathCount
|
|
HStack(spacing: 8) {
|
|
secondaryButton("Add, Stage & Commit", command: .stageAllAndCommit, hovered: $stageAllHovered)
|
|
.disabledLook(!store.canStageAllAndCommit)
|
|
HStack(spacing: 8) {
|
|
secondaryButton(count > 0 ? "Commit \(count) file\(count == 1 ? "" : "s")" : "Nothing staged",
|
|
command: .commit, hovered: $commitHovered)
|
|
Button { model.perform(.commitAndPush) } label: {
|
|
Text("Commit and push")
|
|
.typeRole(Typography.gitCommitButton)
|
|
.foregroundStyle(store.canCommitNow ? Palette.badgeText : Palette.textMuted)
|
|
.padding(.horizontal, 18)
|
|
.frame(height: GitMetrics.commitButton)
|
|
.background(pushFill, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.pointerHand()
|
|
.tracksHover($pushHovered)
|
|
}
|
|
.disabledLook(count == 0)
|
|
}
|
|
.fixedSize()
|
|
}
|
|
|
|
private func secondaryButton(_ title: String, command: Command, hovered: Binding<Bool>) -> some View {
|
|
Button { model.perform(command) } label: {
|
|
Text(title)
|
|
.typeRole(Typography.gitCommitButton)
|
|
.foregroundStyle(hovered.wrappedValue ? Palette.text1 : Palette.text3)
|
|
.padding(.horizontal, 18)
|
|
.frame(minWidth: GitMetrics.commitButtonMinWidth)
|
|
.frame(height: GitMetrics.commitButton)
|
|
.background(hovered.wrappedValue ? Palette.hover : .clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).strokeBorder(Palette.raised, lineWidth: Metrics.hairline))
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.plain)
|
|
.pointerHand()
|
|
.tracksHover(hovered)
|
|
}
|
|
|
|
private var pushFill: Color {
|
|
if store.canCommitNow { return pushHovered ? Palette.amberDeep : Palette.amber }
|
|
return pushHovered ? Palette.commitIdleHover : Palette.raised
|
|
}
|
|
}
|
|
|
|
/// Plex Mono 14/1.6 message field that grows with its wrapped text from 56 to 180pt, measured in `sizeThatFits`.
|
|
struct GitMessageField: NSViewRepresentable {
|
|
let text: String
|
|
/// A new value takes first responder once, a run loop later: opening the view clears first responder first.
|
|
let focusToken: UUID?
|
|
let onChange: (String) -> Void
|
|
let onFocusChange: (Bool) -> Void
|
|
|
|
func makeCoordinator() -> Coordinator { Coordinator() }
|
|
|
|
func makeNSView(context: Context) -> NSScrollView {
|
|
let scroll = NSScrollView()
|
|
scroll.drawsBackground = false
|
|
scroll.hasVerticalScroller = true
|
|
scroll.autohidesScrollers = true
|
|
scroll.borderType = .noBorder
|
|
|
|
let role = Typography.commitMessage
|
|
let textView = MessageTextView()
|
|
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 = role.nsFont
|
|
textView.textColor = NSColor(hex: 0xF4F5F4)
|
|
textView.insertionPointColor = NSColor(hex: 0xE8913A)
|
|
let padding = textView.textContainer?.lineFragmentPadding ?? 5
|
|
textView.textContainerInset = NSSize(width: GitMetrics.messageInset.width - padding, height: GitMetrics.messageInset.height)
|
|
textView.isVerticallyResizable = true
|
|
textView.isHorizontallyResizable = false
|
|
textView.autoresizingMask = [.width]
|
|
textView.textContainer?.widthTracksTextView = true
|
|
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
|
textView.fillsClipView = true
|
|
let paragraph = NSMutableParagraphStyle()
|
|
paragraph.minimumLineHeight = role.lineHeight ?? 22.4
|
|
paragraph.maximumLineHeight = role.lineHeight ?? 22.4
|
|
textView.defaultParagraphStyle = paragraph
|
|
textView.typingAttributes = [.font: role.nsFont, .foregroundColor: NSColor(hex: 0xF4F5F4), .paragraphStyle: paragraph]
|
|
textView.placeholder = "Commit message"
|
|
textView.string = text
|
|
scroll.documentView = textView
|
|
context.coordinator.textView = textView
|
|
return scroll
|
|
}
|
|
|
|
func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSScrollView, context: Context) -> CGSize? {
|
|
let width = proposal.width ?? nsView.frame.width
|
|
return CGSize(width: width, height: context.coordinator.height(of: text, width: width))
|
|
}
|
|
|
|
/// The field height for `text` at `width`: the wrapped lines plus the insets, kept within 56...180pt.
|
|
static func height(of text: String, width: CGFloat) -> CGFloat {
|
|
let role = Typography.commitMessage
|
|
let paragraph = NSMutableParagraphStyle()
|
|
paragraph.minimumLineHeight = role.lineHeight ?? 22.4
|
|
paragraph.maximumLineHeight = role.lineHeight ?? 22.4
|
|
let storage = NSTextStorage(string: text, attributes: [.font: role.nsFont, .paragraphStyle: paragraph])
|
|
let layout = NSLayoutManager()
|
|
let textWidth = max(1, width - 2 * GitMetrics.messageInset.width)
|
|
let container = NSTextContainer(size: NSSize(width: textWidth, height: .greatestFiniteMagnitude))
|
|
container.lineFragmentPadding = 0
|
|
layout.addTextContainer(container)
|
|
storage.addLayoutManager(layout)
|
|
layout.ensureLayout(for: container)
|
|
let needed = ceil(layout.usedRect(for: container).height + 2 * GitMetrics.messageInset.height)
|
|
return min(max(needed, GitMetrics.messageMin), GitMetrics.messageMax)
|
|
}
|
|
|
|
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
|
let coordinator = context.coordinator
|
|
coordinator.parent = self
|
|
guard let textView = coordinator.textView else { return }
|
|
textView.pointerLayer = context.environment.pointerLayer
|
|
textView.onFocusChange = onFocusChange
|
|
if textView.string != text {
|
|
textView.string = text
|
|
}
|
|
if let focusToken, coordinator.lastFocusToken != focusToken {
|
|
coordinator.lastFocusToken = focusToken
|
|
DispatchQueue.main.async {
|
|
guard let window = textView.window, window.makeFirstResponder(textView) else { return }
|
|
textView.setSelectedRange(NSRange(location: (textView.string as NSString).length, length: 0))
|
|
}
|
|
}
|
|
}
|
|
|
|
final class Coordinator: NSObject, NSTextViewDelegate {
|
|
var parent: GitMessageField?
|
|
weak var textView: MessageTextView?
|
|
var lastFocusToken: UUID?
|
|
private var measured: (text: String, width: CGFloat, height: CGFloat)?
|
|
|
|
func height(of text: String, width: CGFloat) -> CGFloat {
|
|
if let measured, measured.text == text, measured.width == width { return measured.height }
|
|
let height = GitMessageField.height(of: text, width: width)
|
|
measured = (text, width, height)
|
|
return height
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// Draws the placeholder and reports focus.
|
|
final class MessageTextView: PointerBoundTextView {
|
|
var placeholder = ""
|
|
var onFocusChange: ((Bool) -> Void)?
|
|
|
|
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
|
|
}
|
|
|
|
override func draw(_ dirtyRect: NSRect) {
|
|
super.draw(dirtyRect)
|
|
guard string.isEmpty, !placeholder.isEmpty else { return }
|
|
let attributes: [NSAttributedString.Key: Any] = [
|
|
.font: font ?? Typography.commitMessage.nsFont, .foregroundColor: NSColor(hex: 0x6C7783),
|
|
.paragraphStyle: defaultParagraphStyle ?? NSParagraphStyle.default,
|
|
]
|
|
let origin = NSPoint(x: textContainerInset.width + (textContainer?.lineFragmentPadding ?? 0), y: textContainerInset.height)
|
|
(placeholder as NSString).draw(at: origin, withAttributes: attributes)
|
|
}
|
|
}
|