264 lines
11 KiB
Swift
264 lines
11 KiB
Swift
import HelderCore
|
|
import SwiftUI
|
|
|
|
/// The full-screen side-by-side Diff: Original left, Actual right, over the whole window.
|
|
/// Its right-click menu and pass-on prompt are drawn inside it, since a second overlay would replace it.
|
|
struct DiffOverlayView: View {
|
|
let editor: EditorStore
|
|
/// Full screen hides the traffic lights, so the header reclaims their inset.
|
|
let isFullScreen: Bool
|
|
/// View → Zoom.
|
|
let scale: CGFloat
|
|
let request: DiffOverlayRequest
|
|
/// Every menu action except pass-on, which stays inside the Diff.
|
|
let perform: (ContextAction) -> Void
|
|
/// The composed text and the reference for the toast.
|
|
let passToAgent: (String, String) -> Void
|
|
|
|
@State private var menu: ContextMenuRequest?
|
|
@State private var highlighted: Int?
|
|
@State private var passOn: PassOnRequest?
|
|
@State private var canvasFocus = 0
|
|
|
|
var body: some View {
|
|
GeometryReader { geometry in
|
|
let origin = geometry.frame(in: .global).origin
|
|
ZStack(alignment: .topLeading) {
|
|
content
|
|
if let menu {
|
|
DiffContextMenu(request: menu, origin: origin, size: geometry.size, highlighted: $highlighted,
|
|
choose: choose, close: closeMenu)
|
|
}
|
|
if let passOn {
|
|
PassOnPanel(request: local(passOn, origin: origin),
|
|
insert: { text in
|
|
passToAgent(text, passOn.reference)
|
|
closePassOn()
|
|
},
|
|
cancel: closePassOn)
|
|
}
|
|
}
|
|
}
|
|
.onExitCommand(perform: escape)
|
|
.onChange(of: menu != nil || passOn != nil, initial: true) { _, busy in editor.diffOverlayBusy = busy }
|
|
.onDisappear { editor.diffOverlayBusy = false }
|
|
}
|
|
|
|
private var content: some View {
|
|
let diff = editor.overlayDiff(for: request.path, side: request.side)
|
|
return VStack(spacing: 0) {
|
|
header(diff)
|
|
HStack(spacing: 0) {
|
|
paneLabel("Original", note: "before", color: Palette.text4)
|
|
Rectangle().fill(Palette.raised).frame(width: Metrics.hairline)
|
|
paneLabel("Actual", note: "after", color: Palette.amber)
|
|
}
|
|
.frame(height: Metrics.panelHeader)
|
|
.overlay(alignment: .bottom) { Rectangle().fill(Palette.raised).frame(height: Metrics.hairline) }
|
|
if let diff, diff.isTooBig {
|
|
Text("Too big.")
|
|
.typeRole(Typography.body)
|
|
.foregroundStyle(Palette.text4)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else if let diff {
|
|
DiffCanvas(rows: diff.split, language: FileType.language(for: request.path), knownSymbols: editor.knownSymbols,
|
|
key: key(diff), focusLine: request.focusLine, scale: scale, focusRequest: canvasFocus,
|
|
onKey: handle, contextMenu: openMenu)
|
|
} else {
|
|
Text("— No changes to compare.")
|
|
.typeRole(Typography.body)
|
|
.foregroundStyle(Palette.text3)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
}
|
|
.background(Palette.editor)
|
|
}
|
|
|
|
// MARK: Menu and prompt
|
|
|
|
private func openMenu(line: Int, anchor: CGPoint) {
|
|
guard passOn == nil else { return }
|
|
let reference = AgentReference(path: request.path, startLine: line)
|
|
let next = editor.contextMenuRequest(reference: reference, code: nil, anchor: anchor)
|
|
menu = next
|
|
highlighted = MenuCursor.first(in: next.entries)
|
|
}
|
|
|
|
private func choose(_ action: ContextAction) {
|
|
let anchor = menu?.anchor ?? .zero
|
|
menu = nil
|
|
if case let .passOnReference(reference, code) = action {
|
|
passOn = PassOnRequest(reference: reference, code: code, anchor: anchor)
|
|
} else {
|
|
perform(action)
|
|
}
|
|
}
|
|
|
|
private func closeMenu() {
|
|
menu = nil
|
|
}
|
|
|
|
private func closePassOn() {
|
|
passOn = nil
|
|
canvasFocus += 1
|
|
}
|
|
|
|
/// Esc closes the innermost layer first: the menu, then the prompt, then the Diff itself.
|
|
private func escape() {
|
|
if menu != nil {
|
|
closeMenu()
|
|
} else if passOn != nil {
|
|
closePassOn()
|
|
} else {
|
|
editor.collapseDiff()
|
|
}
|
|
}
|
|
|
|
private func handle(_ key: DiffCanvasKey) -> Bool {
|
|
if key == .escape {
|
|
escape()
|
|
return true
|
|
}
|
|
guard let menu else { return false }
|
|
switch key {
|
|
case .up: highlighted = MenuCursor.step(highlighted ?? -1, by: -1, in: menu.entries)
|
|
case .down: highlighted = MenuCursor.step(highlighted ?? -1, by: 1, in: menu.entries)
|
|
case .enter:
|
|
if let highlighted, case let .action(action, _) = menu.entries[highlighted] { choose(action) }
|
|
case .escape: break
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// The prompt lays itself out in this view's space, not the window's.
|
|
private func local(_ request: PassOnRequest, origin: CGPoint) -> PassOnRequest {
|
|
var moved = request
|
|
moved.anchor = CGPoint(x: request.anchor.x - origin.x, y: request.anchor.y - origin.y)
|
|
return moved
|
|
}
|
|
|
|
private func key(_ diff: Diff) -> String {
|
|
"\(request.path)|\(request.side?.rawValue ?? "file")|\(request.focusLine ?? 0)|\(diff.added).\(diff.removed).\(diff.split.count).\(diff.updated.utf8.count)"
|
|
}
|
|
|
|
private func header(_ diff: Diff?) -> some View {
|
|
HStack(spacing: 14) {
|
|
TypeBadge(path: request.path)
|
|
Text(request.path)
|
|
.typeRole(TypeRole(.monoMedium, 12))
|
|
.foregroundStyle(Palette.text1)
|
|
.lineLimit(1)
|
|
.truncationMode(.head)
|
|
if let diff {
|
|
let status = FileChangeStatus(diff)
|
|
Text(status.rawValue).typeRole(Typography.panelLabel).foregroundStyle(color(status))
|
|
if diff.added > 0 { Text("+\(diff.added)").typeRole(TypeRole(.monoMedium, 12)).foregroundStyle(Palette.addedText) }
|
|
if diff.removed > 0 { Text("\u{2212}\(diff.removed)").typeRole(TypeRole(.monoMedium, 12)).foregroundStyle(Palette.removedText) }
|
|
}
|
|
if editor.hasBothSides(request.path) {
|
|
Text(request.side == .unstaged ? "staged → actual" : "HEAD → staged")
|
|
.typeRole(Typography.meta)
|
|
.foregroundStyle(Palette.text4)
|
|
}
|
|
Spacer(minLength: 0)
|
|
CollapseButton { editor.collapseDiff() }
|
|
}
|
|
.padding(.leading, isFullScreen ? 16 : Metrics.trafficLightInset)
|
|
.padding(.trailing, 16)
|
|
.frame(height: 42)
|
|
.background(Palette.panel)
|
|
.overlay(alignment: .bottom) { Rectangle().fill(Palette.raised).frame(height: Metrics.hairline) }
|
|
}
|
|
|
|
private func paneLabel(_ title: String, note: String, color: Color) -> some View {
|
|
HStack(spacing: 10) {
|
|
Text(title).typeRole(Typography.panelLabel).foregroundStyle(color)
|
|
Text(note).typeRole(Typography.meta).foregroundStyle(Palette.text5)
|
|
Spacer(minLength: 0)
|
|
}
|
|
.padding(.horizontal, 16)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
|
|
private func color(_ status: FileChangeStatus) -> Color {
|
|
switch status {
|
|
case .added: Palette.addedText
|
|
case .deleted: Palette.removedText
|
|
case .modified: Palette.text4
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The editor menu (Pass on reference, Copy reference) at the pointer, over the Diff.
|
|
private struct DiffContextMenu: View {
|
|
let request: ContextMenuRequest
|
|
/// The Diff's top-left corner in window coordinates.
|
|
let origin: CGPoint
|
|
let size: CGSize
|
|
@Binding var highlighted: Int?
|
|
let choose: (ContextAction) -> Void
|
|
let close: () -> Void
|
|
|
|
var body: some View {
|
|
ZStack(alignment: .topLeading) {
|
|
Color.clear
|
|
.contentShape(Rectangle())
|
|
.onTapGesture(perform: close)
|
|
ContextMenuPanel {
|
|
ContextMenuHeader(request.header)
|
|
ForEach(Array(request.entries.enumerated()), id: \.offset) { index, entry in
|
|
switch entry {
|
|
case .separator:
|
|
ContextMenuSeparator()
|
|
case let .action(action, label):
|
|
ContextMenuRow(label, systemImage: ContextMenuOverlay.symbol(for: action),
|
|
iconTint: ContextMenuOverlay.tint(for: action).color,
|
|
isHighlighted: highlighted == index, isDestructive: action.isDestructive)
|
|
.onHover { inside in if inside { highlighted = index } }
|
|
.onTapGesture { choose(action) }
|
|
case let .disabled(action, label, _):
|
|
ContextMenuRow(label, systemImage: ContextMenuOverlay.symbol(for: action),
|
|
iconTint: ContextMenuOverlay.tint(for: action).color,
|
|
isHighlighted: false, isDisabled: true)
|
|
}
|
|
}
|
|
}
|
|
.fixedSize()
|
|
.offset(position)
|
|
}
|
|
}
|
|
|
|
/// Keeps the menu inside the Diff.
|
|
private var position: CGSize {
|
|
let height = CGFloat(request.entries.count) * Metrics.contextMenuItem + 60
|
|
let x = min(request.anchor.x - origin.x, size.width - Metrics.contextMenuWidth - 8)
|
|
let y = min(request.anchor.y - origin.y, size.height - height)
|
|
return CGSize(width: max(8, x), height: max(8, y))
|
|
}
|
|
}
|
|
|
|
private struct CollapseButton: View {
|
|
let action: () -> Void
|
|
@State private var hovering = false
|
|
|
|
var body: some View {
|
|
HStack(spacing: 8) {
|
|
Image(systemName: "arrow.down.right.and.arrow.up.left")
|
|
.font(.system(size: 11, weight: .regular))
|
|
Text("Collapse").typeRole(Typography.toolbarLabel)
|
|
Keycap("Esc")
|
|
}
|
|
.foregroundStyle(hovering ? Palette.text1 : Palette.text3)
|
|
.padding(.leading, 9)
|
|
.padding(.trailing, 4)
|
|
.padding(.vertical, 3)
|
|
.background(hovering ? Palette.hover : Color.clear, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
|
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).stroke(Palette.raised, lineWidth: Metrics.hairline))
|
|
.contentShape(Rectangle())
|
|
.pointerHand()
|
|
.onTapGesture(perform: action)
|
|
.onHover { hovering = $0 }
|
|
.animation(Metrics.colorTransition, value: hovering)
|
|
}
|
|
}
|