adds nice git interface
This commit is contained in:
@@ -0,0 +1,386 @@
|
||||
import AppKit
|
||||
import HelderCore
|
||||
import SwiftUI
|
||||
|
||||
/// The real git commands and their output, then a `$ git` input line. A click that selects nothing focuses the input.
|
||||
/// The real git commands and their output, then a `$ git` input line. A click that selects nothing focuses the input.
|
||||
struct GitConsolePanel: View {
|
||||
let model: WorkspaceModel
|
||||
let store: SourceControlStore
|
||||
@State private var focusTick = 0
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
header
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
// First pick of the height: the log takes what its lines need, the input sits right under it.
|
||||
ConsoleLogView(entries: store.lines, onPlainClick: { focusTick += 1 })
|
||||
.layoutPriority(1)
|
||||
// An AppKit field has no text baseline for SwiftUI, so the row centres instead.
|
||||
HStack(alignment: .center, spacing: 8) {
|
||||
Text("$ git").typeRole(Typography.console).foregroundStyle(Palette.gitNew).lineLimit(1).fixedSize()
|
||||
ConsoleInputField(
|
||||
text: Binding(get: { store.consoleInput }, set: { store.consoleInput = $0 }),
|
||||
focusTick: focusTick,
|
||||
onSubmit: { model.perform(.submitConsoleInput) },
|
||||
onUp: { store.recallPreviousInput() }
|
||||
)
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
.frame(height: Self.inputHeight)
|
||||
Color.clear
|
||||
.frame(maxWidth: .infinity, minHeight: 12, maxHeight: .infinity)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture { focusTick += 1 }
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
}
|
||||
}
|
||||
|
||||
private static let inputHeight: CGFloat = 19.2
|
||||
|
||||
private var header: some View {
|
||||
HStack(spacing: 12) {
|
||||
Text("[ console ] git").typeRole(Typography.panelLabel).foregroundStyle(Palette.text4).fixedSize()
|
||||
if let busy = store.busy {
|
||||
Text(busy == .push ? "→ pushing to origin…" : "→ pulling from origin…")
|
||||
.typeRole(Typography.path)
|
||||
.foregroundStyle(Palette.amber)
|
||||
.lineLimit(1)
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
EraserButton { store.clearConsole() }
|
||||
}
|
||||
.padding(.leading, 16)
|
||||
.padding(.trailing, 12)
|
||||
.frame(height: GitMetrics.stripHeader)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture { focusTick += 1 }
|
||||
}
|
||||
}
|
||||
|
||||
private struct EraserButton: View {
|
||||
let action: () -> Void
|
||||
@State private var isHovered = false
|
||||
|
||||
var body: some View {
|
||||
Button(action: action) {
|
||||
GitIconView(icon: .eraser, size: 14)
|
||||
.foregroundStyle(isHovered ? Palette.text1 : Palette.textMuted)
|
||||
.padding(4)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.pointerHand()
|
||||
.tracksHover($isHovered)
|
||||
.tip("Clear", edge: .trailing)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Log
|
||||
|
||||
/// A read-only, selectable text view of the console lines; it follows the newest line.
|
||||
/// Its height is measured in `sizeThatFits`, so the input row follows the last line in the same layout pass.
|
||||
private struct ConsoleLogView: NSViewRepresentable {
|
||||
let entries: [GitConsoleEntry]
|
||||
let onPlainClick: () -> Void
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator() }
|
||||
|
||||
func makeNSView(context: Context) -> ConsoleLogScrollView {
|
||||
let scroll = ConsoleLogScrollView()
|
||||
scroll.drawsBackground = false
|
||||
scroll.hasVerticalScroller = true
|
||||
scroll.autohidesScrollers = true
|
||||
scroll.borderType = .noBorder
|
||||
let textView = ConsoleLogTextView(frame: NSRect(x: 0, y: 0, width: 100, height: 0))
|
||||
textView.isEditable = false
|
||||
textView.isSelectable = true
|
||||
textView.drawsBackground = false
|
||||
textView.isRichText = true
|
||||
textView.textContainerInset = .zero
|
||||
textView.textContainer?.lineFragmentPadding = 0
|
||||
textView.isVerticallyResizable = true
|
||||
textView.isHorizontallyResizable = false
|
||||
textView.autoresizingMask = [.width]
|
||||
textView.textContainer?.widthTracksTextView = true
|
||||
// The container starts as tall as the zero-height frame, which lays out no line at all.
|
||||
textView.textContainer?.containerSize = NSSize(width: 100, height: CGFloat.greatestFiniteMagnitude)
|
||||
textView.minSize = .zero
|
||||
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
||||
textView.selectedTextAttributes = [.backgroundColor: NSColor(hex: 0x3A424C)]
|
||||
scroll.documentView = textView
|
||||
return scroll
|
||||
}
|
||||
|
||||
func updateNSView(_ scroll: ConsoleLogScrollView, context: Context) {
|
||||
guard let textView = scroll.documentView as? ConsoleLogTextView else { return }
|
||||
textView.pointerLayer = context.environment.pointerLayer
|
||||
textView.onPlainClick = onPlainClick
|
||||
let coordinator = context.coordinator
|
||||
let text = coordinator.text(for: entries)
|
||||
guard coordinator.applied !== text else { return }
|
||||
coordinator.applied = text
|
||||
textView.textStorage?.setAttributedString(text)
|
||||
textView.scrollToEndOfDocument(nil)
|
||||
}
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: ConsoleLogScrollView, context: Context) -> CGSize? {
|
||||
let width = proposal.width ?? nsView.frame.width
|
||||
let height = entries.isEmpty ? 0 : context.coordinator.height(of: entries, width: width)
|
||||
return CGSize(width: width, height: min(height, proposal.height ?? height))
|
||||
}
|
||||
|
||||
final class Coordinator {
|
||||
private struct Signature: Equatable {
|
||||
var count: Int
|
||||
var first: Int?
|
||||
var last: Int?
|
||||
}
|
||||
|
||||
var applied: NSAttributedString?
|
||||
private var signature: Signature?
|
||||
private var cached = NSAttributedString()
|
||||
private var measured: (width: CGFloat, height: CGFloat)?
|
||||
|
||||
/// Rebuilt only when lines were added or dropped, since one console holds up to 400 of them.
|
||||
func text(for entries: [GitConsoleEntry]) -> NSAttributedString {
|
||||
let next = Signature(count: entries.count, first: entries.first?.id, last: entries.last?.id)
|
||||
if next != signature {
|
||||
signature = next
|
||||
cached = Self.build(entries)
|
||||
measured = nil
|
||||
}
|
||||
return cached
|
||||
}
|
||||
|
||||
func height(of entries: [GitConsoleEntry], width: CGFloat) -> CGFloat {
|
||||
let text = text(for: entries)
|
||||
if let measured, measured.width == width { return measured.height }
|
||||
let storage = NSTextStorage(attributedString: text)
|
||||
let layout = NSLayoutManager()
|
||||
let container = NSTextContainer(size: NSSize(width: max(width, 1), height: .greatestFiniteMagnitude))
|
||||
container.lineFragmentPadding = 0
|
||||
layout.addTextContainer(container)
|
||||
storage.addLayoutManager(layout)
|
||||
layout.ensureLayout(for: container)
|
||||
let height = ceil(layout.usedRect(for: container).height)
|
||||
measured = (width, height)
|
||||
return height
|
||||
}
|
||||
|
||||
private static func build(_ entries: [GitConsoleEntry]) -> NSAttributedString {
|
||||
let role = Typography.console
|
||||
let paragraph = NSMutableParagraphStyle()
|
||||
paragraph.minimumLineHeight = role.lineHeight ?? 19.2
|
||||
paragraph.maximumLineHeight = role.lineHeight ?? 19.2
|
||||
paragraph.lineBreakMode = .byCharWrapping
|
||||
paragraph.tabStops = []
|
||||
paragraph.defaultTabInterval = 4 * (" " as NSString).size(withAttributes: [.font: role.nsFont]).width
|
||||
// AppKit puts a fixed line's spare height above the glyphs; this centres them like the `$ git` row.
|
||||
let spare = paragraph.maximumLineHeight - NSLayoutManager().defaultLineHeight(for: role.nsFont)
|
||||
let out = NSMutableAttributedString()
|
||||
for (index, entry) in entries.enumerated() {
|
||||
let line = entry.line.text + (index == entries.count - 1 ? "" : "\n")
|
||||
out.append(NSAttributedString(string: line, attributes: [
|
||||
.font: role.nsFont, .foregroundColor: NSColor(Palette.console(entry.line.kind)), .paragraphStyle: paragraph,
|
||||
.baselineOffset: max(0, spare / 2),
|
||||
]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Grows with each new line; every resize lands on the newest line.
|
||||
private final class ConsoleLogScrollView: NSScrollView {
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
let grew = newSize.height != frame.height
|
||||
super.setFrameSize(newSize)
|
||||
if grew { documentView?.scrollToEndOfDocument(nil) }
|
||||
}
|
||||
}
|
||||
|
||||
/// A click that leaves no selection hands the keyboard to the input line.
|
||||
private final class ConsoleLogTextView: PointerBoundTextView {
|
||||
var onPlainClick: () -> Void = {}
|
||||
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
super.mouseDown(with: event)
|
||||
if selectedRange().length == 0 { onPlainClick() }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Input
|
||||
|
||||
/// The borderless input after `$ git`: ↵ runs it, ↑ recalls the last command. It never takes focus on its own.
|
||||
private struct ConsoleInputField: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
let focusTick: Int
|
||||
let onSubmit: () -> Void
|
||||
let onUp: () -> Void
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator(self) }
|
||||
|
||||
func makeNSView(context: Context) -> ConsoleFieldBox {
|
||||
let font = Typography.console.nsFont
|
||||
let field = NSTextField()
|
||||
field.cell = ConsoleFieldCell(textCell: "")
|
||||
field.isBordered = false
|
||||
field.isEditable = true
|
||||
field.isSelectable = true
|
||||
field.drawsBackground = false
|
||||
field.focusRingType = .none
|
||||
field.font = font
|
||||
field.textColor = NSColor(hex: 0xF4F5F4)
|
||||
field.placeholderAttributedString = NSAttributedString(
|
||||
string: "status · add -A · commit -m \"…\" · push · pull · log",
|
||||
attributes: [.foregroundColor: NSColor(hex: 0x6C7783), .font: font]
|
||||
)
|
||||
field.stringValue = text
|
||||
field.delegate = context.coordinator
|
||||
field.cell?.isScrollable = true
|
||||
field.cell?.wraps = false
|
||||
context.coordinator.lastFocusTick = focusTick
|
||||
return ConsoleFieldBox(field: field)
|
||||
}
|
||||
|
||||
func updateNSView(_ box: ConsoleFieldBox, context: Context) {
|
||||
let coordinator = context.coordinator
|
||||
coordinator.parent = self
|
||||
box.pointerLayer = context.environment.pointerLayer
|
||||
(box.field.cell as? ConsoleFieldCell)?.pointerLayer = context.environment.pointerLayer
|
||||
if box.field.stringValue != text {
|
||||
box.field.stringValue = text
|
||||
if let editor = box.field.currentEditor() {
|
||||
editor.selectedRange = NSRange(location: (text as NSString).length, length: 0)
|
||||
}
|
||||
}
|
||||
if coordinator.lastFocusTick != focusTick {
|
||||
coordinator.lastFocusTick = focusTick
|
||||
DispatchQueue.main.async { box.focus() }
|
||||
}
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextFieldDelegate {
|
||||
var parent: ConsoleInputField
|
||||
var lastFocusTick = 0
|
||||
|
||||
init(_ parent: ConsoleInputField) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
func controlTextDidChange(_ note: Notification) {
|
||||
guard let field = note.object as? NSTextField else { return }
|
||||
parent.text = field.stringValue
|
||||
}
|
||||
|
||||
func control(_ control: NSControl, textView: NSTextView, doCommandBy selector: Selector) -> Bool {
|
||||
switch selector {
|
||||
case #selector(NSResponder.insertNewline(_:)):
|
||||
parent.onSubmit()
|
||||
return true
|
||||
case #selector(NSResponder.moveUp(_:)):
|
||||
parent.onUp()
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The box around the console input: it takes the I-beam on the policy's terms and focuses the field on a click.
|
||||
private final class ConsoleFieldBox: NSView, PointerTextTarget {
|
||||
let field: NSTextField
|
||||
var pointerLayer = PointerLayer.overlay {
|
||||
didSet { if pointerLayer != oldValue { PointerPolicy.setNeedsRefresh(in: window) } }
|
||||
}
|
||||
private lazy var pointerTracker = PointerTracker(view: self)
|
||||
|
||||
init(field: NSTextField) {
|
||||
self.field = field
|
||||
super.init(frame: .zero)
|
||||
addSubview(field)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("ConsoleFieldBox is built in code")
|
||||
}
|
||||
|
||||
func focus() {
|
||||
guard let window, field.currentEditor() == nil else { return }
|
||||
window.makeFirstResponder(field)
|
||||
field.currentEditor()?.selectedRange = NSRange(location: (field.stringValue as NSString).length, length: 0)
|
||||
}
|
||||
|
||||
override func layout() {
|
||||
super.layout()
|
||||
let height = min(field.intrinsicContentSize.height, bounds.height)
|
||||
field.frame = NSRect(x: 0, y: ((bounds.height - height) / 2).rounded(), width: bounds.width, height: height)
|
||||
}
|
||||
|
||||
var acceptsPointerText: Bool { field.isEnabled }
|
||||
|
||||
func isPointerOverText(atWindowPoint point: NSPoint) -> Bool {
|
||||
isTopmost(atWindowPoint: point)
|
||||
}
|
||||
|
||||
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||
|
||||
override func resetCursorRects() {
|
||||
addPointerCursorRect(bounds)
|
||||
}
|
||||
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
override func setFrameOrigin(_ newOrigin: NSPoint) {
|
||||
super.setFrameOrigin(newOrigin)
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
super.setFrameSize(newSize)
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
pointerTracker.update()
|
||||
}
|
||||
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
focus()
|
||||
}
|
||||
}
|
||||
|
||||
/// Edits through a PointerBoundTextView with the amber caret, instead of the window's shared field editor.
|
||||
private final class ConsoleFieldCell: NSTextFieldCell {
|
||||
var pointerLayer = PointerLayer.overlay {
|
||||
didSet { editor.pointerLayer = pointerLayer }
|
||||
}
|
||||
|
||||
private lazy var editor: GitConsoleFieldEditor = {
|
||||
let editor = GitConsoleFieldEditor()
|
||||
editor.isFieldEditor = true
|
||||
editor.pointerLayer = pointerLayer
|
||||
editor.insertionPointColor = NSColor(hex: 0xE8913A)
|
||||
return editor
|
||||
}()
|
||||
|
||||
override func fieldEditor(for controlView: NSView) -> NSTextView? {
|
||||
editor
|
||||
}
|
||||
|
||||
override func setUpFieldEditorAttributes(_ textObj: NSText) -> NSText {
|
||||
let text = super.setUpFieldEditorAttributes(textObj)
|
||||
(text as? NSTextView)?.insertionPointColor = NSColor(hex: 0xE8913A)
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
/// The console input's field editor; the key router tells the console apart from the message field by it.
|
||||
final class GitConsoleFieldEditor: PointerBoundTextView {}
|
||||
Reference in New Issue
Block a user