Files
helder/Sources/HelderUI/Editor/PassOnPanel.swift
T
2026-10-05 09:24:25 +02:00

112 lines
4.6 KiB
Swift

import HelderCore
import SwiftUI
/// The 460pt prompt: an optional note over the reference it inserts into the agent's composer.
public struct PassOnPanel: View {
let request: PassOnRequest
let insert: (String) -> Void
let cancel: () -> Void
@State private var note = ""
public init(request: PassOnRequest, insert: @escaping (String) -> Void, cancel: @escaping () -> Void) {
self.request = request
self.insert = insert
self.cancel = cancel
}
private var composed: String {
PassOn.compose(note: note, reference: request.reference, code: request.code)
}
public var body: some View {
GeometryReader { geometry in
ZStack(alignment: .topLeading) {
Color.clear
.contentShape(Rectangle())
.onTapGesture(perform: cancel)
panel
.offset(x: min(max(8, request.anchor.x), geometry.size.width - Metrics.passOnWidth - 8),
y: min(max(8, request.anchor.y + 6), geometry.size.height - 170))
}
}
}
private var panel: some View {
VStack(alignment: .leading, spacing: 12) {
HStack(spacing: 10) {
SparkGlyph()
.stroke(Palette.amber, style: StrokeStyle(lineWidth: 1.1 * 13 / 16, lineJoin: .round))
.frame(width: 13, height: 13)
Text("Pass on to agent").typeRole(TypeRole(.monoBold, 13)).foregroundStyle(Palette.text1)
Spacer()
EscChip()
}
OverlayCommandField(text: $note, placeholder: "Add a note (optional)…", font: TypeRole(.monoRegular, 13).nsFont,
initialSelection: nil, horizontalInset: 10, onKey: handle)
.frame(height: 38)
.background(Palette.editor, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).stroke(Palette.amber, lineWidth: Metrics.accentRule))
.padding(Metrics.focusRingWidth)
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall + 2).stroke(Palette.focusRing, lineWidth: Metrics.focusRingWidth))
.padding(-Metrics.focusRingWidth)
HStack(alignment: .top, spacing: 10) {
Text("Inserts").typeRole(Typography.panelLabel).foregroundStyle(Palette.text4).padding(.top, 5)
preview
}
OverlayFooter.hint([("↵", "insert into agent"), ("esc", "cancel")])
.typeRole(Typography.meta)
}
.padding(14)
.frame(width: Metrics.passOnWidth)
.background(Palette.panel, in: RoundedRectangle(cornerRadius: Metrics.radiusMedium))
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusMedium).stroke(Palette.raised, lineWidth: Metrics.hairline))
.shadow(color: Palette.menuShadow, radius: Metrics.menuShadowRadius, y: Metrics.menuShadowY)
}
private func handle(_ key: OverlayKey) -> Bool {
switch key {
case .enter: insert(composed)
case .escape: cancel()
default: return false
}
return true
}
@ViewBuilder
private var preview: some View {
let chip = Text(composed)
.typeRole(Typography.path)
.foregroundStyle(Palette.ink)
.padding(.horizontal, 8)
.padding(.vertical, 4)
if request.code != nil {
ScrollView {
chip.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxHeight: 132)
.fixedSize(horizontal: false, vertical: true)
.background(Palette.amber, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
} else {
chip.lineLimit(1)
.truncationMode(.tail)
.background(Palette.amber, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
}
}
}
/// The four-point spark that marks every "Pass on" action, drawn on a 16-unit grid.
struct SparkGlyph: Shape {
func path(in rect: CGRect) -> Path {
let unit = min(rect.width, rect.height) / 16
let points: [(CGFloat, CGFloat)] = [(8, 1.5), (9.6, 6.4), (14.5, 8), (9.6, 9.6), (8, 14.5), (6.4, 9.6), (1.5, 8), (6.4, 6.4)]
var path = Path()
for (index, point) in points.enumerated() {
let p = CGPoint(x: rect.minX + point.0 * unit, y: rect.minY + point.1 * unit)
if index == 0 { path.move(to: p) } else { path.addLine(to: p) }
}
path.closeSubpath()
return path
}
}