import AppKit import HelderCore import SwiftUI /// The message field over the author, the ⌘↵ hint and the Commit split button. 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) { 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) CommitSplitButton(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) } } } /// `Commit N files` plus a caret that opens Commit / Commit and push. Amber only when git would accept it, /// but both parts always run, so git's own refusal shows in the console. private struct CommitSplitButton: View { let model: WorkspaceModel let store: SourceControlStore @State private var mainHovered = false @State private var caretHovered = false @State private var caretFrame = CGRect.zero var body: some View { let ready = store.canCommitNow let count = store.stagedPathCount HStack(spacing: 0) { Button { model.perform(.commit) } label: { Text(count > 0 ? "Commit \(count) file\(count == 1 ? "" : "s")" : "Nothing staged") .typeRole(Typography.gitCommitButton) .padding(.horizontal, 18) .frame(minWidth: GitMetrics.commitButtonMinWidth) .frame(height: GitMetrics.commitButton) .background(fill(ready: ready, hovered: mainHovered)) .contentShape(Rectangle()) } .buttonStyle(.plain) .pointerHand() .tracksHover($mainHovered) Button(action: openMenu) { GitIconView(icon: .chevronDown, size: 14) .frame(width: GitMetrics.commitButton, height: GitMetrics.commitButton) .background(fill(ready: ready, hovered: caretHovered)) .overlay(alignment: .leading) { Rectangle().fill(Palette.editor).frame(width: Metrics.hairline) } .contentShape(Rectangle()) } .buttonStyle(.plain) .pointerHand() .tracksHover($caretHovered) .background(GlobalFrameReader(frame: $caretFrame)) } .foregroundStyle(ready ? Palette.badgeText : Palette.textMuted) .clipShape(RoundedRectangle(cornerRadius: Metrics.radiusSmall)) .fixedSize() } private func fill(ready: Bool, hovered: Bool) -> Color { if ready { return hovered ? Palette.amberDeep : Palette.amber } return hovered ? Palette.commitIdleHover : Palette.raised } /// The menu hangs under the caret, right-aligned to it. private func openMenu() { let anchor = CGPoint(x: caretFrame.maxX - Metrics.contextMenuWidth, y: caretFrame.maxY + 4) model.perform(.showContextMenu(.commitMenu, anchor: anchor)) } } /// 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) } }