Files
helder/Sources/HelderUI/Overlays/NoteOverlay.swift
T
2026-10-05 10:32:09 +02:00

141 lines
5.8 KiB
Swift

import AppKit
import SwiftUI
/// ⌘N: the project's plain-text note. Closing it in any way saves it.
struct NoteOverlay: View {
let note: NoteStore
var onPassOn: () -> Void
var onClose: () -> Void
var body: some View {
OverlayShell(.sized(width: 780), onDismiss: onClose) {
VStack(spacing: 0) {
OverlayHeader(systemImage: "note.text", title: "Note", detail: NoteStore.fileName) {
Button(action: onPassOn) {
HStack(spacing: 8) {
Text("To the agent")
Keycap("⌘P", style: .onAmber, small: true)
}
}
.buttonStyle(.primary)
}
NoteEditor(text: Binding(get: { note.text }, set: { note.text = $0 }), onKey: handle)
.frame(maxWidth: .infinity, maxHeight: .infinity)
Text("saved on focus loss · \(note.characterCount) characters")
.typeRole(Typography.meta)
.foregroundStyle(Palette.text4)
.padding(.horizontal, 20)
.frame(maxWidth: .infinity, alignment: .leading)
.frame(height: 34)
.hairline(.top)
}
.frame(maxHeight: .infinity)
}
.onDisappear { Task { await note.save() } }
}
private func handle(_ key: OverlayKey) -> Bool {
switch key {
case .commandP: onPassOn()
case .escape: onClose()
default: return false
}
return true
}
}
/// Mono 14/1.7 plain-text editor with an amber caret, focused with the caret at the end.
private struct NoteEditor: NSViewRepresentable {
@Binding var text: String
var onKey: (OverlayKey) -> Bool
func makeCoordinator() -> Coordinator { Coordinator(self) }
func makeNSView(context: Context) -> NSScrollView {
let scroll = NSScrollView()
scroll.drawsBackground = false
scroll.hasVerticalScroller = true
scroll.autohidesScrollers = true
let textView = NoteTextView(frame: .zero)
textView.pointerLayer = .overlay
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
scroll.documentView = textView
let role = Typography.note
let paragraph = NSMutableParagraphStyle()
paragraph.minimumLineHeight = role.lineHeight ?? 23.8
paragraph.maximumLineHeight = role.lineHeight ?? 23.8
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: 0xE4E7E6)
textView.insertionPointColor = NSColor(hex: 0xE8913A)
textView.defaultParagraphStyle = paragraph
textView.typingAttributes = [.font: role.nsFont, .foregroundColor: NSColor(hex: 0xE4E7E6), .paragraphStyle: paragraph]
textView.textContainerInset = NSSize(width: 20, height: 18)
textView.string = text
textView.delegate = context.coordinator
textView.onKey = onKey
textView.placeholder = "Anything you want to keep next to this project…"
DispatchQueue.main.async {
guard let window = textView.window, window.makeFirstResponder(textView) else { return }
textView.setSelectedRange(NSRange(location: (textView.string as NSString).length, length: 0))
textView.scrollRangeToVisible(textView.selectedRange())
}
return scroll
}
func updateNSView(_ scroll: NSScrollView, context: Context) {
context.coordinator.parent = self
guard let textView = scroll.documentView as? NoteTextView else { return }
textView.onKey = onKey
if textView.string != text { textView.string = text }
}
final class Coordinator: NSObject, NSTextViewDelegate {
var parent: NoteEditor
init(_ parent: NoteEditor) {
self.parent = parent
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
parent.text = textView.string
textView.needsDisplay = true
}
func textView(_ textView: NSTextView, doCommandBy selector: Selector) -> Bool {
selector == #selector(NSResponder.cancelOperation(_:)) ? parent.onKey(.escape) : false
}
}
}
/// Catches ⌘P before the menu bar's ⌘P (push) and draws the placeholder.
private final class NoteTextView: PointerBoundTextView {
var onKey: (OverlayKey) -> Bool = { _ in false }
var placeholder = ""
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if window?.firstResponder === self, OverlayKey(event: event) == .commandP, onKey(.commandP) { return true }
return super.performKeyEquivalent(with: event)
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard string.isEmpty, !placeholder.isEmpty else { return }
let attributes: [NSAttributedString.Key: Any] = [.font: font ?? Typography.note.nsFont, .foregroundColor: NSColor(hex: 0x6C7783)]
let origin = NSPoint(x: textContainerInset.width + (textContainer?.lineFragmentPadding ?? 0), y: textContainerInset.height)
(placeholder as NSString).draw(at: origin, withAttributes: attributes)
}
}